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