src/Pure/tctical.ML
author paulson
Fri Feb 16 12:57:32 1996 +0100 (1996-02-16)
changeset 1502 b612093c8bff
parent 1460 5a6f2aabd538
child 1583 bc902840aab5
permissions -rw-r--r--
Elimination of fully-functorial style.
Type tactic changed to a type abbrevation (from a datatype).
Constructor tactic and function apply deleted.
clasohm@1460
     1
(*  Title: 	tctical
clasohm@0
     2
    ID:         $Id$
clasohm@1460
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Tacticals
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
infix 1 THEN THEN' THEN_BEST_FIRST;
clasohm@0
    10
infix 0 ORELSE APPEND INTLEAVE ORELSE' APPEND' INTLEAVE';
clasohm@0
    11
lcp@671
    12
infix 0 THEN_ELSE;
lcp@671
    13
clasohm@0
    14
clasohm@0
    15
signature TACTICAL =
clasohm@0
    16
  sig
paulson@1502
    17
  type tactic  (* = thm -> thm Sequence.seq*)
clasohm@1460
    18
  val all_tac		: tactic
clasohm@1460
    19
  val ALLGOALS		: (int -> tactic) -> tactic   
clasohm@1460
    20
  val APPEND		: tactic * tactic -> tactic
clasohm@1460
    21
  val APPEND'		: ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
clasohm@1460
    22
  val BEST_FIRST	: (thm -> bool) * (thm -> int) -> tactic -> tactic
clasohm@1460
    23
  val BREADTH_FIRST	: (thm -> bool) -> tactic -> tactic
clasohm@1460
    24
  val CHANGED		: tactic -> tactic
clasohm@1460
    25
  val COND		: (thm -> bool) -> tactic -> tactic -> tactic   
clasohm@1460
    26
  val DEPTH_FIRST	: (thm -> bool) -> tactic -> tactic
clasohm@1460
    27
  val DEPTH_SOLVE	: tactic -> tactic
clasohm@1460
    28
  val DEPTH_SOLVE_1	: tactic -> tactic
clasohm@1460
    29
  val DETERM		: tactic -> tactic
clasohm@1460
    30
  val EVERY		: tactic list -> tactic   
clasohm@1460
    31
  val EVERY'		: ('a -> tactic) list -> 'a -> tactic
clasohm@1460
    32
  val EVERY1		: (int -> tactic) list -> tactic
clasohm@1460
    33
  val FILTER		: (thm -> bool) -> tactic -> tactic
clasohm@1460
    34
  val FIRST		: tactic list -> tactic   
clasohm@1460
    35
  val FIRST'		: ('a -> tactic) list -> 'a -> tactic
clasohm@1460
    36
  val FIRST1		: (int -> tactic) list -> tactic
clasohm@1460
    37
  val FIRSTGOAL		: (int -> tactic) -> tactic
clasohm@1460
    38
  val goals_limit	: int ref
clasohm@1460
    39
  val has_fewer_prems	: int -> thm -> bool   
clasohm@1460
    40
  val IF_UNSOLVED	: tactic -> tactic
clasohm@1460
    41
  val INTLEAVE		: tactic * tactic -> tactic
clasohm@1460
    42
  val INTLEAVE'		: ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
clasohm@1460
    43
  val METAHYPS		: (thm list -> tactic) -> int -> tactic
clasohm@1460
    44
  val no_tac		: tactic
clasohm@1460
    45
  val ORELSE		: tactic * tactic -> tactic
clasohm@1460
    46
  val ORELSE'		: ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
clasohm@1460
    47
  val pause_tac		: tactic
clasohm@1460
    48
  val print_tac		: tactic
clasohm@1460
    49
  val REPEAT		: tactic -> tactic
clasohm@1460
    50
  val REPEAT1		: tactic -> tactic
clasohm@1460
    51
  val REPEAT_DETERM_N	: int -> tactic -> tactic
clasohm@1460
    52
  val REPEAT_DETERM	: tactic -> tactic
clasohm@1460
    53
  val REPEAT_DETERM1	: tactic -> tactic
lcp@703
    54
  val REPEAT_DETERM_FIRST: (int -> tactic) -> tactic
lcp@703
    55
  val REPEAT_DETERM_SOME: (int -> tactic) -> tactic
clasohm@1460
    56
  val REPEAT_FIRST	: (int -> tactic) -> tactic
clasohm@1460
    57
  val REPEAT_SOME	: (int -> tactic) -> tactic
clasohm@1460
    58
  val SELECT_GOAL	: tactic -> int -> tactic
clasohm@1460
    59
  val SOMEGOAL		: (int -> tactic) -> tactic   
clasohm@1460
    60
  val STATE		: (thm -> tactic) -> tactic
clasohm@1460
    61
  val strip_context	: term -> (string * typ) list * term list * term
clasohm@1460
    62
  val SUBGOAL		: ((term*int) -> tactic) -> int -> tactic
clasohm@1460
    63
  val suppress_tracing	: bool ref
clasohm@1460
    64
  val THEN		: tactic * tactic -> tactic
clasohm@1460
    65
  val THEN'		: ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
clasohm@1460
    66
  val THEN_BEST_FIRST	: tactic * ((thm->bool) * (thm->int) * tactic) 
clasohm@1460
    67
			  -> tactic
clasohm@1460
    68
  val THEN_ELSE		: tactic * (tactic*tactic) -> tactic
clasohm@1460
    69
  val traced_tac	: (thm -> (thm * thm Sequence.seq) option) -> tactic
clasohm@1460
    70
  val tracify		: bool ref -> tactic -> thm -> thm Sequence.seq
clasohm@1460
    71
  val trace_BEST_FIRST	: bool ref
clasohm@1460
    72
  val trace_DEPTH_FIRST	: bool ref
clasohm@1460
    73
  val trace_REPEAT	: bool ref
clasohm@1460
    74
  val TRY		: tactic -> tactic
clasohm@1460
    75
  val TRYALL		: (int -> tactic) -> tactic   
clasohm@0
    76
  end;
clasohm@0
    77
clasohm@0
    78
paulson@1502
    79
structure Tactical : TACTICAL = 
clasohm@0
    80
struct
clasohm@0
    81
clasohm@0
    82
(**** Tactics ****)
clasohm@0
    83
clasohm@0
    84
(*A tactic maps a proof tree to a sequence of proof trees:
clasohm@0
    85
    if length of sequence = 0 then the tactic does not apply;
clasohm@0
    86
    if length > 1 then backtracking on the alternatives can occur.*)
clasohm@0
    87
paulson@1502
    88
type tactic = thm -> thm Sequence.seq;
clasohm@0
    89
clasohm@0
    90
(*Makes a tactic from one that uses the components of the state.*)
paulson@1502
    91
fun STATE tacfun st = tacfun st st;
clasohm@0
    92
clasohm@0
    93
clasohm@0
    94
(*** LCF-style tacticals ***)
clasohm@0
    95
clasohm@0
    96
(*the tactical THEN performs one tactic followed by another*)
paulson@1502
    97
fun (tac1 THEN tac2) st = Sequence.flats (Sequence.maps tac2 (tac1 st));
clasohm@0
    98
clasohm@0
    99
clasohm@0
   100
(*The tactical ORELSE uses the first tactic that returns a nonempty sequence.
clasohm@0
   101
  Like in LCF, ORELSE commits to either tac1 or tac2 immediately.
clasohm@0
   102
  Does not backtrack to tac2 if tac1 was initially chosen. *)
paulson@1502
   103
fun (tac1 ORELSE tac2) st =
paulson@1502
   104
    case Sequence.pull(tac1 st) of
paulson@1502
   105
	None       => tac2 st
paulson@1502
   106
      | sequencecell => Sequence.seqof(fn()=> sequencecell);
clasohm@0
   107
clasohm@0
   108
clasohm@0
   109
(*The tactical APPEND combines the results of two tactics.
clasohm@0
   110
  Like ORELSE, but allows backtracking on both tac1 and tac2.
clasohm@0
   111
  The tactic tac2 is not applied until needed.*)
paulson@1502
   112
fun (tac1 APPEND tac2) st = 
paulson@1502
   113
  Sequence.append(tac1 st,
paulson@1502
   114
		  Sequence.seqof(fn()=> Sequence.pull (tac2 st)));
clasohm@0
   115
clasohm@0
   116
(*Like APPEND, but interleaves results of tac1 and tac2.*)
paulson@1502
   117
fun (tac1 INTLEAVE tac2) st = 
paulson@1502
   118
    Sequence.interleave(tac1 st,
paulson@1502
   119
			Sequence.seqof(fn()=> Sequence.pull (tac2 st)));
clasohm@0
   120
lcp@671
   121
(*Conditional tactic.
clasohm@1460
   122
	tac1 ORELSE tac2 = tac1 THEN_ELSE (all_tac, tac2)
clasohm@1460
   123
	tac1 THEN tac2   = tac1 THEN_ELSE (tac2, no_tac)
lcp@671
   124
*)
paulson@1502
   125
fun (tac THEN_ELSE (tac1, tac2)) st = 
paulson@1502
   126
    case Sequence.pull(tac st) of
paulson@1502
   127
	None    => tac2 st		(*failed; try tactic 2*)
clasohm@1460
   128
      | seqcell => Sequence.flats 	(*succeeded; use tactic 1*)
paulson@1502
   129
	            (Sequence.maps tac1 (Sequence.seqof(fn()=> seqcell)));
lcp@671
   130
lcp@671
   131
clasohm@0
   132
(*Versions for combining tactic-valued functions, as in
clasohm@0
   133
     SOMEGOAL (resolve_tac rls THEN' assume_tac) *)
paulson@1502
   134
fun (tac1 THEN' tac2) x = tac1 x THEN tac2 x;
paulson@1502
   135
fun (tac1 ORELSE' tac2) x = tac1 x ORELSE tac2 x;
paulson@1502
   136
fun (tac1 APPEND' tac2) x = tac1 x APPEND tac2 x;
paulson@1502
   137
fun (tac1 INTLEAVE' tac2) x = tac1 x INTLEAVE tac2 x;
clasohm@0
   138
clasohm@0
   139
(*passes all proofs through unchanged;  identity of THEN*)
paulson@1502
   140
fun all_tac st = Sequence.single st;
clasohm@0
   141
clasohm@0
   142
(*passes no proofs through;  identity of ORELSE and APPEND*)
paulson@1502
   143
fun no_tac st  = Sequence.null;
clasohm@0
   144
clasohm@0
   145
clasohm@0
   146
(*Make a tactic deterministic by chopping the tail of the proof sequence*)
paulson@1502
   147
fun DETERM tac st =  
paulson@1502
   148
      case Sequence.pull (tac st) of
clasohm@1460
   149
	      None => Sequence.null
paulson@1502
   150
            | Some(x,_) => Sequence.cons(x, Sequence.null);
clasohm@0
   151
clasohm@0
   152
clasohm@0
   153
(*Conditional tactical: testfun controls which tactic to use next.
clasohm@0
   154
  Beware: due to eager evaluation, both thentac and elsetac are evaluated.*)
paulson@1502
   155
fun COND testfun thenf elsef = (fn prf =>
clasohm@0
   156
    if testfun prf then  thenf prf   else  elsef prf);
clasohm@0
   157
clasohm@0
   158
(*Do the tactic or else do nothing*)
clasohm@0
   159
fun TRY tac = tac ORELSE all_tac;
clasohm@0
   160
clasohm@0
   161
clasohm@0
   162
(*** List-oriented tactics ***)
clasohm@0
   163
clasohm@0
   164
(* EVERY [tac1,...,tacn]   equals    tac1 THEN ... THEN tacn   *)
clasohm@0
   165
fun EVERY tacs = foldr (op THEN) (tacs, all_tac);
clasohm@0
   166
paulson@1502
   167
(* EVERY' [tac1,...,tacn] i  equals    tac1 i THEN ... THEN tacn i   *)
paulson@1502
   168
fun EVERY' tacs = foldr (op THEN') (tacs, K all_tac);
clasohm@0
   169
clasohm@0
   170
(*Apply every tactic to 1*)
paulson@1502
   171
fun EVERY1 tacs = EVERY' tacs 1;
clasohm@0
   172
clasohm@0
   173
(* FIRST [tac1,...,tacn]   equals    tac1 ORELSE ... ORELSE tacn   *)
clasohm@0
   174
fun FIRST tacs = foldr (op ORELSE) (tacs, no_tac);
clasohm@0
   175
paulson@1502
   176
(* FIRST' [tac1,...,tacn] i  equals    tac1 i ORELSE ... ORELSE tacn i   *)
paulson@1502
   177
fun FIRST' tacs = foldr (op ORELSE') (tacs, K no_tac);
clasohm@0
   178
clasohm@0
   179
(*Apply first tactic to 1*)
paulson@1502
   180
fun FIRST1 tacs = FIRST' tacs 1;
clasohm@0
   181
clasohm@0
   182
clasohm@0
   183
(*** Tracing tactics ***)
clasohm@0
   184
clasohm@0
   185
(*Max number of goals to print -- set by user*)
clasohm@0
   186
val goals_limit = ref 10;
clasohm@0
   187
clasohm@0
   188
(*Print the current proof state and pass it on.*)
paulson@1502
   189
val print_tac = 
paulson@1502
   190
    (fn st => 
paulson@1502
   191
     (!print_goals_ref (!goals_limit) st;   Sequence.single st));
clasohm@0
   192
clasohm@0
   193
(*Pause until a line is typed -- if non-empty then fail. *)
paulson@1502
   194
fun pause_tac st =  
clasohm@0
   195
  (prs"** Press RETURN to continue: ";
paulson@1502
   196
   if input(std_in,1) = "\n" then Sequence.single st
paulson@1502
   197
   else (prs"Goodbye\n";  Sequence.null));
clasohm@0
   198
clasohm@0
   199
exception TRACE_EXIT of thm
clasohm@0
   200
and TRACE_QUIT;
clasohm@0
   201
lcp@631
   202
(*Tracing flags*)
lcp@631
   203
val trace_REPEAT= ref false
lcp@631
   204
and trace_DEPTH_FIRST = ref false
lcp@631
   205
and trace_BEST_FIRST = ref false
lcp@631
   206
and suppress_tracing = ref false;
lcp@631
   207
clasohm@0
   208
(*Handle all tracing commands for current state and tactic *)
paulson@1502
   209
fun exec_trace_command flag (tac, st) = 
clasohm@0
   210
   case input_line(std_in) of
paulson@1502
   211
       "\n" => tac st
clasohm@0
   212
     | "f\n" => Sequence.null
paulson@1502
   213
     | "o\n" => (flag:=false;  tac st)
paulson@1502
   214
     | "s\n" => (suppress_tracing:=true;  tac st)
paulson@1502
   215
     | "x\n" => (prs"Exiting now\n";  raise (TRACE_EXIT st))
clasohm@0
   216
     | "quit\n" => raise TRACE_QUIT
clasohm@0
   217
     | _     => (prs
clasohm@0
   218
"Type RETURN to continue or...\n\
clasohm@0
   219
\     f    - to fail here\n\
clasohm@0
   220
\     o    - to switch tracing off\n\
lcp@631
   221
\     s    - to suppress tracing until next entry to a tactical\n\
clasohm@0
   222
\     x    - to exit at this point\n\
clasohm@0
   223
\     quit - to abort this tracing run\n\
paulson@1502
   224
\** Well? "     ;  exec_trace_command flag (tac, st));
clasohm@0
   225
clasohm@0
   226
clasohm@0
   227
(*Extract from a tactic, a thm->thm seq function that handles tracing*)
paulson@1502
   228
fun tracify flag tac st =
lcp@631
   229
  if !flag andalso not (!suppress_tracing)
paulson@1502
   230
           then (!print_goals_ref (!goals_limit) st;  
clasohm@1460
   231
		 prs"** Press RETURN to continue: ";
paulson@1502
   232
		 exec_trace_command flag (tac,st))
paulson@1502
   233
  else tac st;
clasohm@0
   234
clasohm@0
   235
(*Create a tactic whose outcome is given by seqf, handling TRACE_EXIT*)
paulson@1502
   236
fun traced_tac seqf st = 
lcp@631
   237
    (suppress_tracing := false;
lcp@631
   238
     Sequence.seqof (fn()=> seqf st
paulson@1502
   239
		         handle TRACE_EXIT st' => Some(st', Sequence.null)));
clasohm@0
   240
clasohm@0
   241
clasohm@0
   242
(*Deterministic REPEAT: only retains the first outcome; 
lcp@703
   243
  uses less space than REPEAT; tail recursive.
lcp@703
   244
  If non-negative, n bounds the number of repetitions.*)
lcp@703
   245
fun REPEAT_DETERM_N n tac = 
paulson@1502
   246
  let val tac = tracify trace_REPEAT tac
lcp@703
   247
      fun drep 0 st = Some(st, Sequence.null)
clasohm@1460
   248
	| drep n st =
paulson@1502
   249
	   (case Sequence.pull(tac st) of
clasohm@1460
   250
		None       => Some(st, Sequence.null)
clasohm@1460
   251
	      | Some(st',_) => drep (n-1) st')
lcp@703
   252
  in  traced_tac (drep n)  end;
lcp@703
   253
lcp@703
   254
(*Allows any number of repetitions*)
lcp@703
   255
val REPEAT_DETERM = REPEAT_DETERM_N ~1;
clasohm@0
   256
clasohm@0
   257
(*General REPEAT: maintains a stack of alternatives; tail recursive*)
clasohm@0
   258
fun REPEAT tac = 
paulson@1502
   259
  let val tac = tracify trace_REPEAT tac
clasohm@0
   260
      fun rep qs st = 
paulson@1502
   261
	case Sequence.pull(tac st) of
clasohm@1460
   262
  	    None       => Some(st, Sequence.seqof(fn()=> repq qs))
clasohm@0
   263
          | Some(st',q) => rep (q::qs) st'
clasohm@0
   264
      and repq [] = None
clasohm@0
   265
        | repq(q::qs) = case Sequence.pull q of
clasohm@1460
   266
  	    None       => repq qs
clasohm@0
   267
          | Some(st,q) => rep (q::qs) st
clasohm@0
   268
  in  traced_tac (rep [])  end;
clasohm@0
   269
clasohm@0
   270
(*Repeat 1 or more times*)
lcp@703
   271
fun REPEAT_DETERM1 tac = DETERM tac THEN REPEAT_DETERM tac;
clasohm@0
   272
fun REPEAT1 tac = tac THEN REPEAT tac;
clasohm@0
   273
clasohm@0
   274
clasohm@0
   275
(** Search tacticals **)
clasohm@0
   276
lcp@729
   277
(*Searches until "satp" reports proof tree as satisfied.
lcp@729
   278
  Suppresses duplicate solutions to minimize search space.*)
clasohm@0
   279
fun DEPTH_FIRST satp tac = 
paulson@1502
   280
 let val tac = tracify trace_DEPTH_FIRST tac
lcp@729
   281
     fun depth used [] = None
lcp@729
   282
       | depth used (q::qs) =
clasohm@1460
   283
	  case Sequence.pull q of
clasohm@1460
   284
	      None         => depth used qs
clasohm@1460
   285
	    | Some(st,stq) => 
clasohm@1460
   286
		if satp st andalso not (gen_mem eq_thm (st, used))
clasohm@1460
   287
		then Some(st, Sequence.seqof
clasohm@1460
   288
			         (fn()=> depth (st::used) (stq::qs)))
paulson@1502
   289
		else depth used (tac st :: stq :: qs)
lcp@729
   290
  in  traced_tac (fn st => depth [] ([Sequence.single st]))  end;
lcp@729
   291
clasohm@0
   292
clasohm@0
   293
clasohm@0
   294
(*Predicate: Does the rule have fewer than n premises?*)
clasohm@0
   295
fun has_fewer_prems n rule = (nprems_of rule < n);
clasohm@0
   296
clasohm@0
   297
(*Apply a tactic if subgoals remain, else do nothing.*)
clasohm@0
   298
val IF_UNSOLVED = COND (has_fewer_prems 1) all_tac;
clasohm@0
   299
clasohm@0
   300
(*Tactical to reduce the number of premises by 1.
clasohm@0
   301
  If no subgoals then it must fail! *)
clasohm@0
   302
fun DEPTH_SOLVE_1 tac = STATE
paulson@1502
   303
 (fn st => 
paulson@1502
   304
    (case nprems_of st of
clasohm@1460
   305
	0 => no_tac
clasohm@0
   306
      | n => DEPTH_FIRST (has_fewer_prems n) tac));
clasohm@0
   307
clasohm@0
   308
(*Uses depth-first search to solve ALL subgoals*)
clasohm@0
   309
val DEPTH_SOLVE = DEPTH_FIRST (has_fewer_prems 1);
clasohm@0
   310
clasohm@0
   311
(*** Best-first search ***)
clasohm@0
   312
clasohm@0
   313
(*Insertion into priority queue of states *)
clasohm@0
   314
fun insert (nth: int*thm, []) = [nth]
clasohm@0
   315
  | insert ((m,th), (n,th')::nths) = 
clasohm@0
   316
      if  n<m then (n,th') :: insert ((m,th), nths)
clasohm@0
   317
      else if  n=m andalso eq_thm(th,th')
clasohm@0
   318
              then (n,th')::nths
clasohm@0
   319
              else (m,th)::(n,th')::nths;
clasohm@0
   320
clasohm@0
   321
(*For creating output sequence*)
clasohm@0
   322
fun some_of_list []     = None
clasohm@0
   323
  | some_of_list (x::l) = Some (x, Sequence.seqof (fn () => some_of_list l));
clasohm@0
   324
clasohm@0
   325
clasohm@0
   326
(* Best-first search for a state that satisfies satp (incl initial state)
clasohm@0
   327
  Function sizef estimates size of problem remaining (smaller means better).
paulson@1502
   328
  tactic tac0 sets up the initial priority queue, which is searched by tac. *)
paulson@1502
   329
fun tac0 THEN_BEST_FIRST (satp, sizef, tac1) = 
paulson@1502
   330
  let val tac = tracify trace_BEST_FIRST tac1
clasohm@0
   331
      fun pairsize th = (sizef th, th);
clasohm@0
   332
      fun bfs (news,nprfs) =
clasohm@1460
   333
	   (case  partition satp news  of
clasohm@1460
   334
		([],nonsats) => next(foldr insert
clasohm@1460
   335
					(map pairsize nonsats, nprfs)) 
clasohm@1460
   336
	      | (sats,_)  => some_of_list sats)
clasohm@0
   337
      and next [] = None
clasohm@0
   338
        | next ((n,prf)::nprfs) =
clasohm@1460
   339
	    (if !trace_BEST_FIRST 
clasohm@1460
   340
	       then writeln("state size = " ^ string_of_int n ^ 
clasohm@1460
   341
		         "  queue length =" ^ string_of_int (length nprfs))  
clasohm@0
   342
               else ();
paulson@1502
   343
	     bfs (Sequence.list_of_s (tac prf), nprfs))
paulson@1502
   344
      fun btac st = bfs (Sequence.list_of_s (tac0 st),  [])
paulson@1502
   345
  in traced_tac btac end;
clasohm@0
   346
clasohm@0
   347
(*Ordinary best-first search, with no initial tactic*)
clasohm@0
   348
fun BEST_FIRST (satp,sizef) tac = all_tac THEN_BEST_FIRST (satp,sizef,tac);
clasohm@0
   349
clasohm@0
   350
(*Breadth-first search to satisfy satpred (including initial state) 
clasohm@0
   351
  SLOW -- SHOULD NOT USE APPEND!*)
paulson@1502
   352
fun BREADTH_FIRST satpred tac = 
paulson@1502
   353
  let val tacf = Sequence.list_of_s o tac;
clasohm@0
   354
      fun bfs prfs =
clasohm@1460
   355
	 (case  partition satpred prfs  of
clasohm@1460
   356
	      ([],[]) => []
clasohm@1460
   357
	    | ([],nonsats) => 
clasohm@1460
   358
		  (prs("breadth=" ^ string_of_int(length nonsats) ^ "\n");
clasohm@1460
   359
		   bfs (flat (map tacf nonsats)))
clasohm@1460
   360
	    | (sats,_)  => sats)
paulson@1502
   361
  in (fn st => Sequence.s_of_list (bfs [st])) end;
clasohm@0
   362
clasohm@0
   363
clasohm@0
   364
(** Filtering tacticals **)
clasohm@0
   365
clasohm@0
   366
(*Returns all states satisfying the predicate*)
paulson@1502
   367
fun FILTER pred tac st = Sequence.filters pred (tac st);
clasohm@0
   368
clasohm@0
   369
(*Returns all changed states*)
paulson@1502
   370
fun CHANGED tac  = 
paulson@1502
   371
  (fn st => 
paulson@1502
   372
    let fun diff st = not (eq_thm(st,st))
paulson@1502
   373
    in  Sequence.filters diff (tac st)
clasohm@0
   374
    end );
clasohm@0
   375
clasohm@0
   376
clasohm@0
   377
(*** Tacticals based on subgoal numbering ***)
clasohm@0
   378
paulson@1502
   379
(*For n subgoals, performs tac(n) THEN ... THEN tac(1) 
paulson@1502
   380
  Essential to work backwards since tac(i) may add/delete subgoals at i. *)
paulson@1502
   381
fun ALLGOALS tac st = 
paulson@1502
   382
  let fun doall 0 = all_tac
paulson@1502
   383
	| doall n = tac(n) THEN doall(n-1)
paulson@1502
   384
  in  doall(nprems_of st)st  end;
clasohm@0
   385
paulson@1502
   386
(*For n subgoals, performs tac(n) ORELSE ... ORELSE tac(1)  *)
paulson@1502
   387
fun SOMEGOAL tac st = 
paulson@1502
   388
  let fun find 0 = no_tac
paulson@1502
   389
	| find n = tac(n) ORELSE find(n-1)
paulson@1502
   390
  in  find(nprems_of st)st  end;
clasohm@0
   391
paulson@1502
   392
(*For n subgoals, performs tac(1) ORELSE ... ORELSE tac(n).
clasohm@0
   393
  More appropriate than SOMEGOAL in some cases.*)
paulson@1502
   394
fun FIRSTGOAL tac st = 
paulson@1502
   395
  let fun find (i,n) = if i>n then no_tac else  tac(i) ORELSE find (i+1,n)
paulson@1502
   396
  in  find(1, nprems_of st)st  end;
clasohm@0
   397
paulson@1502
   398
(*Repeatedly solve some using tac. *)
paulson@1502
   399
fun REPEAT_SOME tac = REPEAT1 (SOMEGOAL (REPEAT1 o tac));
paulson@1502
   400
fun REPEAT_DETERM_SOME tac = REPEAT_DETERM1 (SOMEGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   401
paulson@1502
   402
(*Repeatedly solve the first possible subgoal using tac. *)
paulson@1502
   403
fun REPEAT_FIRST tac = REPEAT1 (FIRSTGOAL (REPEAT1 o tac));
paulson@1502
   404
fun REPEAT_DETERM_FIRST tac = REPEAT_DETERM1 (FIRSTGOAL (REPEAT_DETERM1 o tac));
clasohm@0
   405
paulson@1502
   406
(*For n subgoals, tries to apply tac to n,...1  *)
paulson@1502
   407
fun TRYALL tac = ALLGOALS (TRY o tac);
clasohm@0
   408
clasohm@0
   409
clasohm@0
   410
(*Make a tactic for subgoal i, if there is one.  *)
paulson@1502
   411
fun SUBGOAL goalfun i st = 
paulson@1502
   412
  case drop(i-1, prems_of st) of
clasohm@0
   413
      [] => Sequence.null
paulson@1502
   414
    | prem::_ => goalfun (prem,i) st;
clasohm@0
   415
clasohm@0
   416
(*Tactical for restricting the effect of a tactic to subgoal i.
paulson@1502
   417
  Works by making a new state from subgoal i, applying tac to it, and
clasohm@0
   418
  composing the resulting metathm with the original state.
clasohm@0
   419
  The "main goal" of the new state will not be atomic, some tactics may fail!
clasohm@0
   420
  DOES NOT work if tactic affects the main goal other than by instantiation.*)
clasohm@0
   421
lcp@31
   422
(* (!!x. PROP ?V) ==> PROP ?V ;  contains NO TYPE VARIABLES.*)
clasohm@0
   423
val dummy_quant_rl = 
clasohm@0
   424
  standard (forall_elim_var 0 (assume 
clasohm@922
   425
                  (read_cterm Sign.proto_pure ("!!x::prop. PROP V",propT))));
clasohm@0
   426
clasohm@0
   427
(* Prevent the subgoal's assumptions from becoming additional subgoals in the
clasohm@0
   428
   new proof state by enclosing them by a universal quantification *)
paulson@1502
   429
fun protect_subgoal st i =
paulson@1502
   430
	Sequence.hd (bicompose false (false,dummy_quant_rl,1) i st)
clasohm@1460
   431
	handle _ => error"SELECT_GOAL -- impossible error???";
clasohm@0
   432
clasohm@0
   433
(*Does the work of SELECT_GOAL. *)
paulson@1502
   434
fun select tac st0 i =
paulson@1502
   435
  let val cprem::_ = drop(i-1, cprems_of st0)
paulson@1502
   436
      fun next st = bicompose false (false, st, nprems_of st) i st0
paulson@1502
   437
  in  Sequence.flats (Sequence.maps next (tac (trivial cprem)))
clasohm@0
   438
  end;
clasohm@0
   439
paulson@1502
   440
fun SELECT_GOAL tac i st = 
paulson@1502
   441
  case (i, drop(i-1, prems_of st)) of
clasohm@0
   442
      (_,[]) => Sequence.null
paulson@1502
   443
    | (1,[_]) => tac st		(*If i=1 and only one subgoal do nothing!*)
paulson@1502
   444
    | (_, (Const("==>",_)$_$_) :: _) => select tac (protect_subgoal st i) i
paulson@1502
   445
    | (_, _::_) => select tac st i;
clasohm@0
   446
clasohm@0
   447
clasohm@0
   448
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
clasohm@0
   449
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters. 
clasohm@0
   450
  Main difference from strip_assums concerns parameters: 
clasohm@0
   451
    it replaces the bound variables by free variables.  *)
clasohm@0
   452
fun strip_context_aux (params, Hs, Const("==>", _) $ H $ B) = 
clasohm@1460
   453
	strip_context_aux (params, H::Hs, B)
clasohm@0
   454
  | strip_context_aux (params, Hs, Const("all",_)$Abs(a,T,t)) =
clasohm@0
   455
        let val (b,u) = variant_abs(a,T,t)
clasohm@1460
   456
	in  strip_context_aux ((b,T)::params, Hs, u)  end
clasohm@0
   457
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
clasohm@0
   458
clasohm@0
   459
fun strip_context A = strip_context_aux ([],[],A);
clasohm@0
   460
clasohm@0
   461
clasohm@0
   462
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
paulson@1502
   463
       METAHYPS (fn prems => tac prems) i
clasohm@0
   464
clasohm@0
   465
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
clasohm@0
   466
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
clasohm@0
   467
"prems").  The parameters x1,...,xm become free variables.  If the
clasohm@0
   468
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
clasohm@0
   469
then it is lifted back into the original context, yielding k subgoals.
clasohm@0
   470
clasohm@0
   471
Replaces unknowns in the context by Frees having the prefix METAHYP_
clasohm@0
   472
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
clasohm@0
   473
DOES NOT HANDLE TYPE UNKNOWNS.
clasohm@0
   474
****)
clasohm@0
   475
clasohm@0
   476
local 
clasohm@0
   477
clasohm@0
   478
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
clasohm@0
   479
    Instantiates distinct free variables by terms of same type.*)
clasohm@0
   480
  fun free_instantiate ctpairs = 
clasohm@0
   481
      forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
clasohm@0
   482
clasohm@0
   483
  fun free_of s ((a,i), T) =
clasohm@0
   484
        Free(s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i),
clasohm@1460
   485
	     T)
clasohm@0
   486
clasohm@0
   487
  fun mk_inst (var as Var(v,T))  = (var,  free_of "METAHYP1_" (v,T))
clasohm@0
   488
in
clasohm@0
   489
paulson@1502
   490
fun metahyps_aux_tac tacf (prem,i) state = 
clasohm@0
   491
  let val {sign,maxidx,...} = rep_thm state
lcp@230
   492
      val cterm = cterm_of sign
clasohm@0
   493
      (*find all vars in the hyps -- should find tvars also!*)
paulson@1502
   494
      val hyps_vars = foldr add_term_vars (Logic.strip_assums_hyp prem, [])
clasohm@0
   495
      val insts = map mk_inst hyps_vars
clasohm@0
   496
      (*replace the hyps_vars by Frees*)
clasohm@0
   497
      val prem' = subst_atomic insts prem
clasohm@0
   498
      val (params,hyps,concl) = strip_context prem'
clasohm@0
   499
      val fparams = map Free params
clasohm@0
   500
      val cparams = map cterm fparams
clasohm@0
   501
      and chyps = map cterm hyps
clasohm@0
   502
      val hypths = map assume chyps
clasohm@0
   503
      fun swap_ctpair (t,u) = (cterm u, cterm t)
clasohm@0
   504
      (*Subgoal variables: make Free; lift type over params*)
clasohm@0
   505
      fun mk_subgoal_inst concl_vars (var as Var(v,T)) = 
clasohm@0
   506
          if var mem concl_vars 
clasohm@1460
   507
	  then (var, true, free_of "METAHYP2_" (v,T))
clasohm@1460
   508
	  else (var, false,
clasohm@1460
   509
		free_of "METAHYP2_" (v, map #2 params --->T))
clasohm@0
   510
      (*Instantiate subgoal vars by Free applied to params*)
clasohm@0
   511
      fun mk_ctpair (t,in_concl,u) = 
clasohm@1460
   512
	  if in_concl then (cterm t,  cterm u)
clasohm@0
   513
          else (cterm t,  cterm (list_comb (u,fparams)))
clasohm@0
   514
      (*Restore Vars with higher type and index*)
clasohm@0
   515
      fun mk_subgoal_swap_ctpair 
clasohm@1460
   516
		(t as Var((a,i),_), in_concl, u as Free(_,U)) = 
clasohm@1460
   517
	  if in_concl then (cterm u, cterm t)
clasohm@0
   518
          else (cterm u, cterm(Var((a, i+maxidx), U)))
clasohm@0
   519
      (*Embed B in the original context of params and hyps*)
paulson@1502
   520
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
clasohm@0
   521
      (*Strip the context using elimination rules*)
clasohm@0
   522
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
clasohm@0
   523
      (*Embed an ff pair in the original params*)
paulson@1502
   524
      fun embed_ff(t,u) = Logic.mk_flexpair (list_abs_free (params, t), 
paulson@1502
   525
					     list_abs_free (params, u))
clasohm@0
   526
      (*Remove parameter abstractions from the ff pairs*)
clasohm@0
   527
      fun elim_ff ff = flexpair_abs_elim_list cparams ff
clasohm@0
   528
      (*A form of lifting that discharges assumptions.*)
clasohm@0
   529
      fun relift st = 
clasohm@1460
   530
	let val prop = #prop(rep_thm st)
clasohm@1460
   531
	    val subgoal_vars = (*Vars introduced in the subgoals*)
paulson@1502
   532
		  foldr add_term_vars (Logic.strip_imp_prems prop, [])
paulson@1502
   533
	    and concl_vars = add_term_vars (Logic.strip_imp_concl prop, [])
clasohm@1460
   534
	    val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
clasohm@1460
   535
	    val st' = instantiate ([], map mk_ctpair subgoal_insts) st
clasohm@1460
   536
	    val emBs = map (cterm o embed) (prems_of st')
clasohm@0
   537
            and ffs = map (cterm o embed_ff) (tpairs_of st')
clasohm@1460
   538
	    val Cth  = implies_elim_list st' 
clasohm@1460
   539
			    (map (elim_ff o assume) ffs @
clasohm@1460
   540
			     map (elim o assume) emBs)
clasohm@1460
   541
	in  (*restore the unknowns to the hypotheses*)
clasohm@1460
   542
	    free_instantiate (map swap_ctpair insts @
clasohm@1460
   543
			      map mk_subgoal_swap_ctpair subgoal_insts)
clasohm@1460
   544
		(*discharge assumptions from state in same order*)
clasohm@1460
   545
		(implies_intr_list (ffs@emBs)
clasohm@1460
   546
		  (forall_intr_list cparams (implies_intr_list chyps Cth)))
clasohm@1460
   547
	end
clasohm@0
   548
      val subprems = map (forall_elim_vars 0) hypths
clasohm@0
   549
      and st0 = trivial (cterm concl)
clasohm@0
   550
      (*function to replace the current subgoal*)
clasohm@0
   551
      fun next st = bicompose false (false, relift st, nprems_of st)
clasohm@1460
   552
	            i state
paulson@1502
   553
  in  Sequence.flats (Sequence.maps next (tacf subprems st0))
paulson@1502
   554
  end;
clasohm@0
   555
end;
clasohm@0
   556
clasohm@0
   557
fun METAHYPS tacf = SUBGOAL (metahyps_aux_tac tacf);
clasohm@0
   558
clasohm@0
   559
end;
paulson@1502
   560
paulson@1502
   561
open Tactical;