src/Pure/tctical.ML
author wenzelm
Thu Jul 08 18:31:04 1999 +0200 (1999-07-08)
changeset 6930 4b40fb299f9f
parent 6390 5d58c100ca3f
child 7686 4731f10af2e6
permissions -rw-r--r--
improved error msgs of cterm_instantiate;
fixed incr_indexes;
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@6041
    41
  val print_tac         : string -> 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@6041
   195
fun print_tac msg = 
paulson@1502
   196
    (fn st => 
paulson@6041
   197
     (writeln msg;
paulson@6041
   198
      print_goals (!goals_limit) st; Seq.single st));
clasohm@0
   199
clasohm@0
   200
(*Pause until a line is typed -- if non-empty then fail. *)
paulson@1502
   201
fun pause_tac st =  
wenzelm@5957
   202
  (writeln "** Press RETURN to continue:";
wenzelm@4270
   203
   if TextIO.inputLine TextIO.stdIn = "\n" then Seq.single st
wenzelm@5956
   204
   else (writeln "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 *)
paulson@1502
   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@5956
   220
     | "x\n" => (writeln "Exiting now";  raise (TRACE_EXIT st))
clasohm@0
   221
     | "quit\n" => raise TRACE_QUIT
wenzelm@5956
   222
     | _     => (writeln
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@3669
   235
           then (print_goals (!goals_limit) st;
wenzelm@5957
   236
                 writeln "** 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*)
paulson@1502
   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
clasohm@0
   247
(*Deterministic REPEAT: only retains the first outcome; 
lcp@703
   248
  uses less space than REPEAT; tail recursive.
lcp@703
   249
  If non-negative, n bounds the number of repetitions.*)
lcp@703
   250
fun REPEAT_DETERM_N n tac = 
paulson@1502
   251
  let val tac = tracify trace_REPEAT tac
wenzelm@4270
   252
      fun drep 0 st = Some(st, Seq.empty)
paulson@2244
   253
        | drep n st =
wenzelm@4270
   254
           (case Seq.pull(tac st) of
wenzelm@4270
   255
                None       => Some(st, Seq.empty)
paulson@2244
   256
              | Some(st',_) => drep (n-1) st')
lcp@703
   257
  in  traced_tac (drep n)  end;
lcp@703
   258
lcp@703
   259
(*Allows any number of repetitions*)
lcp@703
   260
val REPEAT_DETERM = REPEAT_DETERM_N ~1;
clasohm@0
   261
clasohm@0
   262
(*General REPEAT: maintains a stack of alternatives; tail recursive*)
clasohm@0
   263
fun REPEAT tac = 
paulson@1502
   264
  let val tac = tracify trace_REPEAT tac
clasohm@0
   265
      fun rep qs st = 
wenzelm@4270
   266
        case Seq.pull(tac st) of
wenzelm@4270
   267
            None       => Some(st, Seq.make(fn()=> repq qs))
clasohm@0
   268
          | Some(st',q) => rep (q::qs) st'
clasohm@0
   269
      and repq [] = None
wenzelm@4270
   270
        | repq(q::qs) = case Seq.pull q of
paulson@2244
   271
            None       => repq qs
clasohm@0
   272
          | Some(st,q) => rep (q::qs) st
clasohm@0
   273
  in  traced_tac (rep [])  end;
clasohm@0
   274
clasohm@0
   275
(*Repeat 1 or more times*)
lcp@703
   276
fun REPEAT_DETERM1 tac = DETERM tac THEN REPEAT_DETERM tac;
clasohm@0
   277
fun REPEAT1 tac = tac THEN REPEAT tac;
clasohm@0
   278
clasohm@0
   279
clasohm@0
   280
(** Filtering tacticals **)
clasohm@0
   281
clasohm@0
   282
(*Returns all states satisfying the predicate*)
wenzelm@4270
   283
fun FILTER pred tac st = Seq.filter pred (tac st);
clasohm@0
   284
clasohm@0
   285
(*Returns all changed states*)
paulson@1643
   286
fun CHANGED tac st = 
paulson@1643
   287
    let fun diff st' = not (eq_thm(st,st'))
wenzelm@4270
   288
    in  Seq.filter diff (tac st)  end;
clasohm@0
   289
clasohm@0
   290
clasohm@0
   291
(*** Tacticals based on subgoal numbering ***)
clasohm@0
   292
paulson@1502
   293
(*For n subgoals, performs tac(n) THEN ... THEN tac(1) 
paulson@1502
   294
  Essential to work backwards since tac(i) may add/delete subgoals at i. *)
paulson@1502
   295
fun ALLGOALS tac st = 
paulson@1502
   296
  let fun doall 0 = all_tac
paulson@2244
   297
        | doall n = tac(n) THEN doall(n-1)
paulson@1502
   298
  in  doall(nprems_of st)st  end;
clasohm@0
   299
paulson@1502
   300
(*For n subgoals, performs tac(n) ORELSE ... ORELSE tac(1)  *)
paulson@1502
   301
fun SOMEGOAL tac st = 
paulson@1502
   302
  let fun find 0 = no_tac
paulson@2244
   303
        | find n = tac(n) ORELSE find(n-1)
paulson@1502
   304
  in  find(nprems_of st)st  end;
clasohm@0
   305
paulson@1502
   306
(*For n subgoals, performs tac(1) ORELSE ... ORELSE tac(n).
clasohm@0
   307
  More appropriate than SOMEGOAL in some cases.*)
paulson@1502
   308
fun FIRSTGOAL tac st = 
paulson@1502
   309
  let fun find (i,n) = if i>n then no_tac else  tac(i) ORELSE find (i+1,n)
paulson@1502
   310
  in  find(1, nprems_of st)st  end;
clasohm@0
   311
paulson@1502
   312
(*Repeatedly solve some using tac. *)
paulson@1502
   313
fun REPEAT_SOME tac = REPEAT1 (SOMEGOAL (REPEAT1 o tac));
paulson@1502
   314
fun REPEAT_DETERM_SOME tac = REPEAT_DETERM1 (SOMEGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   315
paulson@1502
   316
(*Repeatedly solve the first possible subgoal using tac. *)
paulson@1502
   317
fun REPEAT_FIRST tac = REPEAT1 (FIRSTGOAL (REPEAT1 o tac));
paulson@1502
   318
fun REPEAT_DETERM_FIRST tac = REPEAT_DETERM1 (FIRSTGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   319
paulson@1502
   320
(*For n subgoals, tries to apply tac to n,...1  *)
paulson@1502
   321
fun TRYALL tac = ALLGOALS (TRY o tac);
clasohm@0
   322
clasohm@0
   323
clasohm@0
   324
(*Make a tactic for subgoal i, if there is one.  *)
paulson@2580
   325
fun SUBGOAL goalfun i st = goalfun (List.nth(prems_of st, i-1),  i) st
wenzelm@4270
   326
                             handle Subscript => Seq.empty;
clasohm@0
   327
paulson@5141
   328
(*Returns all states that have changed in subgoal i, counted from the LAST
paulson@5141
   329
  subgoal.  For stac, for example.*)
paulson@5141
   330
fun CHANGED_GOAL tac i st = 
paulson@5997
   331
    let val j = nprems_of st
paulson@5141
   332
        val t = List.nth(prems_of st, i-1)
paulson@5997
   333
        fun diff st' = (*true if subgoal still exists and is same as old one*)
paulson@5997
   334
	    not (nprems_of st' >= j
paulson@5141
   335
		 andalso
paulson@5997
   336
		 Pattern.aeconv (t,
paulson@5997
   337
				 List.nth(prems_of st', nprems_of st' - j)))
paulson@5141
   338
    in  Seq.filter diff (tac i st)  end
paulson@5141
   339
    handle Subscript => Seq.empty  (*no subgoal i*);
paulson@5141
   340
wenzelm@4602
   341
fun ALLGOALS_RANGE tac (i:int) j st =
wenzelm@4602
   342
  if i > j then all_tac st
wenzelm@4602
   343
  else (tac j THEN ALLGOALS_RANGE tac i (j - 1)) st;
wenzelm@4602
   344
wenzelm@4602
   345
fun (tac1 THEN_ALL_NEW tac2) i st =
wenzelm@4602
   346
  st |> (tac1 i THEN (fn st' => ALLGOALS_RANGE tac2 i (i + nprems_of st' - nprems_of st) st'));
wenzelm@4602
   347
paulson@2005
   348
paulson@2005
   349
(*** SELECT_GOAL ***)
paulson@2005
   350
clasohm@0
   351
(*Tactical for restricting the effect of a tactic to subgoal i.
paulson@1502
   352
  Works by making a new state from subgoal i, applying tac to it, and
clasohm@0
   353
  composing the resulting metathm with the original state.
clasohm@0
   354
  The "main goal" of the new state will not be atomic, some tactics may fail!
clasohm@0
   355
  DOES NOT work if tactic affects the main goal other than by instantiation.*)
clasohm@0
   356
paulson@2005
   357
(*SELECT_GOAL optimization: replace the conclusion by a variable X,
paulson@2005
   358
  to avoid copying.  Proof states have X==concl as an assuption.*)
paulson@2005
   359
wenzelm@6390
   360
val prop_equals = cterm_of (Theory.sign_of ProtoPure.thy) 
paulson@2005
   361
                    (Const("==", propT-->propT-->propT));
paulson@2005
   362
paulson@2005
   363
fun mk_prop_equals(t,u) = capply (capply prop_equals t) u;
paulson@2005
   364
paulson@2005
   365
(*Like trivial but returns [ct==X] ct==>X instead of ct==>ct, if possible.
paulson@2005
   366
  It is paired with a function to undo the transformation.  If ct contains
paulson@2005
   367
  Vars then it returns ct==>ct.*)
paulson@5312
   368
paulson@2005
   369
fun eq_trivial ct =
wenzelm@6390
   370
  let val xfree = cterm_of (Theory.sign_of ProtoPure.thy)
paulson@5312
   371
                           (Free (gensym"EQ_TRIVIAL_", propT))
paulson@2158
   372
      val ct_eq_x = mk_prop_equals (ct, xfree)
paulson@2005
   373
      and refl_ct = reflexive ct
paulson@2005
   374
      fun restore th = 
paulson@2244
   375
          implies_elim 
paulson@2244
   376
            (forall_elim ct (forall_intr xfree (implies_intr ct_eq_x th)))
paulson@2244
   377
            refl_ct
paulson@2005
   378
  in  (equal_elim
paulson@2005
   379
         (combination (combination refl_implies refl_ct) (assume ct_eq_x))
paulson@5312
   380
         (Drule.mk_triv_goal ct),
paulson@2005
   381
       restore)
paulson@2005
   382
  end  (*Fails if there are Vars or TVars*)
paulson@5312
   383
    handle THM _ => (Drule.mk_triv_goal ct, I);
paulson@5312
   384
paulson@2005
   385
paulson@2005
   386
(*Does the work of SELECT_GOAL. *)
paulson@5312
   387
fun select tac st i =
paulson@2580
   388
  let val (eq_cprem, restore) = (*we hope maxidx goes to ~1*)
paulson@5312
   389
	  eq_trivial (adjust_maxidx (List.nth(cprems_of st, i-1)))
paulson@5312
   390
      fun next st' = 
paulson@5312
   391
	  let val np' = nprems_of st'
paulson@5312
   392
              (*rename the ?A in rev_triv_goal*)
paulson@5312
   393
	      val {maxidx,...} = rep_thm st'
wenzelm@6390
   394
              val ct = cterm_of (Theory.sign_of ProtoPure.thy)
paulson@5312
   395
		                (Var(("A",maxidx+1), propT))
wenzelm@5906
   396
	      val rev_triv_goal' = instantiate' [] [Some ct] Drule.rev_triv_goal
paulson@5312
   397
              fun bic th = bicompose false (false, th, np')
paulson@5312
   398
          in  bic (Seq.hd (bic (restore st') 1 rev_triv_goal')) i st  end 
wenzelm@4270
   399
  in  Seq.flat (Seq.map next (tac eq_cprem))
paulson@2005
   400
  end;
paulson@2005
   401
paulson@1502
   402
fun SELECT_GOAL tac i st = 
paulson@5312
   403
  let val np = nprems_of st
paulson@5312
   404
  in  if 1<=i andalso i<=np then 
paulson@5312
   405
          (*If only one subgoal, then just apply tactic*)
paulson@5312
   406
	  if np=1 then tac st else select tac st i
paulson@5312
   407
      else Seq.empty
paulson@5312
   408
  end;
clasohm@0
   409
clasohm@0
   410
clasohm@0
   411
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
clasohm@0
   412
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters. 
clasohm@0
   413
  Main difference from strip_assums concerns parameters: 
clasohm@0
   414
    it replaces the bound variables by free variables.  *)
clasohm@0
   415
fun strip_context_aux (params, Hs, Const("==>", _) $ H $ B) = 
paulson@2244
   416
        strip_context_aux (params, H::Hs, B)
clasohm@0
   417
  | strip_context_aux (params, Hs, Const("all",_)$Abs(a,T,t)) =
clasohm@0
   418
        let val (b,u) = variant_abs(a,T,t)
paulson@2244
   419
        in  strip_context_aux ((b,T)::params, Hs, u)  end
clasohm@0
   420
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
clasohm@0
   421
clasohm@0
   422
fun strip_context A = strip_context_aux ([],[],A);
clasohm@0
   423
clasohm@0
   424
clasohm@0
   425
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
paulson@1502
   426
       METAHYPS (fn prems => tac prems) i
clasohm@0
   427
clasohm@0
   428
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
clasohm@0
   429
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
clasohm@0
   430
"prems").  The parameters x1,...,xm become free variables.  If the
clasohm@0
   431
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
clasohm@0
   432
then it is lifted back into the original context, yielding k subgoals.
clasohm@0
   433
clasohm@0
   434
Replaces unknowns in the context by Frees having the prefix METAHYP_
clasohm@0
   435
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
clasohm@0
   436
DOES NOT HANDLE TYPE UNKNOWNS.
clasohm@0
   437
****)
clasohm@0
   438
clasohm@0
   439
local 
clasohm@0
   440
clasohm@0
   441
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
clasohm@0
   442
    Instantiates distinct free variables by terms of same type.*)
clasohm@0
   443
  fun free_instantiate ctpairs = 
clasohm@0
   444
      forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
clasohm@0
   445
clasohm@0
   446
  fun free_of s ((a,i), T) =
clasohm@0
   447
        Free(s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i),
paulson@2244
   448
             T)
clasohm@0
   449
clasohm@0
   450
  fun mk_inst (var as Var(v,T))  = (var,  free_of "METAHYP1_" (v,T))
clasohm@0
   451
in
clasohm@0
   452
paulson@1502
   453
fun metahyps_aux_tac tacf (prem,i) state = 
clasohm@0
   454
  let val {sign,maxidx,...} = rep_thm state
lcp@230
   455
      val cterm = cterm_of sign
clasohm@0
   456
      (*find all vars in the hyps -- should find tvars also!*)
paulson@1502
   457
      val hyps_vars = foldr add_term_vars (Logic.strip_assums_hyp prem, [])
clasohm@0
   458
      val insts = map mk_inst hyps_vars
clasohm@0
   459
      (*replace the hyps_vars by Frees*)
clasohm@0
   460
      val prem' = subst_atomic insts prem
clasohm@0
   461
      val (params,hyps,concl) = strip_context prem'
clasohm@0
   462
      val fparams = map Free params
clasohm@0
   463
      val cparams = map cterm fparams
clasohm@0
   464
      and chyps = map cterm hyps
clasohm@0
   465
      val hypths = map assume chyps
clasohm@0
   466
      fun swap_ctpair (t,u) = (cterm u, cterm t)
clasohm@0
   467
      (*Subgoal variables: make Free; lift type over params*)
clasohm@0
   468
      fun mk_subgoal_inst concl_vars (var as Var(v,T)) = 
clasohm@0
   469
          if var mem concl_vars 
paulson@2244
   470
          then (var, true, free_of "METAHYP2_" (v,T))
paulson@2244
   471
          else (var, false,
paulson@2244
   472
                free_of "METAHYP2_" (v, map #2 params --->T))
clasohm@0
   473
      (*Instantiate subgoal vars by Free applied to params*)
clasohm@0
   474
      fun mk_ctpair (t,in_concl,u) = 
paulson@2244
   475
          if in_concl then (cterm t,  cterm u)
clasohm@0
   476
          else (cterm t,  cterm (list_comb (u,fparams)))
clasohm@0
   477
      (*Restore Vars with higher type and index*)
clasohm@0
   478
      fun mk_subgoal_swap_ctpair 
paulson@2244
   479
                (t as Var((a,i),_), in_concl, u as Free(_,U)) = 
paulson@2244
   480
          if in_concl then (cterm u, cterm t)
clasohm@0
   481
          else (cterm u, cterm(Var((a, i+maxidx), U)))
clasohm@0
   482
      (*Embed B in the original context of params and hyps*)
paulson@1502
   483
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
clasohm@0
   484
      (*Strip the context using elimination rules*)
clasohm@0
   485
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
clasohm@0
   486
      (*Embed an ff pair in the original params*)
paulson@1502
   487
      fun embed_ff(t,u) = Logic.mk_flexpair (list_abs_free (params, t), 
paulson@2244
   488
                                             list_abs_free (params, u))
clasohm@0
   489
      (*Remove parameter abstractions from the ff pairs*)
clasohm@0
   490
      fun elim_ff ff = flexpair_abs_elim_list cparams ff
clasohm@0
   491
      (*A form of lifting that discharges assumptions.*)
clasohm@0
   492
      fun relift st = 
paulson@2244
   493
        let val prop = #prop(rep_thm st)
paulson@2244
   494
            val subgoal_vars = (*Vars introduced in the subgoals*)
paulson@2244
   495
                  foldr add_term_vars (Logic.strip_imp_prems prop, [])
paulson@2244
   496
            and concl_vars = add_term_vars (Logic.strip_imp_concl prop, [])
paulson@2244
   497
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
paulson@2244
   498
            val st' = instantiate ([], map mk_ctpair subgoal_insts) st
paulson@2244
   499
            val emBs = map (cterm o embed) (prems_of st')
clasohm@0
   500
            and ffs = map (cterm o embed_ff) (tpairs_of st')
paulson@2244
   501
            val Cth  = implies_elim_list st' 
paulson@2244
   502
                            (map (elim_ff o assume) ffs @
paulson@2244
   503
                             map (elim o assume) emBs)
paulson@2244
   504
        in  (*restore the unknowns to the hypotheses*)
paulson@2244
   505
            free_instantiate (map swap_ctpair insts @
paulson@2244
   506
                              map mk_subgoal_swap_ctpair subgoal_insts)
paulson@2244
   507
                (*discharge assumptions from state in same order*)
paulson@2244
   508
                (implies_intr_list (ffs@emBs)
paulson@2244
   509
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
paulson@2244
   510
        end
clasohm@0
   511
      val subprems = map (forall_elim_vars 0) hypths
clasohm@0
   512
      and st0 = trivial (cterm concl)
clasohm@0
   513
      (*function to replace the current subgoal*)
clasohm@0
   514
      fun next st = bicompose false (false, relift st, nprems_of st)
paulson@2244
   515
                    i state
wenzelm@4270
   516
  in  Seq.flat (Seq.map next (tacf subprems st0))
paulson@1502
   517
  end;
clasohm@0
   518
end;
clasohm@0
   519
clasohm@0
   520
fun METAHYPS tacf = SUBGOAL (metahyps_aux_tac tacf);
clasohm@0
   521
clasohm@0
   522
end;
paulson@1502
   523
paulson@1502
   524
open Tactical;