Skip to content

feat: make errors spanned in computations #1

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["parser", "computations", "repl"]
members = ["parser", "computations", "repl", "utils"]
4 changes: 3 additions & 1 deletion computations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
parser = { path = "../parser" }
parser = { path = "../parser" }
thiserror = "1.0.30"
utils = { path = "../utils" }
154 changes: 98 additions & 56 deletions computations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ mod trees;

use parser::Sym;
use trees::{ComputationTree, Literal, UnOp};
use utils::{Position, Positioned, PositionedExt};

use crate::trees::{BinOp, Combinator};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ComputationError {
#[error("Expected an operator")]
ExpectedOperator,
#[error("Expression is empty")]
Empty,
#[error("Expression could not fully be computed")]
SymbolsRemaining,
#[error("Unsupported feature: {0}")]
UnsupportedFeature(&'static str),
}

pub type Error = Positioned<ComputationError>;

// States for our state machine
// WaitForOp -> We are waiting for an operator as the next symbol
Expand All @@ -27,12 +43,10 @@ struct Accumulator {
impl Accumulator {
// Build a new accumulator containing only the symbol "s" viewed
// as a computation tree
fn new(s: &Sym) -> Result<Self, &'static str> {
Self::to_tree(s).and_then(|acc| {
Ok(Accumulator {
state: State::WaitForOp,
acc,
})
fn new(s: Positioned<&Sym>) -> Result<Self, Error> {
Self::to_tree(s).map(|acc| Accumulator {
state: State::WaitForOp,
acc,
})
}

Expand Down Expand Up @@ -86,8 +100,8 @@ impl Accumulator {
}

/// TODO : captures in lambdas ??
fn count_variables(prog: &Vec<Sym>) -> u32 {
prog.iter()
fn count_variables<'a, I: IntoIterator<Item = &'a Sym>>(prog: I) -> u32 {
prog.into_iter()
.map(|s| match s {
Sym::Var(_) => 1,
_ => 0,
Expand All @@ -96,91 +110,104 @@ impl Accumulator {
}

/// Convert an arbitrary symbol to a computation tree
fn to_tree<'a>(s: &Sym) -> Result<ComputationTree, &'static str> {
match s {
fn to_tree<'a>(s: Positioned<&Sym>) -> Result<ComputationTree, Error> {
match s.value {
Sym::CombS | Sym::CombK | Sym::CombD | Sym::CombI => {
Ok(ComputationTree::CombOp(Self::to_comb(s)))
Ok(ComputationTree::CombOp(Self::to_comb(s.value)))
}
Sym::Map | Sym::Eq | Sym::Add | Sym::And | Sym::Or | Sym::Filter | Sym::Reduce => {
Ok(ComputationTree::BinOpSym(Self::to_binary(s)))
Ok(ComputationTree::BinOpSym(Self::to_binary(s.value)))
}
Sym::Iota | Sym::Len | Sym::Neg => {
Ok(ComputationTree::UnOpSym(Self::to_unary(s.value)))
}
Sym::Iota | Sym::Len | Sym::Neg => Ok(ComputationTree::UnOpSym(Self::to_unary(s))),
Sym::Literal(n) => Ok(ComputationTree::Lit(n.clone().into())),
Sym::Var(v) => Ok(ComputationTree::Lit(Literal::Var(v.clone()))),
Sym::Lambda(prog) => non_linear_check(prog).and_then(|body| {
Ok(ComputationTree::Lambda {
vars: Self::count_variables(prog),
body: Box::new(body),
})
Sym::Lambda(prog) => non_linear_check(
prog.iter().map(|s| &s.value).positioned(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid such "on the fly" computation of default positions and provide another representation of programs that includes position information instead @SolarLiner

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean baking positions into ComputationTree? I don't think it is necessary to duplicate data structures here; otherwise yes, keeping positions around would be best.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. In that case let's just add a field for the positions in the ComputationTree

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the Spanned wrapper type that has all the goodies to make manipulating positions and values easier within it

prog.iter()
.map(|s| s.pos.clone())
.fold(Position::default(), Position::merge),
),
)
.map(|body| ComputationTree::Lambda {
vars: Self::count_variables(prog.iter().map(|s| &s.value)),
body: Box::new(body),
}),
}
}

/// Given an accumulator and a symbol, next computes a new accumulator
/// and consume the symbol to extend the current computation tree.
fn next(self, s: &Sym) -> Result<Accumulator, &'static str> {
fn next(self, s: Positioned<&Sym>) -> Result<Accumulator, Error> {
match self.state {
State::WaitForOp => match s {
State::WaitForOp => match s.value {
Sym::Map | Sym::Eq | Sym::Filter | Sym::Reduce | Sym::Add | Sym::And | Sym::Or => {
Ok(Accumulator {
state: State::WaitForVal(Self::to_binary(s)),
state: State::WaitForVal(Self::to_binary(s.value)),
..self
})
}
Sym::Iota | Sym::Len => Ok(Accumulator {
state: State::WaitForOp,
acc: ComputationTree::UnaryOp {
op: Self::to_unary(s),
op: Self::to_unary(s.value),
lhs: Box::new(self.acc),
},
}),
_ => Err("Expected operator"),
_ => Err(ComputationError::ExpectedOperator.positioned(s.pos)),
},
State::WaitForVal(op) => Self::to_tree(s).and_then(|tree| {
Ok(Accumulator {
state: State::WaitForOp,
acc: ComputationTree::BinaryOp {
op,
lhs: Box::new(tree),
rhs: Box::new(self.acc),
},
})
State::WaitForVal(op) => Self::to_tree(s).map(|tree| Accumulator {
state: State::WaitForOp,
acc: ComputationTree::BinaryOp {
op,
lhs: Box::new(tree),
rhs: Box::new(self.acc),
},
}),
}
}
}

// Check that a sequence of symbols is well formed
fn linear_check(prog: &[Sym]) -> Result<ComputationTree, &'static str> {
let first = Accumulator::new(&prog[0])?;
let next = &prog[1..];
next.iter()
.try_fold(first, |acc, s| acc.next(s))
.and_then(|a| {
// println!("debug {:?}", a);
a.finish().ok_or("Symbols remaining")
})
fn linear_check<'a, I: IntoIterator<Item = Positioned<&'a Sym>>>(
prog: Positioned<I>,
) -> Result<ComputationTree, Error> {
let mut it = prog.value.into_iter();
let acc = Accumulator::new(
it.next()
.ok_or(ComputationError::Empty.positioned(prog.pos.clone()))?,
)?;
let a = it.try_fold(acc, |acc, s| acc.next(s))?;
a.finish()
.ok_or(ComputationError::SymbolsRemaining.positioned(prog.pos))
}

// Check that a sequence of symbols is well formed (in the context of a lambda)
fn non_linear_check(_prog: &[Sym]) -> Result<ComputationTree, &'static str> {
Err("TODO: Lambdas not supported")
fn non_linear_check<'a, I: IntoIterator<Item = &'a Sym>>(
prog: Positioned<I>,
) -> Result<ComputationTree, Error> {
Err(ComputationError::UnsupportedFeature("non_linear_check").positioned(prog.pos))
}

/// Check that an ULP program is well formed and returns its associated
/// computation tree
pub fn check(mut prog: Vec<Sym>) -> Result<ComputationTree, &'static str> {
if prog.len() == 0 {
Err("No symbols")
pub fn check(prog: Vec<Positioned<Sym>>) -> Result<ComputationTree, Error> {
let pos = prog
.iter()
.map(|s| s.pos.clone())
.fold(Position::default(), Position::merge);
if prog.is_empty() {
Err(ComputationError::Empty.positioned(pos))
} else {
prog.reverse();
linear_check(&prog)
linear_check(prog.iter().map(|s| s.as_ref()).positioned(pos))
}
}

#[cfg(test)]
mod test {
use parser::*;
use utils::PositionedExt;

use crate::check;

Expand All @@ -194,7 +221,12 @@ mod test {
Sym::Iota,
Sym::Literal(Lit::Num("2".to_string())),
];
let res = check(prog);
let res = check(
prog.into_iter()
.enumerate()
.map(|(i, s)| s.spanned(i..i + 1))
.collect(),
);
println!("result: {:?}", res);
assert!(res.is_ok())
}
Expand All @@ -206,11 +238,16 @@ mod test {
Sym::Add,
Sym::Map,
Sym::Iota,
Sym::Literal(Lit::Num("2".to_string()))
Sym::Literal(Lit::Num("2".to_string())),
];
let err = check(prog);
println!("result: {:?}", err);
assert!(err.is_err())
let res = check(
prog.into_iter()
.enumerate()
.map(|(i, s)| s.spanned(i..i + 1))
.collect(),
);
println!("result: {:?}", res);
assert!(res.is_err())
}

#[test]
Expand All @@ -223,8 +260,13 @@ mod test {
Sym::Iota,
Sym::Literal(Lit::Num("2".to_string())),
];
let err = check(prog);
println!("result: {:?}", err);
assert!(err.is_ok())
let res = check(
prog.into_iter()
.enumerate()
.map(|(i, s)| s.spanned(i..i + 1))
.collect(),
);
println!("result: {:?}", res);
assert!(res.is_err())
}
}
4 changes: 4 additions & 0 deletions parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ edition = "2021"
[dependencies]
ariadne = "0.1.3"
chumsky = "0.5.0"
utils = { path = "../utils" }

[dev-dependencies]
insta = "1.8.0"
Loading