src/Tools/quickcheck.ML
changeset 28952 15a4b2cf8c34
parent 28336 a8edf4c69a79
child 29266 4a478f9d2847
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Tools/quickcheck.ML	Wed Dec 03 15:58:44 2008 +0100
     1.3 @@ -0,0 +1,235 @@
     1.4 +(*  Title:      Pure/Tools/quickcheck.ML
     1.5 +    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
     1.6 +
     1.7 +Generic counterexample search engine.
     1.8 +*)
     1.9 +
    1.10 +signature QUICKCHECK =
    1.11 +sig
    1.12 +  val test_term: Proof.context -> bool -> string option -> int -> int -> term -> (string * term) list option;
    1.13 +  val add_generator: string * (Proof.context -> term -> int -> term list option) -> theory -> theory
    1.14 +  val auto: bool ref
    1.15 +  val auto_time_limit: int ref
    1.16 +end;
    1.17 +
    1.18 +structure Quickcheck : QUICKCHECK =
    1.19 +struct
    1.20 +
    1.21 +(* quickcheck configuration -- default parameters, test generators *)
    1.22 +
    1.23 +datatype test_params = Test_Params of
    1.24 +  { size: int, iterations: int, default_type: typ option };
    1.25 +
    1.26 +fun dest_test_params (Test_Params { size, iterations, default_type}) =
    1.27 +  ((size, iterations), default_type);
    1.28 +fun mk_test_params ((size, iterations), default_type) =
    1.29 +  Test_Params { size = size, iterations = iterations, default_type = default_type };
    1.30 +fun map_test_params f (Test_Params { size, iterations, default_type}) =
    1.31 +  mk_test_params (f ((size, iterations), default_type));
    1.32 +fun merge_test_params (Test_Params {size = size1, iterations = iterations1, default_type = default_type1},
    1.33 +  Test_Params {size = size2, iterations = iterations2, default_type = default_type2}) =
    1.34 +  mk_test_params ((Int.max (size1, size2), Int.max (iterations1, iterations2)),
    1.35 +    case default_type1 of NONE => default_type2 | _ => default_type1);
    1.36 +
    1.37 +structure Data = TheoryDataFun(
    1.38 +  type T = (string * (Proof.context -> term -> int -> term list option)) list
    1.39 +    * test_params;
    1.40 +  val empty = ([], Test_Params { size = 10, iterations = 100, default_type = NONE });
    1.41 +  val copy = I;
    1.42 +  val extend = I;
    1.43 +  fun merge pp ((generators1, params1), (generators2, params2)) =
    1.44 +    (AList.merge (op = : string * string -> bool) (K true) (generators1, generators2),
    1.45 +      merge_test_params (params1, params2));
    1.46 +)
    1.47 +
    1.48 +val add_generator = Data.map o apfst o AList.update (op =);
    1.49 +
    1.50 +
    1.51 +(* generating tests *)
    1.52 +
    1.53 +fun mk_tester_select name ctxt =
    1.54 +  case AList.lookup (op =) ((fst o Data.get o ProofContext.theory_of) ctxt) name
    1.55 +   of NONE => error ("No such quickcheck generator: " ^ name)
    1.56 +    | SOME generator => generator ctxt;
    1.57 +
    1.58 +fun mk_testers ctxt t =
    1.59 +  (map snd o fst o Data.get o ProofContext.theory_of) ctxt
    1.60 +  |> map_filter (fn generator => try (generator ctxt) t);
    1.61 +
    1.62 +fun mk_testers_strict ctxt t =
    1.63 +  let
    1.64 +    val generators = ((map snd o fst o Data.get o ProofContext.theory_of) ctxt)
    1.65 +    val testers = map (fn generator => Exn.capture (generator ctxt) t) generators;
    1.66 +  in if forall (is_none o Exn.get_result) testers
    1.67 +    then [(Exn.release o snd o split_last) testers]
    1.68 +    else map_filter Exn.get_result testers
    1.69 +  end;
    1.70 +
    1.71 +
    1.72 +(* testing propositions *)
    1.73 +
    1.74 +fun prep_test_term t =
    1.75 +  let
    1.76 +    val _ = (null (term_tvars t) andalso null (term_tfrees t)) orelse
    1.77 +      error "Term to be tested contains type variables";
    1.78 +    val _ = null (term_vars t) orelse
    1.79 +      error "Term to be tested contains schematic variables";
    1.80 +    val frees = map dest_Free (term_frees t);
    1.81 +  in (map fst frees, list_abs_free (frees, t)) end
    1.82 +
    1.83 +fun test_term ctxt quiet generator_name size i t =
    1.84 +  let
    1.85 +    val (names, t') = prep_test_term t;
    1.86 +    val testers = case generator_name
    1.87 +     of NONE => if quiet then mk_testers ctxt t' else mk_testers_strict ctxt t'
    1.88 +      | SOME name => [mk_tester_select name ctxt t'];
    1.89 +    fun iterate f 0 = NONE
    1.90 +      | iterate f k = case f () handle Match => (if quiet then ()
    1.91 +             else warning "Exception Match raised during quickcheck"; NONE)
    1.92 +          of NONE => iterate f (k - 1) | SOME q => SOME q;
    1.93 +    fun with_testers k [] = NONE
    1.94 +      | with_testers k (tester :: testers) =
    1.95 +          case iterate (fn () => tester k) i
    1.96 +           of NONE => with_testers k testers
    1.97 +            | SOME q => SOME q;
    1.98 +    fun with_size k = if k > size then NONE
    1.99 +      else (if quiet then () else priority ("Test data size: " ^ string_of_int k);
   1.100 +        case with_testers k testers
   1.101 +         of NONE => with_size (k + 1) | SOME q => SOME q);
   1.102 +  in case with_size 1
   1.103 +   of NONE => NONE
   1.104 +    | SOME ts => SOME (names ~~ ts)
   1.105 +  end;
   1.106 +
   1.107 +fun monomorphic_term thy insts default_T = 
   1.108 +  let
   1.109 +    fun subst (T as TFree (v, S)) =
   1.110 +          let
   1.111 +            val T' = AList.lookup (op =) insts v
   1.112 +              |> the_default (the_default T default_T)
   1.113 +          in if Sign.of_sort thy (T, S) then T'
   1.114 +            else error ("Type " ^ Syntax.string_of_typ_global thy T ^
   1.115 +              " to be substituted for variable " ^
   1.116 +              Syntax.string_of_typ_global thy T ^ "\ndoes not have sort " ^
   1.117 +              Syntax.string_of_sort_global thy S)
   1.118 +          end
   1.119 +      | subst T = T;
   1.120 +  in (map_types o map_atyps) subst end;
   1.121 +
   1.122 +fun test_goal quiet generator_name size iterations default_T insts i assms state =
   1.123 +  let
   1.124 +    val ctxt = Proof.context_of state;
   1.125 +    val thy = Proof.theory_of state;
   1.126 +    fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
   1.127 +      | strip t = t;
   1.128 +    val (_, (_, st)) = Proof.get_goal state;
   1.129 +    val (gi, frees) = Logic.goal_params (prop_of st) i;
   1.130 +    val gi' = Logic.list_implies (assms, subst_bounds (frees, strip gi))
   1.131 +      |> monomorphic_term thy insts default_T
   1.132 +      |> ObjectLogic.atomize_term thy;
   1.133 +  in test_term ctxt quiet generator_name size iterations gi' end;
   1.134 +
   1.135 +fun pretty_counterex ctxt NONE = Pretty.str "No counterexamples found."
   1.136 +  | pretty_counterex ctxt (SOME cex) =
   1.137 +      Pretty.chunks (Pretty.str "Counterexample found:\n" ::
   1.138 +        map (fn (s, t) =>
   1.139 +          Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]) cex);
   1.140 +
   1.141 +
   1.142 +(* automatic testing *)
   1.143 +
   1.144 +val auto = ref false;
   1.145 +val auto_time_limit = ref 5000;
   1.146 +
   1.147 +fun test_goal_auto int state =
   1.148 +  let
   1.149 +    val ctxt = Proof.context_of state;
   1.150 +    val assms = map term_of (Assumption.assms_of ctxt);
   1.151 +    val Test_Params { size, iterations, default_type } =
   1.152 +      (snd o Data.get o Proof.theory_of) state;
   1.153 +    fun test () =
   1.154 +      let
   1.155 +        val res = TimeLimit.timeLimit (Time.fromMilliseconds (!auto_time_limit))
   1.156 +          (try (test_goal true NONE size iterations default_type [] 1 assms)) state;
   1.157 +      in
   1.158 +        case res of
   1.159 +          NONE => state
   1.160 +        | SOME NONE => state
   1.161 +        | SOME cex => Proof.goal_message (fn () => Pretty.chunks [Pretty.str "",
   1.162 +            Pretty.mark Markup.hilite (pretty_counterex ctxt cex)]) state
   1.163 +      end handle TimeLimit.TimeOut => (warning "Auto quickcheck: timeout."; state);
   1.164 +  in
   1.165 +    if int andalso !auto andalso not (!Toplevel.quiet)
   1.166 +    then test ()
   1.167 +    else state
   1.168 +  end;
   1.169 +
   1.170 +val _ = Context.>> (Specification.add_theorem_hook test_goal_auto);
   1.171 +
   1.172 +
   1.173 +(* Isar interfaces *)
   1.174 +
   1.175 +fun read_nat s = case (Library.read_int o Symbol.explode) s
   1.176 + of (k, []) => if k >= 0 then k
   1.177 +      else error ("Not a natural number: " ^ s)
   1.178 +  | (_, _ :: _) => error ("Not a natural number: " ^ s);
   1.179 +
   1.180 +fun parse_test_param ctxt ("size", arg) =
   1.181 +      (apfst o apfst o K) (read_nat arg)
   1.182 +  | parse_test_param ctxt ("iterations", arg) =
   1.183 +      (apfst o apsnd o K) (read_nat arg)
   1.184 +  | parse_test_param ctxt ("default_type", arg) =
   1.185 +      (apsnd o K o SOME) (ProofContext.read_typ ctxt arg)
   1.186 +  | parse_test_param ctxt (name, _) =
   1.187 +      error ("Bad test parameter: " ^ name);
   1.188 +
   1.189 +fun parse_test_param_inst ctxt ("generator", arg) =
   1.190 +      (apsnd o apfst o K o SOME) arg
   1.191 +  | parse_test_param_inst ctxt (name, arg) =
   1.192 +      case try (ProofContext.read_typ ctxt) name
   1.193 +       of SOME (TFree (v, _)) => (apsnd o apsnd o AList.update (op =))
   1.194 +              (v, ProofContext.read_typ ctxt arg)
   1.195 +        | _ => (apfst o parse_test_param ctxt) (name, arg);
   1.196 +
   1.197 +fun quickcheck_params_cmd args thy =
   1.198 +  let
   1.199 +    val ctxt = ProofContext.init thy;
   1.200 +    val f = fold (parse_test_param ctxt) args;
   1.201 +  in
   1.202 +    thy
   1.203 +    |> (Data.map o apsnd o map_test_params) f
   1.204 +  end;
   1.205 +
   1.206 +fun quickcheck_cmd args i state =
   1.207 +  let
   1.208 +    val prf = Toplevel.proof_of state;
   1.209 +    val thy = Toplevel.theory_of state;
   1.210 +    val ctxt = Toplevel.context_of state;
   1.211 +    val default_params = (dest_test_params o snd o Data.get) thy;
   1.212 +    val f = fold (parse_test_param_inst ctxt) args;
   1.213 +    val (((size, iterations), default_type), (generator_name, insts)) =
   1.214 +      f (default_params, (NONE, []));
   1.215 +    val counterex = test_goal false generator_name size iterations
   1.216 +      default_type insts i [] prf;
   1.217 +  in (Pretty.writeln o pretty_counterex ctxt) counterex end;
   1.218 +
   1.219 +local structure P = OuterParse and K = OuterKeyword in
   1.220 +
   1.221 +val parse_arg = P.name --| P.$$$ "=" -- P.name;
   1.222 +val parse_args = P.$$$ "[" |-- P.list1 parse_arg --| P.$$$ "]"
   1.223 +  || Scan.succeed [];
   1.224 +
   1.225 +val _ = OuterSyntax.command "quickcheck_params" "set parameters for random testing" K.thy_decl
   1.226 +  (parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args)));
   1.227 +
   1.228 +val _ = OuterSyntax.improper_command "quickcheck" "try to find counterexample for subgoal" K.diag
   1.229 +  (parse_args -- Scan.optional P.nat 1
   1.230 +    >> (fn (args, i) => Toplevel.no_timing o Toplevel.keep (quickcheck_cmd args i)));
   1.231 +
   1.232 +end; (*local*)
   1.233 +
   1.234 +end;
   1.235 +
   1.236 +
   1.237 +val auto_quickcheck = Quickcheck.auto;
   1.238 +val auto_quickcheck_time_limit = Quickcheck.auto_time_limit;