src/HOL/Tools/Quickcheck/narrowing_generators.ML
author bulwahn
Wed Nov 09 11:35:09 2011 +0100 (2011-11-09)
changeset 45420 d17556b9a89b
parent 45419 10ba32c347b0
child 45685 e2e928af750b
permissions -rw-r--r--
more precise messages with the tester's name in quickcheck; tuned
     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 active : bool Config.T
    13   val test_term: Proof.context -> term * term list -> Quickcheck.result
    14   datatype counterexample = Universal_Counterexample of (term * counterexample)
    15     | Existential_Counterexample of (term * counterexample) list
    16     | Empty_Assignment
    17   val put_counterexample: (unit -> 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 
    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       $ Const ("TYPE", Term.itselfT 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 (K (Class.intro_classes_tac []))
    58   end;
    59 
    60 fun ensure_partial_term_of (tyco, (raw_vs, _)) thy =
    61   let
    62     val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort partial_term_of})
    63       andalso can (Sorts.mg_domain (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.Ctr"} $ HOLogic.mk_number @{typ code_int} 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.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.ctyp_of thy ty;
    81   in
    82     @{thm partial_term_of_anything}
    83     |> Drule.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.cterm_of thy o Logic.unvarify_types_global o Logic.varify_global)
    98         [Free ("ty", Term.itselfT ty), @{term "Quickcheck_Narrowing.Var p tt"},
    99           @{term "Code_Evaluation.Free (STR ''_'')"} $ HOLogic.mk_typerep ty];
   100     val var_eq =
   101       @{thm partial_term_of_anything}
   102       |> Drule.instantiate' [SOME (Thm.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 = can (Sorts.mg_domain (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 Quickcheck_Narrowing.code_int} --> Type (@{type_name Quickcheck_Narrowing.cons}, [T])
   127 
   128 fun mk_empty T = Const (@{const_name Quickcheck_Narrowing.empty}, narrowingT T)
   129 
   130 fun mk_cons c T = Const (@{const_name Quickcheck_Narrowing.cons}, T --> narrowingT T) $ Const (c, T)
   131 
   132 fun mk_apply (T, t) (U, u) =
   133   let
   134     val (_, U') = dest_funT U
   135   in
   136     (U', Const (@{const_name Quickcheck_Narrowing.apply},
   137       narrowingT U --> narrowingT T --> narrowingT U') $ u $ t)
   138   end
   139   
   140 fun mk_sum (t, u) =
   141   let
   142     val T = fastype_of t
   143   in
   144     Const (@{const_name Quickcheck_Narrowing.sum}, T --> T --> T) $ t $ u
   145   end
   146 
   147 (** deriving narrowing instances **)
   148 
   149 fun mk_equations descr vs tycos narrowings (Ts, Us) =
   150   let
   151     fun mk_call T =
   152       (T, Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, narrowingT T))
   153     fun mk_aux_call fTs (k, _) (tyco, Ts) =
   154       let
   155         val T = Type (tyco, Ts)
   156         val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
   157       in
   158         (T, nth narrowings k)
   159       end
   160     fun mk_consexpr simpleT (c, xs) =
   161       let
   162         val Ts = map fst xs
   163       in snd (fold mk_apply xs (Ts ---> simpleT, mk_cons c (Ts ---> simpleT))) end
   164     fun mk_rhs exprs = foldr1 mk_sum exprs
   165     val rhss =
   166       Datatype_Aux.interpret_construction descr vs
   167         { atyp = mk_call, dtyp = mk_aux_call }
   168       |> (map o apfst) Type
   169       |> map (fn (T, cs) => map (mk_consexpr T) cs)
   170       |> map mk_rhs
   171     val lhss = narrowings
   172     val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
   173   in
   174     eqs
   175   end
   176     
   177 fun contains_recursive_type_under_function_types xs =
   178   exists (fn (_, (_, _, cs)) => cs |> exists (snd #> exists (fn dT =>
   179     (case Datatype_Aux.strip_dtyp dT of (_ :: _, Datatype.DtRec _) => true | _ => false)))) xs
   180 
   181 fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   182   let
   183     val _ = Datatype_Aux.message config "Creating narrowing generators ...";
   184     val narrowingsN = map (prefix (narrowingN ^ "_")) (names @ auxnames);
   185   in
   186     if not (contains_recursive_type_under_function_types descr) then
   187       thy
   188       |> Class.instantiation (tycos, vs, @{sort narrowing})
   189       |> Quickcheck_Common.define_functions
   190         (fn narrowings => mk_equations descr vs tycos narrowings (Ts, Us), NONE)
   191         prfx [] narrowingsN (map narrowingT (Ts @ Us))
   192       |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   193     else
   194       thy
   195   end;
   196 
   197 (* testing framework *)
   198 
   199 val target = "Haskell_Quickcheck"
   200 
   201 (** invocation of Haskell interpreter **)
   202 
   203 val narrowing_engine =
   204   Context.>>> (Context.map_theory_result
   205     (Thy_Load.use_file (Path.explode "Tools/Quickcheck/Narrowing_Engine.hs")))
   206 
   207 val pnf_narrowing_engine =
   208   Context.>>> (Context.map_theory_result
   209     (Thy_Load.use_file (Path.explode "Tools/Quickcheck/PNF_Narrowing_Engine.hs")))
   210 
   211 fun exec verbose code =
   212   ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code)
   213 
   214 fun with_overlord_dir name f =
   215   let
   216     val path =
   217       Path.append (Path.explode "$ISABELLE_HOME_USER") (Path.basic (name ^ serial_string ()))
   218     val _ = Isabelle_System.mkdirs path;
   219   in Exn.release (Exn.capture f path) end;
   220 
   221 fun elapsed_time description e =
   222   let val ({elapsed, ...}, result) = Timing.timing e ()
   223   in (result, (description, Time.toMilliseconds elapsed)) end
   224   
   225 fun value (contains_existentials, (quiet, size)) ctxt cookie (code, value_name) =
   226   let
   227     val (get, put, put_ml) = cookie
   228     fun message s = if quiet then () else Output.urgent_message s
   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 with_tmp_dir =
   235       if Config.get ctxt overlord then with_overlord_dir else Isabelle_System.with_tmp_dir 
   236     fun run in_path = 
   237       let
   238         val code_file = Path.append in_path (Path.basic "Generated_Code.hs")
   239         val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs")
   240         val main_file = Path.append in_path (Path.basic "Main.hs")
   241         val main = "module Main where {\n\n" ^
   242           "import System;\n" ^
   243           "import Narrowing_Engine;\n" ^
   244           "import Generated_Code;\n\n" ^
   245           "main = getArgs >>= \\[size] -> Narrowing_Engine.depthCheck (read size) (Generated_Code.value ())\n\n" ^
   246           "}\n"
   247         val code' = prefix "module Generated_Code where {\n\ndata Typerep = Typerep String [Typerep];\n"
   248           (unprefix "module Generated_Code where {" code)
   249         val _ = File.write code_file code'
   250         val _ = File.write narrowing_engine_file
   251           (if contains_existentials then pnf_narrowing_engine else narrowing_engine)
   252         val _ = File.write main_file main
   253         val executable = File.shell_path (Path.append in_path (Path.basic "isabelle_quickcheck_narrowing"))
   254         val cmd = "exec \"$ISABELLE_GHC\" " ^ Code_Haskell.language_params ^ " " ^
   255           (space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^
   256           " -o " ^ executable ^ ";"
   257         val (result, compilation_time) =
   258           elapsed_time "Haskell compilation" (fn () => Isabelle_System.bash cmd) 
   259         val _ = Quickcheck.add_timing compilation_time current_result
   260         val _ = if Isabelle_System.bash cmd <> 0 then error "Compilation with GHC failed" else ()
   261         fun with_size k =
   262           if k > size then
   263             (NONE, !current_result)
   264           else
   265             let
   266               val _ = message ("[Quickcheck-narrowing] Test data size: " ^ string_of_int k)
   267               val _ = current_size := k
   268               val ((response, _), timing) = elapsed_time ("execution of size " ^ string_of_int k)
   269                 (fn () => Isabelle_System.bash_output (executable ^ " " ^ string_of_int k))
   270               val _ = Quickcheck.add_timing timing current_result
   271             in
   272               if response = "NONE\n" then
   273                 with_size (k + 1)
   274               else
   275                 let
   276                   val output_value = the_default "NONE"
   277                     (try (snd o split_last o filter_out (fn s => s = "") o split_lines) response)
   278                     |> translate_string (fn s => if s = "\\" then "\\\\" else s)
   279                   val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
   280                     ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
   281                   val ctxt' = ctxt
   282                     |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
   283                     |> Context.proof_map (exec false ml_code);
   284                 in (get ctxt' (), !current_result) end
   285             end 
   286       in with_size 0 end
   287   in
   288     with_tmp_dir tmp_prefix run
   289   end;
   290 
   291 fun dynamic_value_strict opts cookie thy postproc t =
   292   let
   293     val ctxt = Proof_Context.init_global thy
   294     fun evaluator naming program ((_, vs_ty), t) deps = Exn.interruptible_capture (value opts ctxt cookie)
   295       (Code_Target.evaluator thy target naming program deps (vs_ty, t));    
   296   in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
   297 
   298 (** counterexample generator **)
   299   
   300 structure Counterexample = Proof_Data
   301 (
   302   type T = unit -> term list option
   303   fun init _ () = error "Counterexample"
   304 )
   305 
   306 datatype counterexample = Universal_Counterexample of (term * counterexample)
   307   | Existential_Counterexample of (term * counterexample) list
   308   | Empty_Assignment
   309   
   310 fun map_counterexample f Empty_Assignment = Empty_Assignment
   311   | map_counterexample f (Universal_Counterexample (t, c)) =
   312       Universal_Counterexample (f t, map_counterexample f c)
   313   | map_counterexample f (Existential_Counterexample cs) =
   314       Existential_Counterexample (map (fn (t, c) => (f t, map_counterexample f c)) cs)
   315 
   316 structure Existential_Counterexample = Proof_Data
   317 (
   318   type T = unit -> counterexample option
   319   fun init _ () = error "Counterexample"
   320 )
   321 
   322 val put_existential_counterexample = Existential_Counterexample.put
   323 
   324 val put_counterexample = Counterexample.put
   325 
   326 fun finitize_functions (xTs, t) =
   327   let
   328     val (names, boundTs) = split_list xTs
   329     fun mk_eval_ffun dT rT =
   330       Const (@{const_name "Quickcheck_Narrowing.eval_ffun"}, 
   331         Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT]) --> dT --> rT)
   332     fun mk_eval_cfun dT rT =
   333       Const (@{const_name "Quickcheck_Narrowing.eval_cfun"}, 
   334         Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT]) --> dT --> rT)
   335     fun eval_function (T as Type (@{type_name fun}, [dT, rT])) =
   336       let
   337         val (rt', rT') = eval_function rT
   338       in
   339         case dT of
   340           Type (@{type_name fun}, _) =>
   341             (fn t => absdummy dT (rt' (mk_eval_cfun dT rT' $ incr_boundvars 1 t $ Bound 0)),
   342               Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT']))
   343         | _ =>
   344             (fn t => absdummy dT (rt' (mk_eval_ffun dT rT' $ incr_boundvars 1 t $ Bound 0)),
   345               Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT']))
   346       end
   347       | eval_function T = (I, T)
   348     val (tt, boundTs') = split_list (map eval_function boundTs)
   349     val t' = subst_bounds (map2 (fn f => fn x => f x) (rev tt) (map_index (Bound o fst) boundTs), t)
   350   in
   351     (names ~~ boundTs', t')
   352   end
   353 
   354 fun dest_ffun (Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT])) = (dT, rT)
   355 
   356 fun eval_finite_functions (Const (@{const_name "Quickcheck_Narrowing.ffun.Constant"}, T) $ value) =
   357     absdummy (fst (dest_ffun (body_type T))) (eval_finite_functions value)
   358   | eval_finite_functions (Const (@{const_name "Quickcheck_Narrowing.ffun.Update"}, T) $ a $ b $ f) =
   359     let
   360       val (T1, T2) = dest_ffun (body_type T)
   361     in
   362       Quickcheck_Common.mk_fun_upd T1 T2
   363         (eval_finite_functions a, eval_finite_functions b) (eval_finite_functions f)
   364     end
   365   | eval_finite_functions t = t
   366 
   367 (** tester **)
   368 
   369 val rewrs =
   370     map (swap o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
   371       (@{thms all_simps} @ @{thms ex_simps})
   372     @ map (HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
   373         [@{thm iff_conv_conj_imp}, @{thm not_ex}, @{thm not_all}]
   374 
   375 fun make_pnf_term thy t = Pattern.rewrite_term thy rewrs [] t
   376 
   377 fun strip_quantifiers (Const (@{const_name Ex}, _) $ Abs (x, T, t)) =
   378     apfst (cons (@{const_name Ex}, (x, T))) (strip_quantifiers t)
   379   | strip_quantifiers (Const (@{const_name All}, _) $ Abs (x, T, t)) =
   380     apfst (cons (@{const_name All}, (x, T))) (strip_quantifiers t)
   381   | strip_quantifiers t = ([], t)
   382 
   383 fun contains_existentials t = exists (fn (Q, _) => Q = @{const_name Ex}) (fst (strip_quantifiers t))
   384 
   385 fun mk_property qs t =
   386   let
   387     fun enclose (@{const_name Ex}, (x, T)) t =
   388         Const (@{const_name Quickcheck_Narrowing.exists}, (T --> @{typ property}) --> @{typ property})
   389           $ Abs (x, T, t)
   390       | enclose (@{const_name All}, (x, T)) t =
   391         Const (@{const_name Quickcheck_Narrowing.all}, (T --> @{typ property}) --> @{typ property})
   392           $ Abs (x, T, t)
   393   in
   394     fold_rev enclose qs (@{term Quickcheck_Narrowing.Property} $ t)
   395   end
   396 
   397 fun mk_case_term ctxt p ((@{const_name Ex}, (x, T)) :: qs') (Existential_Counterexample cs) =
   398     Datatype.make_case ctxt Datatype_Case.Quiet [] (Free (x, T)) (map (fn (t, c) =>
   399       (t, mk_case_term ctxt (p - 1) qs' c)) cs)
   400   | mk_case_term ctxt p ((@{const_name All}, (x, T)) :: qs') (Universal_Counterexample (t, c)) =
   401     if p = 0 then t else mk_case_term ctxt (p - 1) qs' c
   402 
   403 val post_process = (perhaps (try Quickcheck_Common.post_process_term)) o eval_finite_functions
   404 
   405 fun mk_terms ctxt qs result =
   406   let
   407     val
   408       ps = filter (fn (_, (@{const_name All}, _)) => true | _ => false) (map_index I qs)
   409     in
   410       map (fn (p, (_, (x, T))) => (x, mk_case_term ctxt p qs result)) ps
   411       |> map (apsnd post_process)
   412     end
   413   
   414 fun test_term ctxt (t, eval_terms) =
   415   let
   416     fun dest_result (Quickcheck.Result r) = r 
   417     val opts =
   418       (Config.get ctxt Quickcheck.quiet, Config.get ctxt Quickcheck.size)
   419     val thy = Proof_Context.theory_of ctxt
   420     val t' = fold_rev (fn (x, T) => fn t => HOLogic.mk_all (x, T, t)) (Term.add_frees t []) t
   421     val pnf_t = make_pnf_term thy t'
   422   in
   423     if Config.get ctxt allow_existentials andalso contains_existentials pnf_t then
   424       let
   425         fun wrap f (qs, t) =
   426           let val (qs1, qs2) = split_list qs in
   427           apfst (map2 pair qs1) (f (qs2, t)) end
   428         val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
   429         val (qs, prop_t) = finitize (strip_quantifiers pnf_t)
   430         val (counterexample, result) = dynamic_value_strict (true, opts)
   431           (Existential_Counterexample.get, Existential_Counterexample.put,
   432             "Narrowing_Generators.put_existential_counterexample")
   433           thy (apfst o Option.map o map_counterexample) (mk_property qs prop_t)
   434       in
   435         Quickcheck.Result
   436          {counterexample = Option.map (mk_terms ctxt qs) counterexample,
   437           evaluation_terms = Option.map (K []) counterexample,
   438           timings = #timings (dest_result result), reports = #reports (dest_result result)}
   439       end
   440     else
   441       let
   442         val t' = fold_rev absfree (Term.add_frees t []) t
   443         fun wrap f t = list_abs (f (strip_abs t))
   444         val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
   445         fun ensure_testable t =
   446           Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
   447         val (counterexample, result) = dynamic_value_strict (false, opts)
   448           (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
   449           thy (apfst o Option.map o map) (ensure_testable (finitize t'))
   450       in
   451         Quickcheck.Result
   452          {counterexample =
   453            Option.map (((curry (op ~~)) (Term.add_free_names t [])) o map post_process) counterexample,
   454           evaluation_terms = Option.map (K []) counterexample,
   455           timings = #timings (dest_result result), reports = #reports (dest_result result)}
   456       end
   457   end;
   458 
   459 fun test_goals ctxt _ insts goals =
   460   if (not (getenv "ISABELLE_GHC" = "")) then
   461     let
   462       val correct_inst_goals = Quickcheck_Common.instantiate_goals ctxt insts goals
   463     in
   464       Quickcheck_Common.collect_results (test_term ctxt) (maps (map snd) correct_inst_goals) []
   465     end
   466   else
   467     (if Config.get ctxt Quickcheck.quiet then () else Output.urgent_message
   468       ("Environment variable ISABELLE_GHC is not set. To use narrowing-based quickcheck, please set "
   469         ^ "this variable to your GHC Haskell compiler in your settings file. "
   470         ^ "To deactivate narrowing-based quickcheck, set quickcheck_narrowing_active to false.");
   471       [Quickcheck.empty_result])
   472 
   473 (* setup *)
   474 
   475 val active = Attrib.setup_config_bool @{binding quickcheck_narrowing_active} (K true);
   476 
   477 val setup =
   478   Code.datatype_interpretation ensure_partial_term_of
   479   #> Code.datatype_interpretation ensure_partial_term_of_code
   480   #> Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
   481     (((@{sort typerep}, @{sort term_of}), @{sort narrowing}), instantiate_narrowing_datatype))
   482   #> Context.theory_map (Quickcheck.add_tester ("narrowing", (active, test_goals)))
   483     
   484 end;