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