wenzelm@30824: (* Title: Tools/quickcheck.ML bulwahn@40246: Author: Stefan Berghofer, Florian Haftmann, Lukas Bulwahn, TU Muenchen haftmann@28256: haftmann@28256: Generic counterexample search engine. haftmann@28256: *) haftmann@28256: haftmann@28256: signature QUICKCHECK = haftmann@28256: sig blanchet@43020: val quickcheckN: string blanchet@43020: val genuineN: string blanchet@43020: val noneN: string blanchet@43020: val unknownN: string wenzelm@51302: (*configuration*) bulwahn@43882: val batch_tester : string Config.T bulwahn@40644: val size : int Config.T bulwahn@40644: val iterations : int Config.T bulwahn@45213: val depth : int Config.T bulwahn@40644: val no_assms : bool Config.T bulwahn@40644: val report : bool Config.T bulwahn@46565: val timeout : real Config.T bulwahn@42088: val timing : bool Config.T bulwahn@45757: val genuine_only : bool Config.T wenzelm@51302: val abort_potential : bool Config.T bulwahn@40644: val quiet : bool Config.T bulwahn@45764: val verbose : bool Config.T bulwahn@46565: val use_subtype : bool Config.T bulwahn@46565: val allow_function_inversion : bool Config.T bulwahn@40648: val finite_types : bool Config.T bulwahn@40648: val finite_type_size : int Config.T bulwahn@46863: val tag : string Config.T bulwahn@47348: val locale : string Config.T bulwahn@43912: val set_active_testers: string list -> Context.generic -> Context.generic wenzelm@41517: datatype expectation = No_Expectation | No_Counterexample | Counterexample; bulwahn@40644: datatype test_params = Test_Params of {default_type: typ list, expect : expectation}; bulwahn@40246: val test_params_of : Proof.context -> test_params bulwahn@40644: val map_test_params : (typ list * expectation -> typ list * expectation) bulwahn@40246: -> Context.generic -> Context.generic bulwahn@45159: val default_type : Proof.context -> typ list bulwahn@42089: datatype report = Report of bulwahn@42089: { iterations : int, raised_match_errors : int, bulwahn@42089: satisfied_assms : int list, positive_concl_tests : int } wenzelm@51302: (*quickcheck's result*) bulwahn@42088: datatype result = bulwahn@42088: Result of bulwahn@45727: {counterexample : (bool * (string * term) list) option, bulwahn@42088: evaluation_terms : (term * term) list option, bulwahn@42088: timings : (string * int) list, bulwahn@42088: reports : (int * report) list} bulwahn@43314: val empty_result : result bulwahn@45159: val found_counterexample : result -> bool bulwahn@43585: val add_timing : (string * int) -> result Unsynchronized.ref -> unit wenzelm@51302: val add_response : string list -> term list -> (bool * term list) option -> wenzelm@51302: result Unsynchronized.ref -> unit bulwahn@45159: val add_report : int -> report option -> result Unsynchronized.ref -> unit bulwahn@45727: val counterexample_of : result -> (bool * (string * term) list) option bulwahn@42089: val timings_of : result -> (string * int) list wenzelm@51302: (*registering testers & generators*) bulwahn@43878: type tester = bulwahn@45419: Proof.context -> bool -> (string * typ) list -> (term * term list) list -> result list bulwahn@43878: val add_tester : string * (bool Config.T * tester) -> Context.generic -> Context.generic bulwahn@43878: val add_batch_generator : bulwahn@43112: string * (Proof.context -> term list -> (int -> term list option) list) bulwahn@43112: -> Context.generic -> Context.generic bulwahn@43878: val add_batch_validator : bulwahn@43112: string * (Proof.context -> term list -> (int -> bool) list) bulwahn@43112: -> Context.generic -> Context.generic wenzelm@51302: (*basic operations*) bulwahn@45159: val message : Proof.context -> string -> unit bulwahn@45765: val verbose_message : Proof.context -> string -> unit bulwahn@45159: val limit : Time.time -> (bool * bool) -> (unit -> 'a) -> (unit -> 'a) -> unit -> 'a bulwahn@45755: val pretty_counterex : Proof.context -> bool -> bulwahn@45755: ((bool * (string * term) list) * (term * term) list) option -> Pretty.T wenzelm@51302: (*testing terms and proof states*) bulwahn@45159: val mk_batch_validator : Proof.context -> term list -> (int -> bool) list option bulwahn@45159: val mk_batch_tester : Proof.context -> term list -> (int -> term list option) list option bulwahn@45159: val active_testers : Proof.context -> tester list wenzelm@51302: val test_terms : Proof.context -> bool * bool -> (string * typ) list -> wenzelm@51302: (term * term list) list -> result list option wenzelm@51302: val quickcheck: (string * string list) list -> int -> Proof.state -> wenzelm@51302: (bool * (string * term) list) option haftmann@28256: end; haftmann@28256: haftmann@28256: structure Quickcheck : QUICKCHECK = haftmann@28256: struct haftmann@28256: wenzelm@55627: val quickcheckN = "quickcheck"; blanchet@43020: wenzelm@55627: val genuineN = "genuine"; wenzelm@55627: val noneN = "none"; wenzelm@55627: val unknownN = "unknown"; blanchet@43020: wenzelm@51302: bulwahn@35378: (* quickcheck report *) bulwahn@35378: bulwahn@35378: datatype report = Report of wenzelm@55627: {iterations : int, wenzelm@55627: raised_match_errors : int, wenzelm@55627: satisfied_assms : int list, wenzelm@55627: positive_concl_tests : int}; bulwahn@35378: wenzelm@51302: bulwahn@42088: (* Quickcheck Result *) bulwahn@42088: bulwahn@42088: datatype result = Result of wenzelm@55627: {counterexample : (bool * (string * term) list) option, wenzelm@55627: evaluation_terms : (term * term) list option, wenzelm@55627: timings : (string * int) list, wenzelm@55627: reports : (int * report) list}; bulwahn@42088: bulwahn@42088: val empty_result = wenzelm@55627: Result {counterexample = NONE, evaluation_terms = NONE, timings = [], reports = []}; bulwahn@42088: wenzelm@55627: fun counterexample_of (Result r) = #counterexample r; bulwahn@42088: wenzelm@55627: fun found_counterexample (Result r) = is_some (#counterexample r); bulwahn@42088: wenzelm@55627: fun response_of (Result r) = wenzelm@55627: (case (#counterexample r, #evaluation_terms r) of bulwahn@45730: (SOME ts, SOME evals) => SOME (ts, evals) wenzelm@55627: | (NONE, NONE) => NONE); bulwahn@42088: wenzelm@55627: fun timings_of (Result r) = #timings r; bulwahn@42088: bulwahn@45727: fun set_response names eval_terms (SOME (genuine, ts)) (Result r) = wenzelm@55627: let wenzelm@55627: val (ts1, ts2) = chop (length names) ts wenzelm@55627: val (eval_terms', _) = chop (length ts2) eval_terms wenzelm@55627: in wenzelm@55627: Result {counterexample = SOME (genuine, (names ~~ ts1)), wenzelm@55627: evaluation_terms = SOME (eval_terms' ~~ ts2), wenzelm@55627: timings = #timings r, reports = #reports r} wenzelm@55627: end wenzelm@55627: | set_response _ _ NONE result = result; bulwahn@42088: bulwahn@43585: bulwahn@42088: fun cons_timing timing (Result r) = bulwahn@42088: Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r, wenzelm@55627: timings = cons timing (#timings r), reports = #reports r}; bulwahn@42088: bulwahn@42088: fun cons_report size (SOME report) (Result r) = wenzelm@55627: Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r, wenzelm@55627: timings = #timings r, reports = cons (size, report) (#reports r)} wenzelm@55627: | cons_report _ NONE result = result; bulwahn@42088: wenzelm@42198: fun add_timing timing result_ref = wenzelm@55627: Unsynchronized.change result_ref (cons_timing timing); bulwahn@42088: wenzelm@42198: fun add_report size report result_ref = wenzelm@55627: Unsynchronized.change result_ref (cons_report size report); bulwahn@42088: bulwahn@42088: fun add_response names eval_terms response result_ref = wenzelm@55627: Unsynchronized.change result_ref (set_response names eval_terms response); bulwahn@42088: wenzelm@51302: bulwahn@37929: (* expectation *) bulwahn@37929: wenzelm@41517: datatype expectation = No_Expectation | No_Counterexample | Counterexample; bulwahn@37929: bulwahn@37929: fun merge_expectation (expect1, expect2) = wenzelm@55627: if expect1 = expect2 then expect1 else No_Expectation; bulwahn@37929: wenzelm@51302: (*quickcheck configuration -- default parameters, test generators*) wenzelm@67149: val batch_tester = Attrib.setup_config_string \<^binding>\quickcheck_batch_tester\ (K ""); wenzelm@67149: val size = Attrib.setup_config_int \<^binding>\quickcheck_size\ (K 10); wenzelm@67149: val iterations = Attrib.setup_config_int \<^binding>\quickcheck_iterations\ (K 100); wenzelm@67149: val depth = Attrib.setup_config_int \<^binding>\quickcheck_depth\ (K 10); bulwahn@45213: wenzelm@67149: val no_assms = Attrib.setup_config_bool \<^binding>\quickcheck_no_assms\ (K false); wenzelm@67149: val locale = Attrib.setup_config_string \<^binding>\quickcheck_locale\ (K "interpret expand"); wenzelm@67149: val report = Attrib.setup_config_bool \<^binding>\quickcheck_report\ (K true); wenzelm@67149: val timing = Attrib.setup_config_bool \<^binding>\quickcheck_timing\ (K false); wenzelm@67149: val timeout = Attrib.setup_config_real \<^binding>\quickcheck_timeout\ (K 30.0); bulwahn@46565: wenzelm@67149: val genuine_only = Attrib.setup_config_bool \<^binding>\quickcheck_genuine_only\ (K false); wenzelm@67149: val abort_potential = Attrib.setup_config_bool \<^binding>\quickcheck_abort_potential\ (K false); bulwahn@46565: wenzelm@67149: val quiet = Attrib.setup_config_bool \<^binding>\quickcheck_quiet\ (K false); wenzelm@67149: val verbose = Attrib.setup_config_bool \<^binding>\quickcheck_verbose\ (K false); wenzelm@67149: val tag = Attrib.setup_config_string \<^binding>\quickcheck_tag\ (K ""); bulwahn@46565: wenzelm@67149: val use_subtype = Attrib.setup_config_bool \<^binding>\quickcheck_use_subtype\ (K false); bulwahn@46565: bulwahn@45449: val allow_function_inversion = wenzelm@67149: Attrib.setup_config_bool \<^binding>\quickcheck_allow_function_inversion\ (K false); wenzelm@67149: val finite_types = Attrib.setup_config_bool \<^binding>\quickcheck_finite_types\ (K true); wenzelm@67149: val finite_type_size = Attrib.setup_config_int \<^binding>\quickcheck_finite_type_size\ (K 3); bulwahn@40646: bulwahn@40644: datatype test_params = Test_Params of bulwahn@40644: {default_type: typ list, expect : expectation}; wenzelm@38759: bulwahn@40644: fun dest_test_params (Test_Params {default_type, expect}) = (default_type, expect); bulwahn@40644: wenzelm@41517: fun make_test_params (default_type, expect) = wenzelm@41517: Test_Params {default_type = default_type, expect = expect}; bulwahn@40644: wenzelm@41517: fun map_test_params' f (Test_Params {default_type, expect}) = wenzelm@41517: make_test_params (f (default_type, expect)); wenzelm@38759: wenzelm@38759: fun merge_test_params wenzelm@41472: (Test_Params {default_type = default_type1, expect = expect1}, wenzelm@41472: Test_Params {default_type = default_type2, expect = expect2}) = wenzelm@41472: make_test_params wenzelm@41472: (merge (op =) (default_type1, default_type2), merge_expectation (expect1, expect2)); haftmann@28309: bulwahn@43878: type tester = wenzelm@51302: Proof.context -> bool -> (string * typ) list -> (term * term list) list -> result list; bulwahn@43878: bulwahn@39252: structure Data = Generic_Data wenzelm@33522: ( wenzelm@38759: type T = wenzelm@59436: (string * (bool Config.T * tester)) list * wenzelm@59436: (string * (Proof.context -> term list -> (int -> term list option) list)) list * wenzelm@59436: (string * (Proof.context -> term list -> (int -> bool) list)) list * wenzelm@59436: test_params; wenzelm@59436: val empty = ([], [], [], Test_Params {default_type = [], expect = No_Expectation}); haftmann@28256: val extend = I; wenzelm@55627: fun merge wenzelm@59436: ((testers1, batch_generators1, batch_validators1, params1), wenzelm@59436: (testers2, batch_generators2, batch_validators2, params2)) : T = wenzelm@59436: (AList.merge (op =) (K true) (testers1, testers2), wenzelm@59436: AList.merge (op =) (K true) (batch_generators1, batch_generators2), wenzelm@59436: AList.merge (op =) (K true) (batch_validators1, batch_validators2), wenzelm@59436: merge_test_params (params1, params2)); wenzelm@33522: ); haftmann@28256: wenzelm@59436: val test_params_of = #4 o Data.get o Context.Proof; wenzelm@55627: val default_type = fst o dest_test_params o test_params_of; wenzelm@55627: val expect = snd o dest_test_params o test_params_of; wenzelm@59436: val map_test_params = Data.map o @{apply 4(4)} o map_test_params'; bulwahn@40246: wenzelm@59436: val add_tester = Data.map o @{apply 4(1)} o AList.update (op =); wenzelm@59436: val add_batch_generator = Data.map o @{apply 4(2)} o AList.update (op =); wenzelm@59436: val add_batch_validator = Data.map o @{apply 4(3)} o AList.update (op =); haftmann@28309: bulwahn@43881: fun active_testers ctxt = bulwahn@43881: let wenzelm@59436: val testers = map snd (#1 (Data.get (Context.Proof ctxt))); bulwahn@43881: in bulwahn@43881: map snd (filter (fn (active, _) => Config.get ctxt active) testers) wenzelm@55627: end; wenzelm@51302: wenzelm@55629: fun set_active_testers [] context = context wenzelm@55629: | set_active_testers testers context = wenzelm@51302: let wenzelm@59436: val registered_testers = #1 (Data.get context); wenzelm@51302: in wenzelm@51302: fold (fn (name, (config, _)) => Config.put_generic config (member (op =) testers name)) wenzelm@55629: registered_testers context wenzelm@51302: end; wenzelm@51302: wenzelm@51302: haftmann@28315: (* generating tests *) haftmann@28315: bulwahn@41862: fun gen_mk_tester lookup ctxt v = haftmann@28309: let bulwahn@43882: val name = Config.get ctxt batch_tester wenzelm@51302: val tester = wenzelm@51302: (case lookup ctxt name of wenzelm@51302: NONE => error ("No such quickcheck batch-tester: " ^ name) wenzelm@51302: | SOME tester => tester ctxt); wenzelm@40235: in bulwahn@40909: if Config.get ctxt quiet then bulwahn@41862: try tester v bulwahn@40909: else wenzelm@43761: let (* FIXME !?!? *) bulwahn@41862: val tester = Exn.interruptible_capture tester v wenzelm@51302: in wenzelm@51302: (case Exn.get_res tester of bulwahn@40909: NONE => SOME (Exn.release tester) wenzelm@51302: | SOME tester => SOME tester) bulwahn@40909: end wenzelm@51302: end; bulwahn@43882: wenzelm@59436: val mk_batch_tester = gen_mk_tester (AList.lookup (op =) o #2 o Data.get o Context.Proof); wenzelm@59436: val mk_batch_validator = gen_mk_tester (AList.lookup (op =) o #3 o Data.get o Context.Proof); wenzelm@51302: wenzelm@51302: haftmann@28315: (* testing propositions *) haftmann@28315: bulwahn@43876: type compile_generator = wenzelm@51302: Proof.context -> (term * term list) list -> int list -> term list option * report option; bulwahn@43876: bulwahn@43585: fun limit timeout (limit_time, is_interactive) f exc () = bulwahn@41754: if limit_time then wenzelm@62519: Timeout.apply timeout f () wenzelm@62519: handle timeout_exn as Timeout.TIMEOUT _ => wenzelm@62519: if is_interactive then exc () else Exn.reraise timeout_exn wenzelm@55627: else f (); wenzelm@51302: wenzelm@58843: fun message ctxt s = if Config.get ctxt quiet then () else writeln s; bulwahn@41753: wenzelm@51302: fun verbose_message ctxt s = wenzelm@51302: if not (Config.get ctxt quiet) andalso Config.get ctxt verbose wenzelm@58843: then writeln s else (); bulwahn@45765: wenzelm@62983: fun test_terms ctxt0 (limit_time, is_interactive) insts goals = wenzelm@62983: let val ctxt = Simplifier_Trace.disable ctxt0 in wenzelm@62983: (case active_testers ctxt of wenzelm@62983: [] => error "No active testers for quickcheck" wenzelm@62983: | testers => wenzelm@62983: limit (seconds (Config.get ctxt timeout)) (limit_time, is_interactive) wenzelm@62983: (fn () => wenzelm@62983: Par_List.get_some (fn tester => wenzelm@62983: tester ctxt (length testers > 1) insts goals |> wenzelm@62983: (fn result => if exists found_counterexample result then SOME result else NONE)) wenzelm@62983: testers) wenzelm@62983: (fn () => (message ctxt "Quickcheck ran out of time"; NONE)) ()) wenzelm@62983: end bulwahn@46759: bulwahn@46759: fun all_axioms_of ctxt t = bulwahn@46759: let wenzelm@51302: val intros = Locale.get_intros ctxt; wenzelm@51302: val unfolds = Locale.get_unfolds ctxt; wenzelm@51302: fun retrieve_prems thms t = wenzelm@51302: (case filter (fn th => Term.could_unify (Thm.concl_of th, t)) thms of wenzelm@51302: [] => NONE bulwahn@46759: | [th] => wenzelm@51302: let wenzelm@51302: val (tyenv, tenv) = wenzelm@51302: Pattern.match (Proof_Context.theory_of ctxt) wenzelm@51302: (Thm.concl_of th, t) (Vartab.empty, Vartab.empty) wenzelm@51302: in SOME (map (Envir.subst_term (tyenv, tenv)) (Thm.prems_of th)) end); bulwahn@46759: fun all t = wenzelm@51302: (case retrieve_prems intros t of bulwahn@46759: NONE => retrieve_prems unfolds t wenzelm@51302: | SOME ts => SOME (maps (fn t => the_default [t] (all t)) ts)); bulwahn@46759: in bulwahn@46759: all t wenzelm@51302: end; bulwahn@46759: bulwahn@47348: fun locale_config_of s = bulwahn@47348: let wenzelm@51302: val cs = space_explode " " s; bulwahn@47348: in bulwahn@47348: if forall (fn c => c = "expand" orelse c = "interpret") cs then cs wenzelm@55627: else wenzelm@55627: (warning ("Invalid quickcheck_locale setting: falling back to the default setting."); bulwahn@47348: ["interpret", "expand"]) wenzelm@51302: end; bulwahn@47348: bulwahn@42026: fun test_goal (time_limit, is_interactive) (insts, eval_terms) i state = bulwahn@40648: let wenzelm@62982: val ctxt = Proof.context_of state; bulwahn@40648: val thy = Proof.theory_of state; wenzelm@62982: wenzelm@67149: fun strip (Const (\<^const_name>\Pure.all\, _) $ Abs (_, _, t)) = strip t bulwahn@40648: | strip t = t; bulwahn@40648: val {goal = st, ...} = Proof.raw_goal state; wenzelm@59582: val (gi, frees) = Logic.goal_params (Thm.prop_of st) i; wenzelm@62982: val opt_locale = Named_Target.bottom_locale_of ctxt; wenzelm@51302: val assms = wenzelm@62982: if Config.get ctxt no_assms then [] wenzelm@51302: else wenzelm@62982: (case opt_locale of wenzelm@62982: NONE => Assumption.all_assms_of ctxt wenzelm@62982: | SOME locale => Assumption.local_assms_of ctxt (Locale.init locale thy)); bulwahn@40648: val proto_goal = Logic.list_implies (map Thm.term_of assms, subst_bounds (frees, strip gi)); wenzelm@51302: fun axioms_of locale = wenzelm@51302: (case fst (Locale.specification_of thy locale) of bulwahn@46759: NONE => [] wenzelm@62982: | SOME t => the_default [] (all_axioms_of ctxt t)); wenzelm@62982: val config = locale_config_of (Config.get ctxt locale); wenzelm@55627: val goals = wenzelm@62982: (case opt_locale of wenzelm@55627: NONE => [(proto_goal, eval_terms)] wenzelm@55627: | SOME locale => wenzelm@55627: fold (fn c => wenzelm@55627: if c = "expand" then wenzelm@55627: cons (Logic.list_implies (axioms_of locale, proto_goal), eval_terms) wenzelm@55627: else if c = "interpret" then wenzelm@55627: append (map (fn (_, phi) => wenzelm@55627: (Morphism.term phi proto_goal, map (Morphism.term phi) eval_terms)) wenzelm@55627: (Locale.registrations_of (Context.Theory thy) (* FIXME !? *) locale)) wenzelm@55627: else I) config []); wenzelm@51302: val _ = wenzelm@62982: verbose_message ctxt wenzelm@51302: (Pretty.string_of wenzelm@62982: (Pretty.big_list ("Checking goals: ") (map (Syntax.pretty_term ctxt o fst) goals))); bulwahn@40648: in wenzelm@62982: test_terms ctxt (time_limit, is_interactive) insts goals wenzelm@51302: end; wenzelm@51302: bulwahn@40648: bulwahn@37912: (* pretty printing *) haftmann@28315: wenzelm@59435: fun tool_name auto = if auto then "Auto Quickcheck" else "Quickcheck"; blanchet@40225: bulwahn@46863: fun pretty_counterex ctxt auto NONE = wenzelm@59435: Pretty.para (tool_name auto ^ " found no counterexample." ^ Config.get ctxt tag) bulwahn@45730: | pretty_counterex ctxt auto (SOME ((genuine, cex), eval_terms)) = wenzelm@59438: let wenzelm@59438: val header = wenzelm@59438: Pretty.para wenzelm@59438: (tool_name auto ^ " found a " ^ wenzelm@60666: (if genuine then "counterexample" wenzelm@60666: else "potentially spurious counterexample due to underspecified functions") ^ wenzelm@60666: (if null cex then "." else ":") ^ wenzelm@59438: Config.get ctxt tag); wenzelm@59439: fun pretty_cex (x, t) = wenzelm@59439: Pretty.block [Pretty.str (x ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]; wenzelm@59438: in wenzelm@59439: Pretty.chunks (Pretty.block (Pretty.fbreaks (header :: map pretty_cex (rev cex))) :: wenzelm@59438: (if null eval_terms then [] wenzelm@59438: else wenzelm@59438: [Pretty.big_list "Evaluated terms:" wenzelm@59438: (map (fn (t, u) => wenzelm@59438: Pretty.block [Syntax.pretty_term ctxt t, Pretty.str " =", Pretty.brk 1, wenzelm@59438: Syntax.pretty_term ctxt u]) (rev eval_terms))])) wenzelm@59438: end; haftmann@28315: wenzelm@51302: wenzelm@30980: (* Isar commands *) haftmann@28315: wenzelm@51302: fun read_nat s = wenzelm@55627: (case Library.read_int (Symbol.explode s) of wenzelm@51302: (k, []) => wenzelm@51302: if k >= 0 then k haftmann@28336: else error ("Not a natural number: " ^ s) wenzelm@55627: | _ => error ("Not a natural number: " ^ s)); bulwahn@37909: blanchet@34128: fun read_bool "false" = false blanchet@34128: | read_bool "true" = true wenzelm@51302: | read_bool s = error ("Not a Boolean value: " ^ s); haftmann@28315: bulwahn@40366: fun read_real s = wenzelm@55627: (case Real.fromString s of bulwahn@40366: SOME s => s wenzelm@51302: | NONE => error ("Not a real number: " ^ s)); bulwahn@40366: bulwahn@37929: fun read_expectation "no_expectation" = No_Expectation wenzelm@41517: | read_expectation "no_counterexample" = No_Counterexample bulwahn@37929: | read_expectation "counterexample" = Counterexample wenzelm@51302: | read_expectation s = error ("Not an expectation value: " ^ s); bulwahn@37929: wenzelm@59437: fun valid_tester_name context name = wenzelm@59437: AList.defined (op =) (#1 (Data.get context)) name; wenzelm@51302: wenzelm@59437: fun parse_tester name (testers, context) = wenzelm@59437: if valid_tester_name context name then wenzelm@59437: (insert (op =) name testers, context) wenzelm@55627: else error ("Unknown tester: " ^ name); bulwahn@40912: bulwahn@43881: fun parse_test_param ("tester", args) = fold parse_tester args bulwahn@43881: | parse_test_param ("size", [arg]) = apsnd (Config.put_generic size (read_nat arg)) bulwahn@43881: | parse_test_param ("iterations", [arg]) = apsnd (Config.put_generic iterations (read_nat arg)) wenzelm@51302: | parse_test_param ("depth", [arg]) = apsnd (Config.put_generic depth (read_nat arg)) wenzelm@55627: | parse_test_param ("default_type", arg) = wenzelm@55629: (fn (testers, context) => wenzelm@55627: (testers, map_test_params wenzelm@55629: (apfst (K (map (Proof_Context.read_typ (Context.proof_of context)) arg))) context)) bulwahn@43881: | parse_test_param ("no_assms", [arg]) = apsnd (Config.put_generic no_assms (read_bool arg)) wenzelm@55627: | parse_test_param ("expect", [arg]) = apsnd (map_test_params (apsnd (K (read_expectation arg)))) bulwahn@43881: | parse_test_param ("report", [arg]) = apsnd (Config.put_generic report (read_bool arg)) wenzelm@55627: | parse_test_param ("genuine_only", [arg]) = wenzelm@55627: apsnd (Config.put_generic genuine_only (read_bool arg)) wenzelm@55627: | parse_test_param ("abort_potential", [arg]) = wenzelm@55627: apsnd (Config.put_generic abort_potential (read_bool arg)) bulwahn@43881: | parse_test_param ("quiet", [arg]) = apsnd (Config.put_generic quiet (read_bool arg)) bulwahn@45764: | parse_test_param ("verbose", [arg]) = apsnd (Config.put_generic verbose (read_bool arg)) bulwahn@46863: | parse_test_param ("tag", [arg]) = apsnd (Config.put_generic tag arg) wenzelm@55627: | parse_test_param ("use_subtype", [arg]) = wenzelm@55627: apsnd (Config.put_generic use_subtype (read_bool arg)) wenzelm@55627: | parse_test_param ("timeout", [arg]) = wenzelm@55627: apsnd (Config.put_generic timeout (read_real arg)) wenzelm@55627: | parse_test_param ("finite_types", [arg]) = wenzelm@55627: apsnd (Config.put_generic finite_types (read_bool arg)) bulwahn@45449: | parse_test_param ("allow_function_inversion", [arg]) = bulwahn@45449: apsnd (Config.put_generic allow_function_inversion (read_bool arg)) wenzelm@41517: | parse_test_param ("finite_type_size", [arg]) = wenzelm@55627: apsnd (Config.put_generic finite_type_size (read_nat arg)) wenzelm@51302: | parse_test_param (name, _) = wenzelm@59437: (fn (testers, context) => wenzelm@59437: if valid_tester_name context name then wenzelm@59437: (insert (op =) name testers, context) wenzelm@51302: else error ("Unknown tester or test parameter: " ^ name)); haftmann@28315: bulwahn@43881: fun parse_test_param_inst (name, arg) ((insts, eval_terms), (testers, ctxt)) = wenzelm@51302: (case try (Proof_Context.read_typ ctxt) name of wenzelm@51302: SOME (TFree (v, _)) => wenzelm@51302: ((AList.update (op =) (v, Proof_Context.read_typ ctxt (the_single arg)) insts, eval_terms), wenzelm@51302: (testers, ctxt)) wenzelm@51302: | NONE => wenzelm@51302: (case name of wenzelm@51302: "eval" => ((insts, eval_terms @ map (Syntax.read_term ctxt) arg), (testers, ctxt)) wenzelm@51302: | _ => wenzelm@51302: ((insts, eval_terms), wenzelm@55630: let wenzelm@55630: val (testers', Context.Proof ctxt') = wenzelm@55630: parse_test_param (name, arg) (testers, Context.Proof ctxt); wenzelm@55630: in (testers', ctxt') end))); haftmann@28309: wenzelm@55627: fun quickcheck_params_cmd args = wenzelm@55627: Context.theory_map wenzelm@55629: (fn context => uncurry set_active_testers (fold parse_test_param args ([], context))); wenzelm@41517: bulwahn@42088: fun check_expectation state results = wenzelm@51302: if is_some results andalso expect (Proof.context_of state) = No_Counterexample then wenzelm@51302: error "quickcheck expected to find no counterexample but found one" wenzelm@51302: else if is_none results andalso expect (Proof.context_of state) = Counterexample then wenzelm@51302: error "quickcheck expected to find a counterexample but did not find one" wenzelm@51302: else (); bulwahn@42088: bulwahn@35378: fun gen_quickcheck args i state = bulwahn@40644: state bulwahn@43881: |> Proof.map_context_result (fn ctxt => bulwahn@43881: apsnd (fn (testers, ctxt) => Context.proof_map (set_active_testers testers) ctxt) bulwahn@43881: (fold parse_test_param_inst args (([], []), ([], ctxt)))) wenzelm@55627: |> (fn ((insts, eval_terms), state') => wenzelm@55627: test_goal (true, true) (insts, eval_terms) i state' wenzelm@55627: |> tap (check_expectation state') wenzelm@55627: |> rpair state'); boehmes@32297: bulwahn@43879: fun quickcheck args i state = wenzelm@51302: Option.map (the o get_first counterexample_of) (fst (gen_quickcheck args i state)); bulwahn@35378: wenzelm@60190: fun quickcheck_cmd args i st = wenzelm@60190: gen_quickcheck args i (Toplevel.proof_of st) bulwahn@46863: |> apfst (Option.map (the o get_first response_of)) wenzelm@51302: |> (fn (r, state) => wenzelm@58843: writeln (Pretty.string_of wenzelm@51302: (pretty_counterex (Proof.context_of state) false r))); haftmann@28309: wenzelm@41517: val parse_arg = wenzelm@41517: Parse.name -- wenzelm@67149: (Scan.optional (\<^keyword>\=\ |-- wenzelm@41517: (((Parse.name || Parse.float_number) >> single) || wenzelm@67149: (\<^keyword>\[\ |-- Parse.list1 Parse.name --| \<^keyword>\]\))) ["true"]); haftmann@28309: wenzelm@41517: val parse_args = wenzelm@67149: \<^keyword>\[\ |-- Parse.list1 parse_arg --| \<^keyword>\]\ || Scan.succeed []; haftmann@28336: wenzelm@36960: val _ = wenzelm@67149: Outer_Syntax.command \<^command_keyword>\quickcheck_params\ "set parameters for random testing" wenzelm@36960: (parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args))); haftmann@28309: wenzelm@36960: val _ = wenzelm@67149: Outer_Syntax.command \<^command_keyword>\quickcheck\ wenzelm@46961: "try to find counterexample for subgoal" wenzelm@60094: (parse_args -- Scan.optional Parse.nat 1 >> wenzelm@60190: (fn (args, i) => Toplevel.keep_proof (quickcheck_cmd args i))); haftmann@28309: wenzelm@51302: blanchet@43020: (* automatic testing *) blanchet@43020: blanchet@43020: fun try_quickcheck auto state = blanchet@43020: let blanchet@43020: val ctxt = Proof.context_of state; blanchet@43020: val i = 1; blanchet@43020: val res = blanchet@43020: state blanchet@43020: |> Proof.map_context (Config.put report false #> Config.put quiet true) blanchet@43020: |> try (test_goal (false, false) ([], []) i); blanchet@43020: in wenzelm@51302: (case res of wenzelm@58892: NONE => (unknownN, []) blanchet@43020: | SOME results => blanchet@43020: let wenzelm@59184: val msg = wenzelm@59184: Pretty.string_of wenzelm@59184: (pretty_counterex ctxt auto (Option.map (the o get_first response_of) results)) blanchet@43020: in wenzelm@59184: if is_some results then (genuineN, if auto then [msg] else (writeln msg; [])) wenzelm@58892: else (noneN, []) wenzelm@51302: end) blanchet@43020: end wenzelm@51302: |> `(fn (outcome_code, _) => outcome_code = genuineN); blanchet@43020: wenzelm@67149: val _ = Try.tool_setup (quickcheckN, (20, \<^system_option>\auto_quickcheck\, try_quickcheck)); blanchet@43020: haftmann@28315: end; haftmann@28309: