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