diff --git a/src/term/net_to_term.rs b/src/term/net_to_term.rs index 98232d7ea..c958ded08 100644 --- a/src/term/net_to_term.rs +++ b/src/term/net_to_term.rs @@ -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 } @@ -472,3 +478,36 @@ impl std::fmt::Display for ReadbackError { } } } + +impl Term { + pub fn collect_unscoped(&self, unscoped: &mut HashSet, scope: &mut Vec) { + 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) { + 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); + } + } +} diff --git a/tests/snapshots/cli__debug_list_map.hvm.snap b/tests/snapshots/cli__debug_list_map.hvm.snap index 29518e9a1..df8165e31 100644 --- a/tests/snapshots/cli__debug_list_map.hvm.snap +++ b/tests/snapshots/cli__debug_list_map.hvm.snap @@ -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)))) --------------------------------------- diff --git a/tests/snapshots/linear_readback__church_mul.hvm.snap b/tests/snapshots/linear_readback__church_mul.hvm.snap index 28943046b..e6d241759 100644 --- a/tests/snapshots/linear_readback__church_mul.hvm.snap +++ b/tests/snapshots/linear_readback__church_mul.hvm.snap @@ -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 diff --git a/tests/snapshots/readback_lnet__complicated_dup.hvm.snap b/tests/snapshots/readback_lnet__complicated_dup.hvm.snap index 4164310bd..d878e9c70 100644 --- a/tests/snapshots/readback_lnet__complicated_dup.hvm.snap +++ b/tests/snapshots/readback_lnet__complicated_dup.hvm.snap @@ -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)))