Fleet  0.0.9
Inference in the LOT
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Grammar< GRAMMAR_TYPES > Class Template Reference

#include <Grammar.h>

Classes

class  RuleIterator
 

Public Types

using this_t = Grammar< GRAMMAR_TYPES... >
 
template<typename input_t , typename output_t >
using VirtualMachineState_t = VirtualMachineState< input_t, output_t, GRAMMAR_TYPES... >
 
typedef std::tuple< GRAMMAR_TYPES... > GrammarTypesAsTuple
 

Public Member Functions

template<class T >
constexpr nonterminal_t nt ()
 
 Grammar ()
 
template<typename... T>
 Grammar (std::tuple< T... > tup)
 Constructor for grammar that uses a tuple of Primitives. This is the most important and commonly used grammar constructor in Fleet. More...
 
 Grammar (const Grammar &g)=delete
 
 Grammar (const Grammar &&g)=delete
 
RuleIterator begin () const
 
RuleIterator end () const
 
size_t count_nonterminals () const
 
size_t count_rules (const nonterminal_t nt) const
 
size_t count_rules () const
 
size_t count_terminals (nonterminal_t nt) const
 
size_t count_nonterminals (nonterminal_t nt) const
 
void show (std::string prefix="# ")
 For a given nt, returns the number of finite trees that nt can expand to if its finite; 0 if its infinite. *. More...
 
virtual void add (Rule &&r)
 
template<typename... args, size_t... Is>
void add (std::tuple< args... > t, std::index_sequence< Is... >)
 
template<typename T , typename... args>
void add (Primitive< T, args... > p, const int arg=0)
 
template<typename T , typename... args>
void add (BuiltinPrimitive< T, args... > p, const int arg=0)
 
template<typename T , typename... args>
void add (BuiltinOp o, std::string format, const double p=1.0, const int arg=0)
 
size_t get_index_of (const Rule *r) const
 
virtual Ruleget_rule (const nonterminal_t nt, size_t k) const
 
virtual Ruleget_rule (const nonterminal_t nt, const BuiltinOp o, const int a=0)
 
virtual Ruleget_rule (const nonterminal_t nt, const std::string s) const
 
virtual Ruleget_rule (const std::string s) const
 
double rule_normalizer (const nonterminal_t nt) const
 
virtual Rulesample_rule (const nonterminal_t nt) const
 
Node makeNode (const Rule *r) const
 
Node generate (const nonterminal_t nt, unsigned long depth=0) const
 
template<class t >
Node generate (unsigned long depth=0)
 
Node copy_resample (const Node &node, bool f(const Node &n)) const
 
std::vector< size_t > get_counts (const Node &node) const
 
double log_probability (const Node &n) const
 
Node expand_from_names (std::deque< std::string > &q) const
 
Node expand_from_names (std::string s) const
 
Node expand_from_names (const char *c) const
 
size_t neighbors (const Node &node) const
 
void expand_to_neighbor (Node &node, int &which)
 
double neighbor_prior (const Node &node, int &which) const
 
void complete (Node &node)
 

Static Public Member Functions

template<typename X >
static constexpr bool is_in_GRAMMAR_TYPES ()
 

Public Attributes

size_t GRAMMAR_MAX_DEPTH = 64
 
std::vector< Rulerules [N_NTs]
 
double Z [N_NTs]
 

Static Public Attributes

static constexpr size_t N_NTs = std::tuple_size<std::tuple<GRAMMAR_TYPES...>>::value
 

Detailed Description

template<typename... GRAMMAR_TYPES>
class Grammar< GRAMMAR_TYPES >

Author
Steven Piantadosi
Date
05/09/20

Member Typedef Documentation

§ GrammarTypesAsTuple

template<typename... GRAMMAR_TYPES>
typedef std::tuple<GRAMMAR_TYPES...> Grammar< GRAMMAR_TYPES >::GrammarTypesAsTuple

§ this_t

template<typename... GRAMMAR_TYPES>
using Grammar< GRAMMAR_TYPES >::this_t = Grammar<GRAMMAR_TYPES...>

§ VirtualMachineState_t

template<typename... GRAMMAR_TYPES>
template<typename input_t , typename output_t >
using Grammar< GRAMMAR_TYPES >::VirtualMachineState_t = VirtualMachineState<input_t, output_t, GRAMMAR_TYPES...>

Constructor & Destructor Documentation

§ Grammar() [1/4]

template<typename... GRAMMAR_TYPES>
Grammar< GRAMMAR_TYPES >::Grammar ( )
inline

§ Grammar() [2/4]

template<typename... GRAMMAR_TYPES>
template<typename... T>
Grammar< GRAMMAR_TYPES >::Grammar ( std::tuple< T... >  tup)
inline

Constructor for grammar that uses a tuple of Primitives. This is the most important and commonly used grammar constructor in Fleet.

Parameters
tup- a tuple of Primitives

§ Grammar() [3/4]

template<typename... GRAMMAR_TYPES>
Grammar< GRAMMAR_TYPES >::Grammar ( const Grammar< GRAMMAR_TYPES > &  g)
delete

§ Grammar() [4/4]

template<typename... GRAMMAR_TYPES>
Grammar< GRAMMAR_TYPES >::Grammar ( const Grammar< GRAMMAR_TYPES > &&  g)
delete

Member Function Documentation

§ add() [1/5]

template<typename... GRAMMAR_TYPES>
virtual void Grammar< GRAMMAR_TYPES >::add ( Rule &&  r)
inlinevirtual

Add a rule

§ add() [2/5]

template<typename... GRAMMAR_TYPES>
template<typename... args, size_t... Is>
void Grammar< GRAMMAR_TYPES >::add ( std::tuple< args... >  t,
std::index_sequence< Is... >   
)
inline

§ add() [3/5]

template<typename... GRAMMAR_TYPES>
template<typename T , typename... args>
void Grammar< GRAMMAR_TYPES >::add ( Primitive< T, args... >  p,
const int  arg = 0 
)
inline

§ add() [4/5]

template<typename... GRAMMAR_TYPES>
template<typename T , typename... args>
void Grammar< GRAMMAR_TYPES >::add ( BuiltinPrimitive< T, args... >  p,
const int  arg = 0 
)
inline

§ add() [5/5]

template<typename... GRAMMAR_TYPES>
template<typename T , typename... args>
void Grammar< GRAMMAR_TYPES >::add ( BuiltinOp  o,
std::string  format,
const double  p = 1.0,
const int  arg = 0 
)
inline

§ begin()

template<typename... GRAMMAR_TYPES>
RuleIterator Grammar< GRAMMAR_TYPES >::begin ( ) const
inline

§ complete()

template<typename... GRAMMAR_TYPES>
void Grammar< GRAMMAR_TYPES >::complete ( Node node)
inline

§ copy_resample()

template<typename... GRAMMAR_TYPES>
Node Grammar< GRAMMAR_TYPES >::copy_resample ( const Node node,
bool   fconst Node &n 
) const
inline

Make a copy of node where all nodes satisfying f are regenerated from the grammar.

Parameters
node
f- a function saying what we should resample
Returns
NOTE: this does NOT allow f to apply to nullptr children (so cannot be used to fill in)

§ count_nonterminals() [1/2]

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::count_nonterminals ( ) const
inline

How many nonterminals are there in the grammar.

Returns

§ count_nonterminals() [2/2]

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::count_nonterminals ( nonterminal_t  nt) const
inline

Count th enumber of non-terminal rules of return type nt

Parameters
nt
Returns

§ count_rules() [1/2]

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::count_rules ( const nonterminal_t  nt) const
inline

Returns the number of rules of return type nt

Parameters
nt
Returns

§ count_rules() [2/2]

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::count_rules ( ) const
inline

Total number of rules

Returns

§ count_terminals()

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::count_terminals ( nonterminal_t  nt) const
inline

Count the number of terminal rules of return type nt

Parameters
nt
Returns

§ end()

template<typename... GRAMMAR_TYPES>
RuleIterator Grammar< GRAMMAR_TYPES >::end ( ) const
inline

§ expand_from_names() [1/3]

template<typename... GRAMMAR_TYPES>
Node Grammar< GRAMMAR_TYPES >::expand_from_names ( std::deque< std::string > &  q) const
inline

Fills an entire tree using the string format prefixes – see get_rule(std::string). Here q should contain strings like "3:'a'" which says expand nonterminal type 3 to the rule matching 'a'

Parameters
q
Returns

§ expand_from_names() [2/3]

template<typename... GRAMMAR_TYPES>
Node Grammar< GRAMMAR_TYPES >::expand_from_names ( std::string  s) const
inline

Expand from names where s is delimited by ':'

Parameters
s
Returns

§ expand_from_names() [3/3]

template<typename... GRAMMAR_TYPES>
Node Grammar< GRAMMAR_TYPES >::expand_from_names ( const char *  c) const
inline

§ expand_to_neighbor()

template<typename... GRAMMAR_TYPES>
void Grammar< GRAMMAR_TYPES >::expand_to_neighbor ( Node node,
int &  which 
)
inline

§ generate() [1/2]

template<typename... GRAMMAR_TYPES>
Node Grammar< GRAMMAR_TYPES >::generate ( const nonterminal_t  nt,
unsigned long  depth = 0 
) const
inline

Sample an entire tree from this grammar (keeping track of depth in case we recurse too far) of return type nt. This samples a rule, makes them with makeNode, and then recurses.

Parameters
nt
depth
Returns
Returns a Node sampled from the grammar.

NOTE: this may throw a DepthException if the grammar recurses too far (usually that means the grammar is improper)

§ generate() [2/2]

template<typename... GRAMMAR_TYPES>
template<class t >
Node Grammar< GRAMMAR_TYPES >::generate ( unsigned long  depth = 0)
inline

A friendly version of generate that can be called with template by type.

Parameters
depth
Returns

§ get_counts()

template<typename... GRAMMAR_TYPES>
std::vector<size_t> Grammar< GRAMMAR_TYPES >::get_counts ( const Node node) const
inline

Compute a vector of counts of how often each rule was used, in a standard order given by iterating over nts and then iterating over rules

Parameters
node
Returns

§ get_index_of()

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::get_index_of ( const Rule r) const
inline

Find the index in rules of where r is.

Parameters
r
Returns

§ get_rule() [1/4]

template<typename... GRAMMAR_TYPES>
virtual Rule* Grammar< GRAMMAR_TYPES >::get_rule ( const nonterminal_t  nt,
size_t  k 
) const
inlinevirtual

Get the k'th rule of type nt

Parameters
nt
k
Returns

§ get_rule() [2/4]

template<typename... GRAMMAR_TYPES>
virtual Rule* Grammar< GRAMMAR_TYPES >::get_rule ( const nonterminal_t  nt,
const BuiltinOp  o,
const int  a = 0 
)
inlinevirtual

Get rule of type nt with a given BuiltinOp and argument a

Parameters
nt
o
a
Returns

§ get_rule() [3/4]

template<typename... GRAMMAR_TYPES>
virtual Rule* Grammar< GRAMMAR_TYPES >::get_rule ( const nonterminal_t  nt,
const std::string  s 
) const
inlinevirtual

Return a rule based on s, which must uniquely be a prefix of the rule's format of a given nonterminal type. If s is the empty string, however, it must match exactly.

Parameters
s
Returns

§ get_rule() [4/4]

template<typename... GRAMMAR_TYPES>
virtual Rule* Grammar< GRAMMAR_TYPES >::get_rule ( const std::string  s) const
inlinevirtual

Return a rule based on s, which must uniquely be a prefix of the rule's format. If s is the empty string, however, it must match exactly.

Parameters
s
Returns

§ is_in_GRAMMAR_TYPES()

template<typename... GRAMMAR_TYPES>
template<typename X >
static constexpr bool Grammar< GRAMMAR_TYPES >::is_in_GRAMMAR_TYPES ( )
inlinestatic

§ log_probability()

template<typename... GRAMMAR_TYPES>
double Grammar< GRAMMAR_TYPES >::log_probability ( const Node n) const
inline

Compute the log probability of a tree according to the grammar. NOTE: here we ignore nodes that are Null meaning that we compute the partial probability

Parameters
n
Returns

§ makeNode()

template<typename... GRAMMAR_TYPES>
Node Grammar< GRAMMAR_TYPES >::makeNode ( const Rule r) const
inline

Helper function to create a node according to this grammar. This is how nodes get their log probabilities.

Parameters
r
Returns

§ neighbor_prior()

template<typename... GRAMMAR_TYPES>
double Grammar< GRAMMAR_TYPES >::neighbor_prior ( const Node node,
int &  which 
) const
inline

§ neighbors()

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::neighbors ( const Node node) const
inline

§ nt()

template<typename... GRAMMAR_TYPES>
template<class T >
constexpr nonterminal_t Grammar< GRAMMAR_TYPES >::nt ( )
inline

template function giving the index of its template argument (index in GRAMMAR_TYPES). NOTE: The names here are decayed (meaning that references and base types are the same.

§ rule_normalizer()

template<typename... GRAMMAR_TYPES>
double Grammar< GRAMMAR_TYPES >::rule_normalizer ( const nonterminal_t  nt) const
inline

Return the normalizing constant (NOT log) for all rules of type nt

Parameters
nt
Returns

§ sample_rule()

template<typename... GRAMMAR_TYPES>
virtual Rule* Grammar< GRAMMAR_TYPES >::sample_rule ( const nonterminal_t  nt) const
inlinevirtual

Randomly sample a rule of type nt.

Parameters
nt
Returns

§ show()

template<typename... GRAMMAR_TYPES>
void Grammar< GRAMMAR_TYPES >::show ( std::string  prefix = "# ")
inline

For a given nt, returns the number of finite trees that nt can expand to if its finite; 0 if its infinite. *.

Parameters
nt- the type of this nonterminal

Show the grammar by printing each rule

Member Data Documentation

§ GRAMMAR_MAX_DEPTH

template<typename... GRAMMAR_TYPES>
size_t Grammar< GRAMMAR_TYPES >::GRAMMAR_MAX_DEPTH = 64

§ N_NTs

template<typename... GRAMMAR_TYPES>
constexpr size_t Grammar< GRAMMAR_TYPES >::N_NTs = std::tuple_size<std::tuple<GRAMMAR_TYPES...>>::value
static

§ rules

template<typename... GRAMMAR_TYPES>
std::vector<Rule> Grammar< GRAMMAR_TYPES >::rules[N_NTs]

§ Z

template<typename... GRAMMAR_TYPES>
double Grammar< GRAMMAR_TYPES >::Z[N_NTs]

The documentation for this class was generated from the following file: