Fleet  0.0.9
Inference in the LOT
MyGrammar.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include<vector>
4 #include<functional>
5 
6 #include "Grammar.h"
7 #include "Singleton.h"
8 
9 using TSeq = std::vector<BindingTree*>;
10 using TtoT = std::function<BindingTree*(BindingTree*)>;
11 using TtoTSeq = std::function<TSeq(BindingTree*)>;
12 using TtoBool = std::function<bool(BindingTree*)>;
13 using TxTtoBool = std::function<bool(BindingTree*,BindingTree*)>;
14 
15 //std::function coreferent = +[](BindingTree* x) -> BindingTree* {
16 // if(x==nullptr) throw TreeException();
17 // return x->coreferent();
18 //};
19 
20 std::function parent = +[](BindingTree* x) -> BindingTree* {
21  if(x == nullptr) return nullptr;
22  return x->parent;
23 };
24 
25 std::function children = +[](BindingTree* x) -> TSeq {
26  if(x == nullptr) return {};
27  else {
28  TSeq out;
29  for(size_t i=0;i<x->nchildren();i++) {
30  out.push_back(&x->child(i));
31  }
32  return out;
33  }
34 };
35 
36 std::function ancestors = +[](BindingTree* x) -> TSeq {
37  if(x == nullptr) return {};
38  else {
39  TSeq out;
40  while(x->parent != nullptr) {
41  out.push_back(x->parent);
42  x = x->parent;
43  }
44  return out;
45  }
46 };
47 
48 std::function is_subject = +[](BindingTree* x) -> bool {
49  return (x != nullptr) and (x->pos==POS::NPS);
50 };
51 
52 std::function has_index = +[](BindingTree* x) -> bool {
53  return (x != nullptr) and (x->referent != -1);
54 };
55 
56 std::function null = +[](BindingTree* x) -> bool {
57  return x == nullptr;
58 };
59 
60 std::function eq_tree = +[](BindingTree* x, BindingTree* y) -> bool { return x == y;};
61 
62 std::function corefers = +[](BindingTree* x, BindingTree* y) -> bool {
63  return (x != nullptr) and (y != nullptr) and (x->referent == y->referent);
64 };
65 
66 std::function gt_linear = +[](BindingTree* x, BindingTree* y) -> bool {
67  return (x != nullptr) and (y != nullptr) and (x->linear_order > y->linear_order);
68 };
69 
70 // declare a grammar with our primitives
71 // Note that this ordering of primitives defines the order in Grammar
72 class MyGrammar : public Grammar<BindingTree*,bool, S,POS,int,bool,BindingTree*,TSeq,TtoT,TtoTSeq,TtoBool,TxTtoBool>,
73  public Singleton<MyGrammar> {
74 
76  using Super::Super;
77 public:
78 
80 
81  // pos predicates -- part of speech (NOT position)
82 // add("pos(%s)", +[](BindingTree* x) -> POS {
83 // if(x==nullptr) throw TreeException();
84 // return x->pos;
85 // });
86 //
87 // add("word(%s)", +[](BindingTree* x) -> S {
88 // if(x==nullptr) throw TreeException();
89 // if(x->target) throw TreeException(); // well, it's very clever -- we can't allow label on the target or the problem is trivial
90 // return x->word;
91 // });
92 
93  add("true", +[]() -> bool { return true; }, 0.1);
94  add("false", +[]() -> bool { return false; }, 0.1);
95  add("and(%s,%s)", Builtins::And<MyGrammar>, 1./4.);
96  add("or(%s,%s)", Builtins::Or<MyGrammar>, 1./4.);
97  add("iff(%s,%s)", Builtins::Iff<MyGrammar>, 1./4.);
98  add("not(%s)", Builtins::Not<MyGrammar>, 1./4.);
99 
100  add("if(%s,%s,%s)", Builtins::If<MyGrammar,S>, 1./3.);
101  add("if(%s,%s,%s)", Builtins::If<MyGrammar,POS>, 1./3.);
102  add("if(%s,%s,%s)", Builtins::If<MyGrammar,BindingTree*>, 1./3.);
103 
104  add("x", Builtins::X<MyGrammar>, TERMINAL_P);
105 
106  for(auto& w : words) {
107  add_terminal<S>(Q(w), w, TERMINAL_P/words.size());
108  }
109 
110  for(auto [l,p] : posmap) {
111  add_terminal<POS>(Q(l), p, TERMINAL_P/posmap.size());
112  }
113 
114 // add("pstr(%s)", +[](POS x) -> POS {
115 // print(str(int(x)));
116 // return x;
117 // });
118 
119  add("applyC(%s,%s)", +[](TxTtoBool f, BindingTree* x) -> TtoBool {
120  return [=](BindingTree* t) { return f(x,t); };
121  });
122  add("applyCC(%s,%s,%s)", +[](TxTtoBool f, BindingTree* x, BindingTree* y) -> bool {
123  return f(x,y);
124  });
125  add("eq_tree", +[]() -> TxTtoBool { return eq_tree; } );
126  add("corefers", +[]() -> TxTtoBool { return corefers; } );
127 
128  // functions on trees
129  add("applyR(%s,%s)", +[](TtoT f, BindingTree* x) -> BindingTree* { return f(x); } );
130  add("parent", +[]() -> TtoT { return parent;}) ;
131 // add("coreferent", +[]() -> TtoT { return coreferent;}) ;
132 
133  // trees to tree sequences
134  add("applyS(%s,%s)", +[](TtoTSeq s, BindingTree* x) -> TSeq { return s(x);});
135  add("children", +[]() -> TtoTSeq { return children;});
136  add("ancestors", +[]() -> TtoTSeq { return ancestors;});
137 
138  // predicates on trees
139  add("applyB(%s,%s)", +[](TtoBool f, BindingTree* x) -> bool { return f(x);});
140  add("has_index", +[]() -> TtoBool { return has_index;});
141  add("is_subject", +[]() -> TtoBool { return is_subject;});
142  add("null", +[]() -> TtoBool { return null;});
143 
144  // predicates can also be defined by these fancy types
145  // which check equality to some specific word or POS
146 // add("eq_word(%s)", +[](S a) -> TtoBool {
147 // std::function f = [=](BindingTree* t) -> bool {
148 // return (t != nullptr) and (t->word == a);
149 // };
150 // return f;
151 // });
152 
153  add("eq_pos(%s)", +[](POS a) -> TtoBool {
154  std::function f = [=](BindingTree* t) -> bool {
155  return (t != nullptr) and (t->pos == a);
156  };
157  return f;
158  });
159 
160  // Or we can extract these from trees. This has the advantage over pos(%s) and word(%s)
161  // above because we can handle nullptr tree arguments gracefully.
162 // add("eq_wordT(%s)", +[](BindingTree* a) -> TtoBool {
163 // std::function f = [=](BindingTree* t) -> bool {
164 // return (t != nullptr) and (a != nullptr) and (t->word == a->word);
165 // };
166 // return f;
167 // });
168 
169  add("eq_posT(%s)", +[](BindingTree* a) -> TtoBool {
170  std::function f = [=](BindingTree* t) -> bool {
171  return (t != nullptr) and (a != nullptr) and (t->pos == a->pos);
172  };
173  return f;
174  });
175 
176  // operations on tree sequences
177  add("empty(%s)", +[](TSeq s) -> bool {
178  return s.size() == 0;
179  });
180  add("head(%s)", +[](TSeq s) -> BindingTree* {
181  if(s.empty())
182  return nullptr; // throw TreeException(); // or could except but thats trickier
183  else
184  return s.at(0);
185  });
186  add("tail(%s)", +[](TSeq s) -> TSeq {
187  if(s.empty())
188  return {}; // throw TreeException()
189  else {
190  s.erase(s.begin());
191  return s;
192  }
193  });
194  add("append(%s,%s)", +[](TSeq x, TSeq y) -> TSeq {
195  for(auto& yi : y) {
196  x.push_back(yi);
197  }
198  return x;
199  });
200  add("reverse(%s)", +[](TSeq s) -> TSeq {
201  std::reverse(s.begin(), s.end());
202  return s;
203  });
204  add("map(%s,%s)", +[](TtoT f, TSeq s) -> TSeq {
205  TSeq out;
206  for(auto& x : s) {
207  out.push_back(f(x));
208  }
209  return out;
210  });
211  add("map_append(%s,%s)", +[](TtoTSeq f, TSeq s) -> TSeq {
212  TSeq out;
213  for(auto& x : s) {
214  for(auto& y : f(x)) {
215  out.push_back(y);
216  }
217  }
218  return out;
219  });
220 
221  add("filter(%s,%s)", +[](TtoBool f, TSeq s) -> TSeq {
222  TSeq out;
223  for(auto& x : s) {
224  if(f(x)) out.push_back(x);
225  }
226  return out;
227  });
228 
229  // NOTE THIS DOES NOT WORK WITH THE CACHED VERSION
230 // add("F(%s,%s)" , Builtins::LexiconRecurse<MyGrammar,S>); // note the number of arguments here
231  }
232 
233 } grammar;
234 
Definition: Grammar.h:44
MyGrammar grammar
POS
Definition: BindingTree.h:14
Definition: Singleton.h:6
std::function ancestors
Definition: MyGrammar.h:36
std::function gt_linear
Definition: MyGrammar.h:66
std::function has_index
Definition: MyGrammar.h:52
std::function children
Definition: MyGrammar.h:25
std::function< BindingTree *(BindingTree *)> TtoT
Definition: MyGrammar.h:10
Definition: BindingTree.h:46
std::string reverse(std::string x)
Definition: Strings.h:185
std::function null
Definition: MyGrammar.h:56
int referent
Definition: BindingTree.h:48
std::function eq_tree
Definition: MyGrammar.h:60
std::function< bool(BindingTree *, BindingTree *)> TxTtoBool
Definition: MyGrammar.h:13
std::vector< BindingTree * > TSeq
Definition: MyGrammar.h:9
std::string Q(const std::string &x)
Definition: Strings.h:199
std::function< TSeq(BindingTree *)> TtoTSeq
Definition: MyGrammar.h:11
const double TERMINAL_P
Definition: Main.cpp:24
int linear_order
Definition: BindingTree.h:50
this_t * parent
Definition: BaseNode.h:26
std::function< bool(BindingTree *)> TtoBool
Definition: MyGrammar.h:12
Definition: MyGrammar.h:72
MyGrammar()
Definition: MyGrammar.h:79
std::function corefers
Definition: MyGrammar.h:62
std::function is_subject
Definition: MyGrammar.h:48
POS pos
Definition: BindingTree.h:51
void add(std::string fmt, Primitive< T, args... > &b, double p=1.0, int a=0)
Definition: Grammar.h:312
std::function parent
Definition: MyGrammar.h:20
std::map< std::string, POS > posmap
Definition: BindingTree.h:16
const std::vector< std::string > words
Definition: Main.cpp:15
A grammar stores all of the rules associated with any kind of nonterminal and permits us to sample as...