Make supercombinator pass not extract from main

This commit is contained in:
imaqtkatt 2024-01-18 07:32:16 -03:00
parent a90a38d0fe
commit 1909eb26a6
11 changed files with 24 additions and 20 deletions

View File

@ -17,7 +17,7 @@ pub fn pre_reduce_book(book: &mut BTreeMap<String, hvmc::ast::Net>, cross_refs:
let mut rt_book = hvmc::ast::book_to_runtime(book);
for (nam, net) in book.iter() {
// Skip unnecessary work
if net.rdex.is_empty() {
if net.rdex.is_empty() || nam == DefNames::ENTRY_POINT {
continue;
}
let heap = Heap::init(1 << 18);
@ -26,8 +26,7 @@ pub fn pre_reduce_book(book: &mut BTreeMap<String, hvmc::ast::Net>, cross_refs:
rt.boot(fid);
rt.expand(&rt_book);
let fully_reduce = cross_refs && nam != DefNames::ENTRY_POINT;
let iters = if fully_reduce {
let iters = if cross_refs {
let mut iters = 0;
// TODO: If I just call `rt.normal` some terms expand infinitely, so I put this workaround.
// But I don't think this is the right way (even if it works).

View File

@ -50,7 +50,7 @@ pub fn desugar_book(book: &mut Book, opt_level: OptimizationLevel) -> Result<Def
if opt_level >= OptimizationLevel::Heavy {
book.eta_reduction();
}
book.detach_supercombinators();
book.detach_supercombinators(main);
if opt_level >= OptimizationLevel::Heavy {
book.simplify_ref_to_ref()?;
}

View File

@ -5,11 +5,14 @@ use crate::term::{Book, DefId, DefNames, Definition, Name, Pattern, Rule, Term};
/// Replaces closed Terms (i.e. without free variables) with a Ref to the extracted term
/// Precondition: Vars must have been sanitized
impl Book {
pub fn detach_supercombinators(&mut self) {
pub fn detach_supercombinators(&mut self, main: DefId) {
let mut combinators = Combinators::new();
for def in self.defs.values_mut() {
def.assert_no_pattern_matching_rules();
if def.def_id == main {
continue;
}
def.rules[0].body.detach_combinators(def.def_id, &mut self.def_names, &mut combinators);
}

View File

@ -2,7 +2,7 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/addition.hvm
---
@2 = (<2+ a> a)
@main = a
& @2 ~ (#8 a)
& (b c) ~ (#8 a)
& #1 ~ <+ #1 <+ b c>>

View File

@ -2,8 +2,6 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/exp.hvm
---
@2 = ({3 (a b) (b c)} (a c))
@3 = ({5 (a b) (b c)} (a c))
@main = a
& @2 ~ (@3 a)
& ({3 (b c) (c d)} (b d)) ~ (({5 (e f) (f g)} (e g)) a)

View File

@ -2,5 +2,6 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/spacing.hvm
---
@main = ({3 a (a b)} b)
@main = ({3 a b} c)
& (d d) ~ (b (a c))

View File

@ -2,5 +2,6 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/spacing2.hvm
---
@main = (a a)
@main = a
& (b b) ~ ((c c) a)

View File

@ -2,5 +2,8 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/tagged_dup.hvm
---
@main = (a a)
@main = a
& (b b) ~ {3 (c (d (e (f (g a))))) c}
& (h h) ~ {3 f g}
& (i i) ~ {3 d e}

View File

@ -2,9 +2,6 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/tagged_lam.hvm
---
@2 = {2 <+ #1 a> a}
@3 = {2 {3 a <* a b>} b}
@main = [a b]
& @2 ~ {2 #2 a}
& @3 ~ {2 #2 b}
@main = a
& [{2 <+ #1 b> b} {2 {3 c <* c d>} d}] ~ {2 #2 a}

View File

@ -8,5 +8,6 @@ input_file: tests/golden_tests/compile_file/weekday.hvm
@M = {2 * @L}
@N = {2 * @M}
@Saturday = {2 * @N}
@main = @Saturday
@main = a
& (b b) ~ (@Saturday a)

View File

@ -2,5 +2,6 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file_o0/addition_const.hvm
---
@main = #3
@main = a
& #1 ~ <+ #2 a>