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
use std::marker::PhantomData;
use std::fmt;
mod aux {
pub trait IdentityAux<A: ?Sized, B: ?Sized> {
type InverseAux: ?Sized + super::Identity<B, A, InverseAux = Self>;
fn conv_aux(&self, x: A) -> B where A: Sized, B: Sized;
fn conv_ref_aux<'a>(&self, x: &'a A) -> &'a B;
fn conv_mut_aux<'a>(&self, x: &'a mut A) -> &'a mut B;
fn conv_box_aux(&self, x: Box<A>) -> Box<B>;
fn conv_gen_aux<Tc: super::HasParam<A, B>>(&self, x: Tc) -> Tc::SubstParam where Tc::SubstParam: Sized;
fn inv_aux(&self) -> &Self::InverseAux;
}
}
pub trait Identity<A: ?Sized, B: ?Sized>: aux::IdentityAux<A, B> {
fn conv(&self, x: A) -> B where A: Sized, B: Sized;
fn conv_ref<'a>(&self, x: &'a A) -> &'a B;
fn conv_mut<'a>(&self, x: &'a mut A) -> &'a mut B;
fn conv_box(&self, x: Box<A>) -> Box<B>;
fn conv_gen<Tc: HasParam<A, B>>(&self, x: Tc) -> Tc::SubstParam where Tc::SubstParam: Sized;
fn inv(&self) -> &<Self as IdentityUtil<A, B>>::Inverse;
}
impl<A: ?Sized, B: ?Sized, Ev: ?Sized + aux::IdentityAux<A, B>> Identity<A, B> for Ev {
fn conv(&self, x: A) -> B
where A: Sized, B: Sized {
self.conv_aux(x)
}
fn conv_ref<'a>(&self, x: &'a A) -> &'a B { self.conv_ref_aux(x) }
fn conv_mut<'a>(&self, x: &'a mut A) -> &'a mut B { self.conv_mut_aux(x) }
fn conv_box(&self, x: Box<A>) -> Box<B> { self.conv_box_aux(x) }
fn conv_gen<Tc: HasParam<A, B>>(&self, x: Tc) -> Tc::SubstParam
where Tc::SubstParam: Sized {
self.conv_gen_aux(x)
}
fn inv(&self) -> &Ev::InverseAux { self.inv_aux() }
}
pub trait IdentityUtil<A: ?Sized, B: ?Sized>: Identity<A, B> {
type Inverse: ?Sized + Identity<B, A>;
}
impl<A: ?Sized, B: ?Sized, Ev: ?Sized + Identity<A, B>> IdentityUtil<A, B> for Ev {
type Inverse = Self::InverseAux;
}
pub struct Refl<A: ?Sized> {
phantom_fn: PhantomData<fn(A) -> A>,
}
impl<A: ?Sized> Default for Refl<A> {
fn default() -> Refl<A> { Refl { phantom_fn: PhantomData } }
}
impl<A: ?Sized> fmt::Debug for Refl<A> {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result { fmtr.debug_struct("Refl").field("phantom_fn", &self.phantom_fn).finish() }
}
impl<A: ?Sized> aux::IdentityAux<A, A> for Refl<A> {
type InverseAux = Self;
fn conv_aux(&self, x: A) -> A
where A: Sized {
x
}
fn conv_ref_aux<'a>(&self, x: &'a A) -> &'a A { x }
fn conv_mut_aux<'a>(&self, x: &'a mut A) -> &'a mut A { x }
fn conv_box_aux(&self, x: Box<A>) -> Box<A> { x }
fn inv_aux(&self) -> &Self { self }
fn conv_gen_aux<Tc: HasParam<A, A>>(&self, x: Tc) -> Tc::SubstParam
where Tc::SubstParam: Sized {
x.conv_under(self)
}
}
pub trait Equals<Other: ?Sized> {
type IdentityWitness: Identity<Self, Other>;
fn identity_witness() -> Self::IdentityWitness;
}
impl<T: ?Sized> Equals<T> for T {
type IdentityWitness = Refl<T>;
fn identity_witness() -> Refl<T> { Refl::default() }
}
pub trait HasParam<OldParam: ?Sized, NewParam: ?Sized> {
type SubstParam: ?Sized + HasParam<NewParam, OldParam, SubstParam = Self>;
fn conv_under<Ev: Identity<OldParam, NewParam>>(self, ev: &Ev) -> Self::SubstParam where Self: Sized, Self::SubstParam: Sized;
}
impl<'a, OldParam: ?Sized + 'a, NewParam: ?Sized + 'a> HasParam<OldParam, NewParam> for &'a OldParam {
type SubstParam = &'a NewParam;
fn conv_under<Ev: Identity<OldParam, NewParam>>(self, ev: &Ev) -> Self::SubstParam {
ev.conv_ref(self)
}
}
impl<'a, OldParam: ?Sized + 'a, NewParam: ?Sized + 'a> HasParam<OldParam, NewParam> for &'a mut OldParam {
type SubstParam = &'a mut NewParam;
fn conv_under<Ev: Identity<OldParam, NewParam>>(self, ev: &Ev) -> Self::SubstParam {
ev.conv_mut(self)
}
}
impl<OldParam: ?Sized, NewParam: ?Sized> HasParam<OldParam, NewParam> for Box<OldParam> {
type SubstParam = Box<NewParam>;
fn conv_under<Ev: Identity<OldParam, NewParam>>(self, ev: &Ev) -> Self::SubstParam {
ev.conv_box(self)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn inv_conv_test() {
fn inv_conv<A, B, Ev: Identity<A, B>>(ev: Ev, x: B) -> A { ev.inv().conv(x) }
assert_eq!(inv_conv(Refl::default(), 0), 0)
}
}