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

#470 Add fast access to pred variable #603

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project does not currently adhere to a particular versioning scheme.
- Improve error messages for redefinition of types and objects. ([#485][gh-485])
- Don't allow tabs to be used for indentation or spacing. ([#463][gh-463])
- Rename builtin function `sleep` to `IO/nanosleep`. ([#581][gh-581])
- Equational number pattern compilation to use the predecessor variable when possible. ([#470][gh-470])

### Fixed

Expand Down Expand Up @@ -333,6 +334,7 @@ and this project does not currently adhere to a particular versioning scheme.
[gh-465]: https://github.com/HigherOrderCO/Bend/issues/465
[gh-466]: https://github.com/HigherOrderCO/Bend/issues/466
[gh-467]: https://github.com/HigherOrderCO/Bend/issues/467
[gh-470]: https://github.com/HigherOrderCO/Bend/issues/470
[gh-475]: https://github.com/HigherOrderCO/Bend/issues/475
[gh-478]: https://github.com/HigherOrderCO/Bend/issues/478
[gh-479]: https://github.com/HigherOrderCO/Bend/issues/479
Expand Down
24 changes: 23 additions & 1 deletion src/fun/transform/desugar_match_defs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
diagnostics::{Diagnostics, WarningType},
fun::{builtins, Adts, Constructors, Ctx, Definition, FanKind, Name, Num, Pattern, Rule, Tag, Term},
maybe_grow,
};
use std::collections::{BTreeSet, HashSet};

Expand Down Expand Up @@ -301,8 +302,10 @@
let mut body = rule.body.clone();
if let Some(var) = var {
let last_num = *nums.last().unwrap();
let var_recovered = Term::add_num(Term::Var { nam: pred_var.clone() }, Num::U24(1 + last_num));
let curr_num = 1 + last_num;

Check warning on line 305 in src/fun/transform/desugar_match_defs.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (curr)
let var_recovered = Term::add_num(Term::Var { nam: pred_var.clone() }, Num::U24(curr_num));

Check warning on line 306 in src/fun/transform/desugar_match_defs.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (curr)
body = Term::Use { nam: Some(var.clone()), val: Box::new(var_recovered), nxt: Box::new(body) };
fast_pred_access(&mut body, curr_num, var, &pred_var);

Check warning on line 308 in src/fun/transform/desugar_match_defs.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (curr)
}
let rule = Rule { pats: rule.pats[1..].to_vec(), body };
new_rules.push(rule);
Expand Down Expand Up @@ -340,6 +343,25 @@
Ok(term)
}

/// Replaces `body` to `pred_var` if the term is a operation that subtracts the given var by the current
/// switch number.
fn fast_pred_access(body: &mut Term, curr_num: u32, var: &Name, pred_var: &Name) {

Check warning on line 348 in src/fun/transform/desugar_match_defs.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (curr)
maybe_grow(|| {
if let Term::Oper { opr: crate::fun::Op::SUB, fst, snd } = body {
if let Term::Num { val: crate::fun::Num::U24(val) } = &**snd {
if let Term::Var { nam } = &**fst {
if nam == var && *val == curr_num {

Check warning on line 353 in src/fun/transform/desugar_match_defs.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (curr)
*body = Term::Var { nam: pred_var.clone() };
}
}
}
}
for child in body.children_mut() {
fast_pred_access(child, curr_num, var, pred_var)
}
})
}

/// When the first column has constructors, create a branch on the constructors
/// of the first arg.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Scott

(pred2) = λa switch a { 0: 0; _: λb switch b { 0: 0; _: λc c; }; }

(pred3) = λa switch a { 0: 0; _: λb switch b { 0: 0; _: λc switch c { 0: 0; _: λd (- (+ d 3) 3); }; }; }
(pred3) = λa switch a { 0: 0; _: λb switch b { 0: 0; _: λc switch c { 0: 0; _: λd d; }; }; }

(zero) = λa switch a { 0: 1; _: λb switch b { 0: 0; _: λ* 0; }; }

Expand All @@ -18,7 +18,7 @@ NumScott

(pred2) = λa switch a { 0: 0; _: λb switch b { 0: 0; _: λc c; }; }

(pred3) = λa switch a { 0: 0; _: λb switch b { 0: 0; _: λc switch c { 0: 0; _: λd (- (+ d 3) 3); }; }; }
(pred3) = λa switch a { 0: 0; _: λb switch b { 0: 0; _: λc switch c { 0: 0; _: λd d; }; }; }

(zero) = λa switch a { 0: 1; _: λb switch b { 0: 0; _: λ* 0; }; }

Expand Down
Loading