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
use std::fmt;

use binder::Binder;
use bound_var::BoundVar;
use free_var::FreeVar;

/// A variable that can either be free or bound
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum Var<N> {
    /// A free variable
    Free(FreeVar<N>),
    /// A variable that is bound by a scope
    Bound(BoundVar<N>),
}

impl<N> Var<N> {
    pub fn pretty_name(&self) -> Option<&N> {
        match *self {
            Var::Bound(ref bound_var) => bound_var.pretty_name.as_ref(),
            Var::Free(ref free_var) => free_var.pretty_name.as_ref(),
        }
    }
}

impl<N> Eq for Var<N> where N: Eq {}

impl<N: fmt::Display> fmt::Display for Var<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Var::Bound(ref bound_var) => write!(f, "{}", bound_var),
            Var::Free(ref free_var) => write!(f, "{}", free_var),
        }
    }
}

impl<N> PartialEq<Binder<N>> for Var<N>
where
    N: PartialEq,
{
    fn eq(&self, other: &Binder<N>) -> bool {
        match self {
            Var::Free(ref lhs) => other == lhs,
            _ => false,
        }
    }
}

impl<N> PartialEq<FreeVar<N>> for Var<N>
where
    N: PartialEq,
{
    fn eq(&self, other: &FreeVar<N>) -> bool {
        match *self {
            Var::Free(ref lhs) => lhs == other,
            _ => false,
        }
    }
}

impl<N> PartialEq<Var<N>> for FreeVar<N>
where
    N: PartialEq,
{
    fn eq(&self, other: &Var<N>) -> bool {
        match *other {
            Var::Free(ref lhs) => lhs == self,
            _ => false,
        }
    }
}

impl<N> PartialEq<Var<N>> for Binder<N>
where
    N: PartialEq,
{
    fn eq(&self, other: &Var<N>) -> bool {
        match other {
            Var::Free(ref rhs) => self == rhs,
            _ => false,
        }
    }
}