src/Pure/tctical.ML
author wenzelm
Fri Dec 14 11:52:54 2001 +0100 (2001-12-14)
changeset 12498 3b0091bf06e8
parent 12262 11ff5f47df6e
child 12851 e87496286934
permissions -rw-r--r--
changed Thm.varifyT';
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
paulson@2244
    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
paulson@5141
    23
  val CHANGED_GOAL	: (int -> tactic) -> int -> tactic
paulson@2244
    24
  val COND              : (thm -> bool) -> tactic -> tactic -> tactic   
paulson@2244
    25
  val DETERM            : tactic -> tactic
paulson@2244
    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
paulson@2244
    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
paulson@2244
    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@4602
    60
  val THEN_ALL_NEW	: (int -> tactic) * (int -> tactic) -> int -> tactic
wenzelm@8341
    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
paulson@2244
    67
  val TRYALL            : (int -> tactic) -> tactic   
wenzelm@11916
    68
end;
clasohm@0
    69
clasohm@0
    70
paulson@1502
    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.*)
paulson@1502
   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.*)
paulson@1502
   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
*)
paulson@1502
   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*)
paulson@1502
   136
fun DETERM tac st =  
wenzelm@4270
   137
      case Seq.pull (tac st) of
wenzelm@4270
   138
              None => Seq.empty
wenzelm@4270
   139
            | Some(x,_) => Seq.cons(x, Seq.empty);
clasohm@0
   140
clasohm@0
   141
clasohm@0
   142
(*Conditional tactical: testfun controls which tactic to use next.
clasohm@0
   143
  Beware: due to eager evaluation, both thentac and elsetac are evaluated.*)
paulson@1502
   144
fun COND testfun thenf elsef = (fn prf =>
clasohm@0
   145
    if testfun prf then  thenf prf   else  elsef prf);
clasohm@0
   146
clasohm@0
   147
(*Do the tactic or else do nothing*)
clasohm@0
   148
fun TRY tac = tac ORELSE all_tac;
clasohm@0
   149
paulson@2672
   150
(*** List-oriented tactics ***)
paulson@2672
   151
paulson@2672
   152
local
paulson@2672
   153
  (*This version of EVERY avoids backtracking over repeated states*)
paulson@2672
   154
paulson@2672
   155
  fun EVY (trail, []) st = 
wenzelm@4270
   156
	Seq.make (fn()=> Some(st, 
wenzelm@4270
   157
			Seq.make (fn()=> Seq.pull (evyBack trail))))
paulson@2672
   158
    | EVY (trail, tac::tacs) st = 
wenzelm@4270
   159
	  case Seq.pull(tac st) of
paulson@2672
   160
	      None    => evyBack trail              (*failed: backtrack*)
paulson@2672
   161
	    | Some(st',q) => EVY ((st',q,tacs)::trail, tacs) st'
wenzelm@4270
   162
  and evyBack [] = Seq.empty (*no alternatives*)
paulson@2672
   163
    | evyBack ((st',q,tacs)::trail) =
wenzelm@4270
   164
	  case Seq.pull q of
paulson@2672
   165
	      None        => evyBack trail
paulson@2672
   166
	    | Some(st,q') => if eq_thm (st',st) 
paulson@2672
   167
			     then evyBack ((st',q',tacs)::trail)
paulson@2672
   168
			     else EVY ((st,q',tacs)::trail, tacs) st
paulson@2672
   169
in
paulson@2672
   170
paulson@2672
   171
(* EVERY [tac1,...,tacn]   equals    tac1 THEN ... THEN tacn   *)
paulson@2672
   172
fun EVERY tacs = EVY ([], tacs);
paulson@2672
   173
end;
oheimb@2627
   174
clasohm@0
   175
paulson@1502
   176
(* EVERY' [tac1,...,tacn] i  equals    tac1 i THEN ... THEN tacn i   *)
paulson@2672
   177
fun EVERY' tacs i = EVERY (map (fn f => f i) tacs);
clasohm@0
   178
clasohm@0
   179
(*Apply every tactic to 1*)
paulson@1502
   180
fun EVERY1 tacs = EVERY' tacs 1;
clasohm@0
   181
clasohm@0
   182
(* FIRST [tac1,...,tacn]   equals    tac1 ORELSE ... ORELSE tacn   *)
clasohm@0
   183
fun FIRST tacs = foldr (op ORELSE) (tacs, no_tac);
clasohm@0
   184
paulson@1502
   185
(* FIRST' [tac1,...,tacn] i  equals    tac1 i ORELSE ... ORELSE tacn i   *)
paulson@1502
   186
fun FIRST' tacs = foldr (op ORELSE') (tacs, K no_tac);
clasohm@0
   187
clasohm@0
   188
(*Apply first tactic to 1*)
paulson@1502
   189
fun FIRST1 tacs = FIRST' tacs 1;
clasohm@0
   190
wenzelm@11916
   191
(*Apply tactics on consecutive subgoals*)
wenzelm@11916
   192
fun RANGE [] _ = all_tac
wenzelm@11916
   193
  | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
wenzelm@11916
   194
clasohm@0
   195
clasohm@0
   196
(*** Tracing tactics ***)
clasohm@0
   197
clasohm@0
   198
(*Print the current proof state and pass it on.*)
paulson@6041
   199
fun print_tac msg = 
paulson@1502
   200
    (fn st => 
wenzelm@12262
   201
     (tracing msg;
wenzelm@12082
   202
      Display.print_goals (! Display.goals_limit) st; Seq.single st));
clasohm@0
   203
clasohm@0
   204
(*Pause until a line is typed -- if non-empty then fail. *)
paulson@1502
   205
fun pause_tac st =  
wenzelm@12262
   206
  (tracing "** Press RETURN to continue:";
wenzelm@4270
   207
   if TextIO.inputLine TextIO.stdIn = "\n" then Seq.single st
wenzelm@12262
   208
   else (tracing "Goodbye";  Seq.empty));
clasohm@0
   209
clasohm@0
   210
exception TRACE_EXIT of thm
clasohm@0
   211
and TRACE_QUIT;
clasohm@0
   212
lcp@631
   213
(*Tracing flags*)
lcp@631
   214
val trace_REPEAT= ref false
lcp@631
   215
and suppress_tracing = ref false;
lcp@631
   216
clasohm@0
   217
(*Handle all tracing commands for current state and tactic *)
paulson@1502
   218
fun exec_trace_command flag (tac, st) = 
paulson@2244
   219
   case TextIO.inputLine(TextIO.stdIn) of
paulson@1502
   220
       "\n" => tac st
wenzelm@4270
   221
     | "f\n" => Seq.empty
paulson@1502
   222
     | "o\n" => (flag:=false;  tac st)
paulson@1502
   223
     | "s\n" => (suppress_tracing:=true;  tac st)
wenzelm@12262
   224
     | "x\n" => (tracing "Exiting now";  raise (TRACE_EXIT st))
clasohm@0
   225
     | "quit\n" => raise TRACE_QUIT
wenzelm@12262
   226
     | _     => (tracing
clasohm@0
   227
"Type RETURN to continue or...\n\
clasohm@0
   228
\     f    - to fail here\n\
clasohm@0
   229
\     o    - to switch tracing off\n\
lcp@631
   230
\     s    - to suppress tracing until next entry to a tactical\n\
clasohm@0
   231
\     x    - to exit at this point\n\
clasohm@0
   232
\     quit - to abort this tracing run\n\
paulson@1502
   233
\** Well? "     ;  exec_trace_command flag (tac, st));
clasohm@0
   234
clasohm@0
   235
clasohm@0
   236
(*Extract from a tactic, a thm->thm seq function that handles tracing*)
paulson@1502
   237
fun tracify flag tac st =
lcp@631
   238
  if !flag andalso not (!suppress_tracing)
wenzelm@12082
   239
           then (Display.print_goals (! Display.goals_limit) st;
wenzelm@12262
   240
                 tracing "** Press RETURN to continue:";
paulson@2244
   241
                 exec_trace_command flag (tac,st))
paulson@1502
   242
  else tac st;
clasohm@0
   243
clasohm@0
   244
(*Create a tactic whose outcome is given by seqf, handling TRACE_EXIT*)
paulson@1502
   245
fun traced_tac seqf st = 
lcp@631
   246
    (suppress_tracing := false;
wenzelm@4270
   247
     Seq.make (fn()=> seqf st
wenzelm@4270
   248
                         handle TRACE_EXIT st' => Some(st', Seq.empty)));
clasohm@0
   249
clasohm@0
   250
oheimb@8149
   251
(*Deterministic DO..UNTIL: only retains the first outcome; tail recursive.
oheimb@8149
   252
  Forces repitition until predicate on state is fulfilled.*)
oheimb@8149
   253
fun DETERM_UNTIL p tac = 
oheimb@8149
   254
let val tac = tracify trace_REPEAT tac
oheimb@8149
   255
    fun drep st = if p st then Some (st, Seq.empty)
oheimb@8149
   256
                          else (case Seq.pull(tac st) of
oheimb@8149
   257
                                  None        => None
oheimb@8149
   258
                                | Some(st',_) => drep st')
oheimb@8149
   259
in  traced_tac drep  end;
oheimb@8149
   260
clasohm@0
   261
(*Deterministic REPEAT: only retains the first outcome; 
lcp@703
   262
  uses less space than REPEAT; tail recursive.
lcp@703
   263
  If non-negative, n bounds the number of repetitions.*)
lcp@703
   264
fun REPEAT_DETERM_N n tac = 
paulson@1502
   265
  let val tac = tracify trace_REPEAT tac
wenzelm@4270
   266
      fun drep 0 st = Some(st, Seq.empty)
paulson@2244
   267
        | drep n st =
wenzelm@4270
   268
           (case Seq.pull(tac st) of
wenzelm@4270
   269
                None       => Some(st, Seq.empty)
paulson@2244
   270
              | Some(st',_) => drep (n-1) st')
lcp@703
   271
  in  traced_tac (drep n)  end;
lcp@703
   272
lcp@703
   273
(*Allows any number of repetitions*)
lcp@703
   274
val REPEAT_DETERM = REPEAT_DETERM_N ~1;
clasohm@0
   275
clasohm@0
   276
(*General REPEAT: maintains a stack of alternatives; tail recursive*)
clasohm@0
   277
fun REPEAT tac = 
paulson@1502
   278
  let val tac = tracify trace_REPEAT tac
clasohm@0
   279
      fun rep qs st = 
wenzelm@4270
   280
        case Seq.pull(tac st) of
wenzelm@4270
   281
            None       => Some(st, Seq.make(fn()=> repq qs))
clasohm@0
   282
          | Some(st',q) => rep (q::qs) st'
clasohm@0
   283
      and repq [] = None
wenzelm@4270
   284
        | repq(q::qs) = case Seq.pull q of
paulson@2244
   285
            None       => repq qs
clasohm@0
   286
          | Some(st,q) => rep (q::qs) st
clasohm@0
   287
  in  traced_tac (rep [])  end;
clasohm@0
   288
clasohm@0
   289
(*Repeat 1 or more times*)
lcp@703
   290
fun REPEAT_DETERM1 tac = DETERM tac THEN REPEAT_DETERM tac;
clasohm@0
   291
fun REPEAT1 tac = tac THEN REPEAT tac;
clasohm@0
   292
clasohm@0
   293
clasohm@0
   294
(** Filtering tacticals **)
clasohm@0
   295
clasohm@0
   296
(*Returns all states satisfying the predicate*)
wenzelm@4270
   297
fun FILTER pred tac st = Seq.filter pred (tac st);
clasohm@0
   298
clasohm@0
   299
(*Returns all changed states*)
paulson@1643
   300
fun CHANGED tac st = 
paulson@1643
   301
    let fun diff st' = not (eq_thm(st,st'))
wenzelm@4270
   302
    in  Seq.filter diff (tac st)  end;
clasohm@0
   303
wenzelm@10821
   304
fun CHANGED_PROP tac st = 
wenzelm@10821
   305
    let
wenzelm@10821
   306
      val prop = #prop (Thm.rep_thm st);
wenzelm@10821
   307
      fun diff st' = not (prop aconv #prop (Thm.rep_thm st'));
wenzelm@10821
   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
paulson@1502
   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. *)
paulson@1502
   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)  *)
paulson@1502
   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.*)
paulson@1502
   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.  *)
paulson@2580
   345
fun SUBGOAL goalfun i st = goalfun (List.nth(prems_of st, i-1),  i) st
wenzelm@4270
   346
                             handle Subscript => 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.*)
paulson@5141
   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)
paulson@7686
   354
        fun diff st' = 
paulson@7686
   355
	    nprems_of st' - d <= 0   (*the subgoal no longer exists*)
paulson@7686
   356
	    orelse 
paulson@7686
   357
             not (Pattern.aeconv (t,
paulson@7686
   358
				  List.nth(prems_of st', 
paulson@7686
   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
paulson@2005
   371
(*** SELECT_GOAL ***)
paulson@2005
   372
clasohm@0
   373
(*Tactical for restricting the effect of a tactic to subgoal i.
paulson@1502
   374
  Works by making a new state from subgoal i, applying tac to it, and
berghofe@11517
   375
  composing the resulting metathm with the original state.*)
paulson@2005
   376
paulson@2005
   377
(*Does the work of SELECT_GOAL. *)
paulson@5312
   378
fun select tac st i =
berghofe@11517
   379
  let
berghofe@11517
   380
    val thm = Drule.mk_triv_goal (adjust_maxidx (List.nth (cprems_of st, i-1)));
berghofe@11517
   381
    fun restore th = Seq.hd (bicompose false (false, th, nprems_of th) 1
berghofe@11517
   382
      (Thm.incr_indexes (#maxidx (rep_thm th) + 1) Drule.rev_triv_goal));
berghofe@11517
   383
    fun next st' = bicompose false (false, restore st', nprems_of st') i st;
berghofe@11517
   384
  in  Seq.flat (Seq.map next (tac thm))
paulson@2005
   385
  end;
paulson@2005
   386
paulson@1502
   387
fun SELECT_GOAL tac i st = 
paulson@5312
   388
  let val np = nprems_of st
paulson@5312
   389
  in  if 1<=i andalso i<=np then 
paulson@5312
   390
          (*If only one subgoal, then just apply tactic*)
paulson@5312
   391
	  if np=1 then tac st else select tac st i
paulson@5312
   392
      else Seq.empty
paulson@5312
   393
  end;
clasohm@0
   394
clasohm@0
   395
clasohm@0
   396
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
clasohm@0
   397
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters. 
clasohm@0
   398
  Main difference from strip_assums concerns parameters: 
clasohm@0
   399
    it replaces the bound variables by free variables.  *)
clasohm@0
   400
fun strip_context_aux (params, Hs, Const("==>", _) $ H $ B) = 
paulson@2244
   401
        strip_context_aux (params, H::Hs, B)
clasohm@0
   402
  | strip_context_aux (params, Hs, Const("all",_)$Abs(a,T,t)) =
clasohm@0
   403
        let val (b,u) = variant_abs(a,T,t)
paulson@2244
   404
        in  strip_context_aux ((b,T)::params, Hs, u)  end
clasohm@0
   405
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
clasohm@0
   406
clasohm@0
   407
fun strip_context A = strip_context_aux ([],[],A);
clasohm@0
   408
clasohm@0
   409
clasohm@0
   410
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
paulson@1502
   411
       METAHYPS (fn prems => tac prems) i
clasohm@0
   412
clasohm@0
   413
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
clasohm@0
   414
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
clasohm@0
   415
"prems").  The parameters x1,...,xm become free variables.  If the
clasohm@0
   416
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
clasohm@0
   417
then it is lifted back into the original context, yielding k subgoals.
clasohm@0
   418
clasohm@0
   419
Replaces unknowns in the context by Frees having the prefix METAHYP_
clasohm@0
   420
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
clasohm@0
   421
DOES NOT HANDLE TYPE UNKNOWNS.
clasohm@0
   422
****)
clasohm@0
   423
clasohm@0
   424
local 
clasohm@0
   425
clasohm@0
   426
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
clasohm@0
   427
    Instantiates distinct free variables by terms of same type.*)
clasohm@0
   428
  fun free_instantiate ctpairs = 
clasohm@0
   429
      forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
clasohm@0
   430
clasohm@0
   431
  fun free_of s ((a,i), T) =
clasohm@0
   432
        Free(s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i),
paulson@2244
   433
             T)
clasohm@0
   434
clasohm@0
   435
  fun mk_inst (var as Var(v,T))  = (var,  free_of "METAHYP1_" (v,T))
clasohm@0
   436
in
clasohm@0
   437
paulson@1502
   438
fun metahyps_aux_tac tacf (prem,i) state = 
clasohm@0
   439
  let val {sign,maxidx,...} = rep_thm state
lcp@230
   440
      val cterm = cterm_of sign
clasohm@0
   441
      (*find all vars in the hyps -- should find tvars also!*)
paulson@1502
   442
      val hyps_vars = foldr add_term_vars (Logic.strip_assums_hyp prem, [])
clasohm@0
   443
      val insts = map mk_inst hyps_vars
clasohm@0
   444
      (*replace the hyps_vars by Frees*)
clasohm@0
   445
      val prem' = subst_atomic insts prem
clasohm@0
   446
      val (params,hyps,concl) = strip_context prem'
clasohm@0
   447
      val fparams = map Free params
clasohm@0
   448
      val cparams = map cterm fparams
clasohm@0
   449
      and chyps = map cterm hyps
clasohm@0
   450
      val hypths = map assume chyps
clasohm@0
   451
      fun swap_ctpair (t,u) = (cterm u, cterm t)
clasohm@0
   452
      (*Subgoal variables: make Free; lift type over params*)
clasohm@0
   453
      fun mk_subgoal_inst concl_vars (var as Var(v,T)) = 
clasohm@0
   454
          if var mem concl_vars 
paulson@2244
   455
          then (var, true, free_of "METAHYP2_" (v,T))
paulson@2244
   456
          else (var, false,
paulson@2244
   457
                free_of "METAHYP2_" (v, map #2 params --->T))
clasohm@0
   458
      (*Instantiate subgoal vars by Free applied to params*)
clasohm@0
   459
      fun mk_ctpair (t,in_concl,u) = 
paulson@2244
   460
          if in_concl then (cterm t,  cterm u)
clasohm@0
   461
          else (cterm t,  cterm (list_comb (u,fparams)))
clasohm@0
   462
      (*Restore Vars with higher type and index*)
clasohm@0
   463
      fun mk_subgoal_swap_ctpair 
paulson@2244
   464
                (t as Var((a,i),_), in_concl, u as Free(_,U)) = 
paulson@2244
   465
          if in_concl then (cterm u, cterm t)
clasohm@0
   466
          else (cterm u, cterm(Var((a, i+maxidx), U)))
clasohm@0
   467
      (*Embed B in the original context of params and hyps*)
paulson@1502
   468
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
clasohm@0
   469
      (*Strip the context using elimination rules*)
clasohm@0
   470
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
clasohm@0
   471
      (*Embed an ff pair in the original params*)
paulson@1502
   472
      fun embed_ff(t,u) = Logic.mk_flexpair (list_abs_free (params, t), 
paulson@2244
   473
                                             list_abs_free (params, u))
clasohm@0
   474
      (*Remove parameter abstractions from the ff pairs*)
clasohm@0
   475
      fun elim_ff ff = flexpair_abs_elim_list cparams ff
clasohm@0
   476
      (*A form of lifting that discharges assumptions.*)
clasohm@0
   477
      fun relift st = 
paulson@2244
   478
        let val prop = #prop(rep_thm st)
paulson@2244
   479
            val subgoal_vars = (*Vars introduced in the subgoals*)
paulson@2244
   480
                  foldr add_term_vars (Logic.strip_imp_prems prop, [])
paulson@2244
   481
            and concl_vars = add_term_vars (Logic.strip_imp_concl prop, [])
paulson@2244
   482
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
paulson@2244
   483
            val st' = instantiate ([], map mk_ctpair subgoal_insts) st
paulson@2244
   484
            val emBs = map (cterm o embed) (prems_of st')
clasohm@0
   485
            and ffs = map (cterm o embed_ff) (tpairs_of st')
paulson@2244
   486
            val Cth  = implies_elim_list st' 
paulson@2244
   487
                            (map (elim_ff o assume) ffs @
paulson@2244
   488
                             map (elim o assume) emBs)
paulson@2244
   489
        in  (*restore the unknowns to the hypotheses*)
paulson@2244
   490
            free_instantiate (map swap_ctpair insts @
paulson@2244
   491
                              map mk_subgoal_swap_ctpair subgoal_insts)
paulson@2244
   492
                (*discharge assumptions from state in same order*)
paulson@2244
   493
                (implies_intr_list (ffs@emBs)
paulson@2244
   494
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
paulson@2244
   495
        end
clasohm@0
   496
      val subprems = map (forall_elim_vars 0) hypths
clasohm@0
   497
      and st0 = trivial (cterm concl)
clasohm@0
   498
      (*function to replace the current subgoal*)
clasohm@0
   499
      fun next st = bicompose false (false, relift st, nprems_of st)
paulson@2244
   500
                    i state
wenzelm@4270
   501
  in  Seq.flat (Seq.map next (tacf subprems st0))
paulson@1502
   502
  end;
clasohm@0
   503
end;
clasohm@0
   504
clasohm@0
   505
fun METAHYPS tacf = SUBGOAL (metahyps_aux_tac tacf);
clasohm@0
   506
clasohm@0
   507
end;
paulson@1502
   508
paulson@1502
   509
open Tactical;