src/HOL/Tools/Quickcheck/Narrowing_Engine.hs
author bulwahn
Wed Nov 30 09:21:11 2011 +0100 (2011-11-30)
changeset 45685 e2e928af750b
parent 45081 f00e52acbd42
child 45725 2987b29518aa
permissions -rw-r--r--
quickcheck narrowing also shows potential counterexamples
     1 module Narrowing_Engine where {
     2 
     3 import Monad;
     4 import Control.Exception;
     5 import System.IO;
     6 import System.Exit;
     7 import qualified Generated_Code;
     8 
     9 type Pos = [Int];
    10 
    11 -- Term refinement
    12 
    13 new :: Pos -> [[Generated_Code.Narrowing_type]] -> [Generated_Code.Narrowing_term];
    14 new p ps = [ Generated_Code.Ctr c (zipWith (\i t -> Generated_Code.Var (p++[i]) t) [0..] ts)
    15            | (c, ts) <- zip [0..] ps ];
    16 
    17 refine :: Generated_Code.Narrowing_term -> Pos -> [Generated_Code.Narrowing_term];
    18 refine (Generated_Code.Var p (Generated_Code.SumOfProd ss)) [] = new p ss;
    19 refine (Generated_Code.Ctr c xs) p = map (Generated_Code.Ctr c) (refineList xs p);
    20 
    21 refineList :: [Generated_Code.Narrowing_term] -> Pos -> [[Generated_Code.Narrowing_term]];
    22 refineList xs (i:is) = let (ls, x:rs) = splitAt i xs in [ls ++ y:rs | y <- refine x is];
    23 
    24 -- Find total instantiations of a partial value
    25 
    26 total :: Generated_Code.Narrowing_term -> [Generated_Code.Narrowing_term];
    27 total (Generated_Code.Ctr c xs) = [Generated_Code.Ctr c ys | ys <- mapM total xs];
    28 total (Generated_Code.Var p (Generated_Code.SumOfProd ss)) = [y | x <- new p ss, y <- total x];
    29 
    30 -- Answers
    31 
    32 answeri :: a -> (a -> IO b) -> (Pos -> IO b) -> IO b;
    33 answeri a known unknown =
    34   try (evaluate a) >>= (\res ->
    35      case res of
    36        Right b -> known b
    37        Left (ErrorCall ('\0':p)) -> unknown (map fromEnum p)
    38        Left e -> throw e);
    39 
    40 answer :: Bool -> Bool -> (Bool -> IO b) -> (Pos -> IO b) -> IO b;
    41 answer potential a known unknown =
    42   Control.Exception.catch (answeri a known unknown) 
    43     (\ (PatternMatchFail _) -> known (not potential));
    44 
    45 -- Refute
    46 
    47 str_of_list [] = "[]";
    48 str_of_list (x:xs) = "(" ++ x ++ " :: " ++ str_of_list xs ++ ")";
    49 
    50 report :: Result -> [Generated_Code.Narrowing_term] -> IO Int;
    51 report r xs = putStrLn ("SOME (" ++ (str_of_list $ zipWith ($) (showArgs r) xs) ++ ")") >> hFlush stdout >> exitWith ExitSuccess;
    52 
    53 eval :: Bool -> Bool -> (Bool -> IO a) -> (Pos -> IO a) -> IO a;
    54 eval potential p k u = answer potential p (\p -> answer potential p k u) u;
    55 
    56 ref :: Bool -> Result -> [Generated_Code.Narrowing_term] -> IO Int;
    57 ref potential r xs = eval potential (apply_fun r xs) (\res -> if res then return 1 else report r xs)
    58   (\p -> sumMapM (ref potential r) 1 (refineList xs p));
    59           
    60 refute :: Bool -> Result -> IO Int;
    61 refute potential r = ref potential r (args r);
    62 
    63 sumMapM :: (a -> IO Int) -> Int -> [a] -> IO Int;
    64 sumMapM f n [] = return n;
    65 sumMapM f n (a:as) = seq n (do m <- f a ; sumMapM f (n+m) as);
    66 
    67 -- Testable
    68 
    69 instance Show Generated_Code.Typerep where {
    70   show (Generated_Code.Typerep c ts) = "Type (\"" ++ c ++ "\", " ++ show ts ++ ")";
    71 };
    72 
    73 instance Show Generated_Code.Term where {
    74   show (Generated_Code.Const c t) = "Const (\"" ++ c ++ "\", " ++ show t ++ ")";
    75   show (Generated_Code.App s t) = "(" ++ show s ++ ") $ (" ++ show t ++ ")";
    76   show (Generated_Code.Abs s ty t) = "Abs (\"" ++ s ++ "\", " ++ show ty ++ ", " ++ show t ++ ")";
    77   show (Generated_Code.Free s ty) = "Free (\"" ++ s ++  "\", " ++ show ty ++ ")";
    78 };
    79 
    80 data Result =
    81   Result { args     :: [Generated_Code.Narrowing_term]
    82          , showArgs :: [Generated_Code.Narrowing_term -> String]
    83          , apply_fun    :: [Generated_Code.Narrowing_term] -> Bool
    84          };
    85 
    86 data P = P (Int -> Int -> Result);
    87 
    88 run :: Testable a => ([Generated_Code.Narrowing_term] -> a) -> Int -> Int -> Result;
    89 run a = let P f = property a in f;
    90 
    91 class Testable a where {
    92   property :: ([Generated_Code.Narrowing_term] -> a) -> P;
    93 };
    94 
    95 instance Testable Bool where {
    96   property app = P $ \n d -> Result [] [] (app . reverse);
    97 };
    98 
    99 instance (Generated_Code.Partial_term_of a, Generated_Code.Narrowing a, Testable b) => Testable (a -> b) where {
   100   property f = P $ \n d ->
   101     let Generated_Code.C t c = Generated_Code.narrowing d
   102         c' = Generated_Code.conv c
   103         r = run (\(x:xs) -> f xs (c' x)) (n+1) d
   104     in  r { args = Generated_Code.Var [n] t : args r,
   105       showArgs = (show . Generated_Code.partial_term_of (Generated_Code.Type :: Generated_Code.Itself a)) : showArgs r };
   106 };
   107 
   108 -- Top-level interface
   109 
   110 depthCheck :: Testable a => Bool -> Int -> a -> IO ();
   111 depthCheck potential d p =
   112   (refute potential $ run (const p) 0 d) >> putStrLn ("NONE") >> hFlush stdout;
   113 
   114 smallCheck :: Testable a => Bool -> Int -> a -> IO ();
   115 smallCheck potential d p = mapM_ (\d -> depthCheck potential d p) [0..d] >> putStrLn ("NONE") >> hFlush stdout;
   116 
   117 }
   118