1use crate::multi_iterator;
2use hvm::ast::{Net, Tree};
3
4pub mod add_recursive_priority;
5pub mod check_net_size;
6pub mod eta_reduce;
7pub mod inline;
8pub mod mutual_recursion;
9pub mod prune;
10
11pub fn tree_children(tree: &Tree) -> impl DoubleEndedIterator<Item = &Tree> + Clone {
12 multi_iterator!(ChildrenIter { Zero, Two });
13 match tree {
14 Tree::Var { .. } | Tree::Ref { .. } | Tree::Era | Tree::Num { .. } => ChildrenIter::Zero([]),
15 Tree::Con { fst, snd } | Tree::Dup { fst, snd } | Tree::Opr { fst, snd } | Tree::Swi { fst, snd } => {
16 ChildrenIter::Two([fst.as_ref(), snd.as_ref()])
17 }
18 }
19}
20
21pub fn tree_children_mut(tree: &mut Tree) -> impl DoubleEndedIterator<Item = &mut Tree> {
22 multi_iterator!(ChildrenIter { Zero, Two });
23 match tree {
24 Tree::Var { .. } | Tree::Ref { .. } | Tree::Era | Tree::Num { .. } => ChildrenIter::Zero([]),
25 Tree::Con { fst, snd } | Tree::Dup { fst, snd } | Tree::Opr { fst, snd } | Tree::Swi { fst, snd } => {
26 ChildrenIter::Two([fst.as_mut(), snd.as_mut()])
27 }
28 }
29}
30
31pub fn net_trees(net: &Net) -> impl DoubleEndedIterator<Item = &Tree> + Clone {
32 [&net.root].into_iter().chain(net.rbag.iter().flat_map(|(_, fst, snd)| [fst, snd]))
33}
34
35pub fn net_trees_mut(net: &mut Net) -> impl DoubleEndedIterator<Item = &mut Tree> {
36 [&mut net.root].into_iter().chain(net.rbag.iter_mut().flat_map(|(_, fst, snd)| [fst, snd]))
37}
38
39pub fn hvm_book_show_pretty(book: &hvm::ast::Book) -> String {
40 let mut s = String::new();
41 for (nam, def) in book.defs.iter() {
42 s.push_str(&format!("@{} = {}\n", nam, def.root.show()));
43 for (pri, a, b) in def.rbag.iter() {
44 s.push_str(" &");
45 if *pri {
46 s.push('!');
47 } else {
48 s.push(' ');
49 }
50 s.push_str(&a.show());
51 s.push_str(" ~ ");
52 s.push_str(&b.show());
53 s.push('\n');
54 }
55 s.push('\n');
56 }
57 s
58}