Ejemplo n.º 1
0
    def test_transformation_of_result(self):
        mod = Module([Parser.rule("a() --> b where b => 2")])
        term = Parser.term("a()")

        result = Interpreter(mod).interpret(term)

        self.assertEqual(result, IntTerm(2))
Ejemplo n.º 2
0
    def test_one_transformation(self):
        mod = Module([Parser.rule("a(x) --> b where x == 1")])
        term = Parser.term("a(1)")

        result = Interpreter(mod).interpret(term)

        self.assertEqual(result, VarTerm("b"))
Ejemplo n.º 3
0
    def test_block(self):
        mod = Module([Parser.rule("block([x | xs]) --> block(xs)")])
        term = Parser.term("block([1, 2, 3, 4])")

        result = Interpreter(mod).interpret(term)

        self.assertEqual(result, ApplTerm("block"))
Ejemplo n.º 4
0
    def test_invalid_premise(self):
        mod = Module([Parser.rule("a() --> b() where 1 == 2")])
        term = Parser.term("a()")

        with self.assertRaises(DynsemError):
            Interpreter(mod).interpret(
                term)  # does not know where to go when 1 != 2
Ejemplo n.º 5
0
    def test_native(self):
        add = NativeFunction(Parser.native_function("add(x, y)"),
                             lambda x, y: x + y)
        mod = Module([Parser.rule("a(x) --> add(x, 1)")], [add])
        term = Parser.term("a(1)")

        result = Interpreter(mod).interpret(term)

        self.assertEqual(result, IntTerm(2))
Ejemplo n.º 6
0
    def test_environment_retrieval(self):
        mod = Module([Parser.rule("E |- read(y) --> E[y]")])
        term = Parser.term("read(y)")
        sut = Interpreter(mod)
        sut.environment.locate_and_put("y", 42)

        result = sut.interpret(term)

        self.assertEqual(result, 42)
Ejemplo n.º 7
0
    def test_reduction_premise(self):
        mod = Module([
            Parser.rule("b() --> c()"),
            Parser.rule("a(x) --> y where x --> y")
        ])
        term = Parser.term("a(b())")

        result = Interpreter(mod).interpret(term)

        self.assertEqual(result, ApplTerm("c"))
Ejemplo n.º 8
0
    def test_environment_assignment(self):
        mod = Module([Parser.rule("E |- bindVar(k, v) --> {k |--> v, E}")])
        term = Parser.term("bindVar(a, 1)")
        sut = Interpreter(mod)
        a = sut.environment.locate_and_put(
            "a", IntTerm(42))  # this should be overwritten

        result = sut.interpret(term)

        self.assertIsInstance(result, MapWriteTerm)
        self.assertEqual(IntTerm(1), sut.environment.get(a))
Ejemplo n.º 9
0
    def test_multiple_transformations(self):
        mod = Module([
            Parser.rule("a() --> b()"),
            Parser.rule("b() --> c()"),
            Parser.rule("c() --> d()")
        ])
        term = Parser.term("a()")

        result = Interpreter(mod).interpret(term)

        self.assertEqual(result, Parser.term("d()"))
Ejemplo n.º 10
0
    def test_interpreter_caching(self):
        if_rule = Parser.rule("if(a) --> then(a)")
        then1_rule = Parser.rule("then(0) --> b")
        then2_rule = Parser.rule("then(x) --> c")
        module = Module([if_rule, then1_rule, then2_rule])
        interpreter = Interpreter(module)

        result1 = interpreter.interpret(Parser.term("if(0)"))
        self.assertEqual(VarTerm("b"), result1)

        result2 = interpreter.interpret(Parser.term("if(1)"))
        self.assertEqual(VarTerm("c"), result2)
Ejemplo n.º 11
0
    def test_recursive_contexts(self):
        ifz_rule = Parser.rule(
            "ifz(cond, then, else) --> ifzc(value, then, else) where cond --> value"
        )
        ifz0_rule = Parser.rule("ifzc(0, then, else) --> then")
        ifz1_rule = Parser.rule("ifzc(nonzero, then, else) --> else")
        # TODO need inequality check, e.g. where non_zero != 0
        module = Module([ifz_rule, ifz0_rule, ifz1_rule])
        interpreter = Interpreter(module)

        result = interpreter.interpret(Parser.term("ifz(ifz(1, 2, 3), 4, 5)"))
        self.assertEqual(IntTerm(5), result)
Ejemplo n.º 12
0
    Parser.rule("E |- retrieve(x) --> E[x]"),
    # TODO rename this to something other than ifz... it is not an ifz
    Parser.rule(
        "ifz(cond, then, else) --> result where cond --> cond2; case cond2 of {0 => result => else otherwise => result => then}"
    ),
    while_rule,
    Parser.rule("while2(cond, 0, then) --> 0"),
    Parser.rule(
        "while2(cond, value, then) --> while(cond, then) where then --> ignored"
    )
]


def write(s, unused):
    print(s)
    return 0


native_functions = [
    NativeFunction(Parser.native_function("write(x)"),
                   write),  # TODO rpython demands it
    NativeFunction(Parser.native_function("add(x, y)"), lambda x, y: x + y),
    NativeFunction(Parser.native_function("sub(x, y)"), lambda x, y: x - y),
    NativeFunction(Parser.native_function("mul(x, y)"), lambda x, y: x * y),
    NativeFunction(Parser.native_function("div(x, y)"), lambda x, y: x // y),
    NativeFunction(Parser.native_function("leq(x, y)"),
                   lambda x, y: int(x <= y))
]

e2 = Module(rules, native_functions)