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