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