summaryrefslogtreecommitdiff
path: root/source/Felix/Checking/SetConstruction.hs
blob: a5e1d8038d1fa87d11048d323b96c4abd921ce79 (plain)
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | Checked source semantics for named separation and functional replacement.
--
-- A value of 'NamedSetConstruction' is the sole transient owner of the
-- source decomposition.  Its smart constructors validate the complete
-- telescope and derive one canonical term.  Local views, transparent content,
-- direct extensional facts, and cache-scoped descriptors all consume that
-- same checked value.
module Felix.Checking.SetConstruction
    ( NamedSetConstruction
    , checkedSeparationConstruction
    , checkedFunctionalReplacementConstruction
    , namedSetConstructionTerm
    , namedSetConstructionLocalViews
    , namedSetConstructionClosedBody
    , SetConstructionFoundation
    , setConstructionFoundation
    , checkedFoundationSetConstruction
    , NamedSetConstructionFact
    , namedSetConstructionFactProposition
    , namedSetConstructionFactDescriptor
    , namedSetConstructionObjectFact
    , CheckedRelationalSetConstruction
    , checkedRelationalReplacementConstruction
    , relationalSetConstructionTerm
    , relationalSetConstructionFunctionality
    , relationalSetConstructionClosedFunctionality
    , relationalSetConstructionLocalViews
    , relationalSetConstructionClosedBody
    , RelationalSetConstructionFact
    , relationalSetConstructionFactProposition
    , relationalSetConstructionFactDescriptor
    , relationalSetConstructionObjectFact
    ) where

import Base hiding (Empty)
import Felix.Checking.Core
import Felix.Checking.Foundation
import Felix.Checking.Identity
import Felix.Cache.Codec
    ( CacheDigest
    , encodeCache
    , hashCacheFields
    , putCanonicalTermCache
    , putCoreTypeCache
    )

import Data.List.NonEmpty qualified as NonEmpty
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Numeric.Natural (Natural)


-- | The exact fixed rows used by the narrow derived extensionality schema.
-- Proof-local construction obtains these rows through the confined foundation
-- lookup; declaration authorization obtains them from 'CheckedFoundation'.
data SetConstructionFoundation = SetConstructionFoundation
    !(FrozenCheckedCore Void)
    !(FrozenCheckedCore Void)
    !(FrozenCheckedCore Void)
    !(FrozenCheckedCore Void)

setConstructionFoundation
    :: FrozenCheckedCore Void
    -> FrozenCheckedCore Void
    -> FrozenCheckedCore Void
    -> FrozenCheckedCore Void
    -> SetConstructionFoundation
setConstructionFoundation =
    SetConstructionFoundation

checkedFoundationSetConstruction
    :: CheckedFoundation
    -> SetConstructionFoundation
checkedFoundationSetConstruction foundation =
    SetConstructionFoundation
        (foundationAxiomFrozen foundation FamilyUnionCharacteristic)
        (foundationAxiomFrozen foundation SeparationCharacteristic)
        (foundationAxiomFrozen foundation ReplacementCharacteristic)
        (foundationAxiomFrozen foundation SetChooseWitness)

data NamedSetConstruction global = NamedSetConstruction
    ![CoreType]
    !(Map.Map global CoreType)
    !(NamedSetConstructionShape global)
    !(BuiltSetConstruction global)
    deriving stock (Eq)

data NamedSetConstructionShape global
    = SeparationShape
        !(CanonicalTerm global)
        !(CanonicalTerm global)
    | FunctionalReplacementShape
        !(NonEmpty (CanonicalTerm global))
        !(CanonicalTerm global)
        !(Maybe (CanonicalTerm global))
    deriving stock (Eq)

-- | The one canonical build result retained by the checked construction.
-- Characteristic applications describe the exact primitive rows used by the
-- derived theorem schema; the flattened body is the deterministic composition
-- of those rows for the source telescope.
data BuiltSetConstruction global = BuiltSetConstruction
    !(CanonicalTerm global)
    !(CanonicalTerm global)
    ![CheckedCharacteristicApplication global]
    deriving stock (Eq)

data CheckedCharacteristicApplication global =
    CheckedCharacteristicApplication
        !CoreIntrinsicTag
        ![CoreType]
        !(CanonicalTerm global)
        !(NonEmpty (CoreType, CanonicalTerm global))
        !(CanonicalTerm global)
    deriving stock (Eq)

-- | One checked relational replacement.  Unlike the unconditional named
-- constructions above, its flattened membership theorem is available only
-- after the separately checked functionality proposition has authority.
-- This value owns the source telescope, the one canonical choice/replacement
-- term, and the exact primitive characteristic applications used by that
-- narrow derived schema.
data CheckedRelationalSetConstruction global =
    CheckedRelationalSetConstruction
        ![CoreType]
        !(Map.Map global CoreType)
        !(CanonicalTerm global)
        !(CanonicalTerm global)
        !(CanonicalTerm global)
        !(CanonicalTerm global)
        !(CanonicalTerm global)
        ![CheckedCharacteristicApplication global]
    deriving stock (Eq)

-- | Validate one source relational replacement.  The relation is checked in
-- the nearest-first context @[range, domain] <> outer@, matching the source
-- binder order @y x A P@ without retaining source syntax.
checkedRelationalReplacementConstruction
    :: Ord global
    => (global -> Maybe CoreType)
    -> ScopedCheckedCore global
    -> ScopedCheckedCore global
    -> Maybe (CheckedRelationalSetConstruction global)
checkedRelationalReplacementConstruction globalType domain relation = do
    guard (scopedCoreType domain == TySet)
    guard (scopedCoreType relation == TyProp)
    let context = scopedCoreContext domain
    guard (scopedCoreContext relation == TySet : TySet : context)
    globals <-
        captureGlobalTypes globalType
            [scopedCoreTerm domain, scopedCoreTerm relation]
    let domainTerm = scopedCoreTerm domain
        relationTerm = scopedCoreTerm relation
        domainPredicate = CLam TySet (logicalExists relationTerm)
        restrictedDomain = applyIntrinsic2 Sep domainTerm domainPredicate
        choiceFunction =
            CLam TySet
                (applyIntrinsic SetChoose (CLam TySet relationTerm))
        replacement = applyIntrinsic2 Repl restrictedDomain choiceFunction
        functionality = relationalFunctionality domainTerm relationTerm
        membership = relationalMembership domainTerm relationTerm
        applications =
            [ characteristicApplication
                Sep context restrictedDomain
                [ (TySet, domainTerm)
                , (TyArrow TySet TyProp, domainPredicate)
                ]
            , characteristicApplication
                Repl context replacement
                [ (TySet, restrictedDomain)
                , (TyArrow TySet TySet, choiceFunction)
                ]
            ]
        construction =
            CheckedRelationalSetConstruction
                context globals domainTerm relationTerm replacement
                functionality membership applications
    _ <- checkedRelationalDerived construction context TySet replacement
    _ <- checkedRelationalDerived construction context TyProp functionality
    _ <- checkedRelationalDerived
        construction (TySet : context) TyProp membership
    pure construction

-- | Check one source separation.  The callback supplies the exact visible
-- type of every global used by its already checked components.
checkedSeparationConstruction
    :: Ord global
    => (global -> Maybe CoreType)
    -> ScopedCheckedCore global
    -> ScopedCheckedCore global
    -> Maybe (NamedSetConstruction global)
checkedSeparationConstruction globalType bound predicate = do
    guard (scopedCoreType bound == TySet)
    guard (scopedCoreType predicate == TyProp)
    let context = scopedCoreContext bound
    guard (scopedCoreContext predicate == TySet : context)
    globals <-
        captureGlobalTypes globalType
            [scopedCoreTerm bound, scopedCoreTerm predicate]
    finishConstruction
        context
        globals
        (SeparationShape
            (scopedCoreTerm bound)
            (scopedCoreTerm predicate))

-- | Check source-ordered functional replacement once.  Domain @i@ is checked
-- beneath exactly the preceding @i - 1@ source binders; the value and optional
-- condition are checked beneath the complete telescope.
checkedFunctionalReplacementConstruction
    :: Ord global
    => (global -> Maybe CoreType)
    -> NonEmpty (ScopedCheckedCore global)
    -> ScopedCheckedCore global
    -> Maybe (ScopedCheckedCore global)
    -> Maybe (NamedSetConstruction global)
checkedFunctionalReplacementConstruction globalType domains value condition = do
    let domainList = NonEmpty.toList domains
        firstDomain = NonEmpty.head domains
    guard (scopedCoreType firstDomain == TySet)
    let context = scopedCoreContext firstDomain
        expectedDomainContexts =
            [ replicate index TySet <> context
            | index <- [0 .. length domainList - 1]
            ]
        valueContext = replicate (length domainList) TySet <> context
    guard
        (and
            (zipWith
                (\domain expected ->
                    scopedCoreType domain == TySet
                        && scopedCoreContext domain == expected)
                domainList
                expectedDomainContexts))
    guard
        (scopedCoreType value == TySet
            && scopedCoreContext value == valueContext)
    traverse_
        (\predicate ->
            guard
                (scopedCoreType predicate == TyProp
                    && scopedCoreContext predicate == valueContext))
        condition
    globals <-
        captureGlobalTypes globalType
            ( (scopedCoreTerm <$> domainList)
              <> [scopedCoreTerm value]
              <> maybeToList (scopedCoreTerm <$> condition)
            )
    finishConstruction
        context
        globals
        (FunctionalReplacementShape
            (scopedCoreTerm <$> domains)
            (scopedCoreTerm value)
            (scopedCoreTerm <$> condition))

namedSetConstructionTerm
    :: Ord global
    => NamedSetConstruction global
    -> ScopedCheckedCore global
namedSetConstructionTerm construction =
    fromMaybe
        (impossible "a checked construction lost its canonical term")
        (checkedDerived
            construction
            (constructionContext construction)
            TySet
            (constructionCanonicalTerm construction))

-- | Introduce a fresh named set and return adjacent FOF extensional and exact
-- equation locals.  Weakening is internal so construction-local binders and
-- source-domain order cannot drift at a caller.
namedSetConstructionLocalViews
    :: Ord global
    => SetConstructionFoundation
    -> NamedSetConstruction global
    -> Maybe
        ( ScopedCheckedCore global
        , ScopedCheckedCore global
        )
namedSetConstructionLocalViews foundation construction = do
    weakened <- weakenConstruction TySet construction
    let context = TySet : constructionContext construction
        target = CBound 0
    extensional <- extensionalView foundation target weakened
    equation <- checkedDerived weakened context TyProp
        (CEq TySet target (constructionCanonicalTerm weakened))
    pure (extensional, equation)

-- | Closed transparent content derived from the sole canonical construction
-- term.  This retains the pre-existing object-content identity exactly.
namedSetConstructionClosedBody
    :: Ord global
    => NamedSetConstruction global
    -> FrozenCheckedCore global
namedSetConstructionClosedBody construction =
    fromMaybe
        (impossible "a checked construction did not close")
        (freezeDerived construction closedType closedTerm)
  where
    context = constructionContext construction
    closedType = foldr TyArrow TySet (reverse context)
    closedTerm =
        foldl (flip CLam) (constructionCanonicalTerm construction) context

data NamedSetConstructionFact = NamedSetConstructionFact
    !(FrozenCheckedCore ObjectId)
    !CacheDigest

namedSetConstructionFactProposition
    :: NamedSetConstructionFact
    -> FrozenCheckedCore ObjectId
namedSetConstructionFactProposition
        (NamedSetConstructionFact proposition _descriptor) =
    proposition

namedSetConstructionFactDescriptor
    :: NamedSetConstructionFact
    -> CacheDigest
namedSetConstructionFactDescriptor
        (NamedSetConstructionFact _proposition descriptor) =
    descriptor

-- | Derive the only proposition authorized by
-- @CheckedSetConstructionExtensionality@.  This is a deliberately small
-- trusted theorem schema over the fixed foundation: every primitive
-- characteristic specialization is checked against its exact membership
-- formula before the source telescope is composed.  The caller cannot supply
-- either the resulting proposition or its cache descriptor.
namedSetConstructionObjectFact
    :: SetConstructionFoundation
    -> ObjectId
    -> NamedSetConstruction ObjectId
    -> Maybe NamedSetConstructionFact
namedSetConstructionObjectFact foundation object construction = do
    let context = constructionContext construction
        objectType = foldr TyArrow TySet (reverse context)
    globals <- insertGlobalType object objectType (constructionGlobals construction)
    let withObject = replaceConstructionGlobals globals construction
        target =
            foldl
                CApp
                (CGlobal object)
                [ CBound (fromIntegral index)
                | index <- reverse [0 .. length context - 1]
                ]
    view <- extensionalView foundation target withObject
    proposition <- freezeDerived withObject TyProp
        (foldl
            (flip CForall)
            (scopedCoreTerm view)
            context)
    selfView <- extensionalView
        foundation
        (constructionCanonicalTerm construction)
        construction
    closedSelf <- freezeDerived construction TyProp
        (foldl
            (flip CForall)
            (scopedCoreTerm selfView)
            context)
    let closedBody = namedSetConstructionClosedBody construction
        descriptor =
            hashCacheFields
                "felix-checked-named-set-construction-v1"
                [ encodeCache do
                    putCoreTypeCache (frozenCoreType closedBody)
                    putCanonicalTermCache putObjectIdCache
                        (frozenCoreTerm closedBody)
                , encodeCache do
                    putCanonicalTermCache putObjectIdCache
                        (frozenCoreTerm closedSelf)
                ]
    pure (NamedSetConstructionFact proposition descriptor)

relationalSetConstructionTerm
    :: Ord global
    => CheckedRelationalSetConstruction global
    -> ScopedCheckedCore global
relationalSetConstructionTerm construction =
    fromMaybe
        (impossible "a checked relational construction lost its canonical term")
        (checkedRelationalDerived
            construction
            (relationalConstructionContext construction)
            TySet
            (relationalConstructionCanonicalTerm construction))

-- | The exact source functionality obligation, still scoped by the outer
-- definition parameters.  It is discharged independently before the derived
-- extensional theorem can be authorized.
relationalSetConstructionFunctionality
    :: Ord global
    => CheckedRelationalSetConstruction global
    -> ScopedCheckedCore global
relationalSetConstructionFunctionality construction =
    fromMaybe
        (impossible "a checked relational construction lost functionality")
        (checkedRelationalDerived
            construction
            (relationalConstructionContext construction)
            TyProp
            (relationalConstructionFunctionalityTerm construction))

relationalSetConstructionClosedFunctionality
    :: Ord global
    => CheckedRelationalSetConstruction global
    -> FrozenCheckedCore global
relationalSetConstructionClosedFunctionality =
    closeRelationalFunctionality

-- | Introduce a fresh named set.  Assuming the exact checked functionality
-- proposition, derive its two local views; no arbitrary proposition can
-- unlock the extensional view.  The enclosing proof transaction owns the
-- corresponding authority.
relationalSetConstructionLocalViews
    :: Ord global
    => SetConstructionFoundation
    -> CheckedRelationalSetConstruction global
    -> ScopedCheckedCore global
    -> Maybe
        ( ScopedCheckedCore global
        , ScopedCheckedCore global
        )
relationalSetConstructionLocalViews foundation construction functionality = do
    guard
        (functionality
            == relationalSetConstructionFunctionality construction)
    validateRelationalSchema foundation construction
    let context = TySet : relationalConstructionContext construction
        target = CBound 0
    extensional <- checkedRelationalDerived construction context TyProp
        (CForall TySet
            (CEq TyProp
                (member (CBound 0) (CBound 1))
                (shiftCanonical 1 1
                    (relationalConstructionMembershipTerm construction))))
    equation <- checkedRelationalDerived construction context TyProp
        (CEq TySet
            target
            (shiftCanonical 1 0
                (relationalConstructionCanonicalTerm construction)))
    pure (extensional, equation)

relationalSetConstructionClosedBody
    :: Ord global
    => CheckedRelationalSetConstruction global
    -> FrozenCheckedCore global
relationalSetConstructionClosedBody construction =
    fromMaybe
        (impossible "a checked relational construction did not close")
        (freezeRelationalDerived construction closedType closedTerm)
  where
    context = relationalConstructionContext construction
    closedType = foldr TyArrow TySet (reverse context)
    closedTerm =
        foldl
            (flip CLam)
            (relationalConstructionCanonicalTerm construction)
            context

data RelationalSetConstructionFact = RelationalSetConstructionFact
    !(FrozenCheckedCore ObjectId)
    !CacheDigest

relationalSetConstructionFactProposition
    :: RelationalSetConstructionFact
    -> FrozenCheckedCore ObjectId
relationalSetConstructionFactProposition
        (RelationalSetConstructionFact proposition _descriptor) =
    proposition

relationalSetConstructionFactDescriptor
    :: RelationalSetConstructionFact
    -> CacheDigest
relationalSetConstructionFactDescriptor
        (RelationalSetConstructionFact _proposition descriptor) =
    descriptor

-- | The direct relational schema is a deterministic theorem over the fixed
-- separation, choice-witness, and replacement rows.  The functionality fact
-- is a real strictly-earlier candidate: its exact proposition is checked here
-- and its authority safety is consumed separately by declaration admission.
relationalSetConstructionObjectFact
    :: SetConstructionFoundation
    -> ObjectId
    -> CheckedRelationalSetConstruction ObjectId
    -> FrozenCheckedCore ObjectId
    -> Maybe RelationalSetConstructionFact
relationalSetConstructionObjectFact
        foundation object construction functionality = do
    let expectedFunctionality =
            closeRelationalFunctionality construction
    guard (functionality == expectedFunctionality)
    validateRelationalSchema foundation construction
    let context = relationalConstructionContext construction
        objectType = foldr TyArrow TySet (reverse context)
    globals <- insertGlobalType
        object objectType (relationalConstructionGlobals construction)
    let withObject = replaceRelationalGlobals globals construction
        target =
            foldl
                CApp
                (CGlobal object)
                [ CBound (fromIntegral index)
                | index <- reverse [0 .. length context - 1]
                ]
        membership = relationalConstructionMembershipTerm withObject
    proposition <- freezeRelationalDerived withObject TyProp
        (foldl
            (flip CForall)
            (CForall TySet
                (CEq TyProp
                    (member (CBound 0) (shiftCanonical 1 0 target))
                    membership))
            context)
    closedSelf <- freezeRelationalDerived construction TyProp
        (foldl
            (flip CForall)
            (CForall TySet
                (CEq TyProp
                    (member
                        (CBound 0)
                        (shiftCanonical 1 0
                            (relationalConstructionCanonicalTerm construction)))
                    (relationalConstructionMembershipTerm construction)))
            context)
    let closedBody = relationalSetConstructionClosedBody construction
        descriptor =
            hashCacheFields
                "felix-checked-named-relational-set-construction-v1"
                [ encodeCache do
                    putCoreTypeCache (frozenCoreType closedBody)
                    putCanonicalTermCache putObjectIdCache
                        (frozenCoreTerm closedBody)
                , encodeCache do
                    putCanonicalTermCache putObjectIdCache
                        (frozenCoreTerm expectedFunctionality)
                , encodeCache do
                    putCanonicalTermCache putObjectIdCache
                        (frozenCoreTerm closedSelf)
                ]
    pure (RelationalSetConstructionFact proposition descriptor)

finishConstruction
    :: Ord global
    => [CoreType]
    -> Map.Map global CoreType
    -> NamedSetConstructionShape global
    -> Maybe (NamedSetConstruction global)
finishConstruction context globals shape = do
    let built = buildConstruction context shape
        construction = NamedSetConstruction context globals shape built
    _ <- checkedDerived construction context TySet
        (builtConstructionTerm built)
    _ <- checkedDerived construction (TySet : context) TyProp
        (builtConstructionMembership built)
    pure construction

buildConstruction
    :: Eq global
    => [CoreType]
    -> NamedSetConstructionShape global
    -> BuiltSetConstruction global
buildConstruction context = \case
    SeparationShape bound predicate ->
        let function = CLam TySet predicate
            term = applyIntrinsic2 Sep bound function
            membership =
                logicalAnd
                    (member (CBound 0) (shiftCanonical 1 0 bound))
                    predicate
            application =
                characteristicApplication
                    Sep context term
                    [ (TySet, bound)
                    , (TyArrow TySet TyProp, function)
                    ]
        in BuiltSetConstruction term membership [application]
    FunctionalReplacementShape domains value condition ->
        let (term, applications) =
                buildFunctionalReplacement
                    context
                    domains
                    value
                    condition
            domainList = NonEmpty.toList domains
            binderCount = length domainList
            terminal =
                logicalConjunction
                    ( maybeToList
                        (shiftCanonical 1 (fromIntegral binderCount)
                            <$> condition)
                      <> [ CEq TySet
                            (CBound (fromIntegral binderCount))
                            (shiftCanonical 1
                                (fromIntegral binderCount)
                                value)
                         ]
                    )
            membership =
                foldr
                    (\(depth, domain) rest ->
                        logicalExists
                            (logicalAnd
                                (member
                                    (CBound 0)
                                    (shiftCanonical 1 0
                                        (shiftCanonical 1 depth domain)))
                                rest))
                    terminal
                    (zip [0 :: Natural ..] domainList)
        in BuiltSetConstruction term membership applications

-- The only functional-replacement term builder.  Its result is reused by the
-- transparent body, exact-content matching, characteristic validation, local
-- views, and descriptor derivation.
buildFunctionalReplacement
    :: [CoreType]
    -> NonEmpty (CanonicalTerm global)
    -> CanonicalTerm global
    -> Maybe (CanonicalTerm global)
    -> (CanonicalTerm global, [CheckedCharacteristicApplication global])
buildFunctionalReplacement context (domain :| remaining) value condition =
    case remaining of
        [] ->
            let predicate = CLam TySet <$> condition
                filtered = maybe domain
                    (applyIntrinsic2 Sep domain)
                    predicate
                function = CLam TySet value
                replacement = applyIntrinsic2 Repl filtered function
                separationApplications = case predicate of
                    Nothing -> []
                    Just checkedPredicate ->
                        [ characteristicApplication
                            Sep context filtered
                            [ (TySet, domain)
                            , (TyArrow TySet TyProp, checkedPredicate)
                            ]
                        ]
                replacementApplication =
                    characteristicApplication
                        Repl context replacement
                        [ (TySet, filtered)
                        , (TyArrow TySet TySet, function)
                        ]
            in
                ( replacement
                , separationApplications <> [replacementApplication]
                )
        next : rest ->
            let (nested, nestedApplications) =
                    buildFunctionalReplacement
                        (TySet : context)
                        (next :| rest)
                        value
                        condition
                function = CLam TySet nested
                replacement = applyIntrinsic2 Repl domain function
                union = applyIntrinsic FamilyUnion replacement
            in
                ( union
                , characteristicApplication
                    Repl context replacement
                    [ (TySet, domain)
                    , (TyArrow TySet TySet, function)
                    ]
                  : characteristicApplication
                        FamilyUnion context union
                        [(TySet, replacement)]
                  : nestedApplications
                )

characteristicApplication
    :: CoreIntrinsicTag
    -> [CoreType]
    -> CanonicalTerm global
    -> [(CoreType, CanonicalTerm global)]
    -> CheckedCharacteristicApplication global
characteristicApplication intrinsic context target arguments =
    CheckedCharacteristicApplication
        intrinsic
        context
        target
        (NonEmpty.fromList arguments)
        (expectedCharacteristicBody intrinsic arguments)

expectedCharacteristicBody
    :: CoreIntrinsicTag
    -> [(CoreType, CanonicalTerm global)]
    -> CanonicalTerm global
expectedCharacteristicBody intrinsic arguments =
    case (intrinsic, arguments) of
        (Sep, [(_boundType, bound), (_predicateType, CLam TySet predicate)]) ->
            logicalAnd
                (member (CBound 0) (shiftCanonical 2 0 bound))
                (shiftCanonical 1 1 predicate)
        (Repl, [(_domainType, domain), (_functionType, CLam TySet value)]) ->
            logicalExists
                (logicalAnd
                    (member (CBound 0) (shiftCanonical 3 0 domain))
                    (CEq TySet
                        (CBound 1)
                        (shiftCanonical 2 1 value)))
        (FamilyUnion, [(_familyType, family)]) ->
            logicalExists
                (logicalAnd
                    (member (CBound 0) (shiftCanonical 3 0 family))
                    (member (CBound 1) (CBound 0)))
        _ ->
            impossible "invalid checked set-construction characteristic"

extensionalView
    :: Ord global
    => SetConstructionFoundation
    -> CanonicalTerm global
    -> NamedSetConstruction global
    -> Maybe (ScopedCheckedCore global)
extensionalView foundation target construction = do
    traverse_
        (validateCharacteristic foundation construction)
        (constructionApplications construction)
    checkedDerived construction (constructionContext construction) TyProp
        (CForall TySet
            (CEq TyProp
                (member (CBound 0) (shiftCanonical 1 0 target))
                (constructionMembership construction)))

-- Each primitive step is specialized from the actual fixed row, and the
-- complete normalized membership body is checked.  The final flattened view
-- is then the deterministic composition of these exact primitive schemas.
validateCharacteristic
    :: Ord global
    => SetConstructionFoundation
    -> NamedSetConstruction global
    -> CheckedCharacteristicApplication global
    -> Maybe ()
validateCharacteristic foundation construction
        (CheckedCharacteristicApplication
            intrinsic context targetTerm argumentTerms expectedBody) = do
    row <- characteristicRow foundation intrinsic
    target <- checkedDerived construction context TySet targetTerm
    arguments <- traverse
        (\(coreType, term) ->
            checkedDerived construction context coreType term)
        argumentTerms
    specialized <- scopedCharacteristicDefinition row target arguments
    case scopedCoreTerm specialized of
        CForall TySet
                (CEq TyProp actualMembership actualBody)
            | actualMembership
                == member (CBound 0) (CBound 1)
            , scopedCoreContext specialized
                == TySet : scopedCoreContext target
            , actualBody == expectedBody ->
                pure ()
        _ -> Nothing

characteristicRow
    :: SetConstructionFoundation
    -> CoreIntrinsicTag
    -> Maybe (FrozenCheckedCore Void)
characteristicRow
        (SetConstructionFoundation
            familyUnion separation replacement _setChoose) =
    \case
        FamilyUnion -> Just familyUnion
        Sep -> Just separation
        Repl -> Just replacement
        _ -> Nothing

validateRelationalSchema
    :: Ord global
    => SetConstructionFoundation
    -> CheckedRelationalSetConstruction global
    -> Maybe ()
validateRelationalSchema foundation construction = do
    traverse_
        (validateRelationalCharacteristic foundation construction)
        (relationalConstructionApplications construction)
    validateChoiceWitness foundation construction

validateRelationalCharacteristic
    :: Ord global
    => SetConstructionFoundation
    -> CheckedRelationalSetConstruction global
    -> CheckedCharacteristicApplication global
    -> Maybe ()
validateRelationalCharacteristic foundation construction
        (CheckedCharacteristicApplication
            intrinsic context targetTerm argumentTerms expectedBody) = do
    row <- characteristicRow foundation intrinsic
    target <- checkedRelationalDerived construction context TySet targetTerm
    arguments <- traverse
        (\(coreType, term) ->
            checkedRelationalDerived construction context coreType term)
        argumentTerms
    specialized <- scopedCharacteristicDefinition row target arguments
    case scopedCoreTerm specialized of
        CForall TySet (CEq TyProp actualMembership actualBody)
            | actualMembership == member (CBound 0) (CBound 1)
            , scopedCoreContext specialized
                == TySet : scopedCoreContext target
            , actualBody == expectedBody ->
                pure ()
        _ -> Nothing

validateChoiceWitness
    :: Ord global
    => SetConstructionFoundation
    -> CheckedRelationalSetConstruction global
    -> Maybe ()
validateChoiceWitness
        (SetConstructionFoundation
            _familyUnion _separation _replacement setChoose)
        construction = do
    let context = relationalConstructionContext construction
        relation = relationalConstructionRelation construction
        predicate = CLam TySet relation
        choice = applyIntrinsic SetChoose predicate
        witnessContext = TySet : TySet : context
    target <- checkedRelationalDerived construction witnessContext TySet
        (shiftCanonical 1 0 choice)
    checkedPredicate <-
        checkedRelationalDerived construction witnessContext
            (TyArrow TySet TyProp)
            (shiftCanonical 1 0 predicate)
    witness <- checkedRelationalDerived construction witnessContext TySet
        (CBound 0)
    specialized <-
        scopedCharacteristicDefinition
            setChoose target (checkedPredicate :| [witness])
    guard
        (scopedCoreContext specialized
            == TySet : TySet : TySet : context)
    guard
        (scopedCoreTerm specialized
            == CImp
                (shiftCanonical 1 0 relation)
                (shiftCanonical 1 1 relation))

relationalFunctionality
    :: CanonicalTerm global
    -> CanonicalTerm global
    -> CanonicalTerm global
relationalFunctionality domain relation =
    CForall TySet
        (CImp
            (member (CBound 0) (shiftCanonical 1 0 domain))
            (CForall TySet
                (CForall TySet
                    (CImp
                        (logicalAnd
                            (shiftCanonical 1 0 relation)
                            (shiftCanonical 1 1 relation))
                        (CEq TySet (CBound 1) (CBound 0))))))

relationalMembership
    :: CanonicalTerm global
    -> CanonicalTerm global
    -> CanonicalTerm global
relationalMembership domain relation =
    logicalExists
        (logicalAnd
            (member (CBound 0) (shiftCanonical 2 0 domain))
            (applyRelation
                (shiftCanonical 2 0
                    (CLam TySet (CLam TySet relation)))
                (CBound 0)
                (CBound 1)))
  where
    applyRelation function domainValue rangeValue =
        case function of
            CLam TySet domainBody ->
                case instantiateCanonical domainValue domainBody of
                    CLam TySet rangeBody ->
                        instantiateCanonical rangeValue rangeBody
                    _ -> impossible "a checked relation lost its range binder"
            _ -> impossible "a checked relation lost its domain binder"

weakenConstruction
    :: Ord global
    => CoreType
    -> NamedSetConstruction global
    -> Maybe (NamedSetConstruction global)
weakenConstruction binderType construction =
    finishConstruction
        (binderType : constructionContext construction)
        (constructionGlobals construction)
        (case constructionShape construction of
            SeparationShape bound predicate ->
                SeparationShape
                    (shiftCanonical 1 0 bound)
                    (shiftCanonical 1 1 predicate)
            FunctionalReplacementShape domains value condition ->
                let domainList = NonEmpty.toList domains
                    weakenedDomains =
                        zipWith
                            (\depth domain -> shiftCanonical 1 depth domain)
                            [0..]
                            domainList
                    binderDepth = fromIntegral (length domainList)
                in FunctionalReplacementShape
                    (NonEmpty.fromList weakenedDomains)
                    (shiftCanonical 1 binderDepth value)
                    (shiftCanonical 1 binderDepth <$> condition))

captureGlobalTypes
    :: Ord global
    => (global -> Maybe CoreType)
    -> [CanonicalTerm global]
    -> Maybe (Map.Map global CoreType)
captureGlobalTypes globalType terms =
    Map.fromList <$> traverse capture
        (Set.toAscList (foldMap canonicalTermGlobals terms))
  where
    capture global = do
        coreType <- globalType global
        pure (global, coreType)

insertGlobalType
    :: Ord global
    => global
    -> CoreType
    -> Map.Map global CoreType
    -> Maybe (Map.Map global CoreType)
insertGlobalType global coreType globals =
    case Map.lookup global globals of
        Nothing -> Just (Map.insert global coreType globals)
        Just existing
            | existing == coreType -> Just globals
            | otherwise -> Nothing

checkedDerived
    :: Ord global
    => NamedSetConstruction global
    -> [CoreType]
    -> CoreType
    -> CanonicalTerm global
    -> Maybe (ScopedCheckedCore global)
checkedDerived construction context expected term = do
    checked <- either (const Nothing) Just
        (checkScopedCanonicalCore
            (`Map.lookup` constructionGlobals construction)
            context
            term)
    guard (scopedCoreType checked == expected)
    pure checked

freezeDerived
    :: Ord global
    => NamedSetConstruction global
    -> CoreType
    -> CanonicalTerm global
    -> Maybe (FrozenCheckedCore global)
freezeDerived construction expected term = do
    checked <- checkedDerived construction [] expected term
    closeScopedCore checked

checkedRelationalDerived
    :: Ord global
    => CheckedRelationalSetConstruction global
    -> [CoreType]
    -> CoreType
    -> CanonicalTerm global
    -> Maybe (ScopedCheckedCore global)
checkedRelationalDerived construction context expected term = do
    checked <- either (const Nothing) Just
        (checkScopedCanonicalCore
            (`Map.lookup` relationalConstructionGlobals construction)
            context
            term)
    guard (scopedCoreType checked == expected)
    pure checked

freezeRelationalDerived
    :: Ord global
    => CheckedRelationalSetConstruction global
    -> CoreType
    -> CanonicalTerm global
    -> Maybe (FrozenCheckedCore global)
freezeRelationalDerived construction expected term = do
    checked <- checkedRelationalDerived construction [] expected term
    closeScopedCore checked

closeRelationalFunctionality
    :: Ord global
    => CheckedRelationalSetConstruction global
    -> FrozenCheckedCore global
closeRelationalFunctionality construction =
    fromMaybe
        (impossible "a relational functionality proposition did not close")
        (freezeRelationalDerived construction TyProp
            (foldl
                (flip CForall)
                (relationalConstructionFunctionalityTerm construction)
                (relationalConstructionContext construction)))

replaceRelationalGlobals
    :: Map.Map global CoreType
    -> CheckedRelationalSetConstruction global
    -> CheckedRelationalSetConstruction global
replaceRelationalGlobals globals
        (CheckedRelationalSetConstruction
            context _oldGlobals domain relation term functionality
            membership applications) =
    CheckedRelationalSetConstruction
        context globals domain relation term functionality membership applications

relationalConstructionContext
    :: CheckedRelationalSetConstruction global
    -> [CoreType]
relationalConstructionContext
        (CheckedRelationalSetConstruction
            context _globals _domain _relation _term _functionality
            _membership _applications) =
    context

relationalConstructionGlobals
    :: CheckedRelationalSetConstruction global
    -> Map.Map global CoreType
relationalConstructionGlobals
        (CheckedRelationalSetConstruction
            _context globals _domain _relation _term _functionality
            _membership _applications) =
    globals

relationalConstructionRelation
    :: CheckedRelationalSetConstruction global
    -> CanonicalTerm global
relationalConstructionRelation
        (CheckedRelationalSetConstruction
            _context _globals _domain relation _term _functionality
            _membership _applications) =
    relation

relationalConstructionCanonicalTerm
    :: CheckedRelationalSetConstruction global
    -> CanonicalTerm global
relationalConstructionCanonicalTerm
        (CheckedRelationalSetConstruction
            _context _globals _domain _relation term _functionality
            _membership _applications) =
    term

relationalConstructionFunctionalityTerm
    :: CheckedRelationalSetConstruction global
    -> CanonicalTerm global
relationalConstructionFunctionalityTerm
        (CheckedRelationalSetConstruction
            _context _globals _domain _relation _term functionality
            _membership _applications) =
    functionality

relationalConstructionMembershipTerm
    :: CheckedRelationalSetConstruction global
    -> CanonicalTerm global
relationalConstructionMembershipTerm
        (CheckedRelationalSetConstruction
            _context _globals _domain _relation _term _functionality
            membership _applications) =
    membership

relationalConstructionApplications
    :: CheckedRelationalSetConstruction global
    -> [CheckedCharacteristicApplication global]
relationalConstructionApplications
        (CheckedRelationalSetConstruction
            _context _globals _domain _relation _term _functionality
            _membership applications) =
    applications

replaceConstructionGlobals
    :: Map.Map global CoreType
    -> NamedSetConstruction global
    -> NamedSetConstruction global
replaceConstructionGlobals globals
        (NamedSetConstruction context _oldGlobals shape built) =
    NamedSetConstruction context globals shape built

constructionContext :: NamedSetConstruction global -> [CoreType]
constructionContext (NamedSetConstruction context _globals _shape _built) =
    context

constructionGlobals
    :: NamedSetConstruction global
    -> Map.Map global CoreType
constructionGlobals (NamedSetConstruction _context globals _shape _built) =
    globals

constructionShape
    :: NamedSetConstruction global
    -> NamedSetConstructionShape global
constructionShape (NamedSetConstruction _context _globals shape _built) =
    shape

constructionCanonicalTerm
    :: NamedSetConstruction global
    -> CanonicalTerm global
constructionCanonicalTerm
        (NamedSetConstruction _context _globals _shape built) =
    builtConstructionTerm built

constructionMembership
    :: NamedSetConstruction global
    -> CanonicalTerm global
constructionMembership
        (NamedSetConstruction _context _globals _shape built) =
    builtConstructionMembership built

constructionApplications
    :: NamedSetConstruction global
    -> [CheckedCharacteristicApplication global]
constructionApplications
        (NamedSetConstruction _context _globals _shape built) =
    builtConstructionApplications built

builtConstructionTerm
    :: BuiltSetConstruction global
    -> CanonicalTerm global
builtConstructionTerm (BuiltSetConstruction term _membership _applications) =
    term

builtConstructionMembership
    :: BuiltSetConstruction global
    -> CanonicalTerm global
builtConstructionMembership
        (BuiltSetConstruction _term membership _applications) =
    membership

builtConstructionApplications
    :: BuiltSetConstruction global
    -> [CheckedCharacteristicApplication global]
builtConstructionApplications
        (BuiltSetConstruction _term _membership applications) =
    applications

applyIntrinsic
    :: CoreIntrinsicTag
    -> CanonicalTerm global
    -> CanonicalTerm global
applyIntrinsic intrinsic argument =
    CApp (CIntrinsic intrinsic) argument

applyIntrinsic2
    :: CoreIntrinsicTag
    -> CanonicalTerm global
    -> CanonicalTerm global
    -> CanonicalTerm global
applyIntrinsic2 intrinsic first second =
    CApp (CApp (CIntrinsic intrinsic) first) second

member
    :: CanonicalTerm global
    -> CanonicalTerm global
    -> CanonicalTerm global
member = applyIntrinsic2 Member

logicalNot :: CanonicalTerm global -> CanonicalTerm global
logicalNot proposition = CImp proposition CFalsum

logicalAnd
    :: CanonicalTerm global
    -> CanonicalTerm global
    -> CanonicalTerm global
logicalAnd left right =
    logicalNot (CImp left (logicalNot right))

logicalTruth :: CanonicalTerm global
logicalTruth = CImp CFalsum CFalsum

logicalConjunction
    :: Eq global
    => [CanonicalTerm global]
    -> CanonicalTerm global
logicalConjunction =
    foldr combine logicalTruth
  where
    combine proposition remaining
        | proposition == logicalTruth = remaining
        | remaining == logicalTruth = proposition
        | otherwise = logicalAnd proposition remaining

logicalExists :: CanonicalTerm global -> CanonicalTerm global
logicalExists body =
    logicalNot (CForall TySet (logicalNot body))