| | from dreamcoder.program import Primitive, prettyProgram |
| | from dreamcoder.grammar import Grammar |
| | from dreamcoder.type import tlist, tint, arrow, baseType |
| |
|
| | |
| |
|
| |
|
| | |
| | int_to_int = baseType("int_to_int") |
| | int_to_bool = baseType("int_to_bool") |
| | int_to_int_to_int = baseType("int_to_int_to_int") |
| |
|
| |
|
| | |
| | Null = 300 |
| |
|
| | def _head(xs): return xs[0] if len(xs)>0 else Null |
| | def _tail(xs): return xs[-1] if len(xs)>0 else Null |
| | def _take(n): return lambda xs: xs[:n] |
| | def _drop(n): return lambda xs: xs[n:] |
| | def _access(n): return lambda xs: xs[n] if n>=0 and len(xs)>n else Null |
| | def _minimum(xs): return min(xs) if len(xs)>0 else Null |
| | def _maximum(xs): return max(xs) if len(xs)>0 else Null |
| | def _reverse(xs): return list(reversed(xs)) |
| | def _sort(xs): return sorted(xs) |
| | def _sum(xs): return sum(xs) |
| |
|
| | |
| | def _map(f): return lambda l: list(map(f, l)) |
| | def _filter(f): return lambda l: list(filter(f, l)) |
| | def _count(f): return lambda l: len([x for x in l if f(x)]) |
| | def _zipwith(f): return lambda xs: lambda ys: [f(x)(y) for (x, y) in zip(xs, ys)] |
| | def _scanl1(f): |
| | def _inner(xs): |
| | ys = [] |
| | if len(xs) > 0: |
| | ys.append(xs[0]) |
| | for i in range(1, len(xs)): |
| | ys.append( f(ys[i-1])(xs[i])) |
| | return ys |
| | return _inner |
| |
|
| | |
| | def _succ(x): return x+1 |
| | def _pred(x): return x-1 |
| | def _double(x): return x*2 |
| | def _half(x): return int(x/2) |
| | def _negate(x): return -x |
| | def _square(x): return x**2 |
| | def _triple(x): return x*3 |
| | def _third(x): return int(x/3) |
| | def _quad(x): return x*4 |
| | def _quarter(x): return int(x/4) |
| |
|
| | |
| | def _pos(x): return x>0 |
| | def _neg(x): return x<0 |
| | def _even(x): return x%2==0 |
| | def _odd(x): return x%2==1 |
| |
|
| | |
| | def _add(x): return lambda y: x + y |
| | def _sub(x): return lambda y: x - y |
| | def _mult(x): return lambda y: x * y |
| | def _min(x): return lambda y: _minimum([x,y]) |
| | def _max(x): return lambda y: _maximum([x,y]) |
| |
|
| | def deepcoderPrimitives(): |
| | return [ |
| | Primitive("HEAD", arrow(tlist(tint), tint), _head), |
| | Primitive("LAST", arrow(tlist(tint), tint), _tail), |
| | Primitive("TAKE", arrow(tint, tlist(tint), tlist(tint)), _take), |
| | Primitive("DROP", arrow(tint, tlist(tint), tlist(tint)), _drop), |
| | Primitive("ACCESS", arrow(tint, tlist(tint), tint), _access), |
| | Primitive("MINIMUM", arrow(tlist(tint), tint), _minimum), |
| | Primitive("MAXIMUM", arrow(tlist(tint), tint), _maximum), |
| | Primitive("REVERSE", arrow(tlist(tint), tlist(tint)), _reverse), |
| | Primitive("SORT", arrow(tlist(tint), tlist(tint)), _sort), |
| | Primitive("SUM", arrow(tlist(tint), tint), _sum) |
| | ] + [ |
| | Primitive("MAP", arrow(int_to_int, tlist(tint), tlist(tint)), _map), |
| | Primitive("FILTER", arrow(int_to_bool, tlist(tint), tlist(tint)), _filter), |
| | Primitive("COUNT", arrow(int_to_bool, tlist(tint), tint), _count), |
| | Primitive("ZIPWITH", arrow(int_to_int_to_int, tlist(tint), tlist(tint), tlist(tint)), _zipwith), |
| | Primitive("SCANL1", arrow(int_to_int_to_int, tlist(tint), tlist(tint)), _scanl1), |
| | ] + [ |
| | Primitive("INC", int_to_int, _succ), |
| | Primitive("DEC", int_to_int, _pred), |
| | Primitive("SHL", int_to_int, _double), |
| | Primitive("SHR", int_to_int, _half), |
| | Primitive("doNEG", int_to_int, _negate), |
| | Primitive("SQR", int_to_int, _square), |
| | Primitive("MUL3", int_to_int, _triple), |
| | Primitive("DIV3", int_to_int, _third), |
| | Primitive("MUL4", int_to_int, _quad), |
| | Primitive("DIV4", int_to_int, _quarter), |
| | ] + [ |
| | Primitive("isPOS", int_to_bool, _pos), |
| | Primitive("isNEG", int_to_bool, _neg), |
| | Primitive("isEVEN", int_to_bool, _even), |
| | Primitive("isODD", int_to_bool, _odd), |
| | ] + [ |
| | Primitive("+", int_to_int_to_int, _add), |
| | Primitive("-", int_to_int_to_int, _sub), |
| | Primitive("*", int_to_int_to_int, _mult), |
| | Primitive("MIN", int_to_int_to_int, _min), |
| | Primitive("MAX", int_to_int_to_int, _max) |
| | ] |
| |
|
| | def OldDeepcoderPrimitives(): |
| | return [ |
| | Primitive("head", arrow(tlist(tint), tint), _head), |
| | Primitive("tail", arrow(tlist(tint), tint), _tail), |
| | Primitive("take", arrow(tint, tlist(tint), tlist(tint)), _take), |
| | Primitive("drop", arrow(tint, tlist(tint), tlist(tint)), _drop), |
| | Primitive("access", arrow(tint, tlist(tint), tint), _access), |
| | Primitive("minimum", arrow(tlist(tint), tint), _minimum), |
| | Primitive("maximum", arrow(tlist(tint), tint), _maximum), |
| | Primitive("reverse", arrow(tlist(tint), tlist(tint)), _reverse), |
| | Primitive("sort", arrow(tlist(tint), tlist(tint)), _sort), |
| | Primitive("sum", arrow(tlist(tint), tint), _sum) |
| | ] + [ |
| | Primitive("map", arrow(int_to_int, tlist(tint), tlist(tint)), _map), |
| | Primitive("filter_int", arrow(int_to_bool, tlist(tint), tlist(tint)), _filter), |
| | Primitive("count", arrow(int_to_bool, tlist(tint), tint), _count), |
| | Primitive("zipwith", arrow(int_to_int_to_int, tlist(tint), tlist(tint), tlist(tint)), _zipwith), |
| | Primitive("scanl1", arrow(int_to_int_to_int, tlist(tint), tlist(tint)), _scanl1), |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | ] + [ |
| | Primitive("succ_fn", int_to_int, _succ), |
| | Primitive("pred_fn", int_to_int, _pred), |
| | Primitive("double_fn", int_to_int, _double), |
| | Primitive("half_fn", int_to_int, _half), |
| | Primitive("negate_fn", int_to_int, _negate), |
| | Primitive("square_fn", int_to_int, _square), |
| | Primitive("triple_fn", int_to_int, _triple), |
| | Primitive("third_fn", int_to_int, _third), |
| | Primitive("quad_fn", int_to_int, _quad), |
| | Primitive("quarter_fn", int_to_int, _quarter), |
| | ] + [ |
| | Primitive("pos_fn", int_to_bool, _pos), |
| | Primitive("neg_fn", int_to_bool, _neg), |
| | Primitive("even_fn", int_to_bool, _even), |
| | Primitive("odd_fn", int_to_bool, _odd), |
| | ] + [ |
| | Primitive("add_fn", int_to_int_to_int, _add), |
| | Primitive("sub_fn", int_to_int_to_int, _sub), |
| | Primitive("mult_fn", int_to_int_to_int, _mult), |
| | Primitive("min_fn", int_to_int_to_int, _min), |
| | Primitive("max_fn", int_to_int_to_int, _max) |
| | ] |
| |
|
| | def deepcoderProductions(): |
| | return [(0.0, prim) for prim in deepcoderPrimitives()] |
| |
|
| | def flatten_program(p): |
| | string = p.show(False) |
| | num_inputs = string.count('lambda') |
| | string = string.replace('lambda', '') |
| | string = string.replace('(', '') |
| | string = string.replace(')', '') |
| | |
| | for i in range(num_inputs): |
| | string = string.replace('$' + str(num_inputs-i-1),'input_' + str(i)) |
| | string = string.split(' ') |
| | string = list(filter(lambda x: x is not '', string)) |
| | return string |
| |
|
| | if __name__ == "__main__": |
| | |
| | g = Grammar.fromProductions(deepcoderProductions(), logVariable=.9) |
| | request = arrow(tlist(tint), tint, tint) |
| | p = g.sample(request) |
| | print("request:", request) |
| | print("program:") |
| | print(prettyProgram(p)) |
| | print("flattened_program:") |
| | flat = flatten_program(p) |
| | print(flat) |
| |
|
| | |
| |
|
| |
|
| |
|
| |
|
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| |
|