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