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-538] Remove simplify_main_ref and simplify_ref_to_ref passes #249

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 24 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 1 addition & 50 deletions docs/compiler-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
| `-Oall` | Disabled | Enables all compiler passes |
| `-Ono-all` | Disabled | Disables all compiler passes |
| `-Oeta` `-Ono-eta` | Disabled | [eta-reduction](#eta-reduction) |
| `-Oref-to-ref` `-Ono-ref-to-ref` | Disabled | [ref-to-ref](#ref-to-ref) |
| `-Oprune` `-Ono-prune` | Disabled | [definition-pruning](#definition-pruning) |
| `-Opre-reduce` `-Ono-pre-reduce` | Disabled | [pre-reduce](#pre-reduce) |
| `-Olinearize-matches` `-Olinearize-matches-extra` `-Ono-linearize-matches` | Extra | [linearize-matches](#linearize-matches) |
| `-Ofloat_combinators` `-Ono-float_combinators` | Enabled | [float-combinators](#float-combinators) |
| `-Osimplify-main` `-Ono-simplify-main` | Disabled | [simplify-main](#simplify-main) |
| `-Omerge` `-Ono-merge` | Disabled | [definition-merging](#definition-merging) |
| `-Oinline` `-Ono-inline` | Disabled | [inline](#inline) |
| `-e` `--entrypoint` | `Main \| main` | [entrypoint](#entrypoint) |
Expand All @@ -34,34 +32,6 @@ id_id = id
id_id = λz (id z)
```

## Ref-to-ref

If enabled, When a function that is simply directly calling another function, substitutes all occurrences of that function to the one being called.

Example:
```rs
// program
Foo = λ* 0

Bar = Foo

Main = (Bar *)

// -Oref-to-ref
Foo = λ* 0

Bar = Foo

Main = (Foo *) // Call `Bar` is resolved to a call to `Foo`.

// -Ono-ref-to-ref
(Foo) = λ* 0

(Bar) = Foo

(Main) = (Bar *)
```

## Definition-pruning

If enabled, removes all unused definitions.
Expand Down Expand Up @@ -137,7 +107,7 @@ main = (bar)

## linearize-matches

Linearizes the variables between match cases, transforming them into combinators when possible.
Linearizes the variables between match cases, transforming them into combinators when possible.
When the `linearize-matches` option is used, linearizes only vars that are used in more than one arm.

Example:
Expand Down Expand Up @@ -179,25 +149,6 @@ fold = λinit λf λxs (xs λh λt (fold (f init h) f t) init)
// Here we need to extract `λh λt (fold (f init h) f t)` to not expand `fold` infinitely, but it will not be extracted because of the free variable `init`.
```

## Simplify-main

If enabled, when directly calling another function, substitute the ref with a copy of its body.

Example:
```rs
// program
id = λx x
main = id

// -Osimplify-main
@id = (a a)
main = (a a)

// -Ono-simplify-main
@id = (a a)
@main = @id
```

# Inline

If enabled, inlines terms that compiles to 0 or 1 inet nodes at lambda level, before pre reduction.
Expand Down
11 changes: 11 additions & 0 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,14 @@ impl ToStringVerbose for String {
self.clone()
}
}

impl ToStringVerbose for hvmc::transform::TransformError {
fn to_string_verbose(&self, _verbose: bool) -> String {
match self {
hvmc::transform::TransformError::InfiniteRefCycle(..) => {
format!("During inlining:\n{:ERR_INDENT_SIZE$}{self}", "")
}
_ => unreachable!(),
}
}
}
24 changes: 7 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ pub fn compile_book(
if opts.eta {
core_book.values_mut().for_each(Net::eta_reduce);
}
if opts.inline {
diagnostics.start_pass();
if let Err(e) = core_book.inline() {
diagnostics.add_book_error(e);
}
diagnostics.fatal(())?;
}
if opts.prune {
prune_defs(&mut core_book, book.hvmc_entrypoint().to_string());
}
Expand Down Expand Up @@ -110,18 +117,9 @@ pub fn desugar_book(
if opts.float_combinators {
ctx.book.float_combinators();
}
if opts.ref_to_ref {
ctx.simplify_ref_to_ref()?;
}
if opts.simplify_main {
ctx.book.simplify_main_ref();
}

ctx.prune(opts.prune, opts.adt_encoding);

if opts.inline {
ctx.book.inline();
}
if opts.merge {
ctx.book.merge_definitions();
}
Expand Down Expand Up @@ -319,9 +317,6 @@ pub struct CompileOpts {
/// Enables [term::transform::eta_reduction].
pub eta: bool,

/// Enables [term::transform::simplify_ref_to_ref].
pub ref_to_ref: bool,

/// Enables [term::transform::definition_pruning] and [hvmc_net::prune].
pub prune: bool,

Expand All @@ -334,9 +329,6 @@ pub struct CompileOpts {
/// Enables [term::transform::float_combinators].
pub float_combinators: bool,

/// Enables [term::transform::simplify_main_ref].
pub simplify_main: bool,

/// Enables [term::transform::definition_merge]
pub merge: bool,

Expand All @@ -349,11 +341,9 @@ impl CompileOpts {
pub fn heavy() -> Self {
Self {
eta: true,
ref_to_ref: true,
prune: true,
pre_reduce: true,
float_combinators: true,
simplify_main: true,
merge: true,
inline: true,
adt_encoding: Default::default(),
Expand Down
8 changes: 0 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,13 @@ pub enum OptArgs {
NoEta,
Prune,
NoPrune,
RefToRef,
NoRefToRef,
PreReduce,
NoPreReduce,
LinearizeMatches,
LinearizeMatchesExtra,
NoLinearizeMatches,
FloatCombinators,
NoFloatCombinators,
SimplifyMain,
NoSimplifyMain,
Merge,
NoMerge,
Inline,
Expand All @@ -185,14 +181,10 @@ impl OptArgs {
NoEta => opts.eta = false,
Prune => opts.prune = true,
NoPrune => opts.prune = false,
RefToRef => opts.ref_to_ref = true,
NoRefToRef => opts.ref_to_ref = false,
PreReduce => opts.pre_reduce = true,
NoPreReduce => opts.pre_reduce = false,
FloatCombinators => opts.float_combinators = true,
NoFloatCombinators => opts.float_combinators = false,
SimplifyMain => opts.simplify_main = true,
NoSimplifyMain => opts.simplify_main = false,
Merge => opts.merge = true,
NoMerge => opts.merge = false,
Inline => opts.inline = true,
Expand Down
28 changes: 26 additions & 2 deletions src/term/transform/definition_merge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::simplify_ref_to_ref::subst_ref_to_ref;
use crate::term::{Book, Definition, Name, Rule, Term};
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
Expand Down Expand Up @@ -72,7 +71,7 @@ impl Book {
let mut updated_defs = Vec::new();

for def in self.defs.values_mut() {
if subst_ref_to_ref(&mut def.rule_mut().body, name_map) {
if Term::subst_ref_to_ref(&mut def.rule_mut().body, name_map) {
updated_defs.push(def.name.clone());
}
}
Expand All @@ -82,3 +81,28 @@ impl Book {
}
}
}

impl Term {
/// Performs reference substitution within a term replacing any references found in
/// `ref_map` with their corresponding targets.
pub fn subst_ref_to_ref(term: &mut Term, ref_map: &BTreeMap<Name, Name>) -> bool {
Term::recursive_call(move || match term {
Term::Ref { nam: def_name } => {
if let Some(target_name) = ref_map.get(def_name) {
*def_name = target_name.clone();
true
} else {
false
}
}

_ => {
let mut subst = false;
for child in term.children_mut() {
subst |= Term::subst_ref_to_ref(child, ref_map);
}
subst
}
})
}
}
68 changes: 0 additions & 68 deletions src/term/transform/inline.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/term/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ pub mod encode_match_terms;
pub mod fix_match_defs;
pub mod fix_match_terms;
pub mod float_combinators;
pub mod inline;
pub mod linearize_matches;
pub mod linearize_vars;
pub mod resolve_refs;
pub mod resugar_adts;
pub mod resugar_builtins;
pub mod simplify_main_ref;
pub mod simplify_ref_to_ref;
pub mod unique_names;
16 changes: 0 additions & 16 deletions src/term/transform/simplify_main_ref.rs

This file was deleted.

Loading
Loading