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
use crate::{
    prelude_internal::*,
    sub::{CowSub, Sub, SubWith},
    unify::Unify,
    var::{FreeVars, FreshVars},
};
use std::{
    cell::Cell,
    collections::{HashMap, HashSet},
    convert::TryFrom,
    iter,
};

#[derive(Clone, Debug)]
pub enum Ast<L, V> {
    Lit(L),
    Var(Var<V>),
    List(Term<L, V>, Vec<Ast<L, V>>),
    /// NOTE: do NOT rely on unification order for this
    Dict(Term<L, V>, HashMap<L, Ast<L, V>>),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Term<L, V> {
    Lit(L),
    Var(Var<V>),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Var<V> {
    User(V),
    Auto(u32),
}

#[derive(Debug)]
pub struct VarSource(Cell<u32>);

#[derive(Debug, PartialEq)]
pub enum Error {
    BadTermSub, // Attempt to substitute an Ast for a Term
}

// TODO: unit test me
// TODO: property test m
impl<L: Eq + Hash, V: Eq + Hash> PartialEq<Ast<L, V>> for Ast<L, V> {
    fn eq(&self, rhs: &Self) -> bool {
        match (self, rhs) {
            (Ast::Lit(l), Ast::Lit(r)) => l.eq(r),
            (Ast::Var(l), Ast::Var(r)) => l.eq(r),
            (Ast::List(lt, ll), Ast::List(rt, rl)) => lt.eq(rt) && ll.eq(rl),
            (Ast::Dict(lt, ld), Ast::Dict(rt, rd)) => lt.eq(rt) && ld.eq(rd),
            (_, _) => false,
        }
    }
}

impl<L: Eq + Hash, V: Eq + Hash> Eq for Ast<L, V> {}

impl<L, V> From<Term<L, V>> for Ast<L, V> {
    fn from(term: Term<L, V>) -> Self {
        match term {
            Term::Lit(l) => Self::Lit(l),
            Term::Var(v) => Self::Var(v),
        }
    }
}

impl<L, V> From<Var<V>> for Ast<L, V> {
    fn from(var: Var<V>) -> Self { Self::Var(var) }
}

impl<L, V> TryFrom<Ast<L, V>> for Term<L, V> {
    type Error = Ast<L, V>;

    fn try_from(ast: Ast<L, V>) -> Result<Self, Self::Error> {
        match ast {
            Ast::Lit(l) => Ok(Term::Lit(l)),
            Ast::Var(v) => Ok(Term::Var(v)),
            a => Err(a),
        }
    }
}

// TODO: property test me
impl<L, V: Eq + Hash> FreeVars<Var<V>> for Ast<L, V> {
    fn free_vars_into<'a>(&'a self, set: &mut HashSet<&'a Var<V>>) {
        match self {
            Self::Lit(_) => (),
            Self::Var(v) => {
                set.insert(v);
            },
            Self::List(t, l) => {
                t.free_vars_into(set);
                l.free_vars_into(set);
            },
            Self::Dict(t, d) => {
                t.free_vars_into(set);
                d.free_vars_into(set);
            },
        }
    }
}

impl<L: Clone + Eq + Hash, V: Clone + Eq + Hash> SubWith<Var<V>, Ast<L, V>> for Ast<L, V> {
    type Error = Error;

    fn sub(&self, sub: &Sub<Var<V>, Ast<L, V>>) -> Result<Self, Self::Error> {
        match self {
            Self::Lit(l) => Ok(Self::Lit(l.clone())),
            Self::Var(v) => Ok(match sub.get(&v) {
                Some(a) => a.as_ref().clone(),
                None => Self::Var(v.clone()),
            }),
            Self::List(t, l) => t
                .sub(sub)
                .and_then(|t| l.sub(sub).map(|l| Self::List(t, l))),
            Self::Dict(t, d) => t
                .sub(sub)
                .and_then(|t| d.sub(sub).map(|d| Self::Dict(t, d))),
        }
    }
}

// TODO: property test me
impl<L: Clone + Eq + Hash, V: Clone + Eq + Hash> Unify<Var<V>, Ast<L, V>> for Ast<L, V> {
    type Error = Error;

    fn unify_with<'a>(
        &self,
        sub: CowSub<'a, Var<V>, Ast<L, V>>,
        rhs: &Self,
    ) -> UResult<'a, Var<V>, Ast<L, V>, Self::Error>
    {
        match (self, rhs) {
            (Self::Lit(l), Self::Lit(r)) if l == r => UOk(sub),
            (Self::Var(l), Self::Var(r)) if l == r => UOk(sub),
            (Self::Var(l), r) => match sub.get(l) {
                Some(a) => a.unify_with(sub, rhs),
                None => sub.with(l.clone(), r.clone()).into(),
            },
            (l, Self::Var(r)) => match sub.get(r) {
                Some(a) => l.unify_with(sub, a.as_ref()),
                None => sub.with(r.clone(), l.clone()).into(),
            },
            (Self::List(lt, ll), Self::List(rt, rl)) => {
                lt.unify_with(sub, rt).and_then(|s| ll.unify_with(s, rl))
            },
            (Self::Dict(lt, ld), Self::Dict(rt, rd)) => {
                lt.unify_with(sub, rt).and_then(|s| ld.unify_with(s, rd))
            },
            _ => Bottom,
        }
    }
}

impl<L, V> From<Var<V>> for Term<L, V> {
    fn from(var: Var<V>) -> Self { Self::Var(var) }
}

// TODO: property test me
impl<L, V: Eq + Hash> FreeVars<Var<V>> for Term<L, V> {
    fn free_vars_into<'a>(&'a self, set: &mut HashSet<&'a Var<V>>) {
        match self {
            Self::Lit(_) => (),
            Self::Var(v) => {
                set.insert(v);
            },
        }
    }
}

impl<L: Clone, V: Clone + Eq + Hash> SubWith<Var<V>, Ast<L, V>> for Term<L, V> {
    type Error = Error;

    fn sub(&self, sub: &Sub<Var<V>, Ast<L, V>>) -> Result<Self, Self::Error> {
        match self {
            Self::Lit(l) => Ok(Self::Lit(l.clone())),
            Self::Var(v) => match sub.get(&v) {
                Some(r) => match r.as_ref() {
                    Ast::Lit(l) => Ok(Term::Lit(l.clone())),
                    Ast::Var(v) => Ok(Term::Var(v.clone())),
                    _ => Err(Error::BadTermSub),
                },
                None => Ok(Self::Var(v.clone())),
            },
        }
    }
}

// TODO: property test me
impl<L: Clone + Eq + Hash, V: Clone + Eq + Hash> Unify<Var<V>, Ast<L, V>> for Term<L, V> {
    type Error = Error;

    fn unify_with<'a>(
        &self,
        sub: CowSub<'a, Var<V>, Ast<L, V>>,
        rhs: &Self,
    ) -> UResult<'a, Var<V>, Ast<L, V>, Self::Error>
    {
        match (self, rhs) {
            (Term::Lit(l), Term::Lit(r)) if l == r => UOk(sub),
            (Term::Var(l), Term::Var(r)) if l == r => UOk(sub),
            (Term::Var(l), r) => match sub.get(l) {
                Some(a) => match a.as_ref() {
                    Ast::Lit(l) => Term::Lit(l.clone()).unify_with(sub, rhs),
                    Ast::Var(v) => Term::Var(v.clone()).unify_with(sub, rhs),
                    _ => UErr(Error::BadTermSub),
                },
                None => sub.with(l.clone(), r.clone().into()).into(),
            },
            (l, Term::Var(r)) => match sub.get(r) {
                Some(a) => match a.as_ref() {
                    Ast::Lit(i) => l.unify_with(sub, &Term::Lit(i.clone())),
                    Ast::Var(v) => l.unify_with(sub, &Term::Var(v.clone())),
                    _ => UErr(Error::BadTermSub),
                },
                None => sub.with(r.clone(), l.clone().into()).into(),
            },
            _ => Bottom,
        }
    }
}

impl<V: Eq + Hash> FreeVars<Var<V>> for Var<V> {
    fn free_vars_into<'a>(&'a self, set: &mut HashSet<&'a Var<V>>) { set.insert(self); }

    // Overriding this for the world's slightest performance boost by hopefully
    // using the size hint of the iterator
    fn free_vars(&self) -> HashSet<&Var<V>> { iter::once(self).collect() }
}

impl VarSource {
    pub fn new() -> Self { Self(Cell::new(0)) }
}

// TODO: property test me
impl<V> FreshVars<Var<V>> for VarSource {
    fn acquire(&self) -> Var<V> {
        // let curr = self.0.update(|i| i + 1);
        let curr = self.0.get();
        self.0.set(curr + 1);

        Var::Auto(curr)
    }
}