src/HOL/Tools/Quickcheck/narrowing_generators.ML
author bulwahn
Tue May 31 15:45:27 2011 +0200 (2011-05-31)
changeset 43114 b9fca691addd
parent 43079 4022892a2f28
child 43237 8f5c3c6c2909
permissions -rw-r--r--
Quickcheck Narrowing only requires one compilation with GHC now
     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 test_term: Proof.context -> bool * bool -> term * term list -> Quickcheck.result
    10   val put_counterexample: (unit -> term list option) -> Proof.context -> Proof.context
    11   val finite_functions : bool Config.T
    12   val overlord : bool Config.T
    13   val setup: theory -> theory
    14 end;
    15 
    16 structure Narrowing_Generators : NARROWING_GENERATORS =
    17 struct
    18 
    19 (* configurations *)
    20 
    21 val finite_functions = Attrib.setup_config_bool @{binding quickcheck_finite_functions} (K true)
    22 val overlord = Attrib.setup_config_bool @{binding quickcheck_narrowing_overlord} (K false)
    23 
    24 (* partial_term_of instances *)
    25 
    26 fun mk_partial_term_of (x, T) =
    27   Const (@{const_name Quickcheck_Narrowing.partial_term_of_class.partial_term_of},
    28     Term.itselfT T --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
    29       $ Const ("TYPE", Term.itselfT T) $ x
    30 
    31 (** formal definition **)
    32 
    33 fun add_partial_term_of tyco raw_vs thy =
    34   let
    35     val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
    36     val ty = Type (tyco, map TFree vs);
    37     val lhs = Const (@{const_name partial_term_of},
    38         Term.itselfT ty --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
    39       $ Free ("x", Term.itselfT ty) $ Free ("t", @{typ narrowing_term});
    40     val rhs = @{term "undefined :: Code_Evaluation.term"};
    41     val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
    42     fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
    43       o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
    44   in
    45     thy
    46     |> Class.instantiation ([tyco], vs, @{sort partial_term_of})
    47     |> `(fn lthy => Syntax.check_term lthy eq)
    48     |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
    49     |> snd
    50     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
    51   end;
    52 
    53 fun ensure_partial_term_of (tyco, (raw_vs, _)) thy =
    54   let
    55     val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort partial_term_of})
    56       andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
    57   in if need_inst then add_partial_term_of tyco raw_vs thy else thy end;
    58 
    59 
    60 (** code equations for datatypes **)
    61 
    62 fun mk_partial_term_of_eq thy ty (i, (c, (_, tys))) =
    63   let
    64     val frees = map Free (Name.names Name.context "a" (map (K @{typ narrowing_term}) tys))
    65     val narrowing_term = @{term "Quickcheck_Narrowing.Ctr"} $ HOLogic.mk_number @{typ code_int} i
    66       $ (HOLogic.mk_list @{typ narrowing_term} (rev frees))
    67     val rhs = fold (fn u => fn t => @{term "Code_Evaluation.App"} $ t $ u)
    68         (map mk_partial_term_of (frees ~~ tys))
    69         (@{term "Code_Evaluation.Const"} $ HOLogic.mk_literal c $ HOLogic.mk_typerep (tys ---> ty))
    70     val insts =
    71       map (SOME o Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
    72         [Free ("ty", Term.itselfT ty), narrowing_term, rhs]
    73     val cty = Thm.ctyp_of thy ty;
    74   in
    75     @{thm partial_term_of_anything}
    76     |> Drule.instantiate' [SOME cty] insts
    77     |> Thm.varifyT_global
    78   end
    79 
    80 fun add_partial_term_of_code tyco raw_vs raw_cs thy =
    81   let
    82     val algebra = Sign.classes_of thy;
    83     val vs = map (fn (v, sort) =>
    84       (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
    85     val ty = Type (tyco, map TFree vs);
    86     val cs = (map o apsnd o apsnd o map o map_atyps)
    87       (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
    88     val const = AxClass.param_of_inst thy (@{const_name partial_term_of}, tyco);
    89     val var_insts = map (SOME o Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
    90         [Free ("ty", Term.itselfT ty), @{term "Quickcheck_Narrowing.Var p tt"},
    91           @{term "Code_Evaluation.Free (STR ''_'')"} $ HOLogic.mk_typerep ty]
    92     val var_eq =
    93       @{thm partial_term_of_anything}
    94       |> Drule.instantiate' [SOME (Thm.ctyp_of thy ty)] var_insts
    95       |> Thm.varifyT_global
    96     val eqs = var_eq :: map_index (mk_partial_term_of_eq thy ty) cs;
    97  in
    98     thy
    99     |> Code.del_eqns const
   100     |> fold Code.add_eqn eqs
   101   end;
   102 
   103 fun ensure_partial_term_of_code (tyco, (raw_vs, cs)) thy =
   104   let
   105     val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort partial_term_of};
   106   in if has_inst then add_partial_term_of_code tyco raw_vs cs thy else thy end;
   107 
   108 
   109 (* narrowing generators *)
   110 
   111 (** narrowing specific names and types **)
   112 
   113 exception FUNCTION_TYPE;
   114 
   115 val narrowingN = "narrowing";
   116 
   117 fun narrowingT T =
   118   @{typ Quickcheck_Narrowing.code_int} --> Type (@{type_name Quickcheck_Narrowing.cons}, [T])
   119 
   120 fun mk_empty T = Const (@{const_name Quickcheck_Narrowing.empty}, narrowingT T)
   121 
   122 fun mk_cons c T = Const (@{const_name Quickcheck_Narrowing.cons}, T --> narrowingT T) $ Const (c, T)
   123 
   124 fun mk_apply (T, t) (U, u) =
   125   let
   126     val (_, U') = dest_funT U
   127   in
   128     (U', Const (@{const_name Quickcheck_Narrowing.apply},
   129       narrowingT U --> narrowingT T --> narrowingT U') $ u $ t)
   130   end
   131   
   132 fun mk_sum (t, u) =
   133   let
   134     val T = fastype_of t
   135   in
   136     Const (@{const_name Quickcheck_Narrowing.sum}, T --> T --> T) $ t $ u
   137   end
   138 
   139 (** deriving narrowing instances **)
   140 
   141 fun mk_equations descr vs tycos narrowings (Ts, Us) =
   142   let
   143     fun mk_call T =
   144       (T, Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, narrowingT T))
   145     fun mk_aux_call fTs (k, _) (tyco, Ts) =
   146       let
   147         val T = Type (tyco, Ts)
   148         val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
   149       in
   150         (T, nth narrowings k)
   151       end
   152     fun mk_consexpr simpleT (c, xs) =
   153       let
   154         val Ts = map fst xs
   155       in snd (fold mk_apply xs (Ts ---> simpleT, mk_cons c (Ts ---> simpleT))) end
   156     fun mk_rhs exprs = foldr1 mk_sum exprs
   157     val rhss =
   158       Datatype_Aux.interpret_construction descr vs
   159         { atyp = mk_call, dtyp = mk_aux_call }
   160       |> (map o apfst) Type
   161       |> map (fn (T, cs) => map (mk_consexpr T) cs)
   162       |> map mk_rhs
   163     val lhss = narrowings
   164     val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
   165   in
   166     eqs
   167   end
   168   
   169 fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   170   let
   171     val _ = Datatype_Aux.message config "Creating narrowing generators ...";
   172     val narrowingsN = map (prefix (narrowingN ^ "_")) (names @ auxnames);
   173   in
   174     thy
   175     |> Class.instantiation (tycos, vs, @{sort narrowing})
   176     |> Quickcheck_Common.define_functions
   177       (fn narrowings => mk_equations descr vs tycos narrowings (Ts, Us), NONE)
   178       prfx [] narrowingsN (map narrowingT (Ts @ Us))
   179     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   180   end;
   181 
   182 (* testing framework *)
   183 
   184 val target = "Haskell"
   185 
   186 (** invocation of Haskell interpreter **)
   187 
   188 val narrowing_engine = File.read (Path.explode "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs")
   189 
   190 fun exec verbose code =
   191   ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code)
   192 
   193 fun with_overlord_dir name f =
   194   let
   195     val path = Path.append (Path.explode "~/.isabelle") (Path.basic (name ^ serial_string ()))
   196     val _ = Isabelle_System.mkdirs path;
   197   in Exn.release (Exn.capture f path) end;
   198   
   199 fun value ctxt (get, put, put_ml) (code, value_name) =
   200   let
   201     fun message s = if Config.get ctxt Quickcheck.quiet then () else Output.urgent_message s
   202     val tmp_prefix = "Quickcheck_Narrowing"
   203     val with_tmp_dir =
   204       if Config.get ctxt overlord then with_overlord_dir else Isabelle_System.with_tmp_dir 
   205     fun run in_path = 
   206       let
   207         val code_file = Path.append in_path (Path.basic "Code.hs")
   208         val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs")
   209         val main_file = Path.append in_path (Path.basic "Main.hs")
   210         val main = "module Main where {\n\n" ^
   211           "import System;\n" ^
   212           "import Narrowing_Engine;\n" ^
   213           "import Code;\n\n" ^
   214           "main = getArgs >>= \\[size] -> Narrowing_Engine.depthCheck (read size) (Code.value ())\n\n" ^
   215           "}\n"
   216         val code' = prefix "module Code where {\n\ndata Typerep = Typerep String [Typerep];\n"
   217           (unprefix "module Code where {" code)
   218         val _ = File.write code_file code'
   219         val _ = File.write narrowing_engine_file narrowing_engine
   220         val _ = File.write main_file main
   221         val executable = File.shell_path (Path.append in_path (Path.basic "isabelle_quickcheck_narrowing"))
   222         val cmd = "exec \"$ISABELLE_GHC\" -fglasgow-exts " ^
   223           (space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^
   224           " -o " ^ executable ^ ";"
   225         val _ = if bash cmd <> 0 then
   226           error "Compilation failed!"
   227         else
   228           ()
   229         fun with_size k =
   230           if k > Config.get ctxt Quickcheck.size then
   231             NONE
   232           else
   233             let
   234               val _ = message ("Test data size: " ^ string_of_int k)
   235               val (response, _) = bash_output (executable ^ " " ^ string_of_int k)
   236             in
   237               if response = "NONE\n" then with_size (k + 1) else SOME response
   238             end
   239       in case with_size 0 of
   240            NONE => NONE
   241          | SOME response =>
   242            let
   243              val output_value = the_default "NONE"
   244                (try (snd o split_last o filter_out (fn s => s = "") o split_lines) response)
   245                |> translate_string (fn s => if s = "\\" then "\\\\" else s)
   246              val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
   247                ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
   248              val ctxt' = ctxt
   249                |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
   250                |> Context.proof_map (exec false ml_code);
   251            in get ctxt' () end     
   252       end
   253   in with_tmp_dir tmp_prefix run end;
   254 
   255 fun dynamic_value_strict cookie thy postproc t =
   256   let
   257     val ctxt = Proof_Context.init_global thy
   258     fun evaluator naming program ((_, vs_ty), t) deps = Exn.interruptible_capture (value ctxt cookie)
   259       (Code_Target.evaluator thy target naming program deps (vs_ty, t));    
   260   in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
   261 
   262 (** counterexample generator **)
   263   
   264 structure Counterexample = Proof_Data
   265 (
   266   type T = unit -> term list option
   267   fun init _ () = error "Counterexample"
   268 )
   269 
   270 val put_counterexample = Counterexample.put
   271 
   272 fun finitize_functions t =
   273   let
   274     val ((names, Ts), t') = apfst split_list (strip_abs t)
   275     fun mk_eval_ffun dT rT =
   276       Const (@{const_name "Quickcheck_Narrowing.eval_ffun"}, 
   277         Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT]) --> dT --> rT)
   278     fun mk_eval_cfun dT rT =
   279       Const (@{const_name "Quickcheck_Narrowing.eval_cfun"}, 
   280         Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT]) --> dT --> rT)
   281     fun eval_function (T as Type (@{type_name fun}, [dT, rT])) =
   282       let
   283         val (rt', rT') = eval_function rT
   284       in
   285         case dT of
   286           Type (@{type_name fun}, _) =>
   287             (fn t => absdummy (dT, rt' (mk_eval_cfun dT rT' $ incr_boundvars 1 t $ Bound 0)),
   288             Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT']))
   289         | _ => (fn t => absdummy (dT, rt' (mk_eval_ffun dT rT' $ incr_boundvars 1 t $ Bound 0)),
   290             Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT']))
   291       end
   292       | eval_function T = (I, T)
   293     val (tt, Ts') = split_list (map eval_function Ts)
   294     val t'' = subst_bounds (map2 (fn f => fn x => f x) (rev tt) (map_index (Bound o fst) Ts), t')
   295   in
   296     list_abs (names ~~ Ts', t'')
   297   end
   298 
   299 (** tester **)
   300   
   301 fun test_term  ctxt (limit_time, is_interactive) (t, eval_terms) =
   302   let
   303     val thy = Proof_Context.theory_of ctxt
   304     val t' = list_abs_free (Term.add_frees t [], t)
   305     val t'' = if Config.get ctxt finite_functions then finitize_functions t' else t'
   306     fun ensure_testable t =
   307       Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
   308     val result = dynamic_value_strict
   309       (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
   310       thy (Option.map o map) (ensure_testable t'')
   311   in
   312     Quickcheck.Result {counterexample = Option.map ((curry (op ~~)) (Term.add_free_names t [])) result,
   313       evaluation_terms = Option.map (K []) result, timings = [], reports = []}
   314   end;
   315 
   316 fun test_goals ctxt (limit_time, is_interactive) insts goals =
   317   let
   318     val correct_inst_goals = Quickcheck.instantiate_goals ctxt insts goals
   319   in
   320     if Config.get ctxt Quickcheck.finite_types then
   321       error "Quickcheck-Narrowing does not support finite_types"
   322     else
   323       Quickcheck.collect_results (test_term ctxt (limit_time, is_interactive)) (maps (map snd) correct_inst_goals) []
   324   end
   325 
   326 (* setup *)
   327 
   328 val setup =
   329   Code.datatype_interpretation ensure_partial_term_of
   330   #> Code.datatype_interpretation ensure_partial_term_of_code
   331   #> Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
   332     (((@{sort typerep}, @{sort term_of}), @{sort narrowing}), instantiate_narrowing_datatype))
   333   #> Context.theory_map (Quickcheck.add_tester ("narrowing", test_goals))
   334     
   335 end;