Esempio n. 1
0
        def quote(module, contents):
            clc = module.clc
            quotation = Quote(clc)
            quote_expr = quotation.eval(contents)
            quote_scope: Scope = quotation.clc.scope
            sym_arg = quote_scope.enter(".external")
            name_var = new_var(clc, name="external")
            tenv[sym_arg] = name_var

            record_fields = []
            for attr_name, each in quote_scope.freevars.items():
                record_fields.append((each, attr_name, tenv[each]))

            rho_group = types.ForallScope(clc.location, filename=clc.filename)
            rho = te.Bound('ρ', rho_group)
            type_arg = te.Record(
                te.row_from_list([(attr, t) for _, attr, t in record_fields],
                                 te.RowPoly(rho)))
            tc_state.unify(name_var, type_arg)
            exp_arg = ir.Expr(type=tc_state.infer(type_arg), expr=sym_arg)
            block = [
                ignore(
                    ir.Set(
                        each,
                        ir.Expr(type=t, expr=ir.Field(base=exp_arg,
                                                      attr=attr))))
                for each, attr, t in record_fields
            ]
            block.append(quote_expr)
            ret_expr = ir.Fun(
                "<quote>", clc.filename, sym_arg,
                ir.Expr(expr=ir.Block(block), type=quote_expr.type))
            ret_type = te.Arrow(type_arg, quote_expr.type)
            ret_type = te.Forall(rho_group, (rho, ), ret_type)
            return ir.Expr(expr=ret_expr, type=ret_type)
Esempio n. 2
0
        def lam(module, arg, type, ret):
            loc, name = sexpr.unloc(arg)
            clc = module.clc
            outer_scope = clc.scope
            filename = clc.filename
            path = clc.path

            if type:
                type_arg = tc_state.infer(Typing(clc).eval(type))
            else:
                type_arg = new_var(clc, name)

            with clc.resume_scope():
                # level = tc_state.push_level()
                inner_scope = outer_scope.sub_scope(hold_bound=True)
                clc.scope = inner_scope
                sym_arg = inner_scope.enter(name)
                tenv[sym_arg] = type_arg
                ret = module.eval(ret)
                lam_type = te.Arrow(
                    type_arg, ret.type
                )  # lam_type = generalise_type(tc_state, lam_type, loc=loc, filename=filename, level=level)

            name = "{} |{}|".format(path, name)
            sym_arg = ir.Fun(name, filename, sym_arg, ret)
            return ir.Expr(type=lam_type, expr=sym_arg)
Esempio n. 3
0
        def call(module, f, arg):
            clc = module.clc
            f = module.eval(f)
            f.type = infer(f.type)

            to_mono = None
            to_poly = None
            func_type = f.type

            # row polymorphisms
            while isinstance(func_type, te.Implicit):
                func_type = func_type.type
            if isinstance(func_type, te.Arrow):
                # layout of record might change correspondingly.
                to_mono = func_type.ret
                to_poly = func_type.arg

            f.type = inst(f.type)[1]
            resolve_instance_(clc.scope, f)
            level = tc_state.push_level()
            arg = module.eval(arg)
            arg.type = infer(arg.type)
            arg_is_forall = isinstance(arg.type, te.Forall)
            arg.type = inst(arg.type)[1]
            resolve_instance_(clc.scope, arg)
            f.type = infer(f.type)
            if arg_is_forall and isinstance(f.type, te.Arrow) and isinstance(
                    f.type.arg, te.Forall):
                # a simple strategy to support higher rank types when annotated.
                f_type_arg = inst(f.type.arg, rigid=True)[1]
                unify(arg.type, f_type_arg)
                ret_t = f.type.ret
            else:
                ret_t = new_var(clc, name='ret')
                inst_arrow = te.Arrow(arg.type, ret_t)
                unify(inst_arrow, f.type)
                ret_t = generalise_type(tc_state,
                                        ret_t,
                                        loc=clc.location,
                                        filename=clc.filename,
                                        level=level)

            if to_poly:
                arg = ir.Expr(type=arg.type,
                              expr=ir.Polymorphization(layout_type=to_poly,
                                                       expr=arg))

            ret = ir.Expr(type=ret_t, expr=ir.Invoke(f, arg))

            if to_mono:
                ret = ir.Expr(type=ret_t,
                              expr=ir.Momomorphization(layout_type=to_mono,
                                                       expr=ret))
            return ret
Esempio n. 4
0
var = tcs.user_var()
var2 = 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(te.App(var, var), var2)
try:
    tcs.unify(t1, t2)
except te.excs.TypeMismatch:
    pass

var = tcs.user_var()
var2 = tcs.user_var()

t1 = te.normalize_forall(["x"], te.App(te.UnboundFresh("x"), te.Arrow(te.UnboundFresh("x"), var)))
t2 = te.normalize_forall(["x"], te.App(te.UnboundFresh("x"), te.Arrow(var2, var2)))
tcs.unify(var, var2)
tcs.unify(t1, t2)
print(tcs.infer(var))
print(var.belong_to.vars)


var = tcs.new_var()
var2 = tcs.new_var()

t1 = te.normalize_forall(["x"], te.App(te.UnboundFresh("x"), te.Arrow(var, te.UnboundFresh("x"))))
t2 = te.normalize_forall(["x"], te.App(te.UnboundFresh("x"), te.Arrow(var2, var2)))
tcs.unify(t1, t2)
print(tcs.infer(var))
print(var.belong_to.vars)
Esempio n. 5
0
 def arrow(module, arg, ret):
     arg = module.eval(arg)
     ret = module.eval(ret)
     return te.Arrow(arg, ret)