src/HOL/Tools/Quickcheck/PNF_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:
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     1
{-
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     2
A narrowing-based Evaluator for Formulas in Prefix Normal Form based on the compilation technique of LazySmallCheck
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     3
-}
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     4
module Narrowing_Engine where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     5
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     6
import Monad
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     7
import Control.Exception
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     8
import System.Exit
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
     9
import Maybe
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    10
import List (partition, findIndex)
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
    11
import qualified Generated_Code
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    12
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    13
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    14
type Pos = [Int]
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    15
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    16
-- Term refinement
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    17
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    18
-- Operation: termOf
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    19
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    20
posOf :: Edge -> Pos
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    21
posOf (VN pos _) = pos
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    22
posOf (CtrB pos _) = pos
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    23
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    24
tailPosEdge :: Edge -> Edge
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    25
tailPosEdge (VN pos ty) = VN (tail pos) ty
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    26
tailPosEdge (CtrB pos ts) = CtrB (tail pos) ts
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    27
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
    28
termOf :: Pos -> Path -> 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
    29
termOf pos (CtrB [] i : es) = Generated_Code.Ctr i (termListOf pos es)
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
    30
termOf pos [VN [] ty] = Generated_Code.Var pos ty
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    31
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
    32
termListOf :: Pos -> Path -> [Generated_Code.Narrowing_term]
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    33
termListOf pos es = termListOf' 0 es
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    34
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    35
    termListOf' i [] = []
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    36
    termListOf' i (e : es) =
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    37
      let 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    38
        (ts, rs) = List.partition (\e -> head (posOf e) == i) (e : es)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    39
        t = termOf (pos ++ [i]) (map tailPosEdge ts)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    40
      in
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    41
        (t : termListOf' (i + 1) rs) 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    42
{-
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    43
conv :: [[Term] -> a] -> Term -> a
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    44
conv cs (Var p _) = error ('\0':map toEnum p)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    45
conv cs (Ctr i xs) = (cs !! i) xs
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    46
-}
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    47
-- Answers
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    48
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    49
data Answer = Known Bool | Unknown Pos deriving Show;
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    50
45003
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    51
answeri :: a -> (a -> IO b) -> (Pos -> IO b) -> IO b;
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    52
answeri a known unknown =
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    53
  do res <- try (evaluate a)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    54
     case res of
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    55
       Right b -> known b
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    56
       Left (ErrorCall ('\0':p)) -> unknown (map fromEnum p)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    57
       Left e -> throw e
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    58
45003
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    59
answer :: Bool -> (Bool -> IO b) -> (Pos -> IO b) -> IO b;
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    60
answer a known unknown =
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    61
  Control.Exception.catch (answeri a known unknown) 
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    62
    (\ (PatternMatchFail _) -> known True)
7591039fb6b4 catch PatternMatchFail exceptions in narrowing-based quickcheck
bulwahn
parents: 44751
diff changeset
    63
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    64
--  Proofs and Refutation
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    65
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    66
data Quantifier = ExistentialQ | UniversalQ
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    67
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    68
data EvaluationResult = Eval Bool | UnknownValue Bool deriving Eq
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    69
{-
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    70
instance Show EvaluationResult where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    71
  show (Eval True) = "T"
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    72
  show (Eval False) = "F"
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    73
  show (UnknownValue False) = "U"
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    74
  show (UnknownValue True) = "X"
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    75
-}
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    76
uneval = UnknownValue True
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    77
unknown = UnknownValue False
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    78
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    79
andOp :: EvaluationResult -> EvaluationResult -> EvaluationResult
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    80
andOp (Eval b1) (Eval b2) = Eval (b1 && b2)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    81
andOp (Eval True) (UnknownValue b) = UnknownValue b
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    82
andOp (Eval False) (UnknownValue _) = Eval False
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    83
andOp (UnknownValue b) (Eval True) = UnknownValue b
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    84
andOp (UnknownValue _) (Eval False) = Eval False
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    85
andOp (UnknownValue b1) (UnknownValue b2) = UnknownValue (b1 || b2)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    86
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    87
orOp :: EvaluationResult -> EvaluationResult -> EvaluationResult
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    88
orOp (Eval b1) (Eval b2) = Eval (b1 || b2)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    89
orOp (Eval False) (UnknownValue b) = UnknownValue b
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    90
orOp (Eval True) (UnknownValue _) = Eval True
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    91
orOp (UnknownValue b) (Eval False) = UnknownValue b
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    92
orOp (UnknownValue _) (Eval True) = Eval True
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    93
orOp (UnknownValue b1) (UnknownValue b2) = UnknownValue (b1 && b2)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    94
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    95
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
    96
data Edge = VN Pos Generated_Code.Narrowing_type | CtrB Pos Int
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    97
type Path = [Edge]
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    98
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
    99
data QuantTree = Node EvaluationResult
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
  | VarNode Quantifier EvaluationResult Pos Generated_Code.Narrowing_type QuantTree
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   101
  | CtrBranch Quantifier EvaluationResult Pos [QuantTree]
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   102
{-
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   103
instance Show QuantTree where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   104
  show (Node r) = "Node " ++ show r
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   105
  show (VarNode q r p _ t) = "VarNode " ++ show q ++ " " ++ show r ++ " " ++ show p ++ " " ++ show t
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   106
  show (CtrBranch q r p ts) = "CtrBranch " ++ show q ++ show r ++ show p ++ show ts
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   107
-}
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   108
evalOf :: QuantTree -> EvaluationResult
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   109
evalOf (Node r) = r
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   110
evalOf (VarNode _ r _ _ _) = r
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   111
evalOf (CtrBranch _ r _ _) = r
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   112
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   113
-- Operation find: finds first relevant unevaluated node and returns its path
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   114
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   115
find :: QuantTree -> Path
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   116
find (Node (UnknownValue True)) = []
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   117
find (VarNode _ _ pos ty t) = VN pos ty : (find t)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   118
find (CtrBranch _ _ pos ts) = CtrB pos i : find (ts !! i)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   119
  where  
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   120
    Just i = findIndex (\t -> evalOf t == uneval) ts
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   121
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   122
-- Operation: updateNode ( and simplify)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   123
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   124
{- updates the Node and the stored evaluation results along the upper nodes -}
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   125
updateNode :: Path -> EvaluationResult -> QuantTree -> QuantTree
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   126
updateNode [] v (Node _) = Node v
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   127
updateNode (VN _ _ : es) v (VarNode q r p ty t) = VarNode q (evalOf t') p ty t'
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   128
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   129
    t' = updateNode es v t    
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   130
updateNode (CtrB _ i : es) v (CtrBranch q r pos ts) = CtrBranch q r' pos ts' 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   131
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   132
    (xs, y : ys) = splitAt i ts
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   133
    y' = updateNode es v y
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   134
    ts' = xs ++ (y' : ys)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   135
    r' = foldl (\s t -> oper s (evalOf t)) neutral ts'
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   136
    (neutral, oper) = case q of { UniversalQ -> (Eval True, andOp); ExistentialQ -> (Eval False, orOp)}
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   137
 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   138
-- Operation: refineTree
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   139
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   140
updateTree :: (QuantTree -> QuantTree) -> Path -> QuantTree -> QuantTree
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   141
updateTree f [] t = (f t)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   142
updateTree f (VN _ _ : es) (VarNode q r pos ty t) = VarNode q r pos ty (updateTree f es t)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   143
updateTree f (CtrB _ i : es) (CtrBranch q r pos ts) = CtrBranch q r pos (xs ++ (updateTree f es y : ys))
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   144
   where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   145
     (xs, y : ys) = splitAt i ts
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   146
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   147
refineTree :: [Edge] -> Pos -> QuantTree -> QuantTree
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   148
refineTree es p t = updateTree refine (pathPrefix p es) t
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   149
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   150
    pathPrefix p es = takeWhile (\e -> posOf e /= p) es  
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
   151
    refine (VarNode q r p (Generated_Code.SumOfProd ps) t) =
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   152
      CtrBranch q r p [ foldr (\(i,ty) t -> VarNode q r (p++[i]) ty t) t (zip [0..] ts) | ts <- ps ]
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   153
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   154
-- refute
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   155
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
   156
refute :: ([Generated_Code.Narrowing_term] -> Bool) -> Int -> QuantTree -> IO QuantTree
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   157
refute exec d t = ref t
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   158
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   159
    ref t =
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   160
      let path = find t in
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   161
        do
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   162
          t' <- answer (exec (termListOf [] path)) (\b -> return (updateNode path (Eval b) t))
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   163
            (\p -> return (if length p < d then refineTree path p t else updateNode path unknown t));
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   164
          case evalOf t' of
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   165
            UnknownValue True -> ref t'
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   166
            _ -> return t'
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   167
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
   168
depthCheck :: Int -> Generated_Code.Property -> IO ()
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   169
depthCheck d p = refute (checkOf p) d (treeOf 0 p) >>= (\t -> 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   170
  case evalOf t of
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   171
   Eval False -> putStrLn ("SOME (" ++ show (counterexampleOf (reifysOf p) (exampleOf 0 t)) ++ ")")  
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   172
   _ -> putStrLn ("NONE"))
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   173
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   174
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   175
-- presentation of counterexample
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   176
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   177
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
   178
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
   179
  show (Generated_Code.Typerep c ts) = "Type (\"" ++ c ++ "\", " ++ show ts ++ ")";
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   180
};
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   181
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
   182
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
   183
  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
   184
  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
   185
  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
   186
  show (Generated_Code.Free s ty) = "Free (\"" ++ s ++  "\", " ++ show ty ++ ")";
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   187
};
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   188
{-
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   189
posOf :: Edge -> Pos
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   190
posOf (VN pos _) = pos
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   191
posOf (CtrB pos _) = pos
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   192
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   193
tailPosEdge :: Edge -> Edge
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   194
tailPosEdge (VN pos ty) = VN (tail pos) ty
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   195
tailPosEdge (CtrB pos ts) = CtrB (tail pos) ts
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   196
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   197
termOf :: Pos -> Tree -> (Narrowing_term, Tree)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   198
termOf pos = if Ctr i (termListOf (pos ++ [i]) )
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   199
termOf pos [VN [] ty] = Var pos ty
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   200
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   201
termListOf :: Pos -> [Narrowing_term]
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   202
termListOf pos es = termListOf' 0 es
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   203
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   204
    termListOf' i [] = []
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   205
    termListOf' i (e : es) =
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   206
      let
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   207
        (ts, rs) = List.partition (\e -> head (posOf e) == i) (e : es)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   208
        t = termOf (pos ++ [i]) (map tailPosEdge ts)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   209
      in
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   210
        (t : termListOf' (i + 1) rs) 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   211
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   212
termlist_of :: Pos -> QuantTree -> ([Term], QuantTree)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   213
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   214
termlist_of p' (Node r)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   215
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   216
term_of p' (VarNode _ _ p ty t) = if p == p' then
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   217
    (Some (Var ty), t)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   218
  else
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   219
    (None, t)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   220
term_of p' (CtrBranch q _ p ts) =
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   221
  if p == p' then
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   222
    let
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   223
      i = findindex (\t -> evalOf t == Eval False)        
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   224
    in
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   225
      Ctr i (termlist_of (p ++ [i])  (ts ! i) [])
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   226
  else
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   227
    error ""
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   228
-}
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
   229
termlist_of :: Pos -> ([Generated_Code.Narrowing_term], QuantTree) -> ([Generated_Code.Narrowing_term], QuantTree)
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   230
termlist_of p' (terms, Node b) = (terms, Node b) 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   231
termlist_of p' (terms, VarNode q r p ty t) = if p' == take (length p') p then
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
   232
    termlist_of p' (terms ++ [Generated_Code.Var p ty], t)
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   233
  else
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   234
    (terms, VarNode q r p ty t)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   235
termlist_of p' (terms, CtrBranch q r p ts) = if p' == take (length p') p then
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   236
    let
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   237
      Just i = findIndex (\t -> evalOf t == Eval False) ts
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   238
      (subterms, t') = fixp (\j -> termlist_of (p ++ [j])) 0 ([], ts !! i)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   239
    in
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
   240
      (terms ++ [Generated_Code.Ctr i subterms], t')
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   241
  else
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   242
    (terms, CtrBranch q r p ts)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   243
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   244
    fixp f j s = if length (fst (f j s)) == length (fst s) then s else fixp f (j + 1) (f j s)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   245
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   246
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
   247
alltermlist_of :: Pos -> ([Generated_Code.Narrowing_term], QuantTree) -> [([Generated_Code.Narrowing_term], QuantTree)]
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   248
alltermlist_of p' (terms, Node b) = [(terms, Node b)] 
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   249
alltermlist_of p' (terms, VarNode q r p ty t) = if p' == take (length p') p then
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
   250
    alltermlist_of p' (terms ++ [Generated_Code.Var p ty], t)
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   251
  else
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   252
    [(terms, VarNode q r p ty t)]
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   253
alltermlist_of p' (terms, CtrBranch q r p ts) =
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   254
  if p' == take (length p') p then
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   255
    let
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   256
      its = filter (\(i, t) -> evalOf t == Eval False) (zip [0..] ts)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   257
    in
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   258
      concatMap
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
   259
        (\(i, t) -> map (\(subterms, t') -> (terms ++ [Generated_Code.Ctr i subterms], t'))
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   260
           (fixp (\j -> alltermlist_of (p ++ [j])) 0 ([], t))) its
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   261
  else
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   262
    [(terms, CtrBranch q r p ts)]
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   263
  where
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   264
    fixp f j s = case (f j s) of
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   265
      [s'] -> if length (fst s') == length (fst s) then [s'] else concatMap (fixp f (j + 1)) (f j s)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   266
      _ -> concatMap (fixp f (j + 1)) (f j s)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   267
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
   268
data Example = UnivExample Generated_Code.Narrowing_term Example | ExExample [(Generated_Code.Narrowing_term, Example)] | EmptyExample
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   269
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   270
quantifierOf (VarNode q _ _ _ _) = q
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   271
quantifierOf (CtrBranch q _ _ _) = q
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   272
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   273
exampleOf :: Int -> QuantTree -> Example
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   274
exampleOf _ (Node _) = EmptyExample
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   275
exampleOf p t =
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   276
   case quantifierOf t of
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   277
     UniversalQ ->
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   278
       let
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   279
         ([term], rt) = termlist_of [p] ([], t)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   280
       in UnivExample term (exampleOf (p + 1) rt)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   281
     ExistentialQ ->
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   282
       ExExample (map (\([term], rt) -> (term, exampleOf (p + 1) rt)) (alltermlist_of [p] ([], t)))
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   283
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
   284
data Counterexample = Universal_Counterexample (Generated_Code.Term, Counterexample)
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
   285
  | Existential_Counterexample [(Generated_Code.Term, Counterexample)] | Empty_Assignment
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   286
  
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   287
instance Show Counterexample where {
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   288
show Empty_Assignment = "Narrowing_Generators.Empty_Assignment";
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   289
show (Universal_Counterexample x) = "Narrowing_Generators.Universal_Counterexample" ++ show x;
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   290
show (Existential_Counterexample x) = "Narrowing_Generators.Existential_Counterexample" ++ show x;
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   291
};
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   292
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
   293
counterexampleOf :: [Generated_Code.Narrowing_term -> Generated_Code.Term] -> Example -> Counterexample
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   294
counterexampleOf [] EmptyExample = Empty_Assignment
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   295
counterexampleOf (reify : reifys) (UnivExample t ex) = Universal_Counterexample (reify t, counterexampleOf reifys ex)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   296
counterexampleOf (reify : reifys) (ExExample exs) = Existential_Counterexample (map (\(t, ex) -> (reify t, counterexampleOf reifys ex)) exs)
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   297
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
   298
checkOf :: Generated_Code.Property -> [Generated_Code.Narrowing_term] -> Bool
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
   299
checkOf (Generated_Code.Property b) = (\[] -> b)
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
   300
checkOf (Generated_Code.Universal _ f _) = (\(t : ts) -> checkOf (f t) ts)
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
   301
checkOf (Generated_Code.Existential _ f _) = (\(t : ts) -> checkOf (f t) ts)
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   302
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
   303
dummy = Generated_Code.Var [] (Generated_Code.SumOfProd [[]])
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   304
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
   305
treeOf :: Int -> Generated_Code.Property -> QuantTree
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
   306
treeOf n (Generated_Code.Property _) = Node uneval
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
   307
treeOf n (Generated_Code.Universal ty f _)  = VarNode UniversalQ uneval [n] ty (treeOf (n + 1) (f dummy)) 
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
   308
treeOf n (Generated_Code.Existential ty f _) = VarNode ExistentialQ uneval [n] ty (treeOf (n + 1) (f dummy))
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   309
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
   310
reifysOf :: Generated_Code.Property -> [Generated_Code.Narrowing_term -> Generated_Code.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
   311
reifysOf (Generated_Code.Property _) = []
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
   312
reifysOf (Generated_Code.Universal _ f r)  = (r : (reifysOf (f dummy)))
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
   313
reifysOf (Generated_Code.Existential _ f r) = (r : (reifysOf (f dummy)))
43313
d3c34987863b adding narrowing engine for existentials
bulwahn
parents:
diff changeset
   314