def test_refs()->None: e = let_('a', num(33), lambda a: let_('b', num(42), lambda b: intrin(MethodName("add"), [('a',a),('c',ref('c'))]))) assert refs_(e)==set([Ref('a'),Ref('b'),Ref('c')]) assert refs(e)==set([Ref('a'),Ref('c')]) assert decls(e)==set([Ref('a'),Ref('b')])
def test_eq()->None: assert let_('a', num(33), lambda x: x) == let_('a', num(33), lambda x: x) assert intrin(MethodName("add"), [('a',num(1)),('b',num(2))]) == \ intrin(MethodName("add"), [('a',num(1)),('b',num(2))]) assert ap(ref('a'),ref('b')) == ap(ref('a'),ref('b')) assert lam('a',lambda x:num(44)) == lam('a',lambda x:num(44)) assert lam('a',lambda x:num(44)) != lam('a',lambda x:num(0)) assert lam('a',lambda x:num(44)) != lam('b',lambda x:num(44))
def bin2expr(j: BIN) -> Expr: typ = j.tag if typ == Tag.val: vtyp = j.value.node.tag if vtyp == Tag.const: return num(j.value.node.value.int64) elif vtyp == Tag.ref: return ref(j.value.node.value.string) else: raise ValueError(f"Invalid value expression {_flat(j)}") elif typ == Tag.lam: return lam(j.value.tuple.v1.string, lambda _: bin2expr(j.value.tuple.v2.node)) elif typ == Tag.let: return let_(j.value.tuple.v1.string, bin2expr(j.value.tuple.v2.tuple.v1.node), lambda _: bin2expr(j.value.tuple.v2.tuple.v2.node)) elif typ == Tag.ap: return ap(bin2expr(j.value.tuple.v1.node), bin2expr(j.value.tuple.v2.node)) elif typ == Tag.intrin: return intrin(MethodName(str(j.value.tuple.v1.string)), [(str(v.tuple.v1.string), bin2expr(v.tuple.v2.node)) for v in j.value.tuple.v2.list.list]) else: raise ValueError(f"Invalid expression {_flat(j)}")
def test_serexpr(): def _test(ie): assert ie == jstr2expr(expr2jstr(ie)) assert ie == bin2expr(expr2bin(ie)) _test(num(33)) _test(ref('a')) _test(lam('b',lambda x:num(44))) _test(let_('a', num(33), lambda x: x)) _test(ap(ref('a'),ref('b'))) _test(intrin(MethodName('add'),[('a',num(1)),('b',ref('x'))]))
def gengather(top: Ref, mem: Mem) -> Iterator[Expr]: mentioned: Set[Ref] = {top} acc: Optional[Expr] = None for ref, expr in reversed(list(mem.items())): if ref in mentioned: mentioned |= refs(expr) if acc is None: acc = expr else: acc2: Expr = acc acc = let_(ref.name, expr, lambda x: acc2) yield acc
def gather(top: Ref, mem: Mem) -> Expr: """ Re-constructs complext expression using top-level reference `top` and the map of references `mem`. Return the final expression. """ mentioned: Set[Ref] = {top} acc: Optional[Expr] = None for ref, expr in reversed(list(mem.items())): if ref in mentioned: mentioned |= refs(expr) if acc is None: acc = expr else: acc2: Expr = acc acc = let_(ref.name, expr, lambda x: acc2) assert acc is not None return acc
def json2expr(j: dict) -> Expr: typ = j['t'] if typ == 'val': vtyp = j['val']['t'] if vtyp == 'const': return num(j['val']['val']) elif vtyp == 'ref': return ref(j['val']['val']) else: raise ValueError(f"Invalid value expression {j}") elif typ == 'lam': return lam(j['name'], lambda _: json2expr(j['body'])) elif typ == 'let': return let_(j['ref'], json2expr(j['expr']), lambda _: json2expr(j['body'])) elif typ == 'ap': return ap(json2expr(j['func']), json2expr(j['arg'])) elif typ == 'intrin': return intrin(MethodName(j['name']), [(k, json2expr(v)) for k, v in j['args']]) else: raise ValueError(f"Invalid expression {j}")
def test_print()->None: assert print_expr(intrin(MethodName("add"), [('a',num(0)),('b',ref('1'))])) == "add(a=0,b=1)" assert print_expr(let_('a',num(33),lambda a: num(42))) == "let a = 33 in 42" assert print_expr(ap(lam('a',lambda a: num(42)), num(33))) == "((a -> 42) 33)"
with Pool(2) as p: accs=p.map(_test_tmap_pickle, list(range(100)), chunksize=2) assert len(accs)==100 assert all([accs[i][i]==str(i) for i in range(100)]) def test_refs()->None: e = let_('a', num(33), lambda a: let_('b', num(42), lambda b: intrin(MethodName("add"), [('a',a),('c',ref('c'))]))) assert refs_(e)==set([Ref('a'),Ref('b'),Ref('c')]) assert refs(e)==set([Ref('a'),Ref('c')]) assert decls(e)==set([Ref('a'),Ref('b')]) EXPRS = [ intrin(MethodName("add"), [('a',num(0)),('b',ref('1'))]), let_('a',num(33),lambda a: num(42)), ap(lam('a',lambda a: num(42)), num(33)) ] def test_print()->None: assert print_expr(intrin(MethodName("add"), [('a',num(0)),('b',ref('1'))])) == "add(a=0,b=1)" assert print_expr(let_('a',num(33),lambda a: num(42))) == "let a = 33 in 42" assert print_expr(ap(lam('a',lambda a: num(42)), num(33))) == "((a -> 42) 33)" def test_gather()->None: mn = MethodName mem = Mem({ Ref('a'): num(33), Ref('b'): intrin(mn('neg'),[('a',ref('i'))]), Ref('c'): intrin(mn('add'),[('a',ref('a')),('b',ref('b'))]) })