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
|
{-# LANGUAGE NoImplicitPrelude #-}
module Test.Unit.Semantic (unitTests) where
import Base
import Checking.Authority qualified as Authority
import Checking.Core qualified as Core
import Checking.Foundation qualified as Foundation
import Checking.Identity qualified as Identity
import Checking.Semantic qualified as Semantic
import Felix.Cache.Codec
import Felix.Math.Codec
import Felix.Module
import Felix.Parsed.Identity
import Felix.Source
import Felix.Source.Content
import Syntax.Interface qualified as Syntax
import Syntax.Abstract qualified as Raw
import Data.ByteString (ByteString)
import Data.List qualified as List
import Data.Map.Strict qualified as Map
import Test.Tasty
import Test.Tasty.HUnit
unitTests :: TestTree
unitTests =
testGroup "Semantic interfaces"
[ testCase "separates syntax and semantic Merkle identities"
separatesSyntaxAndSemantics
, testCase "round-trips closed semantic declaration state"
roundTripsSemanticState
, testCase "round-trips exact semantic global keys"
roundTripsSemanticGlobalKeys
, testCase "round-trips canonical structure descriptors"
roundTripsStructureDescriptors
, testCase "keys exact proof and module inputs"
keysExactInputs
]
separatesSyntaxAndSemantics :: Assertion
separatesSyntaxAndSemantics = do
fixture <- makeFixture
firstSyntax <- makeSyntax "first"
secondSyntax <- makeSyntax "second"
assertBool
"notation changes syntax identity"
( Syntax.moduleSyntaxAssertedId firstSyntax
/= Syntax.moduleSyntaxAssertedId secondSyntax
)
assertEqual
"notation does not enter semantic prefix identity"
(fixtureNextPrefix fixture)
(Semantic.nextPrefixContextId
(fixtureInitialPrefix fixture)
(fixtureDelta fixture))
roundTripsSemanticState :: Assertion
roundTripsSemanticState = do
fixture <- makeFixture
assertEqual
"semantic interface cache round trip"
(Right (fixtureInterface fixture))
(decodeCache
Semantic.getSemanticInterfaceCache
(encodeCache
(Semantic.putSemanticInterfaceCache
(fixtureInterface fixture))))
assertEqual
"walking-subset environment delta round trip"
(Right Semantic.emptySemanticEnvironmentDelta)
(decodeCache
Semantic.getSemanticEnvironmentDeltaCache
(encodeCache
(Semantic.putSemanticEnvironmentDeltaCache
Semantic.emptySemanticEnvironmentDelta)))
roundTripsSemanticGlobalKeys :: Assertion
roundTripsSemanticGlobalKeys = do
fixture <- makeFixture
let unary = Raw.HoleCons (Raw.TokenCons (Raw.Command "f") Raw.End)
plural = Raw.HoleCons (Raw.TokenCons (Raw.Word "things") Raw.End)
keys =
[ Semantic.SemanticLeftAdjective unary
, Semantic.SemanticRightAdjective unary
, Semantic.SemanticFunctionPhrase unary plural
, Semantic.SemanticNoun unary plural
, Semantic.SemanticVerb unary plural
, Semantic.SemanticRelation
(Raw.Command "rel")
(Raw.ParameterArity 2)
, Semantic.SemanticExpressionFunction unary
, Semantic.SemanticPrefixPredicate "Pred" 3
]
target =
Identity.intrinsicObjectId
(fixtureTheory fixture)
Core.Empty
Core.TySet
bindings =
List.sortOn Semantic.semanticGlobalBindingKey
( case keys of
[] -> []
first : rest ->
Semantic.semanticGlobalBinding
first
(Semantic.ContextualTransparentExpansion
target
(Map.singleton
(Raw.StructSymbol "operation")
target))
: [ Semantic.semanticGlobalBinding
key
(Semantic.GlobalReference target)
| key <- rest
]
)
delta <- expectRight (Semantic.semanticEnvironmentDelta bindings)
assertEqual "binding cache round trip"
(Right delta)
(decodeCache
Semantic.getSemanticEnvironmentDeltaCache
(encodeCache
(Semantic.putSemanticEnvironmentDeltaCache delta)))
case bindings of
first : second : _ -> do
assertEqual "rejects noncanonical order"
(Left Semantic.NonCanonicalSemanticGlobalBindingOrder)
(Semantic.semanticEnvironmentDelta
(second : first : drop 2 bindings))
assertEqual "rejects duplicate key"
(Left
(Semantic.DuplicateSemanticGlobalKey
(Semantic.semanticGlobalBindingKey first)))
(Semantic.semanticEnvironmentDelta [first, first])
_ -> assertFailure "semantic key fixture is unexpectedly empty"
roundTripsStructureDescriptors :: Assertion
roundTripsStructureDescriptors = do
fixture <- makeFixture
let structurePhrase marker word =
Semantic.semanticStructurePhrase
(Raw.LexicalItemSgPl
(Raw.SgPl
(Raw.TokenCons (Raw.Word word) Raw.End)
(Raw.TokenCons (Raw.Word (word <> "s")) Raw.End))
marker)
base = structurePhrase "onesorted_structure" "base"
child = structurePhrase "ordered_structure" "ordered"
object =
Identity.intrinsicObjectId
(fixtureTheory fixture)
Core.Empty
Core.TySet
operation =
Semantic.semanticStructureOperation
(Raw.StructSymbol "carrier")
object
descriptor <- expectRight
(Semantic.semanticStructureDescriptor
child
(Just object)
[base]
[operation])
delta <- expectRight
(Semantic.semanticEnvironmentWithStructures [] [descriptor])
assertEqual
"structure environment cache round trip"
(Right delta)
(decodeCache
Semantic.getSemanticEnvironmentDeltaCache
(encodeCache
(Semantic.putSemanticEnvironmentDeltaCache delta)))
assertEqual
"duplicate local operation is rejected"
(Left
(Semantic.DuplicateSemanticStructureOperation
(Raw.StructSymbol "carrier")))
(Semantic.semanticStructureDescriptor
child
(Just object)
[base]
[operation, operation])
keysExactInputs :: Assertion
keysExactInputs = do
fixture <- makeFixture
let authority =
Authority.factAuthority
(fixtureTheorem fixture)
Authority.cleanAuthoritySafety
certificate <- expectRight
(Authority.validationCertificate
authority
(Authority.CheckedSourceProof []))
let theorem =
Identity.theoremId
(fixtureTheorem fixture)
firstProof =
Semantic.proofValidationKey
theorem
(Semantic.proofSyntaxId "proof-a")
(fixtureInitialPrefix fixture)
secondProof =
Semantic.proofValidationKey
theorem
(Semantic.proofSyntaxId "proof-b")
(fixtureInitialPrefix fixture)
laterContext =
Semantic.proofValidationKey
theorem
(Semantic.proofSyntaxId "proof-a")
(fixtureNextPrefix fixture)
assertBool
"proof syntax is an exact validation input"
(firstProof /= secondProof)
assertBool
"semantic predecessor is an exact validation input"
(firstProof /= laterContext)
let proofRecord =
Semantic.proofValidationRecord
firstProof certificate
declarationKey =
Semantic.declarationValidationKey
(Semantic.declarationSyntaxId "declaration")
(fixtureInitialPrefix fixture)
[]
[theorem, theorem]
declarationRecord =
Semantic.declarationValidationRecord
declarationKey
[certificate, certificate]
assertEqual
"proof validation record cache round trip"
(Right proofRecord)
(decodeCache
Semantic.getProofValidationRecordCache
(encodeCache
(Semantic.putProofValidationRecordCache
proofRecord)))
assertEqual
"ordered declaration certificates retain repetitions"
(Right declarationRecord)
(decodeCache
Semantic.getDeclarationValidationRecordCache
(encodeCache
(Semantic.putDeclarationValidationRecordCache
declarationRecord)))
firstParsedKey <- expectRight
(parsedModuleKey
(fixtureSourceContentId "source-a")
Syntax.baseSyntaxInterfaceId
[])
secondParsedKey <- expectRight
(parsedModuleKey
(fixtureSourceContentId "source-b")
Syntax.baseSyntaxInterfaceId
[])
directSyntax <- makeSyntax "direct"
let directSyntaxId =
Syntax.moduleSyntaxAssertedId directSyntax
assertEqual
"parsed identity rejects duplicate direct syntax"
(Left
(DuplicateParsedDirectSyntaxInput
directSyntaxId))
(parsedModuleKey
(fixtureSourceContentId "source-a")
Syntax.baseSyntaxInterfaceId
[directSyntaxId, directSyntaxId])
let
firstParsed =
parsedModuleId firstParsedKey "parsed"
secondParsed =
parsedModuleId secondParsedKey "parsed"
firstArtifactKey <- expectRight
(Semantic.moduleArtifactKey
(fixtureOwner fixture)
firstParsed
[]
(fixtureTheory fixture))
secondArtifactKey <- expectRight
(Semantic.moduleArtifactKey
(fixtureOwner fixture)
secondParsed
[]
(fixtureTheory fixture))
let firstArtifact =
Semantic.moduleArtifactId firstArtifactKey
secondArtifact =
Semantic.moduleArtifactId secondArtifactKey
assertBool
"module artifact binds parsed source identity"
(firstArtifact /= secondArtifact)
let semanticId =
Semantic.semanticInterfaceAssertedId
(fixtureInterface fixture)
assertEqual
"prefix identity rejects duplicate direct semantics"
(Left
(Semantic.DuplicateInitialPrefixSemanticInput
semanticId))
(Semantic.initialPrefixContextId
(fixtureTheory fixture)
(fixtureOwner fixture)
[semanticId, semanticId])
assertEqual
"module artifact key cache round trip"
(Right firstArtifactKey)
(decodeCache
Semantic.getModuleArtifactKeyCache
(encodeCache
(Semantic.putModuleArtifactKeyCache
firstArtifactKey)))
syntax <- makeSyntax "artifact"
let artifactResult =
Semantic.moduleArtifactResult
firstArtifactKey
(Syntax.moduleSyntaxAssertedId syntax)
(Semantic.semanticInterfaceAssertedId
(fixtureInterface fixture))
assertEqual
"module artifact root round trip"
(Right artifactResult)
(decodeCache
(Semantic.getModuleArtifactResultCache
firstArtifact)
(encodeCache
(Semantic.putModuleArtifactResultCache
artifactResult)))
data Fixture = Fixture
{ fixtureTheory :: !Identity.TheoryId
, fixtureOwner :: !ModuleName
, fixtureTheorem :: !Identity.TheoremRef
, fixtureDelta :: !Semantic.DeclarationInterfaceDelta
, fixtureInterface :: !Semantic.SemanticInterface
, fixtureInitialPrefix :: !Semantic.PrefixContextId
, fixtureNextPrefix :: !Semantic.PrefixContextId
}
makeFixture :: IO Fixture
makeFixture = do
foundation <- expectRight Foundation.checkedFoundation
namespaceDigest <- expectRight
(hashCanonicalFields
"semantic-test-namespace"
["root"])
relative <- expectRight (safeRelativePath "module.tex")
closure <- expectRight
(Identity.validateObjectClosure
(Identity.theoryId foundation)
[])
proposition <- expectRight
(Identity.validatePropositionContent
closure
Core.CFalsum)
let theory =
Identity.theoryId foundation
owner =
moduleNameFromParts
(sourceNamespaceIdFromDigest namespaceDigest)
relative
reference =
Identity.theoremRef
theory
(Identity.checkedPropositionId proposition)
authority =
Authority.factAuthority
reference
Authority.cleanAuthoritySafety
occurrence =
Semantic.semanticFactOccurrence
(Semantic.factSlot owner (localFactOrdinal 0))
authority
Semantic.SearchEligible
slot =
Semantic.declarationSlot
owner
(localDeclarationOrdinal 0)
delta <- expectRight
(Semantic.declarationInterfaceDelta
slot
[occurrence]
[ Semantic.semanticAlias
(Semantic.semanticName "theorem")
(Semantic.semanticFactOccurrenceFingerprint
(Semantic.factSlot owner (localFactOrdinal 0))
authority)
]
[]
[Identity.checkedPropositionId proposition]
Semantic.emptySemanticEnvironmentDelta)
interface <- expectRight
(Semantic.semanticInterface owner [] [delta])
initial <- expectRight
(Semantic.initialPrefixContextId theory owner [])
pure
Fixture
{ fixtureTheory = theory
, fixtureOwner = owner
, fixtureTheorem = reference
, fixtureDelta = delta
, fixtureInterface = interface
, fixtureInitialPrefix = initial
, fixtureNextPrefix =
Semantic.nextPrefixContextId initial delta
}
makeSyntax :: Text -> IO Syntax.ModuleSyntaxInterface
makeSyntax command = do
delta <- expectRight
(Syntax.canonicalSyntaxDelta
[Syntax.CanonicalStructureOperation command])
expectRight (Syntax.moduleSyntaxInterface [] delta)
fixtureSourceContentId :: ByteString -> SourceContentId
fixtureSourceContentId bytes =
either
(impossible . show)
id
(decodeCache
getSourceContentIdCache
(encodeCache
(putCacheDigest
(hashCacheFields
"semantic-test-source"
[bytes]))))
expectRight :: Show error => Either error value -> IO value
expectRight = \case
Left err ->
assertFailure (show err) >> fail "unreachable"
Right value ->
pure value
|