def test10(self): x = LogicVar('x') x1 = Const('x') y1 = Const('y') eq_( eval( begin(assign(x1, L(L(1, x))), assign(y1, L(L(1, x))), unify(x1, y1))), True)
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 alpha_rule_head_item(item, env, compiler): if isinstance(item, Var): if isinstance(item, Const): env.bindings[item] = result = compiler.new_var(Const(item.name)) else: env.bindings[item] = result = compiler.new_var(Var(item.name)) return result else: if isinstance(item, Cons): return Cons(alpha_rule_head_item(item.head, env, compiler), alpha_rule_head_item(item.tail, env, compiler)) else: return item
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 testletdouble2(self): x, f = Const('x'), ConstMacroVar('f') eq_(eval(let([(f, macro([x], add(x, x)))], f(1))), 2)
def test_letrec_assign2(self): x, f = Const('x'), Const('f') assert_raises(MultiAssignToConstError, eval, letrec([(f, macro((x, ), f(1), assign(f, 1)))], f(2)))
def testeval3(self): x = Const('x') eq_(eval(let([(x, quote(1))], eval_(x))), 1)
def test_let_assign_const(self): a = Const('a') assert_raises(MultiAssignToConstError, eval, let([(a, 2)], assign(a, 3)))
def test_closure2(self): x, f = Const('x'), Const('f') eq_(eval(let([(f, macrorules([[x], x])), (x, 1)], f(add(x, x)))), 2)
def testassign2(self): a = Const('a') assert_raises(MultiAssignToConstError, eval, begin(assign(a, 2), assign(a, 3)))
def test_let2(self): x = Const('x') assert_raises(MultiAssignToConstError, eval, let([(x, 1)], let([(x, 2)], assign(x, 2))))
def test_let(self): x = Const('x') eq_(eval(let([(x, 1)], x)), 1) eq_(eval(let([(x, 1)], let([(x, 2)], x))), 2)
def test_lamda2(self): x, y = Const('x'), Const('y') eq_(eval(lamda((x, y), add(x, y))(1, 3)), 4)
def test_lamda(self): x = Const('x') eq_(eval(lamda((x, ), 1)(1)), 1) eq_(eval(lamda((x, ), 1)(2)), 1) eq_(eval(lamda((x, ), x)(2)), 2)
def testassign0(self): a = Const('a') eq_(eval(assign(a, 2)), 2)
def testassign1(self): a = Const('a') eq_(eval(begin(assign(a, 2), a)), 2)
def test_let3(self): x, y = Const('x'), Const('y') eq_(eval(let([(x, 1), (y, 2)], add(x, y))), 3)
def test_closure1(self): x, f = Const('x'), Const('f') eq_(eval(let([(f, macrorules([[x], prin(x)])), (x, 1)], f(add(x, x)))), None)
def test_letrec(self): x, y = Const('x'), Const('y') eq_(eval(letrec([(x, 1), (y, x)], y)), 1) eq_(eval(letrec([(x, 1), (y, add(x, 1))], y)), 2)
def test_closure3(self): x, y, f = Const('x'), Const('y'), Const('f') eq_( eval( let([(f, macrorules([[x, y], begin(x, y)])), (x, 1)], f(prin(x), prin(add(x, x))))), None)
def testletdouble(self): x, f = Const('x'), Const('f') eq_(eval(let([(f, lamda([x], add(x, x)))], f(1))), 2)
def testeval2(self): x = Const('x') eq_(eval(let([(x, 1)], eval_(quote(x)))), 1)
def test_macro3(self): x, y = Const('x'), Const('y') eq_(eval(macro((x, y), y, x, 1)(println(1), prin(2))), 1)
def testeval4(self): x = Const('x') eq_(eval(let([(x, quote(add(1, 1)))], eval_(x))), 2)
def test_macro5(self): x, y, f = Const('x'), Const('y'), MacroVar('f') eq_(eval(let([(f, macro((x, y), y, x, 1))], f(println(1), prin(2)))), 1)