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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
{-# LANGUAGE NoImplicitPrelude #-}
module Test.Unit.Core (unitTests) where
import Base hiding (Empty)
import Checking.Core
import Checking.Foundation qualified as Foundation
import Control.DeepSeq (NFData(..), force)
import Hedgehog
import Hedgehog.Gen qualified as Gen
import Hedgehog.Range qualified as Range
import Test.Tasty
import Test.Tasty.HUnit hiding (assert)
import Test.Tasty.Hedgehog (testPropertyNamed)
data TestGlobal
= TestGlobal
| TestPairGlobal
deriving (Show, Eq, Ord)
instance NFData TestGlobal where
rnf _global =
()
testGlobalType :: TestGlobal -> Maybe CoreType
testGlobalType TestGlobal =
Just TySet
testGlobalType TestPairGlobal =
Just (TySet `TyArrow` (TySet `TyArrow` TySet))
unitTests :: TestTree
unitTests =
testGroup "Checked core"
[ testCase
"freezes explicit binder positions"
freezesExplicitBinderPositions
, testCase
"freezes generalized substitution without capture"
freezesGeneralizedSubstitution
, testCase
"rejects ill-typed and open terms"
rejectsInvalidTerms
, testCase
"rechecks canonical terms without unchecked construction"
rechecksCanonicalTerms
, testCase
"checks scoped canonical weakening and substitution"
checksScopedCanonicalOperations
, testCase
"builds set-induction hypotheses from the complete property"
buildsSetInductionHypotheses
, testCase
"specializes the checked separation characteristic"
specializesCheckedSeparationCharacteristic
, testCase
"specializes the checked replacement characteristic"
specializesCheckedReplacementCharacteristic
, testCase
"thaws checked closed terms without changing them"
thawsCheckedClosedTerms
, testPropertyNamed
"optimized freeze agrees with bounded reference"
"prop_freezeAgreesWithReference"
prop_freezeAgreesWithReference
]
freezesExplicitBinderPositions :: Assertion
freezesExplicitBinderPositions = do
let x = 0 :: Int
y = 1 :: Int
freeze body = do
checked <-
either
(assertFailure . show)
pure
(checkClosedCore
testGlobalType
(coreLambda TySet x
(coreLambda TyProp y body)))
either
(assertFailure . show)
(pure . frozenCoreTerm)
(freezeClosed checked)
nearest <-
freeze (coreLocal y)
outer <-
freeze (coreLocal x)
global <-
freeze (coreGlobal TestGlobal)
assertEqual
"nearest binder"
(CLam TySet (CLam TyProp (CBound 0)))
nearest
assertEqual
"outer binder"
(CLam TySet (CLam TyProp (CBound 1)))
outer
assertEqual
"global with both vacuous binders"
(CLam TySet (CLam TyProp (CGlobal TestGlobal)))
global
freezesGeneralizedSubstitution :: Assertion
freezesGeneralizedSubstitution = do
let outer = 0 :: Int
inner = 1 :: Int
placeholder = 2 :: Int
innerTerm =
coreLambda TySet inner (coreLocal placeholder)
substituted =
innerTerm >>= \local ->
if local == placeholder
then coreLocal outer
else coreLocal local
term =
coreLambda TyProp outer substituted
checked <-
either
(assertFailure . show)
pure
(checkClosedCore testGlobalType term)
optimized <-
either
(assertFailure . show)
pure
(freezeClosed checked)
reference <-
either
(assertFailure . show)
pure
(referenceFreezeClosed checked)
assertEqual "reference result" reference optimized
assertEqual
"outer variable remains outside the inner binder"
(CLam TyProp (CLam TySet (CBound 1)))
(frozenCoreTerm optimized)
rejectsInvalidTerms :: Assertion
rejectsInvalidTerms = do
assertEqual
"free local"
(Left UnboundCoreLocal)
(checkedCoreType
<$> checkClosedCore
testGlobalType
(coreLocal (0 :: Int)))
assertEqual
"application argument"
(Left
(ApplicationArgumentTypeMismatch
TySet
TyProp))
(checkedCoreType
<$> checkClosedCore
testGlobalType
(coreApply
(coreIntrinsic FamilyUnion)
coreFalsum))
assertEqual
"equality operand"
(Left
(EqualityOperandTypeMismatch
TySet
TyProp))
(checkedCoreType
<$> checkClosedCore
testGlobalType
(coreEquality
TySet
coreFalsum
(coreGlobal TestGlobal)))
rechecksCanonicalTerms :: Assertion
rechecksCanonicalTerms = do
let term =
CForall TySet
(CEq TySet
(CBound 0)
(CBound 0))
assertEqual
"well-typed canonical proposition"
(Right (TyProp, term))
( (\checked ->
( frozenCoreType checked
, frozenCoreTerm checked
))
<$> checkCanonicalCore testGlobalType term
)
assertEqual
"out-of-scope de Bruijn index"
(Left (UnboundCoreIndex 1))
(frozenCoreType
<$> checkCanonicalCore
testGlobalType
(CForall TySet (CBound 1)))
assertEqual
"canonical application still checks argument types"
(Left
(ApplicationArgumentTypeMismatch
TySet
TyProp))
(frozenCoreType
<$> checkCanonicalCore
testGlobalType
(CApp
(CIntrinsic FamilyUnion)
CFalsum))
checksScopedCanonicalOperations :: Assertion
checksScopedCanonicalOperations = do
scoped <-
either
(assertFailure . show)
pure
(checkScopedCanonicalCore
testGlobalType
[TySet]
(CBound 0))
weakened <-
either
(assertFailure . show)
pure
(weakenScopedCore
testGlobalType
TyProp
scoped)
assertEqual
"nearest binder insertion shifts the prior local"
( [TyProp, TySet]
, TySet
, CBound 1
)
( scopedCoreContext weakened
, scopedCoreType weakened
, scopedCoreTerm weakened
)
assertEqual
"top-level binder substitution removes its index"
(CEq TySet
(CIntrinsic Empty)
(CIntrinsic Empty))
(instantiateCanonical
(CIntrinsic Empty
:: CanonicalTerm TestGlobal)
(CEq TySet
(CBound 0)
(CBound 0)))
root <-
either
(assertFailure . show)
pure
(checkScopedCanonicalCore
testGlobalType
[]
(CGlobal TestGlobal))
assertEqual
"empty scoped context closes"
(Just
(TySet, CGlobal TestGlobal))
( (\closed ->
( frozenCoreType closed
, frozenCoreTerm closed
))
<$> closeScopedCore root
)
buildsSetInductionHypotheses :: Assertion
buildsSetInductionHypotheses = do
claimProperty <-
either
(assertFailure . show)
pure
(checkScopedCanonicalCore
testGlobalType
[TySet]
(CImp
(CEq TySet
(CBound 0)
(CIntrinsic Empty))
(CEq TySet
(CBound 0)
(CBound 0))))
hypothesis <-
maybe
(assertFailure "set-induction hypothesis was not constructed")
pure
(scopedSetInductionHypothesis 0 claimProperty)
assertEqual
"antecedent and goal are both generalized over the member"
(CForall TySet
(CImp
(CApp
(CApp
(CIntrinsic Member)
(CBound 0))
(CBound 1))
(CImp
(CEq TySet
(CBound 0)
(CIntrinsic Empty))
(CEq TySet
(CBound 0)
(CBound 0)))))
(scopedCoreTerm hypothesis)
specializesCheckedSeparationCharacteristic :: Assertion
specializesCheckedSeparationCharacteristic = do
foundation <-
either
(assertFailure . show)
pure
Foundation.checkedFoundation
let bound = CIntrinsic Empty
predicate =
CLam TySet
(CEq TySet (CBound 0) (CBound 0))
separation =
CApp
(CApp (CIntrinsic Sep) bound)
predicate
body <-
either
(assertFailure . show)
pure
(checkScopedCanonicalCore
testGlobalType
[]
separation)
definition <-
maybe
(assertFailure "separation did not form a set definition")
pure
(scopedSetDefinition
(Foundation.foundationAxiomFrozen
foundation
Foundation.SeparationCharacteristic)
body)
let generated =
instantiateCanonical
separation
(scopedCoreTerm definition)
expected =
betaNormalize
(specializeForall predicate
(specializeForall bound
(mapCanonicalGlobals absurd
(frozenCoreTerm
(Foundation.foundationAxiomFrozen
foundation
Foundation.SeparationCharacteristic)))))
assertEqual
"local characteristic is the checked rule specialization"
expected
generated
specializesCheckedReplacementCharacteristic :: Assertion
specializesCheckedReplacementCharacteristic = do
foundation <-
either
(assertFailure . show)
pure
Foundation.checkedFoundation
pair <- checked [] (CGlobal TestPairGlobal)
domain <- checked [] (CIntrinsic Empty)
value <- checked [TySet] (CBound 0)
(graph, checkedDomain, function) <-
maybe
(assertFailure "checked values did not form a replacement graph")
pure
(scopedReplacementGraph pair domain value)
definition <-
maybe
(assertFailure "replacement graph did not form a definition")
pure
(scopedCharacteristicDefinition
(Foundation.foundationAxiomFrozen
foundation
Foundation.ReplacementCharacteristic)
graph
(checkedDomain :| [function]))
let generated =
instantiateCanonical
(scopedCoreTerm graph)
(scopedCoreTerm definition)
expected =
betaNormalize
(specializeForall (scopedCoreTerm function)
(specializeForall (scopedCoreTerm checkedDomain)
(mapCanonicalGlobals absurd
(frozenCoreTerm
(Foundation.foundationAxiomFrozen
foundation
Foundation.ReplacementCharacteristic)))))
assertEqual
"local graph characteristic is the checked rule specialization"
expected
generated
where
checked context term =
either
(assertFailure . show)
pure
(checkScopedCanonicalCore testGlobalType context term)
specializeForall
:: CanonicalTerm global
-> CanonicalTerm global
-> CanonicalTerm global
specializeForall argument = \case
CForall _binderType body ->
instantiateCanonical argument body
_ ->
error "the checked characteristic lost a binder"
betaNormalize :: CanonicalTerm global -> CanonicalTerm global
betaNormalize = \case
CApp function argument ->
case betaNormalize function of
CLam _binderType body ->
betaNormalize
(instantiateCanonical
(betaNormalize argument)
body)
normalizedFunction ->
CApp normalizedFunction (betaNormalize argument)
CLam binderType body ->
CLam binderType (betaNormalize body)
CImp premise conclusion ->
CImp
(betaNormalize premise)
(betaNormalize conclusion)
CEq operandType left right ->
CEq operandType
(betaNormalize left)
(betaNormalize right)
CForall binderType body ->
CForall binderType (betaNormalize body)
term -> term
thawsCheckedClosedTerms :: Assertion
thawsCheckedClosedTerms = do
let source =
coreLambda TySet (0 :: Int)
(coreForall TySet 1
(coreEquality
TySet
(coreLocal 0)
(coreLocal 1)))
freeze syntax = do
checked <-
either
(assertFailure . show)
pure
(checkClosedCore testGlobalType syntax)
either
(assertFailure . show)
pure
(freezeClosed checked)
original <-
freeze source
roundTrip <-
freeze (thawFrozenCore original)
assertEqual "frozen term" original roundTrip
assertEqual
"global inventory"
mempty
(frozenCoreGlobals original)
prop_freezeAgreesWithReference :: Property
prop_freezeAgreesWithReference = property do
depth <-
forAll (Gen.int (Range.linear 1 10))
binderTypes <-
forAll
(Gen.list
(Range.singleton depth)
(Gen.element
[ TyProp
, TySet
, TySet `TyArrow` TySet
]))
selected <-
forAll
(Gen.maybe
(Gen.int (Range.linear 0 (depth - 1))))
let binders =
zip [0 :: Int ..] binderTypes
body =
maybe
(coreGlobal TestGlobal)
coreLocal
selected
term =
foldr
(\(local, binderType) ->
coreLambda binderType local)
body
binders
checked <-
evalEither
(checkClosedCore testGlobalType term)
optimized <-
evalEither (force <$> freezeClosed checked)
reference <-
evalEither (force <$> referenceFreezeClosed checked)
optimized === reference
|