示例#1
0
文件: lib.py 项目: fritzo/pomagma
def a_construct():
    return join_(
        pair(I, I),
        pair(BOT, TOP),
        pair(div, BOT),
        pair(a_copy, a_join),
        pair(C, C),
        a_preconj(a_construct),
        a_postconj(a_construct),
        a_compose(a_construct, a_construct),
    )
示例#2
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),
    )
示例#3
0
def close(f):
    """Scott's universal closure operator V."""
    return lambda x: join_(x, app(f, close(x)))
示例#4
0
def enum_union(xs, ys):
    return join_(xs, ys)
示例#5
0
def enum(items):
    assert isinstance(items, (list, set, frozenset)), items
    return join_(*map(box, items))
示例#6
0
def build_A(*part_names):
    return rec(join_(*(PARTS[name] for name in part_names)))
示例#7
0
def div_rec_call(x):
    return join_(x, div_rec_call(app(x, TOP)))
示例#8
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)
示例#9
0
    return app(x, QUOTE(true), QUOTE(false))


@combinator
def bool_if_true(x):
    x = bool_type(x)
    return unit_type(app(x, ok, undefined))


@combinator
def bool_if_false(x):
    x = bool_type(x)
    return unit_type(app(x, undefined, ok))


enum_bool = join_(APP(CI, true), APP(CI, false))


# ----------------------------------------------------------------------------
# Maybe

none = K


@combinator
def some(arg):
    return APP(K, APP(CI, arg))


@combinator
def maybe_type(x):
示例#10
0
from pomagma.reducer.sugar import _compile, app, as_term, combinator, join_
from pomagma.reducer.syntax import NVAR, TOP
from pomagma.util.testing import for_each

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


TERM_EXAMPLES = [
    (I, I),
    (I(K), K),
    (K, K),
    (app(S, I, I), app(S, I, I)),
    (join_(K, I), join_(K, I)),
    (S(I, I), app(S, I, I)),
    (K | I, join_(K, I)),
    # (TOP(K | I), TOP),
]

CLOSED_FUN_EXAMPLES = [
    (lambda: x, x),
    (lambda: B, B),
    (lambda x: x, I),
    (lambda x, y: x, K),
    (lambda x, y: y, KI),
    (lambda x: lambda y: x, K),
    (lambda x: lambda x: x, KI),
    (lambda x, y, z: app(x, z, y), C),
    (lambda x, y, z: app(x, app(y, z)), B),
示例#11
0
文件: lib.py 项目: fritzo/pomagma
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)
    all_defined = app(*tests)
    any_error = join_(*tests)
    return any_error(all_defined)
示例#12
0
from pomagma.reducer import oracle
from pomagma.reducer.code import QUOTE, QQUOTE, QAPP
from pomagma.reducer.code import TOP, BOT, I, K, B, C, NVAR
from pomagma.reducer.sugar import app, join_
from pomagma.util.testing import for_each
import pytest

F = app(K, I)
J = join_(K, F)
x = NVAR('x')
y = NVAR('y')


def box(x):
    return app(C, I, x)


ORDER_EXAMPLES = [
    (TOP, TOP, True, True),
    (TOP, BOT, False, True),
    (TOP, I, False, True),
    (TOP, K, False, True),
    (TOP, F, False, True),
    (TOP, J, False, True),
    (BOT, BOT, True, True),
    (BOT, I, True, False),
    (BOT, K, True, False),
    (BOT, F, True, False),
    (BOT, J, True, False),
    (I, I, True, True),
    (I, K, False, False),
示例#13
0
# Maybe


@for_each(
    [
        (lib.none, lib.none),
        (lib.some(undefined), lib.some(undefined)),
        (lib.some(error), lib.some(error)),
        (lib.some(ok), lib.some(ok)),
        (lib.some(true), lib.some(true)),
        (lib.some(false), lib.some(false)),
        (error, error),
        (undefined, undefined),
        (ok, error),
        (join, error),
        (join_(lib.none, lib.some(undefined)), error),
        (join_(lib.some(true), lib.some(false)), lib.some(join)),
    ]
)
def test_maybe_type(x, expected):
    assert simplify(lib.maybe_type(x)) == expected


@for_each([(lib.none, ok), (lib.some(x), ok), (error, error), (undefined, undefined)])
def test_maybe_test(x, expected):
    assert simplify(lib.maybe_test(x)) == expected


@for_each(
    [(lib.none, quote(lib.none)), (lib.some(true), quote(lib.some(true))), (undefined, undefined), (error, error)]
)
示例#14
0
def div(f):
    return join_(f, app(div, f, TOP))
示例#15
0
def enum_maybe(enum_item):
    return join_(box(none), enum_map(some, enum_item))
示例#16
0
def a_join(f, x, y):
    return app(f, join_(x, y))
示例#17
0
def enum_sum(enum_inl, enum_inr):
    return join_(enum_map(inl, enum_inl), enum_map(inr, enum_inr))
示例#18
0
def unit_or(x, y):
    return join_(x, y)
示例#19
0
def enum_num():
    return join_(box(zero), app(enum_map, succ, enum_num))
示例#20
0
    with pytest.raises(TypeError):
        _compile(arg)


@for_each(OPEN_FUN_EXAMPLES)
def test_combinator_must_be_closed(arg, expected):
    with pytest.raises(SyntaxError):
        combinator(arg).code


@combinator
def Y(f):
    return app(lambda x: app(f, app(x, x)), lambda x: app(f, app(x, x)))


div_Y = Y(lambda f, x: join_(x, app(f, app(x, TOP))))


@combinator
def div_rec_call(x):
    return join_(x, div_rec_call(app(x, TOP)))


@combinator
def div_rec_app(x):
    return join_(x, app(div_rec_app, app(x, TOP)))


@for_each([(div_rec_call, div_Y), (div_rec_app, div_Y)])
def test_combinator_recursion(arg, expected):
    actual = arg.code
示例#21
0
def enum_list(enum_item):
    return join_(
        box(nil),
        app(enum_list(enum_item), lambda t:
            app(enum_item, lambda h: box(cons(h, t)))),
    )
示例#22
0
def div_rec_app(x):
    return join_(x, app(div_rec_app, app(x, TOP)))
示例#23
0
  compose := (\f,f'. f\r,s. f'\r',s'. <r o r', s' o s>).
  A = A | <I, I> | <copy, join> | <div, BOT> | <BOT, TOP> | <C, C>
        | preconj A | postconj A | compose A A.

"""

from parsable import parsable

from pomagma.reducer.bohm import (print_tiny, sexpr_simplify, simplify,
                                  try_compute_step)
from pomagma.reducer.lib import BOT, TOP, B, C, I, box, pair
from pomagma.reducer.sugar import app, as_term, join_, rec
from pomagma.reducer.syntax import sexpr_print

CB = app(C, B)
div = rec(lambda a: join_(I, lambda x: app(a, x, TOP)))
copy = as_term(lambda x, y: app(x, y, y))
join = as_term(lambda x, y, z: app(x, join_(y, z)))
postconj = box(lambda r, s: pair(app(B, r), app(B, s)))
preconj = box(lambda r, s: pair(app(CB, s), app(CB, r)))
compose = as_term(lambda f1, f2:
                  app(f1, lambda r1, s1:
                      app(f2, lambda r2, s2:
                          pair(app(B, r1, r2), app(B, s2, s1)))))


# A = A | <I, I> | <copy, join> | <div, BOT> | <BOT, TOP> | <C, C>
#       | preconj A | postconj A | compose A A.
PARTS = {
    'base': as_term(lambda a: pair(I, I)),
    'copy': as_term(lambda a: pair(copy, join)),