Sha256: 42baa39f7d80332db4436453e5aa26b772f597f9489255fdfcdc27f445754fca

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

module Finitio
  class Inference

    def initialize(system, options = {})
      @system = system
      @options = options
    end
    attr_reader :system

    def call(input)
      infer_type(input)
    end

  private

    def infer_type(value)
      case value
      when Hash
        attrs = value.map{|k,v|
          Attribute.new(k.to_sym, infer_type(v))
        }
        heading = Heading.new(attrs)
        TupleType.new(heading)
      when Array
        infered = value.inject(nil){|sup, value|
          value_type = infer_type(value)
          sup.nil? ? value_type : sup.suppremum(value_type)
        }
        SeqType.new(infered.nil? ? ANY_TYPE : infered)
      else
        found = self.system.types.values.find{|t|
          try_dress(t, value)
        }
        found ? found : ANY_TYPE
      end
    end

    def try_dress(t, value)
      raise "Type expected, got #{t}" unless t.is_a?(Type)
      t.dress(value)
      true
    rescue Finitio::Error => ex
      #puts %Q{[#{ex.class}] #{ex.message}}
      nil
    rescue => ex
      puts %Q{[#{ex.class}] #{ex.message}\n#{ex.backtrace.join("\n")}}
      nil
    end

  end # class Inference
end # module Finitio

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
finitio-0.12.0 lib/finitio/inference.rb
finitio-0.11.4 lib/finitio/inference.rb
finitio-0.11.3 lib/finitio/inference.rb
finitio-0.11.2 lib/finitio/inference.rb
finitio-0.11.1 lib/finitio/inference.rb
finitio-0.10.0 lib/finitio/inference.rb
finitio-0.9.1 lib/finitio/inference.rb
finitio-0.9.0 lib/finitio/inference.rb
finitio-0.8.0 lib/finitio/inference.rb