Ejemplo n.º 1
0
 def type_of_type(sym: Sym) -> te.T:
     t = tc_state.infer(tenv[sym])
     if isinstance(t, te.App) and t.f is types.type_type:
         return t.arg
     what_i_want = tc_state.new_var()
     tc_state.unify(t, te.App(types.type_type, what_i_want))
     return what_i_want
Ejemplo n.º 2
0
        def list(module, elts):
            list_t = types.list_t
            if not elts:
                return list_t

            try:
                [a] = elts
                list_elt_type = module.eval(a)
                return te.App(list_t, list_elt_type)
            except ValueError:
                raise excs.InvalidListType("List of {}".format(elts))
Ejemplo n.º 3
0
 def type(module, name, definition):
     assert name is None
     clc = module.clc
     x = Express(clc).eval(definition).type
     ret = tc_state.new_var(Var=types.Var,
                            loc=clc.location,
                            filename=clc.filename,
                            name="valtotype")
     tc_state.unify(te.App(types.type_type, ret), x)
     ret = tc_state.infer(ret)
     return ret
Ejemplo n.º 4
0
        def exist(module, bound_vars: tuple, monotype):
            bound_vars_ = set(bound_vars)
            if len(bound_vars) != len(bound_vars_):
                raise excs.DuplicatedNamedTypeVar(bound_vars_)
            bound_vars = bound_vars_
            filename = module.clc.filename
            scope = module.clc.scope
            for n in bound_vars:
                loc, n = sexpr.unloc(n)
                sym = scope.enter(n)
                tvar = tc_state.user_var(Var=types.Var,
                                         loc=loc,
                                         filename=filename,
                                         name=n)
                tenv[sym] = te.App(types.type_type, tvar)

            return module.eval(monotype)
Ejemplo n.º 5
0
 def forall(module, unbound_fresh_decls: typing.Tuple[str, ...],
            polytype):
     fresh_vars_ = set(unbound_fresh_decls)
     if len(fresh_vars_) != len(unbound_fresh_decls):
         raise excs.DuplicatedNamedTypeVar(fresh_vars_)
     unbound_fresh_decls = fresh_vars_
     clc = module.clc
     forall_scope = types.ForallScope(clc.location, clc.filename)
     bound_vars = []
     with clc.resume_scope():
         clc.scope = sub_scope = clc.scope.sub_scope()
         for n in unbound_fresh_decls:
             _, n = sexpr.unloc(n)
             sym = sub_scope.enter(n)
             bound_var = te.Bound(n, forall_scope)
             bound_vars.append(bound_var)
             tenv[sym] = te.App(types.type_type, bound_var)
         return te.Forall(forall_scope, tuple(bound_vars),
                          module.eval(polytype))
Ejemplo n.º 6
0
 def init_global_context(self) -> CompilerGlobalContext:
     ctx = CompilerGlobalContext.create(self)
     scope = self.top = Scope.top()
     sym = scope.enter("int")
     ctx.tenv[sym] = te.App(types.type_type, types.int_t)
     sym = scope.enter("string")
     ctx.tenv[sym] = te.App(types.type_type, types.string_t)
     sym = scope.enter("bool")
     ctx.tenv[sym] = te.App(types.type_type, types.bool_t)
     sym = scope.enter("float")
     ctx.tenv[sym] = te.App(types.type_type, types.float_t)
     sym = scope.enter("unit")
     ctx.tenv[sym] = te.App(types.type_type, types.unit_t)
     sym = scope.enter("value")
     ctx.tenv[sym] = te.App(types.type_type, types.type_type)
     sym = scope.enter("list")
     ctx.tenv[sym] = te.App(types.type_type, types.list_t)
     return ctx
Ejemplo n.º 7
0
        def type(module, typename, typedef):
            clc = module.clc
            path = clc.path
            loc, typename = sexpr.unloc(typename)
            qual_typename = '{}.{}'.format(path, typename)
            sym_typename = clc.scope.enter(typename)
            if typedef:
                # type alias
                nom_type = tc_state.infer(Typing(clc).eval(typedef))
            else:
                nom_type = types.Nom(qual_typename,
                                     loc=loc,
                                     filename=clc.filename)
            tenv[sym_typename] = te.App(types.type_type, nom_type)

            return wrap_loc(
                loc,
                ignore(
                    ir.Set(sym_typename,
                           ir.Expr(type=nom_type, expr=ir.Const(nom_type)))))
Ejemplo n.º 8
0
 def type(module, name, definition):
     assert name is None
     t = Typing(module.clc).eval(definition)
     return ir.Expr(expr=ir.Const(t), type=te.App(types.type_type, t))
Ejemplo n.º 9
0
from proud.unification.interface import TCState

import proud.unification.type_encode as te

cnt = 0
tcs = TCState()

list = te.InternalNom("list")
var = tcs.user_var()

t1 = te.normalize_forall(["x"], te.App(te.UnboundFresh("x"), te.UnboundFresh("x")))
t2 = te.normalize_forall(["x"], te.App(te.UnboundFresh("x"), var))

tcs.unify(t1, t2)
# print(tcs.infer(t2))

x1 = tcs.new_var()
x2 = tcs.new_var()

int_t = te.InternalNom("base.int")

tcs.unify(x1, int_t)
tcs.unify(x1, x2)

assert tcs.infer(x1) == int_t
assert tcs.infer(x2) == int_t

x3 = tcs.new_var()
r1 = te.row_of_map({'a': x1, 'b': x3}, te.empty_row)
r1 = te.Record(r1)
tho = tcs.new_var()
Ejemplo n.º 10
0
 def call(module, f, arg):
     f = module.eval(f)
     arg = module.eval(arg)
     return te.App(f, arg)