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
use std::fmt;
use std::ops;
use free_var::FreeVar;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct BinderOffset(pub u32);
impl BinderOffset {
pub fn to_usize(self) -> usize {
self.0 as usize
}
}
impl ops::Add for BinderOffset {
type Output = BinderOffset;
fn add(self, other: BinderOffset) -> BinderOffset {
BinderOffset(self.0 + other.0)
}
}
impl ops::AddAssign for BinderOffset {
fn add_assign(&mut self, other: BinderOffset) {
self.0 += other.0;
}
}
impl ops::Sub for BinderOffset {
type Output = BinderOffset;
fn sub(self, other: BinderOffset) -> BinderOffset {
BinderOffset(self.0 - other.0)
}
}
impl ops::SubAssign for BinderOffset {
fn sub_assign(&mut self, other: BinderOffset) {
self.0 -= other.0;
}
}
impl fmt::Display for BinderOffset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct BinderIndex(pub BinderOffset);
impl BinderIndex {
pub fn to_usize(self) -> usize {
self.0.to_usize()
}
}
impl ops::Add<BinderOffset> for BinderIndex {
type Output = BinderIndex;
fn add(self, other: BinderOffset) -> BinderIndex {
BinderIndex(self.0 + other)
}
}
impl ops::AddAssign<BinderOffset> for BinderIndex {
fn add_assign(&mut self, other: BinderOffset) {
self.0 += other;
}
}
impl fmt::Display for BinderIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Binder<N>(pub FreeVar<N>);
impl<N> Binder<N> {
pub fn user<T: Into<N>>(ident: T) -> Binder<N> {
Binder(FreeVar::user(ident))
}
pub fn freshen(self) -> Binder<N> {
Binder(self.0.freshen())
}
}
impl<N: fmt::Display> fmt::Display for Binder<N> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<N> PartialEq<FreeVar<N>> for Binder<N>
where
N: PartialEq,
{
fn eq(&self, other: &FreeVar<N>) -> bool {
self.0 == *other
}
}