1
//! The symbol table.
2
//!
3
//! See the item documentation for [`SymbolTable`] for more details.
4

            
5
use crate::bug;
6
use crate::representation::{Representation, get_repr_rule};
7
use std::any::TypeId;
8

            
9
use std::collections::BTreeSet;
10
use std::collections::btree_map::Entry;
11
use std::collections::{BTreeMap, VecDeque};
12
use std::hash::{Hash, Hasher};
13
use std::sync::Arc;
14
use std::sync::atomic::{AtomicU32, Ordering};
15

            
16
use super::comprehension::Comprehension;
17
use super::serde::{AsId, DefaultWithId, HasId, IdPtr, ObjId, PtrAsInner};
18
use super::{
19
    DeclarationPtr, DomainPtr, Expression, GroundDomain, Model, Moo, Name, ReturnType, Typeable,
20
};
21
use itertools::{Itertools as _, izip};
22
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
23
use serde::{Deserialize, Serialize};
24
use serde_with::serde_as;
25
use tracing::trace;
26
use uniplate::{Biplate, Tree, Uniplate};
27

            
28
/// Global counter of symbol tables.
29
/// Note that the counter is shared between all threads
30
/// Thus, when running multiple models in parallel, IDs may
31
/// be different with every run depending on scheduling order
32
static SYMBOL_TABLE_ID_COUNTER: AtomicU32 = const { AtomicU32::new(0) };
33

            
34
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
35
pub struct SymbolTablePtr
36
where
37
    Self: Send + Sync,
38
{
39
    inner: Arc<SymbolTablePtrInner>,
40
}
41

            
42
impl SymbolTablePtr {
43
    /// Create an empty new [SymbolTable] and return a shared pointer to it
44
47024
    pub fn new() -> Self {
45
47024
        Self::new_with_data(SymbolTable::new())
46
47024
    }
47

            
48
    /// Create an empty new [SymbolTable] with the given parent and return a shared pointer to it
49
11992
    pub fn with_parent(symbols: SymbolTablePtr) -> Self {
50
11992
        Self::new_with_data(SymbolTable::with_parent(symbols))
51
11992
    }
52

            
53
59016
    fn new_with_data(data: SymbolTable) -> Self {
54
59016
        let object_id = SYMBOL_TABLE_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
55
59016
        let id = ObjId {
56
59016
            object_id,
57
59016
            type_name: SymbolTablePtr::TYPE_NAME.into(),
58
59016
        };
59
59016
        Self::new_with_id_and_data(id, data)
60
59016
    }
61

            
62
60376
    fn new_with_id_and_data(id: ObjId, data: SymbolTable) -> Self {
63
60376
        Self {
64
60376
            inner: Arc::new(SymbolTablePtrInner {
65
60376
                id,
66
60376
                value: RwLock::new(data),
67
60376
            }),
68
60376
        }
69
60376
    }
70

            
71
    /// Read the underlying symbol table.
72
    /// This will block the current thread until a read lock can be acquired.
73
    ///
74
    /// # WARNING
75
    ///
76
    /// - If the current thread already holds a lock over this table, this may deadlock.
77
306888352
    pub fn read(&self) -> RwLockReadGuard<'_, SymbolTable> {
78
306888352
        self.inner.value.read()
79
306888352
    }
80

            
81
    /// Mutate the underlying symbol table.
82
    /// This will block the current thread until an exclusive write lock can be acquired.
83
    ///
84
    /// # WARNING
85
    ///
86
    /// - If the current thread already holds a lock over this table, this may deadlock.
87
    /// - Trying to acquire any other lock until the write lock is released will cause a deadlock.
88
    /// - This will mutate the underlying data, which may be shared between other `SymbolTablePtr`s.
89
    ///   Make sure that this is what you want.
90
    ///
91
    /// To create a separate copy of the table, see [SymbolTablePtr::detach].
92
    ///
93
371680
    pub fn write(&self) -> RwLockWriteGuard<'_, SymbolTable> {
94
371680
        self.inner.value.write()
95
371680
    }
96

            
97
    /// Create a new symbol table with the same contents as this one, but a new ID,
98
    /// and return a pointer to it.
99
    pub fn detach(&self) -> Self {
100
        Self::new_with_data(self.read().clone())
101
    }
102
}
103

            
104
impl Default for SymbolTablePtr {
105
    fn default() -> Self {
106
        Self::new()
107
    }
108
}
109

            
110
impl HasId for SymbolTablePtr {
111
    const TYPE_NAME: &'static str = "SymbolTable";
112

            
113
477204
    fn id(&self) -> ObjId {
114
477204
        self.inner.id.clone()
115
477204
    }
116
}
117

            
118
impl DefaultWithId for SymbolTablePtr {
119
    fn default_with_id(id: ObjId) -> Self {
120
        Self::new_with_id_and_data(id, SymbolTable::default())
121
    }
122
}
123

            
124
impl IdPtr for SymbolTablePtr {
125
    type Data = SymbolTable;
126

            
127
694
    fn get_data(&self) -> Self::Data {
128
694
        self.read().clone()
129
694
    }
130

            
131
1360
    fn with_id_and_data(id: ObjId, data: Self::Data) -> Self {
132
1360
        Self::new_with_id_and_data(id, data)
133
1360
    }
134
}
135

            
136
// TODO: this code is almost exactly copied from [DeclarationPtr].
137
//       It should be possible to eliminate the duplication...
138
//       Perhaps by merging SymbolTablePtr and DeclarationPtr together?
139
//       (Alternatively, a macro?)
140

            
141
impl Uniplate for SymbolTablePtr {
142
42824
    fn uniplate(&self) -> (Tree<Self>, Box<dyn Fn(Tree<Self>) -> Self>) {
143
42824
        let symtab = self.read();
144
42824
        let (tree, recons) = Biplate::<SymbolTablePtr>::biplate(&symtab as &SymbolTable);
145

            
146
42824
        let self2 = self.clone();
147
        (
148
42824
            tree,
149
42824
            Box::new(move |x| {
150
                let self3 = self2.clone();
151
                *(self3.write()) = recons(x);
152
                self3
153
            }),
154
        )
155
42824
    }
156
}
157

            
158
impl<To> Biplate<To> for SymbolTablePtr
159
where
160
    SymbolTable: Biplate<To>,
161
    To: Uniplate,
162
{
163
242908
    fn biplate(&self) -> (Tree<To>, Box<dyn Fn(Tree<To>) -> Self>) {
164
242908
        if TypeId::of::<To>() == TypeId::of::<Self>() {
165
            unsafe {
166
42824
                let self_as_to = std::mem::transmute::<&Self, &To>(self).clone();
167
                (
168
42824
                    Tree::One(self_as_to),
169
42824
                    Box::new(move |x| {
170
                        let Tree::One(x) = x else { panic!() };
171

            
172
                        let x_as_self = std::mem::transmute::<&To, &Self>(&x);
173
                        x_as_self.clone()
174
                    }),
175
                )
176
            }
177
        } else {
178
            // call biplate on the enclosed declaration
179
200084
            let decl = self.read();
180
200084
            let (tree, recons) = Biplate::<To>::biplate(&decl as &SymbolTable);
181

            
182
200084
            let self2 = self.clone();
183
            (
184
200084
                tree,
185
200084
                Box::new(move |x| {
186
1480
                    let self3 = self2.clone();
187
1480
                    *(self3.write()) = recons(x);
188
1480
                    self3
189
1480
                }),
190
            )
191
        }
192
242908
    }
193
}
194

            
195
#[derive(Debug)]
196
struct SymbolTablePtrInner {
197
    id: ObjId,
198
    value: RwLock<SymbolTable>,
199
}
200

            
201
impl Hash for SymbolTablePtrInner {
202
    fn hash<H: Hasher>(&self, state: &mut H) {
203
        self.id.hash(state);
204
    }
205
}
206

            
207
impl PartialEq for SymbolTablePtrInner {
208
680
    fn eq(&self, other: &Self) -> bool {
209
680
        self.value.read().eq(&other.value.read())
210
680
    }
211
}
212

            
213
impl Eq for SymbolTablePtrInner {}
214

            
215
/// The global symbol table, mapping names to their definitions.
216
///
217
/// Names in the symbol table are unique, including between different types of object stored in the
218
/// symbol table. For example, you cannot have a letting and decision variable with the same name.
219
///
220
/// # Symbol Kinds
221
///
222
/// The symbol table tracks the following types of symbol:
223
///
224
/// ## Decision Variables
225
///
226
/// ```text
227
/// find NAME: DOMAIN
228
/// ```
229
///
230
/// See [`DecisionVariable`](super::DecisionVariable).
231
///
232
/// ## Lettings
233
///
234
/// Lettings define constants, of which there are two types:
235
///
236
///   + **Constant values**: `letting val be A`, where A is an [`Expression`].
237
///
238
///     A can be any integer, boolean, or matrix expression.
239
///     A can include references to other lettings, model parameters, and, unlike Savile Row,
240
///     decision variables.
241
///
242
///   + **Constant domains**: `letting Domain be domain D`, where D is a [`Domain`].
243
///
244
///     D can include references to other lettings and model parameters, and, unlike Savile Row,
245
///     decision variables.
246
///
247
/// Unless otherwise stated, these follow the semantics specified in section 2.2.2 of the Savile
248
/// Row manual (version 1.9.1 at time of writing).
249
#[serde_as]
250
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
251
pub struct SymbolTable {
252
    #[serde_as(as = "Vec<(_,PtrAsInner)>")]
253
    table: BTreeMap<Name, DeclarationPtr>,
254

            
255
    #[serde_as(as = "Option<AsId>")]
256
    parent: Option<SymbolTablePtr>,
257

            
258
    next_machine_name: i32,
259
}
260

            
261
impl SymbolTable {
262
    /// Creates an empty symbol table.
263
391774
    pub fn new() -> SymbolTable {
264
391774
        SymbolTable::new_inner(None)
265
391774
    }
266

            
267
    /// Creates an empty symbol table with the given parent.
268
11992
    pub fn with_parent(parent: SymbolTablePtr) -> SymbolTable {
269
11992
        SymbolTable::new_inner(Some(parent))
270
11992
    }
271

            
272
403766
    fn new_inner(parent: Option<SymbolTablePtr>) -> SymbolTable {
273
403766
        let id = SYMBOL_TABLE_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
274
403766
        trace!(
275
            "new symbol table: id = {id}  parent_id = {}",
276
560
            parent
277
560
                .as_ref()
278
560
                .map(|x| x.id().to_string())
279
560
                .unwrap_or(String::from("none"))
280
        );
281
403766
        SymbolTable {
282
403766
            table: BTreeMap::new(),
283
403766
            next_machine_name: 0,
284
403766
            parent,
285
403766
        }
286
403766
    }
287

            
288
    /// Looks up the declaration with the given name in the current scope only.
289
    ///
290
    /// Returns `None` if there is no declaration with that name in the current scope.
291
575138208
    pub fn lookup_local(&self, name: &Name) -> Option<DeclarationPtr> {
292
575138208
        self.table.get(name).cloned()
293
575138208
    }
294

            
295
    /// Looks up the declaration with the given name, checking all enclosing scopes.
296
    ///
297
    /// Returns `None` if there is no declaration with that name in scope.
298
574973190
    pub fn lookup(&self, name: &Name) -> Option<DeclarationPtr> {
299
574973190
        self.lookup_local(name).or_else(|| {
300
1063870
            self.parent
301
1063870
                .as_ref()
302
1063870
                .and_then(|parent| parent.read().lookup(name))
303
1063870
        })
304
574973190
    }
305

            
306
    /// Inserts a declaration into the symbol table.
307
    ///
308
    /// Returns `None` if there is already a symbol with this name in the local scope.
309
1449158
    pub fn insert(&mut self, declaration: DeclarationPtr) -> Option<()> {
310
1449158
        let name = declaration.name().clone();
311
1449158
        if let Entry::Vacant(e) = self.table.entry(name) {
312
767738
            e.insert(declaration);
313
767738
            Some(())
314
        } else {
315
681420
            None
316
        }
317
1449158
    }
318

            
319
    /// Updates or adds a declaration in the immediate local scope.
320
84580
    pub fn update_insert(&mut self, declaration: DeclarationPtr) {
321
84580
        let name = declaration.name().clone();
322
84580
        self.table.insert(name, declaration);
323
84580
    }
324

            
325
    /// Looks up the return type for name if it has one and is in scope.
326
    pub fn return_type(&self, name: &Name) -> Option<ReturnType> {
327
        self.lookup(name).map(|x| x.return_type())
328
    }
329

            
330
    /// Looks up the return type for name if has one and is in the local scope.
331
    pub fn return_type_local(&self, name: &Name) -> Option<ReturnType> {
332
        self.lookup_local(name).map(|x| x.return_type())
333
    }
334

            
335
    /// Looks up the domain of name if it has one and is in scope.
336
    ///
337
    /// This method can return domain references: if a ground domain is always required, use
338
    /// [`SymbolTable::resolve_domain`].
339
459528
    pub fn domain(&self, name: &Name) -> Option<DomainPtr> {
340
459528
        if let Name::WithRepresentation(name, _) = name {
341
146296
            self.lookup(name)?.domain()
342
        } else {
343
313232
            self.lookup(name)?.domain()
344
        }
345
459528
    }
346

            
347
    /// Looks up the domain of name, resolving domain references to ground domains.
348
    ///
349
    /// See [`SymbolTable::domain`].
350
459528
    pub fn resolve_domain(&self, name: &Name) -> Option<Moo<GroundDomain>> {
351
459528
        self.domain(name)?.resolve()
352
459528
    }
353

            
354
    /// Iterates over entries in the LOCAL symbol table.
355
11974224
    pub fn into_iter_local(self) -> impl Iterator<Item = (Name, DeclarationPtr)> {
356
11974224
        self.table.into_iter()
357
11974224
    }
358

            
359
    /// Iterates over entries in the LOCAL symbol table, by reference.
360
624770
    pub fn iter_local(&self) -> impl Iterator<Item = (&Name, &DeclarationPtr)> {
361
624770
        self.table.iter()
362
624770
    }
363

            
364
    /// Iterates over entries in the LOCAL symbol table, by reference.
365
12
    pub fn iter_local_mut(&mut self) -> impl Iterator<Item = (&Name, &mut DeclarationPtr)> {
366
12
        self.table.iter_mut()
367
12
    }
368

            
369
    /// Extends the symbol table with the given symbol table, updating the gensym counter if
370
    /// necessary.
371
296888
    pub fn extend(&mut self, other: SymbolTable) {
372
296888
        if other.table.keys().count() > self.table.keys().count() {
373
58252
            let new_vars = other.table.keys().collect::<BTreeSet<_>>();
374
58252
            let old_vars = self.table.keys().collect::<BTreeSet<_>>();
375

            
376
791452
            for added_var in new_vars.difference(&old_vars) {
377
791452
                let next_var = &mut self.next_machine_name;
378
791452
                if let Name::Machine(m) = *added_var
379
706872
                    && *m >= *next_var
380
706872
                {
381
706872
                    *next_var = *m + 1;
382
706872
                }
383
            }
384
238636
        }
385

            
386
296888
        self.table.extend(other.table);
387
296888
    }
388

            
389
    /// Creates a new variable in this symbol table with a unique name, and returns its
390
    /// declaration.
391
722416
    pub fn gensym(&mut self, domain: &DomainPtr) -> DeclarationPtr {
392
722416
        let num = self.next_machine_name;
393
722416
        self.next_machine_name += 1;
394
722416
        let decl = DeclarationPtr::new_find(Name::Machine(num), domain.clone());
395
722416
        self.insert(decl.clone());
396
722416
        decl
397
722416
    }
398

            
399
    /// Gets the parent of this symbol table as a mutable reference.
400
    ///
401
    /// This function provides no sanity checks.
402
1360
    pub fn parent_mut_unchecked(&mut self) -> &mut Option<SymbolTablePtr> {
403
1360
        &mut self.parent
404
1360
    }
405

            
406
    /// Gets the parent of this symbol table.
407
462096
    pub fn parent(&self) -> &Option<SymbolTablePtr> {
408
462096
        &self.parent
409
462096
    }
410

            
411
    /// Gets the representation `representation` for `name`.
412
    ///
413
    /// # Returns
414
    ///
415
    /// + `None` if `name` does not exist, is not a decision variable, or does not have that representation.
416
43100
    pub fn get_representation(
417
43100
        &self,
418
43100
        name: &Name,
419
43100
        representation: &[&str],
420
43100
    ) -> Option<Vec<Box<dyn Representation>>> {
421
        // TODO: move representation stuff to declaration / variable to avoid cloning? (we have to
422
        // move inside of an rc here, so cannot return borrows)
423
        //
424
        // Also would prevent constant "does exist" "is var" checks.
425
        //
426
        // The reason it is not there now is because I'm getting serde issues...
427
        //
428
        // Also might run into issues putting get_or_add into declaration/variable, as that
429
        // requires us to mutably borrow both the symbol table, and the variable inside the symbol
430
        // table..
431

            
432
43100
        let decl = self.lookup(name)?;
433
43100
        let var = &decl.as_find()?;
434

            
435
43100
        var.representations
436
43100
            .iter()
437
43100
            .find(|x| &x.iter().map(|r| r.repr_name()).collect_vec()[..] == representation)
438
43100
            .cloned()
439
43100
    }
440

            
441
    /// Gets all initialised representations for `name`.
442
    ///
443
    /// # Returns
444
    ///
445
    /// + `None` if `name` does not exist, or is not a decision variable.
446
1083576
    pub fn representations_for(&self, name: &Name) -> Option<Vec<Vec<Box<dyn Representation>>>> {
447
1083576
        let decl = self.lookup(name)?;
448
1083576
        decl.as_find().map(|x| x.representations.clone())
449
1083576
    }
450

            
451
    /// Gets the representation `representation` for `name`, creating it if it does not exist.
452
    ///
453
    /// If the representation does not exist, this method initialises the representation in this
454
    /// symbol table, adding the representation to `name`, and the declarations for the represented
455
    /// variables to the symbol table.
456
    ///
457
    /// # Usage
458
    ///
459
    /// Representations for variable references should be selected and created by the
460
    /// `select_representation` rule. Therefore, this method should not be used in other rules.
461
    /// Consider using [`get_representation`](`SymbolTable::get_representation`) instead.
462
    ///
463
    /// # Returns
464
    ///
465
    /// + `None` if `name` does not exist, is not a decision variable, or cannot be given that
466
    ///   representation.
467
151206
    pub fn get_or_add_representation(
468
151206
        &mut self,
469
151206
        name: &Name,
470
151206
        representation: &[&str],
471
151206
    ) -> Option<Vec<Box<dyn Representation>>> {
472
        // Lookup the declaration reference
473
151206
        let mut decl = self.lookup(name)?;
474

            
475
147566
        if let Some(var) = decl.as_find()
476
147566
            && let Some(existing_reprs) = var
477
147566
                .representations
478
147566
                .iter()
479
147566
                .find(|x| &x.iter().map(|r| r.repr_name()).collect_vec()[..] == representation)
480
147566
                .cloned()
481
        {
482
135118
            return Some(existing_reprs); // Found: return early
483
12448
        }
484
        // Representation not found
485

            
486
        // TODO: nested representations logic...
487
12448
        if representation.len() != 1 {
488
            bug!("nested representations not implemented")
489
12448
        }
490
12448
        let repr_name_str = representation[0];
491
12448
        let repr_init_fn = get_repr_rule(repr_name_str)?;
492

            
493
12448
        let reprs = vec![repr_init_fn(name, self)?];
494

            
495
        // Get mutable access to the variable part
496
12448
        let mut var = decl.as_find_mut()?;
497

            
498
12448
        for repr_instance in &reprs {
499
12448
            repr_instance
500
12448
                .declaration_down()
501
12448
                .ok()?
502
12448
                .into_iter()
503
83500
                .for_each(|x| self.update_insert(x));
504
        }
505

            
506
12448
        var.representations.push(reprs.clone());
507

            
508
12448
        Some(reprs)
509
151206
    }
510
}
511

            
512
impl IntoIterator for SymbolTable {
513
    type Item = (Name, DeclarationPtr);
514

            
515
    type IntoIter = SymbolTableIter;
516

            
517
    /// Iterates over symbol table entries in scope.
518
21716
    fn into_iter(self) -> Self::IntoIter {
519
21716
        SymbolTableIter {
520
21716
            inner: self.table.into_iter(),
521
21716
            parent: self.parent,
522
21716
        }
523
21716
    }
524
}
525

            
526
/// Iterator over all symbol table entries in scope.
527
pub struct SymbolTableIter {
528
    // iterator over the current scopes' btreemap
529
    inner: std::collections::btree_map::IntoIter<Name, DeclarationPtr>,
530

            
531
    // the parent scope
532
    parent: Option<SymbolTablePtr>,
533
}
534

            
535
impl Iterator for SymbolTableIter {
536
    type Item = (Name, DeclarationPtr);
537

            
538
1567816
    fn next(&mut self) -> Option<Self::Item> {
539
1567816
        let mut val = self.inner.next();
540

            
541
        // Go up the tree until we find a parent symbol table with declarations to iterate over.
542
        //
543
        // Note that the parent symbol table may be empty - this is why this is a loop!
544
1567816
        while val.is_none() {
545
21716
            let parent = self.parent.clone()?;
546

            
547
            let guard = parent.read();
548
            self.inner = guard.table.clone().into_iter();
549
            self.parent.clone_from(&guard.parent);
550

            
551
            val = self.inner.next();
552
        }
553

            
554
1546100
        val
555
1567816
    }
556
}
557

            
558
impl Default for SymbolTable {
559
    fn default() -> Self {
560
        Self::new_inner(None)
561
    }
562
}
563

            
564
// TODO: if we could override `Uniplate` impl but still derive `Biplate` instances,
565
//       we could remove some of this manual code
566
impl Uniplate for SymbolTable {
567
    fn uniplate(&self) -> (Tree<Self>, Box<dyn Fn(Tree<Self>) -> Self>) {
568
        // do not recurse up parents, that would be weird?
569
        let self2 = self.clone();
570
        (Tree::Zero, Box::new(move |_| self2.clone()))
571
    }
572
}
573

            
574
impl Biplate<SymbolTablePtr> for SymbolTable {
575
42824
    fn biplate(
576
42824
        &self,
577
42824
    ) -> (
578
42824
        Tree<SymbolTablePtr>,
579
42824
        Box<dyn Fn(Tree<SymbolTablePtr>) -> Self>,
580
42824
    ) {
581
42824
        let self2 = self.clone();
582
42824
        (Tree::Zero, Box::new(move |_| self2.clone()))
583
42824
    }
584
}
585

            
586
impl Biplate<Expression> for SymbolTable {
587
502286
    fn biplate(&self) -> (Tree<Expression>, Box<dyn Fn(Tree<Expression>) -> Self>) {
588
502286
        let (child_trees, ctxs): (VecDeque<_>, Vec<_>) = self
589
502286
            .table
590
502286
            .values()
591
502286
            .map(Biplate::<Expression>::biplate)
592
502286
            .unzip();
593

            
594
502286
        let tree = Tree::Many(child_trees);
595

            
596
502286
        let self2 = self.clone();
597
502286
        let ctx = Box::new(move |tree| {
598
1480
            let Tree::Many(exprs) = tree else {
599
                panic!("unexpected children structure");
600
            };
601

            
602
1480
            let mut self3 = self2.clone();
603
1480
            let self3_iter = self3.table.iter_mut();
604
11040
            for (ctx, tree, (_, decl)) in izip!(&ctxs, exprs, self3_iter) {
605
                // update declaration inside the pointer instead of creating a new one, so all
606
                // things referencing this keep referencing this.
607
11040
                *decl = ctx(tree)
608
            }
609

            
610
1480
            self3
611
1480
        });
612

            
613
502286
        (tree, ctx)
614
502286
    }
615
}
616

            
617
impl Biplate<Comprehension> for SymbolTable {
618
240
    fn biplate(
619
240
        &self,
620
240
    ) -> (
621
240
        Tree<Comprehension>,
622
240
        Box<dyn Fn(Tree<Comprehension>) -> Self>,
623
240
    ) {
624
240
        let (expr_tree, expr_ctx) = <SymbolTable as Biplate<Expression>>::biplate(self);
625

            
626
240
        let (exprs, recons_expr_tree) = expr_tree.list();
627

            
628
240
        let (comprehension_tree, comprehension_ctx) =
629
240
            <VecDeque<Expression> as Biplate<Comprehension>>::biplate(&exprs);
630

            
631
240
        let ctx = Box::new(move |x| {
632
            // 1. turn comprehension tree into a list of expressions
633
240
            let exprs = comprehension_ctx(x);
634

            
635
            // 2. turn list of expressions into an expression tree
636
240
            let expr_tree = recons_expr_tree(exprs);
637

            
638
            // 3. turn expression tree into a symbol table
639
240
            expr_ctx(expr_tree)
640
240
        });
641

            
642
240
        (comprehension_tree, ctx)
643
240
    }
644
}
645

            
646
impl Biplate<Model> for SymbolTable {
647
    // walk into expressions
648
    fn biplate(&self) -> (Tree<Model>, Box<dyn Fn(Tree<Model>) -> Self>) {
649
        let (expr_tree, expr_ctx) = <SymbolTable as Biplate<Expression>>::biplate(self);
650

            
651
        let (exprs, recons_expr_tree) = expr_tree.list();
652

            
653
        let (submodel_tree, submodel_ctx) =
654
            <VecDeque<Expression> as Biplate<Model>>::biplate(&exprs);
655

            
656
        let ctx = Box::new(move |x| {
657
            // 1. turn submodel tree into a list of expressions
658
            let exprs = submodel_ctx(x);
659

            
660
            // 2. turn list of expressions into an expression tree
661
            let expr_tree = recons_expr_tree(exprs);
662

            
663
            // 3. turn expression tree into a symbol table
664
            expr_ctx(expr_tree)
665
        });
666
        (submodel_tree, ctx)
667
    }
668
}