forked from Orchid/orchid
Preparation for sharing
- rustfmt - clippy - comments - README
This commit is contained in:
@@ -3,13 +3,12 @@ use std::fmt::Debug;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::interner::{Token, InternedDisplay};
|
||||
use crate::utils::print_nname;
|
||||
|
||||
use super::Literal;
|
||||
use super::location::Location;
|
||||
use super::path_set::PathSet;
|
||||
use super::primitive::Primitive;
|
||||
use super::Literal;
|
||||
use crate::interner::{InternedDisplay, Sym};
|
||||
use crate::utils::sym2string;
|
||||
|
||||
// TODO: implement Debug, Eq and Hash with cycle detection
|
||||
|
||||
@@ -22,20 +21,24 @@ impl Debug for Expr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match &self.location {
|
||||
Location::Unknown => write!(f, "{:?}", self.clause),
|
||||
loc => write!(f, "{:?}@{}", self.clause, loc)
|
||||
loc => write!(f, "{:?}@{}", self.clause, loc),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl InternedDisplay for Expr {
|
||||
fn fmt_i(&self, f: &mut std::fmt::Formatter<'_>, i: &crate::interner::Interner) -> std::fmt::Result {
|
||||
fn fmt_i(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
i: &crate::interner::Interner,
|
||||
) -> std::fmt::Result {
|
||||
match &self.location {
|
||||
Location::Unknown => self.clause.fmt_i(f, i),
|
||||
loc => {
|
||||
write!(f, "{}:(", loc)?;
|
||||
self.clause.fmt_i(f, i)?;
|
||||
write!(f, ")")
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,19 +48,20 @@ impl InternedDisplay for Expr {
|
||||
#[derive(Clone)]
|
||||
pub struct ExprInst(pub Rc<RefCell<Expr>>);
|
||||
impl ExprInst {
|
||||
pub fn expr<'a>(&'a self) -> impl Deref<Target = Expr> + 'a {
|
||||
pub fn expr(&self) -> impl Deref<Target = Expr> + '_ {
|
||||
self.0.as_ref().borrow()
|
||||
}
|
||||
|
||||
pub fn expr_mut<'a>(&'a self) -> impl DerefMut<Target = Expr> + 'a {
|
||||
pub fn expr_mut(&self) -> impl DerefMut<Target = Expr> + '_ {
|
||||
self.0.as_ref().borrow_mut()
|
||||
}
|
||||
|
||||
/// Call a normalization function on the expression. The expr is
|
||||
/// updated with the new clause which affects all copies of it
|
||||
/// across the tree.
|
||||
pub fn try_normalize<T, E>(&self,
|
||||
mapper: impl FnOnce(&Clause) -> Result<(Clause, T), E>
|
||||
pub fn try_normalize<T, E>(
|
||||
&self,
|
||||
mapper: impl FnOnce(&Clause) -> Result<(Clause, T), E>,
|
||||
) -> Result<(Self, T), E> {
|
||||
let (new_clause, extra) = mapper(&self.expr().clause)?;
|
||||
self.expr_mut().clause = new_clause;
|
||||
@@ -67,15 +71,13 @@ impl ExprInst {
|
||||
/// Run a mutation function on the expression, producing a new,
|
||||
/// distinct expression. The new expression shares location info with
|
||||
/// the original but is normalized independently.
|
||||
pub fn try_update<T, E>(&self,
|
||||
mapper: impl FnOnce(&Clause) -> Result<(Clause, T), E>
|
||||
pub fn try_update<T, E>(
|
||||
&self,
|
||||
mapper: impl FnOnce(&Clause) -> Result<(Clause, T), E>,
|
||||
) -> Result<(Self, T), E> {
|
||||
let expr = self.expr();
|
||||
let (clause, extra) = mapper(&expr.clause)?;
|
||||
let new_expr = Expr{
|
||||
clause,
|
||||
location: expr.location.clone(),
|
||||
};
|
||||
let new_expr = Expr { clause, location: expr.location.clone() };
|
||||
Ok((Self(Rc::new(RefCell::new(new_expr))), extra))
|
||||
}
|
||||
|
||||
@@ -86,13 +88,16 @@ impl ExprInst {
|
||||
predicate(&self.expr().clause)
|
||||
}
|
||||
|
||||
pub fn with_literal<T>(&self,
|
||||
predicate: impl FnOnce(&Literal) -> T
|
||||
pub fn with_literal<T>(
|
||||
&self,
|
||||
predicate: impl FnOnce(&Literal) -> T,
|
||||
) -> Result<T, ()> {
|
||||
let expr = self.expr();
|
||||
if let Clause::P(Primitive::Literal(l)) = &expr.clause {
|
||||
Ok(predicate(l))
|
||||
} else {Err(())}
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,10 +111,14 @@ impl Debug for ExprInst {
|
||||
}
|
||||
|
||||
impl InternedDisplay for ExprInst {
|
||||
fn fmt_i(&self, f: &mut std::fmt::Formatter<'_>, i: &crate::interner::Interner) -> std::fmt::Result {
|
||||
fn fmt_i(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
i: &crate::interner::Interner,
|
||||
) -> std::fmt::Result {
|
||||
match self.0.try_borrow() {
|
||||
Ok(expr) => expr.fmt_i(f, i),
|
||||
Err(_) => write!(f, "<borrowed>")
|
||||
Err(_) => write!(f, "<borrowed>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,15 +126,9 @@ impl InternedDisplay for ExprInst {
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Clause {
|
||||
P(Primitive),
|
||||
Apply{
|
||||
f: ExprInst,
|
||||
x: ExprInst
|
||||
},
|
||||
Constant(Token<Vec<Token<String>>>),
|
||||
Lambda{
|
||||
args: Option<PathSet>,
|
||||
body: ExprInst
|
||||
},
|
||||
Apply { f: ExprInst, x: ExprInst },
|
||||
Constant(Sym),
|
||||
Lambda { args: Option<PathSet>, body: ExprInst },
|
||||
LambdaArg,
|
||||
}
|
||||
impl Clause {
|
||||
@@ -133,15 +136,19 @@ impl Clause {
|
||||
/// copied or moved clauses as it does not have debug information and
|
||||
/// does not share a normalization cache list with them.
|
||||
pub fn wrap(self) -> ExprInst {
|
||||
ExprInst(Rc::new(RefCell::new(Expr{
|
||||
ExprInst(Rc::new(RefCell::new(Expr {
|
||||
location: Location::Unknown,
|
||||
clause: self
|
||||
clause: self,
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
impl InternedDisplay for Clause {
|
||||
fn fmt_i(&self, f: &mut std::fmt::Formatter<'_>, i: &crate::interner::Interner) -> std::fmt::Result {
|
||||
fn fmt_i(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
i: &crate::interner::Interner,
|
||||
) -> std::fmt::Result {
|
||||
match self {
|
||||
Clause::P(p) => write!(f, "{p:?}"),
|
||||
Clause::LambdaArg => write!(f, "arg"),
|
||||
@@ -151,7 +158,7 @@ impl InternedDisplay for Clause {
|
||||
write!(f, " ")?;
|
||||
x.fmt_i(f, i)?;
|
||||
write!(f, ")")
|
||||
}
|
||||
},
|
||||
Clause::Lambda { args, body } => {
|
||||
write!(f, "\\")?;
|
||||
match args {
|
||||
@@ -161,7 +168,7 @@ impl InternedDisplay for Clause {
|
||||
write!(f, ".")?;
|
||||
body.fmt_i(f, i)
|
||||
},
|
||||
Clause::Constant(t) => write!(f, "{}", print_nname(*t, i))
|
||||
Clause::Constant(t) => write!(f, "{}", sym2string(*t, i)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,4 +177,4 @@ impl<T: Into<Literal>> From<T> for Clause {
|
||||
fn from(value: T) -> Self {
|
||||
Self::P(Primitive::Literal(value.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user