author | bulwahn |
Mon, 26 Sep 2011 10:30:37 +0200 | |
changeset 45081 | f00e52acbd42 |
parent 45003 | 7591039fb6b4 |
child 45685 | e2e928af750b |
permissions | -rw-r--r-- |
41933
10f254a4e5b9
adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
1 |
module Narrowing_Engine where { |
41905 | 2 |
|
3 |
import Monad; |
|
4 |
import Control.Exception; |
|
5 |
import System.IO; |
|
6 |
import System.Exit; |
|
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
7 |
import qualified Generated_Code; |
41905 | 8 |
|
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
9 |
type Pos = [Int]; |
41905 | 10 |
|
11 |
-- Term refinement |
|
12 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
13 |
new :: Pos -> [[Generated_Code.Narrowing_type]] -> [Generated_Code.Narrowing_term]; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
14 |
new p ps = [ Generated_Code.Ctr c (zipWith (\i t -> Generated_Code.Var (p++[i]) t) [0..] ts) |
41905 | 15 |
| (c, ts) <- zip [0..] ps ]; |
16 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
17 |
refine :: Generated_Code.Narrowing_term -> Pos -> [Generated_Code.Narrowing_term]; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
18 |
refine (Generated_Code.Var p (Generated_Code.SumOfProd ss)) [] = new p ss; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
19 |
refine (Generated_Code.Ctr c xs) p = map (Generated_Code.Ctr c) (refineList xs p); |
41905 | 20 |
|
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
21 |
refineList :: [Generated_Code.Narrowing_term] -> Pos -> [[Generated_Code.Narrowing_term]]; |
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
22 |
refineList xs (i:is) = let (ls, x:rs) = splitAt i xs in [ls ++ y:rs | y <- refine x is]; |
41905 | 23 |
|
24 |
-- Find total instantiations of a partial value |
|
25 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
26 |
total :: Generated_Code.Narrowing_term -> [Generated_Code.Narrowing_term]; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
27 |
total (Generated_Code.Ctr c xs) = [Generated_Code.Ctr c ys | ys <- mapM total xs]; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
28 |
total (Generated_Code.Var p (Generated_Code.SumOfProd ss)) = [y | x <- new p ss, y <- total x]; |
41905 | 29 |
|
30 |
-- Answers |
|
31 |
||
45003
7591039fb6b4
catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents:
44751
diff
changeset
|
32 |
answeri :: a -> (a -> IO b) -> (Pos -> IO b) -> IO b; |
7591039fb6b4
catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents:
44751
diff
changeset
|
33 |
answeri a known unknown = |
41905 | 34 |
try (evaluate a) >>= (\res -> |
35 |
case res of |
|
36 |
Right b -> known b |
|
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
37 |
Left (ErrorCall ('\0':p)) -> unknown (map fromEnum p) |
41905 | 38 |
Left e -> throw e); |
39 |
||
45003
7591039fb6b4
catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents:
44751
diff
changeset
|
40 |
answer :: Bool -> (Bool -> IO b) -> (Pos -> IO b) -> IO b; |
7591039fb6b4
catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents:
44751
diff
changeset
|
41 |
answer a known unknown = |
7591039fb6b4
catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents:
44751
diff
changeset
|
42 |
Control.Exception.catch (answeri a known unknown) |
7591039fb6b4
catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents:
44751
diff
changeset
|
43 |
(\ (PatternMatchFail _) -> known True); |
7591039fb6b4
catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents:
44751
diff
changeset
|
44 |
|
41905 | 45 |
-- Refute |
46 |
||
47 |
str_of_list [] = "[]"; |
|
48 |
str_of_list (x:xs) = "(" ++ x ++ " :: " ++ str_of_list xs ++ ")"; |
|
49 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
50 |
report :: Result -> [Generated_Code.Narrowing_term] -> IO Int; |
43079
4022892a2f28
improving overlord option and partial_term_of derivation; changing Narrowing_Engine to print partial terms
bulwahn
parents:
43047
diff
changeset
|
51 |
report r xs = putStrLn ("SOME (" ++ (str_of_list $ zipWith ($) (showArgs r) xs) ++ ")") >> hFlush stdout >> exitWith ExitSuccess; |
41905 | 52 |
|
53 |
eval :: Bool -> (Bool -> IO a) -> (Pos -> IO a) -> IO a; |
|
54 |
eval p k u = answer p (\p -> answer p k u) u; |
|
55 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
56 |
ref :: Result -> [Generated_Code.Narrowing_term] -> IO Int; |
41905 | 57 |
ref r xs = eval (apply_fun r xs) (\res -> if res then return 1 else report r xs) (\p -> sumMapM (ref r) 1 (refineList xs p)); |
58 |
||
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
59 |
refute :: Result -> IO Int; |
41905 | 60 |
refute r = ref r (args r); |
61 |
||
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
62 |
sumMapM :: (a -> IO Int) -> Int -> [a] -> IO Int; |
41905 | 63 |
sumMapM f n [] = return n; |
64 |
sumMapM f n (a:as) = seq n (do m <- f a ; sumMapM f (n+m) as); |
|
65 |
||
66 |
-- Testable |
|
67 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
68 |
instance Show Generated_Code.Typerep where { |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
69 |
show (Generated_Code.Typerep c ts) = "Type (\"" ++ c ++ "\", " ++ show ts ++ ")"; |
41905 | 70 |
}; |
71 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
72 |
instance Show Generated_Code.Term where { |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
73 |
show (Generated_Code.Const c t) = "Const (\"" ++ c ++ "\", " ++ show t ++ ")"; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
74 |
show (Generated_Code.App s t) = "(" ++ show s ++ ") $ (" ++ show t ++ ")"; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
75 |
show (Generated_Code.Abs s ty t) = "Abs (\"" ++ s ++ "\", " ++ show ty ++ ", " ++ show t ++ ")"; |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
76 |
show (Generated_Code.Free s ty) = "Free (\"" ++ s ++ "\", " ++ show ty ++ ")"; |
41905 | 77 |
}; |
78 |
||
79 |
data Result = |
|
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
80 |
Result { args :: [Generated_Code.Narrowing_term] |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
81 |
, showArgs :: [Generated_Code.Narrowing_term -> String] |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
82 |
, apply_fun :: [Generated_Code.Narrowing_term] -> Bool |
41905 | 83 |
}; |
84 |
||
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
85 |
data P = P (Int -> Int -> Result); |
41905 | 86 |
|
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
87 |
run :: Testable a => ([Generated_Code.Narrowing_term] -> a) -> Int -> Int -> Result; |
41905 | 88 |
run a = let P f = property a in f; |
89 |
||
90 |
class Testable a where { |
|
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
91 |
property :: ([Generated_Code.Narrowing_term] -> a) -> P; |
41905 | 92 |
}; |
93 |
||
94 |
instance Testable Bool where { |
|
95 |
property app = P $ \n d -> Result [] [] (app . reverse); |
|
96 |
}; |
|
97 |
||
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
98 |
instance (Generated_Code.Partial_term_of a, Generated_Code.Narrowing a, Testable b) => Testable (a -> b) where { |
41905 | 99 |
property f = P $ \n d -> |
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
100 |
let Generated_Code.C t c = Generated_Code.narrowing d |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
101 |
c' = Generated_Code.conv c |
41905 | 102 |
r = run (\(x:xs) -> f xs (c' x)) (n+1) d |
45081
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
103 |
in r { args = Generated_Code.Var [n] t : args r, |
f00e52acbd42
importing the Generated_Code module qualified to reduce the probability of name clashes between the static code and the generated code in the narrowing-based Quickcheck
bulwahn
parents:
45003
diff
changeset
|
104 |
showArgs = (show . Generated_Code.partial_term_of (Generated_Code.Type :: Generated_Code.Itself a)) : showArgs r }; |
41905 | 105 |
}; |
106 |
||
107 |
-- Top-level interface |
|
108 |
||
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
109 |
depthCheck :: Testable a => Int -> a -> IO (); |
41905 | 110 |
depthCheck d p = |
42090
ef566ce50170
changing Quickcheck_Narrowing's main function to enumerate the depth instead upto the depth
bulwahn
parents:
41962
diff
changeset
|
111 |
(refute $ run (const p) 0 d) >> putStrLn ("NONE") >> hFlush stdout; |
41905 | 112 |
|
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
113 |
smallCheck :: Testable a => Int -> a -> IO (); |
42090
ef566ce50170
changing Quickcheck_Narrowing's main function to enumerate the depth instead upto the depth
bulwahn
parents:
41962
diff
changeset
|
114 |
smallCheck d p = mapM_ (`depthCheck` p) [0..d] >> putStrLn ("NONE") >> hFlush stdout; |
41905 | 115 |
|
116 |
} |
|
117 |