1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::{
  fun::{Book, Term},
  maybe_grow,
};

impl Book {
  /// Inline copies of the declared bind in the `use` expression.
  ///
  /// Example:
  /// ```bend
  /// use id = λx x
  /// (id id id)
  ///
  /// // Transforms to:
  /// (λx x λx x λx x)
  /// ```
  pub fn apply_use(&mut self) {
    for def in self.defs.values_mut() {
      for rule in def.rules.iter_mut() {
        rule.body.apply_use();
      }
    }
  }
}

impl Term {
  pub fn apply_use(&mut self) {
    maybe_grow(|| {
      for children in self.children_mut() {
        children.apply_use();
      }
    });

    if let Term::Use { nam: Some(nam), val, nxt } = self {
      nxt.subst(nam, val);
      *self = std::mem::take(nxt);
    }
  }
}