[][src]Struct term_rewriting::RuleContext

pub struct RuleContext {
    pub lhs: Context,
    pub rhs: Vec<Context>,
}

A Rule with Holes; a sort of Rule template.

See Context for more information.

Examples

let mut sig = Signature::default();

// Constructing a RuleContext manually.
let left = parse_context(&mut sig, "A(B C [!])").expect("parse of A(B C [!])");
let b = parse_context(&mut sig, "B [!]").expect("parse of B [!]");
let c = parse_context(&mut sig, "C").expect("parse of C");

let r = RuleContext::new(left, vec![b, c]).unwrap();

// Constructing a RuleContext using the parser.
let r2 = parse_rulecontext(&mut sig, "A(B C [!]) = B [!] | C").expect("parse of A(B C [!]) = B [!] | C");

assert_eq!(r, r2);

Fields

lhs: Context

The left hand side (lhs) of a RuleContext.

rhs: Vec<Context>

The right hand sides (rhs) of a RuleContext.

Methods

impl RuleContext[src]

pub fn new(lhs: Context, rhs: Vec<Context>) -> Option<RuleContext>[src]

Create a new RuleContext if possible. The RuleContext must abide by the same restrictions as a Rule being created with Rule::new.

Examples

let mut sig = Signature::default();

let left = parse_context(&mut sig, "A(B C [!])").expect("parse of A(B C [!])");
let b = parse_context(&mut sig, "B [!]").expect("parse of B [!]");
let c = parse_context(&mut sig, "C").expect("parse of C");

let r = RuleContext::new(left, vec![b, c]).unwrap();

assert_eq!(r.pretty(), "A(B, C, [!]) = B [!] | C");

let left = parse_context(&mut sig, "A(B C [!])").expect("parse of A(B C [!])");
let b = parse_context(&mut sig, "B [!] x_").expect("parse of B [!] x_");
let c = parse_context(&mut sig, "C").expect("parse of C");

assert_eq!(RuleContext::new(left, vec![b, c]), None);

let left = parse_context(&mut sig, "x_").expect("parse of x_");
let b = parse_context(&mut sig, "B [!]").expect("parse of B [!]");

assert_eq!(RuleContext::new(left, vec![b]), None);

pub fn display(&self) -> String[src]

Serialize a RuleContext.

Examples

let mut sig = Signature::default();

let rule = parse_rulecontext(&mut sig, "A B(x_) CONS(SUCC(SUCC(ZERO)) CONS(SUCC(ZERO) CONS(ZERO NIL))) DECC(DECC(DIGIT(1) 0) 5) = [!] CONS(A CONS(B(x_) CONS(SUCC(SUCC(ZERO)) NIL)))")
    .expect("parse of A B(x_) CONS(SUCC(SUCC(ZERO)) CONS(SUCC(ZERO) CONS(ZERO NIL))) DECC(DECC(DIGIT(1) 0) 5) = [!] CONS(A CONS(B(x_) CONS( SUCC(SUCC(ZERO)) NIL)))");

assert_eq!(rule.display(), ".(.(.(A B(x_)) CONS(SUCC(SUCC(ZERO)) CONS(SUCC(ZERO) CONS(ZERO NIL)))) DECC(DECC(DIGIT(1) 0) 5)) = .([!] CONS(A CONS(B(x_) CONS(SUCC(SUCC(ZERO)) NIL))))");

pub fn pretty(&self) -> String[src]

A human-readable serialization of the RuleContext.

Examples

let mut sig = Signature::default();

let rule = parse_rulecontext(&mut sig, "A B(x_) CONS(SUCC(SUCC(ZERO)) CONS(SUCC(ZERO) CONS(ZERO NIL))) DECC(DECC(DIGIT(1) 0) 5) = [!] CONS(A CONS(B(x_) CONS(SUCC(SUCC(ZERO)) NIL)))")
    .expect("parse of A B(x_) CONS(SUCC(SUCC(ZERO)) CONS(SUCC(ZERO) CONS(ZERO NIL))) DECC(DECC(DIGIT(1) 0) 5) = [!] CONS(A CONS(B(x_) CONS( SUCC(SUCC(ZERO)) NIL)))");

assert_eq!(rule.pretty(), "A B(x_) [2, 1, 0] 105 = [!] [A, B(x_), 2]");

pub fn subcontexts(&self) -> Vec<(&Context, Place)>[src]

Get all the subcontexts and Places in a RuleContext.

Examples

let mut sig = Signature::default();

let r =
    parse_rulecontext(&mut sig, "A(x_ [!]) = C(x_) | D([!])").expect("parse of A(x_ B[!]) = C(x_) | D([!])");

let subcontexts: Vec<String> = r.subcontexts()
    .iter()
    .map(|(c, p)| format!("({}, {:?})", c.display(), p))
    .collect();

assert_eq!(
    subcontexts,
    vec![
        "(A(x_ [!]), [0])",
        "(x_, [0, 0])",
        "([!], [0, 1])",
        "(C(x_), [1])",
        "(x_, [1, 0])",
        "(D([!]), [2])",
        "([!], [2, 0])",
    ]
);

pub fn holes(&self) -> Vec<Place>[src]

The Places of all of the Holes in the RuleContext.

Examples

let mut sig = Signature::default();

let r =
    parse_rulecontext(&mut sig, "A(x_ [!]) = C(x_) | D([!])").expect("parse of A(x_ B[!]) = C(x_) | D([!])");

let p: &[usize] = &[0, 1];
let p2: &[usize] = &[2, 0];

assert_eq!(r.holes(), vec![p, p2]);

pub fn variables(&self) -> Vec<Variable>[src]

All the Variables in a RuleContext.

Examples

let mut sig = Signature::default();

let r = parse_rulecontext(&mut sig, "A(x_ [!]) = C(x_)").expect("parse of A(x_ [!]) = C(x_)");
let r_variables: Vec<String> = r.variables().iter().map(|v| v.display()).collect();

assert_eq!(r_variables, vec!["x_"]);

let r = parse_rulecontext(&mut sig, "B(y_ z_) = C [!]").expect("parse of B(y_ z_) = C [!]");
let r_variables: Vec<String> = r.variables().iter().map(|v| v.display()).collect();

assert_eq!(r_variables, vec!["y_", "z_"]);

pub fn operators(&self) -> Vec<Operator>[src]

All the Operators in a RuleContext.

Examples

let mut sig = Signature::default();

let r = parse_rulecontext(&mut sig, "A(D E) = C([!])").expect("parse of A(D E) = C([!])");
let r_ops: Vec<String> = r.operators().iter().map(|o| o.display()).collect();

assert_eq!(r_ops, vec!["D", "E", "A", "C"]);

let r = parse_rulecontext(&mut sig, "B(F x_) = C [!]").expect("parse of B(F x_) = C [!]");
let r_ops: Vec<String> = r.operators().iter().map(|o| o.display()).collect();

assert_eq!(r_ops, vec!["F", "B", "C", "."]);

pub fn at(&self, p: &[usize]) -> Option<&Context>[src]

Get a specific subcontext in a RuleContext.

Examples

let mut sig = Signature::default();

let r = parse_rulecontext(&mut sig, "A(x_ [!]) = B | C(x_ [!])").expect("parse of A(x_ [!]) = B | C(x_ [!])");

assert_eq!(r.at(&[0]).unwrap().display(), "A(x_ [!])");
assert_eq!(r.at(&[0,1]).unwrap().display(), "[!]");
assert_eq!(r.at(&[0,0]).unwrap().display(), "x_");
assert_eq!(r.at(&[1]).unwrap().display(), "B");
assert_eq!(r.at(&[2]).unwrap().display(), "C(x_ [!])");

pub fn replace(
    &self,
    place: &[usize],
    subcontext: Context
) -> Option<RuleContext>
[src]

Replace one subcontext with another Context.

Examples

let mut sig = Signature::default();

let mut r = parse_rulecontext(&mut sig, "A(x_) = B | C(x_) | [!]").expect("parse of A(x_) = B| C(x_) | [!]");
let new_context = parse_context(&mut sig, "E [!]").expect("parse of E [!]");
let new_r = r.replace(&[1], new_context);

assert_ne!(r, new_r.clone().unwrap());
assert_eq!(new_r.unwrap().pretty(), "A(x_) = E [!] | C(x_) | [!]");

pub fn to_rule(&self) -> Result<Rule, ()>[src]

Convert a RuleContext to a Rule if possible.

Examples

let mut sig = Signature::default();

let r = parse_rulecontext(&mut sig, "A(x_ [!]) = B | C(x_ [!])").expect("parse of A(x_ [!]) = B | C(x_ [!])");

assert!(r.to_rule().is_err());

let r = parse_rulecontext(&mut sig, "A(x_) = B | C(x_)").expect("parse of A(x_) = B | C(x_)");
let rule = r.to_rule().expect("converting RuleContext to Rule");

assert_eq!(rule.pretty(), "A(x_) = B | C(x_)");

Trait Implementations

impl From<Rule> for RuleContext[src]

impl Clone for RuleContext[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl PartialEq<RuleContext> for RuleContext[src]

impl Eq for RuleContext[src]

impl Hash for RuleContext[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl Debug for RuleContext[src]

Auto Trait Implementations

impl Send for RuleContext

impl Sync for RuleContext

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]