Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal MeTTa interpreter written mostly in Rust #728

Merged
merged 31 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1c122ce
Add minimal MeTTa with Rust interpreter feature
vsbogd Jun 21, 2024
59273cd
Implement interpreter operation in Rust
vsbogd Jun 24, 2024
80ae400
Implement Clone for the iterators over results
vsbogd Jun 25, 2024
833174b
Add expression interpreter skeleton
vsbogd Jun 25, 2024
b44eff0
Implement interpreting tuples without known function type
vsbogd Jun 26, 2024
814f28c
Interpreting typed function with first type found
vsbogd Jun 26, 2024
763e095
Interpreting typed functions with arguments type checking
vsbogd Jun 27, 2024
d1635c6
Remove unnecessary eval wrapper around interpret operation
vsbogd Jun 28, 2024
53c6dae
Fix "bare minimal" unit test
vsbogd Jun 28, 2024
5a6d05e
Fix return vs replace semantics for the Rust minimal interpreter
vsbogd Jul 2, 2024
3cbe1e2
Rename error_atom to error_msg
vsbogd Jul 2, 2024
2b76581
Implement return-on-error as a native function
vsbogd Jul 2, 2024
7018c51
Add native metta-call implementation
vsbogd Jul 2, 2024
c965593
Add call to the stack when native function is called
vsbogd Jul 2, 2024
12a41f2
Apply bindings before evaluating grounded atoms
vsbogd Jul 3, 2024
52d165c
Take into account any function type available
vsbogd Jul 3, 2024
d90cf86
Remove deprecated arithmetics module
vsbogd Jul 3, 2024
f6b73ed
Use minimal Rust interpreter by default
vsbogd Jul 3, 2024
2e1d532
Rename interpret to metta
vsbogd Jul 4, 2024
67d5cb4
Remove eval which is not needed anymore
vsbogd Jul 5, 2024
4de1ced
Replace interpret by metta in unit tests
vsbogd Jul 5, 2024
148cec4
Remove MeTTa interpreter implemented in MeTTa
vsbogd Jul 5, 2024
4c94103
Merge branch 'main' into rust-minimal
vsbogd Jul 5, 2024
89b5014
Merge remote-tracking branch 'vsbogd/rust-minimal' into rust-minimal
vsbogd Jul 5, 2024
ac4a00f
Switch additional GitHub CI job to old interpreter testing
vsbogd Jul 5, 2024
a605bb9
Merge branch 'main' into rust-minimal
Necr0x0Der Jul 5, 2024
fdd9776
Merge remote-tracking branch 'trueagi/main' into rust-minimal
vsbogd Jul 9, 2024
c532c5d
Merge branch 'main' into rust-minimal
vsbogd Jul 9, 2024
2bd7502
Rename interpreter.rs into old_interpreter.rs
vsbogd Jul 10, 2024
8cae12e
Add a note about Rust MeTTa interpreter in minimal MeTTa
vsbogd Jul 10, 2024
e7e6dfa
Document minimal MeTTa interpreter structures and methods
vsbogd Jul 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This workflow is intended to run tests on minimal MeTTa interpreter.
# This workflow is intended to run tests on old Rust MeTTa interpreter.
# It is indicative and temporary, it doesn't prevent any changes from merging.

# This workflow uses actions that are not certified by GitHub. They are
# provided by a third-party and are governed by separate terms of service,
# privacy policy, and support documentation.

name: minimal
name: old_interpreter

on:
push:
Expand All @@ -16,7 +16,7 @@ on:
- main

jobs:
minimal:
old_interpreter:
runs-on: "ubuntu-20.04"

steps:
Expand All @@ -32,13 +32,13 @@ jobs:
- name: Build Rust library
working-directory: ./lib
run: |
cargo check --features minimal
cargo build --features minimal
cargo check --features old_interpreter
cargo build --features old_interpreter

- name: Test Rust library
working-directory: ./lib
run: |
RUST_LOG=hyperon=debug cargo test --features minimal
RUST_LOG=hyperon=debug cargo test --features old_interpreter

- name: Install cbindgen
uses: actions-rs/[email protected]
Expand Down Expand Up @@ -106,7 +106,7 @@ jobs:
cd build
# specify C compiler as conan could not find it automatically
# see https://github.com/conan-io/conan/issues/4322
cmake -DCARGO_ARGS="--features hyperon/minimal" -DCMAKE_BUILD_TYPE=Release -DPython3_EXECUTABLE=`which python` -DCMAKE_C_COMPILER=gcc ..
cmake -DCARGO_ARGS="--features hyperon/old_interpreter" -DCMAKE_BUILD_TYPE=Release -DPython3_EXECUTABLE=`which python` -DCMAKE_C_COMPILER=gcc ..

- name: Build C API
working-directory: ./build
Expand Down
10 changes: 10 additions & 0 deletions c/src/metta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,16 @@ pub extern "C" fn atom_error_message(atom: *const atom_ref_t, buf: *mut c_char,
hyperon::metta::UNIT_ATOM().into()
}

/// @brief Creates a Symbol atom for the special MeTTa symbol used to indicate
/// calling MeTTa interpreter.
/// @ingroup metta_language_group
/// @return The `atom_t` representing the interpret atom
/// @note The returned `atom_t` must be freed with `atom_free()`
///
#[no_mangle] pub extern "C" fn METTA_ATOM() -> atom_t {
hyperon::metta::METTA_SYMBOL.into()
}

/// @brief Checks whether Atom `atom` has Type `typ` in context of `space`
/// @ingroup metta_language_group
/// @param[in] space A pointer to the `space_t` representing the space context in which to perform the check
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ crate-type = ["lib"]
default = ["pkg_mgmt"]
# Add one of the features below into default list to enable.
# See https://doc.rust-lang.org/cargo/reference/features.html#the-features-section
minimal = [] # enables minimal MeTTa interpreter
old_interpreter = [] # enables old Rust interpreter
variable_operation = [] # enables evaluation of the expressions which have
# a variable on the first position
git = ["git2", "pkg_mgmt"]
Expand Down
2 changes: 1 addition & 1 deletion lib/benches/interpreter_minimal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(test)]
#[cfg(feature = "minimal")]
#[cfg(not(feature = "old_interpreter"))]
mod interpreter_minimal_bench {

extern crate test;
Expand Down
16 changes: 15 additions & 1 deletion lib/src/atom/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,10 +1073,24 @@ impl BindingsSet {
}
}

pub trait BindingsResultIter: Iterator<Item=Bindings> {
fn clone_(&self) -> Box<dyn BindingsResultIter>;
}
impl<T: 'static + Clone + Iterator<Item=Bindings>> BindingsResultIter for T {
fn clone_(&self) -> Box<dyn BindingsResultIter> {
Box::new(self.clone())
}
}

/// Iterator over atom matching results. Each result is an instance of [Bindings].
//TODO: A situation where a MatchResultIter returns an unbounded (infinite) number of results
// will hang this implementation, on account of `.collect()`
pub type MatchResultIter = Box<dyn Iterator<Item=matcher::Bindings>>;
pub type MatchResultIter = Box<dyn BindingsResultIter>;
impl Clone for MatchResultIter {
fn clone(&self) -> Self {
self.clone_()
}
}

/// Matches two atoms and returns an iterator over results. Atoms are
/// treated symmetrically.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/atom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@
/// ```
/// #[macro_use]
/// use hyperon::expr;
/// use hyperon::common::MUL;
/// use hyperon::metta::runner::arithmetics::MulOp;
///
/// let sym = expr!("A");
/// let var = expr!(x);
/// let gnd = expr!({42});
/// let expr = expr!("=" ("*2" n) ({MUL} n {2}));
/// let expr = expr!("=" ("*2" n) ({MulOp{}} n {2}));
///
/// assert_eq!(sym.to_string(), "A");
/// assert_eq!(var.to_string(), "$x");
Expand Down
147 changes: 0 additions & 147 deletions lib/src/common/arithmetics.rs

This file was deleted.

3 changes: 0 additions & 3 deletions lib/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub mod owned_or_borrowed;
mod flex_ref;
pub use flex_ref::FlexRef;

mod arithmetics;
pub use arithmetics::*;

use crate::*;
use crate::metta::text::{Tokenizer, SExprParser};
use std::cell::RefCell;
Expand Down
6 changes: 5 additions & 1 deletion lib/src/metta/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct InterpreterState<'a, T: SpaceRef<'a>> {
impl<'a, T: SpaceRef<'a>> InterpreterState<'a, T> {

/// INTERNAL USE ONLY. Create an InterpreterState that is ready to yield results
#[cfg(not(feature = "minimal"))]
#[cfg(feature = "old_interpreter")]
pub(crate) fn new_finished(_space: T, results: Vec<Atom>) -> Self {
Self {
step_result: StepResult::Return(results.into_iter().map(|atom| InterpretedAtom(atom, Bindings::new())).collect()),
Expand Down Expand Up @@ -177,6 +177,10 @@ pub fn interpret_init<'a, T: Space + 'a>(space: T, expr: &Atom) -> InterpreterSt
}

fn interpret_init_internal<'a, T: Space + 'a>(space: T, expr: &Atom) -> StepResult<'a, Results, InterpreterError> {
let expr = match <&[Atom]>::try_from(expr).ok() {
Some([op, atom, _typ, _space]) if *op == METTA_SYMBOL => atom,
_ => expr,
};
let context = InterpreterContextRef::new(space);
interpret_as_type_plan(context,
InterpretedAtom(expr.clone(), Bindings::new()),
Expand Down
Loading
Loading