-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArbitrary.hs
60 lines (46 loc) · 1.55 KB
/
Arbitrary.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
module Arbitrary where
import Test.QuickCheck
import Syntax
identifier :: Gen String
identifier = elements $ zipWith (\c n -> c:show n) ['a'..'z'] [0..]
genTerm 0 = frequency [
(5, identifier `as` Atom),
(1, elements [Top, Bottom, One, Zero])
]
genTerm n | n > 0 = oneof [
genTerm 0,
genBinaryTerm (n `div` 2),
genUnaryTerm (n `div` 2)
]
genBinaryTerm n = do
connective <- elements [(:*:), (:$:), (:-@:), (:&:), (:+:)]
l <- genTerm n
r <- genTerm n
return (l `connective` r)
genUnaryTerm n = do
connective <- elements [Not, OfCourse, WhyNot]
u <- genTerm n
return (connective u)
exponentOnlyTerm :: Gen Term
exponentOnlyTerm = sized $ \n -> do
connectives <- listOf $ elements [OfCourse, WhyNot]
let term = foldr ($) (Atom "a") connectives
return term
-- This make take a very long time for large ASTs
shrinkDeep op (a, b) = [a, b] ++ [ l `op` r | l <- shrink' a, r <- shrink' b]
where shrink' x = take 2 (shrink x)
-- shrinkDeep op (a, b) = [a, b]
instance Arbitrary Term where
arbitrary = sized $ \n -> genTerm n
shrink (a :*: b) = shrinkDeep (:*:) (a, b)
shrink (a :$: b) = shrinkDeep (:$:) (a, b)
shrink (a :-@: b) = shrinkDeep (:-@:) (a, b)
shrink (a :&: b) = shrinkDeep (:&:) (a, b)
shrink (a :+: b) = shrinkDeep (:+:) (a, b)
shrink (Atom s) = []
shrink (Not t) = t : [Not t' | t' <- shrink t]
shrink (OfCourse t) = t : [OfCourse t' | t' <- shrink t]
shrink (WhyNot t) = t : [WhyNot t' | t' <- shrink t]
shrink _ = []
as :: Gen a -> (a -> b) -> Gen b
as = flip fmap