def test_simple_autosubstitution(self): with SConf(substitution_threshold=2): ferb_1 = clean_tag("ferb_1", 1) shape = ferb_1.default_shape children = [nil()] new_shape, new_storage = shape.merge(children) # shape.record_shapes(new_storage) assert shape._hist == { (0, nil()._shape): 1, } assert new_shape is shape c = W_Constructor.construct(new_shape, new_storage) children_1 = [c] new_shape_1, new_storage_1 = shape.merge(children_1) # shape.record_shapes(new_storage_1) assert shape._hist == { (0, nil()._shape): 1, (0, shape): 1, } assert new_shape_1 is shape children_2 = [c] new_shape_2, new_storage_2 = shape.merge(children_2) # shape.record_shapes(new_shape_1, new_storage_1) # assert len(shape._hist) > 1 assert new_shape_2 is not shape
def make_map(): """ in scheme (define (map proc lis) (cond ((null? lis) '()) ((pair? lis) (cons (proc (car lis)) (map proc (cdr lis)))))) nil ≔ (nil) map ≔ λ: F, (cons X, Y) ↦ (cons μ(F, X), μ(map, F, Y)) _, nil ↦ nil """ f = Variable("F") x = Variable("X") y = Variable("Y") _ = Variable("_") _2 = Variable("_") m = lamb() m._name = "map" m._rules = rules([([p(f), p(cons("cons", x, y))], e(cons("cons", mu(f, [x]), mu(m, [f, y])))), ([p(_), p(nil())], e(nil()))]) return m
def test_counting(self): zork_2 = clean_tag("zork_2", 2) shape = zork_2.default_shape c = W_Constructor.construct(shape, [nil(), nil()]) shape.record_shapes([c, c]) assert shape._hist == { (0, shape): 1, (1, shape): 1, }
def test_bounded_deep_structures(self, no_cover): w_1 = integer(1) c = clean_tag("cons", 2) def _cons(*ch): children = list(ch) pre_shape = c.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr def _conslist(p_list): result = nil() for element in reversed(p_list): result = _cons(element, result) return result with SConf(substitution_threshold=17): def check_width(c, width): if isinstance(c, W_Constructor) and not is_nil(c): assert c.get_storage_width() < width # We deliberately use a n-ary Constructor, hence, # know that _structure is there for child in c._storage: check_width(child, width) sys.setrecursionlimit(100000) for num in [50, 100, 1000, 10000, 50000]: l = _cons(w_1, nil()) for i in range(num): l = _cons(w_1, l) check_width(l, 25)
def test_bounded_shallow_deep_structures(self): e = clean_tag("E", 0) def _e(): pre_shape = e.default_shape shape, storage = pre_shape.fusion([]) constr = W_Constructor.construct(shape, storage) return constr with SConf(substitution_threshold=17, max_shape_depth=7): sys.setrecursionlimit(100000) for num in [50, 100, 1000, 10000, 50000]: c = clean_tag("%d_cons" % num, 2) def _cons(*ch): children = list(ch) pre_shape = c.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr l = nil() for i in range(num): l = _cons(_e(), l) assert l.shape().shape_depth() <= 7
def test_post_constr_recursive_structures(self): c = clean_tag("cons", 2) e = clean_tag("E", 0) def _cons(car, cdr): children = [car, cdr] pre_shape = c.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr def _e(): pre_shape = e.default_shape shape, storage = pre_shape.fusion([]) constr = W_Constructor.construct(shape, storage) return constr # Be near immediate with SConf(substitution_threshold=2): l = _cons(_e(), _cons(_e(), _cons(_e(), _cons(_e(), nil())))) s = l.shape() assert len(s._structure) == 2 assert s._structure[0] == e.default_shape s2 = s._structure[1] assert len(s2._structure) == 2 assert s2._structure[1] == in_storage_shape assert s2._structure[0] == e.default_shape
def print_result_string(x, y, z): " A hacky primitive to quickly generate ReBench out format " from theseus.util.construction_helper import nil x *= 1000.0 y *= 1000.0 print "0:RESULT-cpu:ms: %s\n0:RESULT-total:ms: %s\n0:RESULT-gc:ms: %s" % ( x, y, z) return nil()
def test_simple_record(self): w_1 = integer(1) ferb_1 = clean_tag("ferb_0", 1) s = ferb_1.default_shape children = [w_1] new_shape, new_storage = s.merge(children) # s.record_shapes(new_storage) assert s._hist == {} children = [nil()] new_shape, new_storage = s.merge(children) # s.record_shapes(new_storage) assert s._hist == { (0, nil()._shape): 1, }
def prim_print_int(w_args): from theseus.model import W_Integer assert len(w_args) == 1 w_arg0 = w_args[0] assert isinstance(w_arg0, W_Integer) print w_arg0.value() return nil()
def make_reverse(): a1 = Variable("acc") a2 = Variable("acc") h = Variable("head") t = Variable("tail") reverse_acc = lamb() reverse_acc._name = "r_acc" reverse_acc._rules = rules([([p(nil()), p(a1)], e(a1)), ([p(cons("cons", h, t)), p(a2)], e(mu(reverse_acc, [t, cons("cons", h, a2)])))]) l = Variable("list") reverse = lamb() reverse._rules = rules([([p(l)], mu(reverse_acc, [l, nil()]))]) reverse._name = "reverse" return reverse
def peano_num(pynum): i = 0 res = nil() shape = None #peano_jit.can_enter_jit(num=pynum, shape=shape, i=i, res=res) while i < pynum: shape = res._shape #peano_jit.jit_merge_point(num=pynum, shape=shape, i=i, res=res) res = cons("p", res) i += 1 return res
def startup_peano(): if len(functions) != 0: return functions['zero'] = nil() functions['succ'] = make_succ() functions['pred'] = make_pred() functions['plus'] = make_plus() functions['plus_acc'] = make_plus_acc() functions['mult'] = make_mult() functions['mult_acc'] = make_mult_acc()
def make_append(): x1 = Variable("x") x2 = Variable("x") h = Variable("head") t = Variable("tail") l = lamb() l._name = "append" l._rules = rules([([p(nil()), p(x1)], e(x1)), ([p(cons("cons", h, t)), p(x2)], e(cons("cons", h, mu(l, [t, x2]))))]) return l
def test_map(self): """ in scheme (define (map proc lis) (cond ((null? lis) '()) ((pair? lis) (cons (proc (car lis)) (map proc (cdr lis)))))) nil ≔ (nil) map ≔ λ: F, (cons X, Y) ↦ (cons μ(F, X), μ(map, F, Y)) _, nil ↦ nil """ from mu.peano import startup_peano, _succ startup_peano() f = Variable("F") x = Variable("X") y = Variable("Y") _ = Variable("_") _2 = Variable("_") m = lamb() m._name = "map" m._rules = ziprules( ([f, cons("cons", x, y)], cons("cons", mu(f, [x]), mu(m, [f, y]))), ([_, nil()], nil())) x1 = Variable("x") list_w = [peano_num(1), peano_num(2), peano_num(3)] #list_w = [peano_num(1)] res = interpret_expression(mu(m, [_succ(), conslist(list_w)])) assert plist(res) == [peano_num(2), peano_num(3), peano_num(4)]
def test_recognize_unary_transformation(self): ferb_1 = clean_tag("ferb_1", 1) shape = ferb_1.default_shape children = [nil()] new_shape, new_storage = shape.merge(children) assert new_shape is shape assert new_storage == children shape.recognize_transformation(0, nil()._shape) new_shape, new_storage = shape.merge(children) assert shape.transformation_rules == { (0, nil()._shape): new_shape, } assert new_shape is not shape assert new_storage == [] shape.recognize_transformation(0, shape) c = W_Constructor.construct(shape, children) children_1 = [c] new_shape_1, new_storage_1 = shape.merge(children_1) assert shape.transformation_rules == { (0, nil()._shape): new_shape, (0, shape): new_shape_1, } assert new_shape_1 is not shape assert new_shape_1 is not new_shape assert new_storage_1 == children
def test_append(self): x1 = Variable("x") x2 = Variable("x") h = Variable("head") t = Variable("tail") l = lamb() l._rules = ziprules( ([nil(), x1], x1), ([cons("cons", h, t), x2], cons("cons", h, mu(l, [t, x2])))) list1_w = [integer(1), integer(2), integer(3)] list2_w = [integer(4), integer(5), integer(6)] assert plist(l.call([conslist(list1_w), conslist(list2_w)])) == list1_w + list2_w
def test_post_recursive_structures(self): c = clean_tag("cons", 2) def _cons(car, cdr): children = [car, cdr] pre_shape = c.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr # Be near immediate with SConf(substitution_threshold=2): assert len(c.default_shape.transformation_rules) == 0 assert len(c.default_shape._hist) == 0 cell = _cons(integer(1), nil()) assert len(c.default_shape.transformation_rules) == 0 assert len(c.default_shape._hist) == 1 cell2 = _cons(integer(1), cell) assert len(c.default_shape.transformation_rules) == 0 assert len(c.default_shape._hist) == 2 cell3 = _cons(integer(1), cell2) assert len(c.default_shape.transformation_rules) == 1 assert len(c.default_shape._hist) == 2 condition, result_shape = \ c.default_shape.transformation_rules.items()[0] assert condition[0] == 1 # pos assert condition[1] is c.default_shape # shape assert result_shape._tag is c # same tag assert len(result_shape._structure) == 2 assert result_shape._structure[0] is in_storage_shape assert result_shape._structure[1] is c.default_shape assert cell3._shape is result_shape
def test_append(self): x1 = Variable("x") x2 = Variable("x") h = Variable("head") t = Variable("tail") l = lamb() l._name = "append" l._rules = ziprules( ([nil(), x1], x1), ([cons("cons", h, t), x2], cons("cons", h, mu(l, [t, x2])))) list1_w = [integer(1), integer(2), integer(3)] list2_w = [integer(4), integer(5), integer(6)] res = interpret_expression( mu(l, [conslist(list1_w), conslist(list2_w)])) assert plist(res) == list1_w + list2_w
def test_eventual_convergence(self): e = clean_tag("E", 0) def _e(): pre_shape = e.default_shape shape, storage = pre_shape.fusion([]) constr = W_Constructor.construct(shape, storage) return constr n = clean_tag("Node", 3) def _node(left, value, right): children = [left, value, right] pre_shape = n.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr # Be near immediate with SConf(substitution_threshold=2, log_transformations=True): # Warm the rules as to eventually have all E and nil elided. for _ in range(25): n1 = _node(nil(), _e(), nil()) n2 = _node(nil(), _e(), nil()) n3 = _node(n1, _e(), n2) n4 = _node(nil(), _e(), nil()) n5 = _node(nil(), _e(), nil()) n6 = _node(n4, _e(), n5) n7 = _node(n3, _e(), n6) n8 = _node(nil(), _e(), nil()) n9 = _node(nil(), _e(), nil()) nA = _node(n8, _e(), n9) nB = _node(nil(), _e(), nil()) nC = _node(nil(), _e(), nil()) nD = _node(nB, _e(), nC) nE = _node(nA, _e(), nD) tree = _node(n7, _e(), nE) # everything elided... assert len(tree._storage) == 0 # ...but still reifyable assert tree.get_number_of_children() == 3
def make_gc_bench(): w_tStart_v0 = Variable("tStart") w_tStart_v1 = Variable("tStart") w_tStart_v2 = Variable("tStart") w_tFinish_v0 = Variable("tFinish") w_root_v0 = Variable("root") w_root_v1 = Variable("root") w_kStretchTreeDepth_v0 = Variable("kStretchTreeDepth") w_iDepth_v0 = Variable("iDepth") w_Make_Tree = lamb() w_Make_Tree._name = "MakeTree" w_Make_Tree._rules = rules([ ([p(integer(0))], e(cons("Node", nil(), nil()))), ([p(w_iDepth_v0)], e( cons("Node", mu(w_Make_Tree, [mu(_minus(), [w_iDepth_v0, integer(1)])]), mu(w_Make_Tree, [mu(_minus(), [w_iDepth_v0, integer(1)])])))) ]) w_gc_bench_cont1 = lamb() w_gc_bench_cont1._name = "gc_bench$cont0" w_gc_bench_cont1._rules = rules([( [p(w_tStart_v2), p(w_tFinish_v0), p(w_root_v1)], # we 'un-reference' e(mu(_print_int(), [mu(_minus(), [w_tFinish_v0, w_tStart_v2])])))]) w_gc_bench_cont0 = lamb() w_gc_bench_cont0._name = "gc_bench$cont0" w_gc_bench_cont0._rules = rules([([p(w_tStart_v1), p(w_root_v0)], e( mu(w_gc_bench_cont1, [ w_tStart_v1, mu(_currentmilliseconds(), []), w_root_v0, ])))]) w_gc_bench_let0 = lamb() w_gc_bench_let0._name = "gc_bench$let0" w_gc_bench_let0._rules = rules([ ([p(w_kStretchTreeDepth_v0), p(w_tStart_v0)], e( mu(w_gc_bench_cont0, [w_tStart_v0, mu(w_Make_Tree, [w_kStretchTreeDepth_v0])]))) ]) w_gc_bench = lamb() w_gc_bench._name = "gc_bench" w_gc_bench._rules = rules([( [], e( mu( w_gc_bench_let0, [ integer(18), # kStretchTreeDepth mu(_currentmilliseconds(), []), # tStart ])))]) return w_gc_bench
def _conslist(p_list): result = nil() for element in reversed(p_list): result = _cons(element, result) return result
def test_recognize_deep_structures(self): w_1 = integer(1) c = clean_tag("cons", 2) def _cons(*children): ch = list(children) pre_shape = c.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr def _conslist(p_list): result = nil() for element in reversed(p_list): result = _cons(element, result) return result with SConf(substitution_threshold=2): # print "" cons_0 = _cons(w_1, nil()) assert cons_0.shape() == c.default_shape print cons_0.shape() print c.default_shape._hist assert c.default_shape.transformation_rules == {} cons_1 = _cons(w_1, cons_0) assert cons_1.shape() == c.default_shape assert cons_1.shape() == cons_0.shape() print cons_1.shape() print c.default_shape._hist assert c.default_shape.transformation_rules == {} cons_2 = _cons(w_1, cons_1) assert cons_2.shape() != c.default_shape assert cons_2.shape() != cons_0.shape() assert cons_2.shape() != cons_1.shape() print cons_2.shape() print c.default_shape._hist assert c.default_shape.transformation_rules == { (1, c.default_shape): cons_2.shape(), } cons_3 = _cons(w_1, cons_2) print cons_3.shape() print c.default_shape._hist assert c.default_shape.transformation_rules == { (1, c.default_shape): cons_2.shape(), } cons_4 = _cons(w_1, cons_3) print cons_4.shape() print c.default_shape._hist assert c.default_shape.transformation_rules == { (1, c.default_shape): cons_2.shape(), } cons_5 = _cons(w_1, cons_4) print cons_5.shape() print c.default_shape._hist assert c.default_shape.transformation_rules == { (1, c.default_shape): cons_2.shape(), (1, cons_2.shape()): cons_5.shape(), }
def test_reverse(self): debug = False if debug: print "" a1 = Variable("accumulator") a2 = Variable("accumulator") h = Variable("head") t = Variable("tail") w_nil_shape = nil().shape() c = clean_tag("cons", 2) def _cons(*children): ch = list(children) constr = W_Constructor.construct(c.default_shape, ch) return constr def _conslist(p_list): result = nil() for element in reversed(p_list): result = _cons(element, result) return result cons_shape = c.default_shape with SConf(substitution_threshold=sys.maxint): cons_1_shape = CompoundShape(c, [in_storage_shape, cons_shape]) cons_2_shape = CompoundShape(c, [in_storage_shape, cons_1_shape]) cons_3_shape = CompoundShape(c, [in_storage_shape, cons_2_shape]) cons_4_shape = CompoundShape(c, [in_storage_shape, cons_3_shape]) cons_5_shape = CompoundShape(c, [in_storage_shape, cons_4_shape]) cons_1_shape._config.substitution_threshold = sys.maxint cons_2_shape._config.substitution_threshold = sys.maxint cons_3_shape._config.substitution_threshold = sys.maxint cons_4_shape._config.substitution_threshold = sys.maxint cons_5_shape._config.substitution_threshold = sys.maxint cons_shape.transformation_rules[(1, cons_shape)] = cons_1_shape cons_shape.transformation_rules[(1, cons_1_shape)] = cons_2_shape cons_shape.transformation_rules[(1, cons_2_shape)] = cons_3_shape # cons_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape # cons_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape cons_1_shape.transformation_rules[(1, cons_1_shape)] = cons_2_shape cons_1_shape.transformation_rules[(1, cons_2_shape)] = cons_3_shape # cons_1_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape # cons_1_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape cons_2_shape.transformation_rules[(1, cons_2_shape)] = cons_3_shape # cons_2_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape # cons_2_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape # cons_3_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape # cons_3_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape # cons_4_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape reverse_acc = lamb() reverse_acc._name = "reverse_acc" reverse_acc._rules = ziprules( ([nil(), a1], a1), ([_cons(h, t), a2], mu(reverse_acc, [t, _cons(h, a2)])), ) l = Variable("l") reverse = lamb(([l], mu(reverse_acc, [l, nil()]))) reverse._name = "reverse" nums = 50 list1_w = [integer(x) for x in range(nums)] clist1_w = _conslist(list1_w) assert clist1_w.get_tag() is c res = interpret_expression(mu(reverse, [clist1_w])) list1_w.reverse() assert plist(res) == list1_w
def print_string(x): print x return nil()
def test_multi_recursive_structures(self): n = clean_tag("Node", 3) def _node(left, value, right): children = [left, value, right] pre_shape = n.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr def _e(): return integer(1) # Be near immediate with SConf(substitution_threshold=2): n1 = _node( _node( _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil())), _e(), _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil()))), _e(), _node( _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil())), _e(), _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil())))) n2 = _node( _node( _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil())), _e(), _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil()))), _e(), _node( _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil())), _e(), _node(_node(nil(), _e(), nil()), _e(), _node(nil(), _e(), nil())))) tree = _node(n1, _e(), n2) s = tree.shape() assert len(s._structure) == 3 assert s._structure[0] == n.default_shape assert s._structure[1] == in_storage_shape assert s._structure[2] == n.default_shape
def test_multi_constr_recursive_structures(self): py.test.skip("Can't do that, yet :(") e = clean_tag("E", 0) def _e(): pre_shape = e.default_shape shape, storage = pre_shape.fusion([]) constr = W_Constructor.construct(shape, storage) return constr n = clean_tag("Node", 3) def _node(left, value, right): children = [left, value, right] pre_shape = n.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr # Be near immediate with SConf(substitution_threshold=2, log_transformations=True): n1 = _node(nil(), _e(), nil()) n2 = _node(nil(), _e(), nil()) n3 = _node(n1, _e(), n2) n4 = _node(nil(), _e(), nil()) n5 = _node(nil(), _e(), nil()) n6 = _node(n4, _e(), n5) n7 = _node(n3, _e(), n6) n8 = _node(nil(), _e(), nil()) n9 = _node(nil(), _e(), nil()) nA = _node(n8, _e(), n9) nB = _node(nil(), _e(), nil()) nC = _node(nil(), _e(), nil()) nD = _node(nB, _e(), nC) nE = _node(nA, _e(), nD) tree = _node(n7, _e(), nE) s = tree.shape() assert len(s._structure) == 3 s0 = s._structure[0] assert s0 is not in_storage_shape assert s0._structure[0] is in_storage_shape assert s0._structure[1] is e.default_shape assert s0._structure[2] is in_storage_shape s1 = s._structure[1] # assert s1 is e.default_shape #?? assert s1 is in_storage_shape s2 = s._structure[2] assert s2 is not in_storage_shape assert s2._structure[0] is in_storage_shape assert s2._structure[1] is e.default_shape assert s2._structure[2] is in_storage_shape
def print_int(x): print x return nil()