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
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::marker;
use std::fmt::Debug;
use std::marker::PhantomData;
use snapshot_vec as sv;

#[cfg(test)]
mod tests;

/// This trait is implemented by any type that can serve as a type
/// variable. We call such variables *unification keys*. For example,
/// this trait is implemented by `IntVid`, which represents integral
/// variables.
///
/// Each key type has an associated value type `V`. For example, for
/// `IntVid`, this is `Option<IntVarValue>`, representing some
/// (possibly not yet known) sort of integer.
///
/// Clients are expected to provide implementations of this trait; you
/// can see some examples in the `test` module.
pub trait UnifyKey : Copy + Clone + Debug + PartialEq {
    type Value: UnifyValue;

    fn index(&self) -> u32;

    fn from_index(u: u32) -> Self;

    fn tag() -> &'static str;
}

pub trait UnifyValue: Clone + Debug {
    /// Given two values, produce a new value that combines them.
    /// If that is not possible, produce an error.
    fn unify_values(value1: &Self, value2: &Self) -> Result<Self, (Self, Self)>;
}

/// Marker trait which indicates that `UnifyValues::unify_values` will never return `Err`.
pub trait InfallibleUnifyValue: UnifyValue {
}

/// Value of a unification key. We implement Tarjan's union-find
/// algorithm: when two keys are unified, one of them is converted
/// into a "redirect" pointing at the other. These redirects form a
/// DAG: the roots of the DAG (nodes that are not redirected) are each
/// associated with a value of type `V` and a rank. The rank is used
/// to keep the DAG relatively balanced, which helps keep the running
/// time of the algorithm under control. For more information, see
/// <http://en.wikipedia.org/wiki/Disjoint-set_data_structure>.
#[derive(PartialEq,Clone,Debug)]
struct VarValue<K: UnifyKey> {
    parent: K, // if equal to self, this is a root
    value: K::Value, // value assigned (only relevant to root)
    child: K, // if equal to self, no child (relevant to both root/redirect)
    sibling: K, // if equal to self, no sibling (only relevant to redirect)
    rank: u32, // max depth (only relevant to root)
}

/// Table of unification keys and their values.
pub struct UnificationTable<K: UnifyKey> {
    /// Indicates the current value of each key.
    values: sv::SnapshotVec<Delegate<K>>,
}

/// At any time, users may snapshot a unification table.  The changes
/// made during the snapshot may either be *committed* or *rolled back*.
pub struct Snapshot<K: UnifyKey> {
    // Link snapshot to the key type `K` of the table.
    marker: marker::PhantomData<K>,
    snapshot: sv::Snapshot,
}

#[derive(Copy, Clone)]
struct Delegate<K>(PhantomData<K>);

impl<K: UnifyKey> VarValue<K> {
    fn new_var(key: K, value: K::Value) -> VarValue<K> {
        VarValue::new(key, value, key, key, 0)
    }

    fn new(parent: K, value: K::Value, child: K, sibling: K, rank: u32) -> VarValue<K> {
        VarValue {
            parent: parent, // this is a root
            value: value,
            child: child,
            sibling: sibling,
            rank: rank,
        }
    }

    fn redirect(&mut self, to: K, sibling: K) {
        assert_eq!(self.parent, self.sibling); // ...since this used to be a root
        self.parent = to;
        self.sibling = sibling;
    }

    fn root(&mut self, rank: u32, child: K, value: K::Value) {
        self.rank = rank;
        self.child = child;
        self.value = value;
    }

    /// Returns the key of this node. Only valid if this is a root
    /// node, which you yourself must ensure.
    fn key(&self) -> K {
        self.parent
    }

    fn parent(&self, self_key: K) -> Option<K> {
        self.if_not_self(self.parent, self_key)
    }

    fn child(&self, self_key: K) -> Option<K> {
        self.if_not_self(self.child, self_key)
    }

    fn sibling(&self, self_key: K) -> Option<K> {
        self.if_not_self(self.sibling, self_key)
    }

    fn if_not_self(&self, key: K, self_key: K) -> Option<K> {
        if key == self_key {
            None
        } else {
            Some(key)
        }
    }
}

// We can't use V:LatticeValue, much as I would like to,
// because frequently the pattern is that V=Option<U> for some
// other type parameter U, and we have no way to say
// Option<U>:LatticeValue.

impl<K: UnifyKey> UnificationTable<K> {
    pub fn new() -> UnificationTable<K> {
        UnificationTable { values: sv::SnapshotVec::new() }
    }

    /// Starts a new snapshot. Each snapshot must be either
    /// rolled back or committed in a "LIFO" (stack) order.
    pub fn snapshot(&mut self) -> Snapshot<K> {
        Snapshot {
            marker: marker::PhantomData::<K>,
            snapshot: self.values.start_snapshot(),
        }
    }

    /// Reverses all changes since the last snapshot. Also
    /// removes any keys that have been created since then.
    pub fn rollback_to(&mut self, snapshot: Snapshot<K>) {
        debug!("{}: rollback_to()", K::tag());
        self.values.rollback_to(snapshot.snapshot);
    }

    /// Commits all changes since the last snapshot. Of course, they
    /// can still be undone if there is a snapshot further out.
    pub fn commit(&mut self, snapshot: Snapshot<K>) {
        debug!("{}: commit()", K::tag());
        self.values.commit(snapshot.snapshot);
    }

    pub fn new_key(&mut self, value: K::Value) -> K {
        let len = self.values.len();
        let key: K = UnifyKey::from_index(len as u32);
        self.values.push(VarValue::new_var(key, value));
        debug!("{}: created new key: {:?}", K::tag(), key);
        key
    }

    pub fn unioned_keys(&mut self, key: K) -> UnionedKeys<K> {
        let root_key = self.get_root_key(key);
        UnionedKeys {
            table: self,
            stack: vec![root_key],
        }
    }

    fn value(&self, key: K) -> &VarValue<K> {
        &self.values[key.index() as usize]
    }

    /// Find the root node for `vid`. This uses the standard
    /// union-find algorithm with path compression:
    /// <http://en.wikipedia.org/wiki/Disjoint-set_data_structure>.
    ///
    /// NB. This is a building-block operation and you would probably
    /// prefer to call `probe` below.
    fn get_root_key(&mut self, vid: K) -> K {
        let redirect = {
            match self.value(vid).parent(vid) {
                None => return vid,
                Some(redirect) => redirect,
            }
        };

        let root_key: K = self.get_root_key(redirect);
        if root_key != redirect {
            // Path compression
            self.update_value(vid, |value| value.parent = root_key);
        }

        root_key
    }

    fn is_root(&self, key: K) -> bool {
        let index = key.index() as usize;
        self.values.get(index).parent(key).is_none()
    }

    fn update_value<OP>(&mut self, key: K, op: OP)
        where OP: FnOnce(&mut VarValue<K>)
    {
        self.values.update(key.index() as usize, op);
        debug!("Updated variable {:?} to {:?}", key, self.value(key));
    }

    /// Either redirects `node_a` to `node_b` or vice versa, depending
    /// on the relative rank. The value associated with the new root
    /// will be `new_value`.
    ///
    /// NB: This is the "union" operation of "union-find". It is
    /// really more of a building block. If the values associated with
    /// your key are non-trivial, you would probably prefer to call
    /// `unify_var_var` below.
    fn unify_roots(&mut self, key_a: K, key_b: K, new_value: K::Value) {
        debug!("unify(key_a={:?}, key_b={:?})",
               key_a,
               key_b);

        let rank_a = self.value(key_a).rank;
        let rank_b = self.value(key_b).rank;
        if rank_a > rank_b {
            // a has greater rank, so a should become b's parent,
            // i.e., b should redirect to a.
            self.redirect_root(rank_a, key_b, key_a, new_value);
        } else if rank_a < rank_b {
            // b has greater rank, so a should redirect to b.
            self.redirect_root(rank_b, key_a, key_b, new_value);
        } else {
            // If equal, redirect one to the other and increment the
            // other's rank.
            self.redirect_root(rank_a + 1, key_a, key_b, new_value);
        }
    }

    fn redirect_root(&mut self,
                     new_rank: u32,
                     old_root_key: K,
                     new_root_key: K,
                     new_value: K::Value) {
        let sibling = self.value(new_root_key).child(new_root_key)
                                              .unwrap_or(old_root_key);
        self.update_value(old_root_key, |old_root_value| {
            old_root_value.redirect(new_root_key, sibling);
        });
        self.update_value(new_root_key, |new_root_value| {
            new_root_value.root(new_rank, old_root_key, new_value);
        });
    }
}

impl<K: UnifyKey> sv::SnapshotVecDelegate for Delegate<K> {
    type Value = VarValue<K>;
    type Undo = ();

    fn reverse(_: &mut Vec<VarValue<K>>, _: ()) {}
}

/// ////////////////////////////////////////////////////////////////////////
/// Iterator over keys that have been unioned together

pub struct UnionedKeys<'a, K>
    where K: UnifyKey + 'a,
          K::Value: 'a
{
    table: &'a mut UnificationTable<K>,
    stack: Vec<K>,
}

impl<'a, K> UnionedKeys<'a, K>
    where K: UnifyKey,
          K::Value: 'a
{
    fn var_value(&self, key: K) -> VarValue<K> {
        self.table.value(key).clone()
    }
}

impl<'a, K: 'a> Iterator for UnionedKeys<'a, K>
    where K: UnifyKey,
          K::Value: 'a
{
    type Item = K;

    fn next(&mut self) -> Option<K> {
        let key = match self.stack.last() {
            Some(k) => *k,
            None => {
                return None;
            }
        };

        let vv = self.var_value(key);

        match vv.child(key) {
            Some(child_key) => {
                self.stack.push(child_key);
            }

            None => {
                // No child, push a sibling for the current node.  If
                // current node has no siblings, start popping
                // ancestors until we find an aunt or uncle or
                // something to push. Note that we have the invariant
                // that for every node N that we reach by popping
                // items off of the stack, we have already visited all
                // children of N.
                while let Some(ancestor_key) = self.stack.pop() {
                    let ancestor_vv = self.var_value(ancestor_key);
                    match ancestor_vv.sibling(ancestor_key) {
                        Some(sibling) => {
                            self.stack.push(sibling);
                            break;
                        }
                        None => {}
                    }
                }
            }
        }

        Some(key)
    }
}

/// ////////////////////////////////////////////////////////////////////////
/// Public API

impl<'tcx, K, V> UnificationTable<K>
    where K: UnifyKey<Value = V>,
          V: UnifyValue,
{
    /// Unions two keys without the possibility of failure; only
    /// applicable to InfallibleUnifyValue.
    pub fn union(&mut self, a_id: K, b_id: K)
        where V: InfallibleUnifyValue
    {
        self.unify_var_var(a_id, b_id).unwrap();
    }

    /// Given two keys, indicates whether they have been unioned together.
    pub fn unioned(&mut self, a_id: K, b_id: K) -> bool {
        self.find(a_id) == self.find(b_id)
    }

    /// Given a key, returns the (current) root key.
    pub fn find(&mut self, id: K) -> K {
        self.get_root_key(id)
    }

    pub fn unify_var_var(&mut self, a_id: K, b_id: K) -> Result<(), (V, V)> {
        let root_a = self.get_root_key(a_id);
        let root_b = self.get_root_key(b_id);

        if root_a == root_b {
            return Ok(());
        }

        let combined = try!(V::unify_values(&self.value(root_a).value, &self.value(root_b).value));

        Ok(self.unify_roots(root_a, root_b, combined))
    }

    /// Sets the value of the key `a_id` to `b`, attempting to merge
    /// with the previous value.
    pub fn unify_var_value(&mut self, a_id: K, b: V) -> Result<(), (V, V)> {
        let root_a = self.get_root_key(a_id);
        let value = try!(V::unify_values(&self.value(root_a).value, &b));
        self.update_value(root_a, |node| node.value = value);
        Ok(())
    }

    pub fn probe_value(&mut self, id: K) -> V {
        let id = self.get_root_key(id);
        self.value(id).value.clone()
    }
}


///////////////////////////////////////////////////////////////////////////

impl UnifyValue for () {
    fn unify_values(_: &(), _: &()) -> Result<(), ((), ())> {
        Ok(())
    }
}

impl InfallibleUnifyValue for () {
}

impl<V: UnifyValue> UnifyValue for Option<V> {
    fn unify_values(a: &Option<V>, b: &Option<V>) -> Result<Self, (Self, Self)> {
        match (a, b) {
            (&None, &None) => Ok(None),
            (&Some(ref v), &None) | (&None, &Some(ref v)) => Ok(Some(v.clone())),
            (&Some(ref a), &Some(ref b)) => {
                match V::unify_values(a, b) {
                    Ok(v) => Ok(Some(v)),
                    Err((a, b)) => Err((Some(a), Some(b))),
                }
            }
        }
    }
}

impl<V: InfallibleUnifyValue> InfallibleUnifyValue for Option<V> {
}