Example #1
0
def parse_typ(json):
    if isinstance(json, Typ):
        return json
    if isinstance(json, str):
        assert len(json) > 0
        return TypSymbol(json) if not json[0].islower() else TypVar(json)
    elif isinstance(json, int):
        return TypVar(json)
    elif isinstance(json, Sequence):
        args = tuple(parse_typ(x) for x in json)
        if len(args) == 3 and args[1] == T_ARROW:
            return TypTerm.make_arrow(args[0], args[2])
        return TypTerm(args)
    else:
        raise ValueError("Unsupported input value %s" % json)
Example #2
0
 def recursive_subs_call_for_product_tail(self, j, typ_b, n):
     if TypTerm.is_internal_pair_typ(typ_b):
         if self.cache_subproducts:
             return self.subs(j, typ_b, n)
         else:
             # Ensures that intermediate sub-product results are not stored in cache
             return self.subs_product(j, typ_b, n)
     else:
         # typ_b is the last element of the whole product
         return self.subs(j, typ_b, n)
Example #3
0
 def successors_typed(self, gamma, n):
     alpha, n1 = new_var(self.typ, n)
     typ_f = TypTerm.make_arrow(alpha, self.typ)
     ret = [(App(UnfinishedLeaf(typ_f), UnfinishedLeaf(alpha), self.typ), sub.Sub(), n1)]
     for ctx_declaration in gamma.ctx.values():
         fresh_res = fresh(ctx_declaration.typ, self.typ, n)
         mu = sub.mgu(self.typ, fresh_res.typ)
         if not mu.is_failed():
             sigma = mu.restrict(self.typ)
             leaf = Leaf(ctx_declaration.sym, sigma(self.typ))
             ret.append((leaf, sigma, fresh_res.n))
     return ret
Example #4
0
    def subs_uf_ij(self, f_uf, x_uf, i, j, typ, n):
        ret = []
        alpha, n1 = new_var(typ, n)
        typ_f = TypTerm.make_arrow(alpha, typ)

        for res_f in self.subs_uf(f_uf, i, typ_f, n1):
            typ_x = res_f.sub(alpha)
            for res_x in self.subs_uf(x_uf, j, typ_x, res_f.n):
                sigma_fx = sub.dot(res_x.sub, res_f.sub).restrict(typ)
                num_fx = res_x.num * res_f.num

                ret.append(sub.PreSubRes(num_fx, sigma_fx))

        return ret
Example #5
0
def ts_ij(gamma, i, j, typ, n):
    ret = []
    alpha, n1 = new_var(typ, n)
    typ_f = TypTerm.make_arrow(alpha, typ)

    for res_f in ts(gamma, i, typ_f, n1):
        typ_x = res_f.sub(alpha)
        for res_x in ts(gamma, j, typ_x, res_f.n):
            sigma_fx = sub.dot(res_x.sub, res_f.sub).restrict(typ)
            tree_f = res_f.tree.apply_sub(res_x.sub)
            tree_fx = App(tree_f, res_x.tree, sigma_fx(typ))
            ret.append(TsRes(tree_fx, sigma_fx, res_x.n))

    return ret
Example #6
0
    def subs_ij(self, i, j, typ, n):

        # todo potvrdit ze funguje
        # tady se da zapnout stara implementace productu (nahrazeno pomoci subs_product volaneho v subs_compute)
        # if TypTerm.is_internal_pair_typ(typ):
        #    return self.subs_internal_pair(i, j, typ, n)

        ret = []
        alpha, n1 = new_var(typ, n)
        typ_f = TypTerm.make_arrow(alpha, typ)

        for res_f in self.subs(i, typ_f, n1):
            typ_x = res_f.sub(alpha)
            for res_x in self.subs(j, typ_x, res_f.n):
                sigma_fx = sub.dot(res_x.sub, res_f.sub).restrict(typ)
                num_fx = res_x.num * res_f.num

                ret.append(sub.PreSubRes(num_fx, sigma_fx))
        return ret
Example #7
0
    def subs_internal_pair(self, i, j, typ, n):

        i_without_cons = i - 1
        if i_without_cons == 0:
            return []

        ret = []
        typ_a, typ_b_0 = TypTerm.split_internal_pair_typ(typ)
        n = typ_b_0.get_next_var_id(n)

        for res_a in self.subs(i_without_cons, typ_a, n):
            typ_b = res_a.sub(typ_b_0)
            for res_b in self.subs(j, typ_b, res_a.n):
                sigma_ab = sub.dot(res_b.sub, res_a.sub).restrict(typ)
                num_ab = res_b.num * res_a.num

                ret.append(sub.PreSubRes(num_ab, sigma_ab))

        return ret
Example #8
0
 def f(x, acc):
     return TypTerm.make_arrow(parse_typ(x), parse_typ(acc))