src/HOL/Tools/quickcheck_generators.ML
author wenzelm
Thu Jul 02 17:34:14 2009 +0200 (2009-07-02)
changeset 31902 862ae16a799d
parent 31868 edd1f30c0477
child 31933 cd6511035315
permissions -rw-r--r--
renamed NamedThmsFun to Named_Thms;
simplified/unified names of instances of Named_Thms;
     1 (* Author: Florian Haftmann, TU Muenchen
     2 
     3 Quickcheck generators for various types.
     4 *)
     5 
     6 signature QUICKCHECK_GENERATORS =
     7 sig
     8   val compile_generator_expr: theory -> term -> int -> term list option
     9   type seed = Random_Engine.seed
    10   val random_fun: typ -> typ -> ('a -> 'a -> bool) -> ('a -> term)
    11     -> (seed -> ('b * (unit -> term)) * seed) -> (seed -> seed * seed)
    12     -> seed -> (('a -> 'b) * (unit -> Term.term)) * seed
    13   val ensure_random_typecopy: string -> theory -> theory
    14   val random_aux_specification: string -> string -> term list -> local_theory -> local_theory
    15   val mk_random_aux_eqs: theory -> Datatype.descr -> (string * sort) list
    16     -> string list -> string list * string list -> typ list * typ list
    17     -> term list * (term * term) list
    18   val ensure_random_datatype: Datatype.config -> string list -> theory -> theory
    19   val eval_ref: (unit -> int -> seed -> term list option * seed) option ref
    20   val setup: theory -> theory
    21 end;
    22 
    23 structure Quickcheck_Generators : QUICKCHECK_GENERATORS =
    24 struct
    25 
    26 (** building and compiling generator expressions **)
    27 
    28 val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
    29 
    30 val target = "Quickcheck";
    31 
    32 fun mk_generator_expr thy prop tys =
    33   let
    34     val bound_max = length tys - 1;
    35     val bounds = map_index (fn (i, ty) =>
    36       (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
    37     val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
    38     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
    39     val check = @{term "If :: bool => term list option => term list option => term list option"}
    40       $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option "} $ terms);
    41     val return = @{term "Pair :: term list option => Random.seed => term list option * Random.seed"};
    42     fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
    43     fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit => term"});
    44     fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
    45       liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
    46     fun mk_split ty = Sign.mk_const thy
    47       (@{const_name split}, [ty, @{typ "unit => term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
    48     fun mk_scomp_split ty t t' =
    49       mk_scomp (mk_termtyp ty) @{typ "term list option"} @{typ Random.seed} t
    50         (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit => term"}, t')));
    51     fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
    52       (Sign.mk_const thy (@{const_name Quickcheck.random}, [ty]) $ Bound i);
    53   in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
    54 
    55 fun compile_generator_expr thy t =
    56   let
    57     val tys = (map snd o fst o strip_abs) t;
    58     val t' = mk_generator_expr thy t tys;
    59     val f = Code_ML.eval (SOME target) ("Quickcheck_Generators.eval_ref", eval_ref)
    60       (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' [];
    61   in f #> Random_Engine.run end;
    62 
    63 
    64 (** typ "'a => 'b" **)
    65 
    66 type seed = Random_Engine.seed;
    67 
    68 fun random_fun T1 T2 eq term_of random random_split seed =
    69   let
    70     val (seed', seed'') = random_split seed;
    71     val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
    72     val fun_upd = Const (@{const_name fun_upd},
    73       (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
    74     fun random_fun' x =
    75       let
    76         val (seed, fun_map, f_t) = ! state;
    77       in case AList.lookup (uncurry eq) fun_map x
    78        of SOME y => y
    79         | NONE => let
    80               val t1 = term_of x;
    81               val ((y, t2), seed') = random seed;
    82               val fun_map' = (x, y) :: fun_map;
    83               val f_t' = fun_upd $ f_t $ t1 $ t2 ();
    84               val _ = state := (seed', fun_map', f_t');
    85             in y end
    86       end;
    87     fun term_fun' () = #3 (! state);
    88   in ((random_fun', term_fun'), seed'') end;
    89 
    90 
    91 (** type copies **)
    92 
    93 fun mk_random_typecopy tyco vs constr typ thy =
    94   let
    95     val Ts = map TFree vs;  
    96     val T = Type (tyco, Ts);
    97     fun mk_termifyT T = HOLogic.mk_prodT (T, @{typ "unit => term"})
    98     val Ttm = mk_termifyT T;
    99     val typtm = mk_termifyT typ;
   100     fun mk_const c Ts = Const (c, Sign.const_instance thy (c, Ts));
   101     fun mk_random T = mk_const @{const_name Quickcheck.random} [T];
   102     val size = @{term "j::code_numeral"};
   103     val v = "x";
   104     val t_v = Free (v, typtm);
   105     val t_constr = mk_const constr Ts;
   106     val lhs = mk_random T $ size;
   107     val rhs = HOLogic.mk_ST [(((mk_random typ) $ size, @{typ Random.seed}), SOME (v, typtm))]
   108       (HOLogic.mk_return Ttm @{typ Random.seed}
   109       (mk_const "Code_Eval.valapp" [typ, T]
   110         $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
   111       @{typ Random.seed} (SOME Ttm, @{typ Random.seed});
   112     val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
   113   in   
   114     thy
   115     |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
   116     |> `(fn lthy => Syntax.check_term lthy eq)
   117     |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
   118     |> snd
   119     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   120   end;
   121 
   122 fun ensure_random_typecopy tyco thy =
   123   let
   124     val SOME { vs = raw_vs, constr, typ = raw_typ, ... } =
   125       Typecopy.get_info thy tyco;
   126     val constrain = curry (Sorts.inter_sort (Sign.classes_of thy));
   127     val typ = map_atyps (fn TFree (v, sort) =>
   128       TFree (v, constrain sort @{sort random})) raw_typ;
   129     val vs' = Term.add_tfreesT typ [];
   130     val vs = map (fn (v, sort) =>
   131       (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
   132     val do_inst = Sign.of_sort thy (typ, @{sort random});
   133   in if do_inst then mk_random_typecopy tyco vs constr typ thy else thy end;
   134 
   135 
   136 (** datatypes **)
   137 
   138 (* definitional scheme for random instances on datatypes *)
   139 
   140 (*FIXME avoid this low-level proving*)
   141 local
   142 
   143 fun dest_ctyp_nth k cT = nth (Thm.dest_ctyp cT) k;
   144 val eq = Thm.cprop_of @{thm random_aux_rec} |> Thm.dest_arg |> Thm.dest_arg |> Thm.dest_arg;
   145 val lhs = eq |> Thm.dest_arg1;
   146 val pt_random_aux = lhs |> Thm.dest_fun;
   147 val ct_k = lhs |> Thm.dest_arg;
   148 val pt_rhs = eq |> Thm.dest_arg |> Thm.dest_fun;
   149 val aT = pt_random_aux |> Thm.ctyp_of_term |> dest_ctyp_nth 1;
   150 
   151 val rew_thms = map mk_meta_eq [@{thm code_numeral_zero_minus_one},
   152   @{thm Suc_code_numeral_minus_one}, @{thm select_weight_cons_zero}, @{thm beyond_zero}];
   153 val rew_ts = map (Logic.dest_equals o Thm.prop_of) rew_thms;
   154 val rew_ss = HOL_ss addsimps rew_thms;
   155 
   156 in
   157 
   158 fun random_aux_primrec eq lthy =
   159   let
   160     val thy = ProofContext.theory_of lthy;
   161     val ((t_random_aux as Free (random_aux, T)) $ (t_k as Free (v, _)), proto_t_rhs) =
   162       (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
   163     val Type (_, [_, iT]) = T;
   164     val icT = Thm.ctyp_of thy iT;
   165     val cert = Thm.cterm_of thy;
   166     val inst = Thm.instantiate_cterm ([(aT, icT)], []);
   167     fun subst_v t' = map_aterms (fn t as Free (w, _) => if v = w then t' else t | t => t);
   168     val t_rhs = lambda t_k proto_t_rhs;
   169     val eqs0 = [subst_v @{term "0::code_numeral"} eq,
   170       subst_v (@{term "Suc_code_numeral"} $ t_k) eq];
   171     val eqs1 = map (Pattern.rewrite_term thy rew_ts []) eqs0;
   172     val ((_, eqs2), lthy') = Primrec.add_primrec_simple
   173       [((Binding.name random_aux, T), NoSyn)] eqs1 lthy;
   174     val cT_random_aux = inst pt_random_aux;
   175     val cT_rhs = inst pt_rhs;
   176     val rule = @{thm random_aux_rec}
   177       |> Drule.instantiate ([(aT, icT)],
   178            [(cT_random_aux, cert t_random_aux), (cT_rhs, cert t_rhs)]);
   179     val tac = ALLGOALS (rtac rule)
   180       THEN ALLGOALS (simp_tac rew_ss)
   181       THEN (ALLGOALS (ProofContext.fact_tac (flat eqs2)))
   182     val simp = SkipProof.prove lthy' [v] [] eq (K tac);
   183   in (simp, lthy') end;
   184 
   185 end;
   186 
   187 fun random_aux_primrec_multi auxname [eq] lthy =
   188       lthy
   189       |> random_aux_primrec eq
   190       |>> (fn simp => [simp])
   191   | random_aux_primrec_multi auxname (eqs as _ :: _ :: _) lthy =
   192       let
   193         val thy = ProofContext.theory_of lthy;
   194         val (lhss, rhss) = map_split (HOLogic.dest_eq o HOLogic.dest_Trueprop) eqs;
   195         val (vs, (arg as Free (v, _)) :: _) = map_split (fn (t1 $ t2) => (t1, t2)) lhss;
   196         val Ts = map fastype_of lhss;
   197         val tupleT = foldr1 HOLogic.mk_prodT Ts;
   198         val aux_lhs = Free ("mutual_" ^ auxname, fastype_of arg --> tupleT) $ arg;
   199         val aux_eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
   200           (aux_lhs, foldr1 HOLogic.mk_prod rhss);
   201         fun mk_proj t [T] = [t]
   202           | mk_proj t (Ts as T :: (Ts' as _ :: _)) =
   203               Const (@{const_name fst}, foldr1 HOLogic.mk_prodT Ts --> T) $ t
   204                 :: mk_proj (Const (@{const_name snd},
   205                   foldr1 HOLogic.mk_prodT Ts --> foldr1 HOLogic.mk_prodT Ts') $ t) Ts';
   206         val projs = mk_proj (aux_lhs) Ts;
   207         val proj_eqs = map2 (fn v => fn proj => (v, lambda arg proj)) vs projs;
   208         val proj_defs = map2 (fn Free (name, _) => fn (_, rhs) =>
   209           ((Binding.name name, NoSyn), (Attrib.empty_binding, rhs))) vs proj_eqs;
   210         val aux_eq' = Pattern.rewrite_term thy proj_eqs [] aux_eq;
   211         fun prove_eqs aux_simp proj_defs lthy = 
   212           let
   213             val proj_simps = map (snd o snd) proj_defs;
   214             fun tac { context = ctxt, prems = _ } =
   215               ALLGOALS (simp_tac (HOL_ss addsimps proj_simps))
   216               THEN ALLGOALS (EqSubst.eqsubst_tac ctxt [0] [aux_simp])
   217               THEN ALLGOALS (simp_tac (HOL_ss addsimps [fst_conv, snd_conv]));
   218           in (map (fn prop => SkipProof.prove lthy [v] [] prop tac) eqs, lthy) end;
   219       in
   220         lthy
   221         |> random_aux_primrec aux_eq'
   222         ||>> fold_map (LocalTheory.define Thm.definitionK) proj_defs
   223         |-> (fn (aux_simp, proj_defs) => prove_eqs aux_simp proj_defs)
   224       end;
   225 
   226 fun random_aux_specification prfx name eqs lthy =
   227   let
   228     val vs = fold Term.add_free_names ((snd o strip_comb o fst o HOLogic.dest_eq
   229       o HOLogic.dest_Trueprop o hd) eqs) [];
   230     fun mk_proto_eq eq =
   231       let
   232         val (head $ t $ u, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
   233       in ((HOLogic.mk_Trueprop o HOLogic.mk_eq) (head, lambda t (lambda u rhs))) end;
   234     val proto_eqs = map mk_proto_eq eqs;
   235     fun prove_simps proto_simps lthy =
   236       let
   237         val ext_simps = map (fn thm => fun_cong OF [fun_cong OF [thm]]) proto_simps;
   238         val tac = ALLGOALS (ProofContext.fact_tac ext_simps);
   239       in (map (fn prop => SkipProof.prove lthy vs [] prop (K tac)) eqs, lthy) end;
   240     val b = Binding.qualify true prfx (Binding.qualify true name (Binding.name "simps"));
   241   in
   242     lthy
   243     |> random_aux_primrec_multi (name ^ prfx) proto_eqs
   244     |-> (fn proto_simps => prove_simps proto_simps)
   245     |-> (fn simps => LocalTheory.note Thm.generatedK ((b,
   246            Code.add_default_eqn_attrib :: map (Attrib.internal o K)
   247           [Simplifier.simp_add, Nitpick_Const_Simps.add, Quickcheck_RecFun_Simps.add]),
   248             simps))
   249     |> snd
   250   end
   251 
   252 
   253 (* constructing random instances on datatypes *)
   254 
   255 val random_auxN = "random_aux";
   256 
   257 fun mk_random_aux_eqs thy descr vs tycos (names, auxnames) (Ts, Us) =
   258   let
   259     val mk_const = curry (Sign.mk_const thy);
   260     val i = @{term "i\<Colon>code_numeral"};
   261     val i1 = @{term "(i\<Colon>code_numeral) - 1"};
   262     val j = @{term "j\<Colon>code_numeral"};
   263     val seed = @{term "s\<Colon>Random.seed"};
   264     val random_auxsN = map (prefix (random_auxN ^ "_")) (names @ auxnames);
   265     fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit \<Rightarrow> term"});
   266     val rTs = Ts @ Us;
   267     fun random_resultT T = @{typ Random.seed}
   268       --> HOLogic.mk_prodT (termifyT T,@{typ Random.seed});
   269     val pTs = map random_resultT rTs;
   270     fun sizeT T = @{typ code_numeral} --> @{typ code_numeral} --> T;
   271     val random_auxT = sizeT o random_resultT;
   272     val random_auxs = map2 (fn s => fn rT => Free (s, random_auxT rT))
   273       random_auxsN rTs;
   274     fun mk_random_call T = (NONE, (HOLogic.mk_random T j, T));
   275     fun mk_random_aux_call fTs (k, _) (tyco, Ts) =
   276       let
   277         val T = Type (tyco, Ts);
   278         fun mk_random_fun_lift [] t = t
   279           | mk_random_fun_lift (fT :: fTs) t =
   280               mk_const @{const_name random_fun_lift} [fTs ---> T, fT] $
   281                 mk_random_fun_lift fTs t;
   282         val t = mk_random_fun_lift fTs (nth random_auxs k $ i1 $ j);
   283         val size = Option.map snd (DatatypeCodegen.find_shortest_path descr k)
   284           |> the_default 0;
   285       in (SOME size, (t, fTs ---> T)) end;
   286     val tss = DatatypeAux.interpret_construction descr vs
   287       { atyp = mk_random_call, dtyp = mk_random_aux_call };
   288     fun mk_consexpr simpleT (c, xs) =
   289       let
   290         val (ks, simple_tTs) = split_list xs;
   291         val T = termifyT simpleT;
   292         val tTs = (map o apsnd) termifyT simple_tTs;
   293         val is_rec = exists is_some ks;
   294         val k = fold (fn NONE => I | SOME k => curry Int.max k) ks 0;
   295         val vs = Name.names Name.context "x" (map snd simple_tTs);
   296         val vs' = (map o apsnd) termifyT vs;
   297         val tc = HOLogic.mk_return T @{typ Random.seed}
   298           (HOLogic.mk_valtermify_app c vs simpleT);
   299         val t = HOLogic.mk_ST (map (fn (t, _) => (t, @{typ Random.seed})) tTs ~~ map SOME vs')
   300           tc @{typ Random.seed} (SOME T, @{typ Random.seed});
   301         val tk = if is_rec
   302           then if k = 0 then i
   303             else @{term "Quickcheck.beyond :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
   304              $ HOLogic.mk_number @{typ code_numeral} k $ i
   305           else @{term "1::code_numeral"}
   306       in (is_rec, HOLogic.mk_prod (tk, t)) end;
   307     fun sort_rec xs =
   308       map_filter (fn (true, t) => SOME t | _ =>  NONE) xs
   309       @ map_filter (fn (false, t) => SOME t | _ =>  NONE) xs;
   310     val gen_exprss = tss
   311       |> (map o apfst) Type
   312       |> map (fn (T, cs) => (T, (sort_rec o map (mk_consexpr T)) cs));
   313     fun mk_select (rT, xs) =
   314       mk_const @{const_name Quickcheck.collapse} [@{typ "Random.seed"}, termifyT rT]
   315       $ (mk_const @{const_name Random.select_weight} [random_resultT rT]
   316         $ HOLogic.mk_list (HOLogic.mk_prodT (@{typ code_numeral}, random_resultT rT)) xs)
   317           $ seed;
   318     val auxs_lhss = map (fn t => t $ i $ j $ seed) random_auxs;
   319     val auxs_rhss = map mk_select gen_exprss;
   320   in (random_auxs, auxs_lhss ~~ auxs_rhss) end;
   321 
   322 fun mk_random_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   323   let
   324     val _ = DatatypeAux.message config "Creating quickcheck generators ...";
   325     val i = @{term "i\<Colon>code_numeral"};
   326     val mk_prop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq;
   327     fun mk_size_arg k = case DatatypeCodegen.find_shortest_path descr k
   328      of SOME (_, l) => if l = 0 then i
   329           else @{term "max :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
   330             $ HOLogic.mk_number @{typ code_numeral} l $ i
   331       | NONE => i;
   332     val (random_auxs, auxs_eqs) = (apsnd o map) mk_prop_eq
   333       (mk_random_aux_eqs thy descr vs tycos (names, auxnames) (Ts, Us));
   334     val random_defs = map_index (fn (k, T) => mk_prop_eq
   335       (HOLogic.mk_random T i, nth random_auxs k $ mk_size_arg k $ i)) Ts;
   336   in
   337     thy
   338     |> TheoryTarget.instantiation (tycos, vs, @{sort random})
   339     |> random_aux_specification prfx random_auxN auxs_eqs
   340     |> `(fn lthy => map (Syntax.check_term lthy) random_defs)
   341     |-> (fn random_defs' => fold_map (fn random_def =>
   342           Specification.definition (NONE, (Attrib.empty_binding,
   343             random_def))) random_defs')
   344     |> snd
   345     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   346   end;
   347 
   348 fun perhaps_constrain thy insts raw_vs =
   349   let
   350     fun meet_random (T, sort) = Sorts.meet_sort (Sign.classes_of thy) 
   351       (Logic.varifyT T, sort);
   352     val vtab = Vartab.empty
   353       |> fold (fn (v, sort) => Vartab.update ((v, 0), sort)) raw_vs
   354       |> fold meet_random insts;
   355   in SOME (fn (v, _) => (v, (the o Vartab.lookup vtab) (v, 0)))
   356   end handle CLASS_ERROR => NONE;
   357 
   358 fun ensure_random_datatype config raw_tycos thy =
   359   let
   360     val pp = Syntax.pp_global thy;
   361     val algebra = Sign.classes_of thy;
   362     val (descr, raw_vs, tycos, prfx, (names, auxnames), raw_TUs) =
   363       Datatype.the_descr thy raw_tycos;
   364     val typrep_vs = (map o apsnd)
   365       (curry (Sorts.inter_sort algebra) @{sort typerep}) raw_vs;
   366     val random_insts = (map (rpair @{sort random}) o flat o maps snd o maps snd)
   367       (DatatypeAux.interpret_construction descr typrep_vs
   368         { atyp = single, dtyp = (K o K o K) [] });
   369     val term_of_insts = (map (rpair @{sort term_of}) o flat o maps snd o maps snd)
   370       (DatatypeAux.interpret_construction descr typrep_vs
   371         { atyp = K [], dtyp = K o K });
   372     val has_inst = exists (fn tyco =>
   373       can (Sorts.mg_domain algebra tyco) @{sort random}) tycos;
   374   in if has_inst then thy
   375     else case perhaps_constrain thy (random_insts @ term_of_insts) typrep_vs
   376      of SOME constrain => mk_random_datatype config descr
   377           (map constrain typrep_vs) tycos prfx (names, auxnames)
   378             ((pairself o map o map_atyps) (fn TFree v => TFree (constrain v)) raw_TUs) thy
   379       | NONE => thy
   380   end;
   381 
   382 
   383 (** setup **)
   384 
   385 val setup = Code_Target.extend_target (target, (Code_ML.target_Eval, K I))
   386   #> Quickcheck.add_generator ("code", compile_generator_expr o ProofContext.theory_of)
   387   #> Typecopy.interpretation ensure_random_typecopy
   388   #> Datatype.interpretation ensure_random_datatype;
   389 
   390 end;