bug fixes and performance improvements
This commit is contained in:
12
src/external/bool/boolean.rs
vendored
12
src/external/bool/boolean.rs
vendored
@@ -1,5 +1,6 @@
|
||||
|
||||
use crate::{atomic_inert, representations::{interpreted::Clause, Primitive}, foreign::Atom};
|
||||
use crate::foreign::Atom;
|
||||
use crate::representations::{interpreted::{Clause, ExprInst}, Primitive};
|
||||
use crate::atomic_inert;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Boolean(pub bool);
|
||||
@@ -7,11 +8,12 @@ atomic_inert!(Boolean);
|
||||
|
||||
impl From<bool> for Boolean { fn from(value: bool) -> Self { Self(value) } }
|
||||
|
||||
impl<'a> TryFrom<&'a Clause> for Boolean {
|
||||
impl TryFrom<ExprInst> for Boolean {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: &'a Clause) -> Result<Self, Self::Error> {
|
||||
if let Clause::P(Primitive::Atom(Atom(a))) = value {
|
||||
fn try_from(value: ExprInst) -> Result<Self, Self::Error> {
|
||||
let expr = value.expr();
|
||||
if let Clause::P(Primitive::Atom(Atom(a))) = &expr.clause {
|
||||
if let Some(b) = a.as_any().downcast_ref::<Boolean>() {
|
||||
return Ok(*b)
|
||||
}
|
||||
|
||||
46
src/external/bool/equals.rs
vendored
46
src/external/bool/equals.rs
vendored
@@ -1,10 +1,8 @@
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
|
||||
use crate::external::litconv::with_lit;
|
||||
use crate::representations::{interpreted::ExprInst, Literal};
|
||||
use crate::{atomic_impl, atomic_redirect, externfn_impl};
|
||||
use crate::foreign::Atom;
|
||||
use crate::representations::{Primitive, Literal};
|
||||
use crate::representations::interpreted::Clause;
|
||||
|
||||
use super::super::assertion_error::AssertionError;
|
||||
use super::boolean::Boolean;
|
||||
@@ -15,38 +13,34 @@ use super::boolean::Boolean;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Equals2;
|
||||
externfn_impl!(Equals2, |_: &Self, c: Clause| {Ok(Equals1{c})});
|
||||
externfn_impl!(Equals2, |_: &Self, x: ExprInst| {Ok(Equals1{x})});
|
||||
|
||||
/// Partially applied Equals function
|
||||
///
|
||||
/// Prev state: [Equals2]; Next state: [Equals0]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct Equals1{ c: Clause }
|
||||
atomic_redirect!(Equals1, c);
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Equals1{ x: ExprInst }
|
||||
atomic_redirect!(Equals1, x);
|
||||
atomic_impl!(Equals1);
|
||||
externfn_impl!(Equals1, |this: &Self, c: Clause| {
|
||||
let a: Literal = this.c.clone().try_into()
|
||||
.map_err(|_| AssertionError::ext(this.c.clone(), "a primitive"))?;
|
||||
Ok(Equals0{ a, c })
|
||||
externfn_impl!(Equals1, |this: &Self, x: ExprInst| {
|
||||
with_lit(&this.x, |l| Ok(Equals0{ a: l.clone(), x }))
|
||||
});
|
||||
|
||||
/// Fully applied Equals function.
|
||||
///
|
||||
/// Prev state: [Equals1]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct Equals0 { a: Literal, c: Clause }
|
||||
atomic_redirect!(Equals0, c);
|
||||
atomic_impl!(Equals0, |Self{ a, c }: &Self| {
|
||||
let b: Literal = c.clone().try_into()
|
||||
.map_err(|_| AssertionError::ext(c.clone(), "a literal value"))?;
|
||||
let eqls = match (a, b) {
|
||||
(Literal::Char(c1), Literal::Char(c2)) => *c1 == c2,
|
||||
(Literal::Num(n1), Literal::Num(n2)) => *n1 == n2,
|
||||
(Literal::Str(s1), Literal::Str(s2)) => *s1 == s2,
|
||||
(Literal::Uint(i1), Literal::Uint(i2)) => *i1 == i2,
|
||||
(_, _) => AssertionError::fail(c.clone(), "the expected type")?,
|
||||
};
|
||||
Ok(Clause::P(Primitive::Atom(Atom::new(Boolean::from(eqls)))))
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Equals0 { a: Literal, x: ExprInst }
|
||||
atomic_redirect!(Equals0, x);
|
||||
atomic_impl!(Equals0, |Self{ a, x }: &Self| {
|
||||
let eqls = with_lit(x, |l| Ok(match (a, l) {
|
||||
(Literal::Char(c1), Literal::Char(c2)) => c1 == c2,
|
||||
(Literal::Num(n1), Literal::Num(n2)) => n1 == n2,
|
||||
(Literal::Str(s1), Literal::Str(s2)) => s1 == s2,
|
||||
(Literal::Uint(i1), Literal::Uint(i2)) => i1 == i2,
|
||||
(_, _) => AssertionError::fail(x.clone(), "the expected type")?,
|
||||
}))?;
|
||||
Ok(Boolean::from(eqls).to_atom_cls())
|
||||
});
|
||||
|
||||
28
src/external/bool/ifthenelse.rs
vendored
28
src/external/bool/ifthenelse.rs
vendored
@@ -1,11 +1,9 @@
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::external::assertion_error::AssertionError;
|
||||
use crate::representations::PathSet;
|
||||
use crate::representations::{PathSet, interpreted::{Clause, ExprInst}};
|
||||
use crate::{atomic_impl, atomic_redirect, externfn_impl};
|
||||
use crate::representations::interpreted::Clause;
|
||||
|
||||
use super::Boolean;
|
||||
|
||||
@@ -15,29 +13,29 @@ use super::Boolean;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct IfThenElse1;
|
||||
externfn_impl!(IfThenElse1, |_: &Self, c: Clause| {Ok(IfThenElse0{c})});
|
||||
externfn_impl!(IfThenElse1, |_: &Self, x: ExprInst| {Ok(IfThenElse0{x})});
|
||||
|
||||
/// Partially applied IfThenElse function
|
||||
///
|
||||
/// Prev state: [IfThenElse1]; Next state: [IfThenElse0]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct IfThenElse0{ c: Clause }
|
||||
atomic_redirect!(IfThenElse0, c);
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IfThenElse0{ x: ExprInst }
|
||||
atomic_redirect!(IfThenElse0, x);
|
||||
atomic_impl!(IfThenElse0, |this: &Self| {
|
||||
let Boolean(b) = (&this.c).try_into()
|
||||
.map_err(|_| AssertionError::ext(this.c.clone(), "a boolean"))?;
|
||||
let Boolean(b) = this.x.clone().try_into()
|
||||
.map_err(|_| AssertionError::ext(this.x.clone(), "a boolean"))?;
|
||||
Ok(if b { Clause::Lambda {
|
||||
args: Some(PathSet { steps: Rc::new(vec![]), next: None }),
|
||||
body: Rc::new(Clause::Lambda {
|
||||
body: Clause::Lambda {
|
||||
args: None,
|
||||
body: Rc::new(Clause::LambdaArg)
|
||||
})
|
||||
body: Clause::LambdaArg.wrap()
|
||||
}.wrap()
|
||||
}} else { Clause::Lambda {
|
||||
args: None,
|
||||
body: Rc::new(Clause::Lambda {
|
||||
body: Clause::Lambda {
|
||||
args: Some(PathSet { steps: Rc::new(vec![]), next: None }),
|
||||
body: Rc::new(Clause::LambdaArg)
|
||||
})
|
||||
body: Clause::LambdaArg.wrap()
|
||||
}.wrap()
|
||||
}})
|
||||
});
|
||||
11
src/external/bool/mod.rs
vendored
11
src/external/bool/mod.rs
vendored
@@ -3,11 +3,12 @@ mod boolean;
|
||||
mod ifthenelse;
|
||||
pub use boolean::Boolean;
|
||||
|
||||
use crate::project::{Loader, extlib_loader};
|
||||
use crate::{pipeline::ConstTree, interner::Interner};
|
||||
|
||||
pub fn bool() -> impl Loader {
|
||||
extlib_loader(vec![
|
||||
("ifthenelse", Box::new(ifthenelse::IfThenElse1)),
|
||||
("equals", Box::new(equals::Equals2))
|
||||
|
||||
pub fn bool(i: &Interner) -> ConstTree {
|
||||
ConstTree::tree([
|
||||
(i.i("ifthenelse"), ConstTree::xfn(ifthenelse::IfThenElse1)),
|
||||
(i.i("equals"), ConstTree::xfn(equals::Equals2))
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user