src/Pure/tctical.ML
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 24359 44556727197a
child 26626 c6231d64d264
permissions -rw-r--r--
Name.uu, Name.aT;
wenzelm@16179
     1
(*  Title:      Pure/tctical.ML
clasohm@0
     2
    ID:         $Id$
paulson@2244
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
wenzelm@16179
     6
Tacticals.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@4602
     9
infix 1 THEN THEN' THEN_ALL_NEW;
clasohm@0
    10
infix 0 ORELSE APPEND INTLEAVE ORELSE' APPEND' INTLEAVE';
lcp@671
    11
infix 0 THEN_ELSE;
lcp@671
    12
clasohm@0
    13
signature TACTICAL =
wenzelm@11916
    14
sig
wenzelm@23538
    15
  type tactic = thm -> thm Seq.seq
wenzelm@23538
    16
  val THEN: tactic * tactic -> tactic
wenzelm@23538
    17
  val ORELSE: tactic * tactic -> tactic
wenzelm@23538
    18
  val APPEND: tactic * tactic -> tactic
wenzelm@23538
    19
  val INTLEAVE: tactic * tactic -> tactic
wenzelm@23538
    20
  val THEN_ELSE: tactic * (tactic*tactic) -> tactic
wenzelm@23538
    21
  val THEN': ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
wenzelm@23538
    22
  val ORELSE': ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
wenzelm@23538
    23
  val APPEND': ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
wenzelm@23538
    24
  val INTLEAVE': ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
wenzelm@23538
    25
  val all_tac: tactic
wenzelm@23538
    26
  val no_tac: tactic
wenzelm@23538
    27
  val DETERM: tactic -> tactic
wenzelm@23538
    28
  val COND: (thm -> bool) -> tactic -> tactic -> tactic
wenzelm@23538
    29
  val TRY: tactic -> tactic
wenzelm@23538
    30
  val EVERY: tactic list -> tactic
wenzelm@23538
    31
  val EVERY': ('a -> tactic) list -> 'a -> tactic
wenzelm@23538
    32
  val EVERY1: (int -> tactic) list -> tactic
wenzelm@23538
    33
  val FIRST: tactic list -> tactic
wenzelm@23538
    34
  val FIRST': ('a -> tactic) list -> 'a -> tactic
wenzelm@23538
    35
  val FIRST1: (int -> tactic) list -> tactic
wenzelm@23538
    36
  val RANGE: (int -> tactic) list -> int -> tactic
wenzelm@23538
    37
  val print_tac: string -> tactic
wenzelm@23538
    38
  val pause_tac: tactic
wenzelm@23538
    39
  val trace_REPEAT: bool ref
wenzelm@23538
    40
  val suppress_tracing: bool ref
wenzelm@23538
    41
  val tracify: bool ref -> tactic -> tactic
wenzelm@23538
    42
  val traced_tac: (thm -> (thm * thm Seq.seq) option) -> tactic
wenzelm@23538
    43
  val DETERM_UNTIL: (thm -> bool) -> tactic -> tactic
wenzelm@23538
    44
  val REPEAT_DETERM_N: int -> tactic -> tactic
wenzelm@23538
    45
  val REPEAT_DETERM: tactic -> tactic
wenzelm@23538
    46
  val REPEAT: tactic -> tactic
wenzelm@23538
    47
  val REPEAT_DETERM1: tactic -> tactic
wenzelm@23538
    48
  val REPEAT1: tactic -> tactic
wenzelm@23538
    49
  val FILTER: (thm -> bool) -> tactic -> tactic
wenzelm@23538
    50
  val CHANGED: tactic -> tactic
wenzelm@23538
    51
  val CHANGED_PROP: tactic -> tactic
wenzelm@23538
    52
  val ALLGOALS: (int -> tactic) -> tactic
wenzelm@23538
    53
  val SOMEGOAL: (int -> tactic) -> tactic
wenzelm@23538
    54
  val FIRSTGOAL: (int -> tactic) -> tactic
wenzelm@23538
    55
  val REPEAT_SOME: (int -> tactic) -> tactic
wenzelm@23538
    56
  val REPEAT_DETERM_SOME: (int -> tactic) -> tactic
wenzelm@23538
    57
  val REPEAT_FIRST: (int -> tactic) -> tactic
lcp@703
    58
  val REPEAT_DETERM_FIRST: (int -> tactic) -> tactic
wenzelm@23538
    59
  val TRYALL: (int -> tactic) -> tactic
wenzelm@23538
    60
  val CSUBGOAL: ((cterm * int) -> tactic) -> int -> tactic
wenzelm@23538
    61
  val SUBGOAL: ((term * int) -> tactic) -> int -> tactic
wenzelm@23538
    62
  val CHANGED_GOAL: (int -> tactic) -> int -> tactic
wenzelm@23538
    63
  val THEN_ALL_NEW: (int -> tactic) * (int -> tactic) -> int -> tactic
wenzelm@23538
    64
  val REPEAT_ALL_NEW: (int -> tactic) -> int -> tactic
wenzelm@23538
    65
  val strip_context: term -> (string * typ) list * term list * term
wenzelm@23538
    66
  val metahyps_thms: int -> thm -> thm list option
wenzelm@23538
    67
  val METAHYPS: (thm list -> tactic) -> int -> tactic
wenzelm@23538
    68
  val PRIMSEQ: (thm -> thm Seq.seq) -> tactic
wenzelm@23538
    69
  val PRIMITIVE: (thm -> thm) -> tactic
wenzelm@23538
    70
  val SINGLE: tactic -> thm -> thm option
wenzelm@23538
    71
  val CONVERSION: conv -> int -> tactic
wenzelm@11916
    72
end;
clasohm@0
    73
wenzelm@13108
    74
structure Tactical : TACTICAL =
clasohm@0
    75
struct
clasohm@0
    76
clasohm@0
    77
(**** Tactics ****)
clasohm@0
    78
clasohm@0
    79
(*A tactic maps a proof tree to a sequence of proof trees:
clasohm@0
    80
    if length of sequence = 0 then the tactic does not apply;
clasohm@0
    81
    if length > 1 then backtracking on the alternatives can occur.*)
clasohm@0
    82
wenzelm@4270
    83
type tactic = thm -> thm Seq.seq;
clasohm@0
    84
clasohm@0
    85
clasohm@0
    86
(*** LCF-style tacticals ***)
clasohm@0
    87
clasohm@0
    88
(*the tactical THEN performs one tactic followed by another*)
wenzelm@17344
    89
fun (tac1 THEN tac2) st = Seq.maps tac2 (tac1 st);
clasohm@0
    90
clasohm@0
    91
clasohm@0
    92
(*The tactical ORELSE uses the first tactic that returns a nonempty sequence.
clasohm@0
    93
  Like in LCF, ORELSE commits to either tac1 or tac2 immediately.
clasohm@0
    94
  Does not backtrack to tac2 if tac1 was initially chosen. *)
paulson@1502
    95
fun (tac1 ORELSE tac2) st =
wenzelm@4270
    96
    case Seq.pull(tac1 st) of
skalberg@15531
    97
        NONE       => tac2 st
wenzelm@4270
    98
      | sequencecell => Seq.make(fn()=> sequencecell);
clasohm@0
    99
clasohm@0
   100
clasohm@0
   101
(*The tactical APPEND combines the results of two tactics.
clasohm@0
   102
  Like ORELSE, but allows backtracking on both tac1 and tac2.
clasohm@0
   103
  The tactic tac2 is not applied until needed.*)
wenzelm@13108
   104
fun (tac1 APPEND tac2) st =
wenzelm@19861
   105
  Seq.append (tac1 st) (Seq.make(fn()=> Seq.pull (tac2 st)));
clasohm@0
   106
clasohm@0
   107
(*Like APPEND, but interleaves results of tac1 and tac2.*)
wenzelm@13108
   108
fun (tac1 INTLEAVE tac2) st =
wenzelm@4270
   109
    Seq.interleave(tac1 st,
wenzelm@4270
   110
                        Seq.make(fn()=> Seq.pull (tac2 st)));
clasohm@0
   111
lcp@671
   112
(*Conditional tactic.
paulson@2244
   113
        tac1 ORELSE tac2 = tac1 THEN_ELSE (all_tac, tac2)
paulson@2244
   114
        tac1 THEN tac2   = tac1 THEN_ELSE (tac2, no_tac)
lcp@671
   115
*)
wenzelm@13108
   116
fun (tac THEN_ELSE (tac1, tac2)) st =
wenzelm@4270
   117
    case Seq.pull(tac st) of
wenzelm@17344
   118
        NONE    => tac2 st                                   (*failed; try tactic 2*)
wenzelm@17344
   119
      | seqcell => Seq.maps tac1 (Seq.make(fn()=> seqcell)); (*succeeded; use tactic 1*)
lcp@671
   120
lcp@671
   121
clasohm@0
   122
(*Versions for combining tactic-valued functions, as in
clasohm@0
   123
     SOMEGOAL (resolve_tac rls THEN' assume_tac) *)
paulson@1502
   124
fun (tac1 THEN' tac2) x = tac1 x THEN tac2 x;
paulson@1502
   125
fun (tac1 ORELSE' tac2) x = tac1 x ORELSE tac2 x;
paulson@1502
   126
fun (tac1 APPEND' tac2) x = tac1 x APPEND tac2 x;
paulson@1502
   127
fun (tac1 INTLEAVE' tac2) x = tac1 x INTLEAVE tac2 x;
clasohm@0
   128
clasohm@0
   129
(*passes all proofs through unchanged;  identity of THEN*)
wenzelm@4270
   130
fun all_tac st = Seq.single st;
clasohm@0
   131
clasohm@0
   132
(*passes no proofs through;  identity of ORELSE and APPEND*)
wenzelm@4270
   133
fun no_tac st  = Seq.empty;
clasohm@0
   134
clasohm@0
   135
clasohm@0
   136
(*Make a tactic deterministic by chopping the tail of the proof sequence*)
wenzelm@12851
   137
fun DETERM tac = Seq.DETERM tac;
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
wenzelm@13108
   152
  fun EVY (trail, []) st =
skalberg@15531
   153
        Seq.make (fn()=> SOME(st,
wenzelm@13108
   154
                        Seq.make (fn()=> Seq.pull (evyBack trail))))
wenzelm@13108
   155
    | EVY (trail, tac::tacs) st =
wenzelm@13108
   156
          case Seq.pull(tac st) of
skalberg@15531
   157
              NONE    => evyBack trail              (*failed: backtrack*)
skalberg@15531
   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@13108
   161
          case Seq.pull q of
skalberg@15531
   162
              NONE        => evyBack trail
wenzelm@22360
   163
            | SOME(st,q') => if Thm.eq_thm (st',st)
wenzelm@13108
   164
                             then evyBack ((st',q',tacs)::trail)
wenzelm@13108
   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   *)
wenzelm@23178
   180
fun FIRST tacs = fold_rev (curry op ORELSE) tacs no_tac;
clasohm@0
   181
paulson@1502
   182
(* FIRST' [tac1,...,tacn] i  equals    tac1 i ORELSE ... ORELSE tacn i   *)
wenzelm@23178
   183
fun FIRST' tacs = fold_rev (curry 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
wenzelm@11916
   188
(*Apply tactics on consecutive subgoals*)
wenzelm@11916
   189
fun RANGE [] _ = all_tac
wenzelm@11916
   190
  | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
wenzelm@11916
   191
clasohm@0
   192
clasohm@0
   193
(*** Tracing tactics ***)
clasohm@0
   194
clasohm@0
   195
(*Print the current proof state and pass it on.*)
wenzelm@13108
   196
fun print_tac msg =
wenzelm@13108
   197
    (fn st =>
wenzelm@12262
   198
     (tracing msg;
wenzelm@23224
   199
      tracing ((Pretty.string_of o Pretty.chunks o
wenzelm@23224
   200
                 Display.pretty_goals (! Display.goals_limit)) st);
schirmer@15017
   201
      Seq.single st));
clasohm@0
   202
clasohm@0
   203
(*Pause until a line is typed -- if non-empty then fail. *)
wenzelm@13108
   204
fun pause_tac st =
wenzelm@12262
   205
  (tracing "** Press RETURN to continue:";
wenzelm@24359
   206
   if TextIO.inputLine TextIO.stdIn = SOME "\n" then Seq.single st
wenzelm@12262
   207
   else (tracing "Goodbye";  Seq.empty));
clasohm@0
   208
clasohm@0
   209
exception TRACE_EXIT of thm
clasohm@0
   210
and TRACE_QUIT;
clasohm@0
   211
lcp@631
   212
(*Tracing flags*)
lcp@631
   213
val trace_REPEAT= ref false
lcp@631
   214
and suppress_tracing = ref false;
lcp@631
   215
clasohm@0
   216
(*Handle all tracing commands for current state and tactic *)
wenzelm@13108
   217
fun exec_trace_command flag (tac, st) =
wenzelm@24359
   218
   case TextIO.inputLine TextIO.stdIn of
wenzelm@23139
   219
       SOME "\n" => tac st
wenzelm@23139
   220
     | SOME "f\n" => Seq.empty
wenzelm@23139
   221
     | SOME "o\n" => (flag:=false;  tac st)
wenzelm@23139
   222
     | SOME "s\n" => (suppress_tracing:=true;  tac st)
wenzelm@23139
   223
     | SOME "x\n" => (tracing "Exiting now";  raise (TRACE_EXIT st))
wenzelm@23139
   224
     | SOME "quit\n" => raise TRACE_QUIT
wenzelm@12262
   225
     | _     => (tracing
clasohm@0
   226
"Type RETURN to continue or...\n\
clasohm@0
   227
\     f    - to fail here\n\
clasohm@0
   228
\     o    - to switch tracing off\n\
lcp@631
   229
\     s    - to suppress tracing until next entry to a tactical\n\
clasohm@0
   230
\     x    - to exit at this point\n\
clasohm@0
   231
\     quit - to abort this tracing run\n\
paulson@1502
   232
\** Well? "     ;  exec_trace_command flag (tac, st));
clasohm@0
   233
clasohm@0
   234
clasohm@0
   235
(*Extract from a tactic, a thm->thm seq function that handles tracing*)
paulson@1502
   236
fun tracify flag tac st =
lcp@631
   237
  if !flag andalso not (!suppress_tracing)
wenzelm@12082
   238
           then (Display.print_goals (! Display.goals_limit) st;
wenzelm@12262
   239
                 tracing "** Press RETURN to continue:";
paulson@2244
   240
                 exec_trace_command flag (tac,st))
paulson@1502
   241
  else tac st;
clasohm@0
   242
clasohm@0
   243
(*Create a tactic whose outcome is given by seqf, handling TRACE_EXIT*)
wenzelm@13108
   244
fun traced_tac seqf st =
lcp@631
   245
    (suppress_tracing := false;
wenzelm@4270
   246
     Seq.make (fn()=> seqf st
skalberg@15531
   247
                         handle TRACE_EXIT st' => SOME(st', Seq.empty)));
clasohm@0
   248
clasohm@0
   249
oheimb@8149
   250
(*Deterministic DO..UNTIL: only retains the first outcome; tail recursive.
oheimb@8149
   251
  Forces repitition until predicate on state is fulfilled.*)
wenzelm@13108
   252
fun DETERM_UNTIL p tac =
oheimb@8149
   253
let val tac = tracify trace_REPEAT tac
skalberg@15531
   254
    fun drep st = if p st then SOME (st, Seq.empty)
oheimb@8149
   255
                          else (case Seq.pull(tac st) of
skalberg@15531
   256
                                  NONE        => NONE
skalberg@15531
   257
                                | SOME(st',_) => drep st')
oheimb@8149
   258
in  traced_tac drep  end;
oheimb@8149
   259
wenzelm@13108
   260
(*Deterministic REPEAT: only retains the first outcome;
lcp@703
   261
  uses less space than REPEAT; tail recursive.
lcp@703
   262
  If non-negative, n bounds the number of repetitions.*)
wenzelm@13108
   263
fun REPEAT_DETERM_N n tac =
paulson@1502
   264
  let val tac = tracify trace_REPEAT tac
skalberg@15531
   265
      fun drep 0 st = SOME(st, Seq.empty)
paulson@2244
   266
        | drep n st =
wenzelm@4270
   267
           (case Seq.pull(tac st) of
skalberg@15531
   268
                NONE       => SOME(st, Seq.empty)
skalberg@15531
   269
              | SOME(st',_) => drep (n-1) st')
lcp@703
   270
  in  traced_tac (drep n)  end;
lcp@703
   271
lcp@703
   272
(*Allows any number of repetitions*)
lcp@703
   273
val REPEAT_DETERM = REPEAT_DETERM_N ~1;
clasohm@0
   274
clasohm@0
   275
(*General REPEAT: maintains a stack of alternatives; tail recursive*)
wenzelm@13108
   276
fun REPEAT tac =
paulson@1502
   277
  let val tac = tracify trace_REPEAT tac
wenzelm@13108
   278
      fun rep qs st =
wenzelm@4270
   279
        case Seq.pull(tac st) of
skalberg@15531
   280
            NONE       => SOME(st, Seq.make(fn()=> repq qs))
skalberg@15531
   281
          | SOME(st',q) => rep (q::qs) st'
skalberg@15531
   282
      and repq [] = NONE
wenzelm@4270
   283
        | repq(q::qs) = case Seq.pull q of
skalberg@15531
   284
            NONE       => repq qs
skalberg@15531
   285
          | SOME(st,q) => rep (q::qs) st
clasohm@0
   286
  in  traced_tac (rep [])  end;
clasohm@0
   287
clasohm@0
   288
(*Repeat 1 or more times*)
lcp@703
   289
fun REPEAT_DETERM1 tac = DETERM tac THEN REPEAT_DETERM tac;
clasohm@0
   290
fun REPEAT1 tac = tac THEN REPEAT tac;
clasohm@0
   291
clasohm@0
   292
clasohm@0
   293
(** Filtering tacticals **)
clasohm@0
   294
wenzelm@4270
   295
fun FILTER pred tac st = Seq.filter pred (tac st);
clasohm@0
   296
paulson@13650
   297
(*Accept only next states that change the theorem somehow*)
wenzelm@13108
   298
fun CHANGED tac st =
wenzelm@13108
   299
  let fun diff st' = not (Thm.eq_thm (st, st'));
wenzelm@13108
   300
  in Seq.filter diff (tac st) end;
clasohm@0
   301
paulson@13650
   302
(*Accept only next states that change the theorem's prop field
paulson@13650
   303
  (changes to signature, hyps, etc. don't count)*)
wenzelm@13108
   304
fun CHANGED_PROP tac st =
wenzelm@22360
   305
  let fun diff st' = not (Thm.eq_thm_prop (st, st'));
wenzelm@13108
   306
  in Seq.filter diff (tac st) end;
wenzelm@10821
   307
clasohm@0
   308
clasohm@0
   309
(*** Tacticals based on subgoal numbering ***)
clasohm@0
   310
wenzelm@13108
   311
(*For n subgoals, performs tac(n) THEN ... THEN tac(1)
paulson@1502
   312
  Essential to work backwards since tac(i) may add/delete subgoals at i. *)
wenzelm@13108
   313
fun ALLGOALS tac st =
paulson@1502
   314
  let fun doall 0 = all_tac
paulson@2244
   315
        | doall n = tac(n) THEN doall(n-1)
paulson@1502
   316
  in  doall(nprems_of st)st  end;
clasohm@0
   317
paulson@1502
   318
(*For n subgoals, performs tac(n) ORELSE ... ORELSE tac(1)  *)
wenzelm@13108
   319
fun SOMEGOAL tac st =
paulson@1502
   320
  let fun find 0 = no_tac
paulson@2244
   321
        | find n = tac(n) ORELSE find(n-1)
paulson@1502
   322
  in  find(nprems_of st)st  end;
clasohm@0
   323
paulson@1502
   324
(*For n subgoals, performs tac(1) ORELSE ... ORELSE tac(n).
clasohm@0
   325
  More appropriate than SOMEGOAL in some cases.*)
wenzelm@13108
   326
fun FIRSTGOAL tac st =
paulson@1502
   327
  let fun find (i,n) = if i>n then no_tac else  tac(i) ORELSE find (i+1,n)
paulson@1502
   328
  in  find(1, nprems_of st)st  end;
clasohm@0
   329
paulson@1502
   330
(*Repeatedly solve some using tac. *)
paulson@1502
   331
fun REPEAT_SOME tac = REPEAT1 (SOMEGOAL (REPEAT1 o tac));
paulson@1502
   332
fun REPEAT_DETERM_SOME tac = REPEAT_DETERM1 (SOMEGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   333
paulson@1502
   334
(*Repeatedly solve the first possible subgoal using tac. *)
paulson@1502
   335
fun REPEAT_FIRST tac = REPEAT1 (FIRSTGOAL (REPEAT1 o tac));
paulson@1502
   336
fun REPEAT_DETERM_FIRST tac = REPEAT_DETERM1 (FIRSTGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   337
paulson@1502
   338
(*For n subgoals, tries to apply tac to n,...1  *)
paulson@1502
   339
fun TRYALL tac = ALLGOALS (TRY o tac);
clasohm@0
   340
clasohm@0
   341
clasohm@0
   342
(*Make a tactic for subgoal i, if there is one.  *)
wenzelm@23224
   343
fun CSUBGOAL goalfun i st =
wenzelm@23224
   344
  (case SOME (Thm.cprem_of st i) handle THM _ => NONE of
wenzelm@16510
   345
    SOME goal => goalfun (goal, i) st
wenzelm@16510
   346
  | NONE => Seq.empty);
clasohm@0
   347
wenzelm@23224
   348
fun SUBGOAL goalfun =
wenzelm@23224
   349
  CSUBGOAL (fn (goal, i) => goalfun (Thm.term_of goal, i));
wenzelm@23224
   350
paulson@5141
   351
(*Returns all states that have changed in subgoal i, counted from the LAST
paulson@5141
   352
  subgoal.  For stac, for example.*)
wenzelm@13108
   353
fun CHANGED_GOAL tac i st =
paulson@7686
   354
    let val np = nprems_of st
paulson@7686
   355
        val d = np-i                 (*distance from END*)
paulson@5141
   356
        val t = List.nth(prems_of st, i-1)
wenzelm@13108
   357
        fun diff st' =
wenzelm@13108
   358
            nprems_of st' - d <= 0   (*the subgoal no longer exists*)
wenzelm@13108
   359
            orelse
paulson@7686
   360
             not (Pattern.aeconv (t,
wenzelm@13108
   361
                                  List.nth(prems_of st',
wenzelm@13108
   362
                                           nprems_of st' - d - 1)))
paulson@5141
   363
    in  Seq.filter diff (tac i st)  end
paulson@5141
   364
    handle Subscript => Seq.empty  (*no subgoal i*);
paulson@5141
   365
wenzelm@4602
   366
fun (tac1 THEN_ALL_NEW tac2) i st =
wenzelm@8535
   367
  st |> (tac1 i THEN (fn st' => Seq.INTERVAL tac2 i (i + nprems_of st' - nprems_of st) st'));
wenzelm@4602
   368
wenzelm@8341
   369
(*repeatedly dig into any emerging subgoals*)
wenzelm@8341
   370
fun REPEAT_ALL_NEW tac =
wenzelm@8341
   371
  tac THEN_ALL_NEW (TRY o (fn i => REPEAT_ALL_NEW tac i));
wenzelm@8341
   372
paulson@2005
   373
clasohm@0
   374
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
wenzelm@13108
   375
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters.
wenzelm@13108
   376
  Main difference from strip_assums concerns parameters:
clasohm@0
   377
    it replaces the bound variables by free variables.  *)
wenzelm@13108
   378
fun strip_context_aux (params, Hs, Const("==>", _) $ H $ B) =
paulson@2244
   379
        strip_context_aux (params, H::Hs, B)
clasohm@0
   380
  | strip_context_aux (params, Hs, Const("all",_)$Abs(a,T,t)) =
wenzelm@20194
   381
        let val (b,u) = Syntax.variant_abs(a,T,t)
paulson@2244
   382
        in  strip_context_aux ((b,T)::params, Hs, u)  end
clasohm@0
   383
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
clasohm@0
   384
clasohm@0
   385
fun strip_context A = strip_context_aux ([],[],A);
clasohm@0
   386
clasohm@0
   387
clasohm@0
   388
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
paulson@1502
   389
       METAHYPS (fn prems => tac prems) i
clasohm@0
   390
clasohm@0
   391
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
clasohm@0
   392
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
clasohm@0
   393
"prems").  The parameters x1,...,xm become free variables.  If the
clasohm@0
   394
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
clasohm@0
   395
then it is lifted back into the original context, yielding k subgoals.
clasohm@0
   396
clasohm@0
   397
Replaces unknowns in the context by Frees having the prefix METAHYP_
clasohm@0
   398
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
clasohm@0
   399
DOES NOT HANDLE TYPE UNKNOWNS.
clasohm@0
   400
****)
clasohm@0
   401
wenzelm@13108
   402
local
clasohm@0
   403
clasohm@0
   404
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
clasohm@0
   405
    Instantiates distinct free variables by terms of same type.*)
wenzelm@13108
   406
  fun free_instantiate ctpairs =
clasohm@0
   407
      forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
clasohm@0
   408
clasohm@0
   409
  fun free_of s ((a,i), T) =
clasohm@0
   410
        Free(s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i),
paulson@2244
   411
             T)
clasohm@0
   412
clasohm@0
   413
  fun mk_inst (var as Var(v,T))  = (var,  free_of "METAHYP1_" (v,T))
clasohm@0
   414
in
clasohm@0
   415
paulson@19153
   416
(*Common code for METAHYPS and metahyps_thms*)
paulson@19153
   417
fun metahyps_split_prem prem =
paulson@19153
   418
  let (*find all vars in the hyps -- should find tvars also!*)
wenzelm@23178
   419
      val hyps_vars = List.foldr add_term_vars [] (Logic.strip_assums_hyp prem)
clasohm@0
   420
      val insts = map mk_inst hyps_vars
clasohm@0
   421
      (*replace the hyps_vars by Frees*)
clasohm@0
   422
      val prem' = subst_atomic insts prem
clasohm@0
   423
      val (params,hyps,concl) = strip_context prem'
paulson@19153
   424
  in (insts,params,hyps,concl)  end;
paulson@19153
   425
paulson@19153
   426
fun metahyps_aux_tac tacf (prem,gno) state =
wenzelm@23224
   427
  let val (insts,params,hyps,concl) = metahyps_split_prem prem
wenzelm@22596
   428
      val {thy = sign,maxidx,...} = rep_thm state
paulson@19153
   429
      val cterm = cterm_of sign
paulson@19153
   430
      val chyps = map cterm hyps
paulson@19153
   431
      val hypths = map assume chyps
paulson@19153
   432
      val subprems = map (forall_elim_vars 0) hypths
clasohm@0
   433
      val fparams = map Free params
clasohm@0
   434
      val cparams = map cterm fparams
clasohm@0
   435
      fun swap_ctpair (t,u) = (cterm u, cterm t)
clasohm@0
   436
      (*Subgoal variables: make Free; lift type over params*)
wenzelm@13108
   437
      fun mk_subgoal_inst concl_vars (var as Var(v,T)) =
wenzelm@20664
   438
          if member (op =) concl_vars var
paulson@2244
   439
          then (var, true, free_of "METAHYP2_" (v,T))
paulson@2244
   440
          else (var, false,
paulson@2244
   441
                free_of "METAHYP2_" (v, map #2 params --->T))
clasohm@0
   442
      (*Instantiate subgoal vars by Free applied to params*)
wenzelm@13108
   443
      fun mk_ctpair (t,in_concl,u) =
paulson@2244
   444
          if in_concl then (cterm t,  cterm u)
clasohm@0
   445
          else (cterm t,  cterm (list_comb (u,fparams)))
clasohm@0
   446
      (*Restore Vars with higher type and index*)
wenzelm@13108
   447
      fun mk_subgoal_swap_ctpair
wenzelm@13108
   448
                (t as Var((a,i),_), in_concl, u as Free(_,U)) =
paulson@2244
   449
          if in_concl then (cterm u, cterm t)
clasohm@0
   450
          else (cterm u, cterm(Var((a, i+maxidx), U)))
clasohm@0
   451
      (*Embed B in the original context of params and hyps*)
paulson@1502
   452
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
clasohm@0
   453
      (*Strip the context using elimination rules*)
clasohm@0
   454
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
clasohm@0
   455
      (*A form of lifting that discharges assumptions.*)
wenzelm@13108
   456
      fun relift st =
wenzelm@22596
   457
        let val prop = Thm.prop_of st
paulson@2244
   458
            val subgoal_vars = (*Vars introduced in the subgoals*)
wenzelm@23178
   459
                  List.foldr add_term_vars [] (Logic.strip_imp_prems prop)
paulson@2244
   460
            and concl_vars = add_term_vars (Logic.strip_imp_concl prop, [])
paulson@2244
   461
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
berghofe@13664
   462
            val st' = Thm.instantiate ([], map mk_ctpair subgoal_insts) st
paulson@2244
   463
            val emBs = map (cterm o embed) (prems_of st')
berghofe@13664
   464
            val Cth  = implies_elim_list st' (map (elim o assume) emBs)
paulson@2244
   465
        in  (*restore the unknowns to the hypotheses*)
paulson@2244
   466
            free_instantiate (map swap_ctpair insts @
paulson@2244
   467
                              map mk_subgoal_swap_ctpair subgoal_insts)
paulson@2244
   468
                (*discharge assumptions from state in same order*)
berghofe@13664
   469
                (implies_intr_list emBs
paulson@2244
   470
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
paulson@2244
   471
        end
clasohm@0
   472
      (*function to replace the current subgoal*)
clasohm@0
   473
      fun next st = bicompose false (false, relift st, nprems_of st)
paulson@19153
   474
                    gno state
paulson@19153
   475
  in Seq.maps next (tacf subprems (trivial (cterm concl))) end;
paulson@19153
   476
clasohm@0
   477
end;
clasohm@0
   478
paulson@19153
   479
(*Returns the theorem list that METAHYPS would supply to its tactic*)
paulson@19153
   480
fun metahyps_thms i state =
wenzelm@23224
   481
  let val prem = Logic.nth_prem (i, Thm.prop_of state)
paulson@23384
   482
      and cterm = cterm_of (Thm.theory_of_thm state)
paulson@23384
   483
      val (_,_,hyps,_) = metahyps_split_prem prem
paulson@19153
   484
  in SOME (map (forall_elim_vars 0 o assume o cterm) hyps) end
paulson@19153
   485
  handle TERM ("nth_prem", [A]) => NONE;
paulson@19153
   486
haftmann@19455
   487
local
mengj@19229
   488
mengj@19229
   489
fun print_vars_terms thy (n,thm) =
haftmann@19455
   490
  let
wenzelm@19646
   491
    fun typed ty = " has type: " ^ Sign.string_of_typ thy ty;
haftmann@19455
   492
    fun find_vars thy (Const (c, ty)) =
haftmann@19455
   493
        (case Term.typ_tvars ty
haftmann@19455
   494
         of [] => I
wenzelm@19646
   495
          | _ => insert (op =) (c ^ typed ty))
wenzelm@19646
   496
      | find_vars thy (Var (xi, ty)) = insert (op =) (Term.string_of_vname xi ^ typed ty)
haftmann@19455
   497
      | find_vars _ (Free _) = I
haftmann@19455
   498
      | find_vars _ (Bound _) = I
haftmann@19455
   499
      | find_vars thy (Abs (_, _, t)) = find_vars thy t
wenzelm@23224
   500
      | find_vars thy (t1 $ t2) =
haftmann@19455
   501
          find_vars thy t1 #> find_vars thy t1;
haftmann@19455
   502
    val prem = Logic.nth_prem (n, Thm.prop_of thm)
haftmann@19455
   503
    val tms = find_vars thy prem []
haftmann@19455
   504
  in
haftmann@19455
   505
    (warning "Found schematic vars in assumptions:"; warning (cat_lines tms))
haftmann@19455
   506
  end;
haftmann@19455
   507
haftmann@19455
   508
in
mengj@19229
   509
mengj@19229
   510
fun METAHYPS tacf n thm = SUBGOAL (metahyps_aux_tac tacf) n thm
wenzelm@23224
   511
  handle THM("assume: variables",_,_) => (print_vars_terms (theory_of_thm thm) (n,thm); Seq.empty)
clasohm@0
   512
wenzelm@23224
   513
end;
haftmann@19455
   514
skalberg@15006
   515
(*Makes a tactic whose effect on a state is given by thmfun: thm->thm seq.*)
skalberg@15006
   516
fun PRIMSEQ thmfun st =  thmfun st handle THM _ => Seq.empty;
skalberg@15006
   517
skalberg@15006
   518
(*Makes a tactic whose effect on a state is given by thmfun: thm->thm.*)
skalberg@15006
   519
fun PRIMITIVE thmfun = PRIMSEQ (Seq.single o thmfun);
skalberg@15006
   520
wenzelm@23538
   521
(*Inverse (more or less) of PRIMITIVE*)
skalberg@15570
   522
fun SINGLE tacf = Option.map fst o Seq.pull o tacf
haftmann@19455
   523
wenzelm@23538
   524
(*Conversions as tactics*)
wenzelm@23584
   525
fun CONVERSION cv i st = Seq.single (Conv.gconv_rule cv i st)
wenzelm@23561
   526
  handle THM _ => Seq.empty
wenzelm@23561
   527
    | CTERM _ => Seq.empty
wenzelm@23561
   528
    | TERM _ => Seq.empty
wenzelm@23561
   529
    | TYPE _ => Seq.empty;
wenzelm@23538
   530
clasohm@0
   531
end;
paulson@1502
   532
paulson@1502
   533
open Tactical;