HerbCore.jl Documentation

HerbCore.AbstractGrammarType
AbstractGrammar

Abstract type representing all grammars. It is assumed that all grammar structs have at least the following attributes:

  • rules::Vector{Any}: A list of RHS of rules (subexpressions).
  • types::Vector{Symbol}: A list of LHS of rules (types, all symbols).
  • isterminal::BitVector: A bitvector where bit i represents whether rule i is terminal.
  • iseval::BitVector: A bitvector where bit i represents whether rule i is an eval rule.
  • bytype::Dict{Symbol,Vector{Int}}: A dictionary that maps a type to all rules of said type.
  • domains::Dict{Symbol, BitVector}: A dictionary that maps a type to a domain bitvector. The domain bitvector has bit i set to true iff the ith rule is of this type.
  • childtypes::Vector{Vector{Symbol}}: A list of types of the children for each rule.

If a rule is terminal, the corresponding list is empty.

  • log_probabilities::Union{Vector{Real}, Nothing}: A list of probabilities for each rule.

If the grammar is non-probabilistic, the list can be nothing.

For concrete types, see ContextSensitiveGrammar within the HerbGrammar module.

source
HerbCore.AbstractHoleType
AbstractHole <: AbstractRuleNode

A AbstractHole is a placeholder where certain rules from the grammar can still be applied. The domain of a AbstractHole defines which rules can be applied. The domain is a bitvector, where the ith bit is set to true if the ith rule in the grammar can be applied.

source
HerbCore.AbstractRuleNodeType
abstract type AbstractRuleNode end

Abstract type for representing expression trees. An AbstractRuleNode is expected to implement the following functions:

  • isfilled(::AbstractRuleNode)::Bool. True iff the grammar rule this node holds is not ambiguous, i.e. has domain size 1.
  • isuniform(::AbstractRuleNode)::Bool. True iff the children of this node are known.
  • get_rule(::AbstractRuleNode)::Int. Returns the index of the grammar rule it represents.
  • get_children(::AbstractRuleNode)::Vector{AbstractRuleNode}. Returns the children of this node.

Expression trees consist of RuleNodes and AbstractHoles.

source
HerbCore.HoleType

Hole <: AbstractHole

  • domain: A bitvector, where the ith bit is set to true if the ith rule in the grammar can be applied.
source
HerbCore.RuleNodeType
RuleNode <: AbstractRuleNode

A RuleNode represents a node in an expression tree. Each node corresponds to a certain rule in the AbstractGrammar. A RuleNode consists of:

  • ind: The index of the rule in the AbstractGrammar which this node is representing.
  • _val: Field for caching evaluations of RuleNodes, preventing multiple unnecessary evaluations. The field can be used to store any needed infromation.
  • children: The children of this node in the expression tree
Compat

Evaluate immediately functionality is not yet supported by most of Herb.jl.

source
HerbCore.RuleNodeMethod
RuleNode(ind::Int, _val::Any)

Create a RuleNode for the AbstractGrammar rule with index ind, _val as immediately evaluated value and no children

Warning

Only use this constructor if you are absolutely certain that a rule is terminal and cannot have children. Use [RuleNode(ind::Int, grammar::AbstractGrammar)] for rules that might have children. In general, AbstractHoles should be used as a placeholder when the children of a node are not yet known.

Compat

Evaluate immediately functionality is not yet supported by most of Herb.jl.

source
HerbCore.UniformHoleType
UniformHole <: AbstractHole
  • domain: A bitvector, where the ith bit is set to true if the ith rule in the grammar can be applied. All rules in the domain are required to have the same childtypes.
  • children: The children of this hole in the expression tree.
source
Base.islessMethod
Base.isless(rn₁::AbstractRuleNode, rn₂::AbstractRuleNode)::Bool

Compares two RuleNodes. Returns true if the left RuleNode is less than the right RuleNode. Order is determined from the index of the RuleNodes. If both RuleNodes have the same index, a depth-first search is performed in both RuleNodes until nodes with a different index are found.

source
Base.lengthMethod
Base.length(root::RuleNode)

Return the number of nodes in the tree rooted at root.

source
HerbCore.get_node_at_locationMethod
get_node_at_location(root::Hole, location::Vector{Int})

Retrieves the current hole, if location is this very hole. Throws error otherwise.

source
HerbCore.get_pathMethod
get_path(root::AbstractRuleNode, node::AbstractRuleNode)

Returns the path from the root to the targetnode. Returns nothing if no path exists.

source
HerbCore.get_rulesequenceMethod
get_rulesequence(node::RuleNode, path::Vector{Int})

Extract the derivation sequence from a path (sequence of child indices) and an AbstractRuleNode. If the path is deeper than the deepest node, it returns what it has.

source
HerbCore.have_same_shapeMethod
have_same_shape(node1::AbstractRuleNode, node2::AbstractRuleNode)

Returns true iff node1 and node2 have the same shape Example: RuleNode(3, [ RuleNode(1), RuleNode(1) ]) and RuleNode(9, [ RuleNode(2), Hole(domain) ]) have the same shape: 1 root with 2 children.

source
HerbCore.isfilledMethod
isfilled(node::AbstractRuleNode)::Bool

Returns whether the [AbstractRuleNode] holds a single rule. This is always the case for RuleNodes. Holes are considered to be "filled" iff their domain size is exactly 1.

source
HerbCore.node_depthMethod
node_depth(root::AbstractRuleNode, node::AbstractRuleNode)::Int

Return the depth of node for an AbstractRuleNode tree rooted at root. Depth is 1 when root == node.

Warning

node must be a subtree of root in order for this function to work.

source
HerbCore.rulesoftypeFunction
rulesoftype(node::RuleNode, ruleset::Set{Int}[, ignoreNode::AbstractRuleNode])
rulesoftype(node::RuleNode, rule_index::Int[, ignoreNode::AbstractRuleNode])

Returns every rule in the ruleset that is also used in the AbstractRuleNode tree, but not in the ignoreNode subtree.

Warning

The ignoreNode must be a subtree of node for it to have an effect.

source
HerbCore.rulesoftypeFunction
rulesoftype(node::RuleNode, grammar::AbstractGrammar, ruletype::Symbol[, ignoreNode::AbstractRuleNode])

Returns every rule of nonterminal symbol ruletype from the grammar that is also used in the AbstractRuleNode tree, but not in the ignoreNode subtree.

Warning

The ignoreNode must be a subtree of node for it to have an effect.

source
HerbCore.rulesonleftMethod
rulesonleft(expr::RuleNode, path::Vector{Int})::Set{Int}

Finds all rules that are used in the left subtree defined by the path.

source
HerbCore.swap_nodeMethod
swap_node(expr::AbstractRuleNode, new_expr::AbstractRuleNode, path::Vector{Int})

Replace a node in expr, specified by path, with new_expr. Path is a sequence of child indices, starting from the root node.

source
HerbCore.swap_nodeMethod
swap_node(expr::RuleNode, node::RuleNode, child_index::Int, new_expr::RuleNode)

Replace child i of a node, a part of larger expr, with new_expr.

source

Index