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