Module programinduction::pcfg
source · Expand description
(representation) Probabilistic context-free grammar without bound variables or polymorphism.
Examples
use polytype::tp;
use programinduction::{Task, pcfg::{task_by_evaluation, Grammar, Rule}};
fn evaluator(name: &str, inps: &[i32]) -> Result<i32, ()> {
match name {
"0" => Ok(0),
"1" => Ok(1),
"plus" => Ok(inps[0] + inps[1]),
_ => unreachable!(),
}
}
let g = Grammar::new(
tp!(EXPR),
vec![
Rule::new("0", tp!(EXPR), 1.0),
Rule::new("1", tp!(EXPR), 1.0),
Rule::new("plus", tp!(@arrow[tp!(EXPR), tp!(EXPR), tp!(EXPR)]), 1.0),
],
);
// task: the number 4
let target = 4;
let task = task_by_evaluation(&evaluator, &target, tp!(EXPR));
// solution:
let expr = g.parse("plus(1, plus(1, plus(1,1)))").unwrap();
assert!(task.oracle(&g, &expr).is_finite())
Structs
- Identifies a rule by its location in
grammar.rules
. - Parameters for PCFG parameter estimation.
- Parameters for PCFG genetic programming (
GP
). - (representation) Probabilistic context-free grammar. Currently cannot handle bound variables or polymorphism.
- A PCFG rule specifies a production that can happen for a particular nonterminal.
Enums
Functions
- Create a task based on evaluating a PCFG sentence and comparing its output against data.