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