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
|
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
-- | Explicit inputs and sealed outputs for the typed module driver.
module Felix.Checking.Module
( LiveModuleBinding(..)
, IdentifiedModuleInput
, identifiedPhysicalModule
, identifiedReservedPrelude
, identifiedModuleOwner
, identifiedModuleBinding
, identifiedModuleParsed
, TypedSourceDeclaration
, typedSourceDeclarationBlockIndex
, typedSourceDeclarationHead
, typedSourceDeclarationProof
, typedSourceDeclarations
, SealedTypedModule
, sealedTypedModuleOwner
, sealedTypedModuleSyntax
, sealedTypedModuleSemantic
, sealedTypedModuleEvidence
, CachedTypedModuleError(..)
, renderCachedTypedModuleError
, cachedSealedTypedModule
, sealedTypedModulePrefix
, FinalPreludeSession
, ModuleRootAcquisition(..)
, finalPreludeSource
, finalPreludeInput
, finalPreludeModule
, finalPreludeAcquisition
, FinalPreludeReadiness
, finalPreludeReadiness
, BootstrapPreludeFixture
, bootstrapPreludeInput
, bootstrapPreludeModule
, bootstrapPreludeReadiness
, fixtureFinalPreludeReadinessFromSealed
, BootstrapError(..)
, buildBootstrapPreludeFixture
, FinalPreludeReadinessError(..)
, acquireFinalPreludeSession
, TypedModuleInput
, TypedModuleInputError(..)
, renderTypedModuleInputError
, typedModuleInput
, TypedPathError(..)
, TypedModuleFailure(..)
, typedModuleFailureLocation
, renderTypedModuleFailure
, TypedModuleResult(..)
, runTypedModule
) where
import Base
import Felix.Checking.Declaration qualified as Declaration
import Felix.Checking.Exact qualified as Exact
import Felix.Checking.Exact.Datatype qualified as ExactDatatype
import Felix.Checking.Exact.Inductive qualified as ExactInductive
import Felix.Checking.Exact.Proof qualified as ExactProof
import Felix.Checking.FinalPrelude qualified as FinalPrelude
import Felix.Checking.Foundation
import Felix.Checking.Identity
import Felix.Checking.Semantic
import Felix.Module
import Felix.Parse
import Felix.Prelude qualified as Prelude
import Felix.Source
import Felix.Store qualified as Store
import Felix.Report.Location
import Felix.Syntax.Interface
import Felix.Syntax.Abstract qualified as Raw
import Control.Monad (unless)
import Data.Bifunctor (first)
import Data.Text qualified as Text
data LiveModuleBinding
= PhysicalModuleBinding !ResolvedSource
| ReservedModuleBinding !FileId !FilePath
deriving stock (Show, Eq)
data IdentifiedModuleInput = IdentifiedModuleInput
!ModuleName
!LiveModuleBinding
!IdentifiedParsedModule
identifiedPhysicalModule :: ParsedModule -> IdentifiedModuleInput
identifiedPhysicalModule parsed =
IdentifiedModuleInput
(moduleName (parsedModuleAddress parsed))
(PhysicalModuleBinding (parsedModuleResolved parsed))
(parsedModuleIdentified parsed)
identifiedReservedPrelude
:: Prelude.ReservedParsedPrelude
-> IdentifiedModuleInput
identifiedReservedPrelude reserved =
IdentifiedModuleInput
(freshModuleInputOwner input)
(ReservedModuleBinding
(freshModuleInputFileId input)
(freshModuleInputLocationPath input))
(Prelude.reservedParsedPreludeModule reserved)
where
input = Prelude.reservedParsedPreludeInput reserved
identifiedModuleOwner :: IdentifiedModuleInput -> ModuleName
identifiedModuleOwner (IdentifiedModuleInput owner _binding _parsed) =
owner
identifiedModuleBinding
:: IdentifiedModuleInput
-> LiveModuleBinding
identifiedModuleBinding
(IdentifiedModuleInput _owner binding _parsed) =
binding
identifiedModuleParsed
:: IdentifiedModuleInput
-> IdentifiedParsedModule
identifiedModuleParsed
(IdentifiedModuleInput _owner _binding parsed) =
parsed
-- | One source declaration as consumed by the typed module driver.
--
-- A claim and its immediately following proof occupy one declaration slot.
-- The head block index remains the syntax-occurrence association index.
data TypedSourceDeclaration = TypedSourceDeclaration
!Int
!Raw.Block
!(Maybe Raw.Proof)
typedSourceDeclarationBlockIndex :: TypedSourceDeclaration -> Int
typedSourceDeclarationBlockIndex
(TypedSourceDeclaration blockIndex _head _proof) =
blockIndex
typedSourceDeclarationHead :: TypedSourceDeclaration -> Raw.Block
typedSourceDeclarationHead
(TypedSourceDeclaration _blockIndex headBlock _proof) =
headBlock
typedSourceDeclarationProof
:: TypedSourceDeclaration
-> Maybe Raw.Proof
typedSourceDeclarationProof
(TypedSourceDeclaration _blockIndex _head proof) =
proof
typedSourceDeclarations
:: IdentifiedParsedModule
-> [TypedSourceDeclaration]
typedSourceDeclarations parsed =
[ declaration
| item <- typedSourceItems (identifiedParsedModuleBlocks parsed)
, Just declaration <- [sourceItemDeclaration item]
]
data TypedSourceItem
= TypedSourceDeclarationItem !TypedSourceDeclaration
| TypedUnmatchedSourceProof !Location
sourceItemDeclaration
:: TypedSourceItem
-> Maybe TypedSourceDeclaration
sourceItemDeclaration = \case
TypedSourceDeclarationItem declaration -> Just declaration
TypedUnmatchedSourceProof{} -> Nothing
typedSourceItems :: [Raw.Block] -> [TypedSourceItem]
typedSourceItems = go 0
where
go _blockIndex [] = []
go blockIndex (block@Raw.BlockClaim{} : remaining) =
case remaining of
Raw.BlockProof _location proof _end : rest ->
TypedSourceDeclarationItem
(TypedSourceDeclaration blockIndex block (Just proof))
: go (blockIndex + 2) rest
_ ->
TypedSourceDeclarationItem
(TypedSourceDeclaration blockIndex block Nothing)
: go (blockIndex + 1) remaining
go blockIndex (Raw.BlockProof location _proof _end : remaining) =
TypedUnmatchedSourceProof location
: go (blockIndex + 1) remaining
go blockIndex (block : remaining) =
TypedSourceDeclarationItem
(TypedSourceDeclaration blockIndex block Nothing)
: go (blockIndex + 1) remaining
data SealedTypedModule = SealedTypedModule
!ModuleName
!ModuleSyntaxInterface
!SemanticInterface
!Declaration.PendingModulePrefix
!Declaration.ImportedModuleEvidence
sealedTypedModuleOwner :: SealedTypedModule -> ModuleName
sealedTypedModuleOwner (SealedTypedModule owner _syntax _semantic _prefix _evidence) =
owner
sealedTypedModuleSyntax
:: SealedTypedModule
-> ModuleSyntaxInterface
sealedTypedModuleSyntax
(SealedTypedModule _owner syntax _semantic _prefix _evidence) =
syntax
sealedTypedModuleSemantic
:: SealedTypedModule
-> SemanticInterface
sealedTypedModuleSemantic
(SealedTypedModule _owner _syntax semantic _prefix _evidence) =
semantic
sealedTypedModulePrefix
:: SealedTypedModule
-> Declaration.PendingModulePrefix
sealedTypedModulePrefix
(SealedTypedModule _owner _syntax _semantic prefix _evidence) =
prefix
sealedTypedModuleEvidence
:: SealedTypedModule
-> Declaration.ImportedModuleEvidence
sealedTypedModuleEvidence
(SealedTypedModule _owner _syntax _semantic _prefix evidence) =
evidence
data CachedTypedModuleError
= CachedTypedModuleEvidenceError !Declaration.DeclarationError
deriving stock (Show, Eq)
renderCachedTypedModuleError :: CachedTypedModuleError -> Text
renderCachedTypedModuleError = \case
CachedTypedModuleEvidenceError failure ->
Declaration.renderDeclarationError failure
cachedSealedTypedModule
:: CheckedFoundation
-> [SealedTypedModule]
-> Store.CachedModuleInstallation
-> Either CachedTypedModuleError SealedTypedModule
cachedSealedTypedModule
foundation parents installation = do
evidence <-
first CachedTypedModuleEvidenceError
(Declaration.validateImportedModuleEvidence
(theoryId foundation)
(sealedTypedModuleEvidence <$> parents)
semantic
objects
propositions)
pure
(SealedTypedModule
owner
syntax
semantic
(Declaration.emptyPendingModulePrefix
(Store.cachedInstallationFinalPrefix installation))
evidence)
where
syntax = Store.cachedInstallationSyntax installation
semantic = Store.cachedInstallationSemantic installation
owner = semanticInterfaceOwner semantic
objects = Store.cachedInstallationObjects installation
propositions = Store.cachedInstallationPropositions installation
data ModuleRootAcquisition
= ModuleRootHit
| ModuleRootMiss
deriving stock (Show, Eq)
data FinalPreludeSession = FinalPreludeSession
!Prelude.ReservedPreludeSourceInput
!IdentifiedModuleInput
!SealedTypedModule
!ModuleRootAcquisition
finalPreludeSource
:: FinalPreludeSession
-> Prelude.ReservedPreludeSourceInput
finalPreludeSource
(FinalPreludeSession source _input _module _acquisition) =
source
finalPreludeInput
:: FinalPreludeSession
-> IdentifiedModuleInput
finalPreludeInput (FinalPreludeSession _source input _module _acquisition) =
input
finalPreludeModule
:: FinalPreludeSession
-> SealedTypedModule
finalPreludeModule (FinalPreludeSession _source _input sealed _acquisition) =
sealed
finalPreludeAcquisition
:: FinalPreludeSession
-> ModuleRootAcquisition
finalPreludeAcquisition
(FinalPreludeSession _source _input _sealed acquisition) =
acquisition
newtype FinalPreludeReadiness = FinalPreludeReadiness
SealedTypedModule
finalPreludeReadiness
:: FinalPreludeSession
-> FinalPreludeReadiness
finalPreludeReadiness =
FinalPreludeReadiness . finalPreludeModule
-- | Test-only empty-prelude fixture.
--
-- It is exported for focused exact-compiler tests. Production acquisition is
-- exclusively 'acquireFinalPreludeSession'.
newtype BootstrapPreludeFixture = BootstrapPreludeFixture
FinalPreludeSession
bootstrapPreludeInput
:: BootstrapPreludeFixture
-> IdentifiedModuleInput
bootstrapPreludeInput (BootstrapPreludeFixture fixture) =
finalPreludeInput fixture
bootstrapPreludeModule
:: BootstrapPreludeFixture
-> SealedTypedModule
bootstrapPreludeModule (BootstrapPreludeFixture fixture) =
finalPreludeModule fixture
-- | Test-only seam for the empty bootstrap compiler input.
bootstrapPreludeReadiness
:: BootstrapPreludeFixture
-> FinalPreludeReadiness
bootstrapPreludeReadiness =
FinalPreludeReadiness . bootstrapPreludeModule
-- | Test-only seam for exercising a synthetic distinguished prelude.
fixtureFinalPreludeReadinessFromSealed
:: SealedTypedModule
-> FinalPreludeReadiness
fixtureFinalPreludeReadinessFromSealed =
FinalPreludeReadiness
data BootstrapError
= BootstrapParseFailed !Prelude.PreludeParseError
| BootstrapDriverOpenFailed !Declaration.DriverOpenError
| BootstrapDeclarationFailed !Declaration.DeclarationError
| BootstrapSealFailed !SemanticInterfaceError
deriving stock (Show)
-- | Construct the empty distinguished prelude used only by compiler fixtures.
buildBootstrapPreludeFixture
:: CheckedFoundation
-> Declaration.VampireResolver
-> IO (Either BootstrapError BootstrapPreludeFixture)
buildBootstrapPreludeFixture foundation resolver = do
parsedResult <-
Prelude.parseReservedPreludeSource
Prelude.emptyBootstrapSourceInput
case parsedResult of
Left err ->
pure (Left (BootstrapParseFailed err))
Right reserved -> do
let input = identifiedReservedPrelude reserved
syntax =
identifiedParsedModuleSyntaxInterface
(identifiedModuleParsed input)
driver <-
Declaration.runModuleDriver
foundation
preludeModuleName
[]
resolver
Declaration.FreshValidation
emptyDriver
pure case driver of
Left err ->
Left (BootstrapDriverOpenFailed err)
Right (Declaration.DriverSucceeded
() semantic prefix _closure) ->
Right
(BootstrapPreludeFixture
(FinalPreludeSession
Prelude.emptyBootstrapSourceInput
input
(SealedTypedModule
preludeModuleName
syntax
semantic
prefix
(Declaration.freshImportedModuleEvidence
[]
semantic
prefix))
ModuleRootMiss))
Right
(Declaration.DriverFailed
(Declaration.DriverDeclarationFailed err)
_prefix) ->
Left (BootstrapDeclarationFailed err)
Right (Declaration.DriverSealFailed err _prefix) ->
Left (BootstrapSealFailed err)
where
emptyDriver :: Declaration.ModuleDriver Void ()
emptyDriver = pure ()
data FinalPreludeReadinessError
= FinalPreludeReadinessSourceLoadFailed !Prelude.PreludeLoadError
| FinalPreludeReadinessSourceParseFailed !Prelude.PreludeParseError
| FinalPreludeReadinessBuildFailed !FinalPrelude.FinalPreludeFailure
| FinalPreludeReadinessBuildOpenFailed !Declaration.DriverOpenError
| FinalPreludeReadinessArtifactKeyFailed !ModuleArtifactKeyError
| FinalPreludeReadinessStoreFailed !Store.StoreFailure
| FinalPreludeReadinessCachedModuleFailed !CachedTypedModuleError
| FinalPreludeReadinessAcknowledgementMismatch
!ModuleArtifactResult
!ModuleArtifactResult
deriving stock (Show)
-- | Acquire the exact packaged final prelude through its ordinary module root.
--
-- Loading and parsing happen once before the root lookup. A hit is validated
-- and materialized through the generic cached-module boundary; a miss reuses
-- that parsed input for confined construction and atomic publication.
acquireFinalPreludeSession
:: Store.StoreMemo
-> Store.Store
-> CheckedFoundation
-> Declaration.VampireResolver
-> IO (Either FinalPreludeReadinessError FinalPreludeSession)
acquireFinalPreludeSession memo store foundation resolver =
Prelude.loadReservedPreludeSourceInput >>= \case
Left failure ->
pure (Left (FinalPreludeReadinessSourceLoadFailed failure))
Right source ->
Prelude.parseReservedPreludeSource source >>= \case
Left failure ->
pure (Left (FinalPreludeReadinessSourceParseFailed failure))
Right parsed ->
acquire source parsed
where
acquire source parsed =
case moduleArtifactKey
preludeModuleName
parsedId
[]
(theoryId foundation) of
Left failure ->
pure (Left (FinalPreludeReadinessArtifactKeyFailed failure))
Right key -> do
Store.loadCachedModuleInstallation
memo store key (moduleSyntaxAssertedId syntax) >>= \case
Left failure ->
pure (Left (FinalPreludeReadinessStoreFailed failure))
Right (Just installation) ->
pure do
sealed <- first FinalPreludeReadinessCachedModuleFailed
(cachedSealedTypedModule
foundation [] installation)
pure
(FinalPreludeSession
source
identified sealed ModuleRootHit)
Right Nothing ->
build source parsed key
where
identified = identifiedReservedPrelude parsed
parsedId = identifiedParsedModuleId (identifiedModuleParsed identified)
syntax = identifiedParsedModuleSyntaxInterface
(identifiedModuleParsed identified)
build source parsed key =
FinalPrelude.buildParsedFinalPreludeCandidate
foundation parsed resolver >>= \case
FinalPrelude.FinalPreludeBuildFailed failure _prefix ->
pure (Left (FinalPreludeReadinessBuildFailed failure))
FinalPrelude.FinalPreludeBuildOpenFailed failure ->
pure (Left (FinalPreludeReadinessBuildOpenFailed failure))
FinalPrelude.FinalPreludeBuilt candidate ->
publish source key candidate
FinalPrelude.FinalPreludeSourceLoadFailed failure ->
pure (Left (FinalPreludeReadinessSourceLoadFailed failure))
FinalPrelude.FinalPreludeSourceParseFailed failure ->
pure (Left (FinalPreludeReadinessSourceParseFailed failure))
publish source key candidate = do
let parsed = FinalPrelude.finalPreludeParsed candidate
identified = identifiedReservedPrelude parsed
syntax = FinalPrelude.finalPreludeSyntax candidate
semantic = FinalPrelude.finalPreludeSemantic candidate
prefix = FinalPrelude.finalPreludePrefix candidate
artifact =
moduleArtifactResult
key
(moduleSyntaxAssertedId syntax)
(semanticInterfaceAssertedId semantic)
Store.writeSealedModule
store
prefix
[syntax]
[semantic]
artifact >>= \case
Left failure ->
pure (Left (FinalPreludeReadinessStoreFailed failure))
Right acknowledged
| acknowledged /= artifact ->
pure
(Left
(FinalPreludeReadinessAcknowledgementMismatch
artifact
acknowledged))
| otherwise ->
pure
(Right
(FinalPreludeSession
source
identified
(SealedTypedModule
preludeModuleName
syntax
semantic
prefix
(Declaration.freshImportedModuleEvidence
[]
semantic
prefix))
ModuleRootMiss))
data TypedModuleInput = TypedModuleInput
!ModuleName
!IdentifiedModuleInput
!ModuleSyntaxInterface
!CheckedFoundation
!FinalPreludeReadiness
![SealedTypedModule]
!Declaration.VampireResolver
!Declaration.ValidationRun
data TypedModuleInputError
= TypedDirectModuleMismatch ![ModuleName] ![ModuleName]
| TypedSyntaxInputMismatch ![SyntaxInterfaceId] ![SyntaxInterfaceId]
deriving stock (Show, Eq)
renderTypedModuleInputError :: TypedModuleInputError -> Text
renderTypedModuleInputError = \case
TypedDirectModuleMismatch expected actual ->
"direct module inputs differ: expected " <> shown expected
<> ", found " <> shown actual
TypedSyntaxInputMismatch expected actual ->
"direct syntax inputs differ: expected " <> shown expected
<> ", found " <> shown actual
where
shown :: Show value => value -> Text
shown = Text.pack . show
typedModuleInput
:: CheckedFoundation
-> FinalPreludeReadiness
-> Declaration.VampireResolver
-> Declaration.ValidationRun
-> ParsedModule
-> [SealedTypedModule]
-> Either TypedModuleInputError TypedModuleInput
typedModuleInput
foundation readiness resolver validationRun parsed direct = do
unless (expectedOwners == actualOwners)
(Left
(TypedDirectModuleMismatch
expectedOwners
actualOwners))
unless (expectedSyntax == actualSyntax)
(Left
(TypedSyntaxInputMismatch
expectedSyntax
actualSyntax))
pure
(TypedModuleInput
owner
identified
syntax
foundation
readiness
direct
resolver
validationRun)
where
identified = identifiedPhysicalModule parsed
owner = identifiedModuleOwner identified
syntax = identifiedParsedModuleSyntaxInterface (identifiedModuleParsed identified)
expectedOwners =
moduleName
<$> nubOrd
(parsedImportedAddress
<$> parsedModuleImports parsed)
actualOwners = sealedTypedModuleOwner <$> direct
expectedSyntax =
nubOrd
( moduleSyntaxAssertedId
(sealedTypedModuleSyntax prelude)
: ( moduleSyntaxAssertedId
. sealedTypedModuleSyntax
<$> direct
)
)
actualSyntax = moduleSyntaxDirectInputs syntax
FinalPreludeReadiness prelude = readiness
data TypedPathError
= TypedExactCompileFailed !Exact.ExactCompileError
| TypedExactDatatypeFailed !ExactDatatype.ExactDatatypeError
| TypedExactInductiveFailed !ExactInductive.ExactInductiveError
| TypedExactProofFailed !ExactProof.ExactProofError
| TypedUnmatchedProof !Location
deriving stock (Show, Eq)
data TypedModuleFailure
= TypedDeclarationFailed !Declaration.DeclarationError
| TypedActionFailed !TypedPathError
| TypedSealFailed !SemanticInterfaceError
deriving stock (Show, Eq)
typedModuleFailureLocation :: TypedModuleFailure -> Maybe Location
typedModuleFailureLocation = \case
TypedActionFailed (TypedExactCompileFailed failure) ->
Just (Exact.exactCompileErrorLocation failure)
TypedActionFailed (TypedExactDatatypeFailed failure) ->
Just (ExactDatatype.exactDatatypeErrorLocation failure)
TypedActionFailed (TypedExactInductiveFailed failure) ->
Just (ExactInductive.exactInductiveErrorLocation failure)
TypedActionFailed (TypedExactProofFailed failure) ->
Just (ExactProof.exactProofErrorLocation failure)
TypedActionFailed (TypedUnmatchedProof location) ->
Just location
TypedDeclarationFailed failure ->
Declaration.declarationErrorLocation failure
TypedSealFailed{} ->
Nothing
renderTypedModuleFailure :: TypedModuleFailure -> Text
renderTypedModuleFailure = \case
TypedDeclarationFailed failure ->
Declaration.renderDeclarationError failure
TypedActionFailed (TypedExactCompileFailed failure) ->
Exact.renderExactCompileError failure
TypedActionFailed (TypedExactDatatypeFailed failure) ->
ExactDatatype.renderExactDatatypeError failure
TypedActionFailed (TypedExactInductiveFailed failure) ->
ExactInductive.renderExactInductiveError failure
TypedActionFailed (TypedExactProofFailed failure) ->
ExactProof.renderExactProofError failure
TypedActionFailed (TypedUnmatchedProof location) ->
locationToText location
<> ": this proof does not follow a claim"
TypedSealFailed failure ->
renderSemanticInterfaceError failure
data TypedModuleResult
= TypedModuleOpenFailed !Declaration.DriverOpenError
| TypedModuleSucceeded !SealedTypedModule
| TypedModuleFailed
!TypedModuleFailure
!Declaration.PendingModulePrefix
data PlannedTypedDeclaration
= PlannedBinding
!(Declaration.PlannedDeclaration
Exact.CheckedExactBindingAuthorization)
| PlannedSourceAxiom
!(Declaration.PlannedDeclaration ())
| PlannedInductive
!(Declaration.PlannedDeclaration
ExactInductive.CheckedExactInductiveAuthorization)
| PlannedDatatype
!(Declaration.PlannedDeclaration
ExactDatatype.CheckedExactDatatypeAuthorization)
| PlannedStructure
!(Declaration.PlannedDeclaration
Exact.CheckedExactStructureAuthorization)
| PlannedProof
!(Declaration.PlannedDeclaration
ExactProof.CheckedExactProofAuthorization)
data TypedPlanningFailure
= TypedPlanningPathFailure !TypedPathError
| TypedPlanningDeclarationFailure !Declaration.DeclarationError
data TypedModulePlan = TypedModulePlan
![PlannedTypedDeclaration]
!(Maybe TypedPlanningFailure)
runTypedModule :: TypedModuleInput -> IO TypedModuleResult
runTypedModule
(TypedModuleInput
owner identified syntax foundation readiness direct resolver
validationRun) = do
let FinalPreludeReadiness prelude = readiness
action = do
traverse_
(Declaration.importSealedModuleDriver
. sealedTypedModuleEvidence)
effectiveDirect
TypedModulePlan planned terminal <-
Declaration.runProspectiveLoweringDriver
(planSourceItems []
(typedSourceItems
(identifiedParsedModuleBlocks
(identifiedModuleParsed identified))))
traverse_ admitPlannedDeclaration planned
traverse_ failPlanning terminal
semanticDirect =
semanticInterfaceAssertedId
(sealedTypedModuleSemantic prelude)
: ( semanticInterfaceAssertedId
. sealedTypedModuleSemantic
<$> direct
)
effectiveDirect =
prelude : direct
occurrences =
identifiedParsedModuleSyntaxOccurrences
(identifiedModuleParsed identified)
planSourceItems completed [] =
pure (TypedModulePlan (reverse completed) Nothing)
planSourceItems completed (item : remaining) =
case item of
TypedUnmatchedSourceProof location ->
pure
(TypedModulePlan
(reverse completed)
(Just
(TypedPlanningPathFailure
(TypedUnmatchedProof location))))
TypedSourceDeclarationItem declaration -> do
planDeclaration declaration >>= \case
Left failure ->
pure
(TypedModulePlan
(reverse completed)
(Just failure))
Right planned ->
planSourceItems (planned : completed) remaining
planDeclaration sourceDeclaration =
case block of
Raw.BlockClaim{} ->
planClaim block explicitProof
Raw.BlockProof{} ->
impossible
"typed declaration association retained a proof head"
Raw.BlockSig{} ->
planSelected block
Raw.BlockAbbr{} ->
planSelected block
Raw.BlockDefn{} ->
planSelected block
Raw.BlockAxiom{} ->
planSourceAxiom block
Raw.BlockInductive{} ->
planInductive block
Raw.BlockData{} ->
planDatatype block
Raw.BlockStruct{} ->
planStructure block
where
blockIndex =
typedSourceDeclarationBlockIndex sourceDeclaration
block = typedSourceDeclarationHead sourceDeclaration
explicitProof =
typedSourceDeclarationProof sourceDeclaration
planSelected selected = do
prepared <-
Exact.prepareExactDeclaration
selected
[ parsedSyntaxOccurrenceEntry occurrence
| occurrence <- occurrences
, parsedSyntaxOccurrenceBlockIndex occurrence
== blockIndex
]
case prepared of
Left failure ->
pure
(Left
(TypedPlanningPathFailure
(TypedExactCompileFailed failure)))
Right declaration -> do
Exact.lowerPreparedExactBinding declaration >>= \case
Left failure -> planningDeclarationFailure failure
Right checked ->
planChecked PlannedBinding checked
planSourceAxiom selected = do
prepared <- Exact.prepareExactSourceAxiom selected
case prepared of
Left failure ->
pure
(Left
(TypedPlanningPathFailure
(TypedExactCompileFailed failure)))
Right axiom -> do
Exact.lowerPreparedExactSourceAxiom axiom >>= \case
Left failure -> planningDeclarationFailure failure
Right checked ->
planChecked PlannedSourceAxiom checked
planInductive selected = do
prepared <-
ExactInductive.prepareExactInductive
foundation
selected
[ parsedSyntaxOccurrenceEntry occurrence
| occurrence <- occurrences
, parsedSyntaxOccurrenceBlockIndex occurrence
== blockIndex
]
case prepared of
Left failure ->
pure
(Left
(TypedPlanningPathFailure
(TypedExactInductiveFailed failure)))
Right inductive -> do
ExactInductive.lowerPreparedExactInductive inductive
>>= \case
Left failure ->
planningDeclarationFailure failure
Right checked ->
planChecked PlannedInductive checked
planDatatype selected = do
prepared <-
ExactDatatype.prepareExactDatatype
selected
[ ( parsedSyntaxOccurrenceLocation occurrence
, parsedSyntaxOccurrenceMarker occurrence
, parsedSyntaxOccurrenceEntry occurrence
)
| occurrence <- occurrences
, parsedSyntaxOccurrenceBlockIndex occurrence
== blockIndex
]
case prepared of
Left failure ->
pure
(Left
(TypedPlanningPathFailure
(TypedExactDatatypeFailed failure)))
Right datatype -> do
ExactDatatype.lowerPreparedExactDatatype datatype
>>= \case
Left failure ->
planningDeclarationFailure failure
Right checked ->
planChecked PlannedDatatype checked
planStructure selected = do
prepared <-
Exact.prepareExactStructure
selected
[ parsedSyntaxOccurrenceEntry occurrence
| occurrence <- occurrences
, parsedSyntaxOccurrenceBlockIndex occurrence
== blockIndex
]
case prepared of
Left failure ->
pure
(Left
(TypedPlanningPathFailure
(TypedExactCompileFailed failure)))
Right structure -> do
Exact.lowerPreparedExactStructure structure >>= \case
Left failure -> planningDeclarationFailure failure
Right checked ->
planChecked PlannedStructure checked
planClaim selected selectedProof = do
prepared <-
ExactProof.prepareExactProof selected selectedProof
case prepared of
Left failure ->
pure
(Left
(TypedPlanningPathFailure
(TypedExactProofFailed failure)))
Right proof -> do
ExactProof.lowerPreparedExactProof proof >>= \case
Left failure -> planningDeclarationFailure failure
Right checked ->
planChecked PlannedProof checked
planChecked
:: forall body.
(Declaration.PlannedDeclaration body
-> PlannedTypedDeclaration)
-> Declaration.CheckedDeclaration body
-> Declaration.LoweringDriver
(Either TypedPlanningFailure PlannedTypedDeclaration)
planChecked constructor checked =
Declaration.planCheckedDeclaration checked >>= \case
Left failure -> planningDeclarationFailure failure
Right planned -> pure (Right (constructor planned))
planningDeclarationFailure =
pure . Left . TypedPlanningDeclarationFailure
admitPlannedDeclaration = \case
PlannedBinding planned ->
void
(Declaration.admitPlannedCheckedDeclaration
planned Exact.authorizeCheckedExactBinding)
PlannedSourceAxiom planned ->
void
(Declaration.admitPlannedCheckedDeclaration
planned Exact.authorizeCheckedExactSourceAxiom)
PlannedInductive planned ->
void
(Declaration.admitPlannedCheckedDeclaration
planned
ExactInductive.authorizeCheckedExactInductive)
PlannedDatatype planned ->
void
(Declaration.admitPlannedCheckedDeclaration
planned ExactDatatype.authorizeCheckedExactDatatype)
PlannedStructure planned ->
void
(Declaration.admitPlannedCheckedDeclaration
planned Exact.authorizeCheckedExactStructure)
PlannedProof planned ->
void
(Declaration.admitPlannedCheckedDeclaration
planned ExactProof.authorizeCheckedExactProof)
failPlanning = \case
TypedPlanningPathFailure failure ->
Declaration.failModuleDriver failure
TypedPlanningDeclarationFailure failure ->
Declaration.failDeclarationDriver failure
result <-
Declaration.runModuleDriver
foundation
owner
semanticDirect
resolver
validationRun
action
pure case result of
Left err ->
TypedModuleOpenFailed err
Right (Declaration.DriverSucceeded () semantic prefix _closure) ->
TypedModuleSucceeded
(SealedTypedModule
owner
syntax
semantic
prefix
(Declaration.freshImportedModuleEvidence
(sealedTypedModuleEvidence <$> effectiveDirect)
semantic
prefix))
Right
(Declaration.DriverFailed failure prefix) ->
TypedModuleFailed
(case failure of
Declaration.DriverDeclarationFailed err ->
TypedDeclarationFailed err
Declaration.DriverActionFailed err ->
TypedActionFailed err)
prefix
Right (Declaration.DriverSealFailed err prefix) ->
TypedModuleFailed (TypedSealFailed err) prefix
|