src/HOL/Mutabelle/mutabelle_extra.ML
author haftmann
Wed May 05 18:25:34 2010 +0200 (2010-05-05)
changeset 36692 54b64d4ad524
parent 36610 bafd82950e24
child 36743 ce2297415b54
permissions -rw-r--r--
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
bulwahn@34965
     1
(*
bulwahn@34965
     2
    Title:      HOL/Mutabelle/mutabelle_extra.ML
bulwahn@34965
     3
    Author:     Stefan Berghofer, Jasmin Blanchette, Lukas Bulwahn, TU Muenchen
bulwahn@34965
     4
bulwahn@34965
     5
    Invokation of Counterexample generators
bulwahn@34965
     6
*)
bulwahn@34965
     7
signature MUTABELLE_EXTRA =
bulwahn@34965
     8
sig
bulwahn@34965
     9
bulwahn@34965
    10
val take_random : int -> 'a list -> 'a list
bulwahn@34965
    11
bulwahn@34965
    12
datatype outcome = GenuineCex | PotentialCex | NoCex | Donno | Timeout | Error
bulwahn@35324
    13
type timing = (string * int) list
bulwahn@34965
    14
bulwahn@35380
    15
type mtd = string * (theory -> term -> outcome * (timing * (int * Quickcheck.report list) list option))
bulwahn@35324
    16
bulwahn@35380
    17
type mutant_subentry = term * (string * (outcome * (timing * Quickcheck.report option))) list
bulwahn@34965
    18
type detailed_entry = string * bool * term * mutant_subentry list
bulwahn@34965
    19
bulwahn@34965
    20
type subentry = string * int * int * int * int * int * int
bulwahn@34965
    21
type entry = string * bool * subentry list
bulwahn@34965
    22
type report = entry list
bulwahn@34965
    23
bulwahn@34965
    24
val quickcheck_mtd : string -> mtd
bulwahn@34965
    25
(*
bulwahn@34965
    26
val refute_mtd : mtd
bulwahn@34965
    27
val nitpick_mtd : mtd
bulwahn@34965
    28
*)
bulwahn@34965
    29
bulwahn@34965
    30
val freezeT : term -> term
bulwahn@34965
    31
val thms_of : bool -> theory -> thm list
bulwahn@34965
    32
bulwahn@34965
    33
val string_for_report : report -> string
bulwahn@34965
    34
val write_report : string -> report -> unit
bulwahn@34965
    35
val mutate_theorems_and_write_report :
bulwahn@34965
    36
  theory -> mtd list -> thm list -> string -> unit
bulwahn@34965
    37
bulwahn@34965
    38
val random_seed : real Unsynchronized.ref
bulwahn@34965
    39
end;
bulwahn@34965
    40
bulwahn@34965
    41
structure MutabelleExtra : MUTABELLE_EXTRA =
bulwahn@34965
    42
struct
bulwahn@34965
    43
bulwahn@34965
    44
(* Own seed; can't rely on the Isabelle one to stay the same *)
bulwahn@34965
    45
val random_seed = Unsynchronized.ref 1.0;
bulwahn@34965
    46
bulwahn@34965
    47
bulwahn@34965
    48
(* mutation options *)
bulwahn@34965
    49
val max_mutants = 4
bulwahn@34965
    50
val num_mutations = 1
bulwahn@34965
    51
(* soundness check: *)
bulwahn@36255
    52
(*val max_mutants = 1
bulwahn@36255
    53
val num_mutations = 0*)
bulwahn@34965
    54
bulwahn@34965
    55
(* quickcheck options *)
bulwahn@34965
    56
(*val quickcheck_generator = "SML"*)
bulwahn@35537
    57
val iterations = 10
bulwahn@34965
    58
val size = 5
bulwahn@34965
    59
bulwahn@34965
    60
exception RANDOM;
bulwahn@34965
    61
bulwahn@34965
    62
fun rmod x y = x - y * Real.realFloor (x / y);
bulwahn@34965
    63
bulwahn@34965
    64
local
bulwahn@34965
    65
  val a = 16807.0;
bulwahn@34965
    66
  val m = 2147483647.0;
bulwahn@34965
    67
in
bulwahn@34965
    68
bulwahn@34965
    69
fun random () = CRITICAL (fn () =>
bulwahn@34965
    70
  let val r = rmod (a * ! random_seed) m
bulwahn@34965
    71
  in (random_seed := r; r) end);
bulwahn@34965
    72
bulwahn@34965
    73
end;
bulwahn@34965
    74
bulwahn@34965
    75
fun random_range l h =
bulwahn@34965
    76
  if h < l orelse l < 0 then raise RANDOM
bulwahn@34965
    77
  else l + Real.floor (rmod (random ()) (real (h - l + 1)));
bulwahn@34965
    78
bulwahn@34965
    79
datatype outcome = GenuineCex | PotentialCex | NoCex | Donno | Timeout | Error
bulwahn@35324
    80
type timing = (string * int) list
bulwahn@34965
    81
bulwahn@35380
    82
type mtd = string * (theory -> term -> outcome * (timing * (int * Quickcheck.report list) list option))
bulwahn@35324
    83
bulwahn@35380
    84
type mutant_subentry = term * (string * (outcome * (timing * Quickcheck.report option))) list
bulwahn@34965
    85
type detailed_entry = string * bool * term * mutant_subentry list
bulwahn@34965
    86
bulwahn@34965
    87
type subentry = string * int * int * int * int * int * int
bulwahn@34965
    88
type entry = string * bool * subentry list
bulwahn@34965
    89
type report = entry list
bulwahn@34965
    90
bulwahn@34965
    91
fun inst_type insts (Type (s, Ts)) = Type (s, map (inst_type insts) Ts)
bulwahn@34965
    92
  | inst_type insts T = the_default HOLogic.intT (AList.lookup op = insts T)
bulwahn@34965
    93
wenzelm@35625
    94
fun preprocess thy insts t = Object_Logic.atomize_term thy
bulwahn@34965
    95
 (map_types (inst_type insts) (Mutabelle.freeze t));
bulwahn@34965
    96
bulwahn@34965
    97
fun invoke_quickcheck quickcheck_generator thy t =
bulwahn@34965
    98
  TimeLimit.timeLimit (Time.fromSeconds (! Auto_Counterexample.time_limit))
bulwahn@34965
    99
      (fn _ =>
wenzelm@36610
   100
          case Quickcheck.gen_test_term (ProofContext.init_global thy) true true (SOME quickcheck_generator)
bulwahn@34965
   101
                                    size iterations (preprocess thy [] t) of
bulwahn@35380
   102
            (NONE, (time_report, report)) => (NoCex, (time_report, report))
bulwahn@35380
   103
          | (SOME _, (time_report, report)) => (GenuineCex, (time_report, report))) ()
bulwahn@35380
   104
  handle TimeLimit.TimeOut => (Timeout, ([("timelimit", !Auto_Counterexample.time_limit)], NONE))
bulwahn@34965
   105
bulwahn@34965
   106
fun quickcheck_mtd quickcheck_generator =
bulwahn@34965
   107
  ("quickcheck_" ^ quickcheck_generator, invoke_quickcheck quickcheck_generator)
bulwahn@34965
   108
bulwahn@34965
   109
  (*
bulwahn@34965
   110
fun invoke_refute thy t =
bulwahn@34965
   111
  let
bulwahn@34965
   112
    val res = MyRefute.refute_term thy [] t
bulwahn@34965
   113
    val _ = priority ("Refute: " ^ res)
bulwahn@34965
   114
  in
bulwahn@34965
   115
    case res of
bulwahn@34965
   116
      "genuine" => GenuineCex
bulwahn@34965
   117
    | "likely_genuine" => GenuineCex
bulwahn@34965
   118
    | "potential" => PotentialCex
bulwahn@34965
   119
    | "none" => NoCex
bulwahn@34965
   120
    | "unknown" => Donno
bulwahn@34965
   121
    | _ => Error
bulwahn@34965
   122
  end
bulwahn@34965
   123
  handle MyRefute.REFUTE (loc, details) =>
bulwahn@34965
   124
         (error ("Unhandled Refute error (" ^ quote loc ^ "): " ^ details ^
bulwahn@34965
   125
                   "."))
bulwahn@34965
   126
val refute_mtd = ("refute", invoke_refute)
bulwahn@34965
   127
*)
bulwahn@34965
   128
bulwahn@34965
   129
(*
bulwahn@34965
   130
open Nitpick_Util
bulwahn@34965
   131
open Nitpick_Rep
bulwahn@34965
   132
open Nitpick_Nut
bulwahn@34965
   133
bulwahn@34965
   134
fun invoke_nitpick thy t =
bulwahn@34965
   135
  let
wenzelm@36610
   136
    val ctxt = ProofContext.init_global thy
bulwahn@34965
   137
    val state = Proof.init ctxt
bulwahn@34965
   138
  in
bulwahn@34965
   139
    let
bulwahn@34965
   140
      val (res, _) = Nitpick.pick_nits_in_term state (Nitpick_Isar.default_params thy []) false [] t
bulwahn@34965
   141
      val _ = priority ("Nitpick: " ^ res)
bulwahn@34965
   142
    in
bulwahn@34965
   143
      case res of
bulwahn@34965
   144
        "genuine" => GenuineCex
bulwahn@34965
   145
      | "likely_genuine" => GenuineCex
bulwahn@34965
   146
      | "potential" => PotentialCex
bulwahn@34965
   147
      | "none" => NoCex
bulwahn@34965
   148
      | "unknown" => Donno
bulwahn@34965
   149
      | _ => Error
bulwahn@34965
   150
    end
bulwahn@34965
   151
    handle ARG (loc, details) =>
bulwahn@34965
   152
           (error ("Bad argument(s) to " ^ quote loc ^ ": " ^ details ^ "."))
bulwahn@34965
   153
         | BAD (loc, details) =>
bulwahn@34965
   154
           (error ("Internal error (" ^ quote loc ^ "): " ^ details ^ "."))
bulwahn@34965
   155
         | LIMIT (_, details) =>
bulwahn@34965
   156
           (warning ("Limit reached: " ^ details ^ "."); Donno)
bulwahn@34965
   157
         | NOT_SUPPORTED details =>
bulwahn@34965
   158
           (warning ("Unsupported case: " ^ details ^ "."); Donno)
bulwahn@34965
   159
         | NUT (loc, us) =>
bulwahn@34965
   160
           (error ("Invalid nut" ^ plural_s_for_list us ^
bulwahn@34965
   161
                   " (" ^ quote loc ^ "): " ^
bulwahn@34965
   162
                  commas (map (string_for_nut ctxt) us) ^ "."))
bulwahn@34965
   163
         | REP (loc, Rs) =>
bulwahn@34965
   164
           (error ("Invalid representation" ^ plural_s_for_list Rs ^
bulwahn@34965
   165
                   " (" ^ quote loc ^ "): " ^
bulwahn@34965
   166
                   commas (map string_for_rep Rs) ^ "."))
bulwahn@34965
   167
         | TERM (loc, ts) =>
bulwahn@34965
   168
           (error ("Invalid term" ^ plural_s_for_list ts ^
bulwahn@34965
   169
                   " (" ^ quote loc ^ "): " ^
bulwahn@34965
   170
                   commas (map (Syntax.string_of_term ctxt) ts) ^ "."))
bulwahn@34965
   171
         | TYPE (loc, Ts, ts) =>
bulwahn@34965
   172
           (error ("Invalid type" ^ plural_s_for_list Ts ^
bulwahn@34965
   173
                   (if null ts then
bulwahn@34965
   174
                      ""
bulwahn@34965
   175
                    else
bulwahn@34965
   176
                      " for term" ^ plural_s_for_list ts ^ " " ^
bulwahn@34965
   177
                      commas (map (quote o Syntax.string_of_term ctxt) ts)) ^
bulwahn@34965
   178
                   " (" ^ quote loc ^ "): " ^
bulwahn@34965
   179
                   commas (map (Syntax.string_of_typ ctxt) Ts) ^ "."))
bulwahn@34965
   180
         | Kodkod.SYNTAX (_, details) =>
bulwahn@34965
   181
           (warning ("Ill-formed Kodkodi output: " ^ details ^ "."); Error)
bulwahn@34965
   182
         | Refute.REFUTE (loc, details) =>
bulwahn@34965
   183
           (error ("Unhandled Refute error (" ^ quote loc ^ "): " ^ details ^
bulwahn@34965
   184
                   "."))
bulwahn@34965
   185
         | Exn.Interrupt => raise Exn.Interrupt
bulwahn@34965
   186
         | _ => (priority ("Unknown error in Nitpick"); Error)
bulwahn@34965
   187
  end
bulwahn@34965
   188
val nitpick_mtd = ("nitpick", invoke_nitpick)
bulwahn@34965
   189
*)
bulwahn@34965
   190
bulwahn@34965
   191
val comms = [@{const_name "op ="}, @{const_name "op |"}, @{const_name "op &"}]
bulwahn@34965
   192
bulwahn@34965
   193
val forbidden =
bulwahn@34965
   194
 [(* (@{const_name "power"}, "'a"), *)
bulwahn@35325
   195
  (*(@{const_name induct_equal}, "'a"),
bulwahn@35325
   196
  (@{const_name induct_implies}, "'a"),
bulwahn@35325
   197
  (@{const_name induct_conj}, "'a"),*)
bulwahn@34965
   198
  (@{const_name "undefined"}, "'a"),
bulwahn@34965
   199
  (@{const_name "default"}, "'a"),
bulwahn@36255
   200
  (@{const_name "dummy_pattern"}, "'a::{}"),
bulwahn@36255
   201
  (@{const_name "HOL.simp_implies"}, "prop => prop => prop"),
bulwahn@36255
   202
  (@{const_name "bot_fun_inst.bot_fun"}, "'a"),
bulwahn@36255
   203
  (@{const_name "top_fun_inst.top_fun"}, "'a"),
bulwahn@36255
   204
  (@{const_name "Pure.term"}, "'a"),
bulwahn@36255
   205
  (@{const_name "top_class.top"}, "'a"),
bulwahn@36255
   206
  (@{const_name "eq_class.eq"}, "'a"),
bulwahn@36255
   207
  (@{const_name "Quotient.Quot_True"}, "'a")(*,
bulwahn@34965
   208
  (@{const_name "uminus"}, "'a"),
bulwahn@34965
   209
  (@{const_name "Nat.size"}, "'a"),
haftmann@35092
   210
  (@{const_name "Groups.abs"}, "'a") *)]
bulwahn@34965
   211
bulwahn@34965
   212
val forbidden_thms =
bulwahn@34965
   213
 ["finite_intvl_succ_class",
bulwahn@34965
   214
  "nibble"]
bulwahn@34965
   215
bulwahn@34965
   216
val forbidden_consts =
bulwahn@34965
   217
 [@{const_name nibble_pair_of_char}]
bulwahn@34965
   218
bulwahn@34965
   219
fun is_forbidden_theorem (s, th) =
bulwahn@34965
   220
  let val consts = Term.add_const_names (prop_of th) [] in
haftmann@36692
   221
    exists (member (op =) (space_explode "." s)) forbidden_thms orelse
haftmann@36692
   222
    exists (member (op =) forbidden_consts) consts orelse
bulwahn@34965
   223
    length (space_explode "." s) <> 2 orelse
bulwahn@34965
   224
    String.isPrefix "type_definition" (List.last (space_explode "." s)) orelse
bulwahn@34965
   225
    String.isSuffix "_def" s orelse
bulwahn@34965
   226
    String.isSuffix "_raw" s
bulwahn@34965
   227
  end
bulwahn@34965
   228
bulwahn@36255
   229
val forbidden_mutant_constnames =
bulwahn@36255
   230
 ["HOL.induct_equal",
bulwahn@36255
   231
  "HOL.induct_implies",
bulwahn@36255
   232
  "HOL.induct_conj",
bulwahn@36255
   233
 @{const_name undefined},
bulwahn@36255
   234
 @{const_name default},
bulwahn@36255
   235
 @{const_name dummy_pattern},
bulwahn@36255
   236
 @{const_name "HOL.simp_implies"},
bulwahn@36255
   237
 @{const_name "bot_fun_inst.bot_fun"},
bulwahn@36255
   238
 @{const_name "top_fun_inst.top_fun"},
bulwahn@36255
   239
 @{const_name "Pure.term"},
bulwahn@36255
   240
 @{const_name "top_class.top"},
bulwahn@36255
   241
 @{const_name "eq_class.eq"},
bulwahn@36255
   242
 @{const_name "Quotient.Quot_True"}]
bulwahn@36255
   243
bulwahn@34965
   244
fun is_forbidden_mutant t =
bulwahn@36255
   245
  let
bulwahn@36255
   246
    val consts = Term.add_const_names t []
bulwahn@36255
   247
  in
bulwahn@34965
   248
    exists (String.isPrefix "Nitpick") consts orelse
bulwahn@36255
   249
    exists (String.isSubstring "_sumC") consts orelse
bulwahn@36255
   250
    exists (member (op =) forbidden_mutant_constnames) consts
bulwahn@34965
   251
  end
bulwahn@34965
   252
bulwahn@34965
   253
fun is_executable_term thy t = can (TimeLimit.timeLimit (Time.fromMilliseconds 2000) (Quickcheck.test_term
wenzelm@36610
   254
 (ProofContext.init_global thy) false (SOME "SML") 1 0)) (preprocess thy [] t)
bulwahn@34965
   255
fun is_executable_thm thy th = is_executable_term thy (prop_of th)
bulwahn@34965
   256
bulwahn@34965
   257
val freezeT =
bulwahn@34965
   258
  map_types (map_type_tvar (fn ((a, i), S) =>
bulwahn@34965
   259
    TFree (if i = 0 then a else a ^ "_" ^ string_of_int i, S)))
bulwahn@34965
   260
bulwahn@34965
   261
fun thms_of all thy =
bulwahn@34965
   262
  filter
bulwahn@34965
   263
    (fn th => (all orelse Context.theory_name (theory_of_thm th) = Context.theory_name thy)
bulwahn@34965
   264
      (* andalso is_executable_thm thy th *))
bulwahn@34965
   265
    (map snd (filter_out is_forbidden_theorem (Mutabelle.all_unconcealed_thms_of thy)))
bulwahn@34965
   266
bulwahn@34965
   267
val count = length oo filter o equal
bulwahn@34965
   268
bulwahn@34965
   269
fun take_random 0 _ = []
bulwahn@34965
   270
  | take_random _ [] = []
bulwahn@34965
   271
  | take_random n xs =
bulwahn@34965
   272
    let val j = random_range 0 (length xs - 1) in
bulwahn@34965
   273
      Library.nth xs j :: take_random (n - 1) (nth_drop j xs)
bulwahn@34965
   274
    end
bulwahn@34965
   275
bulwahn@35324
   276
fun cpu_time description f =
bulwahn@35324
   277
  let
bulwahn@35324
   278
    val start = start_timing ()
bulwahn@35324
   279
    val result = Exn.capture f ()
bulwahn@35324
   280
    val time = Time.toMilliseconds (#cpu (end_timing start))
bulwahn@35324
   281
  in (Exn.release result, (description, time)) end
bulwahn@35324
   282
bulwahn@34965
   283
fun safe_invoke_mtd thy (mtd_name, invoke_mtd) t =
bulwahn@34965
   284
  let
bulwahn@34965
   285
    val _ = priority ("Invoking " ^ mtd_name)
bulwahn@35380
   286
    val ((res, (timing, reports)), time) = cpu_time "total time"
bulwahn@35324
   287
      (fn () => case try (invoke_mtd thy) t of
bulwahn@35380
   288
          SOME (res, (timing, reports)) => (res, (timing, reports))
bulwahn@35324
   289
        | NONE => (priority ("**** PROBLEMS WITH " ^ Syntax.string_of_term_global thy t);
bulwahn@35380
   290
           (Error , ([], NONE))))
bulwahn@34965
   291
    val _ = priority (" Done")
bulwahn@35380
   292
  in (res, (time :: timing, reports)) end
bulwahn@34965
   293
bulwahn@34965
   294
(* theory -> term list -> mtd -> subentry *)
bulwahn@35324
   295
(*
bulwahn@34965
   296
fun test_mutants_using_one_method thy mutants (mtd_name, invoke_mtd) =
bulwahn@34965
   297
  let
bulwahn@34965
   298
     val res = map (safe_invoke_mtd thy (mtd_name, invoke_mtd)) mutants
bulwahn@34965
   299
  in
bulwahn@34965
   300
    (mtd_name, count GenuineCex res, count PotentialCex res, count NoCex res,
bulwahn@34965
   301
     count Donno res, count Timeout res, count Error res)
bulwahn@34965
   302
  end
bulwahn@34965
   303
bulwahn@34965
   304
fun create_entry thy thm exec mutants mtds =
bulwahn@34965
   305
  (Thm.get_name thm, exec, map (test_mutants_using_one_method thy mutants) mtds)
bulwahn@35324
   306
*)
bulwahn@34965
   307
fun create_detailed_entry thy thm exec mutants mtds =
bulwahn@34965
   308
  let
bulwahn@34965
   309
    fun create_mutant_subentry mutant = (mutant,
bulwahn@34965
   310
      map (fn (mtd_name, invoke_mtd) =>
bulwahn@34965
   311
        (mtd_name, safe_invoke_mtd thy (mtd_name, invoke_mtd) mutant)) mtds)
bulwahn@34965
   312
  in
bulwahn@34965
   313
    (Thm.get_name thm, exec, prop_of thm, map create_mutant_subentry mutants)
bulwahn@34965
   314
  end
bulwahn@34965
   315
bulwahn@34965
   316
(* (theory -> thm -> bool -> term list -> mtd list -> 'a) -> theory -> mtd list -> thm -> 'a *)
bulwahn@34965
   317
fun mutate_theorem create_entry thy mtds thm =
bulwahn@34965
   318
  let
bulwahn@34965
   319
    val pp = Syntax.pp_global thy
bulwahn@34965
   320
    val exec = is_executable_thm thy thm
bulwahn@34965
   321
    val _ = priority (if exec then "EXEC" else "NOEXEC")
bulwahn@34965
   322
    val mutants =
bulwahn@34965
   323
          (if num_mutations = 0 then
bulwahn@34965
   324
             [Thm.prop_of thm]
bulwahn@34965
   325
           else
bulwahn@34965
   326
             Mutabelle.mutate_mix (Thm.prop_of thm) thy comms forbidden
bulwahn@34965
   327
                                  num_mutations)
bulwahn@34965
   328
             |> filter_out is_forbidden_mutant
bulwahn@34965
   329
    val mutants =
bulwahn@34965
   330
      if exec then
bulwahn@34965
   331
        let
bulwahn@34965
   332
          val _ = priority ("BEFORE PARTITION OF " ^
bulwahn@34965
   333
                            Int.toString (length mutants) ^ " MUTANTS")
bulwahn@34965
   334
          val (execs, noexecs) = List.partition (is_executable_term thy) (take_random (20 * max_mutants) mutants)
bulwahn@34965
   335
          val _ = tracing ("AFTER PARTITION (" ^ Int.toString (length execs) ^
bulwahn@34965
   336
                           " vs " ^ Int.toString (length noexecs) ^ ")")
bulwahn@34965
   337
        in
bulwahn@34965
   338
          execs @ take_random (Int.max (0, max_mutants - length execs)) noexecs
bulwahn@34965
   339
        end
bulwahn@34965
   340
      else
bulwahn@34965
   341
        mutants
bulwahn@34965
   342
    val mutants = mutants
bulwahn@34965
   343
          |> take_random max_mutants
bulwahn@34965
   344
          |> map Mutabelle.freeze |> map freezeT
bulwahn@34965
   345
(*          |> filter (not o is_forbidden_mutant) *)
bulwahn@34965
   346
          |> List.mapPartial (try (Sign.cert_term thy))
bulwahn@34965
   347
    val _ = map (fn t => priority ("MUTANT: " ^ Pretty.string_of (Pretty.term pp t))) mutants
bulwahn@34965
   348
  in
bulwahn@34965
   349
    create_entry thy thm exec mutants mtds
bulwahn@34965
   350
  end
bulwahn@34965
   351
bulwahn@34965
   352
(* theory -> mtd list -> thm list -> report *)
bulwahn@34965
   353
val mutate_theorems = map ooo mutate_theorem
bulwahn@34965
   354
bulwahn@34965
   355
fun string_of_outcome GenuineCex = "GenuineCex"
bulwahn@34965
   356
  | string_of_outcome PotentialCex = "PotentialCex"
bulwahn@34965
   357
  | string_of_outcome NoCex = "NoCex"
bulwahn@34965
   358
  | string_of_outcome Donno = "Donno"
bulwahn@34965
   359
  | string_of_outcome Timeout = "Timeout"
bulwahn@34965
   360
  | string_of_outcome Error = "Error"
bulwahn@34965
   361
bulwahn@35324
   362
fun string_of_mutant_subentry thy thm_name (t, results) =
bulwahn@34965
   363
  "mutant: " ^ Syntax.string_of_term_global thy t ^ "\n" ^
bulwahn@35324
   364
  space_implode "; "
bulwahn@35324
   365
    (map (fn (mtd_name, (outcome, timing)) => mtd_name ^ ": " ^ string_of_outcome outcome) results) ^
bulwahn@34965
   366
  "\n"
bulwahn@34965
   367
bulwahn@36255
   368
(* XML.tree -> string *)
bulwahn@36255
   369
fun plain_string_from_xml_tree t =
bulwahn@36255
   370
  Buffer.empty |> XML.add_content t |> Buffer.content
bulwahn@36255
   371
(* string -> string *)
bulwahn@36255
   372
val unyxml = plain_string_from_xml_tree o YXML.parse
bulwahn@36255
   373
bulwahn@35324
   374
fun string_of_mutant_subentry' thy thm_name (t, results) =
bulwahn@35380
   375
  let
bulwahn@35380
   376
    fun string_of_report (Quickcheck.Report {iterations = i, raised_match_errors = e,
bulwahn@35380
   377
      satisfied_assms = s, positive_concl_tests = p}) =
bulwahn@35380
   378
      "errors: " ^ string_of_int e ^ "; conclusion tests: " ^ string_of_int p
bulwahn@35380
   379
    fun string_of_reports NONE = ""
bulwahn@35380
   380
      | string_of_reports (SOME reports) =
bulwahn@35380
   381
        cat_lines (map (fn (size, [report]) =>
bulwahn@35380
   382
          "size " ^ string_of_int size ^ ": " ^ string_of_report report) (rev reports))
bulwahn@35380
   383
    fun string_of_mtd_result (mtd_name, (outcome, (timing, reports))) =
bulwahn@35324
   384
      mtd_name ^ ": " ^ string_of_outcome outcome ^ "; " ^
bulwahn@35380
   385
      space_implode "; " (map (fn (s, t) => (s ^ ": " ^ string_of_int t)) timing)
bulwahn@36255
   386
      (*^ "\n" ^ string_of_reports reports*)
bulwahn@35380
   387
  in
bulwahn@36255
   388
    "mutant of " ^ thm_name ^ ":\n"
bulwahn@36255
   389
    ^ unyxml (Syntax.string_of_term_global thy t) ^ "\n" ^ cat_lines (map string_of_mtd_result results)
bulwahn@35380
   390
  end
bulwahn@35324
   391
bulwahn@34965
   392
fun string_of_detailed_entry thy (thm_name, exec, t, mutant_subentries) = 
bulwahn@34965
   393
   thm_name ^ " " ^ (if exec then "[exe]" else "[noexe]") ^ ": " ^
bulwahn@36255
   394
   Syntax.string_of_term_global thy t ^ "\n" ^                                    
bulwahn@35324
   395
   cat_lines (map (string_of_mutant_subentry' thy thm_name) mutant_subentries) ^ "\n"
bulwahn@34965
   396
bulwahn@36255
   397
fun theoryfile_string_of_mutant_subentry thy thm_name (i, (t, results)) =
bulwahn@36255
   398
  "lemma " ^ thm_name ^ "_" ^ string_of_int (i + 1) ^ ":\n" ^
bulwahn@36255
   399
  "\"" ^ unyxml (Syntax.string_of_term_global thy t) ^
bulwahn@36255
   400
  "\" \nquickcheck[generator = SML]\nquickcheck[generator = predicate_compile_wo_ff]\n" ^
bulwahn@36255
   401
  "quickcheck[generator = predicate_compile_ff_nofs]\noops\n"
bulwahn@36255
   402
bulwahn@36255
   403
fun theoryfile_string_of_detailed_entry thy (thm_name, exec, t, mutant_subentries) =
bulwahn@36255
   404
  "subsubsection {* mutants of " ^ thm_name ^ " *}\n\n" ^
bulwahn@36255
   405
  cat_lines (map_index
bulwahn@36255
   406
    (theoryfile_string_of_mutant_subentry thy thm_name) mutant_subentries) ^ "\n"
bulwahn@36255
   407
bulwahn@34965
   408
(* subentry -> string *)
bulwahn@34965
   409
fun string_for_subentry (mtd_name, genuine_cex, potential_cex, no_cex, donno,
bulwahn@34965
   410
                         timeout, error) =
bulwahn@34965
   411
  "    " ^ mtd_name ^ ": " ^ Int.toString genuine_cex ^ "+ " ^
bulwahn@34965
   412
  Int.toString potential_cex ^ "= " ^ Int.toString no_cex ^ "- " ^
bulwahn@34965
   413
  Int.toString donno ^ "? " ^ Int.toString timeout ^ "T " ^
bulwahn@34965
   414
  Int.toString error ^ "!"
bulwahn@34965
   415
(* entry -> string *)
bulwahn@34965
   416
fun string_for_entry (thm_name, exec, subentries) =
bulwahn@34965
   417
  thm_name ^ " " ^ (if exec then "[exe]" else "[noexe]") ^ ":\n" ^
bulwahn@34965
   418
  cat_lines (map string_for_subentry subentries) ^ "\n"
bulwahn@34965
   419
(* report -> string *)
bulwahn@34965
   420
fun string_for_report report = cat_lines (map string_for_entry report)
bulwahn@34965
   421
bulwahn@34965
   422
(* string -> report -> unit *)
bulwahn@34965
   423
fun write_report file_name =
bulwahn@34965
   424
  File.write (Path.explode file_name) o string_for_report
bulwahn@34965
   425
bulwahn@34965
   426
(* theory -> mtd list -> thm list -> string -> unit *)
bulwahn@34965
   427
fun mutate_theorems_and_write_report thy mtds thms file_name =
bulwahn@34965
   428
  let
bulwahn@34965
   429
    val _ = priority "Starting Mutabelle..."
bulwahn@34965
   430
    val path = Path.explode file_name
bulwahn@34965
   431
    (* for normal report: *)
bulwahn@34965
   432
    (*val (gen_create_entry, gen_string_for_entry) = (create_entry, string_for_entry)*)
bulwahn@34965
   433
    (*for detailled report: *)
bulwahn@34965
   434
    val (gen_create_entry, gen_string_for_entry) =
bulwahn@34965
   435
      (create_detailed_entry, string_of_detailed_entry thy)
bulwahn@36255
   436
    val (gen_create_entry, gen_string_for_entry) =
bulwahn@36255
   437
      (create_detailed_entry, theoryfile_string_of_detailed_entry thy)
bulwahn@34965
   438
  in
bulwahn@34965
   439
    File.write path (
bulwahn@34965
   440
    "Mutation options = "  ^
bulwahn@34965
   441
      "max_mutants: " ^ string_of_int max_mutants ^
bulwahn@34965
   442
      "; num_mutations: " ^ string_of_int num_mutations ^ "\n" ^
bulwahn@34965
   443
    "QC options = " ^
bulwahn@34965
   444
      (*"quickcheck_generator: " ^ quickcheck_generator ^ ";*)
bulwahn@34965
   445
      "size: " ^ string_of_int size ^
bulwahn@34965
   446
      "; iterations: " ^ string_of_int iterations ^ "\n");
bulwahn@34965
   447
    map (File.append path o gen_string_for_entry o mutate_theorem gen_create_entry thy mtds) thms;
bulwahn@34965
   448
    ()
bulwahn@34965
   449
  end
bulwahn@34965
   450
bulwahn@34965
   451
end;