Example #1
0
 def encode(value):
     if not (isinstance(value, tuple) and len(value) == 2 and
             isinstance(value[0], bool)):
         raise TypeError(value)
     if value[0]:
         return lib.inl(encode_inl(value[1]))
     else:
         return lib.inr(encode_inr(value[1]))
Example #2
0
def decode_sum(decode_inl, decode_inr):
    x = unification.var('x')
    inl_pattern = lib.inl(x)
    inr_pattern = lib.inr(x)

    def decode(code):
        match = unification.unify(inl_pattern, code)
        if match:
            return (True, decode_inl(match[x]))
        match = unification.unify(inr_pattern, code)
        if match:
            return (False, decode_inr(match[x]))
        raise TypeError(code)

    return decode
Example #3
0
def decode_sum(decode_inl, decode_inr):
    x = NVAR('x')
    inl_pattern = lib.inl(x)
    inr_pattern = lib.inr(x)

    def decode(term):
        match = pattern.match(inl_pattern, term)
        if match:
            return (True, decode_inl(match[x]))
        match = pattern.match(inr_pattern, term)
        if match:
            return (False, decode_inr(match[x]))
        raise TypeError(pretty(term))

    return decode
Example #4
0
         (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': [
         (lib.none, None),
         (lib.some(lib.true), (True,)),
         (lib.some(lib.false), (False,)),
     ],
     'encode_error': [True, False, 0, 1, 2, (), 'asdf'],
     'decode_error': [I],
 },
 ('list', 'bool'): {
     'ok': [
Example #5
0
        (lib.enum_unit, lib.enum_bool, lib.pair(ok, true), true),
        (lib.enum_unit, lib.enum_bool, lib.pair(ok, false), true),
        (lib.enum_unit, lib.enum_bool, lib.pair(undefined, error), false),
        (lib.enum_unit, lib.enum_bool, lib.pair(error, undefined), false),
    ]
)
def test_enum_prod(enum_fst, enum_snd, y, expected):
    qxs = quote(lib.enum_prod(enum_fst, enum_snd))
    assert simplify(lib.enum_contains(qxs, quote(y))) == expected


# ----------------------------------------------------------------------------
# Sums


@for_each([(lib.inl(x), ok), (lib.inr(y), ok), (error, error), (undefined, undefined)])
def test_sum_test(x, expected):
    assert simplify(lib.sum_test(x)) == expected


@for_each(
    [
        (lib.inl(ok), quote(lib.inl(ok))),
        (lib.inr(true), quote(lib.inr(true))),
        (lib.inr(false), quote(lib.inr(false))),
        (undefined, undefined),
        (error, error),
    ]
)
def test_sum_quote(x, expected):
    quote_inl = lib.unit_quote
Example #6
0
    (lib.enum_unit, lib.enum_bool, lib.pair(ok, true), true),
    (lib.enum_unit, lib.enum_bool, lib.pair(ok, false), true),
    (lib.enum_unit, lib.enum_bool, lib.pair(undefined, error), false),
    (lib.enum_unit, lib.enum_bool, lib.pair(error, undefined), false),
])
def test_enum_prod(enum_fst, enum_snd, y, expected):
    qxs = quote(lib.enum_prod(enum_fst, enum_snd))
    assert simplify(lib.enum_contains(qxs, quote(y))) == expected


# ----------------------------------------------------------------------------
# Sums

@for_each([
    (lib.inl(x), ok),
    (lib.inr(y), ok),
    (error, error),
    (undefined, undefined),
])
def test_sum_test(x, expected):
    assert simplify(lib.sum_test(x)) == expected


@for_each([
    (lib.inl(ok), quote(lib.inl(ok))),
    (lib.inr(true), quote(lib.inr(true))),
    (lib.inr(false), quote(lib.inr(false))),
    (undefined, undefined),
    (error, error),
])
def test_sum_quote(x, expected):