src/Pure/tctical.ML
author wenzelm
Mon Nov 09 15:40:26 1998 +0100 (1998-11-09)
changeset 5838 a4122945d638
parent 5312 b380921982b9
child 5906 1f58694fc3e2
permissions -rw-r--r--
added metacuts_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 =
clasohm@0
    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
paulson@5141
    22
  val CHANGED_GOAL	: (int -> tactic) -> int -> tactic
paulson@2244
    23
  val COND              : (thm -> bool) -> tactic -> tactic -> tactic   
paulson@2244
    24
  val DETERM            : tactic -> tactic
paulson@2244
    25
  val EVERY             : tactic list -> tactic   
paulson@2244
    26
  val EVERY'            : ('a -> tactic) list -> 'a -> tactic
paulson@2244
    27
  val EVERY1            : (int -> tactic) list -> tactic
paulson@2244
    28
  val FILTER            : (thm -> bool) -> tactic -> tactic
paulson@2244
    29
  val FIRST             : tactic list -> tactic   
paulson@2244
    30
  val FIRST'            : ('a -> tactic) list -> 'a -> tactic
paulson@2244
    31
  val FIRST1            : (int -> tactic) list -> tactic
paulson@2244
    32
  val FIRSTGOAL         : (int -> tactic) -> tactic
paulson@2244
    33
  val goals_limit       : int ref
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@2244
    41
  val print_tac         : tactic
paulson@2244
    42
  val REPEAT            : tactic -> tactic
paulson@2244
    43
  val REPEAT1           : tactic -> tactic
paulson@2244
    44
  val REPEAT_DETERM_N   : int -> tactic -> tactic
paulson@2244
    45
  val REPEAT_DETERM     : tactic -> tactic
paulson@2244
    46
  val REPEAT_DETERM1    : tactic -> tactic
lcp@703
    47
  val REPEAT_DETERM_FIRST: (int -> tactic) -> tactic
lcp@703
    48
  val REPEAT_DETERM_SOME: (int -> tactic) -> tactic
paulson@2244
    49
  val REPEAT_FIRST      : (int -> tactic) -> tactic
paulson@2244
    50
  val REPEAT_SOME       : (int -> tactic) -> tactic
paulson@2244
    51
  val SELECT_GOAL       : tactic -> int -> tactic
paulson@2244
    52
  val SOMEGOAL          : (int -> tactic) -> tactic   
paulson@2244
    53
  val strip_context     : term -> (string * typ) list * term list * term
paulson@2244
    54
  val SUBGOAL           : ((term*int) -> tactic) -> int -> tactic
paulson@2244
    55
  val suppress_tracing  : bool ref
paulson@2244
    56
  val THEN              : tactic * tactic -> tactic
paulson@2244
    57
  val THEN'             : ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
wenzelm@4602
    58
  val THEN_ALL_NEW	: (int -> tactic) * (int -> tactic) -> int -> tactic
paulson@2244
    59
  val THEN_ELSE         : tactic * (tactic*tactic) -> tactic
wenzelm@4270
    60
  val traced_tac        : (thm -> (thm * thm Seq.seq) option) -> tactic
paulson@5141
    61
  val tracify           : bool ref -> tactic -> tactic
paulson@2244
    62
  val trace_REPEAT      : bool ref
paulson@2244
    63
  val TRY               : tactic -> tactic
paulson@2244
    64
  val TRYALL            : (int -> tactic) -> tactic   
clasohm@0
    65
  end;
clasohm@0
    66
clasohm@0
    67
paulson@1502
    68
structure Tactical : TACTICAL = 
clasohm@0
    69
struct
clasohm@0
    70
clasohm@0
    71
(**** Tactics ****)
clasohm@0
    72
clasohm@0
    73
(*A tactic maps a proof tree to a sequence of proof trees:
clasohm@0
    74
    if length of sequence = 0 then the tactic does not apply;
clasohm@0
    75
    if length > 1 then backtracking on the alternatives can occur.*)
clasohm@0
    76
wenzelm@4270
    77
type tactic = thm -> thm Seq.seq;
clasohm@0
    78
clasohm@0
    79
clasohm@0
    80
(*** LCF-style tacticals ***)
clasohm@0
    81
clasohm@0
    82
(*the tactical THEN performs one tactic followed by another*)
wenzelm@4270
    83
fun (tac1 THEN tac2) st = Seq.flat (Seq.map tac2 (tac1 st));
clasohm@0
    84
clasohm@0
    85
clasohm@0
    86
(*The tactical ORELSE uses the first tactic that returns a nonempty sequence.
clasohm@0
    87
  Like in LCF, ORELSE commits to either tac1 or tac2 immediately.
clasohm@0
    88
  Does not backtrack to tac2 if tac1 was initially chosen. *)
paulson@1502
    89
fun (tac1 ORELSE tac2) st =
wenzelm@4270
    90
    case Seq.pull(tac1 st) of
paulson@2244
    91
        None       => tac2 st
wenzelm@4270
    92
      | sequencecell => Seq.make(fn()=> sequencecell);
clasohm@0
    93
clasohm@0
    94
clasohm@0
    95
(*The tactical APPEND combines the results of two tactics.
clasohm@0
    96
  Like ORELSE, but allows backtracking on both tac1 and tac2.
clasohm@0
    97
  The tactic tac2 is not applied until needed.*)
paulson@1502
    98
fun (tac1 APPEND tac2) st = 
wenzelm@4270
    99
  Seq.append(tac1 st,
wenzelm@4270
   100
                  Seq.make(fn()=> Seq.pull (tac2 st)));
clasohm@0
   101
clasohm@0
   102
(*Like APPEND, but interleaves results of tac1 and tac2.*)
paulson@1502
   103
fun (tac1 INTLEAVE tac2) st = 
wenzelm@4270
   104
    Seq.interleave(tac1 st,
wenzelm@4270
   105
                        Seq.make(fn()=> Seq.pull (tac2 st)));
clasohm@0
   106
lcp@671
   107
(*Conditional tactic.
paulson@2244
   108
        tac1 ORELSE tac2 = tac1 THEN_ELSE (all_tac, tac2)
paulson@2244
   109
        tac1 THEN tac2   = tac1 THEN_ELSE (tac2, no_tac)
lcp@671
   110
*)
paulson@1502
   111
fun (tac THEN_ELSE (tac1, tac2)) st = 
wenzelm@4270
   112
    case Seq.pull(tac st) of
paulson@2244
   113
        None    => tac2 st              (*failed; try tactic 2*)
wenzelm@4270
   114
      | seqcell => Seq.flat       (*succeeded; use tactic 1*)
wenzelm@4270
   115
                    (Seq.map tac1 (Seq.make(fn()=> seqcell)));
lcp@671
   116
lcp@671
   117
clasohm@0
   118
(*Versions for combining tactic-valued functions, as in
clasohm@0
   119
     SOMEGOAL (resolve_tac rls THEN' assume_tac) *)
paulson@1502
   120
fun (tac1 THEN' tac2) x = tac1 x THEN tac2 x;
paulson@1502
   121
fun (tac1 ORELSE' tac2) x = tac1 x ORELSE tac2 x;
paulson@1502
   122
fun (tac1 APPEND' tac2) x = tac1 x APPEND tac2 x;
paulson@1502
   123
fun (tac1 INTLEAVE' tac2) x = tac1 x INTLEAVE tac2 x;
clasohm@0
   124
clasohm@0
   125
(*passes all proofs through unchanged;  identity of THEN*)
wenzelm@4270
   126
fun all_tac st = Seq.single st;
clasohm@0
   127
clasohm@0
   128
(*passes no proofs through;  identity of ORELSE and APPEND*)
wenzelm@4270
   129
fun no_tac st  = Seq.empty;
clasohm@0
   130
clasohm@0
   131
clasohm@0
   132
(*Make a tactic deterministic by chopping the tail of the proof sequence*)
paulson@1502
   133
fun DETERM tac st =  
wenzelm@4270
   134
      case Seq.pull (tac st) of
wenzelm@4270
   135
              None => Seq.empty
wenzelm@4270
   136
            | Some(x,_) => Seq.cons(x, Seq.empty);
clasohm@0
   137
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
paulson@2672
   152
  fun EVY (trail, []) st = 
wenzelm@4270
   153
	Seq.make (fn()=> Some(st, 
wenzelm@4270
   154
			Seq.make (fn()=> Seq.pull (evyBack trail))))
paulson@2672
   155
    | EVY (trail, tac::tacs) st = 
wenzelm@4270
   156
	  case Seq.pull(tac st) of
paulson@2672
   157
	      None    => evyBack trail              (*failed: backtrack*)
paulson@2672
   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@4270
   161
	  case Seq.pull q of
paulson@2672
   162
	      None        => evyBack trail
paulson@2672
   163
	    | Some(st,q') => if eq_thm (st',st) 
paulson@2672
   164
			     then evyBack ((st',q',tacs)::trail)
paulson@2672
   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   *)
clasohm@0
   180
fun FIRST tacs = foldr (op ORELSE) (tacs, no_tac);
clasohm@0
   181
paulson@1502
   182
(* FIRST' [tac1,...,tacn] i  equals    tac1 i ORELSE ... ORELSE tacn i   *)
paulson@1502
   183
fun FIRST' tacs = foldr (op ORELSE') (tacs, K no_tac);
clasohm@0
   184
clasohm@0
   185
(*Apply first tactic to 1*)
paulson@1502
   186
fun FIRST1 tacs = FIRST' tacs 1;
clasohm@0
   187
clasohm@0
   188
clasohm@0
   189
(*** Tracing tactics ***)
clasohm@0
   190
clasohm@0
   191
(*Max number of goals to print -- set by user*)
clasohm@0
   192
val goals_limit = ref 10;
clasohm@0
   193
clasohm@0
   194
(*Print the current proof state and pass it on.*)
paulson@1502
   195
val print_tac = 
paulson@1502
   196
    (fn st => 
wenzelm@4270
   197
     (print_goals (!goals_limit) st; Seq.single st));
clasohm@0
   198
clasohm@0
   199
(*Pause until a line is typed -- if non-empty then fail. *)
paulson@1502
   200
fun pause_tac st =  
clasohm@0
   201
  (prs"** Press RETURN to continue: ";
wenzelm@4270
   202
   if TextIO.inputLine TextIO.stdIn = "\n" then Seq.single st
wenzelm@4270
   203
   else (prs"Goodbye\n";  Seq.empty));
clasohm@0
   204
clasohm@0
   205
exception TRACE_EXIT of thm
clasohm@0
   206
and TRACE_QUIT;
clasohm@0
   207
lcp@631
   208
(*Tracing flags*)
lcp@631
   209
val trace_REPEAT= ref false
lcp@631
   210
and suppress_tracing = ref false;
lcp@631
   211
clasohm@0
   212
(*Handle all tracing commands for current state and tactic *)
paulson@1502
   213
fun exec_trace_command flag (tac, st) = 
paulson@2244
   214
   case TextIO.inputLine(TextIO.stdIn) of
paulson@1502
   215
       "\n" => tac st
wenzelm@4270
   216
     | "f\n" => Seq.empty
paulson@1502
   217
     | "o\n" => (flag:=false;  tac st)
paulson@1502
   218
     | "s\n" => (suppress_tracing:=true;  tac st)
paulson@1502
   219
     | "x\n" => (prs"Exiting now\n";  raise (TRACE_EXIT st))
clasohm@0
   220
     | "quit\n" => raise TRACE_QUIT
clasohm@0
   221
     | _     => (prs
clasohm@0
   222
"Type RETURN to continue or...\n\
clasohm@0
   223
\     f    - to fail here\n\
clasohm@0
   224
\     o    - to switch tracing off\n\
lcp@631
   225
\     s    - to suppress tracing until next entry to a tactical\n\
clasohm@0
   226
\     x    - to exit at this point\n\
clasohm@0
   227
\     quit - to abort this tracing run\n\
paulson@1502
   228
\** Well? "     ;  exec_trace_command flag (tac, st));
clasohm@0
   229
clasohm@0
   230
clasohm@0
   231
(*Extract from a tactic, a thm->thm seq function that handles tracing*)
paulson@1502
   232
fun tracify flag tac st =
lcp@631
   233
  if !flag andalso not (!suppress_tracing)
wenzelm@3669
   234
           then (print_goals (!goals_limit) st;
paulson@2244
   235
                 prs"** Press RETURN to continue: ";
paulson@2244
   236
                 exec_trace_command flag (tac,st))
paulson@1502
   237
  else tac st;
clasohm@0
   238
clasohm@0
   239
(*Create a tactic whose outcome is given by seqf, handling TRACE_EXIT*)
paulson@1502
   240
fun traced_tac seqf st = 
lcp@631
   241
    (suppress_tracing := false;
wenzelm@4270
   242
     Seq.make (fn()=> seqf st
wenzelm@4270
   243
                         handle TRACE_EXIT st' => Some(st', Seq.empty)));
clasohm@0
   244
clasohm@0
   245
clasohm@0
   246
(*Deterministic REPEAT: only retains the first outcome; 
lcp@703
   247
  uses less space than REPEAT; tail recursive.
lcp@703
   248
  If non-negative, n bounds the number of repetitions.*)
lcp@703
   249
fun REPEAT_DETERM_N n tac = 
paulson@1502
   250
  let val tac = tracify trace_REPEAT tac
wenzelm@4270
   251
      fun drep 0 st = Some(st, Seq.empty)
paulson@2244
   252
        | drep n st =
wenzelm@4270
   253
           (case Seq.pull(tac st) of
wenzelm@4270
   254
                None       => Some(st, Seq.empty)
paulson@2244
   255
              | Some(st',_) => drep (n-1) st')
lcp@703
   256
  in  traced_tac (drep n)  end;
lcp@703
   257
lcp@703
   258
(*Allows any number of repetitions*)
lcp@703
   259
val REPEAT_DETERM = REPEAT_DETERM_N ~1;
clasohm@0
   260
clasohm@0
   261
(*General REPEAT: maintains a stack of alternatives; tail recursive*)
clasohm@0
   262
fun REPEAT tac = 
paulson@1502
   263
  let val tac = tracify trace_REPEAT tac
clasohm@0
   264
      fun rep qs st = 
wenzelm@4270
   265
        case Seq.pull(tac st) of
wenzelm@4270
   266
            None       => Some(st, Seq.make(fn()=> repq qs))
clasohm@0
   267
          | Some(st',q) => rep (q::qs) st'
clasohm@0
   268
      and repq [] = None
wenzelm@4270
   269
        | repq(q::qs) = case Seq.pull q of
paulson@2244
   270
            None       => repq qs
clasohm@0
   271
          | Some(st,q) => rep (q::qs) st
clasohm@0
   272
  in  traced_tac (rep [])  end;
clasohm@0
   273
clasohm@0
   274
(*Repeat 1 or more times*)
lcp@703
   275
fun REPEAT_DETERM1 tac = DETERM tac THEN REPEAT_DETERM tac;
clasohm@0
   276
fun REPEAT1 tac = tac THEN REPEAT tac;
clasohm@0
   277
clasohm@0
   278
clasohm@0
   279
(** Filtering tacticals **)
clasohm@0
   280
clasohm@0
   281
(*Returns all states satisfying the predicate*)
wenzelm@4270
   282
fun FILTER pred tac st = Seq.filter pred (tac st);
clasohm@0
   283
clasohm@0
   284
(*Returns all changed states*)
paulson@1643
   285
fun CHANGED tac st = 
paulson@1643
   286
    let fun diff st' = not (eq_thm(st,st'))
wenzelm@4270
   287
    in  Seq.filter diff (tac st)  end;
clasohm@0
   288
clasohm@0
   289
clasohm@0
   290
(*** Tacticals based on subgoal numbering ***)
clasohm@0
   291
paulson@1502
   292
(*For n subgoals, performs tac(n) THEN ... THEN tac(1) 
paulson@1502
   293
  Essential to work backwards since tac(i) may add/delete subgoals at i. *)
paulson@1502
   294
fun ALLGOALS tac st = 
paulson@1502
   295
  let fun doall 0 = all_tac
paulson@2244
   296
        | doall n = tac(n) THEN doall(n-1)
paulson@1502
   297
  in  doall(nprems_of st)st  end;
clasohm@0
   298
paulson@1502
   299
(*For n subgoals, performs tac(n) ORELSE ... ORELSE tac(1)  *)
paulson@1502
   300
fun SOMEGOAL tac st = 
paulson@1502
   301
  let fun find 0 = no_tac
paulson@2244
   302
        | find n = tac(n) ORELSE find(n-1)
paulson@1502
   303
  in  find(nprems_of st)st  end;
clasohm@0
   304
paulson@1502
   305
(*For n subgoals, performs tac(1) ORELSE ... ORELSE tac(n).
clasohm@0
   306
  More appropriate than SOMEGOAL in some cases.*)
paulson@1502
   307
fun FIRSTGOAL tac st = 
paulson@1502
   308
  let fun find (i,n) = if i>n then no_tac else  tac(i) ORELSE find (i+1,n)
paulson@1502
   309
  in  find(1, nprems_of st)st  end;
clasohm@0
   310
paulson@1502
   311
(*Repeatedly solve some using tac. *)
paulson@1502
   312
fun REPEAT_SOME tac = REPEAT1 (SOMEGOAL (REPEAT1 o tac));
paulson@1502
   313
fun REPEAT_DETERM_SOME tac = REPEAT_DETERM1 (SOMEGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   314
paulson@1502
   315
(*Repeatedly solve the first possible subgoal using tac. *)
paulson@1502
   316
fun REPEAT_FIRST tac = REPEAT1 (FIRSTGOAL (REPEAT1 o tac));
paulson@1502
   317
fun REPEAT_DETERM_FIRST tac = REPEAT_DETERM1 (FIRSTGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   318
paulson@1502
   319
(*For n subgoals, tries to apply tac to n,...1  *)
paulson@1502
   320
fun TRYALL tac = ALLGOALS (TRY o tac);
clasohm@0
   321
clasohm@0
   322
clasohm@0
   323
(*Make a tactic for subgoal i, if there is one.  *)
paulson@2580
   324
fun SUBGOAL goalfun i st = goalfun (List.nth(prems_of st, i-1),  i) st
wenzelm@4270
   325
                             handle Subscript => Seq.empty;
clasohm@0
   326
paulson@5141
   327
(*Returns all states that have changed in subgoal i, counted from the LAST
paulson@5141
   328
  subgoal.  For stac, for example.*)
paulson@5141
   329
fun CHANGED_GOAL tac i st = 
paulson@5141
   330
    let val j = nprems_of st - i
paulson@5141
   331
        val t = List.nth(prems_of st, i-1)
paulson@5141
   332
        fun diff st' = 
paulson@5141
   333
	    not (nprems_of st' > j   (*subgoal is still there*)
paulson@5141
   334
		 andalso
paulson@5141
   335
		 t aconv List.nth(prems_of st', nprems_of st' - j - 1))
paulson@5141
   336
    in  Seq.filter diff (tac i st)  end
paulson@5141
   337
    handle Subscript => Seq.empty  (*no subgoal i*);
paulson@5141
   338
wenzelm@4602
   339
fun ALLGOALS_RANGE tac (i:int) j st =
wenzelm@4602
   340
  if i > j then all_tac st
wenzelm@4602
   341
  else (tac j THEN ALLGOALS_RANGE tac i (j - 1)) st;
wenzelm@4602
   342
wenzelm@4602
   343
fun (tac1 THEN_ALL_NEW tac2) i st =
wenzelm@4602
   344
  st |> (tac1 i THEN (fn st' => ALLGOALS_RANGE tac2 i (i + nprems_of st' - nprems_of st) st'));
wenzelm@4602
   345
paulson@2005
   346
paulson@2005
   347
(*** SELECT_GOAL ***)
paulson@2005
   348
clasohm@0
   349
(*Tactical for restricting the effect of a tactic to subgoal i.
paulson@1502
   350
  Works by making a new state from subgoal i, applying tac to it, and
clasohm@0
   351
  composing the resulting metathm with the original state.
clasohm@0
   352
  The "main goal" of the new state will not be atomic, some tactics may fail!
clasohm@0
   353
  DOES NOT work if tactic affects the main goal other than by instantiation.*)
clasohm@0
   354
paulson@2005
   355
(*SELECT_GOAL optimization: replace the conclusion by a variable X,
paulson@2005
   356
  to avoid copying.  Proof states have X==concl as an assuption.*)
paulson@2005
   357
wenzelm@3991
   358
val prop_equals = cterm_of (sign_of ProtoPure.thy) 
paulson@2005
   359
                    (Const("==", propT-->propT-->propT));
paulson@2005
   360
paulson@2005
   361
fun mk_prop_equals(t,u) = capply (capply prop_equals t) u;
paulson@2005
   362
paulson@2005
   363
(*Like trivial but returns [ct==X] ct==>X instead of ct==>ct, if possible.
paulson@2005
   364
  It is paired with a function to undo the transformation.  If ct contains
paulson@2005
   365
  Vars then it returns ct==>ct.*)
paulson@5312
   366
paulson@2005
   367
fun eq_trivial ct =
paulson@5312
   368
  let val xfree = cterm_of (sign_of ProtoPure.thy)
paulson@5312
   369
                           (Free (gensym"EQ_TRIVIAL_", propT))
paulson@2158
   370
      val ct_eq_x = mk_prop_equals (ct, xfree)
paulson@2005
   371
      and refl_ct = reflexive ct
paulson@2005
   372
      fun restore th = 
paulson@2244
   373
          implies_elim 
paulson@2244
   374
            (forall_elim ct (forall_intr xfree (implies_intr ct_eq_x th)))
paulson@2244
   375
            refl_ct
paulson@2005
   376
  in  (equal_elim
paulson@2005
   377
         (combination (combination refl_implies refl_ct) (assume ct_eq_x))
paulson@5312
   378
         (Drule.mk_triv_goal ct),
paulson@2005
   379
       restore)
paulson@2005
   380
  end  (*Fails if there are Vars or TVars*)
paulson@5312
   381
    handle THM _ => (Drule.mk_triv_goal ct, I);
paulson@5312
   382
paulson@2005
   383
paulson@2005
   384
(*Does the work of SELECT_GOAL. *)
paulson@5312
   385
fun select tac st i =
paulson@2580
   386
  let val (eq_cprem, restore) = (*we hope maxidx goes to ~1*)
paulson@5312
   387
	  eq_trivial (adjust_maxidx (List.nth(cprems_of st, i-1)))
paulson@5312
   388
      fun next st' = 
paulson@5312
   389
	  let val np' = nprems_of st'
paulson@5312
   390
              (*rename the ?A in rev_triv_goal*)
paulson@5312
   391
	      val {maxidx,...} = rep_thm st'
paulson@5312
   392
              val ct = cterm_of (sign_of ProtoPure.thy)
paulson@5312
   393
		                (Var(("A",maxidx+1), propT))
paulson@5312
   394
	      val rev_triv_goal' = instantiate' [] [Some ct] rev_triv_goal
paulson@5312
   395
              fun bic th = bicompose false (false, th, np')
paulson@5312
   396
          in  bic (Seq.hd (bic (restore st') 1 rev_triv_goal')) i st  end 
wenzelm@4270
   397
  in  Seq.flat (Seq.map next (tac eq_cprem))
paulson@2005
   398
  end;
paulson@2005
   399
paulson@1502
   400
fun SELECT_GOAL tac i st = 
paulson@5312
   401
  let val np = nprems_of st
paulson@5312
   402
  in  if 1<=i andalso i<=np then 
paulson@5312
   403
          (*If only one subgoal, then just apply tactic*)
paulson@5312
   404
	  if np=1 then tac st else select tac st i
paulson@5312
   405
      else Seq.empty
paulson@5312
   406
  end;
clasohm@0
   407
clasohm@0
   408
clasohm@0
   409
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
clasohm@0
   410
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters. 
clasohm@0
   411
  Main difference from strip_assums concerns parameters: 
clasohm@0
   412
    it replaces the bound variables by free variables.  *)
clasohm@0
   413
fun strip_context_aux (params, Hs, Const("==>", _) $ H $ B) = 
paulson@2244
   414
        strip_context_aux (params, H::Hs, B)
clasohm@0
   415
  | strip_context_aux (params, Hs, Const("all",_)$Abs(a,T,t)) =
clasohm@0
   416
        let val (b,u) = variant_abs(a,T,t)
paulson@2244
   417
        in  strip_context_aux ((b,T)::params, Hs, u)  end
clasohm@0
   418
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
clasohm@0
   419
clasohm@0
   420
fun strip_context A = strip_context_aux ([],[],A);
clasohm@0
   421
clasohm@0
   422
clasohm@0
   423
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
paulson@1502
   424
       METAHYPS (fn prems => tac prems) i
clasohm@0
   425
clasohm@0
   426
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
clasohm@0
   427
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
clasohm@0
   428
"prems").  The parameters x1,...,xm become free variables.  If the
clasohm@0
   429
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
clasohm@0
   430
then it is lifted back into the original context, yielding k subgoals.
clasohm@0
   431
clasohm@0
   432
Replaces unknowns in the context by Frees having the prefix METAHYP_
clasohm@0
   433
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
clasohm@0
   434
DOES NOT HANDLE TYPE UNKNOWNS.
clasohm@0
   435
****)
clasohm@0
   436
clasohm@0
   437
local 
clasohm@0
   438
clasohm@0
   439
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
clasohm@0
   440
    Instantiates distinct free variables by terms of same type.*)
clasohm@0
   441
  fun free_instantiate ctpairs = 
clasohm@0
   442
      forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
clasohm@0
   443
clasohm@0
   444
  fun free_of s ((a,i), T) =
clasohm@0
   445
        Free(s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i),
paulson@2244
   446
             T)
clasohm@0
   447
clasohm@0
   448
  fun mk_inst (var as Var(v,T))  = (var,  free_of "METAHYP1_" (v,T))
clasohm@0
   449
in
clasohm@0
   450
paulson@1502
   451
fun metahyps_aux_tac tacf (prem,i) state = 
clasohm@0
   452
  let val {sign,maxidx,...} = rep_thm state
lcp@230
   453
      val cterm = cterm_of sign
clasohm@0
   454
      (*find all vars in the hyps -- should find tvars also!*)
paulson@1502
   455
      val hyps_vars = foldr add_term_vars (Logic.strip_assums_hyp prem, [])
clasohm@0
   456
      val insts = map mk_inst hyps_vars
clasohm@0
   457
      (*replace the hyps_vars by Frees*)
clasohm@0
   458
      val prem' = subst_atomic insts prem
clasohm@0
   459
      val (params,hyps,concl) = strip_context prem'
clasohm@0
   460
      val fparams = map Free params
clasohm@0
   461
      val cparams = map cterm fparams
clasohm@0
   462
      and chyps = map cterm hyps
clasohm@0
   463
      val hypths = map assume chyps
clasohm@0
   464
      fun swap_ctpair (t,u) = (cterm u, cterm t)
clasohm@0
   465
      (*Subgoal variables: make Free; lift type over params*)
clasohm@0
   466
      fun mk_subgoal_inst concl_vars (var as Var(v,T)) = 
clasohm@0
   467
          if var mem concl_vars 
paulson@2244
   468
          then (var, true, free_of "METAHYP2_" (v,T))
paulson@2244
   469
          else (var, false,
paulson@2244
   470
                free_of "METAHYP2_" (v, map #2 params --->T))
clasohm@0
   471
      (*Instantiate subgoal vars by Free applied to params*)
clasohm@0
   472
      fun mk_ctpair (t,in_concl,u) = 
paulson@2244
   473
          if in_concl then (cterm t,  cterm u)
clasohm@0
   474
          else (cterm t,  cterm (list_comb (u,fparams)))
clasohm@0
   475
      (*Restore Vars with higher type and index*)
clasohm@0
   476
      fun mk_subgoal_swap_ctpair 
paulson@2244
   477
                (t as Var((a,i),_), in_concl, u as Free(_,U)) = 
paulson@2244
   478
          if in_concl then (cterm u, cterm t)
clasohm@0
   479
          else (cterm u, cterm(Var((a, i+maxidx), U)))
clasohm@0
   480
      (*Embed B in the original context of params and hyps*)
paulson@1502
   481
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
clasohm@0
   482
      (*Strip the context using elimination rules*)
clasohm@0
   483
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
clasohm@0
   484
      (*Embed an ff pair in the original params*)
paulson@1502
   485
      fun embed_ff(t,u) = Logic.mk_flexpair (list_abs_free (params, t), 
paulson@2244
   486
                                             list_abs_free (params, u))
clasohm@0
   487
      (*Remove parameter abstractions from the ff pairs*)
clasohm@0
   488
      fun elim_ff ff = flexpair_abs_elim_list cparams ff
clasohm@0
   489
      (*A form of lifting that discharges assumptions.*)
clasohm@0
   490
      fun relift st = 
paulson@2244
   491
        let val prop = #prop(rep_thm st)
paulson@2244
   492
            val subgoal_vars = (*Vars introduced in the subgoals*)
paulson@2244
   493
                  foldr add_term_vars (Logic.strip_imp_prems prop, [])
paulson@2244
   494
            and concl_vars = add_term_vars (Logic.strip_imp_concl prop, [])
paulson@2244
   495
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
paulson@2244
   496
            val st' = instantiate ([], map mk_ctpair subgoal_insts) st
paulson@2244
   497
            val emBs = map (cterm o embed) (prems_of st')
clasohm@0
   498
            and ffs = map (cterm o embed_ff) (tpairs_of st')
paulson@2244
   499
            val Cth  = implies_elim_list st' 
paulson@2244
   500
                            (map (elim_ff o assume) ffs @
paulson@2244
   501
                             map (elim o assume) emBs)
paulson@2244
   502
        in  (*restore the unknowns to the hypotheses*)
paulson@2244
   503
            free_instantiate (map swap_ctpair insts @
paulson@2244
   504
                              map mk_subgoal_swap_ctpair subgoal_insts)
paulson@2244
   505
                (*discharge assumptions from state in same order*)
paulson@2244
   506
                (implies_intr_list (ffs@emBs)
paulson@2244
   507
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
paulson@2244
   508
        end
clasohm@0
   509
      val subprems = map (forall_elim_vars 0) hypths
clasohm@0
   510
      and st0 = trivial (cterm concl)
clasohm@0
   511
      (*function to replace the current subgoal*)
clasohm@0
   512
      fun next st = bicompose false (false, relift st, nprems_of st)
paulson@2244
   513
                    i state
wenzelm@4270
   514
  in  Seq.flat (Seq.map next (tacf subprems st0))
paulson@1502
   515
  end;
clasohm@0
   516
end;
clasohm@0
   517
clasohm@0
   518
fun METAHYPS tacf = SUBGOAL (metahyps_aux_tac tacf);
clasohm@0
   519
clasohm@0
   520
end;
paulson@1502
   521
paulson@1502
   522
open Tactical;