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-251] Mark scopeless lambdas on readback in hvm-lang #269

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
32 changes: 31 additions & 1 deletion src/term/net_to_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub fn net_to_term(
namegen: Default::default(),
seen: Default::default(),
errors: Default::default(),
vars: Default::default(),
unscoped_vars: Default::default(),
};

let mut term = reader.read_term(net.enter_port(ROOT));
Expand All @@ -47,6 +49,19 @@ pub fn net_to_term(
debug_assert_eq!(result, None);
}

fn switch_lam_to_chn(term: &mut Term, unscoped_vars: &HashSet<Name>) {
if let Term::Lam { tag, nam: Some(nam), bod } = term {
if unscoped_vars.contains(nam) {
*term = Term::Chn { tag: std::mem::take(tag), nam: Some(nam.clone()), bod: std::mem::take(bod) }
}
}
for child in term.children_mut() {
switch_lam_to_chn(child, unscoped_vars);
}
}

switch_lam_to_chn(&mut term, &reader.unscoped_vars);

reader.report_errors(diagnostics);
term
}
Expand All @@ -65,6 +80,8 @@ pub struct Reader<'a> {
seen_fans: Scope,
seen: HashSet<Port>,
errors: Vec<ReadbackError>,
vars: HashSet<Name>,
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
unscoped_vars: HashSet<Name>,
}

impl Reader<'_> {
Expand All @@ -88,11 +105,24 @@ impl Reader<'_> {
// If we're visiting a port 0, then it is a lambda.
0 => {
let nam = self.namegen.decl_name(self.net, Port(node, 1));
if let Some(nam) = &nam {
if !self.unscoped_vars.contains(nam) {
self.vars.insert(nam.clone());
}
}
let bod = self.read_term(self.net.enter_port(Port(node, 2)));
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
Term::Lam { tag: self.labels.con.to_tag(*lab), nam, bod: Box::new(bod) }
}
// If we're visiting a port 1, then it is a variable.
1 => Term::Var { nam: self.namegen.var_name(next) },
1 => {
let nam = self.namegen.var_name(next);
if self.vars.contains(&nam) {
Term::Var { nam }
} else {
self.unscoped_vars.insert(nam.clone());
Term::Lnk { nam }
}
}
// If we're visiting a port 2, then it is an application.
2 => {
let fun = self.read_term(self.net.enter_port(Port(node, 0)));
Expand Down
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/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)))
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
Loading