-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLecture2.hs
177 lines (134 loc) · 4.61 KB
/
Lecture2.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
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
module Lecture2
where
import System.Random
import Test.QuickCheck
getRandomInt :: Int -> IO Int
getRandomInt n = getStdRandom (randomR (0,n))
getRandomInt' n = getStdRandom (randomR (-n,n))
randomFlip :: Int -> IO Int
randomFlip x = do
b <- getRandomInt 1
if b==0 then return x else return (-x)
genIntList :: IO [Int]
genIntList = do
k <- getRandomInt 20
n <- getRandomInt 10
getIntL k n
getIntL :: Int -> Int -> IO [Int]
getIntL _ 0 = return []
getIntL k n = do
x <- getRandomInt k
y <- randomFlip x
xs <- getIntL k (n-1)
return (y:xs)
genIntList' :: IO [Int]
genIntList' = do
k <- getRandomInt 20
n <- getRandomInt 10
getIntL' k n
getIntL' :: Int -> Int -> IO [Int]
getIntL' _ 0 = return []
getIntL' k n = do
x <- getRandomInt' k
xs <- getIntL' k (n-1)
return (x:xs)
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
quicksort [ a | a <- xs, a <= x ]
++ [x]
++ quicksort [ a | a <- xs, a > x ]
isTrue :: a -> Bool
isTrue _ = True
prop_ordered :: Ord a => [a] -> Bool
prop_ordered [] = True
prop_ordered (x:xs) = all (>= x) xs && prop_ordered xs
testR :: Int -> Int -> ([Int] -> [Int])
-> ([Int] -> [Int] -> Bool) -> IO ()
testR k n f r = if k == n then print (show n ++ " tests passed")
else do
xs <- genIntList
if r xs (f xs) then
do print ("pass on: " ++ show xs)
testR (k+1) n f r
else error ("failed test on: " ++ show xs)
testPost :: ([Int] -> [Int]) -> ([Int] -> Bool) -> IO ()
testPost f p = testR 1 100 f (\_ -> p)
testR' :: Int -> Int -> ([Int] -> [Int])
-> ([Int] -> [Int] -> Bool) -> IO ()
testR' k n f r = if k == n then print (show n ++ " tests passed")
else do
xs <- genIntList'
if r xs (f xs) then
do print ("pass on: " ++ show xs)
testR' (k+1) n f r
else error ("failed test on: " ++ show xs)
testPost' :: ([Int] -> [Int]) -> ([Int] -> Bool) -> IO ()
testPost' f p = testR' 1 100 f (\_ -> p)
quicksrt :: Ord a => [a] -> [a]
quicksrt [] = []
quicksrt (x:xs) =
quicksrt [ a | a <- xs, a < x ]
++ [x]
++ quicksrt [ a | a <- xs, a > x ]
samelength :: [Int] -> [Int] -> Bool
samelength xs ys = length xs == length ys
testRl :: ([Int] -> [Int]) -> ([Int] -> [Int] -> Bool) -> IO ()
testRl f r = testR 1 100 f r
testRl' :: ([Int] -> [Int]) -> ([Int] -> [Int] -> Bool) -> IO ()
testRl' f r = testR' 1 100 f r
infix 1 -->
(-->) :: Bool -> Bool -> Bool
p --> q = (not p) || q
forall :: [a] -> (a -> Bool) -> Bool
forall = flip all
hoareTest :: (a -> Bool) -> (a -> a) -> (a -> Bool) -> [a] -> Bool
hoareTest precondition f postcondition =
all (\x -> precondition x --> postcondition (f x))
hoareTestR :: Fractional t =>
(a -> Bool)
-> (a -> a) -> (a -> Bool) -> [a] -> (Bool,t)
hoareTestR precond f postcond testcases = let
a = fromIntegral (length $ filter precond testcases)
b = fromIntegral (length testcases)
in
(all (\x ->
precond x --> postcond (f x)) testcases,a/b)
invarTest :: (a -> Bool) -> (a -> a) -> [a] -> Bool
invarTest invar f = hoareTest invar f invar
invarTestR :: Fractional t =>
(a -> Bool) -> (a -> a) -> [a] -> (Bool,t)
invarTestR invar f = hoareTestR invar f invar
f1,f2 :: Int -> Int
f1 = \n -> sum [0..n]
f2 = \n -> (n*(n+1)) `div` 2
test = verboseCheck (\n -> n >= 0 ==> f1 n == f2 n)
parity n = mod n 2
parity' m n = parity m == parity n
testRel :: (a -> a -> Bool) -> (a -> a) -> [a] -> Bool
testRel spec f = all (\x -> spec x (f x))
testInvar :: Eq b => (a -> b) -> (a -> a) -> [a] -> Bool
testInvar specf = testRel (\ x y -> specf x == specf y)
stronger, weaker :: [a] ->
(a -> Bool) -> (a -> Bool) -> Bool
stronger xs p q = forall xs (\ x -> p x --> q x)
weaker xs p q = stronger xs q p
neg :: (a -> Bool) -> a -> Bool
neg p = \ x -> not (p x)
infixl 2 .&&.
infixl 2 .||.
(.&&.) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
p .&&. q = \ x -> p x && q x
(.||.) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
p .||. q = \ x -> p x || q x
compar :: [a] -> (a -> Bool) -> (a -> Bool) -> String
compar xs p q = let pq = stronger xs p q
qp = stronger xs q p
in
if pq && qp then "equivalent"
else if pq then "stronger"
else if qp then "weaker"
else "incomparable"
infixl 2 #
(#) :: (a -> b) -> (b -> c) -> (a -> c)
(#) = flip (.)