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
|
use crate::r#type::{Type, Type::*, util::*};
use std::collections::HashMap;
impl Type {
/// Tries to convert this type into another type.
///
/// The following conversions are required:
/// 0. self == other => self
/// 1. (T T ... T T) -> (T ...)
/// 2. self = concrete and other = variable type => self,
/// 3. arrow types and lists and sums are mapped
/// 4. makes [self] self if self == other
pub fn convert(self, other: &Type) -> Result<Type, ()> {
let (ty, ctx) = self.convert_ctx(other)?;
let mut checks = HashMap::new();
for (name, ty1) in ctx {
if let Some(ty2) = checks.insert(name, ty1.clone()) {
if ty2 != ty1 {
return Err(());
}
}
}
Ok(ty)
}
fn convert_ctx(
self,
other: &Type
) -> Result<(Type, Vec<(String, Type)>), ()> {
let mut ctx = vec![];
match (self, other) {
(a, b) if a == *b => Ok((a, ctx)),
(ty, VarType(name)) => if ty.is_concrete().is_ok() {
ctx.push((name.clone(), ty.clone()));
Ok((ty, ctx))
} else {
Err(())
},
(List(v1), List(v2)) if v1.len() == v2.len() => {
let mut res = vec![];
for (t1, t2) in v1.into_iter().zip(v2) {
let (t, newctx) = t1.convert_ctx(t2)?;
ctx.extend(newctx);
res.push(t);
}
Ok((List(res), ctx))
},
(Arrow(a1, a2), Arrow(b1, b2)) => {
let (t1, newctx) = a1.convert_ctx(b1)?;
ctx.extend(newctx);
let (t2, newctx) = a2.convert_ctx(b2)?;
ctx.extend(newctx);
Ok((arr(t1, t2), ctx))
},
(SumType(a1, a2), SumType(b1, b2)) => {
let (t1, newctx) = a1.convert_ctx(b1)?;
ctx.extend(newctx);
let (t2, newctx) = a2.convert_ctx(b2)?;
ctx.extend(newctx);
Ok((sumtype(t1, t2), ctx))
},
(List(v), VecOf(ty)) => match v.get(0) {
Some(t) => {
let (convt, newctx) = t.clone().convert_ctx(ty)?;
ctx.extend(newctx);
for t in v {
let (sconvt, newctx) = t.convert_ctx(ty)?;
if sconvt != convt {
return Err(());
} else {
ctx.extend(newctx);
}
}
Ok((vecof(convt), ctx))
},
None => Err(()), // questionable? could be useful to convert?
},
(VecOf(t1), VecOf(t2)) => {
let (convt, ctx) = t1.convert_ctx(t2)?;
Ok((vecof(convt), ctx))
},
// at the end, because it'd be nice to know in the
// guard that the conversion from v to t2 works, and
// if it doesn't, try something else, but now everyhting
// has already been tried so this is safe
(List(v), t2) if v.len() == 1 => v[0].clone().convert_ctx(t2),
_ => Err(())
}
}
}
#[cfg(test)]
mod tests {
use crate::r#type::{Type::*, util::*};
#[test]
fn test_conversion_case0() {
assert_eq!(Integer.convert(&Integer), Ok(Integer));
assert_eq!(Boolean.convert(&Boolean), Ok(Boolean));
assert_eq!(
List(vec![Integer, Boolean])
.convert(&List(vec![Integer, Boolean])),
Ok(List(vec![Integer, Boolean]))
);
assert_eq!(
vecof(Integer).convert(&vecof(Integer)),
Ok(vecof(Integer))
);
assert_eq!(vt("T").convert(&vt("T")), Ok(vt("T")));
assert!(Integer.convert(&Boolean).is_err());
assert!(Boolean.convert(&Integer).is_err());
assert!(
List(vec![Integer, Boolean])
.convert(&List(vec![Integer, Integer]))
.is_err()
);
assert!(
vecof(Integer)
.convert(&vecof(Boolean))
.is_err()
);
assert!(vt("T").convert(&vt("H")).is_err());
}
#[test]
fn test_conversion_case1() {
assert_eq!(
List(vec![Integer]).convert(&vecof(Integer)),
Ok(vecof(Integer))
);
assert_eq!(
List(vec![Integer]).convert(&vecof(vt("T"))),
Ok(vecof(Integer))
);
}
#[test]
fn test_conversion_case2() {
assert_eq!(Integer.convert(&vt("T")), Ok(Integer));
assert_eq!(Boolean.convert(&vt("T")), Ok(Boolean));
assert_eq!(
List(vec![Integer, Integer])
.convert(&List(vec![vt("T"), vt("T")])),
Ok(List(vec![Integer, Integer]))
);
assert_eq!(
List(vec![Integer, Boolean])
.convert(&List(vec![vt("T"), vt("H")])),
Ok(List(vec![Integer, Boolean]))
);
assert_eq!(vecof(Integer).convert(&vecof(vt("T"))), Ok(vecof(Integer)));
assert_eq!(
arr(arr(vecof(Integer), Integer), arr(vecof(Integer), Integer))
.convert(&arr(vt("T"), vt("T"))),
Ok(arr(arr(vecof(Integer), Integer), arr(vecof(Integer), Integer)))
);
assert_eq!(
sumtype(sumtype(vecof(Integer), Integer), sumtype(vecof(Integer), Integer))
.convert(&sumtype(vt("T"), vt("T"))),
Ok(sumtype(sumtype(vecof(Integer), Integer), sumtype(vecof(Integer), Integer)))
);
assert!(
List(vec![Integer, Boolean])
.convert(&List(vec![vt("T"), vt("T")]))
.is_err()
);
assert!(
arr(arr(vecof(Integer), Integer), arr(vecof(Boolean), Integer))
.convert(&arr(vt("T"), vt("T")))
.is_err()
);
}
}
|