def cps(self, compiler, cont): k = compiler.new_var(il.ConstLocalVar("cont")) params = tuple([compiler.new_var(Const("arg")) for x in range(self.arity)]) clauses = [] for head, body in self.rules: head_exps = begin(*tuple(unify_head_item2(param, head_item) for param, head_item in zip(params, head))) clauses.append(begin(head_exps, body)) if self.has_cut(): body = wrap_cut(or_(*clauses)).cps(compiler, k) else: body = or_(*clauses).cps(compiler, k) params = tuple(param.interlang() for param in params) return cont(il.Lamda((k,) + params, body))
def cps(self, compiler, cont): k = compiler.new_var(il.ConstLocalVar('cont')) params = tuple([compiler.new_var(il.ConstLocalVar('arg')) for x in range(self.arity)]) clauses = [] for head, body in self.rules: head_exps = begin(*tuple(unify_macro_head_item2(param, head_item) for param, head_item in zip(params, head))) clauses.append(begin(head_exps, body)) if self.has_cut(): body = wrap_cut(or_(*clauses)).cps(compiler, k) else: body = or_(*clauses).cps(compiler, k) return cont(il.Lamda((k,)+params, body))
def greedy_seplist(compiler, cont, item, separator, template=None, result=None): if result is None: return begin(item, greedy_any1(begin(separator, item)) ).cps(compiler, cont) else: result1 = compiler.new_var(il.ConstLocalVar('result')) result2 = compiler.new_var(Var('result')) result2_2 = result2.interlang() template1 = template.interlang() return begin(item, direct_interlang(il.Assign(result1, il.GetValue(template1))), greedy_any2(begin(separator, item), template, result2), eval_unify(result, direct_interlang(il.add(il.MakeList(result1), result2_2))) ).cps(compiler, cont)
def times(compiler, cont, item, expect_times, template=None, result=None): if result is None: expect_times1 = compiler.new_var(Const('expect_times')) return begin(assign(expect_times1, getvalue(expect_times)), times1(item, expect_times1)).cps(compiler, cont) else: expect_times1 = compiler.new_var(Const('expect_times')) result1 = compiler.new_var(il.ConstLocalVar('result')) result2 = compiler.new_var(Var('result')) result2_2 = result2.interlang() template1 = template.interlang() return begin(assign(expect_times1, getvalue(expect_times)), times2(item, expect_times1, template, result2), unify(result, result2)).cps(compiler, cont)
def seplist(compiler, cont, item, separator, template=None, result=None): if result is None: return begin(item, any1(begin(separator, item))).cps(compiler, cont) else: result1 = compiler.new_var(il.ConstLocalVar('result')) result2 = compiler.new_var(Var('result')) result2_2 = result2.interlang() template1 = template.interlang() return begin( item, direct_interlang(il.Assign(result1, il.GetValue(template1))), any2(begin(separator, item), template, result2), eval_unify( result, direct_interlang(il.add(il.MakeList(result1), result2_2)))).cps(compiler, cont)
def times(compiler, cont, item, expect_times, template=None, result=None): if result is None: expect_times1 = compiler.new_var(Const('expect_times')) return begin(assign(expect_times1, getvalue(expect_times)), times1(item, expect_times1)).cps(compiler, cont) else: expect_times1 = compiler.new_var(Const('expect_times')) result1 = compiler.new_var(il.ConstLocalVar('result')) result2 = compiler.new_var(Var('result')) result2_2 = result2.interlang() template1 = template.interlang() return begin(assign(expect_times1, getvalue(expect_times)), times2(item, expect_times1, template, result2), unify(result, result2) ).cps(compiler, cont)
def macrorules(*rules): result = [] for rule in rules: head = tuple(element(x) for x in rule[0]) body = begin(*(element(x) for x in rule[1:])) result.append((head, body)) return MacroRules(result)
def alpha(self, env, compiler): new_env = env.extend() for var, value in self.bindings: if isinstance(value, Rules): if not isinstance(var, Const): new_var = compiler.new_var(LamdaVar(var.name)) else: new_var = compiler.new_var(ConstLamdaVar(var.name)) elif isinstance(value, Lamda): if not isinstance(var, Const): new_var = compiler.new_var(LamdaVar(var.name)) else: new_var = compiler.new_var(ConstLamdaVar(var.name)) elif isinstance(value, MacroRules): if not isinstance(var, Const): new_var = compiler.new_var(MacroVar(var.name)) else: new_var = compiler.new_var(ConstMacroVar(var.name)) else: new_var = compiler.new_var(var) if isinstance(var, Const): new_var.assigned = True env[var] = new_var alphaed_body = self.body.alpha(new_env, compiler) assign_bindings = tuple(Assign(new_env[var], value.alpha(env, compiler)) for var, value in self.bindings) return begin(*(assign_bindings + (alphaed_body,)))
def lazy_any(compiler, cont, item, template=None, result=None): if result is None: return lazy_any1(item).cps(compiler, cont) else: _result = compiler.new_var(Var('result')) return begin(lazy_any2(item, template, _result), unify(result, _result)).cps(compiler, cont)
def greedy_some(compiler, cont, item, template=None, result=None): if result is None: return greedy_some1(item).cps(compiler, cont) else: _result = compiler.new_var(Var('result')) return begin(greedy_some2(item, template, _result), unify(result, _result)).cps(compiler, cont)
def alpha(self, env, compiler): new_env = env.extend() for var, value in self.bindings: if isinstance(value, Rules): if not isinstance(var, Const): new_var = compiler.new_var(LamdaVar(var.name)) else: new_var = compiler.new_var(ConstLamdaVar(var.name)) elif isinstance(value, Lamda): if not isinstance(var, Const): new_var = compiler.new_var(LamdaVar(var.name)) else: new_var = compiler.new_var(ConstLamdaVar(var.name)) elif isinstance(value, MacroRules): if not isinstance(var, Const): new_var = compiler.new_var(MacroVar(var.name)) else: new_var = compiler.new_var(ConstMacroVar(var.name)) else: new_var = compiler.new_var(var) if isinstance(var, Const): new_var.assigned = True env[var ] = new_var alphaed_body = self.body.alpha(new_env, compiler) assign_bindings = tuple(Assign(new_env[var], value.alpha(env, compiler)) for var, value in self.bindings) return begin(*(assign_bindings+(alphaed_body,)))
def findall(compiler, cont, goal, template=None, bag=None): if bag is None: return findall_1(goal).cps(compiler, cont) else: _bag = compiler.new_var(Var('bag')) return begin(findall_2(goal, template, _bag),unify(bag, _bag) ).cps(compiler, cont)
def cps_call(self, compiler, cont, args): if self.arity!=len(args): raise clauses = [] for head, body in self.rules: clauses.append(begin(unify_macro_head(compiler, cont, args, head), body)) if self.has_cut(): return wrap_cut(or_(*clauses)).cps(compiler, cont) else: return or_(*clauses).cps(compiler, cont)
def cps_call(self, compiler, cont, args): if self.arity != len(args): raise clauses = [] for head, body in self.rules: clauses.append(begin(unify_macro_head(compiler, cont, args, head), body)) if self.has_cut(): return wrap_cut(or_(*clauses)).cps(compiler, cont) else: return or_(*clauses).cps(compiler, cont)
def alpha(self, env, compiler): new_env = env.extend() for var, value in self.bindings: if isinstance(value, Rules): new_var = compiler.new_var(RecursiveFunctionVar(var.name)) elif isinstance(value, Lamda): new_var = compiler.new_var(RecursiveFunctionVar(var.name)) elif isinstance(value, Macro): new_var = compiler.new_var(RecursiveMacroVar(var.name)) elif isinstance(value, MacroRules): new_var = compiler.new_var(RecursiveMacroVar(var.name)) else: new_var = compiler.new_var(var) if isinstance(new_var, Const): new_var.assigned = True new_env.bindings[var] = new_var return begin(*(tuple(Assign(new_env[var], value.alpha(new_env, compiler)) for var, value in self.bindings) +(self.body.alpha(new_env, compiler),)))
def alpha(self, env, compiler): new_env = env.extend() for var, value in self.bindings: if isinstance(value, Rules): new_var = compiler.new_var(RecursiveFunctionVar(var.name)) elif isinstance(value, Lamda): new_var = compiler.new_var(RecursiveFunctionVar(var.name)) elif isinstance(value, Macro): new_var = compiler.new_var(RecursiveMacroVar(var.name)) elif isinstance(value, MacroRules): new_var = compiler.new_var(RecursiveMacroVar(var.name)) else: new_var = compiler.new_var(var) if isinstance(new_var, Const): new_var.assigned = True new_env.bindings[var] = new_var return begin( *( tuple(Assign(new_env[var], value.alpha(new_env, compiler)) for var, value in self.bindings) + (self.body.alpha(new_env, compiler),) ) )
def macro(params, *body): body = tuple(element(x) for x in body) return Macro(params, begin(*body))
def let(bindings, *body): bindings = tuple((var, element(value)) for var, value in bindings) return Let(bindings, begin(*body))
def letrec(bindings, *body): return Letrec( tuple((element(var), element(value)) for var, value in bindings), begin(*tuple(element(exp) for exp in body)) )
def unify_macro_head(compiler, cont, args, head): # for direct call return begin(*tuple(unify_macro_head_item1(compiler, cont, arg, head_item) for arg, head_item, in zip(args, head)))
def lamda(params, *body): body = tuple(element(x) for x in body) return Lamda(params, begin(*body))
def letrec(bindings, *body): return Letrec(tuple((element(var), element(value)) for var, value in bindings), begin(*tuple(element(exp) for exp in body)))