src/Pure/tctical.ML
author haftmann
Fri Nov 10 07:44:47 2006 +0100 (2006-11-10)
changeset 21286 b5e7b80caa6a
parent 20664 ffbc5a57191a
child 22360 26ead7ed4f4b
permissions -rw-r--r--
introduces canonical AList functions for loop_tacs
wenzelm@16179
     1
(*  Title:      Pure/tctical.ML
clasohm@0
     2
    ID:         $Id$
paulson@2244
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
wenzelm@16179
     6
Tacticals.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@4602
     9
infix 1 THEN THEN' THEN_ALL_NEW;
clasohm@0
    10
infix 0 ORELSE APPEND INTLEAVE ORELSE' APPEND' INTLEAVE';
lcp@671
    11
infix 0 THEN_ELSE;
lcp@671
    12
clasohm@0
    13
clasohm@0
    14
signature TACTICAL =
wenzelm@11916
    15
sig
wenzelm@4270
    16
  type tactic  (* = thm -> thm Seq.seq*)
paulson@2244
    17
  val all_tac           : tactic
wenzelm@13108
    18
  val ALLGOALS          : (int -> tactic) -> tactic
paulson@2244
    19
  val APPEND            : tactic * tactic -> tactic
paulson@2244
    20
  val APPEND'           : ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
paulson@2244
    21
  val CHANGED           : tactic -> tactic
wenzelm@10821
    22
  val CHANGED_PROP      : tactic -> tactic
wenzelm@13108
    23
  val CHANGED_GOAL      : (int -> tactic) -> int -> tactic
wenzelm@13108
    24
  val COND              : (thm -> bool) -> tactic -> tactic -> tactic
paulson@2244
    25
  val DETERM            : tactic -> tactic
wenzelm@13108
    26
  val EVERY             : tactic list -> tactic
paulson@2244
    27
  val EVERY'            : ('a -> tactic) list -> 'a -> tactic
paulson@2244
    28
  val EVERY1            : (int -> tactic) list -> tactic
paulson@2244
    29
  val FILTER            : (thm -> bool) -> tactic -> tactic
wenzelm@13108
    30
  val FIRST             : tactic list -> tactic
paulson@2244
    31
  val FIRST'            : ('a -> tactic) list -> 'a -> tactic
paulson@2244
    32
  val FIRST1            : (int -> tactic) list -> tactic
paulson@2244
    33
  val FIRSTGOAL         : (int -> tactic) -> tactic
paulson@2244
    34
  val INTLEAVE          : tactic * tactic -> tactic
paulson@2244
    35
  val INTLEAVE'         : ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
paulson@2244
    36
  val METAHYPS          : (thm list -> tactic) -> int -> tactic
paulson@19153
    37
  val metahyps_thms     : int -> thm -> thm list option
paulson@2244
    38
  val no_tac            : tactic
paulson@2244
    39
  val ORELSE            : tactic * tactic -> tactic
paulson@2244
    40
  val ORELSE'           : ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
paulson@2244
    41
  val pause_tac         : tactic
paulson@6041
    42
  val print_tac         : string -> tactic
skalberg@15006
    43
  val PRIMITIVE         : (thm -> thm) -> tactic
skalberg@15006
    44
  val PRIMSEQ           : (thm -> thm Seq.seq) -> tactic
wenzelm@11916
    45
  val RANGE             : (int -> tactic) list -> int -> tactic
paulson@2244
    46
  val REPEAT            : tactic -> tactic
paulson@2244
    47
  val REPEAT1           : tactic -> tactic
oheimb@8149
    48
  val REPEAT_FIRST      : (int -> tactic) -> tactic
oheimb@8149
    49
  val REPEAT_SOME       : (int -> tactic) -> tactic
paulson@2244
    50
  val REPEAT_DETERM_N   : int -> tactic -> tactic
paulson@2244
    51
  val REPEAT_DETERM     : tactic -> tactic
paulson@2244
    52
  val REPEAT_DETERM1    : tactic -> tactic
lcp@703
    53
  val REPEAT_DETERM_FIRST: (int -> tactic) -> tactic
lcp@703
    54
  val REPEAT_DETERM_SOME: (int -> tactic) -> tactic
oheimb@8149
    55
  val DETERM_UNTIL      : (thm -> bool) -> tactic -> tactic
skalberg@15006
    56
  val SINGLE            : tactic -> thm -> thm option
wenzelm@13108
    57
  val SOMEGOAL          : (int -> tactic) -> tactic
paulson@2244
    58
  val strip_context     : term -> (string * typ) list * term list * term
paulson@2244
    59
  val SUBGOAL           : ((term*int) -> tactic) -> int -> tactic
paulson@2244
    60
  val suppress_tracing  : bool ref
paulson@2244
    61
  val THEN              : tactic * tactic -> tactic
paulson@2244
    62
  val THEN'             : ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
wenzelm@13108
    63
  val THEN_ALL_NEW      : (int -> tactic) * (int -> tactic) -> int -> tactic
wenzelm@13108
    64
  val REPEAT_ALL_NEW    : (int -> tactic) -> int -> tactic
paulson@2244
    65
  val THEN_ELSE         : tactic * (tactic*tactic) -> tactic
wenzelm@4270
    66
  val traced_tac        : (thm -> (thm * thm Seq.seq) option) -> tactic
paulson@5141
    67
  val tracify           : bool ref -> tactic -> tactic
paulson@2244
    68
  val trace_REPEAT      : bool ref
paulson@2244
    69
  val TRY               : tactic -> tactic
wenzelm@13108
    70
  val TRYALL            : (int -> tactic) -> tactic
wenzelm@11916
    71
end;
clasohm@0
    72
clasohm@0
    73
wenzelm@13108
    74
structure Tactical : TACTICAL =
clasohm@0
    75
struct
clasohm@0
    76
clasohm@0
    77
(**** Tactics ****)
clasohm@0
    78
clasohm@0
    79
(*A tactic maps a proof tree to a sequence of proof trees:
clasohm@0
    80
    if length of sequence = 0 then the tactic does not apply;
clasohm@0
    81
    if length > 1 then backtracking on the alternatives can occur.*)
clasohm@0
    82
wenzelm@4270
    83
type tactic = thm -> thm Seq.seq;
clasohm@0
    84
clasohm@0
    85
clasohm@0
    86
(*** LCF-style tacticals ***)
clasohm@0
    87
clasohm@0
    88
(*the tactical THEN performs one tactic followed by another*)
wenzelm@17344
    89
fun (tac1 THEN tac2) st = Seq.maps tac2 (tac1 st);
clasohm@0
    90
clasohm@0
    91
clasohm@0
    92
(*The tactical ORELSE uses the first tactic that returns a nonempty sequence.
clasohm@0
    93
  Like in LCF, ORELSE commits to either tac1 or tac2 immediately.
clasohm@0
    94
  Does not backtrack to tac2 if tac1 was initially chosen. *)
paulson@1502
    95
fun (tac1 ORELSE tac2) st =
wenzelm@4270
    96
    case Seq.pull(tac1 st) of
skalberg@15531
    97
        NONE       => tac2 st
wenzelm@4270
    98
      | sequencecell => Seq.make(fn()=> sequencecell);
clasohm@0
    99
clasohm@0
   100
clasohm@0
   101
(*The tactical APPEND combines the results of two tactics.
clasohm@0
   102
  Like ORELSE, but allows backtracking on both tac1 and tac2.
clasohm@0
   103
  The tactic tac2 is not applied until needed.*)
wenzelm@13108
   104
fun (tac1 APPEND tac2) st =
wenzelm@19861
   105
  Seq.append (tac1 st) (Seq.make(fn()=> Seq.pull (tac2 st)));
clasohm@0
   106
clasohm@0
   107
(*Like APPEND, but interleaves results of tac1 and tac2.*)
wenzelm@13108
   108
fun (tac1 INTLEAVE tac2) st =
wenzelm@4270
   109
    Seq.interleave(tac1 st,
wenzelm@4270
   110
                        Seq.make(fn()=> Seq.pull (tac2 st)));
clasohm@0
   111
lcp@671
   112
(*Conditional tactic.
paulson@2244
   113
        tac1 ORELSE tac2 = tac1 THEN_ELSE (all_tac, tac2)
paulson@2244
   114
        tac1 THEN tac2   = tac1 THEN_ELSE (tac2, no_tac)
lcp@671
   115
*)
wenzelm@13108
   116
fun (tac THEN_ELSE (tac1, tac2)) st =
wenzelm@4270
   117
    case Seq.pull(tac st) of
wenzelm@17344
   118
        NONE    => tac2 st                                   (*failed; try tactic 2*)
wenzelm@17344
   119
      | seqcell => Seq.maps tac1 (Seq.make(fn()=> seqcell)); (*succeeded; use tactic 1*)
lcp@671
   120
lcp@671
   121
clasohm@0
   122
(*Versions for combining tactic-valued functions, as in
clasohm@0
   123
     SOMEGOAL (resolve_tac rls THEN' assume_tac) *)
paulson@1502
   124
fun (tac1 THEN' tac2) x = tac1 x THEN tac2 x;
paulson@1502
   125
fun (tac1 ORELSE' tac2) x = tac1 x ORELSE tac2 x;
paulson@1502
   126
fun (tac1 APPEND' tac2) x = tac1 x APPEND tac2 x;
paulson@1502
   127
fun (tac1 INTLEAVE' tac2) x = tac1 x INTLEAVE tac2 x;
clasohm@0
   128
clasohm@0
   129
(*passes all proofs through unchanged;  identity of THEN*)
wenzelm@4270
   130
fun all_tac st = Seq.single st;
clasohm@0
   131
clasohm@0
   132
(*passes no proofs through;  identity of ORELSE and APPEND*)
wenzelm@4270
   133
fun no_tac st  = Seq.empty;
clasohm@0
   134
clasohm@0
   135
clasohm@0
   136
(*Make a tactic deterministic by chopping the tail of the proof sequence*)
wenzelm@12851
   137
fun DETERM tac = Seq.DETERM tac;
clasohm@0
   138
clasohm@0
   139
(*Conditional tactical: testfun controls which tactic to use next.
clasohm@0
   140
  Beware: due to eager evaluation, both thentac and elsetac are evaluated.*)
paulson@1502
   141
fun COND testfun thenf elsef = (fn prf =>
clasohm@0
   142
    if testfun prf then  thenf prf   else  elsef prf);
clasohm@0
   143
clasohm@0
   144
(*Do the tactic or else do nothing*)
clasohm@0
   145
fun TRY tac = tac ORELSE all_tac;
clasohm@0
   146
paulson@2672
   147
(*** List-oriented tactics ***)
paulson@2672
   148
paulson@2672
   149
local
paulson@2672
   150
  (*This version of EVERY avoids backtracking over repeated states*)
paulson@2672
   151
wenzelm@13108
   152
  fun EVY (trail, []) st =
skalberg@15531
   153
        Seq.make (fn()=> SOME(st,
wenzelm@13108
   154
                        Seq.make (fn()=> Seq.pull (evyBack trail))))
wenzelm@13108
   155
    | EVY (trail, tac::tacs) st =
wenzelm@13108
   156
          case Seq.pull(tac st) of
skalberg@15531
   157
              NONE    => evyBack trail              (*failed: backtrack*)
skalberg@15531
   158
            | SOME(st',q) => EVY ((st',q,tacs)::trail, tacs) st'
wenzelm@4270
   159
  and evyBack [] = Seq.empty (*no alternatives*)
paulson@2672
   160
    | evyBack ((st',q,tacs)::trail) =
wenzelm@13108
   161
          case Seq.pull q of
skalberg@15531
   162
              NONE        => evyBack trail
skalberg@15531
   163
            | SOME(st,q') => if eq_thm (st',st)
wenzelm@13108
   164
                             then evyBack ((st',q',tacs)::trail)
wenzelm@13108
   165
                             else EVY ((st,q',tacs)::trail, tacs) st
paulson@2672
   166
in
paulson@2672
   167
paulson@2672
   168
(* EVERY [tac1,...,tacn]   equals    tac1 THEN ... THEN tacn   *)
paulson@2672
   169
fun EVERY tacs = EVY ([], tacs);
paulson@2672
   170
end;
oheimb@2627
   171
clasohm@0
   172
paulson@1502
   173
(* EVERY' [tac1,...,tacn] i  equals    tac1 i THEN ... THEN tacn i   *)
paulson@2672
   174
fun EVERY' tacs i = EVERY (map (fn f => f i) tacs);
clasohm@0
   175
clasohm@0
   176
(*Apply every tactic to 1*)
paulson@1502
   177
fun EVERY1 tacs = EVERY' tacs 1;
clasohm@0
   178
clasohm@0
   179
(* FIRST [tac1,...,tacn]   equals    tac1 ORELSE ... ORELSE tacn   *)
skalberg@15574
   180
fun FIRST tacs = foldr (op ORELSE) no_tac tacs;
clasohm@0
   181
paulson@1502
   182
(* FIRST' [tac1,...,tacn] i  equals    tac1 i ORELSE ... ORELSE tacn i   *)
skalberg@15574
   183
fun FIRST' tacs = foldr (op ORELSE') (K no_tac) tacs;
clasohm@0
   184
clasohm@0
   185
(*Apply first tactic to 1*)
paulson@1502
   186
fun FIRST1 tacs = FIRST' tacs 1;
clasohm@0
   187
wenzelm@11916
   188
(*Apply tactics on consecutive subgoals*)
wenzelm@11916
   189
fun RANGE [] _ = all_tac
wenzelm@11916
   190
  | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
wenzelm@11916
   191
clasohm@0
   192
clasohm@0
   193
(*** Tracing tactics ***)
clasohm@0
   194
clasohm@0
   195
(*Print the current proof state and pass it on.*)
wenzelm@13108
   196
fun print_tac msg =
wenzelm@13108
   197
    (fn st =>
wenzelm@12262
   198
     (tracing msg;
schirmer@15017
   199
      tracing ((Pretty.string_of o Pretty.chunks o 
schirmer@15017
   200
                 Display.pretty_goals (! Display.goals_limit)) st); 
schirmer@15017
   201
      Seq.single st));
clasohm@0
   202
clasohm@0
   203
(*Pause until a line is typed -- if non-empty then fail. *)
wenzelm@13108
   204
fun pause_tac st =
wenzelm@12262
   205
  (tracing "** Press RETURN to continue:";
wenzelm@4270
   206
   if TextIO.inputLine TextIO.stdIn = "\n" then Seq.single st
wenzelm@12262
   207
   else (tracing "Goodbye";  Seq.empty));
clasohm@0
   208
clasohm@0
   209
exception TRACE_EXIT of thm
clasohm@0
   210
and TRACE_QUIT;
clasohm@0
   211
lcp@631
   212
(*Tracing flags*)
lcp@631
   213
val trace_REPEAT= ref false
lcp@631
   214
and suppress_tracing = ref false;
lcp@631
   215
clasohm@0
   216
(*Handle all tracing commands for current state and tactic *)
wenzelm@13108
   217
fun exec_trace_command flag (tac, st) =
paulson@2244
   218
   case TextIO.inputLine(TextIO.stdIn) of
paulson@1502
   219
       "\n" => tac st
wenzelm@4270
   220
     | "f\n" => Seq.empty
paulson@1502
   221
     | "o\n" => (flag:=false;  tac st)
paulson@1502
   222
     | "s\n" => (suppress_tracing:=true;  tac st)
wenzelm@12262
   223
     | "x\n" => (tracing "Exiting now";  raise (TRACE_EXIT st))
clasohm@0
   224
     | "quit\n" => raise TRACE_QUIT
wenzelm@12262
   225
     | _     => (tracing
clasohm@0
   226
"Type RETURN to continue or...\n\
clasohm@0
   227
\     f    - to fail here\n\
clasohm@0
   228
\     o    - to switch tracing off\n\
lcp@631
   229
\     s    - to suppress tracing until next entry to a tactical\n\
clasohm@0
   230
\     x    - to exit at this point\n\
clasohm@0
   231
\     quit - to abort this tracing run\n\
paulson@1502
   232
\** Well? "     ;  exec_trace_command flag (tac, st));
clasohm@0
   233
clasohm@0
   234
clasohm@0
   235
(*Extract from a tactic, a thm->thm seq function that handles tracing*)
paulson@1502
   236
fun tracify flag tac st =
lcp@631
   237
  if !flag andalso not (!suppress_tracing)
wenzelm@12082
   238
           then (Display.print_goals (! Display.goals_limit) st;
wenzelm@12262
   239
                 tracing "** Press RETURN to continue:";
paulson@2244
   240
                 exec_trace_command flag (tac,st))
paulson@1502
   241
  else tac st;
clasohm@0
   242
clasohm@0
   243
(*Create a tactic whose outcome is given by seqf, handling TRACE_EXIT*)
wenzelm@13108
   244
fun traced_tac seqf st =
lcp@631
   245
    (suppress_tracing := false;
wenzelm@4270
   246
     Seq.make (fn()=> seqf st
skalberg@15531
   247
                         handle TRACE_EXIT st' => SOME(st', Seq.empty)));
clasohm@0
   248
clasohm@0
   249
oheimb@8149
   250
(*Deterministic DO..UNTIL: only retains the first outcome; tail recursive.
oheimb@8149
   251
  Forces repitition until predicate on state is fulfilled.*)
wenzelm@13108
   252
fun DETERM_UNTIL p tac =
oheimb@8149
   253
let val tac = tracify trace_REPEAT tac
skalberg@15531
   254
    fun drep st = if p st then SOME (st, Seq.empty)
oheimb@8149
   255
                          else (case Seq.pull(tac st) of
skalberg@15531
   256
                                  NONE        => NONE
skalberg@15531
   257
                                | SOME(st',_) => drep st')
oheimb@8149
   258
in  traced_tac drep  end;
oheimb@8149
   259
wenzelm@13108
   260
(*Deterministic REPEAT: only retains the first outcome;
lcp@703
   261
  uses less space than REPEAT; tail recursive.
lcp@703
   262
  If non-negative, n bounds the number of repetitions.*)
wenzelm@13108
   263
fun REPEAT_DETERM_N n tac =
paulson@1502
   264
  let val tac = tracify trace_REPEAT tac
skalberg@15531
   265
      fun drep 0 st = SOME(st, Seq.empty)
paulson@2244
   266
        | drep n st =
wenzelm@4270
   267
           (case Seq.pull(tac st) of
skalberg@15531
   268
                NONE       => SOME(st, Seq.empty)
skalberg@15531
   269
              | SOME(st',_) => drep (n-1) st')
lcp@703
   270
  in  traced_tac (drep n)  end;
lcp@703
   271
lcp@703
   272
(*Allows any number of repetitions*)
lcp@703
   273
val REPEAT_DETERM = REPEAT_DETERM_N ~1;
clasohm@0
   274
clasohm@0
   275
(*General REPEAT: maintains a stack of alternatives; tail recursive*)
wenzelm@13108
   276
fun REPEAT tac =
paulson@1502
   277
  let val tac = tracify trace_REPEAT tac
wenzelm@13108
   278
      fun rep qs st =
wenzelm@4270
   279
        case Seq.pull(tac st) of
skalberg@15531
   280
            NONE       => SOME(st, Seq.make(fn()=> repq qs))
skalberg@15531
   281
          | SOME(st',q) => rep (q::qs) st'
skalberg@15531
   282
      and repq [] = NONE
wenzelm@4270
   283
        | repq(q::qs) = case Seq.pull q of
skalberg@15531
   284
            NONE       => repq qs
skalberg@15531
   285
          | SOME(st,q) => rep (q::qs) st
clasohm@0
   286
  in  traced_tac (rep [])  end;
clasohm@0
   287
clasohm@0
   288
(*Repeat 1 or more times*)
lcp@703
   289
fun REPEAT_DETERM1 tac = DETERM tac THEN REPEAT_DETERM tac;
clasohm@0
   290
fun REPEAT1 tac = tac THEN REPEAT tac;
clasohm@0
   291
clasohm@0
   292
clasohm@0
   293
(** Filtering tacticals **)
clasohm@0
   294
wenzelm@4270
   295
fun FILTER pred tac st = Seq.filter pred (tac st);
clasohm@0
   296
paulson@13650
   297
(*Accept only next states that change the theorem somehow*)
wenzelm@13108
   298
fun CHANGED tac st =
wenzelm@13108
   299
  let fun diff st' = not (Thm.eq_thm (st, st'));
wenzelm@13108
   300
  in Seq.filter diff (tac st) end;
clasohm@0
   301
paulson@13650
   302
(*Accept only next states that change the theorem's prop field
paulson@13650
   303
  (changes to signature, hyps, etc. don't count)*)
wenzelm@13108
   304
fun CHANGED_PROP tac st =
wenzelm@13108
   305
  let fun diff st' = not (Drule.eq_thm_prop (st, st'));
wenzelm@13108
   306
  in Seq.filter diff (tac st) end;
wenzelm@10821
   307
clasohm@0
   308
clasohm@0
   309
(*** Tacticals based on subgoal numbering ***)
clasohm@0
   310
wenzelm@13108
   311
(*For n subgoals, performs tac(n) THEN ... THEN tac(1)
paulson@1502
   312
  Essential to work backwards since tac(i) may add/delete subgoals at i. *)
wenzelm@13108
   313
fun ALLGOALS tac st =
paulson@1502
   314
  let fun doall 0 = all_tac
paulson@2244
   315
        | doall n = tac(n) THEN doall(n-1)
paulson@1502
   316
  in  doall(nprems_of st)st  end;
clasohm@0
   317
paulson@1502
   318
(*For n subgoals, performs tac(n) ORELSE ... ORELSE tac(1)  *)
wenzelm@13108
   319
fun SOMEGOAL tac st =
paulson@1502
   320
  let fun find 0 = no_tac
paulson@2244
   321
        | find n = tac(n) ORELSE find(n-1)
paulson@1502
   322
  in  find(nprems_of st)st  end;
clasohm@0
   323
paulson@1502
   324
(*For n subgoals, performs tac(1) ORELSE ... ORELSE tac(n).
clasohm@0
   325
  More appropriate than SOMEGOAL in some cases.*)
wenzelm@13108
   326
fun FIRSTGOAL tac st =
paulson@1502
   327
  let fun find (i,n) = if i>n then no_tac else  tac(i) ORELSE find (i+1,n)
paulson@1502
   328
  in  find(1, nprems_of st)st  end;
clasohm@0
   329
paulson@1502
   330
(*Repeatedly solve some using tac. *)
paulson@1502
   331
fun REPEAT_SOME tac = REPEAT1 (SOMEGOAL (REPEAT1 o tac));
paulson@1502
   332
fun REPEAT_DETERM_SOME tac = REPEAT_DETERM1 (SOMEGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   333
paulson@1502
   334
(*Repeatedly solve the first possible subgoal using tac. *)
paulson@1502
   335
fun REPEAT_FIRST tac = REPEAT1 (FIRSTGOAL (REPEAT1 o tac));
paulson@1502
   336
fun REPEAT_DETERM_FIRST tac = REPEAT_DETERM1 (FIRSTGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   337
paulson@1502
   338
(*For n subgoals, tries to apply tac to n,...1  *)
paulson@1502
   339
fun TRYALL tac = ALLGOALS (TRY o tac);
clasohm@0
   340
clasohm@0
   341
clasohm@0
   342
(*Make a tactic for subgoal i, if there is one.  *)
wenzelm@16510
   343
fun SUBGOAL goalfun i st =
wenzelm@16510
   344
  (case try Logic.nth_prem (i, Thm.prop_of st) of
wenzelm@16510
   345
    SOME goal => goalfun (goal, i) st
wenzelm@16510
   346
  | NONE => Seq.empty);
clasohm@0
   347
paulson@5141
   348
(*Returns all states that have changed in subgoal i, counted from the LAST
paulson@5141
   349
  subgoal.  For stac, for example.*)
wenzelm@13108
   350
fun CHANGED_GOAL tac i st =
paulson@7686
   351
    let val np = nprems_of st
paulson@7686
   352
        val d = np-i                 (*distance from END*)
paulson@5141
   353
        val t = List.nth(prems_of st, i-1)
wenzelm@13108
   354
        fun diff st' =
wenzelm@13108
   355
            nprems_of st' - d <= 0   (*the subgoal no longer exists*)
wenzelm@13108
   356
            orelse
paulson@7686
   357
             not (Pattern.aeconv (t,
wenzelm@13108
   358
                                  List.nth(prems_of st',
wenzelm@13108
   359
                                           nprems_of st' - d - 1)))
paulson@5141
   360
    in  Seq.filter diff (tac i st)  end
paulson@5141
   361
    handle Subscript => Seq.empty  (*no subgoal i*);
paulson@5141
   362
wenzelm@4602
   363
fun (tac1 THEN_ALL_NEW tac2) i st =
wenzelm@8535
   364
  st |> (tac1 i THEN (fn st' => Seq.INTERVAL tac2 i (i + nprems_of st' - nprems_of st) st'));
wenzelm@4602
   365
wenzelm@8341
   366
(*repeatedly dig into any emerging subgoals*)
wenzelm@8341
   367
fun REPEAT_ALL_NEW tac =
wenzelm@8341
   368
  tac THEN_ALL_NEW (TRY o (fn i => REPEAT_ALL_NEW tac i));
wenzelm@8341
   369
paulson@2005
   370
clasohm@0
   371
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
wenzelm@13108
   372
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters.
wenzelm@13108
   373
  Main difference from strip_assums concerns parameters:
clasohm@0
   374
    it replaces the bound variables by free variables.  *)
wenzelm@13108
   375
fun strip_context_aux (params, Hs, Const("==>", _) $ H $ B) =
paulson@2244
   376
        strip_context_aux (params, H::Hs, B)
clasohm@0
   377
  | strip_context_aux (params, Hs, Const("all",_)$Abs(a,T,t)) =
wenzelm@20194
   378
        let val (b,u) = Syntax.variant_abs(a,T,t)
paulson@2244
   379
        in  strip_context_aux ((b,T)::params, Hs, u)  end
clasohm@0
   380
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
clasohm@0
   381
clasohm@0
   382
fun strip_context A = strip_context_aux ([],[],A);
clasohm@0
   383
clasohm@0
   384
clasohm@0
   385
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
paulson@1502
   386
       METAHYPS (fn prems => tac prems) i
clasohm@0
   387
clasohm@0
   388
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
clasohm@0
   389
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
clasohm@0
   390
"prems").  The parameters x1,...,xm become free variables.  If the
clasohm@0
   391
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
clasohm@0
   392
then it is lifted back into the original context, yielding k subgoals.
clasohm@0
   393
clasohm@0
   394
Replaces unknowns in the context by Frees having the prefix METAHYP_
clasohm@0
   395
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
clasohm@0
   396
DOES NOT HANDLE TYPE UNKNOWNS.
clasohm@0
   397
****)
clasohm@0
   398
wenzelm@13108
   399
local
clasohm@0
   400
clasohm@0
   401
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
clasohm@0
   402
    Instantiates distinct free variables by terms of same type.*)
wenzelm@13108
   403
  fun free_instantiate ctpairs =
clasohm@0
   404
      forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
clasohm@0
   405
clasohm@0
   406
  fun free_of s ((a,i), T) =
clasohm@0
   407
        Free(s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i),
paulson@2244
   408
             T)
clasohm@0
   409
clasohm@0
   410
  fun mk_inst (var as Var(v,T))  = (var,  free_of "METAHYP1_" (v,T))
clasohm@0
   411
in
clasohm@0
   412
paulson@19153
   413
(*Common code for METAHYPS and metahyps_thms*)
paulson@19153
   414
fun metahyps_split_prem prem =
paulson@19153
   415
  let (*find all vars in the hyps -- should find tvars also!*)
skalberg@15574
   416
      val hyps_vars = foldr add_term_vars [] (Logic.strip_assums_hyp prem)
clasohm@0
   417
      val insts = map mk_inst hyps_vars
clasohm@0
   418
      (*replace the hyps_vars by Frees*)
clasohm@0
   419
      val prem' = subst_atomic insts prem
clasohm@0
   420
      val (params,hyps,concl) = strip_context prem'
paulson@19153
   421
  in (insts,params,hyps,concl)  end;
paulson@19153
   422
paulson@19153
   423
fun metahyps_aux_tac tacf (prem,gno) state =
paulson@19153
   424
  let val (insts,params,hyps,concl) = metahyps_split_prem prem 
paulson@19153
   425
      val {sign,maxidx,...} = rep_thm state
paulson@19153
   426
      val cterm = cterm_of sign
paulson@19153
   427
      val chyps = map cterm hyps
paulson@19153
   428
      val hypths = map assume chyps
paulson@19153
   429
      val subprems = map (forall_elim_vars 0) hypths
clasohm@0
   430
      val fparams = map Free params
clasohm@0
   431
      val cparams = map cterm fparams
clasohm@0
   432
      fun swap_ctpair (t,u) = (cterm u, cterm t)
clasohm@0
   433
      (*Subgoal variables: make Free; lift type over params*)
wenzelm@13108
   434
      fun mk_subgoal_inst concl_vars (var as Var(v,T)) =
wenzelm@20664
   435
          if member (op =) concl_vars var
paulson@2244
   436
          then (var, true, free_of "METAHYP2_" (v,T))
paulson@2244
   437
          else (var, false,
paulson@2244
   438
                free_of "METAHYP2_" (v, map #2 params --->T))
clasohm@0
   439
      (*Instantiate subgoal vars by Free applied to params*)
wenzelm@13108
   440
      fun mk_ctpair (t,in_concl,u) =
paulson@2244
   441
          if in_concl then (cterm t,  cterm u)
clasohm@0
   442
          else (cterm t,  cterm (list_comb (u,fparams)))
clasohm@0
   443
      (*Restore Vars with higher type and index*)
wenzelm@13108
   444
      fun mk_subgoal_swap_ctpair
wenzelm@13108
   445
                (t as Var((a,i),_), in_concl, u as Free(_,U)) =
paulson@2244
   446
          if in_concl then (cterm u, cterm t)
clasohm@0
   447
          else (cterm u, cterm(Var((a, i+maxidx), U)))
clasohm@0
   448
      (*Embed B in the original context of params and hyps*)
paulson@1502
   449
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
clasohm@0
   450
      (*Strip the context using elimination rules*)
clasohm@0
   451
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
clasohm@0
   452
      (*A form of lifting that discharges assumptions.*)
wenzelm@13108
   453
      fun relift st =
paulson@2244
   454
        let val prop = #prop(rep_thm st)
paulson@2244
   455
            val subgoal_vars = (*Vars introduced in the subgoals*)
skalberg@15574
   456
                  foldr add_term_vars [] (Logic.strip_imp_prems prop)
paulson@2244
   457
            and concl_vars = add_term_vars (Logic.strip_imp_concl prop, [])
paulson@2244
   458
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
berghofe@13664
   459
            val st' = Thm.instantiate ([], map mk_ctpair subgoal_insts) st
paulson@2244
   460
            val emBs = map (cterm o embed) (prems_of st')
berghofe@13664
   461
            val Cth  = implies_elim_list st' (map (elim o assume) emBs)
paulson@2244
   462
        in  (*restore the unknowns to the hypotheses*)
paulson@2244
   463
            free_instantiate (map swap_ctpair insts @
paulson@2244
   464
                              map mk_subgoal_swap_ctpair subgoal_insts)
paulson@2244
   465
                (*discharge assumptions from state in same order*)
berghofe@13664
   466
                (implies_intr_list emBs
paulson@2244
   467
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
paulson@2244
   468
        end
clasohm@0
   469
      (*function to replace the current subgoal*)
clasohm@0
   470
      fun next st = bicompose false (false, relift st, nprems_of st)
paulson@19153
   471
                    gno state
paulson@19153
   472
  in Seq.maps next (tacf subprems (trivial (cterm concl))) end;
paulson@19153
   473
clasohm@0
   474
end;
clasohm@0
   475
paulson@19153
   476
(*Returns the theorem list that METAHYPS would supply to its tactic*)
paulson@19153
   477
fun metahyps_thms i state =
paulson@19153
   478
  let val prem = Logic.nth_prem (i, Thm.prop_of state) 
paulson@19153
   479
      val (insts,params,hyps,concl) = metahyps_split_prem prem 
paulson@19153
   480
      val cterm = cterm_of (#sign (rep_thm state))
paulson@19153
   481
  in SOME (map (forall_elim_vars 0 o assume o cterm) hyps) end
paulson@19153
   482
  handle TERM ("nth_prem", [A]) => NONE;
paulson@19153
   483
haftmann@19455
   484
local
mengj@19229
   485
mengj@19229
   486
fun print_vars_terms thy (n,thm) =
haftmann@19455
   487
  let
wenzelm@19646
   488
    fun typed ty = " has type: " ^ Sign.string_of_typ thy ty;
haftmann@19455
   489
    fun find_vars thy (Const (c, ty)) =
haftmann@19455
   490
        (case Term.typ_tvars ty
haftmann@19455
   491
         of [] => I
wenzelm@19646
   492
          | _ => insert (op =) (c ^ typed ty))
wenzelm@19646
   493
      | find_vars thy (Var (xi, ty)) = insert (op =) (Term.string_of_vname xi ^ typed ty)
haftmann@19455
   494
      | find_vars _ (Free _) = I
haftmann@19455
   495
      | find_vars _ (Bound _) = I
haftmann@19455
   496
      | find_vars thy (Abs (_, _, t)) = find_vars thy t
haftmann@19455
   497
      | find_vars thy (t1 $ t2) = 
haftmann@19455
   498
          find_vars thy t1 #> find_vars thy t1;
haftmann@19455
   499
    val prem = Logic.nth_prem (n, Thm.prop_of thm)
haftmann@19455
   500
    val tms = find_vars thy prem []
haftmann@19455
   501
  in
haftmann@19455
   502
    (warning "Found schematic vars in assumptions:"; warning (cat_lines tms))
haftmann@19455
   503
  end;
haftmann@19455
   504
haftmann@19455
   505
in
mengj@19229
   506
mengj@19229
   507
fun METAHYPS tacf n thm = SUBGOAL (metahyps_aux_tac tacf) n thm
mengj@19229
   508
    handle THM("assume: variables",_,_) => (print_vars_terms (theory_of_thm thm) (n,thm); Seq.empty)
clasohm@0
   509
haftmann@19455
   510
end; (*local*)
haftmann@19455
   511
skalberg@15006
   512
(*Makes a tactic whose effect on a state is given by thmfun: thm->thm seq.*)
skalberg@15006
   513
fun PRIMSEQ thmfun st =  thmfun st handle THM _ => Seq.empty;
skalberg@15006
   514
skalberg@15006
   515
(*Makes a tactic whose effect on a state is given by thmfun: thm->thm.*)
skalberg@15006
   516
fun PRIMITIVE thmfun = PRIMSEQ (Seq.single o thmfun);
skalberg@15006
   517
skalberg@15006
   518
(* Inverse (more or less) of PRIMITIVE *)
skalberg@15570
   519
fun SINGLE tacf = Option.map fst o Seq.pull o tacf
haftmann@19455
   520
clasohm@0
   521
end;
paulson@1502
   522
paulson@1502
   523
open Tactical;