Skip to content
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

[sc-519] Add syntax sugar for Nats #241

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/term/builtins.hvm
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
data String = (String.cons head tail) | (String.nil)
data List = (List.cons head tail) | (List.nil)
data Result = (Result.ok val) | (Result.err val)
data Result = (Result.ok val) | (Result.err val)
data Nat = (Nat.succ pred) | (Nat.zero)
10 changes: 10 additions & 0 deletions src/term/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub const RESULT: &str = "Result";
pub const RESULT_OK: &str = "Result.ok";
pub const RESULT_ERR: &str = "Result.err";

pub const NAT: &str = "Nat";
pub const NAT_SUCC: &str = "Nat.succ";
pub const NAT_ZERO: &str = "Nat.zero";

impl Book {
pub fn builtins() -> Book {
parse_book(BUILTINS, Book::default, true).expect("Error parsing builtin file, this should not happen")
Expand All @@ -37,6 +41,7 @@ impl Term {
match self {
Term::Lst { els } => *self = Term::encode_list(std::mem::take(els)),
Term::Str { val } => *self = Term::encode_str(val),
Term::Nat { val } => *self = Term::encode_nat(*val),
Term::Let { pat, val, nxt } => {
pat.encode_builtins();
val.encode_builtins();
Expand Down Expand Up @@ -87,6 +92,11 @@ impl Term {
Term::call(Term::r#ref(SCONS), [Term::Num { val: u64::from(char) }, acc])
})
}

pub fn encode_nat(val: u64) -> Term {
(0 .. val).fold(Term::r#ref(NAT_ZERO), |acc, _| Term::app(Term::r#ref(NAT_SUCC), acc))
}

pub fn encode_ok(val: Term) -> Term {
Term::call(Term::r#ref(RESULT_OK), [val])
}
Expand Down
1 change: 1 addition & 0 deletions src/term/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl fmt::Display for Term {
}
Term::Era => write!(f, "*"),
Term::Num { val } => write!(f, "{val}"),
Term::Nat { val } => write!(f, "#{val}"),
Term::Str { val } => write!(f, "{val:?}"),
Term::Opx { op, fst, snd } => {
write!(f, "({} {} {})", op, fst, snd)
Expand Down
11 changes: 11 additions & 0 deletions src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ pub enum Term {
Num {
val: u64,
},
Nat {
val: u64,
},
Str {
val: GlobalString,
},
Expand Down Expand Up @@ -316,6 +319,7 @@ impl Clone for Term {
}
Self::Sup { tag, els } => Self::Sup { tag: tag.clone(), els: els.clone() },
Self::Num { val } => Self::Num { val: *val },
Self::Nat { val } => Self::Nat { val: *val },
Self::Str { val } => Self::Str { val: val.clone() },
Self::Lst { els } => Self::Lst { els: els.clone() },
Self::Opx { op, fst, snd } => Self::Opx { op: *op, fst: fst.clone(), snd: snd.clone() },
Expand Down Expand Up @@ -464,6 +468,7 @@ impl Term {
Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
| Term::Nat { .. }
| Term::Str { .. }
| Term::Ref { .. }
| Term::Era
Expand All @@ -487,6 +492,7 @@ impl Term {
Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
| Term::Nat { .. }
| Term::Str { .. }
| Term::Ref { .. }
| Term::Era
Expand Down Expand Up @@ -531,6 +537,7 @@ impl Term {
Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
| Term::Nat { .. }
| Term::Str { .. }
| Term::Ref { .. }
| Term::Era
Expand Down Expand Up @@ -568,6 +575,7 @@ impl Term {
Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
| Term::Nat { .. }
| Term::Str { .. }
| Term::Ref { .. }
| Term::Era
Expand Down Expand Up @@ -607,6 +615,7 @@ impl Term {
Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
| Term::Nat { .. }
| Term::Str { .. }
| Term::Ref { .. }
| Term::Era
Expand All @@ -631,6 +640,7 @@ impl Term {
| Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
| Term::Nat { .. }
| Term::Str { .. }
| Term::Ref { .. }
| Term::Era
Expand All @@ -655,6 +665,7 @@ impl Term {
| Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
| Term::Nat { .. }
| Term::Str { .. }
| Term::Ref { .. }
| Term::Era
Expand Down
4 changes: 2 additions & 2 deletions src/term/net_to_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl Term {
}
n
}
Term::Lst { .. } => unreachable!(),
Term::Nat { .. } | Term::Lst { .. } => unreachable!(),
Term::Lnk { .. } | Term::Num { .. } | Term::Str { .. } | Term::Ref { .. } | Term::Era | Term::Err => 0,
};

Expand Down Expand Up @@ -419,7 +419,7 @@ impl Term {
rule.body.fix_names(id_counter, book);
}
}
Term::Let { .. } | Term::Use { .. } | Term::Lst { .. } => unreachable!(),
Term::Let { .. } | Term::Use { .. } | Term::Nat { .. } | Term::Lst { .. } => unreachable!(),
Term::Var { .. } | Term::Lnk { .. } | Term::Num { .. } | Term::Str { .. } | Term::Era | Term::Err => {}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/term/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ where
}),
);

let nat = just(Token::Hash).ignore_then(select!(Token::Num(num) => Term::Nat { val: num }).or(
select!(Token::Error(LexingError::InvalidNumberLiteral) => ()).validate(|_, span, emit| {
emit.emit(Rich::custom(span, "found invalid nat literal expected number"));
Term::Nat { val: 0 }
}),
));

let term_sep = just(Token::Semicolon).or_not();
let list_sep = just(Token::Comma).or_not();

Expand Down Expand Up @@ -359,7 +366,7 @@ where
// OBS: `num_op` has to be before app, idk why?
// OBS: `app` has to be before `tup` to not overflow on huge app terms
// TODO: What happens on huge `tup` and other terms?
num_op, app, tup, global_var, var, number, list, str, chr, sup, global_lam, lam, dup, let_, use_,
num_op, app, tup, global_var, var, number, nat, list, str, chr, sup, global_lam, lam, dup, let_, use_,
match_, era,
))
})
Expand Down
5 changes: 3 additions & 2 deletions src/term/term_to_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ impl EncodeTermState<'_> {
self.inet.link(Port(node, 1), Port(node, 2));
Some(Port(node, 0))
}
Term::Str { .. } => unreachable!(), // Removed in desugar str
Term::Lst { .. } => unreachable!(), // Removed in desugar list
Term::Nat { .. } => unreachable!(), // Removed in encode_nat
Term::Str { .. } => unreachable!(), // Removed in encode_str
Term::Lst { .. } => unreachable!(), // Removed in encode_list
// core: & fst ~ <op snd ret>
Term::Opx { op, fst, snd } => {
let opx = self.inet.new_node(Op2 { opr: op.to_hvmc_label() });
Expand Down
4 changes: 2 additions & 2 deletions src/term/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ impl Term {
},

Term::Chn { .. } | Term::Lnk { .. } => false,
Term::Str { .. } | Term::Lst { .. } => false,
Term::Let { .. } => false,
Term::Use { .. } => unreachable!(),
Term::App { .. } => false,
Term::Dup { .. } => false,
Term::Opx { .. } => false,
Term::Mat { .. } => false,
Term::Ref { .. } => false,

Term::Nat { .. } | Term::Str { .. } | Term::Lst { .. } | Term::Use { .. } => unreachable!(),

Term::Err => unreachable!(),
})
}
Expand Down
37 changes: 32 additions & 5 deletions src/term/transform/resugar_builtins.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
use crate::term::{Term, LCONS, LNIL, SCONS, SNIL};
use crate::term::{Term, LCONS, LNIL, NAT_SUCC, NAT_ZERO, SCONS, SNIL};

impl Term {
pub fn resugar_builtins(&mut self) {
self.resugar_strings();
self.resugar_nats();

Check warning on line 6 in src/term/transform/resugar_builtins.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (nats)
self.resugar_lists();
}

pub fn resugar_nats(&mut self) {

Check warning on line 10 in src/term/transform/resugar_builtins.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (nats)
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
Term::recursive_call(move || match self {
// (Nat.succ pred)
Term::App { fun: box Term::Ref { nam: ctr }, arg: box pred, .. } => {
pred.resugar_nats();

Check warning on line 14 in src/term/transform/resugar_builtins.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (nats)

if ctr == NAT_SUCC {
if let Term::Nat { val } = pred {
*self = Term::Nat { val: *val + 1 };
} else {
let n = std::mem::take(pred);
*self = Term::call(Term::Ref { nam: ctr.clone() }, [n]);
}
}
}
// (Nat.zero)
Term::Ref { nam: def_name } if def_name == NAT_ZERO => *self = Term::Nat { val: 0 },

_ => {
for child in self.children_mut() {
child.resugar_nats();

Check warning on line 30 in src/term/transform/resugar_builtins.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (nats)
}
}
});
}

/// Rebuilds the String syntax sugar, converting `(Cons 97 Nil)` into `"a"`.
pub fn resugar_strings(&mut self) {
match self {
Term::recursive_call(move || match self {
// (String.cons Num tail)
Term::App {
fun: box Term::App { fun: box Term::Ref { nam: ctr }, arg: box head, .. },
Expand Down Expand Up @@ -48,12 +75,12 @@
child.resugar_strings();
}
}
}
});
}

/// Rebuilds the List syntax sugar, converting `(Cons head Nil)` into `[head]`.
pub fn resugar_lists(&mut self) {
match self {
Term::recursive_call(move || match self {
// (List.cons el tail)
Term::App {
fun: box Term::App { fun: box Term::Ref { nam: ctr }, arg: box head, .. },
Expand Down Expand Up @@ -84,6 +111,6 @@
child.resugar_lists();
}
}
}
});
}
}
2 changes: 1 addition & 1 deletion tests/golden_tests/cli/debug_u60_to_nat.hvm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
data Nat = (Z) | (S nat)
data Nat_ = (Z) | (S nat)

(U60ToNat 0) = Z
(U60ToNat 1+p) = (S (U60ToNat p))
Expand Down
4 changes: 4 additions & 0 deletions tests/golden_tests/run_file/nat_add.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(Nat.add (Nat.zero) x) = x
(Nat.add (Nat.succ p) x) = (Nat.succ (Nat.add p x))
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved

(Main) = (Nat.add #25 #9)
2 changes: 1 addition & 1 deletion tests/golden_tests/simplify_matches/already_flat.hvm
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Rule5 (CtrA3 a) (CtrB3 b) = (a b)
Rule5 a b = (a b)

Rule6 a = a
Rule6 b = b
Rule6 b = b
6 changes: 3 additions & 3 deletions tests/snapshots/cli__debug_u60_to_nat.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ input_file: tests/golden_tests/cli/debug_u60_to_nat.hvm
---------------------------------------
(S (S (S (S Z))))
---------------------------------------
(S (S (S #Nat λ* #Nat λa #Nat (a Z))))
(S (S (S #Nat_ λ* #Nat_ λa #Nat_ (a Z))))
---------------------------------------
(S (S #Nat λ* #Nat λa #Nat (a #Nat λ* #Nat λb #Nat (b Z))))
(S (S #Nat_ λ* #Nat_ λa #Nat_ (a #Nat_ λ* #Nat_ λb #Nat_ (b Z))))
---------------------------------------
(S #Nat λ* #Nat λa #Nat (a #Nat λ* #Nat λb #Nat (b #Nat λ* #Nat λc #Nat (c Z))))
(S #Nat_ λ* #Nat_ λa #Nat_ (a #Nat_ λ* #Nat_ λb #Nat_ (b #Nat_ λ* #Nat_ λc #Nat_ (c Z))))
---------------------------------------
(S (S (S (S Z))))
6 changes: 3 additions & 3 deletions tests/snapshots/compile_file_o_all__list_merge_sort.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ input_file: tests/golden_tests/compile_file_o_all/list_merge_sort.hvm
@If$C0 = (a (* a))
@If$C1 = (* (a a))
@Map = ({4 @Map$C0 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 a}} a)
@Map$C0 = {4 a {4 {4 @Map$C0 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 (b c)}} ({5 (a d) b} {4 {4 d {4 c e}} {4 * e}})}}
@Map$C0 = {4 a {4 {4 @Map$C0 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 (b c)}} ({7 (a d) b} {4 {4 d {4 c e}} {4 * e}})}}
@Merge$C0 = {4 {9 a {9 b c}} {4 {11 d {4 @Merge$C0 {4 @Merge$C1 (e (f (g h)))}}} ({13 (i (a {2 @If$C0 {2 @If$C1 ({4 {4 j {4 k l}} {4 * l}} ({4 {4 c {4 h m}} {4 * m}} n))}})) {13 o e}} ({15 i {15 j f}} ({17 {4 @Merge$C2 {4 @Merge$C3 (o ({4 {4 b {4 d p}} {4 * p}} k))}} g} n)))}}
@Merge$C1 = (* @Cons)
@Merge$C2 = {4 a {4 b (c ({4 @Merge$C0 {4 @Merge$C1 (c (a (b d)))}} d))}}
@Merge$C3 = (* (a a))
@MergePair$C0 = {4 a {4 {4 @MergePair$C3 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 (b c)}} ({7 d b} ({4 @Merge$C2 {4 @Merge$C3 (d (a e))}} {4 {4 e {4 c f}} {4 * f}}))}}
@MergePair$C0 = {4 a {4 {4 @MergePair$C3 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 (b c)}} ({5 d b} ({4 @Merge$C2 {4 @Merge$C3 (d (a e))}} {4 {4 e {4 c f}} {4 * f}}))}}
@MergePair$C1 = (a {4 {4 a {4 @Nil b}} {4 * b}})
@MergePair$C2 = (* @MergePair$C1)
@MergePair$C3 = {4 a {4 {4 @MergePair$C0 {4 @MergePair$C2 (b (a c))}} (b c)}}
@Nil = {4 * {4 a a}}
@Pure = (a {4 {4 a {4 @Nil b}} {4 * b}})
@Unpack = (a ({4 @Unpack$C2 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 (a b)}} b))
@Unpack$C0 = {4 a {4 {4 @MergePair$C3 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 (b {4 @Unpack$C0 {4 @Unpack$C1 (c (d e))}})}} ({3 c {7 f b}} ({4 @Merge$C2 {4 @Merge$C3 (f (a d))}} e))}}
@Unpack$C0 = {4 a {4 {4 @MergePair$C3 {4 @Unpack$C3_$_MergePair$C4_$_Map$C1 (b {4 @Unpack$C0 {4 @Unpack$C1 (c (d e))}})}} ({3 c {5 f b}} ({4 @Merge$C2 {4 @Merge$C3 (f (a d))}} e))}}
@Unpack$C1 = (* (a a))
@Unpack$C2 = {4 a {4 {4 @Unpack$C0 {4 @Unpack$C1 (b (a c))}} (b c)}}
@Unpack$C3_$_MergePair$C4_$_Map$C1 = (* @Nil)
Expand Down
16 changes: 8 additions & 8 deletions tests/snapshots/encode_pattern_match__common.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ TaggedScott:

(Yellow) = #Light λ* #Light λb #Light λ* b

(Red) = #Light λa #Light λ* #Light λ* a
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved

(False) = #Bool λ* #Bool λb b

(Filled) = λa #Box λb #Box λ* #Box (b a)

(Empty) = #Box λ* #Box λb b
Expand All @@ -33,10 +37,6 @@ TaggedScott:

(True) = #Bool λa #Bool λ* a

(False) = #Bool λ* #Bool λb b

(Red) = #Light λa #Light λ* #Light λ* a

Scott:
(West) = λ* λ* λ* λd d

Expand All @@ -50,6 +50,10 @@ Scott:

(Yellow) = λ* λb λ* b

(Red) = λa λ* λ* a

(False) = λ* λb b

(Filled) = λa λb λ* (b a)

(Empty) = λ* λb b
Expand All @@ -67,7 +71,3 @@ Scott:
(Nil) = λ* λb b

(True) = λa λ* a

(False) = λ* λb b

(Red) = λa λ* λ* a
16 changes: 8 additions & 8 deletions tests/snapshots/encode_pattern_match__expr.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ TaggedScott:

(Tup) = λa λb #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λj #Expr λ* #Expr (j a b)

(Dup) = λa λb λc λd #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λk #Expr λ* #Expr λ* #Expr (k a b c d)

(Let) = λa λb λc #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λi #Expr λ* #Expr λ* #Expr λ* #Expr (i a b c)

(Var) = λa #Expr λb #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr (b a)

(Num) = λa #Expr λ* #Expr λc #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr (c a)
Expand All @@ -25,10 +29,6 @@ TaggedScott:

(If) = λa λb λc #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λh #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr (h a b c)

(Let) = λa λb λc #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λi #Expr λ* #Expr λ* #Expr λ* #Expr (i a b c)

(Dup) = λa λb λc λd #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λ* #Expr λk #Expr λ* #Expr λ* #Expr (k a b c d)

Scott:
(Div) = λ* λ* λ* λd d

Expand All @@ -42,6 +42,10 @@ Scott:

(Tup) = λa λb λ* λ* λ* λ* λ* λ* λ* λj λ* (j a b)

(Dup) = λa λb λc λd λ* λ* λ* λ* λ* λ* λk λ* λ* (k a b c d)

(Let) = λa λb λc λ* λ* λ* λ* λ* λi λ* λ* λ* (i a b c)

(Var) = λa λb λ* λ* λ* λ* λ* λ* λ* λ* (b a)

(Num) = λa λ* λc λ* λ* λ* λ* λ* λ* λ* (c a)
Expand All @@ -51,7 +55,3 @@ Scott:
(Fun) = λa λb λ* λ* λ* λf λ* λ* λ* λ* λ* (f a b)

(If) = λa λb λc λ* λ* λ* λ* λh λ* λ* λ* λ* (h a b c)

(Let) = λa λb λc λ* λ* λ* λ* λ* λi λ* λ* λ* (i a b c)

(Dup) = λa λb λc λd λ* λ* λ* λ* λ* λ* λk λ* λ* (k a b c d)
Loading
Loading