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

final separation of math #807

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -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};
Expand Down
3 changes: 1 addition & 2 deletions lib/src/metta/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Contains MeTTa specific types, constants and functions.

pub mod text;
pub mod interpreter_minimal;
pub use interpreter_minimal as interpreter;
pub mod interpreter;
pub mod types;
pub mod runner;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/metta/runner/builtin_mods/catalog_mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/src/metta/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/metta/runner/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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<Tokenizer>, _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<Vec<Vec<Atom>>, 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[macro_use]
pub mod stdlib_math;
pub mod math;

use crate::*;
use crate::space::*;
Expand Down Expand Up @@ -1479,7 +1479,7 @@ fn interpret_no_error(space: DynSpace, expr: &Atom) -> Result<Vec<Atom>, String>

fn interpret(space: DynSpace, expr: &Atom) -> Result<Vec<Atom>, 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
}

Expand Down Expand Up @@ -1790,42 +1790,7 @@ pub fn register_common_tokens(tref: &mut Tokenizer, _tokenizer: Shared<Tokenizer
tref.register_token(regex(r"intersection-atom"), move |_| { intersection_op.clone() });
let union_op = Atom::gnd(UnionAtomOp{});
tref.register_token(regex(r"union-atom"), move |_| { union_op.clone() });
let pow_math_op = Atom::gnd(stdlib_math::PowMathOp {});
tref.register_token(regex(r"pow-math"), move |_| { pow_math_op.clone() });
let sqrt_math_op = Atom::gnd(stdlib_math::SqrtMathOp {});
tref.register_token(regex(r"sqrt-math"), move |_| { sqrt_math_op.clone() });
let abs_math_op = Atom::gnd(stdlib_math::AbsMathOp {});
tref.register_token(regex(r"abs-math"), move |_| { abs_math_op.clone() });
let log_math_op = Atom::gnd(stdlib_math::LogMathOp {});
tref.register_token(regex(r"log-math"), move |_| { log_math_op.clone() });
let trunc_math_op = Atom::gnd(stdlib_math::TruncMathOp {});
tref.register_token(regex(r"trunc-math"), move |_| { trunc_math_op.clone() });
let ceil_math_op = Atom::gnd(stdlib_math::CeilMathOp {});
tref.register_token(regex(r"ceil-math"), move |_| { ceil_math_op.clone() });
let floor_math_op = Atom::gnd(stdlib_math::FloorMathOp{});
tref.register_token(regex(r"floor-math"), move |_| { floor_math_op.clone() });
let round_math_op = Atom::gnd(stdlib_math::RoundMathOp{});
tref.register_token(regex(r"round-math"), move |_| { round_math_op.clone() });
let sin_math_op = Atom::gnd(stdlib_math::SinMathOp{});
tref.register_token(regex(r"sin-math"), move |_| { sin_math_op.clone() });
let asin_math_op = Atom::gnd(stdlib_math::AsinMathOp{});
tref.register_token(regex(r"asin-math"), move |_| { asin_math_op.clone() });
let cos_math_op = Atom::gnd(stdlib_math::CosMathOp{});
tref.register_token(regex(r"cos-math"), move |_| { cos_math_op.clone() });
let acos_math_op = Atom::gnd(stdlib_math::AcosMathOp{});
tref.register_token(regex(r"acos-math"), move |_| { acos_math_op.clone() });
let tan_math_op = Atom::gnd(stdlib_math::TanMathOp{});
tref.register_token(regex(r"tan-math"), move |_| { tan_math_op.clone() });
let atan_math_op = Atom::gnd(stdlib_math::AtanMathOp{});
tref.register_token(regex(r"atan-math"), move |_| { atan_math_op.clone() });
let isnan_math_op = Atom::gnd(stdlib_math::IsNanMathOp{});
tref.register_token(regex(r"isnan-math"), move |_| { isnan_math_op.clone() });
let isinf_math_op = Atom::gnd(stdlib_math::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)) });
math::register_common_tokens(tref, _tokenizer, space, metta);

#[cfg(feature = "pkg_mgmt")]
pkg_mgmt_ops::register_pkg_mgmt_tokens(tref, metta);
Expand Down Expand Up @@ -1925,7 +1890,7 @@ pub fn register_rust_stdlib_tokens(target: &mut Tokenizer) {
target.move_front(&mut rust_tokens);
}

pub static METTA_CODE: &'static str = include_str!("stdlib_minimal.metta");
pub static METTA_CODE: &'static str = include_str!("stdlib.metta");

/// Loader to Initialize the corelib module
///
Expand Down Expand Up @@ -1968,7 +1933,7 @@ mod tests {
use std::fmt::Display;
use regex::Regex;

fn run_program(program: &str) -> Result<Vec<Vec<Atom>>, String> {
pub fn run_program(program: &str) -> Result<Vec<Vec<Atom>>, String> {
let metta = Metta::new(Some(EnvBuilder::test_env()));
metta.run(SExprParser::new(program))
}
Expand Down
Loading