Пример #1
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"))
Пример #2
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()"))
Пример #3
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)
Пример #4
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)
Пример #5
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))
Пример #6
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
Пример #7
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"))
Пример #8
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"))
Пример #9
0
    def test_slots_on_block(self):
        sut = SlotAssigner()
        rule = Parser.rule("block([x | xs]) --> block(xs) where x --> y.")

        assigned = sut.assign_rule(rule.before, rule.after, rule.premises)

        self.assertEqual(3, assigned)
Пример #10
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)
Пример #11
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))
Пример #12
0
    def test_rules(self):
        text = """a(x, y) --> b where 1 == 1"""

        parsed = Parser.rule(text)

        expected = Rule(ApplTerm(
            "a", [VarTerm("x", 0), VarTerm("y", 1)]), VarTerm("b"), None, None,
                        2)
        expected.premises.append(EqualityCheckPremise(IntTerm(1), IntTerm(1)))

        self.assertEqual(expected, parsed)
Пример #13
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))
Пример #14
0
    def test_slots_on_rules(self):
        sut = SlotAssigner()
        rule = Parser.rule("a(x) --> [z] where x == 1; b(y) => x; y --> z.")

        assigned = sut.assign_rule(rule.before, rule.after, rule.premises)

        self.assertEqual(3, assigned)
        self.assertEqual(0, rule.before.args[0].slot)  # a(x)
        self.assertEqual(0, rule.premises[0].left.slot)  # x == 1
        self.assertEqual(1, rule.premises[1].left.args[0].slot)  # b(y) => x
        self.assertEqual(0, rule.premises[1].right.slot)  # b(y) => x
        self.assertEqual(1, rule.premises[2].left.slot)  # y --> z
        self.assertEqual(2, rule.premises[2].right.slot)  # y --> z
        self.assertEqual(2, rule.after.items[0].slot)  # [z]
Пример #15
0
    def test_slot_assignment(self):
        rule = Parser.rule("block([x | xs]) --> block(xs) where x --> y")

        self.assertEqual(3, rule.number_of_bound_terms)
Пример #16
0
    def test_environment_read(self):
        rule = Parser.rule("E |- read(x) --> E[x]")

        self.assertIsInstance(rule.after, MapReadTerm)
        self.assertEqual(VarTerm("E"), rule.after.map)
        self.assertEqual(VarTerm("x", 0), rule.after.key)
Пример #17
0
    def test_environment_write(self):
        rule = Parser.rule("E |- bindVar(x, v) --> {x |--> v, E}")

        self.assertIsInstance(rule.after, MapWriteTerm)
        self.assertEqual(2, len(rule.after.assignments))
        self.assertEqual(VarTerm("E"), rule.components[0])
Пример #18
0
from src.meta.dynsem import Module, NativeFunction
from src.meta.parser import Parser

# TODO not the most elegant but it's what we have to work with
while_rule = Parser.rule(
    "while(cond, then) --> while2(cond, value, then) where cond --> value")
while_rule.has_loop = True

rules = [
    Parser.rule("block([x | xs]) --> block(xs) where x --> y"),
    Parser.rule("E |- assign(x, v) --> {x |--> v, E}"),
    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)"),