Sha256: 66413b2ddc9bb1ce6be4fd455918ef14042f02a9bb9e334638ffe7dd271fe3ac

Contents?: true

Size: 1.48 KB

Versions: 10

Compression:

Stored size: 1.48 KB

Contents

module Finitio
  #
  # A BuiltinType generator allows capuring an information type to a type of
  # the host language, here a Ruby class. For instance,
  #
  #     Int := BuiltinType(ruby.Fixnum)
  #
  # The set of values captured by the information type is the same set of
  # values that can be represented by the host type. In the example, `Int`
  # captures the same set of numbers as ruby's Fixnum.
  #
  # The ruby class is used as concrete representation of the information type.
  # In the example:
  #
  #     R(Int) = Fixnum
  #
  # Accordingly, the `dress` transformation function has the following signature:
  #
  #     dress :: Alpha  -> Int    throws TypeError
  #     dress :: Object -> Fixnum throws TypeError
  #
  class BuiltinType < Type

    def initialize(ruby_type, name = nil, metadata = nil)
      super(name, metadata)
      @ruby_type = ruby_type
    end
    attr_reader :ruby_type

    alias :representator :ruby_type

    def default_name
      @ruby_type.name.to_s
    end

    def include?(value)
      ruby_type === value
    end

    # Check that `value` is a valid instance of `ruby_type` through `===` or
    # raise an error.
    def dress(value, handler = DressHelper.new)
      handler.failed!(self, value) unless ruby_type===value
      value
    end

    def ==(other)
      super || (other.is_a?(BuiltinType) && other.ruby_type==ruby_type)
    end
    alias :eql? :==

    def hash
      self.class.hash ^ ruby_type.hash
    end

  end # class BuiltinType
end # module Finitio

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
finitio-0.7.0 lib/finitio/type/builtin_type.rb
finitio-0.7.0.pre.rc4 lib/finitio/type/builtin_type.rb
finitio-0.7.0.pre.rc3 lib/finitio/type/builtin_type.rb
finitio-0.7.0.pre.rc2 lib/finitio/type/builtin_type.rb
finitio-0.7.0.pre.rc1 lib/finitio/type/builtin_type.rb
finitio-0.6.1 lib/finitio/type/builtin_type.rb
finitio-0.6.0 lib/finitio/type/builtin_type.rb
finitio-0.5.2 lib/finitio/type/builtin_type.rb
finitio-0.5.1 lib/finitio/type/builtin_type.rb
finitio-0.5.0 lib/finitio/type/builtin_type.rb