File: What4.hs

package info (click to toggle)
haskell-copilot-theorem 4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 396 kB
  • sloc: haskell: 4,326; makefile: 6
file content (315 lines) | stat: -rw-r--r-- 10,881 bytes parent folder | download | duplicates (2)
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
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
-- The following warning is disabled due to a necessary instance of SatResult
-- defined in this module.
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- | Test copilot-theorem:Copilot.Theorem.What4.
module Test.Copilot.Theorem.What4 where

-- External imports
import Control.Exception                    (Exception, try)
import Data.Int                             (Int8)
import Data.Proxy                           (Proxy (..))
import Data.Typeable                        (typeRep)
import Data.Word                            (Word32)
import Test.Framework                       (Test, testGroup)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.HUnit                           (Assertion, assertBool,
                                             assertFailure)
import Test.QuickCheck                      (Arbitrary (arbitrary), Property,
                                             arbitrary, forAll)
import Test.QuickCheck.Monadic              (monadicIO, run)

-- External imports: Copilot
import           Copilot.Core.Expr      (Expr (Const, Drop, Op1, Op2), Id)
import           Copilot.Core.Operators (Op1 (..), Op2 (..))
import           Copilot.Core.Spec      (Spec (..), Stream (..))
import qualified Copilot.Core.Spec      as Copilot
import           Copilot.Core.Type      (Field (..),
                                         Struct (toValues, typeName),
                                         Type (Struct), Typed (typeOf),
                                         Value (..))

-- Internal imports: Modules being tested
import Copilot.Theorem.What4 (CounterExample (..), ProveException (..),
                              SatResult (..), SatResultCex (..), Solver (..),
                              prove, proveWithCounterExample)

-- * Constants

-- | Unit tests for copilot-theorem:Copilot.Theorem.What4.
tests :: Test.Framework.Test
tests =
  testGroup "Copilot.Theorem.What4"
    [ testProperty "Prove via Z3 that true is valid"    testProveZ3True
    , testProperty "Prove via Z3 that false is invalid" testProveZ3False
    , testProperty "Prove via Z3 that x == x is valid"  testProveZ3EqConst
    , testProperty "Prove via Z3 that a struct update is valid" testProveZ3StructUpdate
    , testProperty "Counterexample with invalid base case" testCounterExampleBaseCase
    , testProperty "Counterexample with invalid induction step" testCounterExampleInductionStep
    , testProperty "Check that the What4 backend rejects existential quantification" testWhat4ExistsException
    ]

-- * Individual tests

-- | Test that Z3 is able to prove the following expression valid:
-- @
--   constant True
-- @
testProveZ3True :: Property
testProveZ3True =
    monadicIO $ run $ checkResult Z3 propName spec Valid
  where
    propName :: String
    propName = "prop"

    spec :: Spec
    spec = forallPropSpec propName [] $ Const typeOf True

-- | Test that Z3 is able to prove the following expression invalid:
-- @
--   constant False
-- @
testProveZ3False :: Property
testProveZ3False =
    monadicIO $ run $ checkResult Z3 propName spec Invalid
  where
    propName :: String
    propName = "prop"

    spec :: Spec
    spec = forallPropSpec propName [] $ Const typeOf False

-- | Test that Z3 is able to prove the following expresion valid:
-- @
--   for all (x :: Int8), constant x == constant x
-- @
testProveZ3EqConst :: Property
testProveZ3EqConst = forAll arbitrary $ \x ->
    monadicIO $ run $ checkResult Z3 propName (spec x) Valid
  where
    propName :: String
    propName = "prop"

    spec :: Int8 -> Spec
    spec x = forallPropSpec propName [] $
      Op2 (Eq typeOf) (Const typeOf x) (Const typeOf x)

-- | Test that Z3 is able to prove the following expresion valid:
-- @
--   for all (s :: MyStruct),
--   ((s ## testField =$ (+1)) # testField) == ((s # testField) + 1)
-- @
testProveZ3StructUpdate :: Property
testProveZ3StructUpdate = forAll arbitrary $ \x ->
    monadicIO $ run $ checkResult Z3 propName (spec x) Valid
  where
    propName :: String
    propName = "prop"

    spec :: TestStruct -> Spec
    spec s = forallPropSpec propName [] $
      Op2
        (Eq typeOf)
        (getField
          (Op2
            (UpdateField typeOf typeOf testField)
            sExpr
            (add1 (getField sExpr))))
        (add1 (getField sExpr))
      where
        sExpr :: Expr TestStruct
        sExpr = Const typeOf s

        getField :: Expr TestStruct -> Expr Word32
        getField = Op1 (GetField typeOf typeOf testField)

        add1 :: Expr Word32 -> Expr Word32
        add1 x = Op2 (Add typeOf) x (Const typeOf 1)

-- | Test that Z3 is able to produce a counterexample to the following property,
-- where the base case is proved invalid:
--
-- @
--   let s :: Stream Bool
--       s = [False] ++ constant True
--   in forAll s
-- @
testCounterExampleBaseCase :: Property
testCounterExampleBaseCase =
    monadicIO $ run $
      checkCounterExample Z3 propName spec $ \cex ->
        pure $ not $ and $ baseCases cex
  where
    propName :: String
    propName = "prop"

    -- s = [False] ++ constant True
    s :: Stream
    s = Stream
      { streamId       = sId
      , streamBuffer   = [False]
      , streamExpr     = Const typeOf True
      , streamExprType = typeOf
      }

    sId :: Id
    sId = 0

    spec :: Spec
    spec = forallPropSpec propName [s] $ Drop typeOf 0 sId

-- | Test that Z3 is able to produce a counterexample to the following property,
-- where the induction step is proved invalid:
--
-- @
--   let s :: Stream Bool
--       s = [True] ++ constant False
--   in forAll s
-- @
testCounterExampleInductionStep :: Property
testCounterExampleInductionStep =
    monadicIO $ run $
      checkCounterExample Z3 propName spec $ \cex ->
        pure $ not $ inductionStep cex
  where
    propName :: String
    propName = "prop"

    -- s = [True] ++ constant False
    s :: Stream
    s = Stream
      { streamId       = sId
      , streamBuffer   = [True]
      , streamExpr     = Const typeOf False
      , streamExprType = typeOf
      }

    sId :: Id
    sId = 0

    spec :: Spec
    spec = forallPropSpec propName [s] $ Drop typeOf 0 sId

-- | Test that @copilot-theorem@'s @what4@ backend will throw an exception if it
-- attempts to prove an existentially quantified proposition.
testWhat4ExistsException :: Property
testWhat4ExistsException =
    monadicIO $ run $
      checkException (prove Z3 spec) isUnexpectedExistentialProposition
  where
    isUnexpectedExistentialProposition :: ProveException -> Bool
    isUnexpectedExistentialProposition UnexpectedExistentialProposition = True

    propName :: String
    propName = "prop"

    spec :: Spec
    spec = existsPropSpec propName [] $ Const typeOf True

-- | A simple data type with a 'Struct' instance and a 'Field'. This is only
-- used as part of 'testProveZ3StructUpdate'.
newtype TestStruct = TestStruct { testField :: Field "testField" Word32 }

instance Arbitrary TestStruct where
  arbitrary = do
    w32 <- arbitrary
    return (TestStruct (Field w32))

instance Struct TestStruct where
  typeName _ = "testStruct"
  toValues s = [Value typeOf (testField s)]

instance Typed TestStruct where
  typeOf = Struct (TestStruct (Field 0))

-- | Check that the solver's satisfiability result for the given property in
-- the given spec matches the expectation.
checkResult :: Solver -> String -> Spec -> SatResult -> IO Bool
checkResult solver propName spec expectation = do
  results <- prove solver spec

  -- Find the satisfiability result for propName.
  let propResult = lookup propName results

  -- The following check also works for the case in which the property name
  -- does not exist in the results, in which case the lookup returns 'Nothing'.
  return $ propResult == Just expectation

-- | Check that the solver produces an invalid result for the given property and
-- that the resulting 'CounterExample' satifies the given predicate.
checkCounterExample :: Solver
                    -> String
                    -> Spec
                    -> (CounterExample -> IO Bool)
                    -> IO Bool
checkCounterExample solver propName spec cexPred = do
  results <- proveWithCounterExample solver spec

  -- Find the satisfiability result for propName. If the property name does not
  -- exist in the results, raise an assertion failure.
  propResult <-
    case lookup propName results of
      Just propResult ->
        pure propResult
      Nothing ->
        assertFailure $
          "Could not find property in results: " ++ propName

  -- Assert that the solver returned an invalid result and pass the
  -- counterexample to the predicate. If the result is anything other than
  -- invalid, raise an assertion failure.
  case propResult of
    InvalidCex cex ->
      cexPred cex
    ValidCex {} ->
      assertFailure "Expected invalid result, but result was valid"
    UnknownCex {} ->
      assertFailure "Expected invalid result, but result was unknown"

-- | Check that the given 'IO' action throws a particular exception. This is
-- largely taken from the implementation of @shouldThrow@ in
-- @hspec-expectations@ (note that this test suite uses @test-framework@ instead
-- of @hspec@).
checkException :: forall e a. Exception e => IO a -> (e -> Bool) -> Assertion
checkException action p = do
    r <- try action
    case r of
      Right _ ->
        assertFailure $
          "did not get expected exception: " ++ exceptionType
      Left e ->
        assertBool
          ("predicate failed on expected exception: " ++ exceptionType ++
           "\n" ++ show e)
          (p e)
  where
    -- String representation of the expected exception's type
    exceptionType = show $ typeRep $ Proxy @e

-- * Auxiliary

-- | Build a 'Spec' that contains one property with the given name, which
-- contains the given streams, and is defined by the given boolean expression,
-- which is universally quantified.
forallPropSpec :: String -> [Stream] -> Expr Bool -> Spec
forallPropSpec propName propStreams propExpr =
  Spec propStreams [] [] [Copilot.Property propName (Copilot.Forall propExpr)]

-- | Build a 'Spec' that contains one property with the given name, which
-- contains the given streams, and is defined by the given boolean expression,
-- which is existentially quantified.
existsPropSpec :: String -> [Stream] -> Expr Bool -> Spec
existsPropSpec propName propStreams propExpr =
  Spec propStreams [] [] [Copilot.Property propName (Copilot.Exists propExpr)]

-- | Equality for 'SatResult'.
--
-- This is an orphan instance, so we suppress the warning that GHC would
-- normally produce with a GHC option at the top.
instance Eq SatResult where
  Valid   == Valid   = True
  Invalid == Invalid = True
  Unknown == Unknown = True
  _       == _       = False