src/Tools/quickcheck.ML
author bulwahn
Tue Feb 23 13:36:15 2010 +0100 (2010-02-23)
changeset 35324 c9f428269b38
parent 35077 c1dac8ace020
child 35378 95d0e3adf38e
permissions -rw-r--r--
adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
     1 (*  Title:      Tools/quickcheck.ML
     2     Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
     3 
     4 Generic counterexample search engine.
     5 *)
     6 
     7 signature QUICKCHECK =
     8 sig
     9   val auto: bool Unsynchronized.ref
    10   val timing : bool Unsynchronized.ref
    11   val test_term: Proof.context -> bool -> string option -> int -> int -> term ->
    12     (string * term) list option
    13   val timed_test_term: Proof.context -> bool -> string option -> int -> int -> term ->
    14     ((string * term) list option * (string * int) list)
    15   val add_generator: string * (Proof.context -> term -> int -> term list option) -> theory -> theory
    16   val setup: theory -> theory
    17   val quickcheck: (string * string) list -> int -> Proof.state -> (string * term) list option
    18 end;
    19 
    20 structure Quickcheck : QUICKCHECK =
    21 struct
    22 
    23 (* preferences *)
    24 
    25 val auto = Unsynchronized.ref false;
    26 
    27 val timing = Unsynchronized.ref false;
    28 
    29 val _ =
    30   ProofGeneralPgip.add_preference Preferences.category_tracing
    31   (setmp_CRITICAL auto true (fn () =>
    32     Preferences.bool_pref auto
    33       "auto-quickcheck"
    34       "Whether to run Quickcheck automatically.") ());
    35 
    36 
    37 (* quickcheck configuration -- default parameters, test generators *)
    38 
    39 datatype test_params = Test_Params of
    40   { size: int, iterations: int, default_type: typ option, no_assms: bool };
    41 
    42 fun dest_test_params (Test_Params { size, iterations, default_type, no_assms }) =
    43   ((size, iterations), (default_type, no_assms));
    44 fun make_test_params ((size, iterations), (default_type, no_assms)) =
    45   Test_Params { size = size, iterations = iterations, default_type = default_type,
    46                 no_assms = no_assms };
    47 fun map_test_params f (Test_Params { size, iterations, default_type, no_assms }) =
    48   make_test_params (f ((size, iterations), (default_type, no_assms)));
    49 fun merge_test_params (Test_Params { size = size1, iterations = iterations1, default_type = default_type1,
    50                                      no_assms = no_assms1 },
    51   Test_Params { size = size2, iterations = iterations2, default_type = default_type2,
    52                 no_assms = no_assms2 }) =
    53   make_test_params ((Int.max (size1, size2), Int.max (iterations1, iterations2)),
    54     (case default_type1 of NONE => default_type2 | _ => default_type1, no_assms1 orelse no_assms2));
    55 
    56 structure Data = Theory_Data
    57 (
    58   type T = (string * (Proof.context -> term -> int -> term list option)) list
    59     * test_params;
    60   val empty = ([], Test_Params { size = 10, iterations = 100, default_type = NONE, no_assms = false });
    61   val extend = I;
    62   fun merge ((generators1, params1), (generators2, params2)) : T =
    63     (AList.merge (op =) (K true) (generators1, generators2),
    64       merge_test_params (params1, params2));
    65 );
    66 
    67 val add_generator = Data.map o apfst o AList.update (op =);
    68 
    69 
    70 (* generating tests *)
    71 
    72 fun mk_tester_select name ctxt =
    73   case AList.lookup (op =) ((fst o Data.get o ProofContext.theory_of) ctxt) name
    74    of NONE => error ("No such quickcheck generator: " ^ name)
    75     | SOME generator => generator ctxt;
    76 
    77 fun mk_testers ctxt t =
    78   (map snd o fst o Data.get o ProofContext.theory_of) ctxt
    79   |> map_filter (fn generator => try (generator ctxt) t);
    80 
    81 fun mk_testers_strict ctxt t =
    82   let
    83     val generators = ((map snd o fst o Data.get o ProofContext.theory_of) ctxt)
    84     val testers = map (fn generator => Exn.capture (generator ctxt) t) generators;
    85   in if forall (is_none o Exn.get_result) testers
    86     then [(Exn.release o snd o split_last) testers]
    87     else map_filter Exn.get_result testers
    88   end;
    89 
    90 
    91 (* testing propositions *)
    92 
    93 fun prep_test_term t =
    94   let
    95     val _ = (null (Term.add_tvars t []) andalso null (Term.add_tfrees t [])) orelse
    96       error "Term to be tested contains type variables";
    97     val _ = null (Term.add_vars t []) orelse
    98       error "Term to be tested contains schematic variables";
    99     val frees = Term.add_frees t [];
   100   in (map fst frees, list_abs_free (frees, t)) end
   101 
   102 fun cpu_time description f =
   103   let
   104     val start = start_timing ()
   105     val result = Exn.capture f ()
   106     val time = Time.toMilliseconds (#cpu (end_timing start))
   107   in (Exn.release result, (description, time)) end
   108 
   109 fun timed_test_term ctxt quiet generator_name size i t =
   110   let
   111     val (names, t') = prep_test_term t;
   112     val (testers, comp_time) = cpu_time "quickcheck compilation"
   113       (fn () => (case generator_name
   114        of NONE => if quiet then mk_testers ctxt t' else mk_testers_strict ctxt t'
   115         | SOME name => [mk_tester_select name ctxt t']));
   116     fun iterate f 0 = NONE
   117       | iterate f j = case f () handle Match => (if quiet then ()
   118              else warning "Exception Match raised during quickcheck"; NONE)
   119           of NONE => iterate f (j - 1) | SOME q => SOME q;
   120     fun with_testers k [] = NONE
   121       | with_testers k (tester :: testers) =
   122           case iterate (fn () => tester (k - 1)) i
   123            of NONE => with_testers k testers
   124             | SOME q => SOME q;
   125     fun with_size k = if k > size then NONE
   126       else (if quiet then () else priority ("Test data size: " ^ string_of_int k);
   127         case with_testers k testers
   128          of NONE => with_size (k + 1) | SOME q => SOME q);
   129     val (result, exec_time) = cpu_time "quickcheck execution"
   130       (fn () => case with_size 1
   131         of NONE => NONE
   132         | SOME ts => SOME (names ~~ ts))
   133   in
   134     (result, [exec_time, comp_time])
   135   end;
   136 
   137 fun test_term ctxt quiet generator_name size i t =
   138   fst (timed_test_term ctxt quiet generator_name size i t)
   139 
   140 fun monomorphic_term thy insts default_T = 
   141   let
   142     fun subst (T as TFree (v, S)) =
   143           let
   144             val T' = AList.lookup (op =) insts v
   145               |> the_default (the_default T default_T)
   146           in if Sign.of_sort thy (T, S) then T'
   147             else error ("Type " ^ Syntax.string_of_typ_global thy T ^
   148               " to be substituted for variable " ^
   149               Syntax.string_of_typ_global thy T ^ "\ndoes not have sort " ^
   150               Syntax.string_of_sort_global thy S)
   151           end
   152       | subst T = T;
   153   in (map_types o map_atyps) subst end;
   154 
   155 fun test_goal quiet generator_name size iterations default_T no_assms insts i assms state =
   156   let
   157     val ctxt = Proof.context_of state;
   158     val thy = Proof.theory_of state;
   159     fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
   160       | strip t = t;
   161     val {goal = st, ...} = Proof.raw_goal state;
   162     val (gi, frees) = Logic.goal_params (prop_of st) i;
   163     val gi' = Logic.list_implies (if no_assms then [] else assms,
   164                                   subst_bounds (frees, strip gi))
   165       |> monomorphic_term thy insts default_T
   166       |> ObjectLogic.atomize_term thy;
   167   in test_term ctxt quiet generator_name size iterations gi' end;
   168 
   169 fun pretty_counterex ctxt NONE = Pretty.str "Quickcheck found no counterexample."
   170   | pretty_counterex ctxt (SOME cex) =
   171       Pretty.chunks (Pretty.str "Quickcheck found a counterexample:\n" ::
   172         map (fn (s, t) =>
   173           Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]) cex);
   174 
   175 
   176 (* automatic testing *)
   177 
   178 fun auto_quickcheck state =
   179   if not (!auto) then
   180     (false, state)
   181   else
   182     let
   183       val ctxt = Proof.context_of state;
   184       val assms = map term_of (Assumption.all_assms_of ctxt);
   185       val Test_Params { size, iterations, default_type, no_assms } =
   186         (snd o Data.get o Proof.theory_of) state;
   187       val res =
   188         try (test_goal true NONE size iterations default_type no_assms [] 1 assms) state;
   189     in
   190       case res of
   191         NONE => (false, state)
   192       | SOME NONE => (false, state)
   193       | SOME cex => (true, Proof.goal_message (K (Pretty.chunks [Pretty.str "",
   194           Pretty.mark Markup.hilite (pretty_counterex ctxt cex)])) state)
   195     end
   196 
   197 val setup = Auto_Counterexample.register_tool ("quickcheck", auto_quickcheck)
   198 
   199 
   200 (* Isar commands *)
   201 
   202 fun read_nat s = case (Library.read_int o Symbol.explode) s
   203  of (k, []) => if k >= 0 then k
   204       else error ("Not a natural number: " ^ s)
   205   | (_, _ :: _) => error ("Not a natural number: " ^ s);
   206 fun read_bool "false" = false
   207   | read_bool "true" = true
   208   | read_bool s = error ("Not a Boolean value: " ^ s)
   209 
   210 fun parse_test_param ctxt ("size", arg) =
   211       (apfst o apfst o K) (read_nat arg)
   212   | parse_test_param ctxt ("iterations", arg) =
   213       (apfst o apsnd o K) (read_nat arg)
   214   | parse_test_param ctxt ("default_type", arg) =
   215       (apsnd o apfst o K o SOME) (ProofContext.read_typ ctxt arg)
   216   | parse_test_param ctxt ("no_assms", arg) =
   217       (apsnd o apsnd o K) (read_bool arg)
   218   | parse_test_param ctxt (name, _) =
   219       error ("Unknown test parameter: " ^ name);
   220 
   221 fun parse_test_param_inst ctxt ("generator", arg) =
   222       (apsnd o apfst o K o SOME) arg
   223   | parse_test_param_inst ctxt (name, arg) =
   224       case try (ProofContext.read_typ ctxt) name
   225        of SOME (TFree (v, _)) => (apsnd o apsnd o AList.update (op =))
   226               (v, ProofContext.read_typ ctxt arg)
   227         | _ => (apfst o parse_test_param ctxt) (name, arg);
   228 
   229 fun quickcheck_params_cmd args thy =
   230   let
   231     val ctxt = ProofContext.init thy;
   232     val f = fold (parse_test_param ctxt) args;
   233   in
   234     thy
   235     |> (Data.map o apsnd o map_test_params) f
   236   end;
   237 
   238 fun quickcheck args i state =
   239   let
   240     val thy = Proof.theory_of state;
   241     val ctxt = Proof.context_of state;
   242     val assms = map term_of (Assumption.all_assms_of ctxt);
   243     val default_params = (dest_test_params o snd o Data.get) thy;
   244     val f = fold (parse_test_param_inst ctxt) args;
   245     val (((size, iterations), (default_type, no_assms)), (generator_name, insts)) =
   246       f (default_params, (NONE, []));
   247   in
   248     test_goal false generator_name size iterations default_type no_assms insts i assms state
   249   end;
   250 
   251 fun quickcheck_cmd args i state =
   252   quickcheck args i (Toplevel.proof_of state)
   253   |> Pretty.writeln o pretty_counterex (Toplevel.context_of state);
   254 
   255 local structure P = OuterParse and K = OuterKeyword in
   256 
   257 val parse_arg = P.name -- (Scan.optional (P.$$$ "=" |-- P.name) "true")
   258 
   259 val parse_args = P.$$$ "[" |-- P.list1 parse_arg --| P.$$$ "]"
   260   || Scan.succeed [];
   261 
   262 val _ = OuterSyntax.command "quickcheck_params" "set parameters for random testing" K.thy_decl
   263   (parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args)));
   264 
   265 val _ = OuterSyntax.improper_command "quickcheck" "try to find counterexample for subgoal" K.diag
   266   (parse_args -- Scan.optional P.nat 1
   267     >> (fn (args, i) => Toplevel.no_timing o Toplevel.keep (quickcheck_cmd args i)));
   268 
   269 end; (*local*)
   270 
   271 end;
   272 
   273 
   274 val auto_quickcheck = Quickcheck.auto;