extern "C" closure builtin_function_vector_from_list(OperationArgs& Args) { object_ptr<EVector> v (new EVector); const closure* top = &Args.evaluate_slot_to_closure(0); while(top->exp.size()) { assert(has_constructor(top->exp,":")); assert(top->exp.size() == 2); int element_index = top->exp.sub()[0].as_index_var(); int element_reg = top->lookup_in_env( element_index ); int next_index = top->exp.sub()[1].as_index_var(); int next_reg = top->lookup_in_env( next_index ); // Add the element to the list. v->push_back( Args.evaluate_reg_to_object(element_reg) ); // Move to the next element or end top = &Args.evaluate_reg_to_closure(next_reg); } assert(has_constructor(top->exp,"[]")); return v; }
extern "C" closure builtin_function_join(OperationArgs& Args) { Args.evaluate_slot_to_reg(0); int R = Args.evaluate_slot_to_reg(1); return {index_var(0),{R}}; }
extern "C" closure builtin_function_get_modifiable_for_index(OperationArgs& Args) { assert(not Args.evaluate_changeables()); int R1 = Args.evaluate(0).as_int(); return {index_var(0),{R1}}; }
extern "C" closure builtin_function_seq(OperationArgs& Args) { Args.evaluate_slot_no_record(0); int index = Args.reference(1).as_index_var(); int R = Args.current_closure().lookup_in_env( index); return {index_var(0),{R}}; }
extern "C" closure builtin_function_get_modifiable_index(OperationArgs& Args) { assert(not Args.evaluate_changeables()); int R1 = Args.evaluate_slot_to_reg(0); const reg_heap& M = Args.memory(); assert(is_modifiable(M.access(R1).C.exp)); return {R1}; }
extern "C" closure builtin_function_is_modifiable(OperationArgs& Args) { assert(not Args.evaluate_changeables()); int R1 = Args.evaluate_slot_to_reg(0); const reg_heap& M = Args.memory(); if (M.access(R1).C.exp.head().type() == modifiable_type) return constructor("Prelude.True",0); else return constructor("Prelude.False",0); }
extern "C" closure builtin_function_is_changeable(OperationArgs& Args) { assert(not Args.evaluate_changeables()); int R1 = Args.evaluate_slot_to_reg(0); const reg_heap& M = Args.memory(); if (M.reg_is_changeable(R1)) return constructor("Prelude.True",0); else return constructor("Prelude.False",0); }
extern "C" closure builtin_function_trigger(OperationArgs& Args) { int i = Args.evaluate(0).as_int(); reg_heap& M = Args.memory(); // We should be executing in the root token M.triggers().push_back(i); return constructor("()",0); }
extern "C" closure builtin_function_register_probability(OperationArgs& Args) { assert(not Args.evaluate_changeables()); int R = Args.reg_for_slot(0); auto& M = Args.memory(); M.register_probability(R); return constructor("()",0); }
extern "C" closure builtin_function_set_modifiable_value(OperationArgs& Args) { assert(not Args.evaluate_changeables()); int c = Args.evaluate(0).as_int(); int R1 = Args.evaluate_slot_to_reg(1); int R2 = Args.evaluate_slot_to_reg(2); Args.memory().set_reg_value_in_context(R1, {index_var(0),{R2}}, c); return constructor("()",0); }
extern "C" closure builtin_function_get_modifiable_value(OperationArgs& Args) { assert(not Args.evaluate_changeables()); int c = Args.evaluate(0).as_int(); int R1 = Args.evaluate_slot_to_reg(1); int R2 = Args.memory().get_modifiable_value_in_context(R1, c); assert( R2 ); return {index_var(0),{R2}}; }
extern "C" closure builtin_function_add_parameter(OperationArgs& Args) { assert(not Args.evaluate_changeables()); const std::string name = Args.evaluate(0).as_<String>(); int R = Args.evaluate_slot_to_reg(1); auto& M = Args.memory(); M.parameters.push_back({name,R}); return constructor("()",0); }
shared_ptr<const Object> Model::operator()(OperationArgs& Args) const { shared_ptr<Model> M (clone()); for(int i=0;i<n_parameters();i++) M->set_parameter_value(i,Args.evaluate(i)); return M->result(); }
extern "C" closure builtin_function_floor(OperationArgs& Args) { double x = Args.evaluate(0).as_double(); assert(x > 0.0); return {floor(x)}; }
extern "C" closure builtin_function_pow(OperationArgs& Args) { auto x = Args.evaluate(0); auto y = Args.evaluate(1); double yy = 0; if (y.is_double()) yy = y.as_double(); else if (y.is_int()) yy = y.as_int(); else throw myexception()<<"pow: exponent '"<<x.print()<<"' is not double or int"; if (x.is_double()) { double xx = x.as_double(); assert(xx > 0.0); return {pow(xx,yy)}; } else if (x.is_int()) { double xx = x.as_int(); assert(xx > 0.0); return {pow(xx,yy)}; } else if (x.is_log_double()) { log_double_t xx = x.as_log_double(); return {pow(xx,yy)}; } throw myexception()<<"pow: object '"<<x.print()<<"' is not double, int, or log_double"; }
extern "C" closure builtin_function_show(OperationArgs& Args) { auto x = Args.evaluate(0); object_ptr<String> v (new String); if (x.is_double()) *v = convertToString<double>(x.as_double()); else if (x.is_int()) *v = convertToString<int>(x.as_int()); else if (x.is_log_double()) *v = "LD"+convertToString<double>(x.as_log_double().log()); else if (x.is_char()) { std::string s; s = x.as_char(); *v = s; } else if (x.is_a<String>()) *v = x.as_<String>(); else throw myexception()<<"show: object '"<<x.print()<<"' is not double, int, log_double, char, or string'"; return v; }
extern "C" closure builtin_function_read_double(OperationArgs& Args) { string s = Args.evaluate(0).as_<String>(); double d; if (can_be_converted_to(s,d)) return {d}; throw myexception()<<"Cannot convert string '"<<s<<"' to double!"; }
extern "C" closure builtin_function_read_int(OperationArgs& Args) { string s = Args.evaluate(0).as_<String>(); int i; if (can_be_converted_to(s,i)) return {i}; throw myexception()<<"Cannot convert string '"<<s<<"' to int!"; }
extern "C" closure builtin_function_putStrLn(OperationArgs& Args) { std::string message = Args.evaluate(0).as_<String>(); std::cout<<message<<std::endl; return constructor("()",0); }
extern "C" closure builtin_function_new_modifiable(OperationArgs& Args) { assert(not Args.evaluate_changeables()); reg_heap& M = Args.memory(); int D = Args.reg_for_slot(0); // Allocate a reg, and fill it with a modifiable of the correct index expression_ref E(new expression(modifiable(),{index_var(0)})); closure C{E,{D}}; int R1 = Args.allocate(std::move(C)); M.make_reg_changeable(R1); // Return a reference to the new modifiable. return {index_var(0),{R1}}; }
extern "C" closure builtin_function_reapply(OperationArgs& Args) { int index1 = Args.reference(0).as_index_var(); int R1 = Args.current_closure().lookup_in_env( index1 ); int index2 = Args.reference(1).as_index_var(); int R2 = Args.current_closure().lookup_in_env( index2 ); expression_ref apply_E; { expression_ref fE = index_var(1); expression_ref argE = index_var(0); apply_E = (fE, argE); } // %1 %0 {R1,R2} int apply_reg = Args.allocate({apply_E,{R1, R2}}); // FIXME - aren't we trying to eliminate general evaluation of regs that aren't children? See below: // Evaluate the newly create application reg - and depend upon it! if (Args.evaluate_changeables()) Args.evaluate_reg_to_object(apply_reg); return {index_var(0),{apply_reg}}; }
boost::shared_ptr<const Object> model_prior::operator()(OperationArgs& Args) const { expression_ref R = Args.evaluate(0); vector<expression_ref> v = get_ref_vector_from_tuple(R); shared_ptr<Model> M2 (M->clone()); for(int i=0;i<M2->n_parameters();i++) M2->set_parameter_value(i,v[i]); return shared_ptr<const Object>(new Log_Double(M2->prior())); }
extern "C" closure builtin_function_new_random_modifiable(OperationArgs& Args) { // assert(not Args.evaluate_changeables()); reg_heap& M = Args.memory(); int R1 = Args.reg_for_slot(0); int R2 = Args.reg_for_slot(1); int V = Args.reg_for_slot(2); int rate = Args.reg_for_slot(3); // Allocate a reg, and fill it with a modifiable of the correct index expression_ref E(new expression(modifiable(),{index_var(2),index_var(1),index_var(0)})); closure C{E,{R2,R1,rate}}; int r = Args.allocate(std::move(C)); M.make_reg_changeable(r); M.set_shared_value(r,V); M.add_random_modifiable(r); // Return a reference to the new modifiable. return {index_var(0),{r}}; }
extern "C" closure builtin_function_negate(OperationArgs& Args) { auto x = Args.evaluate(0); if (x.is_double()) return {-x.as_double()}; else if (x.is_int()) return {-x.as_int()}; else if (x.is_char()) return {-x.as_char()}; else throw myexception()<<"Negate: object '"<<x.print()<<"' is not double, int, or char'"; }
extern "C" closure builtin_function_mod(OperationArgs& Args) { using boost::dynamic_pointer_cast; auto x = Args.evaluate(0); auto y = Args.evaluate(1); if (x.is_int()) return { x.as_int() % y.as_int() }; else if (x.is_char()) return { x.as_char() % y.as_char() }; else throw myexception()<<"Mod: object '"<<x.print()<<"' is not int, or char'"; }
extern "C" closure builtin_function_evaluate(OperationArgs& Args) { auto& M = Args.memory(); int c = Args.evaluate(0).as_int(); #ifndef NDEBUG if (Args.evaluate_changeables() and c >= 0) throw myexception()<<"Calling builtin_function_evaluate( ) when evaluate_changeables=true and c >= 0"; #endif int R1 = Args.reg_for_slot(1); int R2 = 0; if (c < 0) R2 = M.incremental_evaluate_unchangeable(R1); else R2 = M.incremental_evaluate_in_context(R1, c).first; assert( R2 ); return {index_var(0),{R2}}; }
extern "C" closure builtin_function_lessthanorequal(OperationArgs& Args) { auto x = Args.evaluate(0); auto y = Args.evaluate(1); if (x.is_double()) return {x.as_double() <= y.as_double()}; else if (x.is_int()) return {x.as_int() <= y.as_int()}; else if (x.is_log_double()) return {x.as_log_double() <= y.as_log_double()}; else if (x.is_char()) return {x.as_char() <= y.as_char()}; else throw myexception()<<"<=: object '"<<x.print()<<"' is not double, int, log_double, or char'"; }
extern "C" closure builtin_function_log(OperationArgs& Args) { auto x = Args.evaluate(0); if (x.is_double()) { double xx = x.as_double(); assert(xx > 0.0); return {log(xx)}; } else if (x.is_int()) { double xx = x.as_int(); assert(xx > 0.0); return {log(xx)}; } else if (x.is_log_double()) { log_double_t xx = x.as_log_double(); return {log(xx)}; } throw myexception()<<"log: object '"<<x.print()<<"' is not double, int, or log_double"; }
extern "C" closure builtin_function_ceiling(OperationArgs& Args) { double x = Args.evaluate(0).as_double(); return {ceil(x)}; }
extern "C" closure builtin_function_truncate(OperationArgs& Args) { double x = Args.evaluate(0).as_double(); return {trunc(x)}; }