def decode_prod(decode_fst, decode_snd): x = unification.var('x') y = unification.var('y') pair_pattern = lib.pair(x, y) def decode(code): match = unification.unify(pair_pattern, code) if not match: raise TypeError(code) x_value = decode_fst(match[x]) y_value = decode_snd(match[y]) return (x_value, y_value) return decode
def decode_prod(decode_fst, decode_snd): x = NVAR('x') y = NVAR('y') pair_pattern = lib.pair(x, y) def decode(term): match = pattern.match(pair_pattern, term) if match is None: raise TypeError(pretty(term)) x_value = decode_fst(match[x]) y_value = decode_snd(match[y]) return (x_value, y_value) return decode
], 'encode_error': [0, ['a'], 'asdf'], 'decode_error': [I, K, B, C, APP(C, B)], }, 'num': { 'ok': [ (lib.zero, 0), (lib.succ(lib.zero), 1), (lib.succ(lib.succ(lib.zero)), 2), (lib.succ(lib.succ(lib.succ(lib.zero))), 3), ], 'encode_error': [None, False, True, [0], [True]], }, ('prod', 'bool', 'num'): { 'ok': [ (lib.pair(lib.true, lib.zero), (True, 0)), (lib.pair(lib.false, lib.succ(lib.succ(lib.zero))), (False, 2)), ], 'encode_error': [None, (), (True,), [True, 0], True, 0, 'asdf'] }, ('sum', 'bool', 'num'): { 'ok': [ (lib.inl(lib.true), (True, True)), (lib.inl(lib.false), (True, False)), (lib.inr(lib.zero), (False, 0)), (lib.inr(lib.succ(lib.succ(lib.zero))), (False, 2)), ], 'encode_error': [None, (), (True,), [True, 0], True, 0, 'asdf'] }, ('maybe', 'bool'): { 'ok': [
def encode(value): if not (isinstance(value, tuple) and len(value) == 2): raise TypeError(value) code_fst = encode_fst(value[0]) code_snd = encode_snd(value[1]) return lib.pair(code_fst, code_snd)
(lib.enum_unit, undefined, true), (lib.enum_unit, error, false), (lib.enum_unit, lib.some(ok), true), (lib.enum_unit, lib.some(undefined), true), (lib.enum_unit, lib.some(error), false), ] ) def test_enum_maybe(enum_item, y, expected): qxs = quote(lib.enum_maybe(enum_item)) assert simplify(lib.enum_contains(qxs, quote(y))) == expected # ---------------------------------------------------------------------------- # Products xy = lib.pair(x, y) @for_each([(xy, ok), (error, error), (undefined, undefined)]) def test_prod_test(x, expected): assert simplify(lib.prod_test(x)) == expected @for_each([(xy, x), (error, error), (undefined, undefined)]) def test_prod_fst(x, expected): assert simplify(lib.prod_fst(x)) == expected @for_each([(xy, y), (error, error), (undefined, undefined)]) def test_prod_snd(x, expected): assert simplify(lib.prod_snd(x)) == expected
(lib.enum_unit, lib.none, true), (lib.enum_unit, undefined, true), (lib.enum_unit, error, false), (lib.enum_unit, lib.some(ok), true), (lib.enum_unit, lib.some(undefined), true), (lib.enum_unit, lib.some(error), false), ]) def test_enum_maybe(enum_item, y, expected): qxs = quote(lib.enum_maybe(enum_item)) assert simplify(lib.enum_contains(qxs, quote(y))) == expected # ---------------------------------------------------------------------------- # Products xy = lib.pair(x, y) @for_each([ (xy, ok), (error, error), (undefined, undefined), ]) def test_prod_test(x, expected): assert simplify(lib.prod_test(x)) == expected @for_each([ (xy, x), (error, error), (undefined, undefined),
""" 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)), 'div': as_term(lambda a: pair(div, BOT)), 'bot': as_term(lambda a: pair(BOT, TOP)), 'swap': as_term(lambda a: pair(C, C)),