-
Notifications
You must be signed in to change notification settings - Fork 2
/
fullpoly.rs
206 lines (177 loc) · 6.14 KB
/
fullpoly.rs
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
// chapter 25: An ML Implementation of System F
// Ocaml version
// https://www.cis.upenn.edu/~bcpierce/tapl/checkers/fullpoly/
// For clippy
// #![allow(enum_variant_names)]
// #![allow(many_single_char_names)]
// #![allow(new_without_default_derive)]
#![allow(dead_code)]
#![allow(non_snake_case)]
#[derive(Clone, PartialEq, Debug)]
enum Ty {
// TmVar { index: usize, ctx_len: usize },
TyVar(usize, usize), // Type variables
TyArr(Box<Ty>, Box<Ty>),
TyAll(String, Box<Ty>), // Universal quantifiers
TySome(String, Box<Ty>), // Existential quantifiers
}
use self::Ty::*;
impl Ty {
// p383
// let tymap onvar c tyT
fn ty_map<F>(&self, onvar: &F, c: i32) -> Ty
where F: Fn(i32, usize, usize) -> Ty
{
match *self {
TyArr(ref t1, ref t2) => TyArr(box t1.ty_map(onvar, c), box t2.ty_map(onvar, c)),
TyVar(x, n) => onvar(c, x, n),
TyAll(ref ty_X, ref ty2) => TyAll(ty_X.clone(), box ty2.ty_map(onvar, c + 1)),
TySome(ref ty_X, ref ty2) => TySome(ty_X.clone(), box ty2.ty_map(onvar, c + 1)),
}
}
fn type_shift_above(&self, d: i32, c: i32) -> Ty {
self.ty_map(&|c, x, n| if x >= c as usize {
TyVar(x + d as usize, n + d as usize)
} else {
TyVar(x, n + d as usize)
},
c)
}
fn type_shift(&self, d: i32) -> Ty {
self.type_shift_above(d, 0)
}
// let typeSubst tyS j tyT
// self == tyT
fn type_subst(&self, s: &Ty, j: i32) -> Ty {
self.ty_map(&|j, x, n| if x == j as usize {
s.type_shift(j)
} else {
TyVar(x, n)
},
j)
}
// let typeSubstTop tyS tyT
// ((lambda . t12) (v2))
// => term_subst_top(v2, t12)
// t12.type_subst_top(v2)
fn type_subst_top(&self, s: &Ty) -> Ty {
self.type_subst(&s.type_shift(1), 0).type_shift(-1)
}
}
// p382
#[derive(Clone)]
enum Binding {
NameBind, // Not used. This is usef only by the parsing and printing.
VarBind(Ty),
TyVarBind,
}
// p383
#[derive(Clone, PartialEq, Debug)]
enum Term {
// TmVar { index: usize, ctx_len: usize },
TmVar(usize, usize),
TmAbs(String, Ty, Box<Term>), // "(lambda x:Nat: x)" -> TmAbs("x", Nat, x)
TmApp(Box<Term>, Box<Term>), // "((lambda x:Nat: x) y)" -> TmApp((lambda x:Nat: x), y)
TmTAbs(String, Box<Term>), // "VX.X->X" -> TmTbs("X", X->X)
TmTApp(Box<Term>, Ty), // "((VX.X->X) [Nat])" -> TmTApp((VX.X->X), Nat)
TmPack(Ty, Box<Term>, Ty), // "{*Nat, term}" as {EX, T}" -> TmPack(Nat, term, {EX, T})
TmUnpack(String, String, Box<Term>, Box<Term>), /* "let {X, x} = t1 in t2" -> TmUnpack("X", "x", t1, t2) */
}
use self::Term::*;
impl Term {
// p384
// let tmmap onvar ontype c t
fn tm_map<F1, F2>(&self, onvar: &F1, ontype: &F2, c: i32) -> Term
where F1: Fn(i32, usize, usize) -> Term,
F2: Fn(i32, &Ty) -> Ty
{
match *self {
TmVar(x, n) => onvar(c, x, n),
TmAbs(ref x, ref ty1, ref t2) => {
TmAbs(x.clone(),
ontype(c, ty1),
box t2.tm_map(onvar, ontype, c + 1))
}
TmApp(ref t1, ref t2) => {
TmApp(box t1.tm_map(onvar, ontype, c),
box t2.tm_map(onvar, ontype, c))
}
TmTAbs(ref ty_X, ref t2) => TmTAbs(ty_X.clone(), box t2.tm_map(onvar, ontype, c + 1)),
TmTApp(ref t1, ref ty2) => TmTApp(box t1.tm_map(onvar, ontype, c), ontype(c, ty2)),
TmPack(ref ty1, ref t2, ref ty2) => {
TmPack(ontype(c, ty1),
box t2.tm_map(onvar, ontype, c),
ontype(c, ty2))
}
TmUnpack(ref ty_X, ref x, ref t1, ref t2) => {
TmUnpack(ty_X.clone(),
x.clone(),
box t1.tm_map(onvar, ontype, c),
box t2.tm_map(onvar, ontype, c))
}
}
}
// let termShiftAbove d c t
fn term_shift_above(&self, d: i32, c: i32) -> Term {
self.tm_map(&|c, x, n| if x >= c as usize {
TmVar(x + d as usize, n + d as usize)
} else {
TmVar(x, n + d as usize)
},
&|c, ty| ty.type_shift_above(d, c),
c)
}
// let termShift d t = termShiftAbove d 0 t
fn term_shift(&self, d: i32) -> Term {
self.term_shift_above(d, 0)
}
// let termSubst j s t
fn term_subst(&self, j: i32, s: &Term) -> Term {
self.tm_map(&|j, x, n| if x == j as usize {
s.term_shift(j)
} else {
TmVar(x, n)
},
&|_, ty| ty.clone(),
j)
}
// (E-TappTabs)
// let rec tytermSubst tyS j t
fn ty_term_subst(&self, tyS: &Ty, j: i32) -> Term {
self.tm_map(&|_, x, n| TmVar(x, n), &|j, ty| ty.type_subst(tyS, j), j)
}
fn term_subst_top(&self, s: &Term) -> Term {
self.term_subst(0, &s.term_shift(1)).term_shift(-1)
}
fn ty_term_subst_top(&self, s: &Ty) -> Term {
self.ty_term_subst(&s.type_shift(1), 0).term_shift(-1)
}
// TODO <2016-05-06 Fri>
// fn eval1(&self, ctx: &Context) -> Term {
// }
// TODO <2016-05-06 Fri>
// type_of
}
use self::Binding::*;
struct Context {
contexts: Vec<(String, Binding)>,
}
impl Context {
fn new() -> Context {
Context { contexts: Vec::new() }
}
fn add_binding(&self, x: String, bind: Binding) -> Context {
let mut v = self.contexts.clone();
v.push((x, bind));
Context { contexts: v }
}
fn get_binding(&self, i: usize) -> Binding {
self.contexts[self.contexts.len() - 1 - i].1.clone()
}
fn get_type_from_context(&self, i: usize) -> Result<Ty, String> {
match self.get_binding(i) {
VarBind(ty) => Ok(ty),
_ => Err("get_type_from_context: TODO: Message".to_string()),
}
}
}