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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*!
Reflects the DCG engine, including both the effects of the
programs running in it, and the internal effects of the engine
cleaning and dirtying the DCG.  For the latter effects, see the
`trace` module.

**Reflected Values**.  Notably, the values in the engine
(including the values of mutable and compute nodes, and the values
stored on edges between them) are reflected here into a special `Val`
type.  Primarily, the distinction between actual Rust values and
this reflected `Val` type is what makes the DCG engine "reflected"
by the definitions in this module, and not identical to them.

This module provides an interface used by Adapton Lab to produce
HTML visualizations of these internal structures, for
experimentation and debugging (namely, the `dcg_reflect_begin` and
`dcg_reflect_end` functions).  For the purposes of debugging,
visualization and design/exploration, we exploit the reflected
version of values to "walk" them, finding their articulations, and
walking their values, recursively.
*/

use crate::engine::Name;
use crate::macros::ProgPt;
use std::fmt::Debug;
use std::rc::Rc;
use std::collections::HashMap;

pub use crate::engine::reflect_dcg::*;

/// This trait consists of the ability for a reference to `Self` to
/// produce a `T`.  Conceptually, that value of type T is the
/// "reflection" of `Self`.  A large set of types in `engine`
/// implement this trait for a particular type in this module, which
/// represents its reflection.  The documentation of this module makes
/// this correspondance clear.
pub trait Reflect<T> : Debug {
  fn reflect (&self) -> T;
}

impl<S,T:Reflect<S>+Debug> Reflect<Option<S>> for Option<T> {
  fn reflect (&self) -> Option<S> {
    match *self { 
      None => None, 
      Some(ref x) => Some(x.reflect())
    }
  }
}

impl<'a,S,T:Reflect<S>+Debug> Reflect<S> for &'a Rc<T> {
  fn reflect (&self) -> S {
    (**self).reflect()
  }
}

impl<'a,S,T:Reflect<S>+Debug> Reflect<S> for Rc<T> {
  fn reflect (&self) -> S {
    (**self).reflect()
  }
}

/// Reflected value; Gives a syntax for inductive data type
/// constructors (`Constr`), named articulations (`Art`) and primitive
/// data (`Data`).  All values in the engine (including the values of
/// nodes, and the values stored on edges) are represented with this
/// reflected `Val` type.  Primarily, this distinction between actual
/// Rust values and this type is what makes the DCG engine "reflected"
/// by the definitions in this module, and not identical to them.
#[derive(Debug,Clone,Hash,Eq,PartialEq)]
pub enum Val {
  /// Constructor, with a sequence of value parameters.
  Constr(Name,Vec<Val>),

  /// A tuple of values (like a constructor, but without a constructor
  /// name). Can be seen as a special case of `Constr`.
  Tuple(Vec<Val>),

  /// A list of values (like a tuple, but parsed and printed
  /// differently). Can be seen as a special case of `Constr`.
  Vec(Vec<Val>),
  
  /// Constructor with a sequence of fields (name-value pairs) as parameters.
  Struct(Name,Vec<(Name,Val)>),

  /// Named articulation, and its content (an `Art` is either a named value, or a named computation).
  Art(Loc,ArtContent),

  /// First-class `Name` value.
  Name(Name),
  
  /// Primitive, immutable data.
  Const(Const),
  
  /// Temporary; for marking places in code where we should produce a
  /// value, but don't yet have a good way to do so.
  ValTODO,
}

/// Primitive constants
#[derive(Debug,Clone,Hash,Eq,PartialEq)]
pub enum Const {
  /// Integers
  Num(isize),
  /// Natural numbers
  Nat(usize),
  /// Strings
  String(String),
}

/// The content of an articulation: Either a cell holding a value, or
/// a thunk that has optionally produced a value.
#[derive(Debug,Clone,Hash,Eq,PartialEq)]
pub enum ArtContent {
  /// The `Art` consists of a ref cell holding a value
  Val(Rc<Val>),
  /// The `Art` consists of a thunk that, when forced, computes a value
  Comp(Option<Rc<Val>>),
  /// `Unknown` content means that it has not yet dereferenced by any
  /// reflective process.  A reflective process dereferences an `Art`
  /// by using a reflected `DCG` to map this `Art`'s `Loc` to a
  /// `Node`.  This node gives one of the two known cases (`Val` or
  /// `Comp`), above, depending on whether it is a `RefNode` or a
  /// `CompNode`.
  Unknown,
}

/// Reflected version of `engine::Loc` A `Loc` is a particular
/// template for a `Name`: It is a path (a possibly-empty list of
/// `Name`s), followed by a distinguished `Name`.  A `Loc` can be
/// thought of roughly like a file path in UNIX (but Adapton has
/// nothing to do with files, or with UNIX, directly).
#[derive(PartialEq,Eq,Debug,Hash,Clone)]
pub struct Loc {
  /// The path of the `Loc` is a list of `Name`s.
  pub path: Path,
  /// The distinguished `Name` of the `Loc` (must be unique in the path).
  pub name: Name,
}

/// A `Path` here is just a `Vec` of `Name`s
pub type Path = Vec<Name>;

/// Reflected version of `engine::Effect`
#[derive(Debug,Clone,Eq,PartialEq,Hash)]
pub enum Effect {
  /// The effect consists of a thunk observing the value of another
  /// thunk or reference cell.  That is, the effect consists of
  /// **consuming** a value, by demanding its production.
  Force,
  /// The effect consists of a thunk allocating a value or thunk at a
  /// particular name.  That is, the effect consists of **producing**
  /// a value or computation.  If this content differs, then the
  /// allocation is a **reallocation**, and the engine dirties the old
  /// observers and allocators of the preceding content.
  Alloc,
}

/// Reflected version of `engine::Succ`.  Unlike the real engine's
/// `Succ` type, this version stores a reflected value (of type
/// `Val`).
#[derive(Debug,Clone,Eq,PartialEq,Hash)]
pub struct Succ {
  /// Dirty invariant: If this edge is dirty, then all predecessors of
  /// the edge are dirty too.
  pub dirty:  bool,
  /// The target of the outgoing `Effect`
  pub loc:    Loc,
  /// The effect: either producing or consuming articulated content
  pub effect: Effect,
  /// The value either produced or consumed by this `Effect`
  pub value:  Val,
  /// Duplicate edge invariant: If this edge is a duplicate according
  /// to this flag, then it is preceded by an edge with the same
  /// effect `effect`, targeting the same location `loc`.
  /// Furthermore, because of the (precise naming) semantics of
  /// Adapton and its programs, the observed value `value` and `dirty`
  /// statuses are always the same across duplicated edges.  Hence,
  /// the engine does not store these duplicate edges: They are
  /// completely redundant.
  pub is_dup: bool,
}

// Note: Can't implement Reflect<T> for T, since that conflicts with
// impl Reflect<Option<S>> for Option<T>.  I don't understand why,
// since T != S in the latter case, making it applicable when
// Reflect<T> for T is not.
impl Reflect<Succ> for Succ {
  fn reflect (&self) -> Succ {
    self.clone()
  }
}

/// Reflected version of `engine::Pred`
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Pred {
  /// The predecessor of the node in question
  pub loc:    Loc,
  /// The effect that the predecessor has done to the node in question
  pub effect: Effect,
}

/// Reflected version of `engine::CompNode`.  Stores a reflected value
/// of type `Option<Val>`, which is `None` when the node has not yet
/// been executed, and `Some(_)` otherwise.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct CompNode {
  pub preds:   Vec<Pred>,
  pub succs:   Vec<Succ>,
  pub prog_pt: ProgPt,
  pub value:   Option<Val>,
}

/// Reflected version of `engine::MutNode`.  Stores a reflected value of type `Val`.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct RefNode {  
  pub preds: Vec<Pred>,
  pub value: Val,
}

/// Reflected version of `engine::PureNode`.  Stores a reflected value of type `Val`.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct PureNode {  
  pub value: Val,
}

/// Reflected version of `engine::Node`.  Unlike the real engine,
/// these nodes are not parameterized by a value type.  Instead, their
/// values are all reflected into type `Val`.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Node {
  Comp(CompNode),
  Ref(RefNode),
  Pure(PureNode),
}

/// Get the `Succ`s of a `Node`, if they are defined.
pub fn succs_of_node (nd:&Node) -> Option<&Vec<Succ>> {
  match *nd {
    Node::Comp(ref nd) => Some(&nd.succs),
    Node::Ref(ref _nd) => None,
    Node::Pure(ref _nd) => None,
  }
}

/// Get the `Pred`s of a `Node`, if they are defined.
pub fn preds_of_node (nd:&Node) -> Option<&Vec<Pred>> {
  match *nd {
    Node::Comp(ref nd) => Some(&nd.preds),
    Node::Ref(ref nd) => Some(&nd.preds),
    Node::Pure(ref _nd) => None,
  }
}

/// Reflected version of `engine::Frame`.
#[derive(Debug, Clone)]
pub struct Frame {
  pub loc:   Loc,
  pub succs: Vec<Succ>,
}

/// Reflected version of `engine::DCG`.
#[derive(Debug, Clone)]
pub struct DCG {
  /// The current memo table, mapping `Loc`s to `Node`s.
  pub table: HashMap<Loc, Node>,
  /// A stack of `Frame`s, which store the currently-executing nodes, and their outgoing edges thus far.
  pub stack: Vec<Frame>,
  /// A list of `Name`s, which is extended for nested regions of code
  /// by the `ns` (namespace) combinator.  This path variable
  /// determines the path for each allocated `Loc`.
  pub path:  Vec<Name>,
}


/// Wrapper for `parse_val::parse_val`. Transforms most Rust data
/// that derives `Debug` into a reflected `Val`.
pub fn reflect_val <V:Debug> (v:&V) -> Val { 
    use crate::parse_val::parse_val;
    parse_val(v)
}

/// Gives effects and traces for cleaning and dirtying, the engine's
/// internal DCG traversal/processing.  By contrast, the enclosing
/// module (`reflect`) only gives reflected versions of the DCG
/// itself, not changes that the engine makes to it.
pub mod trace {
    use std::fmt;
    use crate::engine;

  /// Distinguish fresh allocations from those that reuse an existing location.
  #[derive(Clone,Debug)]
  pub enum AllocCase {
    /// The allocation was **created** fresh; it was **not** reused.
    LocFresh,
    /// The allocation matched the location of a prior allocation. its
    /// content may or may not also match.  The `bool` indicates the two
    /// cases: `true` means same content, `false` means changed content.
    LocExists(ChangeFlag),      
  }

  #[derive(Clone,Debug)]
  pub enum ChangeFlag {
      ContentDiff,
      ContentSame,
  }

  /// Distinguish ref cell allocations from thunk allocations
  #[derive(Clone,Debug)]
  pub enum AllocKind {
    RefCell,
    Thunk,
  }

  /// When the program `force`s a computation, either the cache is
  /// either empty (`CacheMiss`) or non-empty (`CacheHit`).  The cached
  /// value may not be consistent without a cleaning.  When the program
  /// `force`s a reference cell, it simply gets the current value.
  #[derive(Clone,Debug)]
  pub enum ForceCase {
    /// The DCG has no cached value for this computation; no prior
    /// computation will be reused.
    CompCacheMiss,
    /// The DCG has a cached value for this computation; it may not be
    /// consistent without a cleaning first.
    CompCacheHit,
    /// The forced node is a ref cell with a (mutable) value, and hence,
    /// no computation was necessary.  The `force` simply gets the
    /// current value.
    RefGet,
  }

    /// For analytic cost analysis
    #[derive(Clone,Debug)]
    pub struct Cost {
        pub work:usize,
        pub depth:usize,
    }

  /// The effects of the DCG (including cleaning and dirtying) on one of
  /// its edges.
  #[derive(Clone,Debug)]
    pub enum Effect {
        /* Deprecated:
        
        /// Catch-all for user-defined (e.g., application-specific)
        /// effects, whose presence in the trace structure may aid
        /// debugging, visualization, analytic cost analysis, etc.
        UserEffect(Cost, String),
        */

        /// Debugging/visualization information: An ID (the optional
        /// name) and string.
        /// 
        // (Maybe this should be combined with `UserEffect`
        // someday?).
        Debug(Option<engine::Name>, Option<String>),

    /// Wrapper for Effect::Alloc; transition to DCG after the alloc.
    Alloc (AllocCase, AllocKind), 
    
    /// Wrapper for Effect::Force; transition to DCG after the force.
    Force (ForceCase),

    /// Transition to this edge as **dirty** (potentially inconsistent).
    /// This transition may consist of marking other edges dirty.  The
    /// DCG's invariant about dirty edges is simple: If an edge is
    /// dirty, then all edges that force (demand/observe) the source of
    /// that edge must also be dirty.  This transitive closure property
    /// ensures that we do not accidentally reuse stale cached values by
    /// mistake (a dirty edge will always witness a potentially stale
    /// cached value).
    Dirty, 
    
    /// Clean this edge, marking it **not dirty**, or equivalently,
    /// **definitely consistent**. Cleaning an edge consists of
    /// processing the edge's dirty transitive dependencies, if any.
    /// Recursively, this processing may consist of marking other edges
    /// clean (`CleanEdge`), and/or, removing edges (`Remove`) and
    /// replacing them via reevaluation under the current DCG state
    /// (`CleanEval`).  
    ///
    /// This process finishes in one of two mutually-exclusive
    /// situations: Either the edge is cleaned, meaning that its target
    /// need not be recomputed (`CleanEdge`). Or else, the value on this
    /// edge has changed, and this means that the edge's source is
    /// potentially affected by the change. Consequently, this change
    /// requires that the edge source be reevaluated (`CleanEval`).
    CleanRec,
    
    /// Transition to this edge as **clean** (definitely consistent),
    /// after doing a recursive cleaning of its dependencies and finding
    /// that they are clean.  This effect is mutually-exclusive with
    /// `CleanEval`, which occurs when `CleanEdge` **cannot** occur on
    /// an edge that is being recursively cleaned (via `CleanRec`).
    CleanEdge,
    
    /// Re-evaluate the previously-forced thunk that is the target of
    /// this edge, to clean it.  This effect is mutually-exclusive with
    /// `CleanEdge`.  It occurs when `CleanEdge` cannot occur.
    CleanEval,
    
    /// Transition to the DCG without this edge.  Perhaps it will be
    /// replaced via re-execution, sometime later.
    Remove,
  }
  
  /// An edge in the DCG, representing an effect of the incremental program.
  #[derive(Clone,Debug)]
  pub struct Edge {
    /// The source of the directed edge; it is actively _doing_ the
    /// effect of `succ.effect` to `succ.loc`.  `None` means the doer is
    /// the **editor**, who is not identified by any location. (The editor
    /// is not a node in the DCG, but rather, an actor operating outside
    /// of it).
    pub loc: Option<super::Loc>,
    /// The effect and target of the directed edge.
    pub succ: super::Succ,
  }
  
  /// `DCGTrace`: A Rose-tree of DCG edge-effects.  This tree structure
  /// allows the effects to have a a "time interval" that nests around
  /// and within the time intervals of other effects.
  #[derive(Clone,Debug)]
  pub struct Trace { 
    /// The DCG effect (e.g., Alloc(MatchDiff), Dirty, Clean, etc.)
    pub effect:Effect,
    
    /// The DCG edge on which this DCG effect takes place
    pub edge: EffectEdge,
    
    /// The DCG effects that occur subordinately as a result of this
    /// effect. (They begin after this effect begins, and the end before
    /// this effect ends).
    pub extent:Box<Vec<Trace>>,
  }

    /// Edge associated with an `Effect`.
    #[derive(Clone,Debug)]
    pub enum EffectEdge {
        /// Edge effect follows the edge "forward" to its target
        Fwd(Edge),
        /// Edge effect follows the edge "backward" to its source
        Bwd(Edge),
        /// No edge associated with Trace effect (e.g., `Debug`)
        None,
    }
    
    #[derive(Clone,Copy)]
    pub enum Role { Editor, Archivist }
    
    /// Count of effects and effect-patterns in trace.  For fields
    /// with two numbers, the first and second projection give the
    /// `Role::Editor` and `Role::Archivist` counts, respectively.
    #[derive(Clone)]
    pub struct TraceCount {
        pub total_updates:   usize,
        pub total_archivist: usize,
        pub reeval_nochange: usize,
        pub reeval_change:   usize,
        pub clean_rec:       usize,
        pub dirty:           (usize, usize),
        pub alloc_fresh:     (usize, usize),
        pub alloc_nochange:  (usize, usize),
        pub alloc_change:    (usize, usize),
    }
    
    pub fn trace_count_zero() -> TraceCount {
        TraceCount{ alloc_fresh:(0,0), 
                    alloc_change:(0,0), 
                    alloc_nochange:(0,0),
                    reeval_change:0, 
                    reeval_nochange:0, 
                    dirty:(0,0), 
                    clean_rec:0,
                    total_updates:0,
                    total_archivist:0,
        }
    }

    impl fmt::Debug for TraceCount {
        fn fmt(&self, f:&mut fmt::Formatter) -> fmt::Result {  
            write!(f,"\
                Trace counts:        {:>12} {:>12}
------------------------------------------------
editor:
  alloc_fresh:       {:>12} {:>12.2}
  alloc_nochange:    {:>12} {:>12.2}
  alloc_change:      {:>12} {:>12.2}
  dirty:             {:>12} {:>12.2} 
archivist:
  alloc_fresh:       {:>12} {:>12.2}
  alloc_nochange:    {:>12} {:>12.2}
  alloc_change:      {:>12} {:>12.2}
  dirty:             {:>12} {:>12.2}
  reeval:
    clean_rec:       {:>12} {:>12.2}
    reeval_nochange: {:>12} {:>12.2}
    reeval_change:   {:>12} {:>12.2}
",
                   format!("sum"),        format!("ave"),
                   // Editor
                   self.alloc_fresh.0,    self.alloc_fresh.0    as f32 / self.total_updates as f32,
                   self.alloc_nochange.0, self.alloc_nochange.0 as f32 / self.total_updates as f32,
                   self.alloc_change.0,   self.alloc_change.0   as f32 / self.total_updates as f32,
                   self.dirty.0,          self.dirty.0          as f32 / self.total_updates as f32,
                   // Archivist
                   self.alloc_fresh.1,    self.alloc_fresh.1    as f32 / self.total_updates as f32,
                   self.alloc_nochange.1, self.alloc_nochange.1 as f32 / self.total_updates as f32,
                   self.alloc_change.1,   self.alloc_change.1   as f32 / self.total_updates as f32,
                   self.dirty.1,          self.dirty.1          as f32 / self.total_updates as f32,
                   self.clean_rec,        self.clean_rec        as f32 / self.total_updates as f32,
                   self.reeval_nochange,  self.reeval_nochange  as f32 / self.total_updates as f32, 
                   self.reeval_change,    self.reeval_change    as f32 / self.total_updates as f32,
            )
        }
    }

    
    pub fn trace_count(trs:&Vec<Trace>, total_updates:Option<usize>) -> TraceCount {
        let mut trace_count = trace_count_zero();
        match total_updates {
            Some(x) => trace_count.total_updates = x,
            None    => trace_count.total_updates = 1,
        }
        for tr in trs {
            trace_count_rec(Role::Editor, tr, &mut trace_count)
        }
        return trace_count
    }

    fn trace_count_dirty(role:Role, tr:&Trace, c:&mut TraceCount) {
        match tr.effect {
            Effect::Dirty => match role { 
                Role::Editor    => c.dirty.0 += 1,
                Role::Archivist => c.dirty.1 += 1,
            },
            _ => println!("WARNING: unexpected effect: {:?}", tr), // XXX
        }
        for sub_tr in tr.extent.iter() {
            trace_count_dirty(role, sub_tr, c)
        }
    }

    pub fn trace_count_rec(role:Role, tr:&Trace, c:&mut TraceCount) {
        match role {
            Role::Editor =>
                match tr.effect {
                    Effect::Debug(_,_) |
                    // Effect::UserEffect(_,_) => {
                    //     for sub_tr in tr.extent.iter() { 
                    //         trace_count_rec(Role::Editor, sub_tr, c);
                    //     }                        
                    // },
                    Effect::Dirty => trace_count_dirty(role, tr, c),
                    Effect::Remove => unreachable!(),
                    Effect::CleanEdge => unreachable!(),
                    Effect::CleanEval => unreachable!(),
                    Effect::CleanRec => unreachable!(),
                    Effect::Alloc(AllocCase::LocFresh, _) => { 
                        c.alloc_fresh.0 += 1; 
                        assert!(tr.extent.len() == 0)  
                    }
                    Effect::Alloc(AllocCase::LocExists(ChangeFlag::ContentSame), _) => { 
                        c.alloc_nochange.0 += 1;
                        for sub_tr in tr.extent.iter() { trace_count_dirty(role, sub_tr, c) }
                    }
                    Effect::Alloc(AllocCase::LocExists(ChangeFlag::ContentDiff), _) => {
                        c.alloc_change.0 += 1;
                        for sub_tr in tr.extent.iter() { trace_count_dirty(role, sub_tr, c) }
                    }
                    Effect::Force(ForceCase::RefGet) => assert!(tr.extent.len() == 0),
                    Effect::Force(ForceCase::CompCacheHit)  |
                    Effect::Force(ForceCase::CompCacheMiss) => {
                        c.total_archivist += 1;
                        for sub_tr in tr.extent.iter() { 
                            trace_count_rec(Role::Archivist, sub_tr, c);
                        }
                    }
                },
            Role::Archivist =>
                match tr.effect {
                    Effect::Debug(_,_) |
                    // Effect::UserEffect(_,_) => {
                    //     for sub_tr in tr.extent.iter() { 
                    //         trace_count_rec(Role::Archivist, sub_tr, c);
                    //     }                        
                    // },
                    Effect::Dirty => trace_count_dirty(role, tr, c),
                    Effect::Remove => assert!(tr.extent.len() == 0),
                    Effect::CleanEdge => assert!(tr.extent.len() == 0),
                    Effect::CleanEval => {
                        let mut change = false;
                        for subtr in tr.extent.iter() {
                            match subtr.effect {
                                Effect::Debug(_,_) |
                                //Effect::UserEffect(_,_) => unreachable!(),
                                Effect::Remove => (),
                                Effect::CleanEdge => (),
                                Effect::CleanEval => (),
                                Effect::CleanRec => (),
                                Effect::Alloc(AllocCase::LocExists(ChangeFlag::ContentSame), _) => (),
                                Effect::Alloc(AllocCase::LocFresh, _)                           => change = true,
                                Effect::Alloc(AllocCase::LocExists(ChangeFlag::ContentDiff), _) => change = true,
                                Effect::Force(ForceCase::RefGet)        => (), /* XXX/TODO: This is undercounting?; do we need to know if gotten value was different? */
                                Effect::Force(ForceCase::CompCacheHit)  => (),
                                Effect::Force(ForceCase::CompCacheMiss) => change = true,
                                Effect::Dirty => change = true,
                            }
                        }
                        if change { c.reeval_change   += 1 }
                        else      { c.reeval_nochange += 1 };
                        for sub_tr in tr.extent.iter() { 
                            trace_count_rec(Role::Archivist, sub_tr, c);
                        }     
                    },
                    Effect::CleanRec => { 
                        c.clean_rec += 1;
                        for sub_tr in tr.extent.iter() { 
                            trace_count_rec(Role::Archivist, sub_tr, c);
                        }
                    }
                    Effect::Alloc(AllocCase::LocFresh, _) => {
                        c.alloc_fresh.1 += 1;
                        assert!(tr.extent.len() == 0);
                    }
                    Effect::Alloc(AllocCase::LocExists(ChangeFlag::ContentSame), _) => {
                        c.alloc_nochange.1 += 1;
                        for sub_tr in tr.extent.iter() { 
                            trace_count_rec(Role::Archivist, sub_tr, c);
                        }
                    }
                    Effect::Alloc(AllocCase::LocExists(ChangeFlag::ContentDiff), _) => {
                        c.alloc_change.1 += 1;
                        for sub_tr in tr.extent.iter() { 
                            trace_count_rec(Role::Archivist, sub_tr, c);
                        }                  
                    }
                    Effect::Force(ForceCase::RefGet) => {
                        assert!(tr.extent.len() == 0);
                    },
                    Effect::Force(ForceCase::CompCacheHit) => {
                        for sub_tr in tr.extent.iter() { 
                            trace_count_rec(Role::Archivist, sub_tr, c);
                        }
                    }
                    Effect::Force(ForceCase::CompCacheMiss) => {
                        for sub_tr in tr.extent.iter() { 
                            trace_count_rec(Role::Archivist, sub_tr, c);
                        }                  
                    }
                }
        }
    }
}