Skip to content

Commit

Permalink
Add test, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Fischman committed Oct 29, 2024
1 parent 5eb6dfc commit 7262571
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/sort/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impl Sort for BigIntSort {
fn register_primitives(self: Arc<Self>, eg: &mut TypeInfo) {
type Opt<T=()> = Option<T>;

add_primitives!(eg, "bigint" = |a: i64| -> Z { a.into() });

add_primitives!(eg, "+" = |a: Z, b: Z| -> Z { a + b });
add_primitives!(eg, "-" = |a: Z, b: Z| -> Z { a - b });
add_primitives!(eg, "*" = |a: Z, b: Z| -> Z { a * b });
Expand Down Expand Up @@ -58,7 +60,7 @@ impl Sort for BigIntSort {
add_primitives!(eg, "max" = |a: Z, b: Z| -> Z { a.max(b) });

add_primitives!(eg, "to-string" = |a: Z| -> Symbol { a.to_string().into() });
add_primitives!(eg, "from-string" = |a: Symbol| -> Opt<Z> { a.as_str().parse::<Z>().ok() })
add_primitives!(eg, "from-string" = |a: Symbol| -> Opt<Z> { a.as_str().parse::<Z>().ok() });
}

fn make_expr(&self, _egraph: &EGraph, value: Value) -> (Cost, Expr) {
Expand Down
22 changes: 14 additions & 8 deletions src/sort/bigrat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{ast::Literal, util::IndexSet};
use super::*;

lazy_static! {
static ref BIG_RAT_SORT_NAME: Symbol = "Rational".into();
static ref BIG_RAT_SORT_NAME: Symbol = "BigRat".into();
static ref RATS: Mutex<IndexSet<Q>> = Default::default();
}

Expand Down Expand Up @@ -41,7 +41,7 @@ impl Sort for BigRatSort {
add_primitives!(eg, "floor" = |a: Q| -> Q { a.floor() });
add_primitives!(eg, "ceil" = |a: Q| -> Q { a.ceil() });
add_primitives!(eg, "round" = |a: Q| -> Q { a.round() });
add_primitives!(eg, "rational" = |a: Z, b: Z| -> Q { Q::new(a, b) });
add_primitives!(eg, "bigrat" = |a: Z, b: Z| -> Q { Q::new(a, b) });
add_primitives!(eg, "numer" = |a: Q| -> Z { a.numer().clone() });
add_primitives!(eg, "denom" = |a: Q| -> Z { a.denom().clone() });

Expand Down Expand Up @@ -114,13 +114,19 @@ impl Sort for BigRatSort {
Expr::call_no_span(
"bigrat",
vec![
Expr::Lit(
DUMMY_SPAN.clone(),
Literal::String(numer.to_string().into()),
Expr::call_no_span(
"from-string",
vec![GenericExpr::Lit(
DUMMY_SPAN.clone(),
Literal::String(numer.to_string().into()),
)],
),
Expr::Lit(
DUMMY_SPAN.clone(),
Literal::String(denom.to_string().into()),
Expr::call_no_span(
"from-string",
vec![GenericExpr::Lit(
DUMMY_SPAN.clone(),
Literal::String(denom.to_string().into()),
)],
),
],
),
Expand Down
2 changes: 2 additions & 0 deletions src/typechecking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl Default for TypeInfo {
res.add_sort(I64Sort, DUMMY_SPAN.clone()).unwrap();
res.add_sort(F64Sort, DUMMY_SPAN.clone()).unwrap();
res.add_sort(RationalSort, DUMMY_SPAN.clone()).unwrap();
res.add_sort(BigIntSort, DUMMY_SPAN.clone()).unwrap();
res.add_sort(BigRatSort, DUMMY_SPAN.clone()).unwrap();

res.add_presort::<MapSort>(DUMMY_SPAN.clone()).unwrap();
res.add_presort::<SetSort>(DUMMY_SPAN.clone()).unwrap();
Expand Down
5 changes: 5 additions & 0 deletions tests/bignum.egg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

(let x (bigint -1234))
(let y (from-string "2"))
(let z (bigrat x y))
(check (= (to-string (numer z)) "-617"))

0 comments on commit 7262571

Please sign in to comment.