author | huffman |
Wed, 17 Aug 2011 15:12:34 -0700 | |
changeset 44262 | 355d5438f5fb |
parent 43079 | 4022892a2f28 |
child 44751 | f523923d8182 |
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; |
|
7 |
import Code; |
|
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 |
||
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
13 |
new :: Pos -> [[Narrowing_type]] -> [Narrowing_term]; |
41905 | 14 |
new p ps = [ Ctr c (zipWith (\i t -> Var (p++[i]) t) [0..] ts) |
15 |
| (c, ts) <- zip [0..] ps ]; |
|
16 |
||
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
17 |
refine :: Narrowing_term -> Pos -> [Narrowing_term]; |
41905 | 18 |
refine (Var p (SumOfProd ss)) [] = new p ss; |
19 |
refine (Ctr c xs) p = map (Ctr c) (refineList xs p); |
|
20 |
||
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
21 |
refineList :: [Narrowing_term] -> Pos -> [[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 |
||
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
26 |
total :: Narrowing_term -> [Narrowing_term]; |
41905 | 27 |
total (Ctr c xs) = [Ctr c ys | ys <- mapM total xs]; |
28 |
total (Var p (SumOfProd ss)) = [y | x <- new p ss, y <- total x]; |
|
29 |
||
30 |
-- Answers |
|
31 |
||
32 |
answer :: a -> (a -> IO b) -> (Pos -> IO b) -> IO b; |
|
33 |
answer a known unknown = |
|
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 |
||
40 |
-- Refute |
|
41 |
||
42 |
str_of_list [] = "[]"; |
|
43 |
str_of_list (x:xs) = "(" ++ x ++ " :: " ++ str_of_list xs ++ ")"; |
|
44 |
||
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
45 |
report :: Result -> [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
|
46 |
report r xs = putStrLn ("SOME (" ++ (str_of_list $ zipWith ($) (showArgs r) xs) ++ ")") >> hFlush stdout >> exitWith ExitSuccess; |
41905 | 47 |
|
48 |
eval :: Bool -> (Bool -> IO a) -> (Pos -> IO a) -> IO a; |
|
49 |
eval p k u = answer p (\p -> answer p k u) u; |
|
50 |
||
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
51 |
ref :: Result -> [Narrowing_term] -> IO Int; |
41905 | 52 |
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)); |
53 |
||
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
|
54 |
refute :: Result -> IO Int; |
41905 | 55 |
refute r = ref r (args r); |
56 |
||
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
|
57 |
sumMapM :: (a -> IO Int) -> Int -> [a] -> IO Int; |
41905 | 58 |
sumMapM f n [] = return n; |
59 |
sumMapM f n (a:as) = seq n (do m <- f a ; sumMapM f (n+m) as); |
|
60 |
||
61 |
-- Testable |
|
62 |
||
63 |
instance Show Typerep where { |
|
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
|
64 |
show (Typerep c ts) = "Type (\"" ++ c ++ "\", " ++ show ts ++ ")"; |
41905 | 65 |
}; |
66 |
||
41933
10f254a4e5b9
adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
67 |
instance Show Term where { |
41905 | 68 |
show (Const c t) = "Const (\"" ++ c ++ "\", " ++ show t ++ ")"; |
69 |
show (App s t) = "(" ++ show s ++ ") $ (" ++ show t ++ ")"; |
|
43079
4022892a2f28
improving overlord option and partial_term_of derivation; changing Narrowing_Engine to print partial terms
bulwahn
parents:
43047
diff
changeset
|
70 |
show (Abs s ty t) = "Abs (\"" ++ s ++ "\", " ++ show ty ++ ", " ++ show t ++ ")"; |
4022892a2f28
improving overlord option and partial_term_of derivation; changing Narrowing_Engine to print partial terms
bulwahn
parents:
43047
diff
changeset
|
71 |
show (Free s ty) = "Free (\"" ++ s ++ "\", " ++ show ty ++ ")"; |
41905 | 72 |
}; |
73 |
||
74 |
data Result = |
|
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
75 |
Result { args :: [Narrowing_term] |
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
76 |
, showArgs :: [Narrowing_term -> String] |
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
77 |
, apply_fun :: [Narrowing_term] -> Bool |
41905 | 78 |
}; |
79 |
||
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
|
80 |
data P = P (Int -> Int -> Result); |
41905 | 81 |
|
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
82 |
run :: Testable a => ([Narrowing_term] -> a) -> Int -> Int -> Result; |
41905 | 83 |
run a = let P f = property a in f; |
84 |
||
85 |
class Testable a where { |
|
43047
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn
parents:
42090
diff
changeset
|
86 |
property :: ([Narrowing_term] -> a) -> P; |
41905 | 87 |
}; |
88 |
||
89 |
instance Testable Bool where { |
|
90 |
property app = P $ \n d -> Result [] [] (app . reverse); |
|
91 |
}; |
|
92 |
||
43079
4022892a2f28
improving overlord option and partial_term_of derivation; changing Narrowing_Engine to print partial terms
bulwahn
parents:
43047
diff
changeset
|
93 |
instance (Partial_term_of a, Narrowing a, Testable b) => Testable (a -> b) where { |
41905 | 94 |
property f = P $ \n d -> |
41962
27a61a3266d8
correcting names in Narrowing_Engine and example theory for Quickcheck_Narrowing
bulwahn
parents:
41933
diff
changeset
|
95 |
let C t c = narrowing d |
41905 | 96 |
c' = conv c |
97 |
r = run (\(x:xs) -> f xs (c' x)) (n+1) d |
|
43079
4022892a2f28
improving overlord option and partial_term_of derivation; changing Narrowing_Engine to print partial terms
bulwahn
parents:
43047
diff
changeset
|
98 |
in r { args = Var [n] t : args r, showArgs = (show . partial_term_of (Type :: Itself a)) : showArgs r }; |
41905 | 99 |
}; |
100 |
||
101 |
-- Top-level interface |
|
102 |
||
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
|
103 |
depthCheck :: Testable a => Int -> a -> IO (); |
41905 | 104 |
depthCheck d p = |
42090
ef566ce50170
changing Quickcheck_Narrowing's main function to enumerate the depth instead upto the depth
bulwahn
parents:
41962
diff
changeset
|
105 |
(refute $ run (const p) 0 d) >> putStrLn ("NONE") >> hFlush stdout; |
41905 | 106 |
|
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
|
107 |
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
|
108 |
smallCheck d p = mapM_ (`depthCheck` p) [0..d] >> putStrLn ("NONE") >> hFlush stdout; |
41905 | 109 |
|
110 |
} |
|
111 |