Skip to content

Commit

Permalink
Update float children lambda case and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
imaqtkatt committed Mar 28, 2024
1 parent ab2e081 commit c695bbc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
18 changes: 6 additions & 12 deletions src/term/transform/float_combinators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ impl Book {
/// Precondition: Variables must have been sanitized.
///
/// The floating algorithm follows these rules:
/// - Don't extract safe terms or terms that contains unscoped variables.
/// - Extract if it is a closed Application
/// - Extract if it is a supercombinator
/// - Float every element of a Superposition
/// - Recusively float every child term.

Check warning on line 17 in src/term/transform/float_combinators.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (Recusively)
/// - Extract if it is a combinator and not is a safe term.
pub fn float_combinators(&mut self) {
let mut combinators = Combinators::new();

Expand Down Expand Up @@ -71,8 +69,8 @@ fn float_combinator(
let comb_name = Name::new(format!("{}$C{}", def_name, *name_gen));
*name_gen += 1;

let comb_var = Term::Ref { nam: comb_name.clone() };
let extracted_term = std::mem::replace(term, comb_var);
let comb_ref = Term::Ref { nam: comb_name.clone() };
let extracted_term = std::mem::replace(term, comb_ref);

let rules = vec![Rule { body: extracted_term, pats: Vec::new() }];
let rule = Definition { name: comb_name.clone(), rules, builtin };
Expand Down Expand Up @@ -136,17 +134,13 @@ impl Term {
}

fn is_combinator(&self) -> bool {
self.is_closed()
}

pub fn is_closed(&self) -> bool {
self.free_vars().is_empty() && !self.has_unscoped_diff() && !matches!(self, Term::Ref { .. })
}
}

impl Term {
pub fn float_children_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut Term> {
multi_iterator!(FloatIter { Zero, One, Two, Vec, Mat, App });
multi_iterator!(FloatIter { Zero, Two, Vec, Mat, App });
match self {
Term::App { fun, arg, .. } => {
let mut args = vec![arg.as_mut()];
Expand All @@ -166,7 +160,7 @@ impl Term {
| Term::Use { val: fst, nxt: snd, .. }
| Term::Dup { val: fst, nxt: snd, .. }
| Term::Opx { fst, snd, .. } => FloatIter::Two([fst.as_mut(), snd.as_mut()]),
Term::Lam { bod, .. } | Term::Chn { bod, .. } => FloatIter::One([bod.as_mut()]),
Term::Lam { bod, .. } | Term::Chn { bod, .. } => bod.float_children_mut(),
Term::Var { .. }
| Term::Lnk { .. }
| Term::Num { .. }
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/compile_file_o_all__list_merge_sort.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ input_file: tests/golden_tests/compile_file_o_all/list_merge_sort.hvm

@Merge$C1 = {4 a {4 b (c ({4 @Merge$C0 {4 (* @Cons) (c (a (b d)))}} d))}}

@MergePair$C0 = (a {4 {4 a {4 @Nil b}} {4 * b}})
@MergePair$C0 = (* (a {4 {4 a {4 @Nil b}} {4 * b}}))

@MergePair$C1 = {4 a {4 {4 @MergePair$C2 {4 (* @Nil) (b c)}} ({15 d b} ({4 @Merge$C1 {4 (* (e e)) (d (a f))}} {4 {4 f {4 c g}} {4 * g}}))}}

@MergePair$C2 = {4 a {4 {4 @MergePair$C1 {4 (* @MergePair$C0) (b (a c))}} (b c)}}
@MergePair$C2 = {4 a {4 {4 @MergePair$C1 {4 @MergePair$C0 (b (a c))}} (b c)}}

@Nil = {4 * {4 a a}}

Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/desugar_file__combinators.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input_file: tests/golden_tests/desugar_file/combinators.hvm

(bar) = λa λb (a bar b)

(List.ignore) = λa λ* #List (a #List λ* List.ignore$C0 0)
(List.ignore) = λa λ* #List (a List.ignore$C0 0)

(baz) = {0 1 2 3 λa a foo}

Expand All @@ -30,6 +30,6 @@ input_file: tests/golden_tests/desugar_file/combinators.hvm

(B$C0) = let (c, d) = B; λe (c d e)

(List.ignore$C0) = #List λd (List.ignore d List.ignore)
(List.ignore$C0) = #List λ* #List λd (List.ignore d List.ignore)

(list$C0) = (List.cons list List.nil)
4 changes: 2 additions & 2 deletions tests/snapshots/mutual_recursion__len.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/mutual_recursion/len.hvm
---
@Len = ({2 {2 * @Len$C0} {2 #0 a}} a)
@Len = ({2 @Len$C0 {2 #0 a}} a)

@Len$C0 = {2 a b}
@Len$C0 = {2 * {2 a b}}
& #1 ~ <+ c b>
& @Len ~ (a c)

Expand Down

0 comments on commit c695bbc

Please sign in to comment.