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