コード例 #1
0
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;
}
コード例 #2
0
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}};
}
コード例 #3
0
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}};
}
コード例 #4
0
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}};
}
コード例 #5
0
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};
}
コード例 #6
0
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);
}
コード例 #7
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);
}
コード例 #8
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);
}
コード例 #9
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);
}
コード例 #10
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);
}
コード例 #11
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}};
}
コード例 #12
0
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);
}
コード例 #13
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();
}
コード例 #14
0
extern "C" closure builtin_function_floor(OperationArgs& Args)
{
  double x = Args.evaluate(0).as_double();
  assert(x > 0.0);

  return {floor(x)};
}
コード例 #15
0
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";
}
コード例 #16
0
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;
}
コード例 #17
0
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!";
}
コード例 #18
0
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!";
}
コード例 #19
0
extern "C" closure builtin_function_putStrLn(OperationArgs& Args)
{
  std::string message = Args.evaluate(0).as_<String>();

  std::cout<<message<<std::endl;

  return constructor("()",0);
}
コード例 #20
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}};
}
コード例 #21
0
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}};
}
コード例 #22
0
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()));
}
コード例 #23
0
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}};
}
コード例 #24
0
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'";
}
コード例 #25
0
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'";
}
コード例 #26
0
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}};
}
コード例 #27
0
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'";
}
コード例 #28
0
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";
}
コード例 #29
0
extern "C" closure builtin_function_ceiling(OperationArgs& Args)
{
  double x = Args.evaluate(0).as_double();

  return {ceil(x)};
}
コード例 #30
0
extern "C" closure builtin_function_truncate(OperationArgs& Args)
{
  double x = Args.evaluate(0).as_double();

  return {trunc(x)};
}