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

module Test.Core.AST
  ( tests
  ) where

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T

import Assertica.Core.AST

-- ============================================================================
-- UNIT TESTS
-- ============================================================================

tests :: TestTree
tests = testGroup "Assertica.Core.AST"
  [ testGroup "Variables and Names"
      [ testCase "Create variable" testCreateVar
      , testCase "Create qualified name" testCreateQName
      , testCase "Unqualified name" testUnqualifiedName
      ]
  , testGroup "Lambda Terms"
      [ testCase "Simple lambda" testSimpleLambda
      , testCase "Nested lambda" testNestedLambda
      , testCase "Lambda with type annotation" testLambdaWithType
      ]
  , testGroup "Applications"
      [ testCase "Function application" testApplication
      , testCase "Nested application" testNestedApplication
      ]
  , testGroup "Type Construction"
      [ testCase "Function type" testFunctionType
      , testCase "Forall type (dependent)" testForallType
      , testCase "Type application" testTypeApplication
      ]
  , testGroup "Free Variables"
      [ testCase "Free variable in lambda body" testFreeVarInLambda
      , testCase "Bound variable not free" testBoundVarNotFree
      , testCase "Multiple free variables" testMultipleFreeVars
      , testCase "Free variables in application" testFreeVarsInApp
      ]
  , testGroup "Substitution"
      [ testCase "Simple substitution" testSimpleSubstitution
      , testCase "Substitution with shadowing" testSubstitutionShadowing
      , testCase "No capture in substitution" testNoCapture
      , testCase "Substitution in nested lambdas" testSubstitutionNested
      ]
  , testGroup "Propositions"
      [ testCase "Equality proposition" testEqualityProposition
      , testCase "Conjunction" testConjunction
      , testCase "Universal quantification" testUniversalQuant
      ]
  , testGroup "Proofs"
      [ testCase "Reflexivity proof" testReflexivityProof
      , testCase "Symmetry proof" testSymmetryProof
      , testCase "Transitivity proof" testTransitivityProof
      ]
  , testGroup "Assertions"
      [ testCase "Create assertion" testCreateAssertion
      , testCase "Assertion with proof" testAssertionWithProof
      , testCase "Unproven assertion" testUnprovenAssertion
      ]
  , testGroup "Pretty Printing"
      [ testCase "Pretty print variable" testPrettyVar
      , testCase "Pretty print lambda" testPrettyLambda
      , testCase "Pretty print function type" testPrettyType
      ]
  , testGroup "Data Constructors"
      [ testCase "Simple constructor" testSimpleConstructor
      , testCase "Nested constructors" testNestedConstructors
      ]
  , testGroup "Case Expressions"
      [ testCase "Simple case" testSimpleCase
      , testCase "Case with patterns" testCaseWithPatterns
      ]
  ]

-- ============================================================================
-- VARIABLE AND NAME TESTS
-- ============================================================================

testCreateVar :: Assertion
testCreateVar =
  let v = Var "x" 1
  in varName v @?= "x"

testCreateQName :: Assertion
testCreateQName =
  let q = QName ["Prelude", "Core"] "map"
  in qnameLocal q @?= "map"

testUnqualifiedName :: Assertion
testUnqualifiedName =
  let q = QName [] "x"
  in qnameModule q @?= []

-- ============================================================================
-- LAMBDA TERM TESTS
-- ============================================================================

testSimpleLambda :: Assertion
testSimpleLambda =
  let x = Var "x" 1
      binder = Binder x Nothing
      body = Var x
      lam = Lam binder body
  in case lam of
       Lam b e -> binderVar b @?= x
       _ -> assertFailure "Expected lambda"

testNestedLambda :: Assertion
testNestedLambda =
  let x = Var "x" 1
      y = Var "y" 2
      bx = Binder x Nothing
      by = Binder y Nothing
      innerLam = Lam by (Var y)
      outerLam = Lam bx innerLam
  in case outerLam of
       Lam b1 (Lam b2 _) ->
         do binderVar b1 @?= x
            binderVar b2 @?= y
       _ -> assertFailure "Expected nested lambdas"

testLambdaWithType :: Assertion
testLambdaWithType =
  let x = Var "x" 1
      intType = TyConst (QName ["Prelude"] "Int") []
      binder = Binder x (Just intType)
      lam = Lam binder (Var x)
  in case lam of
       Lam b _ -> binderType b @?= Just intType
       _ -> assertFailure "Expected lambda with type"

-- ============================================================================
-- APPLICATION TESTS
-- ============================================================================

testApplication :: Assertion
testApplication =
  let f = Var "f" 1
      x = Var "x" 2
      app = App f x
  in case app of
       App f' x' ->
         do f' @?= f
            x' @?= x
       _ -> assertFailure "Expected application"

testNestedApplication :: Assertion
testNestedApplication =
  let f = Var "f" 1
      x = Var "x" 2
      y = Var "y" 3
      app = App (App f x) y  -- (f x) y
  in case app of
       App (App f' x') y' ->
         do f' @?= f
            x' @?= x
            y' @?= y
       _ -> assertFailure "Expected nested application"

-- ============================================================================
-- TYPE CONSTRUCTION TESTS
-- ============================================================================

testFunctionType :: Assertion
testFunctionType =
  let a = TyVar (Var "a" 1)
      b = TyVar (Var "b" 2)
      funType = TyFun a b
  in case funType of
       TyFun a' b' ->
         do a' @?= a
            b' @?= b
       _ -> assertFailure "Expected function type"

testForallType :: Assertion
testForallType =
  let a = Var "a" 1
      binder = Binder a (Just (TyUniverse Type0))
      body = TyVar a
      forallType = TyForall binder body
  in case forallType of
       TyForall b _ ->
         binderVar b @?= a
       _ -> assertFailure "Expected forall type"

testTypeApplication :: Assertion
testTypeApplication =
  let t1 = TyVar (Var "f" 1)
      t2 = TyVar (Var "a" 2)
      app = TyApp t1 t2
  in case app of
       TyApp t1' t2' ->
         do t1' @?= t1
            t2' @?= t2
       _ -> assertFailure "Expected type application"

-- ============================================================================
-- FREE VARIABLE TESTS
-- ============================================================================

testFreeVarInLambda :: Assertion
testFreeVarInLambda =
  let x = Var "x" 1
      y = Var "y" 2
      binder = Binder x Nothing
      body = App (Var x) (Var y)  -- λx. x y
      lam = Lam binder body
      freeVars = freeVarsInTerm lam
  in freeVars @?= Set.singleton y

testBoundVarNotFree :: Assertion
testBoundVarNotFree =
  let x = Var "x" 1
      binder = Binder x Nothing
      body = Var x  -- λx. x
      lam = Lam binder body
      freeVars = freeVarsInTerm lam
  in freeVars @?= Set.empty

testMultipleFreeVars :: Assertion
testMultipleFreeVars =
  let x = Var "x" 1
      y = Var "y" 2
      z = Var "z" 3
      binder = Binder x Nothing
      body = App (Var y) (Var z)  -- λx. y z
      lam = Lam binder body
      freeVars = freeVarsInTerm lam
  in Set.size freeVars @?= 2 .&&. Set.member y freeVars .&&. Set.member z freeVars

testFreeVarsInApp :: Assertion
testFreeVarsInApp =
  let x = Var "x" 1
      y = Var "y" 2
      app = App (Var x) (Var y)
      freeVars = freeVarsInTerm app
  in Set.size freeVars @?= 2

-- ============================================================================
-- SUBSTITUTION TESTS
-- ============================================================================

testSimpleSubstitution :: Assertion
testSimpleSubstitution =
  let x = Var "x" 1
      y = Var "y" 2
      term = Var x
      replacement = Var y
      result = substituteTerm x replacement term
  in result @?= Var y

testSubstitutionShadowing :: Assertion
testSubstitutionShadowing =
  let x = Var "x" 1
      y = Var "y" 2
      z = Var "z" 3
      binder = Binder x Nothing
      body = Var x  -- λx. x
      lam = Lam binder body
      replacement = Var z
      result = substituteTerm y replacement lam
  in result @?= lam  -- y doesn't appear, so no substitution

testNoCapture :: Assertion
testNoCapture =
  let x = Var "x" 1
      y = Var "y" 2
      z = Var "z" 3
      binder_x = Binder x Nothing
      binder_z = Binder z Nothing
      -- λz. x = replacement
      inner = Var x
      lam_z = Lam binder_z inner
      -- λx. (λz. x)
      lam_x = Lam binder_x lam_z
      replacement = Var y
      result = substituteTerm x replacement lam_x
  in case result of
       Lam _ (Lam _ body) -> body @?= Var y
       _ -> assertFailure "Unexpected result structure"

testSubstitutionNested :: Assertion
testSubstitutionNested =
  let x = Var "x" 1
      y = Var "y" 2
      z = Var "z" 3
      bx = Binder x Nothing
      by = Binder y Nothing
      inner = Var x  -- λy. x
      lam_y = Lam by inner
      -- λx. (λy. x)
      lam_x = Lam bx lam_y
      replacement = Var z
      result = substituteTerm x replacement lam_x
  in case result of
       Lam bx' (Lam by' body) ->
         do binderVar bx' @?= x  -- Outer binding unchanged
            body @?= Var z  -- x replaced with z
       _ -> assertFailure "Unexpected structure"

-- ============================================================================
-- PROPOSITION TESTS
-- ============================================================================

testEqualityProposition :: Assertion
testEqualityProposition =
  let e1 = Var (Var "a" 1)
      e2 = Var (Var "b" 2)
      prop = Eq e1 e2
  in case prop of
       Eq a b ->
         do a @?= e1
            b @?= e2
       _ -> assertFailure "Expected equality"

testConjunction :: Assertion
testConjunction =
  let p1 = Top
      p2 = Bot
      conj = And p1 p2
  in case conj of
       And p1' p2' ->
         do p1' @?= p1
            p2' @?= p2
       _ -> assertFailure "Expected conjunction"

testUniversalQuant :: Assertion
testUniversalQuant =
  let x = Var "x" 1
      binder = Binder x Nothing
      prop = Top
      forall = Forall' binder prop
  in case forall of
       Forall' b p ->
         do binderVar b @?= x
            p @?= prop
       _ -> assertFailure "Expected forall"

-- ============================================================================
-- PROOF TESTS
-- ============================================================================

testReflexivityProof :: Assertion
testReflexivityProof =
  let e = Var (Var "a" 1)
      proof = Refl e
  in case proof of
       Refl e' -> e' @?= e
       _ -> assertFailure "Expected reflexivity"

testSymmetryProof :: Assertion
testSymmetryProof =
  let e1 = Var (Var "a" 1)
      e2 = Var (Var "b" 2)
      prop_proof = Refl e1
      symm_proof = Symm prop_proof
  in case symm_proof of
       Symm p -> p @?= prop_proof
       _ -> assertFailure "Expected symmetry"

testTransitivityProof :: Assertion
testTransitivityProof =
  let e1 = Var (Var "a" 1)
      p1 = Refl e1
      p2 = Refl e1
      trans = Trans p1 p2
  in case trans of
       Trans p1' p2' ->
         do p1' @?= p1
            p2' @?= p2
       _ -> assertFailure "Expected transitivity"

-- ============================================================================
-- ASSERTION TESTS
-- ============================================================================

testCreateAssertion :: Assertion
testCreateAssertion =
  let name = QName ["Test"] "prop1"
      prop = Top
      assertion = Assertion name prop Nothing
  in assertionName assertion @?= name

testAssertionWithProof :: Assertion
testAssertionWithProof =
  let name = QName [] "true_proof"
      prop = Top
      proof = Refl (Const UnitConst)
      assertion = Assertion name prop (Just proof)
  in assertionProof assertion @?= Just proof

testUnprovenAssertion :: Assertion
testUnprovenAssertion =
  let name = QName ["Test"] "unproven"
      prop = Bot
      assertion = Assertion name prop Nothing
  in assertionProof assertion @?= Nothing

-- ============================================================================
-- PRETTY PRINTING TESTS
-- ============================================================================

testPrettyVar :: Assertion
testPrettyVar =
  let v = Var "x" 1
      term = Var v
      pretty = prettyTerm term
  in pretty @?= "x"

testPrettyLambda :: Assertion
testPrettyLambda =
  let x = Var "x" 1
      binder = Binder x Nothing
      body = Var x
      lam = Lam binder body
      pretty = prettyTerm lam
  in "λ" `elem` pretty && "x" `elem` pretty @? "Lambda formatting"

testPrettyType :: Assertion
testPrettyType =
  let a = TyVar (Var "a" 1)
      b = TyVar (Var "b" 2)
      funType = TyFun a b
      pretty = prettyType funType
  in "→" `elem` pretty @? "Arrow in type"

-- ============================================================================
-- DATA CONSTRUCTOR TESTS
-- ============================================================================

testSimpleConstructor :: Assertion
testSimpleConstructor =
  let name = QName ["Prelude"] "Just"
      value = Var (Var "x" 1)
      constr = Constr name [value]
  in case constr of
       Constr n args ->
         do n @?= name
            length args @?= 1
       _ -> assertFailure "Expected constructor"

testNestedConstructors :: Assertion
testNestedConstructors =
  let cons_name = QName ["Prelude"] "Cons"
      nil_name = QName ["Prelude"] "Nil"
      x = Var (Var "x" 1)
      nil = Constr nil_name []
      cons_cell = Constr cons_name [x, nil]
  in case cons_cell of
       Constr name args ->
         do name @?= cons_name
            length args @?= 2
       _ -> assertFailure "Expected nested constructor"

-- ============================================================================
-- CASE EXPRESSION TESTS
-- ============================================================================

testSimpleCase :: Assertion
testSimpleCase =
  let scrutinee = Var (Var "x" 1)
      pattern1 = PatLit "1"
      body1 = Const (IntLit 10)
      clause = Clause pattern1 body1
      case_expr = Case scrutinee [clause] Nothing
  in case case_expr of
       Case e clauses def ->
         do e @?= scrutinee
            length clauses @?= 1
            def @?= Nothing
       _ -> assertFailure "Expected case"

testCaseWithPatterns :: Assertion
testCaseWithPatterns =
  let scrutinee = Var (Var "x" 1)
      x = Var "x" 1
      y = Var "y" 2
      pat1 = PatVar x
      body1 = Const (IntLit 1)
      pat2 = PatVar y
      body2 = Const (IntLit 2)
      clause1 = Clause pat1 body1
      clause2 = Clause pat2 body2
      case_expr = Case scrutinee [clause1, clause2] Nothing
  in case case_expr of
       Case _ clauses _ -> length clauses @?= 2
       _ -> assertFailure "Expected case with patterns"

-- ============================================================================
-- PROPERTY-BASED TESTS
-- ============================================================================

prop_freeVarsConservative :: Term -> Bool
prop_freeVarsConservative term =
  -- Free variables found should be a subset of all variables in the term
  let freeVars = freeVarsInTerm term
  in all (not . Set.null) [Set.singleton v | v <- Set.toList freeVars]

prop_substitutionIdempotent :: Term -> Var -> Term -> Bool
prop_substitutionIdempotent term var replacement =
  -- Substituting the same variable twice should be idempotent
  let result1 = substituteTerm var replacement term
      result2 = substituteTerm var replacement result1
  in result1 == result2

prop_prettyTermNoError :: Term -> Bool
prop_prettyTermNoError term =
  -- Pretty printing should not crash
  not (null (prettyTerm term))

prop_prettyTypeNoError :: Type -> Bool
prop_prettyTypeNoError ty =
  -- Pretty printing should not crash
  not (null (prettyType ty))

-- ============================================================================
-- PROPERTY TESTS (QuickCheck Integration)
-- ============================================================================

-- These would require Arbitrary instances for AST types
-- For now, we'll add placeholder instances
instance Arbitrary Var where
  arbitrary = Var <$> (T.pack <$> arbitrary) <*> arbitrary

instance Arbitrary QName where
  arbitrary = QName <$> arbitrary <*> (T.pack <$> arbitrary)

instance Arbitrary Term where
  arbitrary = oneof
    [ Var <$> arbitrary
    , Const <$> arbitrary
    ]

instance Arbitrary Type where
  arbitrary = oneof
    [ TyVar <$> arbitrary
    , TyUniverse <$> arbitrary
    ]

instance Arbitrary TypeLevel where
  arbitrary = oneof [pure Type0, TypeN <$> arbitrary]

instance Arbitrary Constant where
  arbitrary = oneof
    [ IntLit <$> arbitrary
    , BoolLit <$> arbitrary
    , pure UnitConst
    ]

instance Arbitrary Proposition where
  arbitrary = oneof
    [ pure Top
    , pure Bot
    ]

-- Property tests can be added here
propTests :: TestTree
propTests = testGroup "Property Tests"
  [ QC.testProperty "Pretty term doesn't error" prop_prettyTermNoError
  , QC.testProperty "Pretty type doesn't error" prop_prettyTypeNoError
  ]