Skip to content

Commit

Permalink
Merge pull request #269 from HigherOrderCO/feature/sc-251/mark-scopel…
Browse files Browse the repository at this point in the history
…ess-lambdas-on-readback-in-hvm

[sc-251] Mark scopeless lambdas on readback in hvm-lang
  • Loading branch information
imaqtkatt authored Apr 16, 2024
2 parents 352d318 + 5124e82 commit f46f28e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 38 deletions.
39 changes: 39 additions & 0 deletions src/term/net_to_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ pub fn net_to_term(
}

reader.report_errors(diagnostics);

let mut unscoped = HashSet::new();
let mut scope = Vec::new();
term.collect_unscoped(&mut unscoped, &mut scope);
term.apply_unscoped(&unscoped);

term
}

Expand Down Expand Up @@ -472,3 +478,36 @@ impl std::fmt::Display for ReadbackError {
}
}
}

impl Term {
pub fn collect_unscoped(&self, unscoped: &mut HashSet<Name>, scope: &mut Vec<Name>) {
match self {
Term::Var { nam } if !scope.contains(nam) => _ = unscoped.insert(nam.clone()),
_ => {}
}
for (child, binds) in self.children_with_binds() {
let binds: Vec<_> = binds.collect();
for bind in binds.iter().copied().flatten() {
scope.push(bind.clone());
}
child.collect_unscoped(unscoped, scope);
for _bind in binds.into_iter().flatten() {
scope.pop();
}
}
}

pub fn apply_unscoped(&mut self, unscoped: &HashSet<Name>) {
match self {
Term::Var { nam } if unscoped.contains(nam) => *self = Term::Lnk { nam: std::mem::take(nam) },
Term::Lam { tag, nam: Some(nam), bod } if unscoped.contains(nam) => {
*self =
Term::Chn { tag: std::mem::take(tag), nam: Some(std::mem::take(nam)), bod: std::mem::take(bod) };
}
_ => {}
}
for child in self.children_mut() {
child.apply_unscoped(unscoped);
}
}
}
72 changes: 36 additions & 36 deletions tests/snapshots/cli__debug_list_map.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,77 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/cli/debug_list_map.hvm
---
#List λa #List λ* #List (a b c)
#List λa #List λ* #List (a $b $c)
---------------------------------------
#List λa #List λ* #List (a b c)
#List λa #List λ* #List (a $b $c)
---------------------------------------
#List λa #List λ* #List (a b c)
#List λa #List λ* #List (a $b $c)
---------------------------------------
#List λa #List λ* #List (a b c)
#List λa #List λ* #List (a $b $c)
---------------------------------------
#List λa #List λ* #List (a b c)
#List λa #List λ* #List (a $b $c)
---------------------------------------
#List λa #List λ* #List (a b c)
#List λa #List λ* #List (a $b $c)
---------------------------------------
#List λa #List λ* #List (a b c)
#List λa #List λ* #List (a $b $c)
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d e))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d $e))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d e))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d $e))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d e))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d $e))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d e))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d $e))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d e))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d $e))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d e))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d $e))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d e))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d $e))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f g)))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f $g)))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f g)))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f $g)))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f g)))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f $g)))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f g)))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f $g)))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f g)))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f $g)))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f g)))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f $g)))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f g)))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f $g)))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h $i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h $i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h $i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h $i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h $i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h $i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h $i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h #List λ* #List λi i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h #List λ* #List λi i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h #List λ* #List λi i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h #List λ* #List λi i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h #List λ* #List λi i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h #List λ* #List λi i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h #List λ* #List λi i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h #List λ* #List λi i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g h #List λ* #List λi i))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g $h #List λ* #List λi i))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e f #List λg #List λ* #List (g 91 #List λ* #List λh h))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e $f #List λg #List λ* #List (g 91 #List λ* #List λh h))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c d #List λe #List λ* #List (e 256 #List λf #List λ* #List (f 91 #List λ* #List λg g))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c $d #List λe #List λ* #List (e 256 #List λf #List λ* #List (f 91 #List λ* #List λg g))))
---------------------------------------
#List λa #List λ* #List (a b #List λc #List λ* #List (c 42 #List λd #List λ* #List (d 256 #List λe #List λ* #List (e 91 #List λ* #List λf f))))
#List λa #List λ* #List (a $b #List λc #List λ* #List (c 42 #List λd #List λ* #List (d 256 #List λe #List λ* #List (e 91 #List λ* #List λf f))))
---------------------------------------
#List λa #List λ* #List (a 2 #List λb #List λ* #List (b 42 #List λc #List λ* #List (c 256 #List λd #List λ* #List (d 91 #List λ* #List λe e))))
---------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/linear_readback__church_mul.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/linear_readback/church_mul.hvm
---
λa λb let #0{c g} = (let #1{d h} = a; d let #1{e f} = h; (e (f #0{g b}))); c
λa λb let #0{c g} = (let #1{d h} = a; d let #1{e f} = $h; (e (f #0{$g b}))); c
2 changes: 1 addition & 1 deletion tests/snapshots/readback_lnet__complicated_dup.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/readback_lnet/complicated_dup.hvm
---
λa (λb (a (b c)) λc (a (b c)))
λa (λ$b (a ($b $c)) λ$c (a ($b $c)))

0 comments on commit f46f28e

Please sign in to comment.