def reducedConcatPrimitives(): #uses strConcat!! #[Primitive("empty_string", arrow(tpregex, tpregex), PRC(pregex.String(""))) #] + [ return [ Primitive("string_" + i, arrow(tpregex, tpregex), PRC( pregex.String(i))) for i in printable[:-4] if i not in disallowed_list ] + [ Primitive("string_" + name, arrow(tpregex, tpregex), PRC(pregex.String(char))) for char, name in disallowed ] + [ Primitive("r_dot", arrow(tpregex, tpregex), PRC(pregex.dot)), Primitive("r_d", arrow(tpregex, tpregex), PRC(pregex.d)), Primitive("r_s", arrow(tpregex, tpregex), PRC(pregex.s)), #Primitive("r_w", arrow(tpregex, tpregex), PRC(pregex.w)), Primitive("r_l", arrow(tpregex, tpregex), PRC(pregex.l)), Primitive("r_u", arrow(tpregex, tpregex), PRC(pregex.u)), #todo Primitive("r_kleene", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.KleeneStar, 1)), #Primitive("r_plus", arrow(arrow(tpregex, tpregex), arrow(tpregex,tpregex)), PRC(pregex.Plus,1)), #Primitive("r_maybe", arrow(arrow(tpregex, tpregex), arrow(tpregex,tpregex)), PRC(pregex.Maybe,1)), Primitive( "r_alt", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Alt, 2)), ] + [Primitive("r_const", arrow(tpregex, tpregex), None)]
def McCarthyPrimitives(): "These are < primitives provided by 1959 lisp as introduced by McCarthy" return [ Primitive("if", arrow(tbool, t0, t0, t0), _if), primitiveRecursion1, primitiveRecursion2, # Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("positive?", arrow(tint, tbool), _positive), Primitive("incr", arrow(tint, tint), _succ), Primitive("decr", arrow(tint, tint), _desc) ] + [Primitive(str(j), tint, j) for j in range(2)]
def easyWordsPrimitives(): return [ Primitive("string_" + i, tpregex, pregex.String(i)) for i in printable[10:62] if i not in disallowed_list ] + [ Primitive("r_d", tpregex, pregex.d), Primitive("r_s", tpregex, pregex.s), #Primitive("r_w", tpregex, pregex.w), Primitive("r_l", tpregex, pregex.l), Primitive("r_u", tpregex, pregex.u), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), Primitive("r_plus", arrow(tpregex, tpregex), _plus), Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ]
def McCarthyPrimitives(): "These are < primitives provided by 1959 lisp as introduced by McCarthy" primitives = [ Primitive("0", tint, 0), Primitive("incr", arrow(tint, tint), _incr), Primitive("decr0", arrow(tint, tint), _decr0), # Primitive("if", arrow(tbool, t0, t0, t0), _if), # Primitive("eq0", arrow(tint, tbool), _eq0), Primitive("if0", arrow(t0, t0, t0, t0), _if0), # primitiveRecursion1, primitiveRecursion2, # Primitive("gt?", arrow(tint, tint, tbool), _gt), # Primitive("positive?", arrow(tint, tbool), _positive), # Primitive("+", arrow(tint, tint, tint), _add), # Primitive("-0", arrow(tint, tint, tint), _minus0), ] return primitives
def matchEmpericalNoLetterPrimitives(corpus): return lambda: [ Primitive("empty_string", tpregex, pregex.String("")) ] + [ Primitive("string_" + i, tpregex, pregex.String(i)) for i in printable[:-4] if i not in disallowed_list + list(printable[10:62]) ] + [ Primitive("string_" + name, tpregex, pregex.String(char)) for char, name in disallowed ] + [ Primitive("r_dot", tpregex, emp_dot_no_letter(corpus) ), Primitive("r_d", tpregex, emp_d(corpus) ), Primitive("r_s", tpregex, pregex.s), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), #Primitive("r_plus", arrow(tpregex, tpregex), _plus), #Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ]
def algolispPrimitives(): return [ Primitive("fn_call", arrow(tfunction, tlist(tsymbol), tsymbol), _fn_call), Primitive("lambda1_call", arrow(tfunction, tlist(tsymbol), tsymbol), lambda f: lambda sx: ["lambda1", [f] + sx] if type(sx)==list else ["lambda1", [f] + [sx]] ), Primitive("lambda2_call", arrow(tfunction, tlist(tsymbol), tsymbol), lambda f: lambda sx: ["lambda2", [f] + sx] if type(sx)==list else ["lambda2", [f] + [sx]] ), #symbol converters: # SYMBOL = constant | argument | function_call | function | lambda Primitive("symbol_constant", arrow(tconstant, tsymbol), lambda x: x), Primitive("symbol_function", arrow(tfunction, tsymbol), lambda x: x), #list converters Primitive('list_init_symbol', arrow(tsymbol, tlist(tsymbol)), lambda symbol: [symbol] ), Primitive('list_add_symbol', arrow(tsymbol, tlist(tsymbol), tlist(tsymbol)), lambda symbol: lambda symbols: symbols + [symbol] if type(symbols) == list else [symbols] + [symbol]) ] + [ #functions: Primitive(ec_name, tfunction, algo_name) for algo_name, ec_name in fn_lookup.items() ] + [ #Constants Primitive(ec_name, tconstant, algo_name) for algo_name, ec_name in const_lookup.items() ]
def bootstrapTarget_extra(): """This is the bootstrap target plus list domain specific stuff""" return bootstrapTarget() + [ Primitive("*", arrow(tint, tint, tint), _multiplication), Primitive("mod", arrow(tint, tint, tint), _mod), Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("eq?", arrow(tint, tint, tbool), _eq), Primitive("is-prime", arrow(tint, tbool), _isPrime), Primitive("is-square", arrow(tint, tbool), _isSquare), ]
def no_length(): """this is the primitives without length because one of the reviewers wanted this""" return [p for p in bootstrapTarget() if p.name != "length"] + [ Primitive("*", arrow(tint, tint, tint), _multiplication), Primitive("mod", arrow(tint, tint, tint), _mod), Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("eq?", arrow(tint, tint, tbool), _eq), Primitive("is-prime", arrow(tint, tbool), _isPrime), Primitive("is-square", arrow(tint, tbool), _isSquare), ]
def napsPrimitives(): return [ Primitive("program", arrow(tlist(RECORD), tlist(FUNC), PROGRAM), _program), # TODO # RECORD Primitive( "func", arrow(TYPE, name, tlist(VAR), tlist(VAR), tlist(VAR), tlist(STMT)), _func('func')), # TODO Primitive( "ctor", arrow(TYPE, name, tlist(VAR), tlist(VAR), tlist(VAR), tlist(STMT)), _func('ctor')), Primitive("var", arrow(TYPE, name, VAR), _var) ] + [ # STMT ::= EXPR | IF | FOREACH | WHILE | BREAK | CONTINUE | RETURN | NOOP Primitive("stmt_expr", arrow(EXPR, STMT), lambda x: x), Primitive("stmt_if", arrow(IF, STMT), lambda x: x), Primitive("stmt_foreach", arrow(FOREACH, STMT), lambda x: x), Primitive("stmt_while", arrow(WHILE, STMT), lambda x: x), Primitive("stmt_break", arrow(BREAK, STMT), lambda x: x), Primitive("stmt_continue", arrow(CONTINUE, STMT), lambda x: x), Primitive("stmt_return", arrow(RETURN, STMT), lambda x: x), Primitive("stmt_noop", arrow(NOOP, STMT), lambda x: x) ] + [ # EXPR ::= ASSIGN | VAR | FIELD | CONSTANT | INVOKE | TERNARY | CAST Primitive("expr_assign", arrow(ASSIGN, EXPR), lambda x: x), Primitive("expr_var", arrow(VAR, EXPR), lambda x: x), Primitive("expr_field", arrow(FIELD, EXPR), lambda x: x), Primitive("expr_constant", arrow(CONSTANT, EXPR), lambda x: x), Primitive("expr_invoke", arrow(INVOKE, EXPR), lambda x: x), Primitive("expr_ternary", arrow(TERNARY, EXPR), lambda x: x), Primitive("expr_cast", arrow(CAST, EXPR), lambda x: x) ] + [Primitive("assign", arrow(TYPE, LHS, EXPR, ASSIGN), _assign)] + [ # LHS ::= VAR | FIELD | INVOKE Primitive("lhs_var", arrow(VAR, LHS), lambda x: x), Primitive("lhs_field", arrow(FIELD, LHS), lambda x: x), Primitive("lhs_invoke", arrow(INVOKE, LHS), lambda x: x) ] + [ Primitive("if", arrow(TYPE, EXPR, tlist(STMT), tlist(STMT), IF), _if), Primitive("foreach", arrow(TYPE, VAR, EXPR, tlist(STMT), FOREACH), _foreach), Primitive("while", arrow(TYPE, EXPR, tlist(STMT), tlist(STMT), WHILE), _while), Primitive("break", arrow(TYPE, BREAK), lambda tp: ['break', tp]), Primitive("continue", arrow(TYPE, CONTINUE), lambda tp: ['continue', tp]), Primitive("return", arrow(TYPE, EXPR, RETURN), _return), Primitive("noop", NOOP, ['noop']), Primitive("field", arrow(TYPE, EXPR, field_name, FIELD), _field), # TODO Primitive("constant", arrow(TYPE, value, CONSTANT), _constant), Primitive("invoke", arrow(TYPE, function_name, tlist(EXPR), INVOKE), _invoke), # TODO Primitive("ternary", arrow(TYPE, EXPR, EXPR, EXPR, TERNARY), _ternary), Primitive("cast", arrow(TYPE, EXPR, CAST), _cast) ] + [ # below are TYPE: Primitive("bool", TYPE, 'bool'), Primitive("char", TYPE, 'char'), Primitive("char*", TYPE, 'char*'), Primitive("int", TYPE, 'int'), Primitive("real", TYPE, 'real'), Primitive("array", arrow(TYPE, TYPE), lambda tp: tp + '*'), Primitive("set", arrow(TYPE, TYPE), lambda tp: tp + '%'), Primitive("map", arrow(TYPE, TYPE, TYPE), lambda tp1: lambda tp2: '<' + tp1 + '|' + tp2 + '>'), Primitive("record_name", TYPE, 'record_name#') # TODO ] + [ #stuff about lists: # STMTs, EXPRs, VARs, maybe Funcs and records Primitive('list_init_stmt', arrow(STMT, tlist(STMT)), lambda stmt: [stmt]), Primitive('list_add_stmt', arrow(STMT, tlist(STMT), tlist(STMT)), lambda stmt: lambda stmts: stmts + [stmt]), Primitive('list_init_expr', arrow(EXPR, tlist(EXPR)), lambda expr: [expr]), Primitive('list_add_expr', arrow(EXPR, tlist(EXPR), tlist(EXPR)), lambda expr: lambda exprs: exprs + [expr]), Primitive('list_init_var', arrow(VAR, tlist(VAR)), lambda var: [var]), Primitive('list_add_var', arrow(VAR, tlist(VAR), tlist(VAR)), lambda var: lambda _vars: _vars + [var]) ] + [ # value Primitive('0', value, 0), Primitive("1", value, "1"), Primitive("-1", value, "-1") # ... ] + [ # function_name: Primitive('+', function_name, '+'), Primitive('&&', function_name, "&&"), Primitive("!", function_name, "!"), Primitive("!=", function_name, "!="), Primitive("string_find", function_name, "string_find") # ... ] + [ # field_name: Primitive('', field_name, '') # ... ] + [ # Primitive(f'var{str(i)}', name, f'var{str(i)}') for i in range(12) ]
def bootstrapTarget(): """These are the primitives that we hope to learn from the bootstrapping procedure""" return [ # learned primitives Primitive("map", arrow(arrow(t0, t1), tlist(t0), tlist(t1)), _map), Primitive("unfold", arrow(t0, arrow(t0,tbool), arrow(t0,t1), arrow(t0,t0), tlist(t1)), _unfold), Primitive("range", arrow(tint, tlist(tint)), _range), Primitive("index", arrow(tint, tlist(t0), t0), _index), Primitive("fold", arrow(tlist(t0), t1, arrow(t0, t1, t1), t1), _fold), Primitive("length", arrow(tlist(t0), tint), len), # built-ins Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("-", arrow(tint, tint, tint), _subtraction), Primitive("empty", tlist(t0), []), Primitive("cons", arrow(t0, tlist(t0), tlist(t0)), _cons), Primitive("car", arrow(tlist(t0), t0), _car), Primitive("cdr", arrow(tlist(t0), tlist(t0)), _cdr), Primitive("empty?", arrow(tlist(t0), tbool), _isEmpty), ] + [Primitive(str(j), tint, j) for j in range(2)]
def basePrimitives(): return [Primitive(str(j), tint, j) for j in range(6)] + [ Primitive("*", arrow(tint, tint, tint), _multiplication), Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("is-prime", arrow(tint, tbool), _isPrime), Primitive("is-square", arrow(tint, tbool), _isSquare), # McCarthy Primitive("empty", tlist(t0), []), Primitive("cons", arrow(t0, tlist(t0), tlist(t0)), _cons), Primitive("car", arrow(tlist(t0), t0), _car), Primitive("cdr", arrow(tlist(t0), tlist(t0)), _cdr), Primitive("empty?", arrow(tlist(t0), tbool), _isEmpty), Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("eq?", arrow(tint, tint, tbool), _eq), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("-", arrow(tint, tint, tint), _subtraction) ]
def primitives(): return [Primitive(str(j), tint, j) for j in range(6)] + [ Primitive("empty", tlist(t0), []), Primitive("singleton", arrow(t0, tlist(t0)), _single), Primitive("range", arrow(tint, tlist(tint)), _range), Primitive("++", arrow(tlist(t0), tlist(t0), tlist(t0)), _append), # Primitive("map", arrow(arrow(t0, t1), tlist(t0), tlist(t1)), _map), Primitive( "mapi", arrow( arrow( tint, t0, t1), tlist(t0), tlist(t1)), _mapi), # Primitive("reduce", arrow(arrow(t1, t0, t1), t1, tlist(t0), t1), _reduce), Primitive( "reducei", arrow( arrow( tint, t1, t0, t1), t1, tlist(t0), t1), _reducei), Primitive("true", tbool, True), Primitive("not", arrow(tbool, tbool), _not), Primitive("and", arrow(tbool, tbool, tbool), _and), Primitive("or", arrow(tbool, tbool, tbool), _or), # Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("sort", arrow(tlist(tint), tlist(tint)), sorted), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("*", arrow(tint, tint, tint), _multiplication), Primitive("negate", arrow(tint, tint), _negate), Primitive("mod", arrow(tint, tint, tint), _mod), Primitive("eq?", arrow(tint, tint, tbool), _eq), Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("is-prime", arrow(tint, tbool), _isPrime), Primitive("is-square", arrow(tint, tbool), _isSquare), # these are achievable with above primitives, but unlikely #Primitive("flatten", arrow(tlist(tlist(t0)), tlist(t0)), _flatten), # (lambda (reduce (lambda (lambda (++ $1 $0))) empty $0)) Primitive("sum", arrow(tlist(tint), tint), sum), # (lambda (lambda (reduce (lambda (lambda (+ $0 $1))) 0 $0))) Primitive("reverse", arrow(tlist(t0), tlist(t0)), _reverse), # (lambda (reduce (lambda (lambda (++ (singleton $0) $1))) empty $0)) Primitive("all", arrow(arrow(t0, tbool), tlist(t0), tbool), _all), # (lambda (lambda (reduce (lambda (lambda (and $0 $1))) true (map $1 $0)))) Primitive("any", arrow(arrow(t0, tbool), tlist(t0), tbool), _any), # (lambda (lambda (reduce (lambda (lambda (or $0 $1))) true (map $1 $0)))) Primitive("index", arrow(tint, tlist(t0), t0), _index), # (lambda (lambda (reducei (lambda (lambda (lambda (if (eq? $1 $4) $0 0)))) 0 $0))) Primitive("filter", arrow(arrow(t0, tbool), tlist(t0), tlist(t0)), _filter), # (lambda (lambda (reduce (lambda (lambda (++ $1 (if ($3 $0) (singleton $0) empty)))) empty $0))) #Primitive("replace", arrow(arrow(tint, t0, tbool), tlist(t0), tlist(t0), tlist(t0)), _replace), # (FLATTEN (lambda (lambda (lambda (mapi (lambda (lambda (if ($4 $1 $0) $3 (singleton $1)))) $0))))) Primitive("slice", arrow(tint, tint, tlist(t0), tlist(t0)), _slice), # (lambda (lambda (lambda (reducei (lambda (lambda (lambda (++ $2 (if (and (or (gt? $1 $5) (eq? $1 $5)) (not (or (gt? $4 $1) (eq? $1 $4)))) (singleton $0) empty))))) empty $0)))) ]
return fix(argument) return inner def curry(f): return lambda x: lambda y: f((x, y)) def _fix2(a1): return lambda a2: lambda body: \ _fix((a1, a2))(lambda r: lambda n_l: body(curry(r))(n_l[0])(n_l[1])) primitiveRecursion1 = Primitive("fix1", arrow(t0, arrow(arrow(t0, t1), t0, t1), t1), _fix) primitiveRecursion2 = Primitive("fix2", arrow(t0, t1, arrow(arrow(t0, t1, t2), t0, t1, t2), t2), _fix2) def _match(l): return lambda b: lambda f: b if l == [] else f(l[0])(l[1:]) def primitives():
def concatPrimitives(): return [ Primitive("string_" + i, arrow(tpregex, tpregex), PRC( pregex.String(i))) for i in printable[:-4] if i not in disallowed_list ] + [ Primitive("string_" + name, arrow(tpregex, tpregex), PRC(pregex.String(char))) for char, name in disallowed ] + [ Primitive("r_dot", arrow(tpregex, tpregex), PRC(pregex.dot)), Primitive("r_d", arrow(tpregex, tpregex), PRC(pregex.d)), Primitive("r_s", arrow(tpregex, tpregex), PRC(pregex.s)), Primitive("r_w", arrow(tpregex, tpregex), PRC(pregex.w)), Primitive("r_l", arrow(tpregex, tpregex), PRC(pregex.l)), Primitive("r_u", arrow(tpregex, tpregex), PRC(pregex.u)), #todo Primitive("r_kleene", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.KleeneStar, 1)), Primitive("r_plus", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Plus, 1)), Primitive("r_maybe", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Maybe, 1)), Primitive( "r_alt", arrow(arrow(tpregex, tpregex), arrow(tpregex, tpregex), arrow(tpregex, tpregex)), PRC(pregex.Alt, 2)), ]
def alt2Primitives(): return [Primitive("empty_string", tpregex, pregex.String(""))] + [ Primitive("string_" + i, tpregex, pregex.String(i)) for i in printable[:-4] if i not in disallowed_list ] + [ Primitive("string_" + name, tpregex, pregex.String(char)) for char, name in disallowed ] + [ Primitive("r_dot", tpregex, pregex.dot), Primitive("r_d", tpregex, pregex.d), Primitive("r_s", tpregex, pregex.s), Primitive("r_w", tpregex, pregex.w), Primitive("r_l", tpregex, pregex.l), Primitive("r_u", tpregex, pregex.u), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), #Primitive("r_plus", arrow(tpregex, tpregex), _plus), #Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ]
# rotate = Primitive("rotate", arrow(tartist, tangle, tartist), Curried(_rotate)) # translate = Primitive("translate", arrow(tartist, tdist, tdist, tartist), # Curried(_translate)) # reflect = Primitive("reflect", arrow(tartist, tangle, tartist), Curried(_reflect)) # draw = Primitive("draw", arrow(taxes, tartist, taxes), Curried(_draw)) # Primitive("scale", arrow(tartist, tscale, tartist), Curried(_scale)), # Primitive("rotate", arrow(tartist, tangle, tartist), Curried(_rotate)), # Primitive("translate", arrow(tartist, tdist, tdist, tartist), # Curried(_translate)), p1 = [ # Primitive("blankaxes", taxes, _blankAxes), # Primitive("line", tartist, _line), # Primitive("circle", tartist, _circle), Primitive("line", arrow(tdummy, tartist), Curried(_line)), Primitive("circle", arrow(tdummy, tartist), Curried(_circle)), Primitive("transform", arrow(tartist, tscale, tangle, tdist, tdist, ttrorder, tartist), Curried(_transform)), Primitive("reflect", arrow(tartist, tangle, tartist), Curried(_reflect)), Primitive("draw", arrow(taxes, tartist, taxes), Curried(_draw)), Primitive("dummy", tdummy, 0) ] p2 = [ Primitive("scale{}".format(i), tscale, 2.0**j) for i, j in enumerate(np.linspace(-1.0, 1.0, NBINS)) ] p3 = [ Primitive("dist{}".format(i), tdist, j) for i, j in enumerate(np.linspace(-4, 4, 9))
def human_scale_primitives(): return [Primitive(str(j), tint, j) for j in range(-2, 100)] + [ Primitive("%", arrow(tint, tint, tint), _mod), Primitive("*", arrow(tint, tint, tint), _multiplication), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("-", arrow(tint, tint, tint), _subtraction), Primitive("/", arrow(tint, tint, tint), _division), Primitive("<", arrow(tint, tint, tbool), _lt), Primitive("==", arrow(t0, t0, tbool), _eq), Primitive(">", arrow(tint, tint, tbool), _gt), Primitive("abs", arrow(tint, tint), abs), Primitive("and", arrow(tbool, tbool, tbool), _and), Primitive("append", arrow(tlist(t0), t0, tlist(t0)), _append), Primitive("concat", arrow(tlist(t0), tlist(t0), tlist(t0)), _concat), Primitive("cons", arrow(t0, tlist(t0), tlist(t0)), _cons), Primitive("count", arrow(arrow(t0, tbool), tlist(t0), tint), _count), Primitive("cut_idx", arrow(tint, tlist(t0), tlist(t0)), _cut_idx), Primitive("cut_slice", arrow(tint, tint, tlist(t0), tlist(t0)), _cut_slice), Primitive("cut_val", arrow(t0, tlist(t0), tlist(t0)), _cut_val), Primitive("cut_vals", arrow(t0, tlist(t0), tlist(t0)), _cut_vals), Primitive("drop", arrow(tint, tlist(t0), tlist(t0)), _drop), Primitive("droplast", arrow(tint, tlist(t0), tlist(t0)), _droplast), Primitive("empty", tlist(t0), []), Primitive("false", tbool, False), Primitive("filter", arrow(arrow(t0, tbool), tlist(t0), tlist(t0)), _filter), Primitive("filteri", arrow(arrow(tint, t0, tbool), tlist(t0), tlist(t0)), _filteri), Primitive("find", arrow(arrow(t0, tbool), tlist(t0), tlist(tint)), _find), Primitive("first", arrow(tlist(t0), t0), _first), Primitive("fix1", arrow(t0, arrow(arrow(t0, t1), t0, t1), t1), _fix), Primitive("flatten", arrow(tlist(tlist(t0)), tlist(t0)), _flatten), Primitive("fold", arrow(arrow(t1, t0, t1), t1, tlist(t0), t1), _fold), Primitive("foldi", arrow(arrow(tint, t1, t0, t1), t1, tlist(t0), t1), _foldi), Primitive("group", arrow(arrow(t0, t1), tlist(t1), tlist(tlist(t1))), _group), Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("insert", arrow(t0, tint, tlist(t0), tlist(t0)), _insert), Primitive("is_even", arrow(tint, tbool), _is_even), Primitive("is_in", arrow(tlist(t0), t0, tbool), _is_in), Primitive("is_odd", arrow(tint, tbool), _is_odd), # `lambda` is built into the representation. Primitive("last", arrow(tlist(t0), t0), _last), Primitive("length", arrow(tlist(t0), tint), len), Primitive("map", arrow(arrow(t0, t1), tlist(t0), tlist(t1)), _map), Primitive("mapi", arrow(arrow(tint, t0, t1), tlist(t0), tlist(t1)), _mapi), Primitive("max", arrow(tlist(t0), tint), max), Primitive("min", arrow(tlist(t0), tint), min), Primitive("not", arrow(tbool, tbool), _not), Primitive("nth", arrow(tint, tlist(t0), t0), _nth), Primitive("or", arrow(tbool, tbool, tbool), _or), Primitive("product", arrow(tlist(tint), tint), _product), Primitive("range", arrow(tint, tint, tint, tlist(tint)), _range), Primitive("repeat", arrow(t0, tint, tlist(t0)), _repeat), Primitive("replace", arrow(tint, t0, tlist(t0), tlist(t0)), _replace), Primitive("reverse", arrow(tlist(t0), tlist(t0)), _reverse), Primitive("second", arrow(tlist(t0), t0), _second), Primitive("singleton", arrow(t0, tlist(t0)), _single), Primitive("slice", arrow(tint, tint, tlist(t0), tlist(t0)), _slice), Primitive("sort", arrow(arrow(t0, tint), tlist(t0), tlist(t0)), _sort), Primitive("splice", arrow(tlist(t0), tint, tlist(t0), tlist(t0)), _splice), Primitive("sum", arrow(tlist(tint), tint), sum), Primitive("swap", arrow(tint, tint, tlist(t0), tlist(t0)), _swap), Primitive("take", arrow(tint, tlist(t0), tlist(t0)), _take), Primitive("takelast", arrow(tint, tlist(t0), tlist(t0)), _takelast), Primitive("third", arrow(tlist(t0), t0), _third), Primitive("true", tbool, True), Primitive("unique", arrow(tlist(t0), tlist(t0)), _unique), Primitive("zip", arrow(tlist(t0), tlist(t0), tlist(tlist(t0))), _zip), ]
def _model_comparison_primitives(max_num): return [Primitive(str(j), tint, j) for j in range(0, max_num + 1)] + [ Primitive("nan", tint, math.nan), Primitive("true", tbool, True), Primitive("false", tbool, False), Primitive("empty", tlist(t0), []), Primitive("cons", arrow(t0, tlist(t0), tlist(t0)), _cons), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("-", arrow(tint, tint, tint), _subtraction), Primitive(">", arrow(tint, tint, tbool), _gt), Primitive("fix", arrow(t0, arrow(arrow(t0, t1), t0, t1), t1), _fix), Primitive("head", arrow(tlist(t0), t0), _first), Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("is_empty", arrow(t0, t0, tbool), _eq), Primitive("is_equal", arrow(t0, t0, tbool), _eq), # `lambda` is built into the representation. Primitive("tail", arrow(tlist(t0), tlist(t0)), _tail), ]
from dreamcoder.program import Primitive, Program from dreamcoder.type import arrow, baseType, tint turtle = baseType("turtle") tstate = baseType("tstate") tangle = baseType("tangle") tlength = baseType("tlength") primitives = [ Primitive("logo_UA", tangle, ""), Primitive("logo_UL", tlength, ""), Primitive("logo_ZA", tangle, ""), Primitive("logo_ZL", tlength, ""), Primitive("logo_DIVA", arrow(tangle, tint, tangle), ""), Primitive("logo_MULA", arrow(tangle, tint, tangle), ""), Primitive("logo_DIVL", arrow(tlength, tint, tlength), ""), Primitive("logo_MULL", arrow(tlength, tint, tlength), ""), Primitive("logo_ADDA", arrow(tangle, tangle, tangle), ""), Primitive("logo_SUBA", arrow(tangle, tangle, tangle), ""), # Primitive("logo_ADDL", arrow(tlength,tlength,tlength), ""), # Primitive("logo_SUBL", arrow(tlength,tlength,tlength), ""), # Primitive("logo_PU", arrow(turtle,turtle), ""), # Primitive("logo_PD", arrow(turtle,turtle), ""), Primitive("logo_PT", arrow(arrow(turtle, turtle), arrow(turtle, turtle)), None), Primitive("logo_FWRT", arrow(tlength, tangle, turtle, turtle), ""), Primitive("logo_GETSET", arrow(arrow(turtle, turtle), turtle, turtle), "") ] + [ Primitive("logo_IFTY", tint, ""), Primitive("logo_epsA", tangle, ""),
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), #is this okay??? Primitive("FILTER", arrow(int_to_bool, tlist(tint), tlist(tint)), _filter), #is this okay??? Primitive("COUNT", arrow(int_to_bool, tlist(tint), tint), _count), #is this okay??? Primitive( "ZIPWITH", arrow(int_to_int_to_int, tlist(tint), tlist(tint), tlist(tint)), _zipwith), #is this okay??? Primitive("SCANL1", arrow(int_to_int_to_int, tlist(tint), tlist(tint)), _scanl1), #is this okay??? ] + [ 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) ]
return fix(argument) return inner def curry(f): return lambda x: lambda y: f((x, y)) def _fix2(a1): return lambda a2: lambda body: \ _fix((a1, a2))(lambda r: lambda n_l: body(curry(r))(n_l[0])(n_l[1])) primitiveRecursion1 = Primitive("fix1", arrow(t0, arrow(arrow(t0, t1), t0, t1), t1), _fix) primitiveRecursion2 = Primitive( "fix2", arrow(t0, t1, arrow(arrow(t0, t1, t2), t0, t1, t2), t2), _fix2) def McCarthyPrimitives(): "These are < primitives provided by 1959 lisp as introduced by McCarthy" return [ Primitive("if", arrow(tbool, t0, t0, t0), _if), primitiveRecursion1, primitiveRecursion2, # Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("positive?", arrow(tint, tbool), _positive), Primitive("incr", arrow(tint, tint), _succ),
def primitive(self, e): if e.name == 'REAL': return Primitive(str(e), e.tp, randomCoefficient()) return e
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), #is this okay??? Primitive("filter_int", arrow(int_to_bool, tlist(tint), tlist(tint)), _filter), #is this okay??? Primitive("count", arrow(int_to_bool, tlist(tint), tint), _count), #is this okay??? Primitive( "zipwith", arrow(int_to_int_to_int, tlist(tint), tlist(tint), tlist(tint)), _zipwith), #is this okay??? Primitive("scanl1", arrow(int_to_int_to_int, tlist(tint), tlist(tint)), _scanl1), #is this okay??? # ] + [ # Primitive("succ", arrow(tint, tint), _succ), # Primitive("pred", arrow(tint, tint), _pred), # Primitive("double", arrow(tint, tint), _double), # Primitive("half", arrow(tint, tint), _half), # Primitive("neg", arrow(tint, tint), _neg), # Primitive("square", arrow(tint, tint), _square), # Primitive("triple", arrow(tint, tint), _triple), # Primitive("third", arrow(tint, tint), _third), # Primitive("quad", arrow(tint, tint), _quad), # Primitive("quarter", arrow(tint, tint), _quarter), # ] + [ # Primitive("pos", arrow(tint, tbool), _pos), # Primitive("neg", arrow(tint, tbool), _neg), # Primitive("even", arrow(tint, tbool), _even), # Primitive("odd", arrow(tint, tbool), _odd), # ] + [ # Primitive("add", arrow(tint, tint, tint), _add), # Primitive("sub", arrow(tint, tint, tint), _sub), # Primitive("mult", arrow(tint, tint, tint), _mult), # Primitive("min", arrow(tint, tint, tint), _min), # Primitive("max", arrow(tint, tint, tint), _max) ] + [ 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 McCarthyPrimitives(): "These are < primitives provided by 1959 lisp as introduced by McCarthy" return [ Primitive("empty", tlist(t0), []), Primitive("cons", arrow(t0, tlist(t0), tlist(t0)), _cons), Primitive("car", arrow(tlist(t0), t0), _car), Primitive("cdr", arrow(tlist(t0), tlist(t0)), _cdr), Primitive("empty?", arrow(tlist(t0), tbool), _isEmpty), #Primitive("unfold", arrow(t0, arrow(t0,t1), arrow(t0,t0), arrow(t0,tbool), tlist(t1)), _isEmpty), #Primitive("1+", arrow(tint,tint),None), # Primitive("range", arrow(tint, tlist(tint)), range), # Primitive("map", arrow(arrow(t0, t1), tlist(t0), tlist(t1)), _map), # Primitive("index", arrow(tint,tlist(t0),t0),None), # Primitive("length", arrow(tlist(t0),tint),None), primitiveRecursion1, #primitiveRecursion2, Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("eq?", arrow(tint, tint, tbool), _eq), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("-", arrow(tint, tint, tint), _subtraction), ] + [Primitive(str(j), tint, j) for j in range(2)]
Primitive("is-prime", arrow(tint, tbool), _isPrime), Primitive("is-square", arrow(tint, tbool), _isSquare), # McCarthy Primitive("empty", tlist(t0), []), Primitive("cons", arrow(t0, tlist(t0), tlist(t0)), _cons), Primitive("car", arrow(tlist(t0), t0), _car), Primitive("cdr", arrow(tlist(t0), tlist(t0)), _cdr), Primitive("empty?", arrow(tlist(t0), tbool), _isEmpty), Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("eq?", arrow(tint, tint, tbool), _eq), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("-", arrow(tint, tint, tint), _subtraction) ] zip_primitive = Primitive( "zip", arrow(tlist(t0), tlist(t1), arrow(t0, t1, t2), tlist(t2)), _zip) def bootstrapTarget(): """These are the primitives that we hope to learn from the bootstrapping procedure""" return [ # learned primitives Primitive("map", arrow(arrow(t0, t1), tlist(t0), tlist(t1)), _map), Primitive( "unfold", arrow(t0, arrow(t0, tbool), arrow(t0, t1), arrow(t0, t0), tlist(t1)), _unfold), Primitive("range", arrow(tint, tlist(tint)), _range), Primitive("index", arrow(tint, tlist(t0), t0), _index), Primitive("fold", arrow(tlist(t0), t1, arrow(t0, t1, t1), t1), _fold), Primitive("length", arrow(tlist(t0), tint), len),