Fleet  0.0.9
Inference in the LOT
Namespaces | Macros | Functions
Builtins.h File Reference

The Builtin type just stores a function pointer and an Op command. This makes it a bit handier to define Primitives because they all take this form. More...

#include "Numerics.h"
#include "IO.h"
#include "Errors.h"
#include "Primitive.h"
Include dependency graph for Builtins.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 Builtins
 

Macros

#define BUILTIN_LAMBDA   +[](typename Grammar_t::VirtualMachineState_t* vms, int arg) -> void
 

Functions

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());})
 
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 { } })
 
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 { } })
 
template<typename Grammar_t >
Primitive< bool, bool > Builtins::Not (Op::Not, BUILTIN_LAMBDA { vms->push(not vms->template getpop< bool >());})
 
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 {};})
 
template<typename Grammar_t >
Primitive Builtins::Jmp (Op::Jmp, BUILTIN_LAMBDA { vms->program.popn(arg);})
 
template<typename Grammar_t >
Primitive Builtins::PopX (Op::PopX, BUILTIN_LAMBDA { vms->xstack.pop();})
 
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;} })
 
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;} })
 
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;} })
 
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;} })
 
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;})
 
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 >();} })
 
template<typename Grammar_t >
Primitive Builtins::NoOp (Op::NoOp, BUILTIN_LAMBDA { })
 
template<typename Grammar_t >
Primitive Builtins::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 > 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);})
 
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);} })
 
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);} })
 
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);} })
 
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);})
 
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);} })
 
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);} })
 
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);} })
 

Detailed Description

The Builtin type just stores a function pointer and an Op command. This makes it a bit handier to define Primitives because they all take this form.

Macro Definition Documentation

◆ BUILTIN_LAMBDA

#define BUILTIN_LAMBDA   +[](typename Grammar_t::VirtualMachineState_t* vms, int arg) -> void