Skip to content

ecoricemon/logic-eval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logic-eval

Crates.io CI Status Codecov

+------------------+
|  Feel(fresh) :-  |
|   Sleep(well),   |
|   Sun(shine),    |
|   Air(cool).     |
+------------------+

logic-eval is a Prolog-like logic evaluation library for Rust.

Features

  • SLG resolution: handles recursive queries with tabling.
  • Custom type support: use &str, interned strings, or your own Atom type.
  • Parsing: parse facts, rules, and queries from a Prolog-like text syntax with parse_str.
  • Basic logical operators: supports NOT, AND, and OR in rule bodies.

Examples

Parse text and query a database

use logic_eval::{Database, StrInterner, parse_str};

let mut db = Database::new();
let interner = StrInterner::new();

let dataset = "
    child(a, b).
    child(b, c).
    child(c, d).
    descend($X, $Y) :- child($X, $Y).
    descend($X, $Z) :- child($X, $Y), descend($Y, $Z).
";
db.insert_dataset(parse_str(dataset, &interner).unwrap());
db.commit();

let mut cx = db.query(parse_str("descend($X, $Y).", &interner).unwrap());

let mut answer = Vec::new();
while let Some(eval) = cx.prove_next() {
    let s = eval.into_iter().map(|assign| assign.to_string()).collect::<Vec<_>>().join(", ");
    answer.push(s);
}

assert_eq!(answer, [
    "$X = a, $Y = b",
    "$X = b, $Y = c",
    "$X = c, $Y = d",
    "$X = a, $Y = c",
    "$X = b, $Y = d",
    "$X = a, $Y = d",
]);

About

A prolog-like logic evaluator

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE.txt
MIT
LICENSE-MIT.txt

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages