Skip to content

Commit

Permalink
Merge branch 'main' into edit_listening_agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Necr0x0Der authored Dec 16, 2024
2 parents 52f4ab6 + b0b92c4 commit b236879
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 128 deletions.
4 changes: 2 additions & 2 deletions c/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ pub extern "C" fn bindings_add_var_binding(bindings: *mut bindings_t, var: atom_
_ => panic!("var argument must be variable atom")
};
let atom = atom.into_inner();
match bindings.clone().add_var_binding_v2(var, atom) {
match bindings.clone().add_var_binding(var, atom) {
Ok(new_bindings) => {
*bindings = new_bindings;
true
Expand Down Expand Up @@ -1309,7 +1309,7 @@ pub extern "C" fn bindings_merge(_self: bindings_t, other: *const bindings_t) ->
let other = unsafe{ &*other }.borrow();
let owned_self = _self.into_inner();

let new_set = owned_self.merge_v2(other);
let new_set = owned_self.merge(other);
new_set.into()
}

Expand Down
9 changes: 5 additions & 4 deletions lib/examples/custom_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ impl Grounded for TestDict {
impl CustomMatch for TestDict {
fn match_(&self, other: &Atom) -> MatchResultIter {
if let Some(other) = other.as_gnd::<TestDict>() {
other.0.iter().map(|(ko, vo)| {
Box::new(other.0.iter().map(|(ko, vo)| {
self.0.iter().map(|(k, v)| {
match_atoms(&Atom::expr(vec![k.clone(), v.clone()]), &Atom::expr(vec![ko.clone(), vo.clone()]))
}).fold(Box::new(std::iter::empty()) as MatchResultIter, |acc, i| {
Box::new(acc.chain(i))
})
}).fold(Box::new(std::iter::once(Bindings::new())),
|acc, i| { matcher::match_result_product(acc, i) })
}).collect::<BindingsSet>()
}).fold(BindingsSet::single(),
|acc, i| { acc.merge(&i) })
.into_iter())
} else {
Box::new(std::iter::empty())
}
Expand Down
166 changes: 53 additions & 113 deletions lib/src/atom/matcher.rs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions lib/src/metta/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ fn query<'a, T: Space>(space: T, prev: Option<Rc<RefCell<Stack>>>, to_eval: Atom
let result = |res, bindings| eval_result(prev.clone(), res, &call_stack, bindings);
let results: Vec<InterpretedAtom> = results.into_iter().flat_map(|b| {
log::debug!("interpreter::query: b: {}", b);
b.merge_v2(&bindings).into_iter()
b.merge(&bindings).into_iter()
}).filter_map(move |b| {
b.resolve(&var_x).map_or(None, |res| {
if b.has_loops() {
Expand Down Expand Up @@ -640,7 +640,7 @@ fn chain(stack: Stack, bindings: Bindings) -> Vec<InterpretedAtom> {
panic!("Unexpected state")
}
};
let b = Bindings::new().add_var_binding_v2(var, nested).unwrap();
let b = Bindings::new().add_var_binding(var, nested).unwrap();
let templ = apply_bindings_to_atom_move(templ, &b);
vec![InterpretedAtom(atom_to_stack(templ, prev), bindings)]
}
Expand Down Expand Up @@ -760,7 +760,7 @@ fn unify(stack: Stack, bindings: Bindings) -> Vec<InterpretedAtom> {
};
let bindings_ref = &bindings;
let matches: Vec<InterpretedAtom> = matches.into_iter().flat_map(move |b| {
b.merge_v2(bindings_ref).into_iter().filter_map(move |b| {
b.merge(bindings_ref).into_iter().filter_map(move |b| {
if b.has_loops() {
None
} else {
Expand Down Expand Up @@ -842,7 +842,7 @@ fn superpose_bind(stack: Stack, bindings: Bindings) -> Vec<InterpretedAtom> {
let stack = Stack::finished(prev.clone(), atom);
InterpretedAtom(stack, bindings)
};
b.merge_v2(&bindings).into_iter().filter_map(move |b| {
b.merge(&bindings).into_iter().filter_map(move |b| {
if b.has_loops() {
None
} else {
Expand Down Expand Up @@ -989,7 +989,7 @@ fn match_types(type1: &Atom, type2: &Atom, bindings: Bindings) -> Result<MatchRe
} else {
let bindings_copy = bindings.clone();
let mut result = match_atoms(type1, type2)
.flat_map(move |b| b.merge_v2(&bindings).into_iter())
.flat_map(move |b| b.merge(&bindings).into_iter())
.peekable();
if result.peek().is_none() {
log::trace!("match_types: no match: {} !~ {}", type1, type2);
Expand Down
12 changes: 11 additions & 1 deletion lib/src/metta/runner/stdlib/stdlib.metta
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@
(@doc-formal (@item $atom) (@kind atom) (@type $type) (@desc "No documentation")) )))))

(@doc help!
(@desc "Function prints documentation for the input atom.")
(@desc "Function prints documentation for the input atom. Without parameters prints the list of the stdlib functions.")
(@params (
(@param "Input to get documentation for")))
(@return "Unit atom"))
Expand All @@ -839,6 +839,16 @@
() ))
($other (Error $other "Cannot match @doc-formal structure") ))))

(: help! (-> (->)))
(= (help!) (let $top-space (mod-space! top)
(unify $top-space (@doc $name (@desc $desc) $params $ret)
(let () (println! (format-args "{}\n\t{}" ($name $desc))) Empty)
Empty)))
(= (help!) (let $top-space (mod-space! top)
(unify $top-space (@doc $name (@desc $desc))
(let () (println! (format-args "{}\n\t{}" ($name $desc))) Empty)
Empty)))

(@doc help-param!
(@desc "Function used by function help! to output parameters using println!")
(@params (
Expand Down
4 changes: 2 additions & 2 deletions lib/src/metta/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn check_arg_types(actual: &[Vec<Atom>], meta: &[Vec<Atom>], expected: &[Atom],
for typ in actual {
result_bindings.extend(
match_reducted_types_v2(typ, expected)
.flat_map(|b| b.merge_v2(&bindings))
.flat_map(|b| b.merge(&bindings))
.flat_map(|b| check_arg_types(actual_tail, meta_tail, expected_tail, b))
);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ pub fn match_reducted_types(left: &Atom, right: &Atom, bindings: &mut Bindings)
let matched = match result.len() {
0 => false,
1 => {
let result_set = result.pop().unwrap().merge_v2(bindings);
let result_set = result.pop().unwrap().merge(bindings);
*bindings = result_set.try_into().expect("Single result is expected because custom matching for types is not supported yet!");
true
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/space/grounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl GroundingSpace {
let query = matcher::apply_bindings_to_atom_move(query.clone(), &prev);
let mut res = self.query(&query);
res.drain(0..)
.flat_map(|next| next.merge_v2(&prev))
.flat_map(|next| next.merge(&prev))
.collect()
}).collect()
};
Expand Down
7 changes: 7 additions & 0 deletions repl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ fn main() -> Result<()> {
}
}

fn show_welcome_message() {
println!("Visit https://metta-lang.dev/ for tutorials.");
println!("Execute !(help!) to get list of the standard library functions.");
}

// To debug rustyline:
// RUST_LOG=rustyline=debug cargo run --example example 2> debug.log
fn start_interactive_mode(repl_params: ReplParams, mut metta: MettaShim) -> rustyline::Result<()> {
Expand Down Expand Up @@ -137,6 +142,8 @@ fn start_interactive_mode(repl_params: ReplParams, mut metta: MettaShim) -> rust
}
}

show_welcome_message();

//The Interpreter Loop
loop {

Expand Down

0 comments on commit b236879

Please sign in to comment.