-
Notifications
You must be signed in to change notification settings - Fork 12
/
Reduction.scala
341 lines (302 loc) · 10.6 KB
/
Reduction.scala
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright 2009-2022 EPFL, Lausanne
*
* Authors: Andrea Gilot, Noé De Santo
*/
import stainless.lang._
import stainless.collection._
import stainless.annotation._
object Reduction {
import SystemF._
import Transformations._
import TransformationsProperties.{Terms => TermsProp, Types => TypesProp}
sealed trait ReductionRule
sealed trait AppRule extends ReductionRule
case object App1Congruence extends AppRule
case object App2Congruence extends AppRule
case object AbsAppReduction extends AppRule
sealed trait FixRule extends ReductionRule
case object FixCongruence extends FixRule
case object AbsFixReduction extends FixRule
sealed trait TAppRule extends ReductionRule
case object TAppCongruence extends TAppRule
case object TAbsTappReduction extends TAppRule
sealed trait TAbsRule extends ReductionRule
case object TAbsCongruence extends TAbsRule
sealed trait AbsRule extends ReductionRule
case object AbsCongruence extends AbsRule
// ↑⁻¹( [0 -> ↑¹(arg)]body )
def absSubstitution(body: Term, arg: Term): Term = {
assert(!arg.hasFreeVariablesIn(0, 0))
TermsProp.boundRangeShift(arg, 1, 0, 0, 0)
TermsProp.boundRangeSubstitutionLemma(body, 0, Terms.shift(arg, 1, 0))
Terms.shift(Terms.substitute(body, 0, Terms.shift(arg, 1, 0)), -1, 0)
}
// ↑⁻¹( [0 -> ↑¹(arg)]body )
def universalSubstitution(body: Type, arg: Type): Type = {
assert(!arg.hasFreeVariablesIn(0, 0))
TypesProp.boundRangeShift(arg, 1, 0, 0, 0)
TypesProp.boundRangeSubstitutionLemma(body, 0, Types.shift(arg, 1, 0))
Types.shift(Types.substitute(body, 0, Types.shift(arg, 1, 0)), -1, 0)
}
// ↑⁻¹( [0 -> ↑¹(arg)]body )
def tabsSubstitution(body: Term, arg: Type): Term = {
assert(!arg.hasFreeVariablesIn(0, 0))
TypesProp.boundRangeShift(arg, 1, 0, 0, 0)
TypesProp.boundRangeSubstitutionLemma(body, 0, Types.shift(arg, 1, 0))
Types.shift(Types.substitute(body, 0, Types.shift(arg, 1, 0)), -1, 0)
}
// [t -> t']
def reducesTo(t: Term, tp: Term): Option[ReductionRule] = {
t match {
case Var(_) => None[ReductionRule]()
case Abs(argTyp, body) =>{
tp match {
case Abs(argTypp, bodyp) if argTyp == argTypp && reducesTo(body, bodyp).isDefined => Some[ReductionRule](AbsCongruence)
case _ => None[ReductionRule]()
}
}
case App(t1, t2) => {
(tp match {
case App(t1p, t2p) if reducesTo(t1, t1p).isDefined && t2 == t2p => Some[ReductionRule](App1Congruence)
case App(t1p, t2p) if t1 == t1p && reducesTo(t2, t2p).isDefined => Some[ReductionRule](App2Congruence)
case _ => None[ReductionRule]()
}).orElse(t1 match {
case Abs(_, body) if absSubstitution(body, t2) == tp => Some[ReductionRule](AbsAppReduction)
case _ => None[ReductionRule]()
})
}
case Fix(f) => {
(tp match {
case Fix(fp) if reducesTo(f, fp).isDefined => Some[ReductionRule](FixCongruence)
case _ => None[ReductionRule]()
}).orElse(f match {
case Abs(_, body) if absSubstitution(body, t) == tp => Some[ReductionRule](AbsFixReduction)
case _ => None[ReductionRule]()
})
}
case TAbs(body) => {
tp match {
case TAbs(bodyp) if reducesTo(body, bodyp).isDefined => Some[ReductionRule](TAbsCongruence)
case _ => None[ReductionRule]()
}
}
case TApp(term, typ) => {
(tp match {
case TApp(termp, typp) if reducesTo(term, termp).isDefined && typ == typp => Some[ReductionRule](TAppCongruence)
case _ => None[ReductionRule]()
}).orElse(term match {
case TAbs(body) if tabsSubstitution(body, typ) == tp => Some[ReductionRule](TAbsTappReduction)
case _ => None[ReductionRule]()
})
}
}
}
/// Call-by-value lambda calculus evaluation
def reduceCallByValue(t: Term): Option[Term] = {
t match {
case Var(_) => None[Term]()
case Abs(_, _) => None[Term]()
case App(t1, t2) => {
if(!t1.isValue) {
reduceCallByValue(t1).map(t1p => App(t1p, t2))
}
else if(!t2.isValue) {
reduceCallByValue(t2).map(t2p => App(t1, t2p))
}
else {
t1 match {
case Abs(_, body) => {
Some(absSubstitution(body, t2))
}
case _ => None[Term]()
}
}
}
case Fix(f) => {
if(!f.isValue) {
reduceCallByValue(f).map(Fix(_))
}
else {
f match {
case Abs(_, body) => {
Some(absSubstitution(body, t))
}
case _ => None[Term]()
}
}
}
case TAbs(body) => None[Term]()
case TApp(term, typ) => {
if(!term.isValue) {
reduceCallByValue(term).map[Term](TApp(_, typ))
}
else {
term match {
case TAbs(body) => Some(tabsSubstitution(body, typ))
case _ => None[Term]()
}
}
}
}
}
}
object ReductionProperties {
import SystemF._
import Reduction._
/// ReducesTo semantics & inversion
def appReducesToSoundness(app: App, tp: Term): Unit = {
require(reducesTo(app, tp).isDefined)
}.ensuring(reducesTo(app, tp).get.isInstanceOf[AppRule])
def app1CongruenceInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == App1Congruence)
assert(t.isInstanceOf[App])
}.ensuring(
t.isInstanceOf[App] && tp.isInstanceOf[App] &&
(t.asInstanceOf[App].t2 == tp.asInstanceOf[App].t2) &&
reducesTo(t.asInstanceOf[App].t1, tp.asInstanceOf[App].t1).isDefined
)
def app2CongruenceInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == App2Congruence)
assert(t.isInstanceOf[App])
}.ensuring(
t.isInstanceOf[App] && tp.isInstanceOf[App] &&
(t.asInstanceOf[App].t1 == tp.asInstanceOf[App].t1) &&
reducesTo(t.asInstanceOf[App].t2, tp.asInstanceOf[App].t2).isDefined
)
def absAppReductionInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == AbsAppReduction)
assert(t.isInstanceOf[App])
val App(t1, _) = t: @unchecked
assert(t1.isInstanceOf[Abs])
}.ensuring(
t.isInstanceOf[App] && t.asInstanceOf[App].t1.isInstanceOf[Abs] &&
(
tp
==
absSubstitution(t.asInstanceOf[App].t1.asInstanceOf[Abs].body, t.asInstanceOf[App].t2)
)
)
def fixReducesToSoundness(fix: Fix, tp: Term): Unit = {
require(reducesTo(fix, tp).isDefined)
}.ensuring(reducesTo(fix, tp).get.isInstanceOf[FixRule])
def fixCongruenceInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == FixCongruence)
assert(t.isInstanceOf[Fix])
}.ensuring(
t.isInstanceOf[Fix] && tp.isInstanceOf[Fix] &&
reducesTo(t.asInstanceOf[Fix].t, tp.asInstanceOf[Fix].t).isDefined
)
def absFixReductionInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == AbsFixReduction)
assert(t.isInstanceOf[Fix])
val Fix(f) = t: @unchecked
assert(f.isInstanceOf[Abs])
}.ensuring(
t.isInstanceOf[Fix] && t.asInstanceOf[Fix].t.isInstanceOf[Abs] &&
(
tp
==
absSubstitution(t.asInstanceOf[Fix].t.asInstanceOf[Abs].body, t)
)
)
def tappReducesToSoundness(tapp: TApp, tp: Term): Unit = {
require(reducesTo(tapp, tp).isDefined)
}.ensuring(reducesTo(tapp, tp).get.isInstanceOf[TAppRule])
@opaque @pure
def tappCongruenceInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == TAppCongruence)
assert(t.isInstanceOf[TApp])
}.ensuring(
t.isInstanceOf[TApp] && tp.isInstanceOf[TApp] &&
( t.asInstanceOf[TApp].typ == tp.asInstanceOf[TApp].typ ) &&
reducesTo(t.asInstanceOf[TApp].t, tp.asInstanceOf[TApp].t).isDefined
)
def tabsTappReductionInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == TAbsTappReduction)
assert(t.isInstanceOf[TApp])
val TApp(body, _) = t: @unchecked
assert(body.isInstanceOf[TAbs])
}.ensuring(
t.isInstanceOf[TApp] && t.asInstanceOf[TApp].t.isInstanceOf[TAbs] &&
(
tp
==
tabsSubstitution(
t.asInstanceOf[TApp].t.asInstanceOf[TAbs].t,
t.asInstanceOf[TApp].typ
)
)
)
def absReducesToSoundness(abs: Abs, tp: Term): Unit = {
require(reducesTo(abs, tp).isDefined)
}.ensuring(reducesTo(abs, tp).get.isInstanceOf[AbsRule])
def absCongruenceInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == AbsCongruence)
}.ensuring(
t.isInstanceOf[Abs] && tp.isInstanceOf[Abs] &&
( t.asInstanceOf[Abs].argType == tp.asInstanceOf[Abs].argType ) &&
reducesTo(t.asInstanceOf[Abs].body, tp.asInstanceOf[Abs].body).isDefined
)
def tabsReducesToSoundness(tabs: TAbs, tp: Term): Unit = {
require(reducesTo(tabs, tp).isDefined)
}.ensuring(reducesTo(tabs, tp).get.isInstanceOf[TAbsRule])
def tabsCongruenceInversion(t: Term, tp: Term): Unit = {
require(reducesTo(t, tp).isDefined)
require(reducesTo(t, tp).get == TAbsCongruence)
}.ensuring(
t.isInstanceOf[TAbs] && tp.isInstanceOf[TAbs] &&
reducesTo(t.asInstanceOf[TAbs].t, tp.asInstanceOf[TAbs].t).isDefined
)
/// Call-by-value soudness
@opaque @pure
def reduceCallByValueSoundness(t: Term): ReductionRule = {
require(reduceCallByValue(t).isDefined)
val tp = reduceCallByValue(t).get
t match {
case Var(_) => assert(false)
case Abs(_, _) => assert(false)
case App(t1, t2) => {
if(!t1.isValue) {
reduceCallByValueSoundness(t1)
}
else if(!t2.isValue) {
reduceCallByValueSoundness(t2)
}
else {
assert(reducesTo(t, tp).isDefined)
}
}
case Fix(f) => {
if(!f.isValue) {
reduceCallByValueSoundness(f)
}
else {
assert(reducesTo(t, tp).isDefined)
}
}
case TAbs(_) => assert(false)
case TApp(term, typ) => {
if(!term.isValue) {
reduceCallByValueSoundness(term)
}
else {
assert(reducesTo(t, tp).isDefined)
}
}
}
assert(reducesTo(t, reduceCallByValue(t).get).isDefined)
reducesTo(t, reduceCallByValue(t).get).get
}.ensuring(res =>
reducesTo(t, reduceCallByValue(t).get).isDefined &&
( res == reducesTo(t, reduceCallByValue(t).get).get )
)
}