HerbInterpret.jl Documentation

HerbInterpret._is_input_tagMethod
_is_input_tag(tag, input_set)

Checks whether a tag is an input. A tag is treated as an input terminal if:

  1. it is a Symbol that contains the substring "_arg_" (the default convention), OR
  2. input_set is provided and tag ∈ input_set.
source
HerbInterpret.build_match_casesMethod
build_match_cases(grammar; target_module=@__MODULE__, input_symbols=nothing)

Return a vector of "guarded return" branches of the form:

r == k && return <rhs>

These branches are intended to be spliced into a block after r = get_rule(prog); c = get_children(prog).

Returns a vector of branching expressions.

source
HerbInterpret.build_match_cases_statefulMethod
build_match_cases_stateful_rgf(grammar; target_module=@__MODULE__, state_name=:state, max_steps=1000)

Like build_match_cases_stateful, but emits code for a RuntimeGeneratedFunction body:

  • recursion is expressed as self(self, child, state)
  • WHILE is inlined (bounded by max_steps) to avoid needing external helpers
source
HerbInterpret.evaluate_programMethod
evaluate_program(program::RuleNode, examples::Vector{<:IOExample}, grammar::AbstractGrammar, evaluation_function::Function)

Runs a program on the examples and returns tuples of actual desired output and the program's output

source
HerbInterpret.execute_on_inputMethod
execute_on_input(grammar::AbstractGrammar, program::RuleNode, input::Dict{Symbol, T})::Any where T

Converts a RuleNode program into an expression using a given grammar, then evaluates this expression with a single input dictionary input and a symbol table derived from the grammar using execute_on_input(tab::SymbolTable, expr::Any, input::Dict{Symbol, T}).

Arguments

  • grammar::AbstractGrammar: A grammar object used to convert the RuleNode into an executable expression.
  • program::RuleNode: The program, represented as a RuleNode, to be converted and evaluated.
  • input::Dict{Symbol, T}: A dictionary providing input values for symbols used in the generated expression.

Returns

  • Any: The result of evaluating the generated expression with the given input dictionary.
source
HerbInterpret.execute_on_inputMethod
execute_on_input(grammar::AbstractGrammar, program::RuleNode, input::Vector{T})::Vector{Any} where T <: Dict{Symbol, <:Any}

Converts a RuleNode program into an expression using a given grammar, then evaluates this expression for each input dictionary in a vector input and a symbol table derived from the grammar using execute_on_input(tab::SymbolTable, expr::Any, input::Dict{Symbol, T}).

Arguments

  • grammar::AbstractGrammar: A grammar object used to convert the RuleNode into an executable expression.
  • program::RuleNode: The program, represented as a RuleNode, to be converted and evaluated.
  • input::Vector{T}: A vector of dictionaries, each providing input values for symbols used in the generated expression.

Returns

  • Vector{Any}: A vector containing the results of evaluating the generated expression for each input dictionary.
source
HerbInterpret.execute_on_inputMethod
execute_on_input(tab::SymbolTable, expr::Any, input::Dict{Symbol, T})::Any where T

Evaluates an expression expr within the context of a symbol table tab and a single input dictionary input. The input dictionary keys should match the symbols used in the expression, and their values are used during the expression's evaluation.

Arguments

  • tab::SymbolTable: A symbol table containing predefined symbols and their associated values or functions.
  • expr::Any: The expression to be evaluated. Can be any Julia expression that is valid within the context of the provided symbol table and input.
  • input::Dict{Symbol, T}: A dictionary where each key is a symbol used in the expression, and the value is the corresponding value to be used in the expression's evaluation. The type T can be any type.

Returns

  • Any: The result of evaluating the expression with the given symbol table and input dictionary.
Warning

This function throws exceptions that are caused in the given expression. These exceptions have to be handled by the caller of this function.

source
HerbInterpret.execute_on_inputMethod
execute_on_input(tab::SymbolTable, expr::Any, input::Vector{T})::Vector{<:Any} where T <: Dict{Symbol, <:Any}

Wrapper around execute_on_input to execute all inputs given as an array.

Arguments

  • tab::SymbolTable: A symbol table containing predefined symbols and their associated values or functions.
  • expr::Any: The expression to be evaluated for each input dictionary.
  • inputs::Vector{T}: A vector of dictionaries, each serving as an individual set of inputs for the expression's evaluation.

Returns

  • Vector{<:Any}: A vector containing the results of evaluating the expression for each input dictionary.
source
HerbInterpret.interpretMethod
interpret(tab::SymbolTable, ex::Expr)

Evaluates an expression without compiling it. Uses AST and symbol lookups. Only supports :call and :(=) expressions at the moment.

Example usage:

tab = SymbolTable(:f => f, :x => x)
ex = :(f(x))
interpret(tab, ex)

WARNING: This function throws exceptions that are caused in the given expression. These exceptions have to be handled by the caller of this function.

source
HerbInterpret.make_interpreterMethod
make_interpreter(grammar::AbstractGrammar; input_symbols::Union{Nothing,AbstractVector{Symbol}} = nothing, target_module::Module = @__MODULE__, cache_module::Module = HerbInterpret)

Construct a fast, runtime-generated interpreter for programs represented as HerbCore.AbstractRuleNodes.

The returned value is a callable GeneratedInterpreter (a small wrapper around a RuntimeGeneratedFunctions.RuntimeGeneratedFunction) that can be applied to:

  • a single input dictionary: interp(prog, input::AbstractDict{Symbol,Any})
  • a vector of input dictionaries: interp(prog, inputs::AbstractVector{<:AbstractDict{Symbol,Any}})
  • a single HerbSpecification.IOExample: interp(prog, ex::IOExample) (uses ex.in)
  • a vector of IOExamples: interp(prog, exs::AbstractVector{<:IOExample})

Arguments

  • grammar: The grammar whose rule indices define the operational semantics. The interpreter dispatches by r = HerbCore.get_rule(prog).

Keyword arguments

  • input_symbols: Optional list of symbols that should be interpreted as inputs. If provided, terminals matching these symbols (and any symbol following the _arg_ convention) are read from the input dict.

  • target_module: Module in which operator/function symbols appearing in the grammar are resolved. This is important when the grammar uses domain-specific primitives (e.g. concat_cvc, substr_cvc) that are defined in a benchmark module rather than in the caller’s module.

  • cache_module: Module used by RuntimeGeneratedFunctions.jl to store its internal cache.

source
HerbInterpret.make_stateful_interpreterMethod
make_stateful_interpreter_rgf(grammar; target_module=@__MODULE__, cache_module=HerbInterpret, max_steps=1000)

Build a RuntimeGeneratedFunctions-backed state-threading interpreter.

  • target_module controls where primitives (inc, moveRight, etc.) are resolved.
  • cache_module controls where RGF stores its cache. This module must have RuntimeGeneratedFunctions.init(@__MODULE__) executed at module top level.
  • max_steps bounds generated WHILE loops.
source

Index