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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
{-# LANGUAGE OverloadedStrings #-}

{-|
Module      : Test.StdLib.Monomorphism
Description : Tests for lattice monomorphism theorem
Copyright   : (c) 2026 Ahmad Ali Parr
-}

module Test.StdLib.Monomorphism
  ( tests
  ) where

import Test.Tasty
import Test.Tasty.HUnit

import Assertica.Core.AST
import Assertica.Core.ProofTerm
import Assertica.StdLib.Lattice
import Assertica.StdLib.Monomorphism

-- ============================================================================
-- UNIT TESTS: MONOMORPHISM THEOREM
-- ============================================================================

tests :: TestTree
tests = testGroup "Assertica.StdLib.Monomorphism"
  [ testGroup "Monomorphism Theorem"
      [ testCase "Identity homomorphism is monomorphic" testIdentityMonomorphic
      , testCase "Same homomorphisms agree on generators" testSameHomsAgree
      , testCase "Monomorphism proof is constructible" testMonomorphismConstructible
      , testCase "Monomorphism proof on generators" testMonomorphismOnGenerators
      , testCase "Monomorphism proof on compounds" testMonomorphismOnCompounds
      ]
  , testGroup "Homomorphism Equality"
      [ testCase "Equal homomorphisms have same map" testEqualHomsSameMap
      , testCase "Homomorphism equality proof" testHomEqualityProof
      ]
  , testGroup "Structural Induction"
      [ testCase "Base case: generators" testInductionBase
      , testCase "Induction step: join" testInductionStepJoin
      , testCase "Induction step: meet" testInductionStepMeet
      , testCase "Full induction" testFullInduction
      ]
  , testGroup "Generator Extension"
      [ testCase "Extension on generators" testExtensionGenerators
      , testCase "Extended function preserves joins" testExtensionPreservesJoins
      , testCase "Extended function preserves meets" testExtensionPreservesMeets
      , testCase "Unique extension" testUniqueExtension
      ]
  , testGroup "Homomorphism Composition"
      [ testCase "Composed homomorphisms" testComposedHomomorphisms
      , testCase "Composition preserves monomorphism" testCompositionMonomorphism
      ]
  , testGroup "Proof Term Correctness"
      [ testCase "Monomorphism proof type-checks" testMonomorphismProofChecks
      , testCase "Induction step proofs" testInductionStepProofs
      , testCase "Extension proofs" testExtensionProofs
      ]
  , testGroup "Determinism"
      [ testCase "Monomorphism proof is deterministic" testMonomorphismDeterminism
      , testCase "Structural induction is deterministic" testInductionDeterminism
      , testCase "Extension is deterministic" testExtensionDeterminism
      ]
  , testGroup "Negative Tests"
      [ testCase "Different homomorphisms may not agree" testDifferentHomsMayDiffer
      , testCase "Non-agreeing on generators" testNonAgreeingGenerators
      ]
  ]

-- ============================================================================
-- MONOMORPHISM THEOREM TESTS
-- ============================================================================

testIdentityMonomorphic :: Assertion
testIdentityMonomorphic =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl  -- Identity agrees with itself
      a = Const (BoolLit True)
      proof = monomorphismTheorem booleanLattice booleanLattice f g genEq a
  in proof `shouldBeProofTerm` ()

testSameHomsAgree :: Assertion
testSameHomsAgree =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      -- Same homomorphism always agrees
  in homMap f (Const (BoolLit True)) @?= homMap g (Const (BoolLit True))

testMonomorphismConstructible :: Assertion
testMonomorphismConstructible =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      a = Const (BoolLit True)
      proof = monomorphismTheorem booleanLattice booleanLattice f g genEq a
  in case proof of
      _ -> assertBool "Monomorphism proof is constructible" True

testMonomorphismOnGenerators :: Assertion
testMonomorphismOnGenerators =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      -- Both agree on generators
      genEq gen = Refl
      -- Generator (a constant)
      gen = Const (BoolLit True)
      proof = monomorphismTheorem booleanLattice booleanLattice f g genEq gen
  in proof @?= Refl

testMonomorphismOnCompounds :: Assertion
testMonomorphismOnCompounds =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      -- Compound: a ⊔ b
      compound = Constr (QName [] "join") [Const (BoolLit True), Const (BoolLit False)]
      proof = monomorphismTheorem booleanLattice booleanLattice f g genEq compound
  in proof `shouldBeProofTerm` ()

-- ============================================================================
-- HOMOMORPHISM EQUALITY TESTS
-- ============================================================================

testEqualHomsSameMap :: Assertion
testEqualHomsSameMap =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      a = Const (BoolLit True)
  in homMap f a @?= homMap g a

testHomEqualityProof :: Assertion
testHomEqualityProof =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      proof = homomorphismsEqual booleanLattice booleanLattice f g genEq
  in proof `shouldBeProofTerm` ()

-- ============================================================================
-- STRUCTURAL INDUCTION TESTS
-- ============================================================================

testInductionBase :: Assertion
testInductionBase =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      genProof gen = Refl
      gen = Const (BoolLit True)
      proof = inductionBase booleanLattice genProof gen
  in proof @?= Refl

testInductionStepJoin :: Assertion
testInductionStepJoin =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      a = Const (BoolLit True)
      b = Const (BoolLit False)
      hypA = Refl
      hypB = Refl
      proof = inductionStep booleanLattice booleanLattice f g a b hypA hypB
  in proof `shouldBeProofTerm` ()

testInductionStepMeet :: Assertion
testInductionStepMeet =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      a = Const (BoolLit True)
      b = Const (BoolLit False)
      hypA = Refl
      hypB = Refl
      proof = inductionStep booleanLattice booleanLattice f g a b hypA hypB
  in proof `shouldBeProofTerm` ()

testFullInduction :: Assertion
testFullInduction =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      -- Complex term: (a ⊔ b) ⊓ (c ⊔ d)
      a = Const (BoolLit True)
      b = Const (BoolLit False)
      c = Const (BoolLit True)
      d = Const (BoolLit False)
      complex = Constr (QName [] "meet")
                  [ Constr (QName [] "join") [a, b]
                  , Constr (QName [] "join") [c, d]
                  ]
      proof = structuralInductionLattice booleanLattice booleanLattice f g genEq complex
  in proof `shouldBeProofTerm` ()

-- ============================================================================
-- GENERATOR EXTENSION TESTS
-- ============================================================================

testExtensionGenerators :: Assertion
testExtensionGenerators =
  let phi gen = Const (IntLit 1)  -- Map everything to 1
      hom = generatorExtension booleanLattice phi
      gen = Const (BoolLit True)
  in homMap hom gen @?= Const (IntLit 1)

testExtensionPreservesJoins :: Assertion
testExtensionPreservesJoins =
  let phi gen = Const (IntLit 1)
      hom = generatorExtension booleanLattice phi
      a = Const (BoolLit True)
      b = Const (BoolLit False)
      proof = homPreservesJoin hom a b
  in proof @?= Refl

testExtensionPreservesMeets :: Assertion
testExtensionPreservesMeets =
  let phi gen = Const (IntLit 1)
      hom = generatorExtension booleanLattice phi
      a = Const (BoolLit True)
      b = Const (BoolLit False)
      proof = homPreservesMeet hom a b
  in proof @?= Refl

testUniqueExtension :: Assertion
testUniqueExtension =
  let phi gen = Const (IntLit 1)
      a = Const (BoolLit True)
      proof = uniqueExtension booleanLattice booleanLattice phi a
  in proof `shouldBeProofTerm` ()

-- ============================================================================
-- HOMOMORPHISM COMPOSITION TESTS
-- ============================================================================

testComposedHomomorphisms :: Assertion
testComposedHomomorphisms =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      a = Const (BoolLit True)
      composed = homMap f (homMap g a)
  in composed @?= a

testCompositionMonomorphism :: Assertion
testCompositionMonomorphism =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      a = Const (BoolLit True)
      proof = monomorphismTheorem booleanLattice booleanLattice f g genEq a
  in proof `shouldBeProofTerm` ()

-- ============================================================================
-- PROOF TERM CORRECTNESS TESTS
-- ============================================================================

testMonomorphismProofChecks :: Assertion
testMonomorphismProofChecks =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      a = Const (BoolLit True)
      proof = monomorphismTheorem booleanLattice booleanLattice f g genEq a
  in case proof of
      _ -> assertBool "Monomorphism proof exists" True

testInductionStepProofs :: Assertion
testInductionStepProofs =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      a = Const (BoolLit True)
      b = Const (BoolLit False)
      proof = inductionStep booleanLattice booleanLattice f g a b Refl Refl
  in case proof of
      _ -> assertBool "Induction step proof exists" True

testExtensionProofs :: Assertion
testExtensionProofs =
  let phi gen = Const (IntLit 1)
      ext = extendFromGenerators booleanLattice booleanLattice phi
      a = Const (BoolLit True)
  in homMap ext a @?= phi a

-- ============================================================================
-- DETERMINISM TESTS
-- ============================================================================

testMonomorphismDeterminism :: Assertion
testMonomorphismDeterminism =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      a = Const (BoolLit True)
      proof1 = monomorphismTheorem booleanLattice booleanLattice f g genEq a
      proof2 = monomorphismTheorem booleanLattice booleanLattice f g genEq a
  in proof1 @?= proof2

testInductionDeterminism :: Assertion
testInductionDeterminism =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = f
      genEq gen = Refl
      a = Const (BoolLit True)
      proof1 = structuralInductionLattice booleanLattice booleanLattice f g genEq a
      proof2 = structuralInductionLattice booleanLattice booleanLattice f g genEq a
  in proof1 @?= proof2

testExtensionDeterminism :: Assertion
testExtensionDeterminism =
  let phi gen = Const (IntLit 1)
      ext1 = extendFromGenerators booleanLattice booleanLattice phi
      ext2 = extendFromGenerators booleanLattice booleanLattice phi
      a = Const (BoolLit True)
  in homMap ext1 a @?= homMap ext2 a

-- ============================================================================
-- NEGATIVE TESTS
-- ============================================================================

testDifferentHomsMayDiffer :: Assertion
testDifferentHomsMayDiffer =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = \_ -> Const (BoolLit False)  -- Constant False
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      a = Const (BoolLit True)
  in homMap f a /= homMap g a @? "Different homomorphisms may map differently"

testNonAgreeingGenerators :: Assertion
testNonAgreeingGenerators =
  let f = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = id
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      g = LatticeHomomorphism
             { homSource = booleanLattice
             , homTarget = booleanLattice
             , homMap = \_ -> Const (BoolLit False)
             , homPreservesJoin = \_ _ -> Refl
             , homPreservesMeet = \_ _ -> Refl
             }
      true = Const (BoolLit True)
  in homMap f true /= homMap g true @? "Homomorphisms that differ on generators are different"

-- ============================================================================
-- HELPER FUNCTIONS
-- ============================================================================

shouldBeProofTerm :: Proof -> () -> Assertion
shouldBeProofTerm proof () = case proof of
  Refl {} -> assertBool "Is a proof term (Refl)" True
  Symm {} -> assertBool "Is a proof term (Symm)" True
  Trans {} -> assertBool "Is a proof term (Trans)" True
  Intro {} -> assertBool "Is a proof term (Intro)" True
  Elim {} -> assertBool "Is a proof term (Elim)" True
  Constr' {} -> assertBool "Is a proof term (Constr')" True
  Opaque {} -> assertBool "Is a proof term (Opaque)" True
  ProofAnn {} -> assertBool "Is a proof term (ProofAnn)" True
  _ -> assertFailure "Not a proper proof term"