Fleet  0.0.9
Inference in the LOT
MyGrammar.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include "Grammar.h"
8 #include "Singleton.h"
9 
10 
11 const double CONSTANT_P = 3.0; // upweight probability for grammar constants.
12 
13 
14 class MyGrammar : public Grammar<S,S, S,char,bool,double,StrSet,int>,
15  public Singleton<MyGrammar> {
16 public:
17 
18  MyGrammar() {
19 
20  add("tail(%s)", +[](S s) -> S {
21  if(s.length()>0)
22  s.erase(0);
23  return s;
24  });
25 
26  add_vms<S,S,S>("append(%s,%s)", new std::function(+[](MyGrammar::VirtualMachineState_t* vms, int) {
27  S b = vms->getpop<S>();
28  S& a = vms->stack<S>().topref();
29 
30  if(a.length() + b.length() > max_length) throw VMSRuntimeError();
31  else a += b;
32  }));
33 
34  add_vms<S,S,char>("pair(%s,%s)", new std::function(+[](MyGrammar::VirtualMachineState_t* vms, int) {
35  char b = vms->getpop<char>();
36  S& a = vms->stack<S>().topref();
37 
38  if(a.length() + 1 > max_length) throw VMSRuntimeError();
39  else a += b;
40  }));
41 
42 // add("c2s(%s)", +[](char c) -> S { return S(1,c); });
43  add("head(%s)", +[](S s) -> S { return (s.empty() ? EMPTY_STRING : S(1,s.at(0))); }); // head here could be a char, except that it complicates stuff, so we'll make it a string str
44  add("\u00D8", +[]() -> S { return EMPTY_STRING; }, CONSTANT_P);
45  add("(%s==%s)", +[](S x, S y) -> bool { return x==y; });
46  add("empty(%s)", +[](S x) -> bool { return x.length()==0; });
47 
48  add("insert(%s,%s)", +[](S x, S y) -> S {
49  size_t l = x.length();
50  if(l == 0)
51  return y;
52  else if(l + y.length() > max_length)
53  throw VMSRuntimeError();
54  else {
55  // put y into the middle of x
56  size_t pos = l/2;
57  S out = x.substr(0, pos);
58  out.append(y);
59  out.append(x.substr(pos));
60  return out;
61  }
62  });
63 
64 
65  // add an alphabet symbol (\Sigma)
66  add("\u03A3", +[]() -> StrSet {
67  StrSet out;
68  for(const auto& a: alphabet) {
69  out.emplace(1,a);
70  }
71  return out;
72  }, CONSTANT_P);
73 
74  // set operations:
75  add("{%s}", +[](S x) -> StrSet {
76  StrSet s; s.insert(x); return s;
77  }, CONSTANT_P);
78 
79  add("(%s\u222A%s)", +[](StrSet s, StrSet x) -> StrSet {
80  for(auto& xi : x) {
81  s.insert(xi);
82  if(s.size() > max_setsize) throw VMSRuntimeError();
83  }
84  return s;
85  });
86 
87  add("(%s\u2216%s)", +[](StrSet s, StrSet x) -> StrSet {
88  StrSet output;
89 
90  // this would usually be implemented like this, but it's overkill (and slower) because normally
91  // we just have single elemnents
92  std::set_difference(s.begin(), s.end(), x.begin(), x.end(), std::inserter(output, output.begin()));
93 
94  return output;
95  });
96 
97 
98  add("and(%s,%s)", Builtins::And<MyGrammar>);
99  add("or(%s,%s)", Builtins::Or<MyGrammar>);
100  add("not(%s)", Builtins::Not<MyGrammar>);
101 
102  add("x", Builtins::X<MyGrammar>, CONSTANT_P);
103 
104  add("if(%s,%s,%s)", Builtins::If<MyGrammar,S>);
105  add("if(%s,%s,%s)", Builtins::If<MyGrammar,StrSet>);
106  add("if(%s,%s,%s)", Builtins::If<MyGrammar,double>);
107 
108  add("flip(%s)", Builtins::FlipP<MyGrammar>, CONSTANT_P);
109  add("sample(%s)", Builtins::Sample<MyGrammar, S>, CONSTANT_P);
110 
111  const int pdenom=24;
112  for(int a=1;a<=pdenom/2;a++) {
113  std::string s = str(a/std::gcd(a,pdenom)) + "/" + str(pdenom/std::gcd(a,pdenom));
114  if(a==pdenom/2) {
115  add_terminal( s, double(a)/pdenom, CONSTANT_P);
116  }
117  else {
118  add_terminal( s, double(a)/pdenom, 1.0);
119  }
120  }
121 
122  add("F%s(%s)" , Builtins::LexiconRecurse<MyGrammar,int>, 1./2.);
123  add("Fm%s(%s)", Builtins::LexiconMemRecurse<MyGrammar,int>, 1./2.);
124  }
125 } grammar;
Definition: VMSRuntimeError.h:13
Definition: Grammar.h:44
MyGrammar grammar
Definition: VirtualMachineState.h:46
Definition: Singleton.h:6
std::string S
Definition: Main.cpp:28
T getpop()
Retrieves and pops the element of type T from the stack.
Definition: VirtualMachineState.h:145
std::set< S > StrSet
Definition: Main.cpp:15
std::string str(BindingTree *t)
Definition: BindingTree.h:195
VMSStack< T > & stack()
Returns a reference to the stack (of a given type)
Definition: VirtualMachineState.h:125
void add_terminal(std::string fmt, T x, double p=1.0, Op o=Op::Standard, int a=0)
Add a variable that is NOT A function – simplification for adding alphabets etc. This just wraps stu...
Definition: Grammar.h:435
const std::string EMPTY_STRING
Definition: Strings.h:17
Definition: MyGrammar.h:72
MyGrammar()
Definition: MyGrammar.h:79
size_t max_length
Definition: Main.cpp:20
size_t max_setsize
Definition: Main.cpp:21
const double CONSTANT_P
Definition: Main.cpp:42
void add(std::string fmt, Primitive< T, args... > &b, double p=1.0, int a=0)
Definition: Grammar.h:312
S alphabet
Definition: Main.cpp:19
A grammar stores all of the rules associated with any kind of nonterminal and permits us to sample as...