module Dhallish::Types

Constants

Bool
Double
DoubleList
Integer
IntegerList
Natural
NaturalList
Numbers
Text
TextList

Public Class Methods

is_a_type?(x) click to toggle source
# File lib/types.rb, line 280
def is_a_type?(x)
        !not_a_type?(x)
end
not_a_type?(x) click to toggle source
# File lib/types.rb, line 271
def not_a_type?(x)
        if x.is_a? Symbol
                x != Natural and x != Integer and x != Double and x != Bool and x != Text
        else
                !x.is_a? Optional and !x.is_a? List and !x.is_a? Record and !x.is_a? Function and !x.is_a? Unresolved and !x.is_a? Type and !x.is_a? Union
        end
end
resolve(orgtype, name, newtype) click to toggle source
# File lib/types.rb, line 152
def resolve(orgtype, name, newtype)
        case orgtype
        when Type
                Type.new(resolve(orgtype.metadata, name, newtype))
        when Unresolved
                if name == orgtype.name
                        newtype
                else
                        orgtype
                end
        when Function
                if orgtype.restype.nil?
                        Function.new(resolve(orgtype.argtype, name, newtype), nil, orgtype.unres)
                else
                        restype = orgtype.restype
                        argtype = orgtype.argtype
                        if orgtype.unres.nil? or orgtype.unres != name
                                restype = resolve(orgtype.restype, name, newtype)
                                argtype = resolve(orgtype.argtype, name, newtype)
                        end
                        Function.new(argtype, restype, orgtype.unres)
                end
        when Optional
                Optional.new(resolve(orgtype.type, name, newtype))
        when List
                List.new(resolve(orgtype.type, name, newtype))
        when Record
                Record.new(orgtype.types.map{ |key, val| [key, resolve(val, name, newtype)] }.to_h)
        when Union
                Union.new(orgtype.types.map{ |key, val| [key, resolve(val, name, newtype)] }.to_h)
        else
                orgtype
        end
end
unification(a, b, mapping={}) click to toggle source
# File lib/types.rb, line 188
def unification(a, b, mapping={})
        case a
        when Type
                if b.is_a? Type
                        unification(a.metadata, b.metadata, mapping)
                else
                        nil
                end
        when Unresolved
                if b.is_a? Unresolved
                        if mapping[a.name] == nil
                                mapping[a.name] = b.name
                                mapping
                        elsif mapping[a.name] != b.name
                                nil
                        else
                                mapping
                        end
                else
                        nil
                end
        when Function
                if b.is_a? Function
                        if unification(a.argtype, b.argtype, mapping).nil?
                                nil
                        else
                                arestype = a.restype
                                brestype = b.restype
                                if !a.unres.nil?; arestype = resolve(a.restype, a.unres, Unresolved.new(get_new_sym())) end
                                if !b.unres.nil?; brestype = resolve(b.restype, b.unres, Unresolved.new(get_new_sym())) end
                                unification(arestype, brestype, mapping)
                        end
                else
                        nil
                end
        when Optional
                if b.is_a? Optional
                        unification(a.type, b.type, mapping)
                else
                        nil
                end
        when List
                if b.is_a? List
                        unification(a.type, b.type, mapping)
                else
                        nil
                end
        when Record
                if b.is_a? Record and a.types.keys.length == b.types.keys.length
                        for key in a.types.keys
                                aelm = a.types[key]
                                belm = b.types[key]
                                if belm.nil?; return nil end
                                ret = unification(aelm, belm, mapping)
                                if ret.nil?; return nil end
                        end
                        mapping
                else
                        nil
                end
        when Union
                if b.is_a? Union and a.types.keys.length == b.types.keys.length
                        for key in a.types.keys
                                aelm = a.types[key]
                                belm = b.types[key]
                                if belm.nil?; return nil end
                                ret = unification(aelm, belm, mapping)
                                if ret.nil?; return nil end
                        end
                        mapping
                else
                        nil
                end
        else
                if a == b
                        mapping
                else
                        nil
                end
        end
end

Private Instance Methods

is_a_type?(x) click to toggle source
# File lib/types.rb, line 280
def is_a_type?(x)
        !not_a_type?(x)
end
not_a_type?(x) click to toggle source
# File lib/types.rb, line 271
def not_a_type?(x)
        if x.is_a? Symbol
                x != Natural and x != Integer and x != Double and x != Bool and x != Text
        else
                !x.is_a? Optional and !x.is_a? List and !x.is_a? Record and !x.is_a? Function and !x.is_a? Unresolved and !x.is_a? Type and !x.is_a? Union
        end
end
resolve(orgtype, name, newtype) click to toggle source
# File lib/types.rb, line 152
def resolve(orgtype, name, newtype)
        case orgtype
        when Type
                Type.new(resolve(orgtype.metadata, name, newtype))
        when Unresolved
                if name == orgtype.name
                        newtype
                else
                        orgtype
                end
        when Function
                if orgtype.restype.nil?
                        Function.new(resolve(orgtype.argtype, name, newtype), nil, orgtype.unres)
                else
                        restype = orgtype.restype
                        argtype = orgtype.argtype
                        if orgtype.unres.nil? or orgtype.unres != name
                                restype = resolve(orgtype.restype, name, newtype)
                                argtype = resolve(orgtype.argtype, name, newtype)
                        end
                        Function.new(argtype, restype, orgtype.unres)
                end
        when Optional
                Optional.new(resolve(orgtype.type, name, newtype))
        when List
                List.new(resolve(orgtype.type, name, newtype))
        when Record
                Record.new(orgtype.types.map{ |key, val| [key, resolve(val, name, newtype)] }.to_h)
        when Union
                Union.new(orgtype.types.map{ |key, val| [key, resolve(val, name, newtype)] }.to_h)
        else
                orgtype
        end
end
unification(a, b, mapping={}) click to toggle source
# File lib/types.rb, line 188
def unification(a, b, mapping={})
        case a
        when Type
                if b.is_a? Type
                        unification(a.metadata, b.metadata, mapping)
                else
                        nil
                end
        when Unresolved
                if b.is_a? Unresolved
                        if mapping[a.name] == nil
                                mapping[a.name] = b.name
                                mapping
                        elsif mapping[a.name] != b.name
                                nil
                        else
                                mapping
                        end
                else
                        nil
                end
        when Function
                if b.is_a? Function
                        if unification(a.argtype, b.argtype, mapping).nil?
                                nil
                        else
                                arestype = a.restype
                                brestype = b.restype
                                if !a.unres.nil?; arestype = resolve(a.restype, a.unres, Unresolved.new(get_new_sym())) end
                                if !b.unres.nil?; brestype = resolve(b.restype, b.unres, Unresolved.new(get_new_sym())) end
                                unification(arestype, brestype, mapping)
                        end
                else
                        nil
                end
        when Optional
                if b.is_a? Optional
                        unification(a.type, b.type, mapping)
                else
                        nil
                end
        when List
                if b.is_a? List
                        unification(a.type, b.type, mapping)
                else
                        nil
                end
        when Record
                if b.is_a? Record and a.types.keys.length == b.types.keys.length
                        for key in a.types.keys
                                aelm = a.types[key]
                                belm = b.types[key]
                                if belm.nil?; return nil end
                                ret = unification(aelm, belm, mapping)
                                if ret.nil?; return nil end
                        end
                        mapping
                else
                        nil
                end
        when Union
                if b.is_a? Union and a.types.keys.length == b.types.keys.length
                        for key in a.types.keys
                                aelm = a.types[key]
                                belm = b.types[key]
                                if belm.nil?; return nil end
                                ret = unification(aelm, belm, mapping)
                                if ret.nil?; return nil end
                        end
                        mapping
                else
                        nil
                end
        else
                if a == b
                        mapping
                else
                        nil
                end
        end
end