src/HOL/Tools/Quickcheck/Narrowing_Engine.hs
author huffman
Wed, 17 Aug 2011 15:12:34 -0700
changeset 44262 355d5438f5fb
parent 43079 4022892a2f28
child 44751 f523923d8182
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41933
10f254a4e5b9 adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
     1
module Narrowing_Engine where {
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     2
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     3
import Monad;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     4
import Control.Exception;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     5
import System.IO;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     6
import System.Exit;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     7
import Code;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    10
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    11
-- Term refinement
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    14
new p ps = [ Ctr c (zipWith (\i t -> Var (p++[i]) t) [0..] ts)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    15
           | (c, ts) <- zip [0..] ps ];
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    18
refine (Var p (SumOfProd ss)) [] = new p ss;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    19
refine (Ctr c xs) p = map (Ctr c) (refineList xs p);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    23
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    24
-- Find total instantiations of a partial value
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    27
total (Ctr c xs) = [Ctr c ys | ys <- mapM total xs];
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    28
total (Var p (SumOfProd ss)) = [y | x <- new p ss, y <- total x];
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    29
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    30
-- Answers
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    31
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    32
answer :: a -> (a -> IO b) -> (Pos -> IO b) -> IO b;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    33
answer a known unknown =
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    34
  try (evaluate a) >>= (\res ->
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    35
     case res of
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    38
       Left e -> throw e);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    39
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    40
-- Refute
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    41
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    42
str_of_list [] = "[]";
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    43
str_of_list (x:xs) = "(" ++ x ++ " :: " ++ str_of_list xs ++ ")";
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    47
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    48
eval :: Bool -> (Bool -> IO a) -> (Pos -> IO a) -> IO a;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    49
eval p k u = answer p (\p -> answer p k u) u;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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));
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    55
refute r = ref r (args r);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    58
sumMapM f n [] = return n;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    59
sumMapM f n (a:as) = seq n (do m <- f a ; sumMapM f (n+m) as);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    60
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    61
-- Testable
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    62
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    65
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    66
41933
10f254a4e5b9 adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
    67
instance Show Term where {
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    68
  show (Const c t) = "Const (\"" ++ c ++ "\", " ++ show t ++ ")";
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    72
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    73
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    78
         };
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    83
run a = let P f = property a in f;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    84
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    87
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    88
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    89
instance Testable Bool where {
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    90
  property app = P $ \n d -> Result [] [] (app . reverse);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    91
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    96
        c' = conv c
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    99
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   100
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   101
-- Top-level interface
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   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
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   109
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   110
}
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   111