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
//! Record sequences of rewrites.
//!
//! # Examples
//!
//! A TRS with mutually exclusive rules gives a straightfoward linear record of
//! rewrites:
//!
//! ```
//! use term_rewriting::{parse, trace::Trace, Signature, Strategy};
//!
//! let mut sig = Signature::default();
//! let inp = "
//!     PLUS(SUCC(x_) y_) = PLUS(x_ SUCC(y_));
//!     PLUS(ZERO x_) = x_;
//!
//!     PLUS(SUCC(SUCC(SUCC(ZERO))) SUCC(ZERO));"
//!     .trim();
//! let (trs, mut terms) = parse(&mut sig, inp).unwrap();
//! let mut term = terms.pop().unwrap();
//! let mut trace = Trace::new(&trs, &term, 0.5, 1.0, None, Strategy::Normal);
//!
//! let expected = vec!["PLUS(3, 1)", "PLUS(2, 2)", "PLUS(1, 3)", "PLUS(0, 4)", "4"];
//! let got = trace
//!     .by_ref()
//!     .take(5)
//!     .map(|n| n.term().pretty())
//!     .collect::<Vec<_>>();
//! assert_eq!(got, expected);
//! assert!(trace.next().is_none());
//! ```

use rand::{
    distributions::{Distribution, Uniform},
    Rng,
};
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::f64;
use std::sync::{Arc, RwLock, Weak};

use {Strategy, Term, TRS};

/// A `Trace` provides first-class control over [`Term`] rewriting.
///
/// It gives access to each evaluation step which has already occurred or could immediately descend
/// from a previously occurring step and provides tools for introducing new evaluation steps.
///
/// Each evaluation step from a source term gets yielded via the [`Iterator` implementation].
///
/// [`Term`]: ../enum.Term.html
/// [`Iterator` implementation]: #impl-Iterator
pub struct Trace<'a> {
    trs: &'a TRS,
    root: TraceNode,
    unobserved: BinaryHeap<TraceNode>,
    p_observe: f64,
    noise_level: f64,
    max_term_size: Option<usize>,
    strategy: Strategy,
}
impl<'a> Trace<'a> {
    /// Create a trace initialized with `term` undergoing rewrites in `trs`. Every step for a node
    /// in the trace multiplies the node's probability score by `p_observe`.
    pub fn new(
        trs: &'a TRS,
        term: &Term,
        p_observe: f64,
        noise_level: f64,
        max_term_size: Option<usize>,
        strategy: Strategy,
    ) -> Trace<'a> {
        let root = TraceNode::new(term.clone(), TraceState::Unobserved, 0.0, 0, None, vec![]);
        let mut unobserved = BinaryHeap::new();
        unobserved.push(root.clone());
        Trace {
            trs,
            root,
            unobserved,
            p_observe,
            noise_level,
            max_term_size,
            strategy,
        }
    }
    fn new_node(
        &mut self,
        term: Term,
        parent: Option<&TraceNode>,
        depth: usize,
        state: TraceState,
        log_p: f64,
    ) -> TraceNode {
        let node = TraceNode::new(term, state, log_p, depth, parent, vec![]);
        self.unobserved.push(node.clone());
        node
    }
    /// The initial node of the `Trace`.
    pub fn root(&self) -> &TraceNode {
        &self.root
    }
    /// The length of the longest chain of evaluation steps.
    pub fn depth(&self) -> usize {
        let mut deepest = 0;
        self.root.walk(|n| {
            deepest = deepest.max(n.depth());
        });
        deepest
    }
    /// The total count of nodes maintained by the `Trace`.
    pub fn size(&self) -> usize {
        let mut count = 0;
        self.root.walk(|_| count += 1);
        count
    }
    /// The probability mass that has been explored by the Trace.
    pub fn mass(&self) -> f64 {
        // the mass is 1 - the mass in unobserved leaves
        let mut masses = self.root.accumulate(|n| {
            if [TraceState::Unobserved].contains(&n.state()) && n.is_leaf() {
                Some(n.log_p())
            } else {
                None
            }
        });
        masses.push(f64::NEG_INFINITY);
        1.0 - logsumexp(masses.as_slice()).exp()
    }
    /// Give all possible outcomes within `max_steps` of evaluation.
    pub fn outcomes(&mut self, max_steps: usize) -> Vec<TraceNode> {
        self.rewrite(max_steps);
        self.root.leaves(&[TraceState::Normal])
    }
    /// Sample a possible outcome in proportion to its probability.
    pub fn sample<R: Rng>(&self, rng: &mut R) -> TraceNode {
        let leaves = self.root.leaves(&[TraceState::Normal]);
        let ws = leaves.iter().map(|x| x.log_p().exp()).collect::<Vec<f64>>();
        weighted_sample(rng, &leaves, &ws).clone()
    }
    /// Run the `Trace` until no further steps are possible, or until `max_steps` is reached.
    pub fn rewrite(&mut self, max_steps: usize) {
        if max_steps > self.size() {
            let n_steps = max_steps - self.size();
            self.nth(n_steps);
        }
    }
    /// A lower bound on the probability that `self` rewrites to `term`.
    pub fn rewrites_to(&mut self, max_steps: usize, term: &Term) -> f64 {
        // NOTE: we use Term::shared_score rather than equality
        self.rewrite(max_steps);
        let lps = self.root.accumulate(|n| {
            let n_r = &n.0.read().expect("poisoned TraceNode");
            let ln_p = n_r.log_p;
            let score = Term::shared_score(term, &n_r.term);
            let ln_adj_score = score.powf(self.noise_level).ln();
            Some(ln_p + ln_adj_score)
        });
        if lps.is_empty() {
            f64::NEG_INFINITY
        } else {
            logsumexp(&lps)
        }
    }
}
impl<'a> Iterator for Trace<'a> {
    type Item = TraceNode;
    /// Record single-step rewrites on the highest-scoring unobserved node. `None` if there are no
    /// remaining unobserved nodes.
    fn next(&mut self) -> Option<TraceNode> {
        if let Some(node) = self.unobserved.pop() {
            // scope in for write access to node
            {
                let mut node_w = node.0.write().expect("poisoned TraceNode");
                match self.max_term_size {
                    Some(max_term_size) if node_w.term.size() > max_term_size => {
                        node_w.state = TraceState::TooBig;
                    }
                    _ => match self.trs.rewrite(&node_w.term, self.strategy) {
                        None => node_w.state = TraceState::Normal,
                        Some(ref rewrites) if rewrites.is_empty() => {
                            node_w.state = TraceState::Normal
                        }
                        Some(rewrites) => {
                            let term_selection_p = -(rewrites.len() as f64).ln();
                            node_w.log_p += self.p_observe.ln();
                            node_w.state = TraceState::Rewritten;
                            for term in rewrites {
                                let new_p = node_w.log_p + term_selection_p;
                                let new_node = self.new_node(
                                    term,
                                    Some(&node),
                                    node_w.depth + 1,
                                    TraceState::Unobserved,
                                    new_p,
                                );
                                node_w.children.push(new_node);
                            }
                        }
                    },
                }
            }
            Some(node)
        } else {
            None
        }
    }
}

/// All the data pertaining to a single evaluation step in a Trace.
#[derive(Debug, Clone)]
struct TraceNodeStore {
    term: Term,
    state: TraceState,
    log_p: f64,
    depth: usize,
    parent: Option<Weak<RwLock<TraceNodeStore>>>,
    children: Vec<TraceNode>,
}

/// The state of a [`Term`] at a particular evaluation step in a [`Trace`].
///
/// [`Term`]: ../enum.Term.html
/// [`Trace`]: struct.Trace.html
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TraceState {
    /// For a [`TraceNode`] which is part of the [`Trace`] but has not yet itself been examined or
    /// rewritten.
    ///
    /// [`Trace`]: struct.Trace.html
    /// [`TraceNode`]: struct.TraceNode.html
    Unobserved,
    /// For a [`TraceNode`] which has been examined and found to have a [`Term`] too large for
    /// efficient rewriting.
    ///
    /// [`Term`]: ../struct.Term.html
    /// [`TraceNode`]: struct.TraceNode.html
    TooBig,
    /// For a [`TraceNode`] which has been examined and whose [`Term`] is already in normal form.
    ///
    /// [`Term`]: ../struct.Term.html
    /// [`TraceNode`]: struct.TraceNode.html
    Normal,
    /// For a [`TraceNode`] which has been examined and is no longer a leaf node.
    ///
    /// [`TraceNode`]: struct.TraceNode.html
    Rewritten,
}

/// A `TraceNode` describes a specific evaluation step in the [`Trace`].
///
/// [`Trace`]: struct.Trace.html
#[derive(Debug, Clone)]
pub struct TraceNode(Arc<RwLock<TraceNodeStore>>);
impl TraceNode {
    fn new(
        term: Term,
        state: TraceState,
        log_p: f64,
        depth: usize,
        parent: Option<&TraceNode>,
        children: Vec<TraceNode>,
    ) -> TraceNode {
        let parent = parent.map(|n| Arc::downgrade(&n.0));
        TraceNode(Arc::new(RwLock::new(TraceNodeStore {
            term,
            state,
            log_p,
            depth,
            parent,
            children,
        })))
    }
    /// The [`TraceState`] associated with this evaluation step.
    ///
    /// [`TraceState`]: enum.TraceState.html
    pub fn state(&self) -> TraceState {
        self.0.read().expect("poisoned TraceNode").state
    }
    /// The [`Term`] associated with this evaluation step.
    ///
    /// [`Term`]: ../enum.Term.html
    pub fn term(&self) -> Term {
        self.0.read().expect("poisoned TraceNode").term.clone()
    }
    /// The log probability of reaching this particular evaluation step.
    pub fn log_p(&self) -> f64 {
        self.0.read().expect("poisoned TraceNode").log_p
    }
    /// The depth (i.e. number of previous evaluation steps) of this evaluation step.
    pub fn depth(&self) -> usize {
        self.0.read().expect("poisoned TraceNode").depth
    }
    /// The parent `TraceNode` of this evaluation step.
    pub fn parent(&self) -> Option<TraceNode> {
        self.0
            .read()
            .expect("poisoned TraceNode")
            .parent
            .as_ref()
            .and_then(Weak::upgrade)
            .map(TraceNode)
    }
    /// The children `TraceNode`s of this evaluation step.
    pub fn children(&self) -> Vec<TraceNode> {
        self.0.read().expect("poisoned TraceNode").children.clone()
    }
    /// Whether this evaluation step has no associated children.
    pub fn is_leaf(&self) -> bool {
        self.0
            .read()
            .expect("poisoned TraceNode")
            .children
            .is_empty()
    }

    fn accumulate<T, F>(&self, condition: F) -> Vec<T>
    where
        F: Fn(&TraceNode) -> Option<T>,
    {
        let mut values = Vec::new();
        self.walk(|n| {
            if let Some(v) = condition(n) {
                values.push(v)
            }
        });
        values
    }
    fn walk<F>(&self, mut callback: F)
    where
        F: FnMut(&TraceNode),
    {
        self.walk_helper(&mut callback)
    }
    fn walk_helper<F>(&self, callback: &mut F)
    where
        F: FnMut(&TraceNode),
    {
        callback(self);
        for child in &self.0.read().expect("poisoned TraceNode").children {
            child.walk_helper(callback)
        }
    }
    /// Returns an iterator over all nodes that descend from this node.
    pub fn iter(&self) -> TraceNodeIter {
        TraceNodeIter::new(self.clone())
    }

    /// All the nodes descending from this node.
    pub fn progeny(&self, states: &[TraceState]) -> Vec<TraceNode> {
        self.accumulate(|n| {
            if states.contains(&n.state()) {
                Some(n.clone())
            } else {
                None
            }
        })
    }
    /// All the leaf nodes that descend from this node.
    pub fn leaves(&self, states: &[TraceState]) -> Vec<TraceNode> {
        self.accumulate(|n| {
            if states.contains(&n.state()) && n.is_leaf() {
                Some(n.clone())
            } else {
                None
            }
        })
    }
    /// Like `leaves`, but returns `Term`s instead of `TraceNodes`s.
    pub fn leaf_terms(&self, states: &[TraceState]) -> Vec<Term> {
        self.accumulate(|n| {
            if states.contains(&n.state()) && n.is_leaf() {
                Some(n.term())
            } else {
                None
            }
        })
    }
}
impl PartialEq for TraceNode {
    fn eq(&self, other: &TraceNode) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}
impl Eq for TraceNode {}
impl PartialOrd for TraceNode {
    fn partial_cmp(&self, other: &TraceNode) -> Option<Ordering> {
        self.log_p().partial_cmp(&other.log_p())
    }
}
impl Ord for TraceNode {
    fn cmp(&self, other: &TraceNode) -> Ordering {
        if let Some(x) = self.partial_cmp(&other) {
            x
        } else {
            Ordering::Equal
        }
    }
}
impl<'a> IntoIterator for &'a TraceNode {
    type Item = TraceNode;
    type IntoIter = TraceNodeIter;
    fn into_iter(self) -> TraceNodeIter {
        self.iter()
    }
}
impl IntoIterator for TraceNode {
    type Item = TraceNode;
    type IntoIter = TraceNodeIter;
    fn into_iter(self) -> TraceNodeIter {
        TraceNodeIter::new(self)
    }
}

pub struct TraceNodeIter {
    queue: Vec<TraceNode>,
}
impl TraceNodeIter {
    fn new(root: TraceNode) -> TraceNodeIter {
        TraceNodeIter { queue: vec![root] }
    }
}
impl Iterator for TraceNodeIter {
    type Item = TraceNode;
    fn next(&mut self) -> Option<TraceNode> {
        let node = self.queue.pop()?;
        self.queue.append(&mut node.children());
        Some(node)
    }
}

fn logsumexp(lps: &[f64]) -> f64 {
    let largest = lps.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
    if largest == f64::NEG_INFINITY {
        f64::NEG_INFINITY
    } else {
        let x = lps.iter().map(|lp| (lp - largest).exp()).sum::<f64>().ln();
        largest + x
    }
}

/// Samples an item from `xs` given the weights `ws`.
fn weighted_sample<'a, T, R: Rng>(rng: &mut R, xs: &'a [T], ws: &[f64]) -> &'a T {
    assert_eq!(xs.len(), ws.len(), "weighted sample given invalid inputs");
    let total = ws.iter().fold(0f64, |acc, x| acc + x);
    let threshold: f64 = Uniform::new(0f64, total).sample(rng);
    let mut cum = 0f64;
    for (wp, x) in ws.iter().zip(xs) {
        cum += *wp;
        if threshold <= cum {
            return x;
        }
    }
    unreachable!()
}