|
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 T , typename Grammar_t , size_t MX> |
Primitive< T, T > | Sample_int (Op::Sample, BUILTIN_LAMBDA { const auto mx=vms->template getpop< T >();if(mx >=MX) throw VMSRuntimeError();const double lp=-log(mx);for(T 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 T , typename Grammar_t , size_t MX> |
Primitive< T, T, double > | Sample_int_geom (Op::Sample, BUILTIN_LAMBDA { const auto mx=vms->template getpop< T >();if(mx >=MX) throw VMSRuntimeError();const auto p=vms->template getpop< double >();double z=0.0;for(size_t i=0;i< mx;i++) z+=pow(p, i) *(1-p);const double lz=log(z);for(T i=0;i< mx;i++) { const double lp=i *log(p)+log(1-p) - lz;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 input_t = typename Grammar_t::input_t, typename output_t = typename Grammar_t::output_t> |
Primitive< output_t, input_t > | RecurseEmptyOnDepthException (Op::Recurse, BUILTIN_LAMBDA { assert(vms->program.loader !=nullptr);if(vms->recursion_depth++> vms->MAX_RECURSE) { auto mynewx=vms->template getpop< input_t >();vms->template push< output_t >(output_t{});} else { 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);} }) |
| This is a kind of recursion that, when it reaches the max recursion depth, returns empty string. This allows us to (approximately) deal with infinite sequences. More...
|
|
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);} }) |
|