From c537f1000f17e7565e6aa1568e6001842d47165d Mon Sep 17 00:00:00 2001 From: Innokenty Date: Fri, 22 Nov 2024 08:06:54 +0300 Subject: [PATCH 1/2] final separation of math --- ...{interpreter_minimal.rs => interpreter.rs} | 2 +- lib/src/metta/mod.rs | 4 +- .../metta/runner/builtin_mods/catalog_mods.rs | 2 +- lib/src/metta/runner/mod.rs | 4 +- lib/src/metta/runner/modules/mod.rs | 4 +- .../stdlib_math.rs => stdlib/math.rs} | 61 ++++++++++++++++--- .../runner/{stdlib_minimal => stdlib}/mod.rs | 45 ++------------ .../stdlib.metta} | 0 8 files changed, 65 insertions(+), 57 deletions(-) rename lib/src/metta/{interpreter_minimal.rs => interpreter.rs} (99%) rename lib/src/metta/runner/{stdlib_minimal/stdlib_math.rs => stdlib/math.rs} (90%) rename lib/src/metta/runner/{stdlib_minimal => stdlib}/mod.rs (97%) rename lib/src/metta/runner/{stdlib_minimal/stdlib_minimal.metta => stdlib/stdlib.metta} (100%) diff --git a/lib/src/metta/interpreter_minimal.rs b/lib/src/metta/interpreter.rs similarity index 99% rename from lib/src/metta/interpreter_minimal.rs rename to lib/src/metta/interpreter.rs index f2d5d18da..e024fd3cf 100644 --- a/lib/src/metta/interpreter_minimal.rs +++ b/lib/src/metta/interpreter.rs @@ -6,7 +6,7 @@ use crate::atom::matcher::*; use crate::space::*; use crate::metta::*; use crate::metta::types::*; -use crate::metta::runner::stdlib_minimal::IfEqualOp; +use crate::metta::runner::stdlib::IfEqualOp; use crate::common::collections::CowArray; use std::fmt::{Debug, Display, Formatter}; diff --git a/lib/src/metta/mod.rs b/lib/src/metta/mod.rs index 59d56d7f8..545b66be1 100644 --- a/lib/src/metta/mod.rs +++ b/lib/src/metta/mod.rs @@ -1,8 +1,8 @@ //! Contains MeTTa specific types, constants and functions. pub mod text; -pub mod interpreter_minimal; -pub use interpreter_minimal as interpreter; +pub mod interpreter; +// pub use interpreter_minimal as interpreter; pub mod types; pub mod runner; diff --git a/lib/src/metta/runner/builtin_mods/catalog_mods.rs b/lib/src/metta/runner/builtin_mods/catalog_mods.rs index be850be1e..36b125dfe 100644 --- a/lib/src/metta/runner/builtin_mods/catalog_mods.rs +++ b/lib/src/metta/runner/builtin_mods/catalog_mods.rs @@ -3,7 +3,7 @@ use crate::space::grounding::GroundingSpace; use crate::metta::{ARROW_SYMBOL, ATOM_TYPE_SYMBOL, UNIT_TYPE}; use crate::metta::runner::{Metta, ModuleLoader, RunContext, DynSpace}; use crate::metta::runner::pkg_mgmt::{UpdateMode, ManagedCatalog}; -use crate::metta::runner::stdlib_minimal::{regex, unit_result}; +use crate::metta::runner::stdlib::{regex, unit_result}; //DISCUSSION: We want to expose more of the pkg_mgmt / catalog system to MeTTa through programmatic // interfaces, but the details are unclear. Most importantly, the use cases are unclear, and those diff --git a/lib/src/metta/runner/mod.rs b/lib/src/metta/runner/mod.rs index 3cec2109b..9e65b97ab 100644 --- a/lib/src/metta/runner/mod.rs +++ b/lib/src/metta/runner/mod.rs @@ -88,8 +88,8 @@ pub use environment::{Environment, EnvBuilder}; use super::interpreter::{interpret, interpret_init, interpret_step, InterpreterState}; #[macro_use] -pub mod stdlib_minimal; -use stdlib_minimal::CoreLibLoader; +pub mod stdlib; +use stdlib::CoreLibLoader; mod builtin_mods; use builtin_mods::*; diff --git a/lib/src/metta/runner/modules/mod.rs b/lib/src/metta/runner/modules/mod.rs index f17628526..a7cbc8bf7 100644 --- a/lib/src/metta/runner/modules/mod.rs +++ b/lib/src/metta/runner/modules/mod.rs @@ -7,8 +7,8 @@ use crate::metta::runner::*; use regex::Regex; -use super::interpreter_minimal::interpret; -use super::stdlib_minimal::*; +use super::interpreter::interpret; +use super::stdlib::*; mod mod_names; pub(crate) use mod_names::{ModNameNode, mod_name_from_path, normalize_relative_module_name, mod_name_remove_prefix, ModNameNodeDisplayWrapper}; diff --git a/lib/src/metta/runner/stdlib_minimal/stdlib_math.rs b/lib/src/metta/runner/stdlib/math.rs similarity index 90% rename from lib/src/metta/runner/stdlib_minimal/stdlib_math.rs rename to lib/src/metta/runner/stdlib/math.rs index 2de6ed0db..245417a7c 100644 --- a/lib/src/metta/runner/stdlib_minimal/stdlib_math.rs +++ b/lib/src/metta/runner/stdlib/math.rs @@ -4,8 +4,13 @@ use crate::metta::*; use std::convert::TryInto; +use crate::space::DynSpace; +use crate::common::shared::Shared; +use crate::metta::text::Tokenizer; use crate::metta::runner::arithmetics::*; -use crate::metta::runner::stdlib_minimal::grounded_op; +use crate::metta::runner::stdlib::grounded_op; +use crate::metta::runner::Metta; +use crate::metta::runner::stdlib::regex; #[derive(Clone, Debug)] pub struct PowMathOp {} @@ -405,17 +410,55 @@ impl CustomExecute for IsInfMathOp { } } +//TODO: The additional arguments are a temporary hack on account of the way the operation atoms store references +// to the runner & module state. https://github.com/trueagi-io/hyperon-experimental/issues/410 +pub fn register_common_tokens(tref: &mut Tokenizer, _tokenizer: Shared, _space: &DynSpace, metta: &Metta) { + + let pow_math_op = Atom::gnd(PowMathOp {}); + tref.register_token(regex(r"pow-math"), move |_| { pow_math_op.clone() }); + let sqrt_math_op = Atom::gnd(SqrtMathOp {}); + tref.register_token(regex(r"sqrt-math"), move |_| { sqrt_math_op.clone() }); + let abs_math_op = Atom::gnd(AbsMathOp {}); + tref.register_token(regex(r"abs-math"), move |_| { abs_math_op.clone() }); + let log_math_op = Atom::gnd(LogMathOp {}); + tref.register_token(regex(r"log-math"), move |_| { log_math_op.clone() }); + let trunc_math_op = Atom::gnd(TruncMathOp {}); + tref.register_token(regex(r"trunc-math"), move |_| { trunc_math_op.clone() }); + let ceil_math_op = Atom::gnd(CeilMathOp {}); + tref.register_token(regex(r"ceil-math"), move |_| { ceil_math_op.clone() }); + let floor_math_op = Atom::gnd(FloorMathOp{}); + tref.register_token(regex(r"floor-math"), move |_| { floor_math_op.clone() }); + let round_math_op = Atom::gnd(RoundMathOp{}); + tref.register_token(regex(r"round-math"), move |_| { round_math_op.clone() }); + let sin_math_op = Atom::gnd(SinMathOp{}); + tref.register_token(regex(r"sin-math"), move |_| { sin_math_op.clone() }); + let asin_math_op = Atom::gnd(AsinMathOp{}); + tref.register_token(regex(r"asin-math"), move |_| { asin_math_op.clone() }); + let cos_math_op = Atom::gnd(CosMathOp{}); + tref.register_token(regex(r"cos-math"), move |_| { cos_math_op.clone() }); + let acos_math_op = Atom::gnd(AcosMathOp{}); + tref.register_token(regex(r"acos-math"), move |_| { acos_math_op.clone() }); + let tan_math_op = Atom::gnd(TanMathOp{}); + tref.register_token(regex(r"tan-math"), move |_| { tan_math_op.clone() }); + let atan_math_op = Atom::gnd(AtanMathOp{}); + tref.register_token(regex(r"atan-math"), move |_| { atan_math_op.clone() }); + let isnan_math_op = Atom::gnd(IsNanMathOp{}); + tref.register_token(regex(r"isnan-math"), move |_| { isnan_math_op.clone() }); + let isinf_math_op = Atom::gnd(IsInfMathOp{}); + tref.register_token(regex(r"isinf-math"), move |_| { isinf_math_op.clone() }); + tref.register_token(regex(r"PI"), + |_| { Atom::gnd(Number::Float(std::f64::consts::PI)) }); + tref.register_token(regex(r"EXP"), + |_| { Atom::gnd(Number::Float(std::f64::consts::E)) }); + + #[cfg(feature = "pkg_mgmt")] + metta::runner::stdlib::pkg_mgmt_ops::register_pkg_mgmt_tokens(tref, metta); +} + #[cfg(test)] mod tests { use super::*; - use crate::metta::text::SExprParser; - use crate::metta::runner::EnvBuilder; - use crate::metta::runner::Metta; - - fn run_program(program: &str) -> Result>, String> { - let metta = Metta::new(Some(EnvBuilder::test_env())); - metta.run(SExprParser::new(program)) - } + use crate::metta::runner::stdlib::tests::run_program; #[test] fn metta_pow_math() { diff --git a/lib/src/metta/runner/stdlib_minimal/mod.rs b/lib/src/metta/runner/stdlib/mod.rs similarity index 97% rename from lib/src/metta/runner/stdlib_minimal/mod.rs rename to lib/src/metta/runner/stdlib/mod.rs index 686905cdb..65b313495 100644 --- a/lib/src/metta/runner/stdlib_minimal/mod.rs +++ b/lib/src/metta/runner/stdlib/mod.rs @@ -1,5 +1,5 @@ #[macro_use] -pub mod stdlib_math; +pub mod math; use crate::*; use crate::space::*; @@ -1479,7 +1479,7 @@ fn interpret_no_error(space: DynSpace, expr: &Atom) -> Result, String> fn interpret(space: DynSpace, expr: &Atom) -> Result, String> { let expr = Atom::expr([METTA_SYMBOL, expr.clone(), ATOM_TYPE_UNDEFINED, Atom::gnd(space.clone())]); - let result = crate::metta::interpreter_minimal::interpret(space, &expr); + let result = crate::metta::interpreter::interpret(space, &expr); result } @@ -1790,42 +1790,7 @@ pub fn register_common_tokens(tref: &mut Tokenizer, _tokenizer: Shared Result>, String> { + pub fn run_program(program: &str) -> Result>, String> { let metta = Metta::new(Some(EnvBuilder::test_env())); metta.run(SExprParser::new(program)) } diff --git a/lib/src/metta/runner/stdlib_minimal/stdlib_minimal.metta b/lib/src/metta/runner/stdlib/stdlib.metta similarity index 100% rename from lib/src/metta/runner/stdlib_minimal/stdlib_minimal.metta rename to lib/src/metta/runner/stdlib/stdlib.metta From 9db3bcd1e9c176195caddd8d2d2412c7903c7ff5 Mon Sep 17 00:00:00 2001 From: Vitaly Bogdanov Date: Fri, 22 Nov 2024 12:49:54 +0300 Subject: [PATCH 2/2] Update lib/src/metta/mod.rs --- lib/src/metta/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/metta/mod.rs b/lib/src/metta/mod.rs index 545b66be1..0963a36dc 100644 --- a/lib/src/metta/mod.rs +++ b/lib/src/metta/mod.rs @@ -2,7 +2,6 @@ pub mod text; pub mod interpreter; -// pub use interpreter_minimal as interpreter; pub mod types; pub mod runner;