bend/fun/transform/
desugar_use.rs

1use crate::{
2  fun::{Book, Term},
3  maybe_grow,
4};
5
6impl Book {
7  /// Inline copies of the declared bind in the `use` expression.
8  ///
9  /// Example:
10  /// ```bend
11  /// use id = λx x
12  /// (id id id)
13  ///
14  /// // Transforms to:
15  /// (λx x λx x λx x)
16  /// ```
17  pub fn desugar_use(&mut self) {
18    for def in self.defs.values_mut() {
19      for rule in def.rules.iter_mut() {
20        rule.body.desugar_use();
21      }
22    }
23  }
24
25  /// Inline copies of the declared bind in `Fold`, `Mat` and `Open` inside `use` expressions.
26  pub fn desugar_ctr_use(&mut self) {
27    for def in self.defs.values_mut() {
28      for rule in def.rules.iter_mut() {
29        rule.body.desugar_ctr_use();
30      }
31    }
32  }
33}
34
35impl Term {
36  pub fn desugar_use(&mut self) {
37    maybe_grow(|| {
38      for children in self.children_mut() {
39        children.desugar_use();
40      }
41    });
42
43    if let Term::Use { nam: Some(nam), val, nxt } = self {
44      nxt.subst(nam, val);
45      *self = std::mem::take(nxt);
46    }
47  }
48
49  pub fn desugar_ctr_use(&mut self) {
50    maybe_grow(|| {
51      for children in self.children_mut() {
52        children.desugar_ctr_use();
53      }
54    });
55
56    if let Term::Use { nam: Some(nam), val, nxt } = self {
57      if let Term::Var { nam: val } = val.as_ref() {
58        nxt.subst_ctrs(nam, val);
59      }
60    }
61  }
62}