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