src/HOL/Tools/Quickcheck/Narrowing_Engine.hs
author bulwahn
Mon, 26 Sep 2011 10:30:37 +0200
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
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;
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
     7
import qualified Generated_Code;
41905
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
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    13
new :: Pos -> [[Generated_Code.Narrowing_type]] -> [Generated_Code.Narrowing_term];
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    14
new p ps = [ Generated_Code.Ctr c (zipWith (\i t -> Generated_Code.Var (p++[i]) t) [0..] ts)
41905
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
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    17
refine :: Generated_Code.Narrowing_term -> Pos -> [Generated_Code.Narrowing_term];
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    18
refine (Generated_Code.Var p (Generated_Code.SumOfProd ss)) [] = new p ss;
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    19
refine (Generated_Code.Ctr c xs) p = map (Generated_Code.Ctr c) (refineList xs p);
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    20
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    21
refineList :: [Generated_Code.Narrowing_term] -> Pos -> [[Generated_Code.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
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    26
total :: Generated_Code.Narrowing_term -> [Generated_Code.Narrowing_term];
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    27
total (Generated_Code.Ctr c xs) = [Generated_Code.Ctr c ys | ys <- mapM total xs];
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    28
total (Generated_Code.Var p (Generated_Code.SumOfProd ss)) = [y | x <- new p ss, y <- total x];
41905
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
45003
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    32
answeri :: a -> (a -> IO b) -> (Pos -> IO b) -> IO b;
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    33
answeri a known unknown =
41905
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
45003
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    40
answer :: Bool -> (Bool -> IO b) -> (Pos -> IO b) -> IO b;
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    41
answer a known unknown =
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    42
  Control.Exception.catch (answeri a known unknown) 
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    43
    (\ (PatternMatchFail _) -> known True);
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    44
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    45
-- Refute
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    46
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    47
str_of_list [] = "[]";
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    48
str_of_list (x:xs) = "(" ++ x ++ " :: " ++ str_of_list xs ++ ")";
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    49
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    50
report :: Result -> [Generated_Code.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
    51
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
    52
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    53
eval :: Bool -> (Bool -> IO a) -> (Pos -> IO a) -> IO a;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    54
eval p k u = answer p (\p -> answer p k u) u;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    55
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    56
ref :: Result -> [Generated_Code.Narrowing_term] -> IO Int;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    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));
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    58
          
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
    59
refute :: Result -> IO Int;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    60
refute r = ref r (args r);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    61
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
    62
sumMapM :: (a -> IO Int) -> Int -> [a] -> IO Int;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    63
sumMapM f n [] = return n;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    64
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
    65
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    66
-- Testable
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    67
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    68
instance Show Generated_Code.Typerep where {
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    69
  show (Generated_Code.Typerep c ts) = "Type (\"" ++ c ++ "\", " ++ show ts ++ ")";
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    70
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    71
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    72
instance Show Generated_Code.Term where {
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    73
  show (Generated_Code.Const c t) = "Const (\"" ++ c ++ "\", " ++ show t ++ ")";
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    74
  show (Generated_Code.App s t) = "(" ++ show s ++ ") $ (" ++ show t ++ ")";
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    75
  show (Generated_Code.Abs s ty t) = "Abs (\"" ++ s ++ "\", " ++ show ty ++ ", " ++ show t ++ ")";
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    76
  show (Generated_Code.Free s ty) = "Free (\"" ++ s ++  "\", " ++ show ty ++ ")";
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    77
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    78
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    79
data Result =
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    80
  Result { args     :: [Generated_Code.Narrowing_term]
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    81
         , showArgs :: [Generated_Code.Narrowing_term -> String]
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    82
         , apply_fun    :: [Generated_Code.Narrowing_term] -> Bool
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    83
         };
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    84
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
    85
data P = P (Int -> Int -> Result);
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    86
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    87
run :: Testable a => ([Generated_Code.Narrowing_term] -> a) -> Int -> Int -> Result;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    88
run a = let P f = property a in f;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    89
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    90
class Testable a where {
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    91
  property :: ([Generated_Code.Narrowing_term] -> a) -> P;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    92
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    93
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    94
instance Testable Bool where {
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    95
  property app = P $ \n d -> Result [] [] (app . reverse);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    96
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    97
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
    98
instance (Generated_Code.Partial_term_of a, Generated_Code.Narrowing a, Testable b) => Testable (a -> b) where {
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    99
  property f = P $ \n d ->
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
   100
    let Generated_Code.C t c = Generated_Code.narrowing d
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
   101
        c' = Generated_Code.conv c
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   102
        r = run (\(x:xs) -> f xs (c' x)) (n+1) d
45081
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
   103
    in  r { args = Generated_Code.Var [n] t : args r,
f00e52acbd42 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
bulwahn
parents: 45003
diff changeset
   104
      showArgs = (show . Generated_Code.partial_term_of (Generated_Code.Type :: Generated_Code.Itself a)) : showArgs r };
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   105
};
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   106
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   107
-- Top-level interface
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   108
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
   109
depthCheck :: Testable a => Int -> a -> IO ();
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   110
depthCheck d p =
42090
ef566ce50170 changing Quickcheck_Narrowing's main function to enumerate the depth instead upto the depth
bulwahn
parents: 41962
diff changeset
   111
  (refute $ run (const p) 0 d) >> putStrLn ("NONE") >> hFlush stdout;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   112
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
   113
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
   114
smallCheck d p = mapM_ (`depthCheck` p) [0..d] >> putStrLn ("NONE") >> hFlush stdout;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   115
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   116
}
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   117