Fleet  0.0.9
Inference in the LOT
Functions
Builtins Namespace Reference

Functions

template<typename Grammar_t >
Primitive< typename Grammar_t::input_t > X (Op::X, BUILTIN_LAMBDA { assert(!vms->xstack.empty());vms->template push< typename Grammar_t::input_t >(vms->xstack.top());})
 
template<typename Grammar_t >
Primitive< bool, bool, bool > And (Op::And, BUILTIN_LAMBDA { bool b=vms->template getpop< bool >();if(!b) { vms->program.popn(arg);vms->template push< bool >(false);} else { } })
 
template<typename Grammar_t >
Primitive< bool, bool, bool > Or (Op::Or, BUILTIN_LAMBDA { bool b=vms->template getpop< bool >();if(b) { vms->program.popn(arg);vms->template push< bool >(true);} else { } })
 
template<typename Grammar_t >
Primitive< bool, bool > Not (Op::Not, BUILTIN_LAMBDA { vms->push(not vms->template getpop< bool >());})
 
template<typename Grammar_t >
Primitive< bool, bool, bool > Implies (Op::Implies, BUILTIN_LAMBDA { bool x=vms->template getpop< bool >();bool y=vms->template getpop< bool >();vms->template push< bool >((not x) or y);})
 
template<typename Grammar_t >
Primitive< bool, bool, bool > Iff (Op::Iff, BUILTIN_LAMBDA { bool x=vms->template getpop< bool >();bool y=vms->template getpop< bool >();vms->template push< bool >(x==y);})
 
template<typename Grammar_t , typename T >
Primitive< T, bool, T, T > If (Op::If, BUILTIN_LAMBDA { bool b=vms->template getpop< bool >();if(!b) vms->program.popn(arg);else {};})
 
template<typename Grammar_t >
Primitive Jmp (Op::Jmp, BUILTIN_LAMBDA { vms->program.popn(arg);})
 
template<typename Grammar_t >
Primitive PopX (Op::PopX, BUILTIN_LAMBDA { vms->xstack.pop();})
 
template<typename Grammar_t >
Primitive< bool > Flip (Op::Flip, BUILTIN_LAMBDA { assert(vms->pool !=nullptr);vms->pool->copy_increment_push(vms, true, -LOG2);bool b=vms->pool->increment_push(vms, false, -LOG2);if(b) { vms->status=vmstatus_t::RANDOM_CHOICE_NO_DELETE;} else { vms->status=vmstatus_t::RANDOM_CHOICE;} })
 
template<typename Grammar_t >
Primitive< bool, double > FlipP (Op::FlipP, BUILTIN_LAMBDA { assert(vms->pool !=nullptr);double p=vms->template getpop< double >();if(std::isnan(p)) { p=0.0;} if(p > 1.0 or p< 0.0) { print("*** Error, received p not in [0,1]:", p);assert(false);} vms->pool->copy_increment_push(vms, true, log(p));bool b=vms->pool->increment_push(vms, false, log(1.0-p));if(b) { vms->status=vmstatus_t::RANDOM_CHOICE_NO_DELETE;} else { vms->status=vmstatus_t::RANDOM_CHOICE;} })
 
template<typename Grammar_t >
Primitive< bool, double > SafeFlipP (Op::SafeFlipP, BUILTIN_LAMBDA { assert(vms->pool !=nullptr);double p=vms->template getpop< double >();if(std::isnan(p)) p=0.0;else if(p > 1.0) p=1.0;else if(p< 0.0) p=0.0;vms->pool->copy_increment_push(vms, true, log(p));bool b=vms->pool->increment_push(vms, false, log(1.0-p));if(b) { vms->status=vmstatus_t::RANDOM_CHOICE_NO_DELETE;} else { vms->status=vmstatus_t::RANDOM_CHOICE;} })
 
template<typename Grammar_t , typename t , typename T = std::set<t>>
Primitive< t, T > Sample (Op::Sample, BUILTIN_LAMBDA { auto s=vms->template getpop< T >();if(s.size()==1) { auto v=std::move(*s.begin());vms->template push< t >(std::move(v));} else { const double lp=-log(s.size());for(const auto &x :s) { bool b=vms->pool->copy_increment_push(vms, x, lp);if(not b) break;} vms->status=vmstatus_t::RANDOM_CHOICE;} })
 
template<typename Grammar_t >
Primitive< int, int > Sample_int (Op::Sample, BUILTIN_LAMBDA { const int mx=vms->template getpop< int >();const double lp=-log(mx);for(int i=0;i< mx;i++) { bool b=vms->pool->copy_increment_push(vms, i, lp);if(not b) break;} vms->status=vmstatus_t::RANDOM_CHOICE;})
 
template<typename Grammar_t , typename key_t , typename output_t = typename Grammar_t::output_t>
Primitive Mem (Op::Mem, BUILTIN_LAMBDA { auto memindex=vms->template memstack< key_t >().top();vms->template memstack< key_t >().pop();if(vms->template mem< key_t >().count(memindex)==0) { vms->template mem< key_t >()[memindex]=vms->template gettop< output_t >();} })
 
template<typename Grammar_t >
Primitive NoOp (Op::NoOp, BUILTIN_LAMBDA { })
 
template<typename Grammar_t >
Primitive UnusedNoOp (Op::NoOp, BUILTIN_LAMBDA { assert(false);})
 
template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, input_t > Recurse (Op::Recurse, BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto mynewx=vms->template getpop< input_t >();vms->xstack.push(std::move(mynewx));vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->program.loader->push_program(vms->program);})
 
template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, input_t > SafeRecurse (Op::SafeRecurse, BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { Recurse< Grammar_t >.call(vms, arg);} })
 
template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, input_t > MemRecurse (Op::MemRecurse, BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);using mykey_t=short;if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto x=vms->template getpop< input_t >();auto memindex=std::make_pair(arg, x);if(vms->template mem< mykey_t >().count(memindex)){ vms->push(vms->template mem< mykey_t >()[memindex]);} else { vms->xstack.push(x);vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->template memstack< mykey_t >().push(memindex);vms->program.push(Builtins::Mem< Grammar_t, mykey_t, output_t >.makeInstruction());vms->program.loader->push_program(vms->program);} })
 
template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, input_t > SafeMemRecurse (Op::SafeMemRecurse, BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { MemRecurse< Grammar_t >.call(vms, arg);} })
 
template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, key_t, input_t > LexiconRecurse (Op::LexiconRecurse, BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto key=vms->template getpop< key_t >();auto mynewx=vms->template getpop< input_t >();vms->xstack.push(std::move(mynewx));vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->program.loader->push_program(vms->program, key);})
 
template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, key_t, input_t > LexiconSafeRecurse (Op::LexiconSafeRecurse, BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());assert(vms->program.loader !=nullptr);if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< key_t >();vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { LexiconRecurse< Grammar_t, key_t >.call(vms, arg);} })
 
template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, key_t, input_t > LexiconMemRecurse (Op::LexiconMemRecurse, BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto key=vms->template getpop< key_t >();auto x=vms->template getpop< input_t >();auto memindex=std::make_pair(key, x);if(vms->template mem< key_t >().count(memindex)){ vms->push(vms->template mem< key_t >()[memindex]);} else { vms->xstack.push(x);vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->template memstack< key_t >().push(memindex);vms->program.push(Builtins::Mem< Grammar_t, key_t, output_t >.makeInstruction());vms->program.loader->push_program(vms->program, key);} })
 
template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive< output_t, key_t, input_t > LexiconSafeMemRecurse (Op::LexiconSafeMemRecurse, BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());assert(vms->program.loader !=nullptr);if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< key_t >();vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { LexiconMemRecurse< Grammar_t, key_t >.call(vms, arg);} })
 

Function Documentation

◆ And()

template<typename Grammar_t >
Primitive<bool,bool,bool> Builtins::And ( Op::And  ,
BUILTIN_LAMBDA { bool b=vms->template getpop< bool >();if(!b) { vms->program.popn(arg);vms->template push< bool >(false);} else { } }   
)

◆ Flip()

template<typename Grammar_t >
Primitive<bool> Builtins::Flip ( Op::Flip  ,
BUILTIN_LAMBDA { assert(vms->pool !=nullptr);vms->pool->copy_increment_push(vms, true, -LOG2);bool b=vms->pool->increment_push(vms, false, -LOG2);if(b) { vms->status=vmstatus_t::RANDOM_CHOICE_NO_DELETE;} else { vms->status=vmstatus_t::RANDOM_CHOICE;} }   
)

◆ FlipP()

template<typename Grammar_t >
Primitive<bool,double> Builtins::FlipP ( Op::FlipP  ,
BUILTIN_LAMBDA { assert(vms->pool !=nullptr);double p=vms->template getpop< double >();if(std::isnan(p)) { p=0.0;} if(p > 1.0 or p< 0.0) { print("*** Error, received p not in [0,1]:", p);assert(false);} vms->pool->copy_increment_push(vms, true, log(p));bool b=vms->pool->increment_push(vms, false, log(1.0-p));if(b) { vms->status=vmstatus_t::RANDOM_CHOICE_NO_DELETE;} else { vms->status=vmstatus_t::RANDOM_CHOICE;} }   
)

◆ If()

template<typename Grammar_t , typename T >
Primitive<T,bool,T,T> Builtins::If ( Op::If  ,
BUILTIN_LAMBDA { bool b=vms->template getpop< bool >();if(!b) vms->program.popn(arg);else {};}   
)

◆ Iff()

template<typename Grammar_t >
Primitive<bool,bool,bool> Builtins::Iff ( Op::Iff  ,
BUILTIN_LAMBDA { bool x=vms->template getpop< bool >();bool y=vms->template getpop< bool >();vms->template push< bool >(x==y);}   
)

◆ Implies()

template<typename Grammar_t >
Primitive<bool,bool,bool> Builtins::Implies ( Op::Implies  ,
BUILTIN_LAMBDA { bool x=vms->template getpop< bool >();bool y=vms->template getpop< bool >();vms->template push< bool >((not x) or y);}   
)

◆ Jmp()

template<typename Grammar_t >
Primitive Builtins::Jmp ( Op::Jmp  ,
BUILTIN_LAMBDA { vms->program.popn(arg);}   
)

◆ LexiconMemRecurse()

template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t,key_t,input_t> Builtins::LexiconMemRecurse ( Op::LexiconMemRecurse  ,
BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto key=vms->template getpop< key_t >();auto x=vms->template getpop< input_t >();auto memindex=std::make_pair(key, x);if(vms->template mem< key_t >().count(memindex)){ vms->push(vms->template mem< key_t >()[memindex]);} else { vms->xstack.push(x);vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->template memstack< key_t >().push(memindex);vms->program.push(Builtins::Mem< Grammar_t, key_t, output_t >.makeInstruction());vms->program.loader->push_program(vms->program, key);} }   
)

◆ LexiconRecurse()

template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t,key_t,input_t> Builtins::LexiconRecurse ( Op::LexiconRecurse  ,
BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto key=vms->template getpop< key_t >();auto mynewx=vms->template getpop< input_t >();vms->xstack.push(std::move(mynewx));vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->program.loader->push_program(vms->program, key);}   
)

◆ LexiconSafeMemRecurse()

template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t,key_t,input_t> Builtins::LexiconSafeMemRecurse ( Op::LexiconSafeMemRecurse  ,
BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());assert(vms->program.loader !=nullptr);if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< key_t >();vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { LexiconMemRecurse< Grammar_t, key_t >.call(vms, arg);} }   
)

◆ LexiconSafeRecurse()

template<typename Grammar_t , typename key_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t,key_t,input_t> Builtins::LexiconSafeRecurse ( Op::LexiconSafeRecurse  ,
BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());assert(vms->program.loader !=nullptr);if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< key_t >();vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { LexiconRecurse< Grammar_t, key_t >.call(vms, arg);} }   
)

◆ Mem()

template<typename Grammar_t , typename key_t , typename output_t = typename Grammar_t::output_t>
Primitive Builtins::Mem ( Op::Mem  ,
BUILTIN_LAMBDA { auto memindex=vms->template memstack< key_t >().top();vms->template memstack< key_t >().pop();if(vms->template mem< key_t >().count(memindex)==0) { vms->template mem< key_t >()[memindex]=vms->template gettop< output_t >();} }   
)

◆ MemRecurse()

template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t,input_t> Builtins::MemRecurse ( Op::MemRecurse  ,
BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);using mykey_t=short;if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto x=vms->template getpop< input_t >();auto memindex=std::make_pair(arg, x);if(vms->template mem< mykey_t >().count(memindex)){ vms->push(vms->template mem< mykey_t >()[memindex]);} else { vms->xstack.push(x);vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->template memstack< mykey_t >().push(memindex);vms->program.push(Builtins::Mem< Grammar_t, mykey_t, output_t >.makeInstruction());vms->program.loader->push_program(vms->program);} }   
)

◆ NoOp()

template<typename Grammar_t >
Primitive Builtins::NoOp ( Op::NoOp  ,
BUILTIN_LAMBDA { }   
)

◆ Not()

template<typename Grammar_t >
Primitive<bool,bool> Builtins::Not ( Op::Not  ,
BUILTIN_LAMBDA { vms->push(not vms->template getpop< bool >());}   
)

◆ Or()

template<typename Grammar_t >
Primitive<bool,bool,bool> Builtins::Or ( Op::Or  ,
BUILTIN_LAMBDA { bool b=vms->template getpop< bool >();if(b) { vms->program.popn(arg);vms->template push< bool >(true);} else { } }   
)

◆ PopX()

template<typename Grammar_t >
Primitive Builtins::PopX ( Op::PopX  ,
BUILTIN_LAMBDA { vms->xstack.pop();}   
)

◆ Recurse()

template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t,input_t> Builtins::Recurse ( Op::Recurse  ,
BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);if(vms->recursion_depth++> vms->MAX_RECURSE) { throw VMSRuntimeError();} auto mynewx=vms->template getpop< input_t >();vms->xstack.push(std::move(mynewx));vms->program.push(Builtins::PopX< Grammar_t >.makeInstruction());vms->program.loader->push_program(vms->program);}   
)

◆ SafeFlipP()

template<typename Grammar_t >
Primitive<bool,double> Builtins::SafeFlipP ( Op::SafeFlipP  ,
BUILTIN_LAMBDA { assert(vms->pool !=nullptr);double p=vms->template getpop< double >();if(std::isnan(p)) p=0.0;else if(p > 1.0) p=1.0;else if(p< 0.0) p=0.0;vms->pool->copy_increment_push(vms, true, log(p));bool b=vms->pool->increment_push(vms, false, log(1.0-p));if(b) { vms->status=vmstatus_t::RANDOM_CHOICE_NO_DELETE;} else { vms->status=vmstatus_t::RANDOM_CHOICE;} }   
)

◆ SafeMemRecurse()

template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t,input_t> Builtins::SafeMemRecurse ( Op::SafeMemRecurse  ,
BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { MemRecurse< Grammar_t >.call(vms, arg);} }   
)

◆ SafeRecurse()

template<typename Grammar_t , typename input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t>
Primitive<output_t, input_t> Builtins::SafeRecurse ( Op::SafeRecurse  ,
BUILTIN_LAMBDA { assert(not vms->template stack< input_t >().empty());if(vms->template stack< input_t >().top().size()==0) { vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { Recurse< Grammar_t >.call(vms, arg);} }   
)

◆ Sample()

template<typename Grammar_t , typename t , typename T = std::set<t>>
Primitive<t, T> Builtins::Sample ( Op::Sample  ,
BUILTIN_LAMBDA { auto s=vms->template getpop< T >();if(s.size()==1) { auto v=std::move(*s.begin());vms->template push< t >(std::move(v));} else { const double lp=-log(s.size());for(const auto &x :s) { bool b=vms->pool->copy_increment_push(vms, x, lp);if(not b) break;} vms->status=vmstatus_t::RANDOM_CHOICE;} }   
)

◆ Sample_int()

template<typename Grammar_t >
Primitive<int, int> Builtins::Sample_int ( Op::Sample  ,
BUILTIN_LAMBDA { const int mx=vms->template getpop< int >();const double lp=-log(mx);for(int i=0;i< mx;i++) { bool b=vms->pool->copy_increment_push(vms, i, lp);if(not b) break;} vms->status=vmstatus_t::RANDOM_CHOICE;}   
)

◆ UnusedNoOp()

template<typename Grammar_t >
Primitive Builtins::UnusedNoOp ( Op::NoOp  ,
BUILTIN_LAMBDA { assert(false);}   
)

◆ X()

template<typename Grammar_t >
Primitive<typename Grammar_t::input_t> Builtins::X ( Op::X  ,
BUILTIN_LAMBDA { assert(!vms->xstack.empty());vms->template push< typename Grammar_t::input_t >(vms->xstack.top());}   
)