예제 #1
0
def maybe_quote(quote_some, x):
    x = maybe_type(x)
    return app(
        x,
        QUOTE(none),
        lambda y: qapp(quote(some), app(quote_some, y)),
    )
예제 #2
0
def list_sort(lt, xs):
    return let(
        app(list_sort, lt), lambda sort:
        app(xs, nil, lambda h, t:
            let(app(lt, h), lambda lt_h:
                app(list_cat,
                    app(sort, app(list_filter, lt_h, t)),
                    cons(h,
                         app(sort,
                             app(list_filter, compose(bool_not, lt_h), t)))))))
예제 #3
0
def a_construct():
    return join_(
        app(pair, I, I),
        app(pair, BOT, TOP),
        app(pair, div, BOT),
        app(pair, a_copy, a_join),
        app(pair, C, C),
        app(a_preconj, a_construct),
        app(a_postconj, a_construct),
        app(a_compose, a_construct, a_construct),
    )
예제 #4
0
 def __call__(self, *args):
     if len(args) != len(self._encoders):
         raise TypeError('{} takes {} arguments ({} given)'.format(
             self.__name__, len(self._encoders), len(args)))
     if ENGINE is None:
         raise RuntimeError('No engine specified')
     code_args = [encode(arg) for encode, arg in izip(self._encoders, args)]
     code_in = app(self.combinator.code, *code_args)
     code_out = ENGINE.reduce(code_in)
     io.check_for_errors(code_out)
     data_out = self._decoder(code_out)
     return data_out
예제 #5
0
def trace(*part_names, **kwargs):
    """Trace an approximation to A.

    Possible names: all base copy div bot swap preconj postconj compose

    Kwargs:
      steps = 10
      type = '(FUN r (FUN s (FUN f (FUN x (r (f (s x)))))))'
      inhab = '(FUN x x)'
      fmt = 'sexpr' (one of: 'sexpr', 'tiny')
    """
    print_ = PRINTERS[kwargs.get('fmt', 'sexpr')]

    # Construct an approximation of A with only a few parts.
    if 'all' in part_names:
        part_names = PARTS.keys()
    for name in part_names:
        print('{} = {}'.format(name, print_(simplify(PARTS[name]))))
        assert name in PARTS, name
    A = simplify(build_A(*part_names))
    print('A = {}'.format(print_(A)))

    # Cast a candidate inhabitant via the defined type.
    type_ = sexpr_simplify(kwargs.get('type', default_type))
    inhab = sexpr_simplify(kwargs.get('inhab', default_inhab))
    term = simplify(app(A, type_, inhab))
    print('0\t{}'.format(print_(term)))

    # Print a reduction sequence.
    steps = int(kwargs.get('steps', 10))
    for step in xrange(steps):
        term = try_compute_step(term)
        if term is None:
            print '--- Normalized ---'
            return
        print('{}\t{}'.format(1 + step, print_(term)))
    print('... Not normalized ...')
예제 #6
0
def div_rec_call(x):
    return join_(x, div_rec_call(app(x, TOP)))
예제 #7
0
from pomagma.reducer.code import I, K, B, C, S, TOP, NVAR, APP
from pomagma.reducer.sugar import _compile
from pomagma.reducer.sugar import combinator, as_code, app, join_
from pomagma.util.testing import for_each
import pytest

f = NVAR("f")
x = NVAR("x")
y = NVAR("y")
z = NVAR("z")


CODE_EXAMPLES = [(I, I), (K, K), (app(S, I, I), app(S, I, I))]

CLOSED_FUN_EXAMPLES = [
    (lambda: x, x),
    (lambda: B, B),
    (lambda x: x, I),
    (lambda x, y: x, K),
    (lambda x, y: y, APP(K, I)),
    (lambda x: lambda y: x, K),
    (lambda x: lambda x: x, APP(K, I)),
    (lambda x, y, z: app(x, z, y), C),
    (lambda x, y, z: app(x, app(y, z)), B),
    (lambda x, y, z: app(x, z, app(y, z)), S),
    (lambda x: app(x, x), app(S, I, I)),
    (lambda x: app(f, app(x, x)), app(B, f, app(S, I, I))),
]

OPEN_FUN_EXAMPLES = [(lambda: x, x), (lambda x: y, app(K, y)), (lambda x: app(f, x), f)]
예제 #8
0
from pomagma.reducer import lib
from pomagma.reducer.code import NVAR
from pomagma.reducer.linker import link
from pomagma.reducer.sugar import as_code, app
from pomagma.util.testing import for_each

x = NVAR('x')


@for_each([
    (NVAR('lib.true'), lib.true),
    (NVAR('lib.false'), lib.false),
    (NVAR('lib.zero'), lib.zero),
    (NVAR('lib.succ'), lib.succ),
    (app(x, NVAR('lib.ok'), NVAR('lib.ok')), app(x, lib.ok, lib.ok)),
])
def test_link(code, expected):
    expected = as_code(expected)
    actual = link(code)
    assert actual == expected
예제 #9
0
def bool_and(x, y):
    return app(x, y, false)
예제 #10
0
def bool_test(x):
    return app(x, ok, ok)
예제 #11
0
def unit_quote(x):
    x = unit_type(x)
    return app(x, QUOTE(ok))
예제 #12
0
def unit_and(x, y):
    return app(x, y)
예제 #13
0
def a_join(f, x, y):
    return app(f, join_(x, y))
예제 #14
0
def a_copy(f, x):
    return app(f, x, x)
예제 #15
0
def div(f):
    return join_(f, app(div, f, TOP))
예제 #16
0
def byte_make(b0, b1, b2, b3, b4, b5, b6, b7):
    result = I
    for b in (b0, b1, b2, b3, b4, b5, b6, b7):
        result = app(C, result, b)
    return result
예제 #17
0
def bytes_test(xs):
    return unit_type(
        app(xs, ok, lambda h, t: unit_and(byte_test(h), bytes_test(t))))
예제 #18
0
def construct(f):
    """The simple type constructor, aka AAA."""
    return app(a_construct, f)
예제 #19
0
def bool_type(x):
    return app(BOOL, x)
예제 #20
0
def a_arrow(a, b):
    return lambda f, x: app(b, app(f, app(a, x)))
예제 #21
0
def bool_not(x):
    return app(x, false, true)
예제 #22
0
def equal(x, y):
    return bool_type(app(EQUAL, x, y))
예제 #23
0
def partial_pred(x):
    return app(x, lib.undefined, lambda px: px)
예제 #24
0
def less(x, y):
    return bool_type(app(LESS, x, y))
예제 #25
0
파일: sudoku.py 프로젝트: fritzo/pomagma
def un_all_different(xs):
    return app(xs, lib.true, lambda h, t:
               lib.bool_and(
                   lib.list_all(lambda x: lib.bool_not(lib.list_num_eq(h, x))),
                   all_different(t)))
예제 #26
0
def enum_contains(qxs, qy):
    return app(LESS, qapp(quote(box), qy), qxs)
예제 #27
0
def Y(f):
    return app(lambda x: app(f, app(x, x)), lambda x: app(f, app(x, x)))
예제 #28
0
def _bits_test(b0, b1, b2, b3, b4, b5, b6, b7):
    bits = [b0, b1, b2, b3, b4, b5, b6, b7]
    tests = map(bool_test, bits)
    return app(join_(*tests), *tests)
예제 #29
0
def div_rec_app(x):
    return join_(x, app(div_rec_app, app(x, TOP)))
예제 #30
0
def byte_test(x):
    return unit_type(app(x, _bits_test))