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
pub use crate::{
    prelude_internal::*,
    sub::{CowSub, Sub},
};
pub use std::{borrow::Cow, collections::HashMap};

// TODO: property test me for validity in unify tests
// TODO: property test me for commutativity in unify tests
// TODO: property test me for identity in unify tests
// TODO: property test me for unify_with vs .sub.unify in unify tests
pub trait Unify<K: Clone, V: Clone> {
    type Error;

    fn unify_with<'a>(&self, sub: CowSub<'a, K, V>, rhs: &Self) -> UResult<'a, K, V, Self::Error>;

    fn unify(&self, rhs: &Self) -> UResult<K, V, Self::Error> {
        self.unify_with(Cow::Owned(Sub::top()), rhs)
    }
}

// TODO: unit test me
// TODO: property test me
impl<K: Clone, V: Clone, E, T: Unify<K, V, Error = E>> Unify<K, V> for Option<T> {
    type Error = E;

    fn unify_with<'a>(&self, sub: CowSub<'a, K, V>, rhs: &Self) -> UResult<'a, K, V, Self::Error> {
        match (self, rhs) {
            (Some(l), Some(r)) => l.unify_with(sub, r),
            (None, None) => UOk(sub),
            _ => Bottom,
        }
    }
}

// TODO: unit test me
// TODO: property test me
impl<K: Clone, V: Clone, E, T: Unify<K, V, Error = E>> Unify<K, V> for Vec<T> {
    type Error = E;

    fn unify_with<'a>(
        &self,
        mut sub: CowSub<'a, K, V>,
        rhs: &Self,
    ) -> UResult<'a, K, V, Self::Error>
    {
        if self.len() != rhs.len() {
            return Bottom;
        }

        for (l, r) in self.iter().zip(rhs.iter()) {
            match l.unify_with(sub, r) {
                UOk(s) => sub = s,
                Bottom => return Bottom,
                UErr(e) => return UErr(e),
            }
        }

        UOk(sub)
    }
}

// TODO: unit test me
// TODO: property test me
impl<K: Clone, V: Clone, E, K2: Eq + Hash, V2: Unify<K, V, Error = E>> Unify<K, V>
    for HashMap<K2, V2>
{
    type Error = E;

    fn unify_with<'a>(
        &self,
        mut sub: CowSub<'a, K, V>,
        rhs: &Self,
    ) -> UResult<'a, K, V, Self::Error>
    {
        if self.len() != rhs.len() {
            return Bottom;
        }

        for (l, r) in self.iter().map(|(k, v)| (v, rhs.get(k))) {
            match r {
                Some(r) => match l.unify_with(sub, r) {
                    UOk(s) => sub = s,
                    Bottom => return Bottom,
                    UErr(e) => return UErr(e),
                },
                None => return Bottom,
            }
        }

        UOk(sub)
    }
}