src/Provers/clasimp.ML
author wenzelm
Thu Sep 02 00:48:07 2010 +0200 (2010-09-02)
changeset 38980 af73cf0dc31f
parent 37720 50a9e2fa4f6b
child 42373 fb539d6d1ac2
permissions -rw-r--r--
turned show_question_marks into proper configuration option;
show_question_marks only affects regular type/term pretty printing, not raw Term.string_of_vname;
tuned;
wenzelm@9772
     1
(*  Title:      Provers/clasimp.ML
oheimb@4652
     2
    Author:     David von Oheimb, TU Muenchen
oheimb@4652
     3
wenzelm@5219
     4
Combination of classical reasoner and simplifier (depends on
wenzelm@16019
     5
splitter.ML, classical.ML, blast.ML).
oheimb@4652
     6
*)
oheimb@4652
     7
wenzelm@9860
     8
infix 4 addSIs2 addSEs2 addSDs2 addIs2 addEs2 addDs2 addsimps2 delsimps2
berghofe@13603
     9
  addSss addss addss' addIffs delIffs;
oheimb@4652
    10
wenzelm@5219
    11
signature CLASIMP_DATA =
wenzelm@5219
    12
sig
wenzelm@8469
    13
  structure Splitter: SPLITTER
wenzelm@5219
    14
  structure Classical: CLASSICAL
wenzelm@5219
    15
  structure Blast: BLAST
wenzelm@5219
    16
  sharing type Classical.claset = Blast.claset
wenzelm@9860
    17
  val notE: thm
wenzelm@9860
    18
  val iffD1: thm
wenzelm@9860
    19
  val iffD2: thm
wenzelm@5219
    20
end
wenzelm@5219
    21
oheimb@4652
    22
signature CLASIMP =
oheimb@4652
    23
sig
wenzelm@5219
    24
  include CLASIMP_DATA
wenzelm@5219
    25
  type claset
wenzelm@5219
    26
  type clasimpset
wenzelm@26497
    27
  val get_css: Context.generic -> clasimpset
wenzelm@26497
    28
  val map_css: (clasimpset -> clasimpset) -> Context.generic -> Context.generic
wenzelm@32148
    29
  val clasimpset_of: Proof.context -> clasimpset
wenzelm@9860
    30
  val addSIs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    31
  val addSEs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    32
  val addSDs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    33
  val addIs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    34
  val addEs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    35
  val addDs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    36
  val addsimps2: clasimpset * thm list -> clasimpset
wenzelm@9860
    37
  val delsimps2: clasimpset * thm list -> clasimpset
wenzelm@9860
    38
  val addSss: claset * simpset -> claset
wenzelm@9860
    39
  val addss: claset * simpset -> claset
berghofe@13603
    40
  val addss': claset * simpset -> claset
wenzelm@9860
    41
  val addIffs: clasimpset * thm list -> clasimpset
wenzelm@9860
    42
  val delIffs: clasimpset * thm list -> clasimpset
oheimb@5483
    43
  val clarsimp_tac: clasimpset -> int -> tactic
wenzelm@9860
    44
  val mk_auto_tac: clasimpset -> int -> int -> tactic
wenzelm@9860
    45
  val auto_tac: clasimpset -> tactic
wenzelm@9860
    46
  val force_tac: clasimpset  -> int -> tactic
wenzelm@9860
    47
  val fast_simp_tac: clasimpset -> int -> tactic
wenzelm@9860
    48
  val slow_simp_tac: clasimpset -> int -> tactic
wenzelm@9860
    49
  val best_simp_tac: clasimpset -> int -> tactic
wenzelm@18728
    50
  val attrib: (clasimpset * thm list -> clasimpset) -> attribute
wenzelm@18728
    51
  val iff_add: attribute
wenzelm@18728
    52
  val iff_add': attribute
wenzelm@18728
    53
  val iff_del: attribute
wenzelm@30513
    54
  val iff_modifiers: Method.modifier parser list
wenzelm@30513
    55
  val clasimp_modifiers: Method.modifier parser list
wenzelm@26497
    56
  val clasimp_setup: theory -> theory
oheimb@4652
    57
end;
oheimb@4652
    58
wenzelm@5219
    59
functor ClasimpFun(Data: CLASIMP_DATA): CLASIMP =
oheimb@4652
    60
struct
oheimb@4652
    61
wenzelm@5219
    62
open Data;
wenzelm@5219
    63
wenzelm@26497
    64
wenzelm@26497
    65
(* type clasimpset *)
wenzelm@26497
    66
wenzelm@5219
    67
type claset = Classical.claset;
wenzelm@5219
    68
type clasimpset = claset * simpset;
wenzelm@5219
    69
wenzelm@26497
    70
fun get_css context = (Classical.get_cs context, Simplifier.get_ss context);
wenzelm@26497
    71
wenzelm@26497
    72
fun map_css f context =
wenzelm@36601
    73
  let
wenzelm@36601
    74
    val (cs, ss) = get_css context;
wenzelm@36601
    75
    val (cs', ss') = f (cs, Simplifier.context (Context.proof_of context) ss);
wenzelm@36601
    76
  in
wenzelm@36601
    77
    context
wenzelm@36601
    78
    |> Classical.map_cs (K cs')
wenzelm@36601
    79
    |> Simplifier.map_ss (K (Simplifier.inherit_context ss ss'))
wenzelm@36601
    80
  end;
wenzelm@17879
    81
wenzelm@32148
    82
fun clasimpset_of ctxt = (Classical.claset_of ctxt, Simplifier.simpset_of ctxt);
wenzelm@26497
    83
wenzelm@5219
    84
wenzelm@5219
    85
(* clasimpset operations *)
wenzelm@5219
    86
wenzelm@5219
    87
(*this interface for updating a clasimpset is rudimentary and just for
wenzelm@5219
    88
  convenience for the most common cases*)
wenzelm@5219
    89
wenzelm@26497
    90
fun pair_upd1 f ((a, b), x) = (f (a, x), b);
wenzelm@26497
    91
fun pair_upd2 f ((a, b), x) = (a, f (b, x));
wenzelm@5219
    92
wenzelm@5219
    93
fun op addSIs2   arg = pair_upd1 Classical.addSIs arg;
wenzelm@5219
    94
fun op addSEs2   arg = pair_upd1 Classical.addSEs arg;
wenzelm@5219
    95
fun op addSDs2   arg = pair_upd1 Classical.addSDs arg;
wenzelm@5219
    96
fun op addIs2    arg = pair_upd1 Classical.addIs arg;
wenzelm@5219
    97
fun op addEs2    arg = pair_upd1 Classical.addEs arg;
wenzelm@5219
    98
fun op addDs2    arg = pair_upd1 Classical.addDs arg;
wenzelm@5219
    99
fun op addsimps2 arg = pair_upd2 Simplifier.addsimps arg;
wenzelm@5219
   100
fun op delsimps2 arg = pair_upd2 Simplifier.delsimps arg;
oheimb@4652
   101
oheimb@9402
   102
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@9402
   103
val safe_asm_full_simp_tac = Simplifier.generic_simp_tac true (true,true,true);
oheimb@9402
   104
oheimb@4652
   105
(*Add a simpset to a classical set!*)
oheimb@4652
   106
(*Caution: only one simpset added can be added by each of addSss and addss*)
oheimb@11181
   107
fun cs addSss ss = Classical.addSafter (cs, ("safe_asm_full_simp_tac",
wenzelm@9772
   108
                            CHANGED o safe_asm_full_simp_tac ss));
wenzelm@9772
   109
fun cs addss  ss = Classical.addbefore  (cs, ("asm_full_simp_tac",
wenzelm@9772
   110
                            CHANGED o Simplifier.asm_full_simp_tac ss));
berghofe@13603
   111
fun cs addss' ss = Classical.addbefore  (cs, ("asm_full_simp_tac",
berghofe@13603
   112
                            CHANGED o Simplifier.asm_lr_simp_tac ss));
oheimb@4652
   113
wenzelm@9860
   114
(* iffs: addition of rules to simpsets and clasets simultaneously *)
wenzelm@9860
   115
oheimb@11344
   116
(*Takes (possibly conditional) theorems of the form A<->B to
wenzelm@9860
   117
        the Safe Intr     rule B==>A and
wenzelm@9860
   118
        the Safe Destruct rule A==>B.
wenzelm@9860
   119
  Also ~A goes to the Safe Elim rule A ==> ?R
paulson@11462
   120
  Failing other cases, A is added as a Safe Intr rule*)
wenzelm@9860
   121
local
wenzelm@9860
   122
wenzelm@13026
   123
fun addIff decls1 decls2 simp ((cs, ss), th) =
wenzelm@13026
   124
  let
wenzelm@13026
   125
    val n = nprems_of th;
paulson@17084
   126
    val (elim, intro) = if n = 0 then decls1 else decls2;
wenzelm@13026
   127
    val zero_rotate = zero_var_indexes o rotate_prems n;
wenzelm@13026
   128
  in
paulson@21709
   129
    (elim (intro (cs, [zero_rotate (th RS iffD2)]),
paulson@21709
   130
           [Tactic.make_elim (zero_rotate (th RS iffD1))])
paulson@21709
   131
      handle THM _ => (elim (cs, [zero_rotate (th RS notE)])
wenzelm@11902
   132
        handle THM _ => intro (cs, [th])),
wenzelm@11902
   133
     simp (ss, [th]))
wenzelm@11902
   134
  end;
wenzelm@9860
   135
wenzelm@12375
   136
fun delIff delcs delss ((cs, ss), th) =
wenzelm@11902
   137
  let val zero_rotate = zero_var_indexes o rotate_prems (nprems_of th) in
wenzelm@18688
   138
    (delcs (cs, [zero_rotate (th RS iffD2),
wenzelm@18688
   139
        Tactic.make_elim (zero_rotate (th RS iffD1))])
wenzelm@18688
   140
      handle THM _ => (delcs (cs, [zero_rotate (th RS notE)])
wenzelm@12375
   141
        handle THM _ => delcs (cs, [th])),
wenzelm@12375
   142
     delss (ss, [th]))
wenzelm@11902
   143
  end;
wenzelm@9860
   144
wenzelm@18630
   145
fun modifier att (x, ths) =
wenzelm@30190
   146
  fst (Library.foldl_map (Library.apply [att]) (x, rev ths));
wenzelm@18630
   147
wenzelm@33369
   148
val addXIs = modifier (Context_Rules.intro_query NONE);
wenzelm@33369
   149
val addXEs = modifier (Context_Rules.elim_query NONE);
wenzelm@33369
   150
val delXs = modifier Context_Rules.rule_del;
wenzelm@18630
   151
wenzelm@9860
   152
in
wenzelm@9860
   153
wenzelm@10033
   154
val op addIffs =
wenzelm@26497
   155
  Library.foldl
paulson@17084
   156
      (addIff (Classical.addSEs, Classical.addSIs)
wenzelm@26497
   157
              (Classical.addEs, Classical.addIs)
paulson@17084
   158
              Simplifier.addsimps);
skalberg@15570
   159
val op delIffs = Library.foldl (delIff Classical.delrules Simplifier.delsimps);
wenzelm@10033
   160
wenzelm@18688
   161
fun addIffs_generic (x, ths) =
wenzelm@18688
   162
  Library.foldl (addIff (addXEs, addXIs) (addXEs, addXIs) #1) ((x, ()), ths) |> #1;
wenzelm@12375
   163
wenzelm@18688
   164
fun delIffs_generic (x, ths) =
wenzelm@18688
   165
  Library.foldl (delIff delXs #1) ((x, ()), ths) |> #1;
wenzelm@12375
   166
wenzelm@9860
   167
end;
wenzelm@9860
   168
wenzelm@9860
   169
wenzelm@5219
   170
(* tacticals *)
wenzelm@5219
   171
wenzelm@12375
   172
fun clarsimp_tac (cs, ss) =
wenzelm@12375
   173
  safe_asm_full_simp_tac ss THEN_ALL_NEW
wenzelm@12375
   174
  Classical.clarify_tac (cs addSss ss);
wenzelm@12375
   175
oheimb@5483
   176
wenzelm@5219
   177
(* auto_tac *)
oheimb@4652
   178
wenzelm@5219
   179
fun blast_depth_tac cs m i thm =
wenzelm@9772
   180
    Blast.depth_tac cs m i thm
oheimb@5554
   181
      handle Blast.TRANS s => (warning ("Blast_tac: " ^ s); Seq.empty);
wenzelm@9772
   182
wenzelm@9772
   183
(* a variant of depth_tac that avoids interference of the simplifier
wenzelm@5219
   184
   with dup_step_tac when they are combined by auto_tac *)
oheimb@5756
   185
local
wenzelm@9772
   186
fun slow_step_tac' cs = Classical.appWrappers cs
wenzelm@9772
   187
        (Classical.instp_step_tac cs APPEND' Classical.haz_step_tac cs);
wenzelm@9772
   188
in fun nodup_depth_tac cs m i state = SELECT_GOAL
wenzelm@9772
   189
   (Classical.safe_steps_tac cs 1 THEN_ELSE
wenzelm@9772
   190
        (DEPTH_SOLVE (nodup_depth_tac cs m 1),
wenzelm@9772
   191
         Classical.inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
wenzelm@9772
   192
             (slow_step_tac' cs 1 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) 1))
oheimb@5756
   193
        )) i state;
oheimb@5756
   194
end;
oheimb@4652
   195
oheimb@9402
   196
(*Designed to be idempotent, except if blast_depth_tac instantiates variables
oheimb@4652
   197
  in some of the subgoals*)
oheimb@4652
   198
fun mk_auto_tac (cs, ss) m n =
wenzelm@5219
   199
    let val cs' = cs addss ss
wenzelm@9772
   200
        val maintac =
wenzelm@9772
   201
          blast_depth_tac cs m               (* fast but can't use wrappers *)
oheimb@4652
   202
          ORELSE'
oheimb@5756
   203
          (CHANGED o nodup_depth_tac cs' n); (* slower but more general *)
wenzelm@5219
   204
    in  EVERY [ALLGOALS (Simplifier.asm_full_simp_tac ss),
wenzelm@9772
   205
               TRY (Classical.safe_tac cs),
paulson@37720
   206
               REPEAT_DETERM (FIRSTGOAL maintac),
wenzelm@5219
   207
               TRY (Classical.safe_tac (cs addSss ss)),
wenzelm@9772
   208
               prune_params_tac]
oheimb@4652
   209
    end;
oheimb@4652
   210
wenzelm@9772
   211
fun auto_tac css = mk_auto_tac css 4 2;
oheimb@4652
   212
wenzelm@9772
   213
wenzelm@5219
   214
(* force_tac *)
wenzelm@5219
   215
oheimb@4659
   216
(* aimed to solve the given subgoal totally, using whatever tools possible *)
oheimb@4717
   217
fun force_tac (cs,ss) = let val cs' = cs addss ss in SELECT_GOAL (EVERY [
wenzelm@9772
   218
        Classical.clarify_tac cs' 1,
wenzelm@9772
   219
        IF_UNSOLVED (Simplifier.asm_full_simp_tac ss 1),
wenzelm@9772
   220
        ALLGOALS (Classical.first_best_tac cs')]) end;
oheimb@4659
   221
wenzelm@5219
   222
wenzelm@9805
   223
(* basic combinations *)
wenzelm@9805
   224
wenzelm@9805
   225
fun ADDSS tac (cs, ss) = let val cs' = cs addss ss in tac cs' end;
wenzelm@9591
   226
wenzelm@9805
   227
val fast_simp_tac = ADDSS Classical.fast_tac;
wenzelm@9805
   228
val slow_simp_tac = ADDSS Classical.slow_tac;
wenzelm@9805
   229
val best_simp_tac = ADDSS Classical.best_tac;
wenzelm@9591
   230
wenzelm@9591
   231
wenzelm@9860
   232
(* access clasimpset *)
wenzelm@8639
   233
wenzelm@15032
   234
wenzelm@9860
   235
wenzelm@9860
   236
(* attributes *)
wenzelm@9860
   237
wenzelm@26497
   238
fun attrib f = Thm.declaration_attribute (fn th => map_css (fn css => f (css, [th])));
wenzelm@18688
   239
fun attrib' f (x, th) = (f (x, [th]), th);
wenzelm@10033
   240
wenzelm@18688
   241
val iff_add = attrib (op addIffs);
wenzelm@18688
   242
val iff_add' = attrib' addIffs_generic;
wenzelm@18688
   243
val iff_del = attrib (op delIffs) o attrib' delIffs_generic;
wenzelm@18688
   244
wenzelm@30528
   245
fun iff_att x = (Scan.lift
wenzelm@18688
   246
 (Args.del >> K iff_del ||
wenzelm@18688
   247
  Scan.option Args.add -- Args.query >> K iff_add' ||
wenzelm@30528
   248
  Scan.option Args.add >> K iff_add)) x;
wenzelm@9860
   249
wenzelm@9860
   250
wenzelm@9860
   251
(* method modifiers *)
wenzelm@9860
   252
wenzelm@9860
   253
val iffN = "iff";
wenzelm@9860
   254
wenzelm@9860
   255
val iff_modifiers =
wenzelm@18728
   256
 [Args.$$$ iffN -- Scan.option Args.add -- Args.colon >> K ((I, iff_add): Method.modifier),
wenzelm@18728
   257
  Args.$$$ iffN -- Scan.option Args.add -- Args.query_colon >> K (I, iff_add'),
wenzelm@18728
   258
  Args.$$$ iffN -- Args.del -- Args.colon >> K (I, iff_del)];
wenzelm@9860
   259
wenzelm@8469
   260
val clasimp_modifiers =
wenzelm@9860
   261
  Simplifier.simp_modifiers @ Splitter.split_modifiers @
wenzelm@9860
   262
  Classical.cla_modifiers @ iff_modifiers;
wenzelm@9860
   263
wenzelm@9860
   264
wenzelm@9860
   265
(* methods *)
wenzelm@5926
   266
wenzelm@35613
   267
fun clasimp_meth tac ctxt = METHOD (fn facts =>
wenzelm@35613
   268
  ALLGOALS (Method.insert_tac facts) THEN tac (clasimpset_of ctxt));
wenzelm@7132
   269
wenzelm@35613
   270
fun clasimp_meth' tac ctxt = METHOD (fn facts =>
wenzelm@35613
   271
  HEADGOAL (Method.insert_tac facts THEN' tac (clasimpset_of ctxt)));
wenzelm@5926
   272
wenzelm@5926
   273
wenzelm@30541
   274
fun clasimp_method' tac =
wenzelm@35613
   275
  Method.sections clasimp_modifiers >> K (clasimp_meth' tac);
wenzelm@9772
   276
wenzelm@30541
   277
val auto_method =
wenzelm@36960
   278
  Scan.lift (Scan.option (Parse.nat -- Parse.nat)) --|
wenzelm@35613
   279
    Method.sections clasimp_modifiers >>
wenzelm@35613
   280
  (fn NONE => clasimp_meth (CHANGED_PROP o auto_tac)
wenzelm@35613
   281
    | SOME (m, n) => clasimp_meth (CHANGED_PROP o (fn css => mk_auto_tac css m n)));
wenzelm@9772
   282
wenzelm@9772
   283
wenzelm@9772
   284
(* theory setup *)
wenzelm@9772
   285
wenzelm@26497
   286
val clasimp_setup =
wenzelm@30541
   287
  Attrib.setup @{binding iff} iff_att "declaration of Simplifier / Classical rules" #>
wenzelm@30541
   288
  Method.setup @{binding fastsimp} (clasimp_method' fast_simp_tac) "combined fast and simp" #>
wenzelm@30541
   289
  Method.setup @{binding slowsimp} (clasimp_method' slow_simp_tac) "combined slow and simp" #>
wenzelm@30541
   290
  Method.setup @{binding bestsimp} (clasimp_method' best_simp_tac) "combined best and simp" #>
wenzelm@30541
   291
  Method.setup @{binding force} (clasimp_method' force_tac) "force" #>
wenzelm@30541
   292
  Method.setup @{binding auto} auto_method "auto" #>
wenzelm@30541
   293
  Method.setup @{binding clarsimp} (clasimp_method' (CHANGED_PROP oo clarsimp_tac))
wenzelm@30541
   294
    "clarify simplified goal";
wenzelm@5926
   295
oheimb@4652
   296
end;