-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.hs
71 lines (60 loc) · 2.09 KB
/
calculator.hs
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
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving, CPP #-} -- for GHC <= 7.8
-- Example taken from Lee Pike's SmartCheck:
-- https://github.com/leepike/SmartCheck/blob/master/paper/paper.pdf
-- https://github.com/leepike/smartcheck
-- The version here is the one from the paper (similar to the one in the README
-- file). I chose the one in the paper as it is clearer.
import Control.Monad
import Test.Extrapolate
import Data.Maybe
#if __GLASGOW_HASKELL__ < 710
import Data.Typeable (Typeable)
deriving instance Typeable Exp
#endif
data Exp = C Int
| Add Exp Exp
| Div Exp Exp
deriving (Eq, Ord, Show)
eval :: Exp -> Maybe Int
eval (C i) = Just i
eval (Add e0 e1) =
liftM2 (+) (eval e0) (eval e1)
eval (Div e0 e1) =
let e = eval e1 in
if e == Just 0 then Nothing
else liftM2 div (eval e0) e
-- originally called divSubTerms by Pike
noDiv0 :: Exp -> Bool
noDiv0 (C _) = True
noDiv0 (Div _ (C 0)) = False
noDiv0 (Add e0 e1) = noDiv0 e0 && noDiv0 e1
noDiv0 (Div e0 e1) = noDiv0 e0 && noDiv0 e1
prop_div :: Exp -> Bool
prop_div e = noDiv0 e ==> eval e /= Nothing
instance Listable Exp where
tiers = cons1 C
\/ cons2 Add
\/ cons2 Div
-- deriveGeneralizable ''Exp
-- {-
instance Name Exp where name _ = "e1"
instance Express Exp where
expr (C i) = value "C" C :$ expr i
expr (Add e1 e2) = value "Add" Add :$ expr e1 :$ expr e2
expr (Div e1 e2) = value "Div" Div :$ expr e1 :$ expr e2
instance Generalizable Exp where
background e = [ value "eval" eval
, value "noDiv0" noDiv0 ]
subInstances _ = instances (undefined :: Int)
-- -}
main :: IO ()
main = do
check prop_div
check (isJust . eval)
-- The following generalized counter-example from the paper is wrong!
-- > forall x. Div x (Add (C (-5)) (C 5))
print $ prop_div (Div (C 0) (C 0 `Add` C 0))
-- setting x to (Div (C 0) (C 0)) makes the property pass
print $ prop_div (Div (C 0 `Div` C 0) (C 0 `Add` C 0))
-- As Lee Pike points out, SmartCheck's algorithm is unsound anyway,
-- sometimes returning generalized counter-examples that are *too* general.