Example #1
0
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))
Example #2
0
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'))]))
Example #3
0
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'))])
  })
  expr = gather(Ref('c'), mem)
  assert extrefs(expr)==set([Ref('i')])
  iexpr,_ = interp(expr, lib_arith, mkmap({Ref('i'):ival(32)}))
  assert iexpr == IVal(1)
Example #4
0
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')])
Example #5
0
def test_example():
  e=Example(inp=mkmap({Ref('a'):ival(0)}),
            expr=ap(ref('a'),ref('b')),
            out=IVal(33))

  with open('/tmp/binfile','wb') as f:
    add=examples2fd(f)
    add(e)
    add(e)

  with open('/tmp/binfile','rb') as f:
    _next=fd2examples(f)
    n=_next()
    assert n==e
    n=_next()
    assert n==e
Example #6
0
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)}")
Example #7
0
def test_seriexpr():
  def _test(ie):
    assert ie == bin2iexpr(iexpr2bin(ie))
    assert ie == jstr2iexpr(iexpr2jstr(ie))
  _test(IVal(33))
  _test(IVal("foo"))
  _test(IAp(ILam("pat", intrin(MethodName('neg'),[('a',ref('pat'))])), IVal(42)))
  _test(IError("the message"))
Example #8
0
def test_gengather()->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'))])
  })
  imem = IMem({
    Ref('i'): IVal(32),
    Ref('a'): IVal(33),
    Ref('b'): IVal(-32),
    Ref('c'): IVal(1)
  })
  exprs = gengather(Ref('c'), mem)
  for e in exprs:
    inps = IMem({k:v for k,v in imem.items() if k in extrefs(e)})
    iexpr,_ = interp(e, lib_arith, inps)
    print(print_expr(e), iexpr, inps.dict)
    assert iexpr == imem[Ref('c')]
Example #9
0
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}")
Example #10
0
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)"
Example #11
0
  accs=[]
  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'))])