src/HOL/ex/Quickcheck.thy
author haftmann
Mon, 22 Sep 2008 08:00:26 +0200
changeset 28309 c24bc53c815c
parent 28228 7ebe8dc06cbb
child 28315 d3cf88fe77bc
permissions -rw-r--r--
some steps towards generic quickcheck framework
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     1
(*  ID:         $Id$
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     3
*)
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     4
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     5
header {* A simple counterexample generator *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     6
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     7
theory Quickcheck
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 28145
diff changeset
     8
imports Random Code_Eval
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     9
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    10
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    11
subsection {* The @{text random} class *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    12
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    13
class random = rtype +
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    14
  fixes random :: "index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    15
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    16
text {* Type @{typ "'a itself"} *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    17
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    18
instantiation itself :: ("{type, rtype}") random
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    19
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    20
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    21
definition
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 28145
diff changeset
    22
  "random _ = return (TYPE('a), \<lambda>u. Code_Eval.Const (STR ''TYPE'') RTYPE('a))"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    23
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    24
instance ..
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    25
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    26
end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    27
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    28
text {* Datatypes *}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    29
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    30
definition
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    31
  collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    32
  "collapse f = (do g \<leftarrow> f; g done)"
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    33
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    34
ML {*
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    35
structure StateMonad =
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    36
struct
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    37
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    38
fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    39
fun liftT' sT = sT --> sT;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    40
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    41
fun return T sT x = Const (@{const_name return}, T --> liftT T sT) $ x;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    42
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    43
fun scomp T1 T2 sT f g = Const (@{const_name scomp},
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    44
  liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    45
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    46
end;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    47
*}
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    48
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    49
lemma random'_if:
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    50
  fixes random' :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    51
  assumes "random' 0 j = undefined"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    52
    and "\<And>i. random' (Suc_index i) j = rhs2 i"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    53
  shows "random' i j s = (if i = 0 then undefined else rhs2 (i - 1) s)"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    54
  by (cases i rule: index.exhaust) (insert assms, simp_all add: undefined_fun)
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    55
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    56
setup {*
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    57
let
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    58
  exception REC of string;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    59
  fun mk_collapse thy ty = Sign.mk_const thy
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    60
    (@{const_name collapse}, [@{typ seed}, ty]);
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    61
  fun term_ty ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    62
  fun mk_split thy ty ty' = Sign.mk_const thy
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    63
    (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT (term_ty ty') @{typ seed}]);
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
    64
  fun mk_scomp_split thy ty ty' t t' =
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
    65
    StateMonad.scomp (term_ty ty) (term_ty ty') @{typ seed} t
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    66
      (mk_split thy ty ty' $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')))
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    67
  fun mk_cons thy this_ty (c, args) =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    68
    let
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    69
      val tys = map (fst o fst) args;
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    70
      val c_ty = tys ---> this_ty;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    71
      val c = Const (c, tys ---> this_ty);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    72
      val t_indices = map (curry ( op * ) 2) (length tys - 1 downto 0);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    73
      val c_indices = map (curry ( op + ) 1) t_indices;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    74
      val c_t = list_comb (c, map Bound c_indices);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    75
      val t_t = Abs ("", @{typ unit}, Eval.mk_term Free RType.rtype
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    76
        (list_comb (c, map (fn k => Bound (k + 1)) t_indices))
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    77
        |> map_aterms (fn t as Bound _ => t $ @{term "()"} | t => t));
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    78
      val return = StateMonad.return (term_ty this_ty) @{typ seed}
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    79
        (HOLogic.mk_prod (c_t, t_t));
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    80
      val t = fold_rev (fn ((ty, _), random) =>
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
    81
        mk_scomp_split thy ty this_ty random)
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    82
          args return;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    83
      val is_rec = exists (snd o fst) args;
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28084
diff changeset
    84
    in (is_rec, t) end;
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    85
  fun mk_conss thy ty [] = NONE
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    86
    | mk_conss thy ty [(_, t)] = SOME t
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    87
    | mk_conss thy ty ts = SOME (mk_collapse thy (term_ty ty) $
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    88
          (Sign.mk_const thy (@{const_name select}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    89
            HOLogic.mk_list (StateMonad.liftT (term_ty ty) @{typ seed}) (map snd ts)));
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    90
  fun mk_clauses thy ty (tyco, (ts_rec, ts_atom)) = 
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    91
    let
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    92
      val SOME t_atom = mk_conss thy ty ts_atom;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    93
    in case mk_conss thy ty ts_rec
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    94
     of SOME t_rec => mk_collapse thy (term_ty ty) $
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    95
          (Sign.mk_const thy (@{const_name select_default}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    96
             @{term "i\<Colon>index"} $ t_rec $ t_atom)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    97
      | NONE => t_atom
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    98
    end;
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    99
  fun mk_random_eqs thy vs tycos =
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   100
    let
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   101
      val this_ty = Type (hd tycos, map TFree vs);
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   102
      val this_ty' = StateMonad.liftT (term_ty this_ty) @{typ seed};
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   103
      val random_name = NameSpace.base @{const_name random};
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   104
      val random'_name = random_name ^ "_" ^ Class.type_name (hd tycos) ^ "'";
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   105
      fun random ty = Sign.mk_const thy (@{const_name random}, [ty]);
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   106
      val random' = Free (random'_name,
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   107
        @{typ index} --> @{typ index} --> this_ty');
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   108
      fun atom ty = ((ty, false), random ty $ @{term "j\<Colon>index"});
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   109
      fun dtyp tyco = ((this_ty, true), random' $ @{term "i\<Colon>index"} $ @{term "j\<Colon>index"});
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   110
      fun rtyp tyco tys = raise REC
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   111
        ("Will not generate random elements for mutual recursive type " ^ quote (hd tycos));
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   112
      val rhss = DatatypePackage.construction_interpretation thy
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   113
            { atom = atom, dtyp = dtyp, rtyp = rtyp } vs tycos
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   114
        |> (map o apsnd o map) (mk_cons thy this_ty) 
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   115
        |> (map o apsnd) (List.partition fst)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   116
        |> map (mk_clauses thy this_ty)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   117
      val eqss = map ((apsnd o map) (HOLogic.mk_Trueprop o HOLogic.mk_eq) o (fn rhs => ((this_ty, random'), [
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   118
          (random' $ @{term "0\<Colon>index"} $ @{term "j\<Colon>index"}, Const (@{const_name undefined}, this_ty')),
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   119
          (random' $ @{term "Suc_index i"} $ @{term "j\<Colon>index"}, rhs)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   120
        ]))) rhss;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   121
    in eqss end;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   122
  fun random_inst [tyco] thy =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   123
        let
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   124
          val (raw_vs, _) = DatatypePackage.the_datatype_spec thy tyco;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   125
          val vs = (map o apsnd)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   126
            (curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort random}) raw_vs;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   127
          val { descr, index, ... } = DatatypePackage.the_datatype thy tyco;
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   128
          val ((this_ty, random'), eqs') = singleton (mk_random_eqs thy vs) tyco;
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   129
          val eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   130
            (Sign.mk_const thy (@{const_name random}, [this_ty]) $ @{term "i\<Colon>index"},
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   131
               random' $ @{term "i\<Colon>index"} $ @{term "i\<Colon>index"})
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   132
          val del_func = Attrib.internal (fn _ => Thm.declaration_attribute
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   133
            (fn thm => Context.mapping (Code.del_func thm) I));
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   134
          fun add_code simps lthy =
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   135
            let
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   136
              val thy = ProofContext.theory_of lthy;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   137
              val thm = @{thm random'_if}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   138
                |> Drule.instantiate' [SOME (Thm.ctyp_of thy this_ty)] [SOME (Thm.cterm_of thy random')]
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   139
                |> (fn thm => thm OF simps)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   140
                |> singleton (ProofContext.export lthy (ProofContext.init thy))
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   141
            in
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   142
              lthy
27865
27a8ad9612a3 moved basic thm operations from structure PureThy to Thm (cf. more_thm.ML);
wenzelm
parents: 27683
diff changeset
   143
              |> LocalTheory.theory (PureThy.add_thm ((fst (dest_Free random') ^ "_code", thm), [Thm.kind_internal])
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   144
                   #-> Code.add_func)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   145
            end;
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   146
        in
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   147
          thy
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   148
          |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   149
          |> PrimrecPackage.add_primrec
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 28054
diff changeset
   150
               [(Name.binding (fst (dest_Free random')), SOME (snd (dest_Free random')), NoSyn)]
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 28054
diff changeset
   151
                 (map (fn eq => ((Name.no_binding, [del_func]), eq)) eqs')
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   152
          |-> add_code
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   153
          |> `(fn lthy => Syntax.check_term lthy eq)
28084
a05ca48ef263 type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents: 28083
diff changeset
   154
          |-> (fn eq => Specification.definition (NONE, (Attrib.no_binding, eq)))
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   155
          |> snd
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   156
          |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   157
          |> LocalTheory.exit
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   158
          |> ProofContext.theory_of
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   159
        end
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   160
    | random_inst tycos thy = raise REC
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   161
        ("Will not generate random elements for mutual recursive type(s) " ^ commas (map quote tycos));
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   162
  fun add_random_inst tycos thy = random_inst tycos thy
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   163
     handle REC msg => (warning msg; thy);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   164
in DatatypePackage.interpretation add_random_inst end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   165
*}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   166
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   167
text {* Type @{typ int} *}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   168
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   169
instantiation int :: random
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   170
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   171
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   172
definition
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   173
  "random n = (do
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   174
     (b, _) \<leftarrow> random n;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   175
     (m, t) \<leftarrow> random n;
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 28145
diff changeset
   176
     return (if b then (int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''Int.int'') RTYPE(nat \<Rightarrow> int)) (t ()))
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 28145
diff changeset
   177
       else (- int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''HOL.uminus_class.uminus'') RTYPE(int \<Rightarrow> int))
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 28145
diff changeset
   178
         (Code_Eval.App (Code_Eval.Const (STR ''Int.int'') RTYPE(nat \<Rightarrow> int)) (t ()))))
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   179
   done)"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   180
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   181
instance ..
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   182
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   183
end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   184
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   185
text {* Type @{typ "'a \<Rightarrow> 'b"} *}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   186
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   187
ML {*
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   188
structure Random_Engine =
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   189
struct
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   190
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   191
open Random_Engine;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   192
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   193
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   194
    (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   195
    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   196
    (seed : Random_Engine.seed) =
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   197
  let
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   198
    val (seed', seed'') = random_split seed;
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   199
    val state = ref (seed', [], Const (@{const_name arbitrary}, T1 --> T2));
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   200
    val fun_upd = Const (@{const_name fun_upd},
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   201
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   202
    fun random_fun' x =
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   203
      let
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   204
        val (seed, fun_map, f_t) = ! state;
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   205
      in case AList.lookup (uncurry eq) fun_map x
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   206
       of SOME y => y
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   207
        | NONE => let
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   208
              val t1 = term_of x;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   209
              val ((y, t2), seed') = random seed;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   210
              val fun_map' = (x, y) :: fun_map;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   211
              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   212
              val _ = state := (seed', fun_map', f_t');
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   213
            in y end
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   214
      end;
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   215
    fun term_fun' () = #3 (! state);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   216
  in ((random_fun', term_fun'), seed'') end;
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   217
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   218
end
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   219
*}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   220
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   221
axiomatization
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   222
  random_fun_aux :: "rtype \<Rightarrow> rtype \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   223
    \<Rightarrow> (seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> seed) \<Rightarrow> (seed \<Rightarrow> seed \<times> seed)
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   224
    \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed"
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   225
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   226
code_const random_fun_aux (SML "Random'_Engine.random'_fun")
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   227
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   228
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   229
begin
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   230
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   231
definition random_fun :: "index \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" where
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 28145
diff changeset
   232
  "random n = random_fun_aux RTYPE('a) RTYPE('b) (op =) Code_Eval.term_of (random n) split_seed"
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   233
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   234
instance ..
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   235
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   236
end
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   237
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   238
code_reserved SML Random_Engine
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   239
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   240
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   241
subsection {* Quickcheck generator *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   242
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   243
ML {*
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   244
structure Quickcheck =
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   245
struct
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   246
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   247
open Quickcheck;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   248
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   249
val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   250
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   251
fun mk_generator_expr thy prop tys =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   252
  let
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   253
    val bound_max = length tys - 1;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   254
    val bounds = map_index (fn (i, ty) =>
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   255
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   256
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   257
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   258
    val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   259
      $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   260
    val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"};
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   261
    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   262
    fun mk_split ty = Sign.mk_const thy
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   263
      (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT @{typ "term list option"} @{typ seed}]);
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
   264
    fun mk_scomp_split ty t t' =
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
   265
      StateMonad.scomp (mk_termtyp ty) @{typ "term list option"} @{typ seed} t (*FIXME*)
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   266
        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
   267
    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   268
      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   269
    val t = fold_rev mk_bindclause bounds (return $ check);
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   270
  in Abs ("n", @{typ index}, t) end;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   271
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   272
fun compile_generator_expr thy t =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   273
  let
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   274
    val tys = (map snd o fst o strip_abs) t;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   275
    val t' = mk_generator_expr thy t tys;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   276
    val f = Code_ML.eval_term ("Quickcheck.eval_ref", eval_ref) thy t' [];
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   277
  in f #> Random_Engine.run #> (Option.map o map) (Code.postprocess_term thy) end;
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   278
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   279
end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   280
*}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   281
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   282
setup {*
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   283
  Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   284
*}
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   285
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   286
subsection {* Examples *}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   287
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   288
(*lemma
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   289
  fixes n m :: nat
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   290
  shows "n + m \<le> n * m"
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   291
;test_goal [code];
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   292
oops*)
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   293
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   294
ML {* val f = Quickcheck.compile_generator_expr @{theory}
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   295
  @{term "\<lambda>(n::nat) (m::nat) (q::nat). n = m + q + 1"} *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   296
26944
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   297
ML {* f 5 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   298
ML {* f 5 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   299
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   300
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   301
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   302
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   303
ML {* f 25 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   304
ML {* f 1 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   305
ML {* f 1 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   306
ML {* f 2 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   307
ML {* f 2 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   308
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   309
ML {* val f = Quickcheck.compile_generator_expr @{theory}
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   310
  @{term "\<lambda>(n::int) (m::int) (q::int). n = m + q + 1"} *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   311
26944
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   312
ML {* f 5 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   313
ML {* f 5 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   314
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   315
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   316
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   317
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   318
ML {* f 25 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   319
ML {* f 1 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   320
ML {* f 1 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   321
ML {* f 2 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   322
ML {* f 2 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   323
ML {* f 3 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   324
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   325
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   326
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   327
ML {* val f = Quickcheck.compile_generator_expr @{theory}
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   328
  @{term "\<lambda>(xs\<Colon>int list) ys. rev (xs @ ys) = rev xs @ rev ys"} *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   329
26944
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   330
ML {* f 15 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   331
ML {* f 5 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   332
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   333
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   334
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   335
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   336
ML {* f 25 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   337
ML {* f 1 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   338
ML {* f 1 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   339
ML {* f 2 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   340
ML {* f 2 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   341
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   342
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   343
ML {* f 5 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   344
ML {* f 8 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   345
ML {* f 8 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   346
ML {* f 8 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   347
ML {* f 88 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   348
26944
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   349
ML {* f 1 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   350
ML {* f 2 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   351
ML {* f 3 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   352
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   353
ML {* f 5 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   354
ML {* f 6 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   355
ML {* f 10 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   356
ML {* f 10 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   357
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   358
ML {* val f = Quickcheck.compile_generator_expr @{theory}
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   359
  @{term "\<lambda>(s \<Colon> string). s \<noteq> rev s"} *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   360
26944
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   361
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   362
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   363
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   364
ML {* f 4 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   365
ML {* f 10 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   366
ML {* f 10 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   367
ML {* f 10 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   368
ML {* f 10 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   369
ML {* f 10 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   370
ML {* f 8 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   371
ML {* f 8 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   372
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   373
ML {* val f = Quickcheck.compile_generator_expr @{theory}
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   374
  @{term "\<lambda>f k. int (f k) = k"} *}
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   375
26944
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   376
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   377
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   378
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   379
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   380
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
1fe801f9cfc9 Syntax.string_of_term with proper context;
wenzelm
parents: 26829
diff changeset
   381
ML {* f 20 |> (Option.map o map) (Syntax.string_of_term @{context}) *}
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   382
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   383
end