File size: 7,533 Bytes
a10b189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{-# LANGUAGE OverloadedStrings #-}

module Test.Backend.ProofCompiler
  ( tests
  ) where

import Test.Tasty
import Test.Tasty.HUnit
import qualified Data.Text as T

import Assertica.Core.AST
import Assertica.Backend.CodeGen (defaultConfig)
import Assertica.Backend.ProofCompiler

-- ============================================================================
-- TEST SUITE
-- ============================================================================

tests :: TestTree
tests = testGroup "Assertica.Backend.ProofCompiler"
  [ testGroup "Basic Proofs"
      [ testCase "Reflexivity proof" testReflexivity
      , testCase "Symmetry proof" testSymmetry
      , testCase "Transitivity proof" testTransitivity
      ]
  , testGroup "Proof Variables"
      [ testCase "Proof variable" testProofVariable
      ]
  , testGroup "Proof Constructors"
      [ testCase "Simple proof constructor" testSimpleProofConstructor
      , testCase "Proof constructor with args" testProofConstructorWithArgs
      ]
  , testGroup "Introduction and Elimination"
      [ testCase "Proof introduction" testProofIntroduction
      , testCase "Proof elimination" testProofElimination
      ]
  , testGroup "Proof Application"
      [ testCase "Application of proof to term" testProofApplication
      ]
  , testGroup "Opaque Proofs"
      [ testCase "Opaque proof" testOpaqueProof
      ]
  , testGroup "Proof Annotation"
      [ testCase "Proof annotation" testProofAnnotation
      ]
  , testGroup "Determinism"
      [ testCase "Proof compilation is deterministic" testProofDeterminism
      ]
  ]

-- ============================================================================
-- UNIT TESTS: BASIC PROOFS
-- ============================================================================

testReflexivity :: Assertion
testReflexivity = do
  let x = Var (Var "x" 0)
  let refl = Refl x
  result <- case compileProof defaultConfig refl of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertEqual "Should be Refl" "Refl" result

testSymmetry :: Assertion
testSymmetry = do
  let x = Var (Var "x" 0)
  let baseProof = Refl x
  let symProof = Symm baseProof
  result <- case compileProof defaultConfig symProof of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertBool "Should contain Symm" ("Symm" `T.isInfixOf` result)
  assertBool "Should contain Refl" ("Refl" `T.isInfixOf` result)

testTransitivity :: Assertion
testTransitivity = do
  let x = Var (Var "x" 0)
  let proof1 = Refl x
  let proof2 = Refl x
  let transProof = Trans proof1 proof2
  result <- case compileProof defaultConfig transProof of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertBool "Should contain Trans" ("Trans" `T.isInfixOf` result)

-- ============================================================================
-- UNIT TESTS: PROOF VARIABLES
-- ============================================================================

testProofVariable :: Assertion
testProofVariable = do
  let pv = ProofVar (Var "p" 0)
  result <- case compileProof defaultConfig pv of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertEqual "Should be p" "p" result

-- ============================================================================
-- UNIT TESTS: PROOF CONSTRUCTORS
-- ============================================================================

testSimpleProofConstructor :: Assertion
testSimpleProofConstructor = do
  let ctor = Constr' (QName [] "Constructor") []
  result <- case compileProof defaultConfig ctor of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertEqual "Should be Constructor" "Constructor" result

testProofConstructorWithArgs :: Assertion
testProofConstructorWithArgs = do
  let x = Var (Var "x" 0)
  let proof1 = Refl x
  let proof2 = Refl x
  let ctor = Constr' (QName ["Data"] "Pair") [proof1, proof2]
  result <- case compileProof defaultConfig ctor of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertBool "Should contain Pair" ("Pair" `T.isInfixOf` result)
  assertBool "Should contain Refl" ("Refl" `T.isInfixOf` result)

-- ============================================================================
-- UNIT TESTS: INTRO/ELIM
-- ============================================================================

testProofIntroduction :: Assertion
testProofIntroduction = do
  let x = Var (Var "x" 0)
  let baseProof = Refl x
  let intro = Intro (Binder (Var "h" 0) Nothing) baseProof
  result <- case compileProof defaultConfig intro of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertBool "Should contain lambda" ("\\" `T.isInfixOf` result)
  assertBool "Should contain h" ("h" `T.isInfixOf` result)

testProofElimination :: Assertion
testProofElimination = do
  let x = Var (Var "x" 0)
  let proof1 = Refl x
  let proof2 = Refl x
  let elim = Elim proof1 proof2
  result <- case compileProof defaultConfig elim of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  -- Should contain two applications of Refl
  assertBool "Should be valid" (not (T.null result))

-- ============================================================================
-- UNIT TESTS: PROOF APPLICATION
-- ============================================================================

testProofApplication :: Assertion
testProofApplication = do
  let p = ProofVar (Var "p" 0)
  let x = Var (Var "x" 0)
  let appProof = App' p x
  result <- case compileProof defaultConfig appProof of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertBool "Should contain p" ("p" `T.isInfixOf` result)
  assertBool "Should contain x" ("x" `T.isInfixOf` result)

-- ============================================================================
-- UNIT TESTS: OPAQUE PROOFS
-- ============================================================================

testOpaqueProof :: Assertion
testOpaqueProof = do
  let prop = Eq (Var (Var "x" 0)) (Var (Var "y" 0))
  let opaque = Opaque (QName ["Axioms"] "axiom1") prop
  result <- case compileProof defaultConfig opaque of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertBool "Should reference proof" ("proof" `T.isInfixOf` result)
  assertBool "Should contain axiom1" ("axiom1" `T.isInfixOf` result)

-- ============================================================================
-- UNIT TESTS: PROOF ANNOTATION
-- ============================================================================

testProofAnnotation :: Assertion
testProofAnnotation = do
  let x = Var (Var "x" 0)
  let proof = Refl x
  let prop = Eq x x
  let annProof = ProofAnn proof prop
  result <- case compileProof defaultConfig annProof of
    Right x -> return x
    Left err -> assertFailure $ "Unexpected error: " ++ err
  assertBool "Should contain Refl" ("Refl" `T.isInfixOf` result)

-- ============================================================================
-- PROPERTY TESTS: DETERMINISM
-- ============================================================================

testProofDeterminism :: Assertion
testProofDeterminism = do
  let x = Var (Var "x" 0)
  let proof = Symm (Refl x)
  let result1 = compileProof defaultConfig proof
  let result2 = compileProof defaultConfig proof
  assertEqual "Proof compilation should be deterministic" result1 result2