src/HOL/Smallcheck.thy
author bulwahn
Fri Dec 03 08:40:46 2010 +0100 (2010-12-03)
changeset 40899 ef6fde932f4c
parent 40639 f1f0e6adca0a
child 40914 0c071c5202b5
permissions -rw-r--r--
improving readability of Smallcheck theory; adding constant orelse to improve performance of the function package
bulwahn@40420
     1
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@40420
     2
bulwahn@40420
     3
header {* Another simple counterexample generator *}
bulwahn@40420
     4
bulwahn@40420
     5
theory Smallcheck
bulwahn@40420
     6
imports Quickcheck
bulwahn@40420
     7
uses ("Tools/smallvalue_generators.ML")
bulwahn@40420
     8
begin
bulwahn@40420
     9
bulwahn@40420
    10
huffman@40620
    11
subsection {* small value generator type classes *}
bulwahn@40420
    12
bulwahn@40420
    13
class small = term_of +
bulwahn@40420
    14
fixes small :: "('a \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
bulwahn@40420
    15
bulwahn@40420
    16
instantiation unit :: small
bulwahn@40420
    17
begin
bulwahn@40420
    18
bulwahn@40639
    19
definition "small f d = f ()"
bulwahn@40420
    20
bulwahn@40420
    21
instance ..
bulwahn@40420
    22
bulwahn@40420
    23
end
bulwahn@40420
    24
bulwahn@40420
    25
instantiation int :: small
bulwahn@40420
    26
begin
bulwahn@40420
    27
bulwahn@40420
    28
function small' :: "(int => term list option) => int => int => term list option"
bulwahn@40420
    29
where "small' f d i = (if d < i then None else (case f i of Some t => Some t | None => small' f d (i + 1)))"
bulwahn@40420
    30
by pat_completeness auto
bulwahn@40420
    31
bulwahn@40420
    32
termination 
bulwahn@40420
    33
  by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@40420
    34
bulwahn@40420
    35
definition "small f d = small' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@40420
    36
bulwahn@40420
    37
instance ..
bulwahn@40420
    38
bulwahn@40420
    39
end
bulwahn@40420
    40
bulwahn@40420
    41
instantiation prod :: (small, small) small
bulwahn@40420
    42
begin
bulwahn@40420
    43
bulwahn@40420
    44
definition
bulwahn@40420
    45
  "small f d = small (%x. small (%y. f (x, y)) d) d"
bulwahn@40420
    46
bulwahn@40420
    47
instance ..
bulwahn@40420
    48
bulwahn@40420
    49
end
bulwahn@40420
    50
bulwahn@40899
    51
subsection {* full small value generator type classes *}
bulwahn@40639
    52
bulwahn@40639
    53
class full_small = term_of +
bulwahn@40639
    54
fixes full_small :: "('a * (unit => term) \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
bulwahn@40639
    55
bulwahn@40639
    56
instantiation unit :: full_small
bulwahn@40639
    57
begin
bulwahn@40639
    58
bulwahn@40639
    59
definition "full_small f d = f (Code_Evaluation.valtermify ())"
bulwahn@40639
    60
bulwahn@40639
    61
instance ..
bulwahn@40639
    62
bulwahn@40639
    63
end
bulwahn@40639
    64
bulwahn@40639
    65
instantiation int :: full_small
bulwahn@40639
    66
begin
bulwahn@40639
    67
bulwahn@40639
    68
function full_small' :: "(int * (unit => term) => term list option) => int => int => term list option"
bulwahn@40639
    69
  where "full_small' f d i = (if d < i then None else (case f (i, %_. Code_Evaluation.term_of i) of Some t => Some t | None => full_small' f d (i + 1)))"
bulwahn@40639
    70
by pat_completeness auto
bulwahn@40639
    71
bulwahn@40639
    72
termination 
bulwahn@40639
    73
  by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@40639
    74
bulwahn@40639
    75
definition "full_small f d = full_small' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@40639
    76
bulwahn@40639
    77
instance ..
bulwahn@40639
    78
bulwahn@40639
    79
end
bulwahn@40639
    80
bulwahn@40639
    81
instantiation prod :: (full_small, full_small) full_small
bulwahn@40639
    82
begin
bulwahn@40899
    83
bulwahn@40639
    84
definition
bulwahn@40639
    85
  "full_small f d = full_small (%(x, t1). full_small (%(y, t2). f ((x, y),
bulwahn@40639
    86
    %u. Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.term_of (Pair :: 'a => 'b => ('a * 'b))) (t1 ())) (t2 ()))) d) d"
bulwahn@40639
    87
bulwahn@40639
    88
instance ..
bulwahn@40639
    89
bulwahn@40639
    90
end
bulwahn@40639
    91
bulwahn@40639
    92
instantiation "fun" :: ("{equal, full_small}", full_small) full_small
bulwahn@40639
    93
begin
bulwahn@40639
    94
bulwahn@40639
    95
fun full_small_fun' :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => code_numeral => term list option"
bulwahn@40639
    96
where
bulwahn@40639
    97
  "full_small_fun' f i d = (if i > 1 then
bulwahn@40639
    98
    full_small (%(a, at). full_small (%(b, bt).
bulwahn@40639
    99
      full_small_fun' (%(g, gt). f (g(a := b),
bulwahn@40899
   100
        (%_. let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@40899
   101
                 T2 = (Typerep.typerep (TYPE('b)))
bulwahn@40899
   102
             in
bulwahn@40899
   103
               Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
bulwahn@40899
   104
                 (Code_Evaluation.Const (STR ''Fun.fun_upd'')
bulwahn@40899
   105
                    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
bulwahn@40899
   106
                       Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
bulwahn@40899
   107
               (gt ())) (at ())) (bt ())))) (i - 1) d) d) d
bulwahn@40899
   108
  else (if i > 0 then
bulwahn@40899
   109
    full_small (%(b, t). f (%_. b, %_. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a)) (t ()))) d else None))"
bulwahn@40639
   110
bulwahn@40639
   111
definition full_small_fun :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => term list option"
bulwahn@40639
   112
where
bulwahn@40639
   113
  "full_small_fun f d = full_small_fun' f d d" 
bulwahn@40639
   114
bulwahn@40639
   115
bulwahn@40639
   116
instance ..
bulwahn@40639
   117
bulwahn@40639
   118
end
bulwahn@40639
   119
huffman@40620
   120
subsection {* Defining combinators for any first-order data type *}
bulwahn@40420
   121
bulwahn@40899
   122
definition orelse :: "'a option => 'a option => 'a option"
bulwahn@40899
   123
where
bulwahn@40899
   124
  [code_unfold]: "orelse x y = (case x of Some x' => Some x' | None => y)"
bulwahn@40899
   125
bulwahn@40899
   126
bulwahn@40420
   127
definition catch_match :: "term list option => term list option => term list option"
bulwahn@40420
   128
where
bulwahn@40420
   129
  [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
bulwahn@40420
   130
bulwahn@40420
   131
code_const catch_match 
bulwahn@40420
   132
  (SML "(_) handle Match => _")
bulwahn@40420
   133
bulwahn@40420
   134
use "Tools/smallvalue_generators.ML"
bulwahn@40420
   135
bulwahn@40639
   136
(* We do not activate smallcheck yet.
bulwahn@40420
   137
setup {* Smallvalue_Generators.setup *}
bulwahn@40420
   138
*)
bulwahn@40420
   139
bulwahn@40899
   140
hide_fact orelse_def catch_match_def
bulwahn@40899
   141
hide_const (open) orelse catch_match
bulwahn@40420
   142
bulwahn@40420
   143
end