src/Pure/tactical.ML
author bulwahn
Tue Aug 31 08:00:53 2010 +0200 (2010-08-31)
changeset 38950 62578950e748
parent 34885 6587c24ef6d8
child 39125 f45d332a90e3
permissions -rw-r--r--
storing options for prolog code generation in the theory
     1 (*  Title:      Pure/tactical.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 Unsynchronized.ref
    38   val suppress_tracing: bool Unsynchronized.ref
    39   val tracify: bool Unsynchronized.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 SOLVED': (int -> tactic) -> int -> tactic
    62   val THEN_ALL_NEW: (int -> tactic) * (int -> tactic) -> int -> tactic
    63   val REPEAT_ALL_NEW: (int -> tactic) -> int -> tactic
    64   val PRIMSEQ: (thm -> thm Seq.seq) -> tactic
    65   val PRIMITIVE: (thm -> thm) -> tactic
    66   val SINGLE: tactic -> thm -> thm option
    67   val CONVERSION: conv -> int -> tactic
    68 end;
    69 
    70 structure Tactical : TACTICAL =
    71 struct
    72 
    73 (**** Tactics ****)
    74 
    75 (*A tactic maps a proof tree to a sequence of proof trees:
    76     if length of sequence = 0 then the tactic does not apply;
    77     if length > 1 then backtracking on the alternatives can occur.*)
    78 
    79 type tactic = thm -> thm Seq.seq;
    80 
    81 
    82 (*** LCF-style tacticals ***)
    83 
    84 (*the tactical THEN performs one tactic followed by another*)
    85 fun (tac1 THEN tac2) st = Seq.maps tac2 (tac1 st);
    86 
    87 
    88 (*The tactical ORELSE uses the first tactic that returns a nonempty sequence.
    89   Like in LCF, ORELSE commits to either tac1 or tac2 immediately.
    90   Does not backtrack to tac2 if tac1 was initially chosen. *)
    91 fun (tac1 ORELSE tac2) st =
    92     case Seq.pull(tac1 st) of
    93         NONE       => tac2 st
    94       | sequencecell => Seq.make(fn()=> sequencecell);
    95 
    96 
    97 (*The tactical APPEND combines the results of two tactics.
    98   Like ORELSE, but allows backtracking on both tac1 and tac2.
    99   The tactic tac2 is not applied until needed.*)
   100 fun (tac1 APPEND tac2) st =
   101   Seq.append (tac1 st) (Seq.make(fn()=> Seq.pull (tac2 st)));
   102 
   103 (*Like APPEND, but interleaves results of tac1 and tac2.*)
   104 fun (tac1 INTLEAVE tac2) st =
   105     Seq.interleave(tac1 st,
   106                         Seq.make(fn()=> Seq.pull (tac2 st)));
   107 
   108 (*Conditional tactic.
   109         tac1 ORELSE tac2 = tac1 THEN_ELSE (all_tac, tac2)
   110         tac1 THEN tac2   = tac1 THEN_ELSE (tac2, no_tac)
   111 *)
   112 fun (tac THEN_ELSE (tac1, tac2)) st =
   113     case Seq.pull(tac st) of
   114         NONE    => tac2 st                                   (*failed; try tactic 2*)
   115       | seqcell => Seq.maps tac1 (Seq.make(fn()=> seqcell)); (*succeeded; use tactic 1*)
   116 
   117 
   118 (*Versions for combining tactic-valued functions, as in
   119      SOMEGOAL (resolve_tac rls THEN' assume_tac) *)
   120 fun (tac1 THEN' tac2) x = tac1 x THEN tac2 x;
   121 fun (tac1 ORELSE' tac2) x = tac1 x ORELSE tac2 x;
   122 fun (tac1 APPEND' tac2) x = tac1 x APPEND tac2 x;
   123 fun (tac1 INTLEAVE' tac2) x = tac1 x INTLEAVE tac2 x;
   124 
   125 (*passes all proofs through unchanged;  identity of THEN*)
   126 fun all_tac st = Seq.single st;
   127 
   128 (*passes no proofs through;  identity of ORELSE and APPEND*)
   129 fun no_tac st  = Seq.empty;
   130 
   131 
   132 (*Make a tactic deterministic by chopping the tail of the proof sequence*)
   133 fun DETERM tac = Seq.DETERM tac;
   134 
   135 (*Conditional tactical: testfun controls which tactic to use next.
   136   Beware: due to eager evaluation, both thentac and elsetac are evaluated.*)
   137 fun COND testfun thenf elsef = (fn prf =>
   138     if testfun prf then  thenf prf   else  elsef prf);
   139 
   140 (*Do the tactic or else do nothing*)
   141 fun TRY tac = tac ORELSE all_tac;
   142 
   143 (*** List-oriented tactics ***)
   144 
   145 local
   146   (*This version of EVERY avoids backtracking over repeated states*)
   147 
   148   fun EVY (trail, []) st =
   149         Seq.make (fn()=> SOME(st,
   150                         Seq.make (fn()=> Seq.pull (evyBack trail))))
   151     | EVY (trail, tac::tacs) st =
   152           case Seq.pull(tac st) of
   153               NONE    => evyBack trail              (*failed: backtrack*)
   154             | SOME(st',q) => EVY ((st',q,tacs)::trail, tacs) st'
   155   and evyBack [] = Seq.empty (*no alternatives*)
   156     | evyBack ((st',q,tacs)::trail) =
   157           case Seq.pull q of
   158               NONE        => evyBack trail
   159             | SOME(st,q') => if Thm.eq_thm (st',st)
   160                              then evyBack ((st',q',tacs)::trail)
   161                              else EVY ((st,q',tacs)::trail, tacs) st
   162 in
   163 
   164 (* EVERY [tac1,...,tacn]   equals    tac1 THEN ... THEN tacn   *)
   165 fun EVERY tacs = EVY ([], tacs);
   166 end;
   167 
   168 
   169 (* EVERY' [tac1,...,tacn] i  equals    tac1 i THEN ... THEN tacn i   *)
   170 fun EVERY' tacs i = EVERY (map (fn f => f i) tacs);
   171 
   172 (*Apply every tactic to 1*)
   173 fun EVERY1 tacs = EVERY' tacs 1;
   174 
   175 (* FIRST [tac1,...,tacn]   equals    tac1 ORELSE ... ORELSE tacn   *)
   176 fun FIRST tacs = fold_rev (curry op ORELSE) tacs no_tac;
   177 
   178 (* FIRST' [tac1,...,tacn] i  equals    tac1 i ORELSE ... ORELSE tacn i   *)
   179 fun FIRST' tacs = fold_rev (curry op ORELSE') tacs (K no_tac);
   180 
   181 (*Apply first tactic to 1*)
   182 fun FIRST1 tacs = FIRST' tacs 1;
   183 
   184 (*Apply tactics on consecutive subgoals*)
   185 fun RANGE [] _ = all_tac
   186   | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
   187 
   188 
   189 (*** Tracing tactics ***)
   190 
   191 (*Print the current proof state and pass it on.*)
   192 fun print_tac msg st =
   193  (tracing (msg ^ "\n" ^
   194     Pretty.string_of (Pretty.chunks
   195       (Goal_Display.pretty_goals_without_context (! Goal_Display.goals_limit) st)));
   196   Seq.single st);
   197 
   198 (*Pause until a line is typed -- if non-empty then fail. *)
   199 fun pause_tac st =
   200   (tracing "** Press RETURN to continue:";
   201    if TextIO.inputLine TextIO.stdIn = SOME "\n" then Seq.single st
   202    else (tracing "Goodbye";  Seq.empty));
   203 
   204 exception TRACE_EXIT of thm
   205 and TRACE_QUIT;
   206 
   207 (*Tracing flags*)
   208 val trace_REPEAT= Unsynchronized.ref false
   209 and suppress_tracing = Unsynchronized.ref false;
   210 
   211 (*Handle all tracing commands for current state and tactic *)
   212 fun exec_trace_command flag (tac, st) =
   213    case TextIO.inputLine TextIO.stdIn of
   214        SOME "\n" => tac st
   215      | SOME "f\n" => Seq.empty
   216      | SOME "o\n" => (flag := false;  tac st)
   217      | SOME "s\n" => (suppress_tracing := true;  tac st)
   218      | SOME "x\n" => (tracing "Exiting now";  raise (TRACE_EXIT st))
   219      | SOME "quit\n" => raise TRACE_QUIT
   220      | _     => (tracing
   221 "Type RETURN to continue or...\n\
   222 \     f    - to fail here\n\
   223 \     o    - to switch tracing off\n\
   224 \     s    - to suppress tracing until next entry to a tactical\n\
   225 \     x    - to exit at this point\n\
   226 \     quit - to abort this tracing run\n\
   227 \** Well? "     ;  exec_trace_command flag (tac, st));
   228 
   229 
   230 (*Extract from a tactic, a thm->thm seq function that handles tracing*)
   231 fun tracify flag tac st =
   232   if !flag andalso not (!suppress_tracing) then
   233     (tracing (Pretty.string_of (Pretty.chunks
   234         (Goal_Display.pretty_goals_without_context (! Goal_Display.goals_limit) st @
   235           [Pretty.str "** Press RETURN to continue:"])));
   236      exec_trace_command flag (tac, st))
   237   else tac st;
   238 
   239 (*Create a tactic whose outcome is given by seqf, handling TRACE_EXIT*)
   240 fun traced_tac seqf st =
   241     (suppress_tracing := false;
   242      Seq.make (fn()=> seqf st
   243                          handle TRACE_EXIT st' => SOME(st', Seq.empty)));
   244 
   245 
   246 (*Deterministic DO..UNTIL: only retains the first outcome; tail recursive.
   247   Forces repitition until predicate on state is fulfilled.*)
   248 fun DETERM_UNTIL p tac =
   249 let val tac = tracify trace_REPEAT tac
   250     fun drep st = if p st then SOME (st, Seq.empty)
   251                           else (case Seq.pull(tac st) of
   252                                   NONE        => NONE
   253                                 | SOME(st',_) => drep st')
   254 in  traced_tac drep  end;
   255 
   256 (*Deterministic REPEAT: only retains the first outcome;
   257   uses less space than REPEAT; tail recursive.
   258   If non-negative, n bounds the number of repetitions.*)
   259 fun REPEAT_DETERM_N n tac =
   260   let val tac = tracify trace_REPEAT tac
   261       fun drep 0 st = SOME(st, Seq.empty)
   262         | drep n st =
   263            (case Seq.pull(tac st) of
   264                 NONE       => SOME(st, Seq.empty)
   265               | SOME(st',_) => drep (n-1) st')
   266   in  traced_tac (drep n)  end;
   267 
   268 (*Allows any number of repetitions*)
   269 val REPEAT_DETERM = REPEAT_DETERM_N ~1;
   270 
   271 (*General REPEAT: maintains a stack of alternatives; tail recursive*)
   272 fun REPEAT tac =
   273   let val tac = tracify trace_REPEAT tac
   274       fun rep qs st =
   275         case Seq.pull(tac st) of
   276             NONE       => SOME(st, Seq.make(fn()=> repq qs))
   277           | SOME(st',q) => rep (q::qs) st'
   278       and repq [] = NONE
   279         | repq(q::qs) = case Seq.pull q of
   280             NONE       => repq qs
   281           | SOME(st,q) => rep (q::qs) st
   282   in  traced_tac (rep [])  end;
   283 
   284 (*Repeat 1 or more times*)
   285 fun REPEAT_DETERM1 tac = DETERM tac THEN REPEAT_DETERM tac;
   286 fun REPEAT1 tac = tac THEN REPEAT tac;
   287 
   288 
   289 (** Filtering tacticals **)
   290 
   291 fun FILTER pred tac st = Seq.filter pred (tac st);
   292 
   293 (*Accept only next states that change the theorem somehow*)
   294 fun CHANGED tac st =
   295   let fun diff st' = not (Thm.eq_thm (st, st'));
   296   in Seq.filter diff (tac st) end;
   297 
   298 (*Accept only next states that change the theorem's prop field
   299   (changes to signature, hyps, etc. don't count)*)
   300 fun CHANGED_PROP tac st =
   301   let fun diff st' = not (Thm.eq_thm_prop (st, st'));
   302   in Seq.filter diff (tac st) end;
   303 
   304 
   305 (*** Tacticals based on subgoal numbering ***)
   306 
   307 (*For n subgoals, performs tac(n) THEN ... THEN tac(1)
   308   Essential to work backwards since tac(i) may add/delete subgoals at i. *)
   309 fun ALLGOALS tac st =
   310   let fun doall 0 = all_tac
   311         | doall n = tac(n) THEN doall(n-1)
   312   in  doall(nprems_of st)st  end;
   313 
   314 (*For n subgoals, performs tac(n) ORELSE ... ORELSE tac(1)  *)
   315 fun SOMEGOAL tac st =
   316   let fun find 0 = no_tac
   317         | find n = tac(n) ORELSE find(n-1)
   318   in  find(nprems_of st)st  end;
   319 
   320 (*For n subgoals, performs tac(1) ORELSE ... ORELSE tac(n).
   321   More appropriate than SOMEGOAL in some cases.*)
   322 fun FIRSTGOAL tac st =
   323   let fun find (i,n) = if i>n then no_tac else  tac(i) ORELSE find (i+1,n)
   324   in  find(1, nprems_of st)st  end;
   325 
   326 (*Repeatedly solve some using tac. *)
   327 fun REPEAT_SOME tac = REPEAT1 (SOMEGOAL (REPEAT1 o tac));
   328 fun REPEAT_DETERM_SOME tac = REPEAT_DETERM1 (SOMEGOAL (REPEAT_DETERM1 o tac));
   329 
   330 (*Repeatedly solve the first possible subgoal using tac. *)
   331 fun REPEAT_FIRST tac = REPEAT1 (FIRSTGOAL (REPEAT1 o tac));
   332 fun REPEAT_DETERM_FIRST tac = REPEAT_DETERM1 (FIRSTGOAL (REPEAT_DETERM1 o tac));
   333 
   334 (*For n subgoals, tries to apply tac to n,...1  *)
   335 fun TRYALL tac = ALLGOALS (TRY o tac);
   336 
   337 
   338 (*Make a tactic for subgoal i, if there is one.  *)
   339 fun CSUBGOAL goalfun i st =
   340   (case SOME (Thm.cprem_of st i) handle THM _ => NONE of
   341     SOME goal => goalfun (goal, i) st
   342   | NONE => Seq.empty);
   343 
   344 fun SUBGOAL goalfun =
   345   CSUBGOAL (fn (goal, i) => goalfun (Thm.term_of goal, i));
   346 
   347 (*Returns all states that have changed in subgoal i, counted from the LAST
   348   subgoal.  For stac, for example.*)
   349 fun CHANGED_GOAL tac i st =
   350     let val np = Thm.nprems_of st
   351         val d = np-i                 (*distance from END*)
   352         val t = Thm.term_of (Thm.cprem_of st i)
   353         fun diff st' =
   354             Thm.nprems_of st' - d <= 0   (*the subgoal no longer exists*)
   355             orelse
   356              not (Pattern.aeconv (t, Thm.term_of (Thm.cprem_of st' (Thm.nprems_of st' - d))))
   357     in  Seq.filter diff (tac i st)  end
   358     handle Subscript => Seq.empty  (*no subgoal i*);
   359 
   360 (*Returns all states where some subgoals have been solved.  For
   361   subgoal-based tactics this means subgoal i has been solved
   362   altogether -- no new subgoals have emerged.*)
   363 fun SOLVED' tac i st =
   364   tac i st |> Seq.filter (fn st' => nprems_of st' < nprems_of st);
   365 
   366 (*Apply second tactic to all subgoals emerging from the first --
   367   following usual convention for subgoal-based tactics.*)
   368 fun (tac1 THEN_ALL_NEW tac2) i st =
   369   st |> (tac1 i THEN (fn st' => Seq.INTERVAL tac2 i (i + nprems_of st' - nprems_of st) st'));
   370 
   371 (*Repeatedly dig into any emerging subgoals.*)
   372 fun REPEAT_ALL_NEW tac =
   373   tac THEN_ALL_NEW (TRY o (fn i => REPEAT_ALL_NEW tac i));
   374 
   375 (*Makes a tactic whose effect on a state is given by thmfun: thm->thm seq.*)
   376 fun PRIMSEQ thmfun st =  thmfun st handle THM _ => Seq.empty;
   377 
   378 (*Makes a tactic whose effect on a state is given by thmfun: thm->thm.*)
   379 fun PRIMITIVE thmfun = PRIMSEQ (Seq.single o thmfun);
   380 
   381 (*Inverse (more or less) of PRIMITIVE*)
   382 fun SINGLE tacf = Option.map fst o Seq.pull o tacf
   383 
   384 (*Conversions as tactics*)
   385 fun CONVERSION cv i st = Seq.single (Conv.gconv_rule cv i st)
   386   handle THM _ => Seq.empty
   387     | CTERM _ => Seq.empty
   388     | TERM _ => Seq.empty
   389     | TYPE _ => Seq.empty;
   390 
   391 end;
   392 
   393 open Tactical;