src/HOL/Tools/Quickcheck/narrowing_generators.ML
author wenzelm
Fri Jul 08 14:37:19 2011 +0200 (2011-07-08)
changeset 43702 24fb44c1086a
parent 43619 3803869014aa
child 43850 7f2cbc713344
permissions -rw-r--r--
more abstract Thy_Load.load_file/use_file for external theory resources;
prefer Boogie_Loader.parse_b2i on already loaded text, bypassing former File.fold_lines optimization;
     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 test_term: Proof.context -> bool * bool -> term * term list -> Quickcheck.result
    13   datatype counterexample = Universal_Counterexample of (term * counterexample)
    14     | Existential_Counterexample of (term * counterexample) list
    15     | Empty_Assignment
    16   val put_counterexample: (unit -> term list option) -> Proof.context -> Proof.context
    17   val put_existential_counterexample : (unit -> counterexample option) -> Proof.context -> Proof.context
    18   val setup: theory -> theory
    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 
    30 (* partial_term_of instances *)
    31 
    32 fun mk_partial_term_of (x, T) =
    33   Const (@{const_name Quickcheck_Narrowing.partial_term_of_class.partial_term_of},
    34     Term.itselfT T --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
    35       $ Const ("TYPE", Term.itselfT T) $ x
    36 
    37 (** formal definition **)
    38 
    39 fun add_partial_term_of tyco raw_vs thy =
    40   let
    41     val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
    42     val ty = Type (tyco, map TFree vs);
    43     val lhs = Const (@{const_name partial_term_of},
    44         Term.itselfT ty --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
    45       $ Free ("x", Term.itselfT ty) $ Free ("t", @{typ narrowing_term});
    46     val rhs = @{term "undefined :: Code_Evaluation.term"};
    47     val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
    48     fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
    49       o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
    50   in
    51     thy
    52     |> Class.instantiation ([tyco], vs, @{sort partial_term_of})
    53     |> `(fn lthy => Syntax.check_term lthy eq)
    54     |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
    55     |> snd
    56     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
    57   end;
    58 
    59 fun ensure_partial_term_of (tyco, (raw_vs, _)) thy =
    60   let
    61     val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort partial_term_of})
    62       andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
    63   in if need_inst then add_partial_term_of tyco raw_vs thy else thy end;
    64 
    65 
    66 (** code equations for datatypes **)
    67 
    68 fun mk_partial_term_of_eq thy ty (i, (c, (_, tys))) =
    69   let
    70     val frees = map Free (Name.invent_names Name.context "a" (map (K @{typ narrowing_term}) tys))
    71     val narrowing_term = @{term "Quickcheck_Narrowing.Ctr"} $ HOLogic.mk_number @{typ code_int} i
    72       $ (HOLogic.mk_list @{typ narrowing_term} (rev frees))
    73     val rhs = fold (fn u => fn t => @{term "Code_Evaluation.App"} $ t $ u)
    74         (map mk_partial_term_of (frees ~~ tys))
    75         (@{term "Code_Evaluation.Const"} $ HOLogic.mk_literal c $ HOLogic.mk_typerep (tys ---> ty))
    76     val insts =
    77       map (SOME o Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
    78         [Free ("ty", Term.itselfT ty), narrowing_term, rhs]
    79     val cty = Thm.ctyp_of thy ty;
    80   in
    81     @{thm partial_term_of_anything}
    82     |> Drule.instantiate' [SOME cty] insts
    83     |> Thm.varifyT_global
    84   end
    85 
    86 fun add_partial_term_of_code tyco raw_vs raw_cs thy =
    87   let
    88     val algebra = Sign.classes_of thy;
    89     val vs = map (fn (v, sort) =>
    90       (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
    91     val ty = Type (tyco, map TFree vs);
    92     val cs = (map o apsnd o apsnd o map o map_atyps)
    93       (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
    94     val const = AxClass.param_of_inst thy (@{const_name partial_term_of}, tyco);
    95     val var_insts = map (SOME o Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
    96         [Free ("ty", Term.itselfT ty), @{term "Quickcheck_Narrowing.Var p tt"},
    97           @{term "Code_Evaluation.Free (STR ''_'')"} $ HOLogic.mk_typerep ty]
    98     val var_eq =
    99       @{thm partial_term_of_anything}
   100       |> Drule.instantiate' [SOME (Thm.ctyp_of thy ty)] var_insts
   101       |> Thm.varifyT_global
   102     val eqs = var_eq :: map_index (mk_partial_term_of_eq thy ty) cs;
   103  in
   104     thy
   105     |> Code.del_eqns const
   106     |> fold Code.add_eqn eqs
   107   end;
   108 
   109 fun ensure_partial_term_of_code (tyco, (raw_vs, cs)) thy =
   110   let
   111     val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort partial_term_of};
   112   in if has_inst then add_partial_term_of_code tyco raw_vs cs thy else thy end;
   113 
   114 
   115 (* narrowing generators *)
   116 
   117 (** narrowing specific names and types **)
   118 
   119 exception FUNCTION_TYPE;
   120 
   121 val narrowingN = "narrowing";
   122 
   123 fun narrowingT T =
   124   @{typ Quickcheck_Narrowing.code_int} --> Type (@{type_name Quickcheck_Narrowing.cons}, [T])
   125 
   126 fun mk_empty T = Const (@{const_name Quickcheck_Narrowing.empty}, narrowingT 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 tycos narrowings (Ts, Us) =
   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       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 Datatype_Aux.strip_dtyp dT of (_ :: _, Datatype.DtRec _) => true | _ => false)))) xs
   178 
   179 fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   180   let
   181     val _ = 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 tycos narrowings (Ts, Us), NONE)
   189         prfx [] narrowingsN (map narrowingT (Ts @ Us))
   190       |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   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   Context.>>> (Context.map_theory_result
   203     (Thy_Load.use_file (Path.explode "Tools/Quickcheck/Narrowing_Engine.hs")))
   204 
   205 val pnf_narrowing_engine =
   206   Context.>>> (Context.map_theory_result
   207     (Thy_Load.use_file (Path.explode "Tools/Quickcheck/PNF_Narrowing_Engine.hs")))
   208 
   209 fun exec verbose code =
   210   ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose 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, opts) ctxt cookie (code, value_name) =
   224   let
   225     val (limit_time, is_interactive, timeout, quiet, size) = opts
   226     val (get, put, put_ml) = cookie
   227     fun message s = if quiet then () else Output.urgent_message s
   228     val current_size = Unsynchronized.ref 0
   229     val current_result = Unsynchronized.ref Quickcheck.empty_result 
   230     fun excipit () =
   231       "Quickcheck ran out of time while testing at size " ^ string_of_int (!current_size)
   232     val tmp_prefix = "Quickcheck_Narrowing"
   233     val with_tmp_dir =
   234       if Config.get ctxt overlord then with_overlord_dir else Isabelle_System.with_tmp_dir 
   235     fun run in_path = 
   236       let
   237         val code_file = Path.append in_path (Path.basic "Code.hs")
   238         val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs")
   239         val main_file = Path.append in_path (Path.basic "Main.hs")
   240         val main = "module Main where {\n\n" ^
   241           "import System;\n" ^
   242           "import Narrowing_Engine;\n" ^
   243           "import Code;\n\n" ^
   244           "main = getArgs >>= \\[size] -> Narrowing_Engine.depthCheck (read size) (Code.value ())\n\n" ^
   245           "}\n"
   246         val code' = prefix "module Code where {\n\ndata Typerep = Typerep String [Typerep];\n"
   247           (unprefix "module Code where {" code)
   248         val _ = File.write code_file code'
   249         val _ = File.write narrowing_engine_file
   250           (if contains_existentials then pnf_narrowing_engine else narrowing_engine)
   251         val _ = File.write main_file main
   252         val executable = File.shell_path (Path.append in_path (Path.basic "isabelle_quickcheck_narrowing"))
   253         val cmd = "exec \"$ISABELLE_GHC\" -fglasgow-exts " ^
   254           (space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^
   255           " -o " ^ executable ^ ";"
   256         val (result, compilation_time) = elapsed_time "Haskell compilation" (fn () => bash cmd) 
   257         val _ = Quickcheck.add_timing compilation_time current_result
   258         val _ = if bash cmd <> 0 then error "Compilation with GHC failed" else ()
   259         fun with_size k =
   260           if k > size then
   261             (NONE, !current_result)
   262           else
   263             let
   264               val _ = message ("Test data size: " ^ string_of_int k)
   265               val _ = current_size := k
   266               val ((response, _), timing) = elapsed_time ("execution of size " ^ string_of_int k)
   267                 (fn () => bash_output (executable ^ " " ^ string_of_int k))
   268               val _ = Quickcheck.add_timing timing current_result
   269             in
   270               if response = "NONE\n" then
   271                 with_size (k + 1)
   272               else
   273                 let
   274                   val output_value = the_default "NONE"
   275                     (try (snd o split_last o filter_out (fn s => s = "") o split_lines) response)
   276                     |> translate_string (fn s => if s = "\\" then "\\\\" else s)
   277                   val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
   278                     ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
   279                   val ctxt' = ctxt
   280                     |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
   281                     |> Context.proof_map (exec false ml_code);
   282                 in (get ctxt' (), !current_result) end
   283             end 
   284       in with_size 0 end
   285   in
   286     Quickcheck.limit timeout (limit_time, is_interactive) 
   287       (fn () => with_tmp_dir tmp_prefix run)
   288       (fn () => (message (excipit ()); (NONE, !current_result))) ()
   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         | _ => (fn t => absdummy (dT, rt' (mk_eval_ffun dT rT' $ incr_boundvars 1 t $ Bound 0)),
   344             Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT']))
   345       end
   346       | eval_function T = (I, T)
   347     val (tt, boundTs') = split_list (map eval_function boundTs)
   348     val t' = subst_bounds (map2 (fn f => fn x => f x) (rev tt) (map_index (Bound o fst) boundTs), t)
   349   in
   350     (names ~~ boundTs', t')
   351   end
   352 
   353 (** tester **)
   354 
   355 val rewrs =
   356     map (swap o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
   357       (@{thms all_simps} @ @{thms ex_simps})
   358     @ map (HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
   359         [@{thm iff_conv_conj_imp}, @{thm not_ex}, @{thm not_all}]
   360 
   361 fun make_pnf_term thy t = Pattern.rewrite_term thy rewrs [] t
   362 
   363 fun strip_quantifiers (Const (@{const_name Ex}, _) $ Abs (x, T, t)) =
   364     apfst (cons (@{const_name Ex}, (x, T))) (strip_quantifiers t)
   365   | strip_quantifiers (Const (@{const_name All}, _) $ Abs (x, T, t)) =
   366     apfst (cons (@{const_name All}, (x, T))) (strip_quantifiers t)
   367   | strip_quantifiers t = ([], t)
   368 
   369 fun contains_existentials t = exists (fn (Q, _) => Q = @{const_name Ex}) (fst (strip_quantifiers t))
   370 
   371 fun mk_property qs t =
   372   let
   373     fun enclose (@{const_name Ex}, (x, T)) t =
   374         Const (@{const_name Quickcheck_Narrowing.exists}, (T --> @{typ property}) --> @{typ property})
   375           $ Abs (x, T, t)
   376       | enclose (@{const_name All}, (x, T)) t =
   377         Const (@{const_name Quickcheck_Narrowing.all}, (T --> @{typ property}) --> @{typ property})
   378           $ Abs (x, T, t)
   379   in
   380     fold_rev enclose qs (@{term Quickcheck_Narrowing.Property} $
   381       (list_comb (t , map Bound (((length qs) - 1) downto 0))))
   382   end
   383 
   384 fun mk_case_term ctxt p ((@{const_name Ex}, (x, T)) :: qs') (Existential_Counterexample cs) =
   385     Datatype.make_case ctxt Datatype_Case.Quiet [] (Free (x, T)) (map (fn (t, c) =>
   386       (t, mk_case_term ctxt (p - 1) qs' c)) cs)
   387   | mk_case_term ctxt p ((@{const_name All}, (x, T)) :: qs') (Universal_Counterexample (t, c)) =
   388     if p = 0 then t else mk_case_term ctxt (p - 1) qs' c
   389 
   390 fun mk_terms ctxt qs result =
   391   let
   392     val
   393       ps = filter (fn (_, (@{const_name All}, _)) => true | _ => false) (map_index I qs)
   394     in
   395       map (fn (p, (_, (x, T))) => (x, mk_case_term ctxt p qs result)) ps
   396     end
   397   
   398 fun test_term ctxt (limit_time, is_interactive) (t, eval_terms) =
   399   let
   400     fun dest_result (Quickcheck.Result r) = r 
   401     val opts =
   402       (limit_time, is_interactive, seconds (Config.get ctxt Quickcheck.timeout),
   403         Config.get ctxt Quickcheck.quiet, Config.get ctxt Quickcheck.size)
   404     val thy = Proof_Context.theory_of ctxt
   405     val t' = fold_rev (fn (x, T) => fn t => HOLogic.mk_all (x, T, t)) (Term.add_frees t []) t
   406     val pnf_t = make_pnf_term thy t'
   407   in
   408     if Config.get ctxt allow_existentials andalso contains_existentials pnf_t then
   409       let
   410         fun wrap f (qs, t) =
   411           let val (qs1, qs2) = split_list qs in
   412           apfst (map2 pair qs1) (f (qs2, t)) end
   413         val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
   414         val (qs, prop_t) = finitize (strip_quantifiers pnf_t)
   415         val prop_term = fold_rev (fn (_, (x, T)) => fn t => Abs (x, T, t)) qs prop_t
   416         (* FIXME proper naming convention for local_theory *)
   417         val ((prop_def, _), ctxt') =
   418           Local_Theory.define ((Binding.conceal @{binding test_property}, NoSyn),
   419             ((Binding.conceal Binding.empty, [Code.add_default_eqn_attrib]), prop_term)) ctxt
   420         val (prop_def', thy') = Local_Theory.exit_result_global Morphism.term (prop_def, ctxt') 
   421         val (counterexample, result) = dynamic_value_strict (true, opts)
   422           (Existential_Counterexample.get, Existential_Counterexample.put,
   423             "Narrowing_Generators.put_existential_counterexample")
   424           thy' (apfst o Option.map o map_counterexample) (mk_property qs prop_def')
   425       in
   426         Quickcheck.Result
   427          {counterexample = Option.map (mk_terms ctxt' qs) counterexample,
   428           evaluation_terms = Option.map (K []) counterexample,
   429           timings = #timings (dest_result result), reports = #reports (dest_result result)}
   430       end
   431     else
   432       let
   433         val t' = Term.list_abs_free (Term.add_frees t [], t)
   434         fun wrap f t = list_abs (f (strip_abs t))
   435         val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
   436         fun ensure_testable t =
   437           Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
   438         val (counterexample, result) = dynamic_value_strict (false, opts)
   439           (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
   440           thy (apfst o Option.map o map) (ensure_testable (finitize t'))
   441       in
   442         Quickcheck.Result
   443          {counterexample = Option.map ((curry (op ~~)) (Term.add_free_names t [])) counterexample,
   444           evaluation_terms = Option.map (K []) counterexample,
   445           timings = #timings (dest_result result), reports = #reports (dest_result result)}
   446       end
   447   end;
   448 
   449 fun test_goals ctxt (limit_time, is_interactive) insts goals =
   450   if (not (getenv "ISABELLE_GHC" = "")) then
   451     let
   452       val correct_inst_goals = Quickcheck.instantiate_goals ctxt insts goals
   453     in
   454       Quickcheck.collect_results (test_term ctxt (limit_time, is_interactive)) (maps (map snd) correct_inst_goals) []
   455     end
   456   else
   457     (if Config.get ctxt Quickcheck.quiet then () else Output.urgent_message
   458       ("Environment variable ISABELLE_GHC is not set. To use narrowing-based quickcheck, please set "
   459         ^ "this variable to your GHC Haskell compiler in your settings file."); [Quickcheck.empty_result])
   460 
   461 (* setup *)
   462 
   463 val setup =
   464   Code.datatype_interpretation ensure_partial_term_of
   465   #> Code.datatype_interpretation ensure_partial_term_of_code
   466   #> Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
   467     (((@{sort typerep}, @{sort term_of}), @{sort narrowing}), instantiate_narrowing_datatype))
   468   #> Context.theory_map (Quickcheck.add_tester ("narrowing", test_goals))
   469     
   470 end;