-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtermdag.rs
273 lines (241 loc) · 8.53 KB
/
termdag.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use crate::{
ast::Literal,
util::{HashMap, IndexSet},
Expr, GenericExpr, Symbol,
};
use std::io::Write;
pub type TermId = usize;
#[allow(rustdoc::private_intra_doc_links)]
/// Like [`Expr`]s but with sharing and deduplication.
///
/// Terms refer to their children indirectly via opaque [TermId]s (internally
/// these are just `usize`s) that map into an ambient [`TermDag`].
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Term {
Lit(Literal),
Var(Symbol),
App(Symbol, Vec<TermId>),
}
/// A hashconsing arena for [`Term`]s.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct TermDag {
/// A bidirectional map between deduplicated `Term`s and indices.
nodes: IndexSet<Term>,
}
#[macro_export]
macro_rules! match_term_app {
($e:expr; $body:tt) => {
match $e {
Term::App(head, args) => {
match (head.as_str(), args.as_slice())
$body
}
_ => panic!("not an app")
}
}
}
impl TermDag {
/// Returns the number of nodes in this DAG.
pub fn size(&self) -> usize {
self.nodes.len()
}
/// Convert the given term to its id.
///
/// Panics if the term does not already exist in this [TermDag].
pub fn lookup(&self, node: &Term) -> TermId {
self.nodes.get_index_of(node).unwrap()
}
/// Convert the given id to the corresponding term.
///
/// Panics if the id is not valid.
pub fn get(&self, id: TermId) -> &Term {
self.nodes.get_index(id).unwrap()
}
/// Make and return a [`Term::App`] with the given head symbol and children,
/// and insert into the DAG if it is not already present.
///
/// Panics if any of the children are not already in the DAG.
pub fn app(&mut self, sym: Symbol, children: Vec<Term>) -> Term {
let node = Term::App(sym, children.iter().map(|c| self.lookup(c)).collect());
self.add_node(&node);
node
}
/// Make and return a [`Term::Lit`] with the given literal, and insert into
/// the DAG if it is not already present.
pub fn lit(&mut self, lit: Literal) -> Term {
let node = Term::Lit(lit);
self.add_node(&node);
node
}
/// Make and return a [`Term::Var`] with the given symbol, and insert into
/// the DAG if it is not already present.
pub fn var(&mut self, sym: Symbol) -> Term {
let node = Term::Var(sym);
self.add_node(&node);
node
}
fn add_node(&mut self, node: &Term) {
if self.nodes.get(node).is_none() {
self.nodes.insert(node.clone());
}
}
/// Recursively converts the given expression to a term.
///
/// This involves inserting every subexpression into this DAG. Because
/// TermDags are hashconsed, the resulting term is guaranteed to maximally
/// share subterms.
pub fn expr_to_term(&mut self, expr: &GenericExpr<Symbol, Symbol>) -> Term {
let res = match expr {
GenericExpr::Lit(_, lit) => Term::Lit(lit.clone()),
GenericExpr::Var(_, v) => Term::Var(*v),
GenericExpr::Call(_, op, args) => {
let args = args
.iter()
.map(|a| {
let term = self.expr_to_term(a);
self.lookup(&term)
})
.collect();
Term::App(*op, args)
}
};
self.add_node(&res);
res
}
/// Recursively converts the given term to an expression.
///
/// Panics if the term contains subterms that are not in the DAG.
pub fn term_to_expr(&self, term: &Term) -> Expr {
match term {
Term::Lit(lit) => Expr::lit_no_span(lit.clone()),
Term::Var(v) => Expr::var_no_span(*v),
Term::App(op, args) => {
let args: Vec<_> = args
.iter()
.map(|a| self.term_to_expr(self.get(*a)))
.collect();
Expr::call_no_span(*op, args)
}
}
}
/// Converts the given term to a string.
///
/// Panics if the term or any of its subterms are not in the DAG.
pub fn to_string(&self, term: &Term) -> String {
// Vec is used here instead of String as String doesn't have it's
// extend_from_within method stabilized.
let mut result = vec![];
// subranges of the `result` string containing already stringified subterms
let mut ranges = HashMap::<TermId, (usize, usize)>::default();
let id = self.lookup(term);
// use a stack to avoid stack overflow
let mut stack = vec![(id, false, None)];
while let Some((id, space_before, mut start_index)) = stack.pop() {
if space_before {
result.push(b' ');
}
if let Some((start, end)) = ranges.get(&id) {
result.extend_from_within(*start..*end);
continue;
}
match self.nodes[id].clone() {
Term::App(name, children) => {
if start_index.is_some() {
result.push(b')');
} else {
stack.push((id, false, Some(result.len())));
write!(&mut result, "({}", name).unwrap();
for c in children.iter().rev() {
stack.push((*c, true, None));
}
}
}
Term::Lit(lit) => {
start_index = Some(result.len());
write!(&mut result, "{lit}").unwrap();
}
Term::Var(v) => {
start_index = Some(result.len());
write!(&mut result, "{v}").unwrap();
}
}
if let Some(start_index) = start_index {
ranges.insert(id, (start_index, result.len()));
}
}
String::from_utf8(result).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::*;
fn parse_term(s: &str) -> (TermDag, Term) {
let e = parse_expr(None, s).unwrap();
let mut td = TermDag::default();
let t = td.expr_to_term(&e);
(td, t)
}
#[test]
fn test_to_from_expr() {
let s = r#"(f (g x y) x y (g x y))"#;
let e = parse_expr(None, s).unwrap();
let mut td = TermDag::default();
assert_eq!(td.size(), 0);
let t = td.expr_to_term(&e);
assert_eq!(td.size(), 4);
// the expression above has 4 distinct subterms.
// in left-to-right, depth-first order, they are:
// x, y, (g x y), and the root call to f
// so we can compute expected answer by hand:
assert_eq!(
td.nodes.as_slice().iter().cloned().collect::<Vec<_>>(),
vec![
Term::Var("x".into()),
Term::Var("y".into()),
Term::App("g".into(), vec![0, 1]),
Term::App("f".into(), vec![2, 0, 1, 2]),
]
);
let e2 = td.term_to_expr(&t);
// This is tested using Sexp's equality because e1 and e2 have different
// annotations. A better way to test this would be to implement a map_ann
// function for GenericExpr.
assert_eq!(e.to_sexp(), e2.to_sexp()); // roundtrip
}
#[test]
fn test_match_term_app() {
let s = r#"(f (g x y) x y (g x y))"#;
let (td, t) = parse_term(s);
match_term_app!(t; {
("f", [_, x, _, _]) => assert_eq!(
td.term_to_expr(td.get(*x)),
crate::ast::GenericExpr::Var(DUMMY_SPAN.clone(), Symbol::new("x"))
),
(head, _) => panic!("unexpected head {}, in {}:{}:{}", head, file!(), line!(), column!())
})
}
#[test]
fn test_to_string() {
let s = r#"(f (g x y) x y (g x y))"#;
let (td, t) = parse_term(s);
assert_eq!(td.to_string(&t), s);
}
#[test]
fn test_lookup() {
let s = r#"(f (g x y) x y (g x y))"#;
let (td, t) = parse_term(s);
assert_eq!(td.lookup(&t), td.size() - 1);
}
#[test]
fn test_app_var_lit() {
let s = r#"(f (g x y) x 7 (g x y))"#;
let (mut td, t) = parse_term(s);
let x = td.var("x".into());
let y = td.var("y".into());
let seven = td.lit(7.into());
let g = td.app("g".into(), vec![x.clone(), y.clone()]);
let t2 = td.app("f".into(), vec![g.clone(), x, seven, g]);
assert_eq!(t, t2);
}
}