def __venture_start__(ripl): start = time.time() # NOTE: these are all currently inference SPs ripl.bind_foreign_inference_sp( "make_symbol", deterministic_typed(make_name, [t.SymbolType(), t.NumberType()], t.SymbolType())) ripl.bind_foreign_inference_sp( "logsumexp", deterministic_typed(logsumexp, [t.ArrayUnboxedType(t.NumberType())], t.NumberType())) ripl.bind_foreign_inference_sp( "concatenate", deterministic_typed(concatenate, [ t.ArrayUnboxedType(t.NumberType()), t.ArrayUnboxedType(t.NumberType()) ], t.ArrayUnboxedType(t.NumberType()))) ripl.bind_foreign_inference_sp( "sum", deterministic_typed(sum_sp, [t.ArrayUnboxedType(t.NumberType())], t.NumberType())) ripl.bind_foreign_inference_sp( "mean", deterministic_typed(mean_sp, [t.ArrayUnboxedType(t.NumberType())], t.NumberType())) ripl.bind_foreign_inference_sp( "stderr", deterministic_typed(stderr, [t.ArrayUnboxedType(t.NumberType())], t.NumberType())) ripl.bind_foreign_inference_sp( "random_string", deterministic_typed(random_string, [t.IntegerType()], t.StringType())) ripl.bind_foreign_inference_sp( "cat_string", deterministic_typed(cat_string, [t.StringType(), t.StringType()], t.StringType())) ripl.bind_foreign_inference_sp( "start_timer", deterministic_typed(start_timer, [], t.NumberType())) ripl.bind_foreign_inference_sp( "time_elapsed", deterministic_typed(time_elapsed, [t.NumberType()], t.NumberType())) ripl.execute_program("define new_trace = proc() { run(new_model()) };") ripl.execute_program( "define run_in_trace = proc(trace, program) { first(run(in_model(trace, program))) };" ) ripl.execute_program( "define parallel_mapv = proc(f, l) { run(parallel_mapv_action(f, l, 4)) };" )
def follow_func(edge, trace): if edge in t.NumberType(): def doit(node): if n.isApplicationNode(node): # The subexpressions are the definite parents, accidentally in # the order useful for this return OrderedFrozenSet( [trace.definiteParentsAt(node)[int(edge.getNumber())]]) else: return OrderedFrozenSet([]) return doit elif edge in t.SymbolType() or edge in t.StringType(): name = edge.getSymbol() if name == "operator": return follow_func(t.NumberType().asVentureValue(0), trace) elif name == "source": def doit(node): if n.isLookupNode(node): # The definite parents are the lookup source return OrderedFrozenSet([trace.definiteParentsAt(node)[0]]) else: return OrderedFrozenSet([]) return doit elif name == "request": def doit(node): if n.isOutputNode(node): # The last definite parent is the request node return OrderedFrozenSet( [trace.definiteParentsAt(node)[-1]]) else: return OrderedFrozenSet([]) return doit else: raise Exception("Unknown named edge type %s" % (name, )) elif edge in t.ForeignBlobType() and isinstance(edge.datum, EsrEdge): def doit(node): if n.isApplicationNode(node): return OrderedFrozenSet( [trace.esrParentsAt(node)[int(edge.datum.index)]]) else: return OrderedFrozenSet([]) return doit else: raise Exception("Unknown edge type %s" % (edge, ))
def __venture_start__(ripl): ripl.execute_program(''' assume gp_cov_wn = (c) -> { gp_cov_scale(c, gp_cov_bump(1e-9, 1e-11)) }; define gp_cov_wn = (c) -> { gp_cov_scale(c, gp_cov_bump(1e-9, 1e-11)) }; ''') ripl.bind_foreign_inference_sp( 'sort', deterministic_typed(np.sort, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1)) ripl.bind_foreign_inference_sp( 'get_mean', deterministic_typed(np.mean, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.NumberType(), min_req_args=1)) ripl.bind_foreign_inference_sp( 'load_csv', deterministic_typed(load_csv, [vt.StringType()], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1)) ripl.bind_foreign_sp( 'compile_ast_to_venturescript', deterministic_typed(compile_ast_to_embedded_dsl, [vt.AnyType()], vt.StringType(), min_req_args=1)) ripl.bind_foreign_sp( 'eval_expr', deterministic_typed(interpret_embedded_dsl, [vt.StringType()], gp.gpType, min_req_args=1))
"(lists and arrays work too)")) registerBuiltinSP("take", deterministic_typed(lambda ind, xs: xs.take(ind), [t.IntegerType(), t.HomogeneousSequenceType(t.AnyType("k"))], t.HomogeneousSequenceType(t.AnyType("k")), descr="take returns the requested number of elements from the " \ "beginning of the given sequence, as another sequence of " \ "the same type.")) def debug_print(label, value): print 'debug ' + label + ': ' + str(value) return value registerBuiltinSP("debug", deterministic_typed(debug_print, [t.StringType(), t.AnyType("k")], t.AnyType("k"), descr = "Print the given value, labeled by a string. Return the value. " \ "Intended for debugging or for monitoring execution.")) registerBuiltinSP( "value_error", deterministic_typed(lambda s: raise_(VentureValueError(str(s))), [t.AnyType()], t.AnyType())) def make_name(sym, index): return sym + "_" + str(int(index)) registerBuiltinSP("name", deterministic_typed(make_name, [t.SymbolType(), t.NumberType()], t.SymbolType(), descr = "Programmatically synthesize a variable name. " \
def __venture_start__(ripl): ripl.execute_program(''' define set_value_at_scope_block = (scope, block, value) -> { set_value_at2(scope, block, value) }; ''') ripl.bind_foreign_inference_sp( 'sort', deterministic_typed( np.sort, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'get_mean', deterministic_typed( np.mean, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.NumberType(), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'get_predictive_mean', deterministic_typed( lambda x: np.mean(x, axis=0), [ vt.ArrayUnboxedType(vt.ArrayUnboxedType(vt.NumberType())), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'load_csv', deterministic_typed( load_csv, [vt.StringType()], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'concatenate', deterministic_typed( concatenate, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=2 ) ) ripl.bind_foreign_inference_sp( 'scatter_plot', deterministic_typed( scatter_plot, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), vt.HomogeneousDictType(vt.StringType(), vt.AnyType()) ], vt.NilType(), min_req_args=2 ) ) ripl.bind_foreign_inference_sp( 'line_plot', deterministic_typed( line_plot, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), vt.HomogeneousDictType(vt.StringType(), vt.AnyType()) ], vt.NilType(), min_req_args=2 ) ) ripl.bind_foreign_inference_sp( 'legend', deterministic_typed( legend, [vt.StringType()], vt.NilType(), min_req_args=0 ) ) ripl.bind_foreign_inference_sp( 'square_heatmap', deterministic_typed( square_heatmap, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), vt.HomogeneousDictType(vt.StringType(), vt.AnyType()) ], vt.NilType(), min_req_args=2 ) ) ripl.bind_foreign_sp( 'gp_cov_cp', _cov_sp( change_point, [ vt.NumberType(), vt.NumberType(), GPCovarianceType('K'), GPCovarianceType('H') ] ) )
parser = VentureScriptParser() ast = parser.parse_instruction(expr)['expression'] assert len(ast) == 3 assert ast[0]['value'] == 'make_gp' gp_mean = interpret_mean_kernel(ast[1]) gp_cov = interpret_covariance_kernel(ast[2]) return sp.VentureSPRecord(gp.GPSP(gp_mean, gp_cov)) if __name__ == '__main__': ripl = vs.make_lite_ripl() ripl.bind_foreign_sp( 'interpret_embedded_dsl', deterministic_typed( interpret_embedded_dsl, [vt.StringType()], gp.gpType, min_req_args=1 ) ) ripl.evaluate(""" make_gp(gp_mean_const(0.), gp_cov_scale(0.1, gp_cov_bump(.1,.1))) """) ripl.execute_program(""" assume gp = interpret_embedded_dsl( "make_gp( gp_mean_const(0.), gp_cov_sum( gp_cov_scale(0.1, gp_cov_bump(.1,.1)), gp_cov_se(0.1)))" )
registerBuiltinSP("extend_environment", typed_nr(ExtendEnvOutputPSP(), [env.EnvironmentType(), t.SymbolType(), t.AnyType()], env.EnvironmentType())) class EvalRequestPSP(DeterministicPSP): def simulate(self,args): (exp, en) = args.operandValues() # Point to the desugared source code location of expression. # This is not a full address, because the call stack is gone. source_loc = addr.append(addr.top_frame(args.operandNodes[0].address), 1) return Request([ESR(args.node,exp,source_loc,en)]) def description(self,name): return "%s evaluates the given expression in the given environment and returns the result. Is itself deterministic, but the given expression may involve a stochasitc computation." % name registerBuiltinSP("eval", esr_output(TypedPSP(EvalRequestPSP(), SPType([t.ExpressionType(), env.EnvironmentType()], t.RequestType("<object>"))))) class AddressOfOutputPSP(DeterministicPSP): def simulate(self,args): place = args.operandNodes[0] node = args.trace.getOutermostNonReferenceNode(place) return jsonable_address(node) def description(self,name): return "%s returns a string representing the address of the top nontrivial node of its argument" % name registerBuiltinSP("address_of", typed_nr(AddressOfOutputPSP(), [t.AnyType()], t.StringType()))