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