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 2 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
22 changes: 4 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(let_chains)]

use builtins::create_host;
use diagnostics::{DiagnosticOrigin, Diagnostics, DiagnosticsConfig, Severity};
use diagnostics::{DiagnosticOrigin, Diagnostics, DiagnosticsConfig, Severity, ERR_INDENT_SIZE};
use hvmc::{
ast::Net,
dispatch_dyn_net,
Expand Down Expand Up @@ -49,6 +49,9 @@ pub fn compile_book(
if opts.eta {
core_book.values_mut().for_each(Net::eta_reduce);
}
if opts.inline {
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
core_book.inline().map_err(|e| format!("Inline:\n{:ERR_INDENT_SIZE$}{e}", ""))?;
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
}
if opts.pre_reduce {
core_book.pre_reduce(&|x| x == book.hvmc_entrypoint(), None, 100_000);
}
Expand Down Expand Up @@ -110,18 +113,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 +313,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 +325,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 +337,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: 27 additions & 1 deletion src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use crate::{
use indexmap::{IndexMap, IndexSet};
use interner::global::GlobalString;
use itertools::Itertools;
use std::{borrow::Cow, collections::HashMap, ops::Deref};
use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
ops::Deref,
};

pub mod builtins;
pub mod check;
Expand Down Expand Up @@ -701,6 +705,28 @@ impl Term {
})
}

/// Returns whether any substitution happened within the term or not
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
pub fn subst_ref_to_ref(term: &mut Term, ref_map: &BTreeMap<Name, Name>) -> bool {
imaqtkatt marked this conversation as resolved.
Show resolved Hide resolved
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
}
})
}

/// Collects all the free variables that a term has
/// and the number of times each var is used
pub fn free_vars(&self) -> HashMap<Name, u64> {
Expand Down
3 changes: 1 addition & 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 Down
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