Exemplo n.º 1
0
def main():
    env = list_to_tuples([
        ("+", TMono(TFunc(TTerm("int"), TFunc(TTerm("int"), TTerm("int"))))),
        ("-", TMono(TFunc(TTerm("int"), TFunc(TTerm("int"), TTerm("int"))))),
        ("*", TMono(TFunc(TTerm("int"), TFunc(TTerm("int"), TTerm("int"))))),
        ("==", TMono(TFunc(TTerm("int"), TFunc(TTerm("int"), TTerm("bool"))))),
        ("bool", TMono(TFunc(TTerm("int"), TTerm("bool")))),
    ])
    p = Rec(
        Sym("fac"),
        Abs(
            Sym("n"),
            If(
                App(App(Sym("=="), Sym("n")), 0), 1,
                App(
                    App(Sym("*"),
                        App(Sym("fac"), App(App(Sym("-"), Sym("n")), 1))),
                    Sym("n")))))
    t = Var()
    m = Var()
    print(p)
    for s in run(0, (t, m), infer(p, env, t, m)):
        print(s[0])
        print(s[1])

    t = TFunc(TTerm("int"), TTerm("bool"))
    m = Var()
    p = Var()
    for s in run(10, p, infer(p, env, t, m)):
        print(s)
Exemplo n.º 2
0
def calc(e, o):
    a, b = Var(), Var()
    ca, cb = Var(), Var()
    op = Var()
    return conde([
        eq((op, a, b), e),
        calc(a, ca),
        calc(b, cb),
        conde(
            [eq("+", op), add(ca, cb, o)],
            [eq("-", op), sub(ca, cb, o)],
            [eq("*", op), mul(ca, cb, o)],
            [eq("/", op), div(ca, cb, o)],
        )
    ], [eqt(e, int), eq(e, o)])
Exemplo n.º 3
0
def generate_fn():
    q = Var()
    a = Symbol("a")
    env = Env(Symbol("cadr"), Closure(a, [car, [cdr, a]], ()), ())
    p = conj(no_item(q, lambda a: isinstance(a, int)),
             eval_expr([q, [quote, [1, 2, 3]]], env, [1, 2]))
    for s in run(5, q, p):
        print(format_sexpr(s))
Exemplo n.º 4
0
def main():
    states = Var()
    big = Var()
    small = Var()
    _ = Var()
    __ = Var()
    for i in range(1, BIG + 1):
        p = conjp(eq([(big, small, _), __, ...], states),
                  disj(
                      eq(big, i),
                      conj(neq(big, i), eq(small, i)),
                  ), jugs(states))
        for answer in run(1, states, p):
            print("{}:".format(i))
            for b, s, a in reversed(answer):
                print(b, s, a)
            print()
Exemplo n.º 5
0
def solve():
    solutions = {}

    states = Var()
    big = Var()
    small = Var()
    _ = Var()
    __ = Var()

    for i in range(1, BIG + 1):
        p = conjp(
            eq([(big, small, _), __, ...], states),
            disj(
                eq(big, i),
                conj(neq(big, i), eq(small, i)),
            ),
            jugs(states)
        )
        for answer in run(1, states, p):
            solutions[i] = list(reversed(answer))

    return solutions
Exemplo n.º 6
0
import pytest
from mk.unify import Var, convert, unify

u = Var()
v = Var()

UNIFY_DATA = [
    (u, 1, {}, [u], {
        u: 1
    }),
    (u, 1, {
        u: 1
    }, [], {
        u: 1
    }),
    (u, 1, {
        u: 2
    }, None, None),
    (1, v, {}, [v], {
        v: 1
    }),
    (1, v, {
        v: 1
    }, [], {
        v: 1
    }),
    (1, v, {
        v: 2
    }, None, None),
    (u, v, {}, [u], {
        u: v
Exemplo n.º 7
0
from collections import namedtuple

import pytest
from mk.core import conj, eq
from mk.dsl import conjp
from mk.ext.tuples import no_item
from mk.run import initial, reify
from mk.stream import unfold
from mk.unify import Var

a = Var()
b = Var()

Tuple = namedtuple("Tuple", "x, y")

GOALS_DATA = [
    (eq(a, ()), a, [()]),
    (eq(a, (1, )), a, [(1, )]),
    (conj(eq(a, (b, )), eq(b, 1)), a, [(1, )]),
    (eq(a, (a, )), a, []),
    (conjp(eq(a, (1, )), eq(b, (1, )), eq(a, b)), a, [(1, )]),
    (conjp(eq(a, (1, )), eq(b, (2, )), eq(a, b)), a, []),
    (conjp(eq(a, (1, )), eq(b, (1, 2)), eq(a, b)), a, []),
    (conjp(eq(a, (1, )), eq(b, ()), eq(a, b)), a, []),
    (conj(eq(a, Tuple(1, 2)), eq(a, Tuple(1, 2))), a, [Tuple(1, 2)]),
    (conj(eq(a, Tuple(1, 1)), eq(a, Tuple(1, 2))), a, []),
    (conj(eq(a, Tuple(1, 2)), eq(a, (1, 2))), a, []),
    (no_item((1, 2, 3), lambda v: v == 0), None, [None]),
    (no_item((1, 2, 3), lambda v: v == 1), None, []),
    (conj(no_item(a, lambda v: v == 0), eq(a, (1, 2, 3))), None, [None]),
    (conj(no_item(a, lambda v: v == 1), eq(a, (1, 2, 3))), None, []),
Exemplo n.º 8
0
import pytest
from mk.arithmetic import add, div, gte, lte, mul, sub
from mk.core import conj, eq
from mk.run import initial
from mk.stream import unfold
from mk.unify import Var

a = Var()
b = Var()
c = Var()

GOALS_DATA = [
    (add(1, 2, a), [{
        a: 3
    }]),
    (add(1, a, 3), [{
        a: 2
    }]),
    (add(a, 2, 3), [{
        a: 1
    }]),
    (conj(add(1, a, b), eq(a, 2)), [{
        a: 2,
        b: 3
    }]),
    (conj(add(a, 2, b), eq(a, 1)), [{
        a: 1,
        b: 3
    }]),
    (conj(add(a, b, 3), eq(a, 1)), [{
        a: 1,
Exemplo n.º 9
0
def quines():
    q = Var()
    for s in run(5, q, eval_expr(q, (), q)):
        print(format_sexpr(s))
Exemplo n.º 10
0
def main():
    expr = ("/", ("+", 4, ("*", 2, ("-", 8, 5))), 2)
    out = Var()
    for s in run(0, out, calc(expr, out)):
        print(s)
Exemplo n.º 11
0
def quines():
    q = Var()
    return next(run(0, q, eval_expr(q, (), q)))