src/HOL/Tools/Quickcheck/Narrowing_Engine.hs
author bulwahn
Mon Sep 26 10:30:37 2011 +0200 (2011-09-26)
changeset 45081 f00e52acbd42
parent 45003 7591039fb6b4
child 45685 e2e928af750b
permissions -rw-r--r--
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
     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 -> IO b) -> (Pos -> IO b) -> IO b;
    41 answer a known unknown =
    42   Control.Exception.catch (answeri a known unknown) 
    43     (\ (PatternMatchFail _) -> known True);
    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 -> IO a) -> (Pos -> IO a) -> IO a;
    54 eval p k u = answer p (\p -> answer p k u) u;
    55 
    56 ref :: Result -> [Generated_Code.Narrowing_term] -> IO Int;
    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           
    59 refute :: Result -> IO Int;
    60 refute r = ref r (args r);
    61 
    62 sumMapM :: (a -> IO Int) -> Int -> [a] -> IO Int;
    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 
    68 instance Show Generated_Code.Typerep where {
    69   show (Generated_Code.Typerep c ts) = "Type (\"" ++ c ++ "\", " ++ show ts ++ ")";
    70 };
    71 
    72 instance Show Generated_Code.Term where {
    73   show (Generated_Code.Const c t) = "Const (\"" ++ c ++ "\", " ++ show t ++ ")";
    74   show (Generated_Code.App s t) = "(" ++ show s ++ ") $ (" ++ show t ++ ")";
    75   show (Generated_Code.Abs s ty t) = "Abs (\"" ++ s ++ "\", " ++ show ty ++ ", " ++ show t ++ ")";
    76   show (Generated_Code.Free s ty) = "Free (\"" ++ s ++  "\", " ++ show ty ++ ")";
    77 };
    78 
    79 data Result =
    80   Result { args     :: [Generated_Code.Narrowing_term]
    81          , showArgs :: [Generated_Code.Narrowing_term -> String]
    82          , apply_fun    :: [Generated_Code.Narrowing_term] -> Bool
    83          };
    84 
    85 data P = P (Int -> Int -> Result);
    86 
    87 run :: Testable a => ([Generated_Code.Narrowing_term] -> a) -> Int -> Int -> Result;
    88 run a = let P f = property a in f;
    89 
    90 class Testable a where {
    91   property :: ([Generated_Code.Narrowing_term] -> a) -> P;
    92 };
    93 
    94 instance Testable Bool where {
    95   property app = P $ \n d -> Result [] [] (app . reverse);
    96 };
    97 
    98 instance (Generated_Code.Partial_term_of a, Generated_Code.Narrowing a, Testable b) => Testable (a -> b) where {
    99   property f = P $ \n d ->
   100     let Generated_Code.C t c = Generated_Code.narrowing d
   101         c' = Generated_Code.conv c
   102         r = run (\(x:xs) -> f xs (c' x)) (n+1) d
   103     in  r { args = Generated_Code.Var [n] t : args r,
   104       showArgs = (show . Generated_Code.partial_term_of (Generated_Code.Type :: Generated_Code.Itself a)) : showArgs r };
   105 };
   106 
   107 -- Top-level interface
   108 
   109 depthCheck :: Testable a => Int -> a -> IO ();
   110 depthCheck d p =
   111   (refute $ run (const p) 0 d) >> putStrLn ("NONE") >> hFlush stdout;
   112 
   113 smallCheck :: Testable a => Int -> a -> IO ();
   114 smallCheck d p = mapM_ (`depthCheck` p) [0..d] >> putStrLn ("NONE") >> hFlush stdout;
   115 
   116 }
   117