Working example
This commit is contained in:
21
src/external/bool/boolean.rs
vendored
Normal file
21
src/external/bool/boolean.rs
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
use crate::{atomic_inert, representations::{interpreted::Clause, Primitive}, foreign::Atom};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Boolean(pub bool);
|
||||
atomic_inert!(Boolean);
|
||||
|
||||
impl From<bool> for Boolean { fn from(value: bool) -> Self { Self(value) } }
|
||||
|
||||
impl<'a> TryFrom<&'a Clause> for Boolean {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: &'a Clause) -> Result<Self, Self::Error> {
|
||||
if let Clause::P(Primitive::Atom(Atom(a))) = value {
|
||||
if let Some(b) = a.as_any().downcast_ref::<Boolean>() {
|
||||
return Ok(*b)
|
||||
}
|
||||
}
|
||||
return Err(())
|
||||
}
|
||||
}
|
||||
52
src/external/bool/equals.rs
vendored
Normal file
52
src/external/bool/equals.rs
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
|
||||
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;
|
||||
|
||||
/// Equals function
|
||||
///
|
||||
/// Next state: [Equals1]
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Equals2;
|
||||
externfn_impl!(Equals2, |_: &Self, c: Clause| {Ok(Equals1{c})});
|
||||
|
||||
/// Partially applied Equals function
|
||||
///
|
||||
/// Prev state: [Equals2]; Next state: [Equals0]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct Equals1{ c: Clause }
|
||||
atomic_redirect!(Equals1, c);
|
||||
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 })
|
||||
});
|
||||
|
||||
/// 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)))))
|
||||
});
|
||||
43
src/external/bool/ifthenelse.rs
vendored
Normal file
43
src/external/bool/ifthenelse.rs
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::external::assertion_error::AssertionError;
|
||||
use crate::representations::PathSet;
|
||||
use crate::{atomic_impl, atomic_redirect, externfn_impl};
|
||||
use crate::representations::interpreted::Clause;
|
||||
|
||||
use super::Boolean;
|
||||
|
||||
/// IfThenElse function
|
||||
///
|
||||
/// Next state: [IfThenElse0]
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct IfThenElse1;
|
||||
externfn_impl!(IfThenElse1, |_: &Self, c: Clause| {Ok(IfThenElse0{c})});
|
||||
|
||||
/// Partially applied IfThenElse function
|
||||
///
|
||||
/// Prev state: [IfThenElse1]; Next state: [IfThenElse0]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct IfThenElse0{ c: Clause }
|
||||
atomic_redirect!(IfThenElse0, c);
|
||||
atomic_impl!(IfThenElse0, |this: &Self| {
|
||||
let Boolean(b) = (&this.c).try_into()
|
||||
.map_err(|_| AssertionError::ext(this.c.clone(), "a boolean"))?;
|
||||
Ok(if b { Clause::Lambda {
|
||||
args: Some(PathSet { steps: Rc::new(vec![]), next: None }),
|
||||
body: Rc::new(Clause::Lambda {
|
||||
args: None,
|
||||
body: Rc::new(Clause::LambdaArg)
|
||||
})
|
||||
}} else { Clause::Lambda {
|
||||
args: None,
|
||||
body: Rc::new(Clause::Lambda {
|
||||
args: Some(PathSet { steps: Rc::new(vec![]), next: None }),
|
||||
body: Rc::new(Clause::LambdaArg)
|
||||
})
|
||||
}})
|
||||
});
|
||||
13
src/external/bool/mod.rs
vendored
Normal file
13
src/external/bool/mod.rs
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
mod equals;
|
||||
mod boolean;
|
||||
mod ifthenelse;
|
||||
pub use boolean::Boolean;
|
||||
|
||||
use crate::project::{Loader, fnlib_loader};
|
||||
|
||||
pub fn bool() -> impl Loader {
|
||||
fnlib_loader(vec![
|
||||
("ifthenelse", Box::new(ifthenelse::IfThenElse1)),
|
||||
("equals", Box::new(equals::Equals2))
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user