src/HOL/Tools/Quickcheck/narrowing_generators.ML
author wenzelm
Wed Apr 06 16:33:33 2016 +0200 (2016-04-06)
changeset 62889 99c7f31615c2
parent 62876 507c90523113
child 62902 3c0f53eae166
permissions -rw-r--r--
clarified modules;
tuned signature;
     1 (*  Title:      HOL/Tools/Quickcheck/narrowing_generators.ML
     2     Author:     Lukas Bulwahn, TU Muenchen
     3 
     4 Narrowing-based counterexample generation.
     5 *)
     6 
     7 signature NARROWING_GENERATORS =
     8 sig
     9   val allow_existentials : bool Config.T
    10   val finite_functions : bool Config.T
    11   val overlord : bool Config.T
    12   val ghc_options : string Config.T  (* FIXME prefer settings, i.e. getenv (!?) *)
    13   val active : bool Config.T
    14   datatype counterexample = Universal_Counterexample of (term * counterexample)
    15     | Existential_Counterexample of (term * counterexample) list
    16     | Empty_Assignment
    17   val put_counterexample: (unit -> (bool * term list) option) -> Proof.context -> Proof.context
    18   val put_existential_counterexample : (unit -> counterexample option) -> Proof.context -> Proof.context
    19 end;
    20 
    21 structure Narrowing_Generators : NARROWING_GENERATORS =
    22 struct
    23 
    24 (* configurations *)
    25 
    26 val allow_existentials = Attrib.setup_config_bool @{binding quickcheck_allow_existentials} (K true)
    27 val finite_functions = Attrib.setup_config_bool @{binding quickcheck_finite_functions} (K true)
    28 val overlord = Attrib.setup_config_bool @{binding quickcheck_narrowing_overlord} (K false)
    29 val ghc_options = Attrib.setup_config_string @{binding quickcheck_narrowing_ghc_options} (K "")
    30 
    31 (* partial_term_of instances *)
    32 
    33 fun mk_partial_term_of (x, T) =
    34   Const (@{const_name Quickcheck_Narrowing.partial_term_of_class.partial_term_of},
    35     Term.itselfT T --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
    36       $ Logic.mk_type T $ x
    37 
    38 (** formal definition **)
    39 
    40 fun add_partial_term_of tyco raw_vs thy =
    41   let
    42     val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
    43     val ty = Type (tyco, map TFree vs);
    44     val lhs = Const (@{const_name partial_term_of},
    45         Term.itselfT ty --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
    46       $ Free ("x", Term.itselfT ty) $ Free ("t", @{typ narrowing_term});
    47     val rhs = @{term "undefined :: Code_Evaluation.term"};
    48     val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
    49     fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
    50       o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
    51   in
    52     thy
    53     |> Class.instantiation ([tyco], vs, @{sort partial_term_of})
    54     |> `(fn lthy => Syntax.check_term lthy eq)
    55     |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
    56     |> snd
    57     |> Class.prove_instantiation_exit (fn ctxt => Class.intro_classes_tac ctxt [])
    58   end;
    59 
    60 fun ensure_partial_term_of (tyco, (raw_vs, _)) thy =
    61   let
    62     val need_inst = not (Sorts.has_instance (Sign.classes_of thy) tyco @{sort partial_term_of})
    63       andalso Sorts.has_instance (Sign.classes_of thy) tyco @{sort typerep};
    64   in if need_inst then add_partial_term_of tyco raw_vs thy else thy end;
    65 
    66 
    67 (** code equations for datatypes **)
    68 
    69 fun mk_partial_term_of_eq thy ty (i, (c, (_, tys))) =
    70   let
    71     val frees = map Free (Name.invent_names Name.context "a" (map (K @{typ narrowing_term}) tys))
    72     val narrowing_term = @{term Quickcheck_Narrowing.Narrowing_constructor} $ HOLogic.mk_number @{typ integer} i
    73       $ HOLogic.mk_list @{typ narrowing_term} (rev frees)
    74     val rhs = fold (fn u => fn t => @{term "Code_Evaluation.App"} $ t $ u)
    75         (map mk_partial_term_of (frees ~~ tys))
    76         (@{term "Code_Evaluation.Const"} $ HOLogic.mk_literal c $ HOLogic.mk_typerep (tys ---> ty))
    77     val insts =
    78       map (SOME o Thm.global_cterm_of thy o Logic.unvarify_types_global o Logic.varify_global)
    79         [Free ("ty", Term.itselfT ty), narrowing_term, rhs]
    80     val cty = Thm.global_ctyp_of thy ty;
    81   in
    82     @{thm partial_term_of_anything}
    83     |> Thm.instantiate' [SOME cty] insts
    84     |> Thm.varifyT_global
    85   end
    86 
    87 fun add_partial_term_of_code tyco raw_vs raw_cs thy =
    88   let
    89     val algebra = Sign.classes_of thy;
    90     val vs = map (fn (v, sort) =>
    91       (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
    92     val ty = Type (tyco, map TFree vs);
    93     val cs = (map o apsnd o apsnd o map o map_atyps)
    94       (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
    95     val const = Axclass.param_of_inst thy (@{const_name partial_term_of}, tyco);
    96     val var_insts =
    97       map (SOME o Thm.global_cterm_of thy o Logic.unvarify_types_global o Logic.varify_global)
    98         [Free ("ty", Term.itselfT ty), @{term "Quickcheck_Narrowing.Narrowing_variable p tt"},
    99           @{term "Code_Evaluation.Free (STR ''_'')"} $ HOLogic.mk_typerep ty];
   100     val var_eq =
   101       @{thm partial_term_of_anything}
   102       |> Thm.instantiate' [SOME (Thm.global_ctyp_of thy ty)] var_insts
   103       |> Thm.varifyT_global
   104     val eqs = var_eq :: map_index (mk_partial_term_of_eq thy ty) cs;
   105  in
   106     thy
   107     |> Code.del_eqns const
   108     |> fold Code.add_eqn eqs
   109   end;
   110 
   111 fun ensure_partial_term_of_code (tyco, (raw_vs, cs)) thy =
   112   let
   113     val has_inst = Sorts.has_instance (Sign.classes_of thy) tyco @{sort partial_term_of};
   114   in if has_inst then add_partial_term_of_code tyco raw_vs cs thy else thy end;
   115 
   116 
   117 (* narrowing generators *)
   118 
   119 (** narrowing specific names and types **)
   120 
   121 exception FUNCTION_TYPE;
   122 
   123 val narrowingN = "narrowing";
   124 
   125 fun narrowingT T =
   126   @{typ integer} --> Type (@{type_name Quickcheck_Narrowing.narrowing_cons}, [T])
   127 
   128 fun mk_cons c T = Const (@{const_name Quickcheck_Narrowing.cons}, T --> narrowingT T) $ Const (c, T)
   129 
   130 fun mk_apply (T, t) (U, u) =
   131   let
   132     val (_, U') = dest_funT U
   133   in
   134     (U', Const (@{const_name Quickcheck_Narrowing.apply},
   135       narrowingT U --> narrowingT T --> narrowingT U') $ u $ t)
   136   end
   137 
   138 fun mk_sum (t, u) =
   139   let
   140     val T = fastype_of t
   141   in
   142     Const (@{const_name Quickcheck_Narrowing.sum}, T --> T --> T) $ t $ u
   143   end
   144 
   145 (** deriving narrowing instances **)
   146 
   147 fun mk_equations descr vs narrowings =
   148   let
   149     fun mk_call T =
   150       (T, Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, narrowingT T))
   151     fun mk_aux_call fTs (k, _) (tyco, Ts) =
   152       let
   153         val T = Type (tyco, Ts)
   154         val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
   155       in
   156         (T, nth narrowings k)
   157       end
   158     fun mk_consexpr simpleT (c, xs) =
   159       let
   160         val Ts = map fst xs
   161       in snd (fold mk_apply xs (Ts ---> simpleT, mk_cons c (Ts ---> simpleT))) end
   162     fun mk_rhs exprs = foldr1 mk_sum exprs
   163     val rhss =
   164       Old_Datatype_Aux.interpret_construction descr vs
   165         { atyp = mk_call, dtyp = mk_aux_call }
   166       |> (map o apfst) Type
   167       |> map (fn (T, cs) => map (mk_consexpr T) cs)
   168       |> map mk_rhs
   169     val lhss = narrowings
   170     val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
   171   in
   172     eqs
   173   end
   174 
   175 fun contains_recursive_type_under_function_types xs =
   176   exists (fn (_, (_, _, cs)) => cs |> exists (snd #> exists (fn dT =>
   177     (case Old_Datatype_Aux.strip_dtyp dT of (_ :: _, Old_Datatype_Aux.DtRec _) => true | _ => false)))) xs
   178 
   179 fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   180   let
   181     val _ = Old_Datatype_Aux.message config "Creating narrowing generators ...";
   182     val narrowingsN = map (prefix (narrowingN ^ "_")) (names @ auxnames);
   183   in
   184     if not (contains_recursive_type_under_function_types descr) then
   185       thy
   186       |> Class.instantiation (tycos, vs, @{sort narrowing})
   187       |> Quickcheck_Common.define_functions
   188         (fn narrowings => mk_equations descr vs narrowings, NONE)
   189         prfx [] narrowingsN (map narrowingT (Ts @ Us))
   190       |> Class.prove_instantiation_exit (fn ctxt => Class.intro_classes_tac ctxt [])
   191     else
   192       thy
   193   end;
   194 
   195 (* testing framework *)
   196 
   197 val target = "Haskell_Quickcheck"
   198 
   199 (** invocation of Haskell interpreter **)
   200 
   201 val narrowing_engine =
   202   File.read @{path "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs"}
   203 
   204 val pnf_narrowing_engine =
   205   File.read @{path "~~/src/HOL/Tools/Quickcheck/PNF_Narrowing_Engine.hs"}
   206 
   207 fun exec verbose code =
   208   ML_Context.exec (fn () =>
   209     ML_Compiler0.use_text ML_Env.context
   210       {line = 0, file = "generated code", verbose = verbose, debug = false} code)
   211 
   212 fun with_overlord_dir name f =
   213   let
   214     val path =
   215       Path.append (Path.explode "$ISABELLE_HOME_USER") (Path.basic (name ^ serial_string ()))
   216     val _ = Isabelle_System.mkdirs path;
   217   in Exn.release (Exn.capture f path) end;
   218 
   219 fun elapsed_time description e =
   220   let val ({elapsed, ...}, result) = Timing.timing e ()
   221   in (result, (description, Time.toMilliseconds elapsed)) end
   222 
   223 fun value (contains_existentials, ((genuine_only, (quiet, verbose)), size)) ctxt cookie (code_modules, _) =
   224   let
   225     val ((is_genuine, counterexample_of), (get, put, put_ml)) = cookie
   226     fun message s = if quiet then () else writeln s
   227     fun verbose_message s = if not quiet andalso verbose then writeln s else ()
   228     val current_size = Unsynchronized.ref 0
   229     val current_result = Unsynchronized.ref Quickcheck.empty_result
   230     val tmp_prefix = "Quickcheck_Narrowing"
   231     val ghc_options = Config.get ctxt ghc_options
   232     val with_tmp_dir =
   233       if Config.get ctxt overlord then with_overlord_dir else Isabelle_System.with_tmp_dir
   234     fun run in_path =
   235       let
   236         fun mk_code_file name = Path.append in_path (Path.basic (name ^ ".hs"))
   237         val generatedN = Code_Target.generatedN
   238         val includes = AList.delete (op =) generatedN code_modules |> (map o apfst) mk_code_file;
   239         val code = the (AList.lookup (op =) code_modules generatedN)
   240         val code_file = mk_code_file generatedN
   241         val narrowing_engine_file = mk_code_file "Narrowing_Engine"
   242         val main_file = mk_code_file "Main"
   243         val main = "module Main where {\n\n" ^
   244           "import System.IO;\n" ^
   245           "import System.Environment;\n" ^
   246           "import Narrowing_Engine;\n" ^
   247           "import " ^ generatedN ^ " ;\n\n" ^
   248           "main = getArgs >>= \\[potential, size] -> " ^
   249           "Narrowing_Engine.depthCheck (read potential) (read size) (" ^ generatedN ^ ".value ())\n\n" ^
   250           "}\n"
   251         val _ = map (uncurry File.write) (includes @
   252           [(narrowing_engine_file, if contains_existentials then pnf_narrowing_engine else narrowing_engine),
   253            (code_file, code), (main_file, main)])
   254         val executable = File.bash_path (Path.append in_path (Path.basic "isabelle_quickcheck_narrowing"))
   255         val cmd = "exec \"$ISABELLE_GHC\" " ^ Code_Haskell.language_params ^ " " ^
   256           ghc_options ^ " " ^
   257           (space_implode " " (map File.bash_path (map fst includes @ [code_file, narrowing_engine_file, main_file]))) ^
   258           " -o " ^ executable ^ ";"
   259         val (_, compilation_time) =
   260           elapsed_time "Haskell compilation" (fn () => Isabelle_System.bash cmd)
   261         val _ = Quickcheck.add_timing compilation_time current_result
   262         fun haskell_string_of_bool v = if v then "True" else "False"
   263         val _ = if Isabelle_System.bash cmd <> 0 then error "Compilation with GHC failed" else ()
   264         fun with_size genuine_only k =
   265           if k > size then
   266             (NONE, !current_result)
   267           else
   268             let
   269               val _ = verbose_message ("[Quickcheck-narrowing] Test data size: " ^ string_of_int k)
   270               val _ = current_size := k
   271               val ((response, _), timing) = elapsed_time ("execution of size " ^ string_of_int k)
   272                 (fn () => Isabelle_System.bash_output
   273                   (executable ^ " " ^ haskell_string_of_bool genuine_only ^ " " ^ string_of_int k))
   274               val _ = Quickcheck.add_timing timing current_result
   275             in
   276               if response = "NONE\n" then
   277                 with_size genuine_only (k + 1)
   278               else
   279                 let
   280                   val output_value = the_default "NONE"
   281                     (try (snd o split_last o filter_out (fn s => s = "") o split_lines) response)
   282                   val ml_code =
   283                     "\nval _ = Context.put_generic_context (SOME (Context.map_proof (" ^ put_ml
   284                     ^ " (fn () => " ^ output_value ^ ")) (Context.the_generic_context ())))";
   285                   val ctxt' = ctxt
   286                     |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
   287                     |> Context.proof_map (exec false ml_code);
   288                   val counterexample = get ctxt' ()
   289                 in
   290                   if is_genuine counterexample then
   291                     (counterexample, !current_result)
   292                   else
   293                     let
   294                       val cex = Option.map (rpair []) (counterexample_of counterexample);
   295                       val _ = message (Pretty.string_of (Quickcheck.pretty_counterex ctxt false cex));
   296                       val _ = message "Quickcheck continues to find a genuine counterexample...";
   297                     in with_size true (k + 1) end
   298                end
   299             end
   300       in with_size genuine_only 0 end
   301   in
   302     with_tmp_dir tmp_prefix run
   303   end;
   304 
   305 fun dynamic_value_strict opts cookie ctxt postproc t =
   306   let
   307     fun evaluator program _ vs_ty_t deps =
   308       Exn.interruptible_capture (value opts ctxt cookie)
   309         (Code_Target.evaluator ctxt target program deps true vs_ty_t);
   310   in Exn.release (Code_Thingol.dynamic_value ctxt (Exn.map_res o postproc) evaluator t) end;
   311 
   312 
   313 (** counterexample generator **)
   314 
   315 datatype counterexample =
   316     Universal_Counterexample of (term * counterexample)
   317   | Existential_Counterexample of (term * counterexample) list
   318   | Empty_Assignment
   319 
   320 fun map_counterexample f Empty_Assignment = Empty_Assignment
   321   | map_counterexample f (Universal_Counterexample (t, c)) =
   322       Universal_Counterexample (f t, map_counterexample f c)
   323   | map_counterexample f (Existential_Counterexample cs) =
   324       Existential_Counterexample (map (fn (t, c) => (f t, map_counterexample f c)) cs)
   325 
   326 structure Data = Proof_Data
   327 (
   328   type T =
   329     (unit -> (bool * term list) option) *
   330     (unit -> counterexample option);
   331   val empty: T =
   332    (fn () => raise Fail "counterexample",
   333     fn () => raise Fail "existential_counterexample");
   334   fun init _ = empty;
   335 );
   336 
   337 val get_counterexample = #1 o Data.get;
   338 val get_existential_counterexample = #2 o Data.get;
   339 
   340 val put_counterexample = Data.map o @{apply 2(1)} o K;
   341 val put_existential_counterexample = Data.map o @{apply 2(2)} o K;
   342 
   343 fun finitize_functions (xTs, t) =
   344   let
   345     val (names, boundTs) = split_list xTs
   346     fun mk_eval_ffun dT rT =
   347       Const (@{const_name "Quickcheck_Narrowing.eval_ffun"},
   348         Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT]) --> dT --> rT)
   349     fun mk_eval_cfun dT rT =
   350       Const (@{const_name "Quickcheck_Narrowing.eval_cfun"},
   351         Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT]) --> dT --> rT)
   352     fun eval_function (Type (@{type_name fun}, [dT, rT])) =
   353       let
   354         val (rt', rT') = eval_function rT
   355       in
   356         case dT of
   357           Type (@{type_name fun}, _) =>
   358             (fn t => absdummy dT (rt' (mk_eval_cfun dT rT' $ incr_boundvars 1 t $ Bound 0)),
   359               Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT']))
   360         | _ =>
   361             (fn t => absdummy dT (rt' (mk_eval_ffun dT rT' $ incr_boundvars 1 t $ Bound 0)),
   362               Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT']))
   363       end
   364       | eval_function (T as Type (@{type_name prod}, [fT, sT])) =
   365         let
   366           val (ft', fT') = eval_function fT
   367           val (st', sT') = eval_function sT
   368           val T' = Type (@{type_name prod}, [fT', sT'])
   369           val map_const = Const (@{const_name map_prod}, (fT' --> fT) --> (sT' --> sT) --> T' --> T)
   370           fun apply_dummy T t = absdummy T (t (Bound 0))
   371         in
   372           (fn t => list_comb (map_const, [apply_dummy fT' ft', apply_dummy sT' st', t]), T')
   373         end
   374       | eval_function T = (I, T)
   375     val (tt, boundTs') = split_list (map eval_function boundTs)
   376     val t' = subst_bounds (map2 (fn f => fn x => f x) (rev tt) (map_index (Bound o fst) boundTs), t)
   377   in
   378     (names ~~ boundTs', t')
   379   end
   380 
   381 fun dest_ffun (Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT])) = (dT, rT)
   382 
   383 fun eval_finite_functions (Const (@{const_name "Quickcheck_Narrowing.ffun.Constant"}, T) $ value) =
   384     absdummy (fst (dest_ffun (body_type T))) (eval_finite_functions value)
   385   | eval_finite_functions (Const (@{const_name "Quickcheck_Narrowing.ffun.Update"}, T) $ a $ b $ f) =
   386     let
   387       val (T1, T2) = dest_ffun (body_type T)
   388     in
   389       Quickcheck_Common.mk_fun_upd T1 T2
   390         (eval_finite_functions a, eval_finite_functions b) (eval_finite_functions f)
   391     end
   392   | eval_finite_functions t = t
   393 
   394 (** tester **)
   395 
   396 val rewrs =
   397     map (swap o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
   398       (@{thms all_simps} @ @{thms ex_simps})
   399     @ map (HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
   400         [@{thm iff_conv_conj_imp}, @{thm not_ex}, @{thm not_all},
   401          @{thm meta_eq_to_obj_eq [OF Ex1_def]}]
   402 
   403 fun make_pnf_term thy t = Pattern.rewrite_term thy rewrs [] t
   404 
   405 fun strip_quantifiers (Const (@{const_name Ex}, _) $ Abs (x, T, t)) =
   406     apfst (cons (@{const_name Ex}, (x, T))) (strip_quantifiers t)
   407   | strip_quantifiers (Const (@{const_name All}, _) $ Abs (x, T, t)) =
   408     apfst (cons (@{const_name All}, (x, T))) (strip_quantifiers t)
   409   | strip_quantifiers t = ([], t)
   410 
   411 fun contains_existentials t = exists (fn (Q, _) => Q = @{const_name Ex}) (fst (strip_quantifiers t))
   412 
   413 fun mk_property qs t =
   414   let
   415     fun enclose (@{const_name Ex}, (x, T)) t =
   416         Const (@{const_name Quickcheck_Narrowing.exists}, (T --> @{typ property}) --> @{typ property})
   417           $ Abs (x, T, t)
   418       | enclose (@{const_name All}, (x, T)) t =
   419         Const (@{const_name Quickcheck_Narrowing.all}, (T --> @{typ property}) --> @{typ property})
   420           $ Abs (x, T, t)
   421   in
   422     fold_rev enclose qs (@{term Quickcheck_Narrowing.Property} $ t)
   423   end
   424 
   425 fun mk_case_term ctxt p ((@{const_name Ex}, (x, T)) :: qs') (Existential_Counterexample cs) =
   426     Case_Translation.make_case ctxt Case_Translation.Quiet Name.context (Free (x, T)) (map (fn (t, c) =>
   427       (t, mk_case_term ctxt (p - 1) qs' c)) cs)
   428   | mk_case_term ctxt p ((@{const_name All}, _) :: qs') (Universal_Counterexample (t, c)) =
   429     if p = 0 then t else mk_case_term ctxt (p - 1) qs' c
   430 
   431 val post_process = (perhaps (try Quickcheck_Common.post_process_term)) o eval_finite_functions
   432 
   433 fun mk_terms ctxt qs result =
   434   let
   435     val
   436       ps = filter (fn (_, (@{const_name All}, _)) => true | _ => false) (map_index I qs)
   437     in
   438       map (fn (p, (_, (x, _))) => (x, mk_case_term ctxt p qs result)) ps
   439       |> map (apsnd post_process)
   440     end
   441 
   442 fun test_term ctxt catch_code_errors (t, _) =
   443   let
   444     fun dest_result (Quickcheck.Result r) = r
   445     val opts =
   446       ((Config.get ctxt Quickcheck.genuine_only,
   447        (Config.get ctxt Quickcheck.quiet, Config.get ctxt Quickcheck.verbose)),
   448         Config.get ctxt Quickcheck.size)
   449     val thy = Proof_Context.theory_of ctxt
   450     val t' = fold_rev (fn (x, T) => fn t => HOLogic.mk_all (x, T, t)) (Term.add_frees t []) t
   451     val pnf_t = make_pnf_term thy t'
   452   in
   453     if Config.get ctxt allow_existentials andalso contains_existentials pnf_t then
   454       let
   455         fun wrap f (qs, t) =
   456           let val (qs1, qs2) = split_list qs in
   457           apfst (map2 pair qs1) (f (qs2, t)) end
   458         val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
   459         val (qs, prop_t) = finitize (strip_quantifiers pnf_t)
   460         val act = if catch_code_errors then try else (fn f => SOME o f)
   461         val execute =
   462           dynamic_value_strict (true, opts)
   463             ((K true, fn _ => error ""),
   464               (get_existential_counterexample, put_existential_counterexample,
   465                 "Narrowing_Generators.put_existential_counterexample"))
   466             ctxt (apfst o Option.map o map_counterexample)
   467       in
   468         case act execute (mk_property qs prop_t) of
   469           SOME (counterexample, result) => Quickcheck.Result
   470             {counterexample = Option.map (pair true o mk_terms ctxt qs) counterexample,
   471             evaluation_terms = Option.map (K []) counterexample,
   472             timings = #timings (dest_result result), reports = #reports (dest_result result)}
   473         | NONE =>
   474           (Quickcheck.message ctxt "Conjecture is not executable with Quickcheck-narrowing";
   475            Quickcheck.empty_result)
   476       end
   477     else
   478       let
   479         val frees = Term.add_frees t []
   480         val t' = fold_rev absfree frees t
   481         fun wrap f t = uncurry (fold_rev Term.abs) (f (strip_abs t))
   482         val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
   483         fun ensure_testable t =
   484           Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
   485         fun is_genuine (SOME (true, _)) = true
   486           | is_genuine _ = false
   487         val counterexample_of = Option.map (apsnd (curry (op ~~) (map fst frees) o map post_process))
   488         val act = if catch_code_errors then try else (fn f => SOME o f)
   489         val execute =
   490           dynamic_value_strict (false, opts)
   491             ((is_genuine, counterexample_of),
   492               (get_counterexample, put_counterexample,
   493                 "Narrowing_Generators.put_counterexample"))
   494             ctxt (apfst o Option.map o apsnd o map)
   495       in
   496         case act execute (ensure_testable (finitize t')) of
   497           SOME (counterexample, result) =>
   498             Quickcheck.Result
   499              {counterexample = counterexample_of counterexample,
   500               evaluation_terms = Option.map (K []) counterexample,
   501               timings = #timings (dest_result result),
   502               reports = #reports (dest_result result)}
   503         | NONE =>
   504           (Quickcheck.message ctxt "Conjecture is not executable with Quickcheck-narrowing";
   505            Quickcheck.empty_result)
   506       end
   507   end;
   508 
   509 fun test_goals ctxt catch_code_errors insts goals =
   510   if (not (getenv "ISABELLE_GHC" = "")) then
   511     let
   512       val _ = Quickcheck.message ctxt "Testing conjecture with Quickcheck-narrowing...";
   513       val correct_inst_goals = Quickcheck_Common.instantiate_goals ctxt insts goals
   514     in
   515       Quickcheck_Common.collect_results (test_term ctxt catch_code_errors)
   516         (maps (map snd) correct_inst_goals) []
   517     end
   518   else
   519     (if Config.get ctxt Quickcheck.quiet then () else writeln
   520       ("Environment variable ISABELLE_GHC is not set. To use narrowing-based quickcheck, please set "
   521         ^ "this variable to your GHC Haskell compiler in your settings file. "
   522         ^ "To deactivate narrowing-based quickcheck, set quickcheck_narrowing_active to false.");
   523       [Quickcheck.empty_result])
   524 
   525 (* setup *)
   526 
   527 val active = Attrib.setup_config_bool @{binding quickcheck_narrowing_active} (K false);
   528 
   529 val _ =
   530   Theory.setup
   531    (Code.datatype_interpretation ensure_partial_term_of
   532     #> Code.datatype_interpretation ensure_partial_term_of_code
   533     #> Quickcheck_Common.datatype_interpretation @{plugin quickcheck_narrowing}
   534       (@{sort narrowing}, instantiate_narrowing_datatype)
   535     #> Context.theory_map (Quickcheck.add_tester ("narrowing", (active, test_goals))));
   536 
   537 end;