src/Pure/tctical.ML
author wenzelm
Thu, 03 Feb 1994 13:55:42 +0100
changeset 254 b1fcd27fcac4
parent 230 ec8a2b6aa8a7
child 631 8bc44f7bbab8
permissions -rw-r--r--
replaced pprint_sg by Sign.pprint_sg; added Syntax.simple_pprint_typ;
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.*)
67
8380bc0adde7 Pure/drule/print_goals_ref: new, for Centaur interface
lcp
parents: 31
diff changeset
   182
val print_tac = Tactic 
8380bc0adde7 Pure/drule/print_goals_ref: new, for Centaur interface
lcp
parents: 31
diff changeset
   183
    (fn state => 
8380bc0adde7 Pure/drule/print_goals_ref: new, for Centaur interface
lcp
parents: 31
diff changeset
   184
     (!print_goals_ref (!goals_limit) state;   Sequence.single state));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
(*Pause until a line is typed -- if non-empty then fail. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
val pause_tac = Tactic (fn state => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
  (prs"** Press RETURN to continue: ";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
   if input(std_in,1) = "\n" then Sequence.single state
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
   else (prs"Goodbye\n";  Sequence.null)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
exception TRACE_EXIT of thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
and TRACE_QUIT;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
(*Handle all tracing commands for current state and tactic *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
fun exec_trace_command flag (tf, state) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
   case input_line(std_in) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
       "\n" => tf state
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
     | "f\n" => Sequence.null
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
     | "o\n" => (flag:=false; tf state)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
     | "x\n" => (prs"Exiting now\n";  raise (TRACE_EXIT state))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
     | "quit\n" => raise TRACE_QUIT
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
     | _     => (prs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
"Type RETURN to continue or...\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
\     f    - to fail here\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
\     o    - to switch tracing off\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
\     x    - to exit at this point\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
\     quit - to abort this tracing run\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
\** Well? "     ;  exec_trace_command flag (tf, state));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
(*Extract from a tactic, a thm->thm seq function that handles tracing*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
fun tracify flag (Tactic tf) state =
67
8380bc0adde7 Pure/drule/print_goals_ref: new, for Centaur interface
lcp
parents: 31
diff changeset
   214
  if !flag then (!print_goals_ref (!goals_limit) state;  
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
		 prs"** Press RETURN to continue: ";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
		 exec_trace_command flag (tf,state))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
  else tf state;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
(*Create a tactic whose outcome is given by seqf, handling TRACE_EXIT*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
fun traced_tac seqf = Tactic (fn st =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
    Sequence.seqof (fn()=> seqf st
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
		           handle TRACE_EXIT st' => Some(st', Sequence.null)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
(*Tracing flags*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
val trace_REPEAT= ref false
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
and trace_DEPTH_FIRST = ref false
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
and trace_BEST_FIRST = ref false;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
(*Deterministic REPEAT: only retains the first outcome; 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
  uses less space than REPEAT; tail recursive*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
fun REPEAT_DETERM tac = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
  let val tf = tracify trace_REPEAT tac
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
      fun drep st =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
        case Sequence.pull(tf st) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
  	    None       => Some(st, Sequence.null)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
          | Some(st',_) => drep st'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
  in  traced_tac drep  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
(*General REPEAT: maintains a stack of alternatives; tail recursive*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
fun REPEAT tac = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
  let val tf = tracify trace_REPEAT tac
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
      fun rep qs st = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
	case Sequence.pull(tf st) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
  	    None       => Some(st, Sequence.seqof(fn()=> repq qs))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
          | Some(st',q) => rep (q::qs) st'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
      and repq [] = None
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
        | repq(q::qs) = case Sequence.pull q of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
  	    None       => repq qs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
          | Some(st,q) => rep (q::qs) st
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
  in  traced_tac (rep [])  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
(*Repeat 1 or more times*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
fun REPEAT1 tac = tac THEN REPEAT tac;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
(** Search tacticals **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
(*Seaarches "satp" reports proof tree as satisfied*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
fun DEPTH_FIRST satp tac = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
 let val tf = tracify trace_DEPTH_FIRST tac
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   262
     fun depth [] = None
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
       | depth(q::qs) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
	  case Sequence.pull q of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
	      None         => depth qs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
	    | Some(st,stq) => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   267
		if satp st then Some(st, Sequence.seqof(fn()=> depth(stq::qs)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
		else depth (tf st :: stq :: qs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
  in  traced_tac (fn st => depth([Sequence.single st]))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   271
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
(*Predicate: Does the rule have fewer than n premises?*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
fun has_fewer_prems n rule = (nprems_of rule < n);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
(*Apply a tactic if subgoals remain, else do nothing.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
val IF_UNSOLVED = COND (has_fewer_prems 1) all_tac;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
(*Tactical to reduce the number of premises by 1.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
  If no subgoals then it must fail! *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
fun DEPTH_SOLVE_1 tac = STATE
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
 (fn state => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
    (case nprems_of state of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   283
	0 => no_tac
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   284
      | n => DEPTH_FIRST (has_fewer_prems n) tac));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
(*Uses depth-first search to solve ALL subgoals*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
val DEPTH_SOLVE = DEPTH_FIRST (has_fewer_prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   289
(*** Best-first search ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   291
(*Insertion into priority queue of states *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
fun insert (nth: int*thm, []) = [nth]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
  | insert ((m,th), (n,th')::nths) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
      if  n<m then (n,th') :: insert ((m,th), nths)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   295
      else if  n=m andalso eq_thm(th,th')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
              then (n,th')::nths
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   297
              else (m,th)::(n,th')::nths;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   299
(*For creating output sequence*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
fun some_of_list []     = None
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   301
  | some_of_list (x::l) = Some (x, Sequence.seqof (fn () => some_of_list l));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   302
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   303
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   304
(* Best-first search for a state that satisfies satp (incl initial state)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   305
  Function sizef estimates size of problem remaining (smaller means better).
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   306
  tactic tf0 sets up the initial priority queue, which is searched by tac. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   307
fun (Tactic tf0) THEN_BEST_FIRST (satp, sizef, tac) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   308
  let val tf = tracify trace_BEST_FIRST tac
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   309
      fun pairsize th = (sizef th, th);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   310
      fun bfs (news,nprfs) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   311
	   (case  partition satp news  of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   312
		([],nonsats) => next(foldr insert
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   313
					(map pairsize nonsats, nprfs)) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   314
	      | (sats,_)  => some_of_list sats)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   315
      and next [] = None
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   316
        | next ((n,prf)::nprfs) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   317
	    (if !trace_BEST_FIRST 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   318
	       then writeln("state size = " ^ string_of_int n ^ 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   319
		         "  queue length =" ^ string_of_int (length nprfs))  
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   320
               else ();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   321
	     bfs (Sequence.list_of_s (tf prf), nprfs))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   322
      fun tf st = bfs (Sequence.list_of_s (tf0 st),  [])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   323
  in traced_tac tf end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   324
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   325
(*Ordinary best-first search, with no initial tactic*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   326
fun BEST_FIRST (satp,sizef) tac = all_tac THEN_BEST_FIRST (satp,sizef,tac);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   327
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
(*Breadth-first search to satisfy satpred (including initial state) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   329
  SLOW -- SHOULD NOT USE APPEND!*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   330
fun BREADTH_FIRST satpred (Tactic tf) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
  let val tacf = Sequence.list_of_s o tf;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   332
      fun bfs prfs =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   333
	 (case  partition satpred prfs  of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   334
	      ([],[]) => []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   335
	    | ([],nonsats) => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   336
		  (prs("breadth=" ^ string_of_int(length nonsats) ^ "\n");
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   337
		   bfs (flat (map tacf nonsats)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   338
	    | (sats,_)  => sats)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   339
  in Tactic (fn state => Sequence.s_of_list (bfs [state])) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   340
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   341
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   342
(** Filtering tacticals **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   343
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   344
(*Returns all states satisfying the predicate*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   345
fun FILTER pred (Tactic tf) = Tactic
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   346
      (fn state => Sequence.filters pred (tf state));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   347
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   348
(*Returns all changed states*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   349
fun CHANGED (Tactic tf)  = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   350
  Tactic (fn state => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   351
    let fun diff st = not (eq_thm(state,st))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   352
    in  Sequence.filters diff (tf state)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   353
    end );
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   354
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   355
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   356
(*** Tacticals based on subgoal numbering ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   357
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   358
(*For n subgoals, performs tf(n) THEN ... THEN tf(1) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   359
  Essential to work backwards since tf(i) may add/delete subgoals at i. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   360
fun ALLGOALS tf = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   361
  let fun tac 0 = all_tac
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   362
	| tac n = tf(n) THEN tac(n-1)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   363
  in  Tactic(fn state => tapply(tac(nprems_of state), state))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   364
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   365
(*For n subgoals, performs tf(n) ORELSE ... ORELSE tf(1)  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   366
fun SOMEGOAL tf = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   367
  let fun tac 0 = no_tac
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   368
	| tac n = tf(n) ORELSE tac(n-1)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   369
  in  Tactic(fn state => tapply(tac(nprems_of state), state))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   370
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   371
(*For n subgoals, performs tf(1) ORELSE ... ORELSE tf(n).
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   372
  More appropriate than SOMEGOAL in some cases.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   373
fun FIRSTGOAL tf = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   374
  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
   375
  in  Tactic(fn state => tapply(tac(1, nprems_of state), state))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   376
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   377
(*Repeatedly solve some using tf. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   378
fun REPEAT_SOME tf = REPEAT1 (SOMEGOAL (REPEAT1 o tf));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   379
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   380
(*Repeatedly solve the first possible subgoal using tf. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   381
fun REPEAT_FIRST tf = REPEAT1 (FIRSTGOAL (REPEAT1 o tf));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   382
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   383
(*For n subgoals, tries to apply tf to n,...1  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   384
fun TRYALL tf = ALLGOALS (TRY o tf);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   385
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   386
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   387
(*Make a tactic for subgoal i, if there is one.  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   388
fun SUBGOAL goalfun i = Tactic(fn state =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   389
  case drop(i-1, prems_of state) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   390
      [] => Sequence.null
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   391
    | prem::_ => tapply(goalfun (prem,i), state));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   392
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   393
(*Tactical for restricting the effect of a tactic to subgoal i.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   394
  Works by making a new state from subgoal i, applying tf to it, and
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   395
  composing the resulting metathm with the original state.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   396
  The "main goal" of the new state will not be atomic, some tactics may fail!
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   397
  DOES NOT work if tactic affects the main goal other than by instantiation.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   398
31
eb01df4ffe66 tctical/dummy_quant_rl: specifies type prop to avoid the type variable
lcp
parents: 0
diff changeset
   399
(* (!!x. PROP ?V) ==> PROP ?V ;  contains NO TYPE VARIABLES.*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   400
val dummy_quant_rl = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   401
  standard (forall_elim_var 0 (assume 
230
ec8a2b6aa8a7 Many other files modified as follows:
lcp
parents: 67
diff changeset
   402
                  (read_cterm Sign.pure ("!!x::prop. PROP V",propT))));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   403
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   404
(* Prevent the subgoal's assumptions from becoming additional subgoals in the
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   405
   new proof state by enclosing them by a universal quantification *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   406
fun protect_subgoal state i =
31
eb01df4ffe66 tctical/dummy_quant_rl: specifies type prop to avoid the type variable
lcp
parents: 0
diff changeset
   407
  case Sequence.chop (1, bicompose false (false,dummy_quant_rl,1) i state) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   408
      ([state'],_) => state'
31
eb01df4ffe66 tctical/dummy_quant_rl: specifies type prop to avoid the type variable
lcp
parents: 0
diff changeset
   409
    | _            => error"SELECT_GOAL -- impossible error???";
0
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)
230
ec8a2b6aa8a7 Many other files modified as follows:
lcp
parents: 67
diff changeset
   414
      val st0 = trivial (cterm_of (#sign(rep_thm state)) prem);
0
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
fun SELECT_GOAL tac i = Tactic (fn state =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   420
  case (i, drop(i-1, prems_of state)) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   421
      (_,[]) => Sequence.null
31
eb01df4ffe66 tctical/dummy_quant_rl: specifies type prop to avoid the type variable
lcp
parents: 0
diff changeset
   422
    | (1,[_]) => tapply(tac,state)  (*If i=1 and only one subgoal do nothing!*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   423
    | (_, (Const("==>",_)$_$_) :: _) => select tac (protect_subgoal state i) i
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   424
    | (_, _::_) => select tac state i);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   425
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   426
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   427
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   428
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters. 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   429
  Main difference from strip_assums concerns parameters: 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   430
    it replaces the bound variables by free variables.  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   431
fun strip_context_aux (params, Hs, Const("==>", _) $ H $ B) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   432
	strip_context_aux (params, H::Hs, B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   433
  | strip_context_aux (params, Hs, Const("all",_)$Abs(a,T,t)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   434
        let val (b,u) = variant_abs(a,T,t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   435
	in  strip_context_aux ((b,T)::params, Hs, u)  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   436
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   437
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   438
fun strip_context A = strip_context_aux ([],[],A);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   439
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   440
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   441
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   442
       METAHYPS (fn prems => tac (prems)) i
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   443
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   444
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   445
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   446
"prems").  The parameters x1,...,xm become free variables.  If the
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   447
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   448
then it is lifted back into the original context, yielding k subgoals.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   449
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   450
Replaces unknowns in the context by Frees having the prefix METAHYP_
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   451
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   452
DOES NOT HANDLE TYPE UNKNOWNS.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   453
****)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   454
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   455
local 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   456
  open Logic 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   457
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   458
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   459
    Instantiates distinct free variables by terms of same type.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   460
  fun free_instantiate ctpairs = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   461
      forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   462
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   463
  fun free_of s ((a,i), T) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   464
        Free(s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   465
	     T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   466
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   467
  fun mk_inst (var as Var(v,T))  = (var,  free_of "METAHYP1_" (v,T))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   468
in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   469
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   470
fun metahyps_aux_tac tacf (prem,i) = Tactic (fn state =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   471
  let val {sign,maxidx,...} = rep_thm state
230
ec8a2b6aa8a7 Many other files modified as follows:
lcp
parents: 67
diff changeset
   472
      val cterm = cterm_of sign
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   473
      (*find all vars in the hyps -- should find tvars also!*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   474
      val hyps_vars = foldr add_term_vars (strip_assums_hyp prem, [])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   475
      val insts = map mk_inst hyps_vars
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   476
      (*replace the hyps_vars by Frees*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   477
      val prem' = subst_atomic insts prem
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   478
      val (params,hyps,concl) = strip_context prem'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   479
      val fparams = map Free params
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   480
      val cparams = map cterm fparams
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   481
      and chyps = map cterm hyps
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   482
      val hypths = map assume chyps
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   483
      fun swap_ctpair (t,u) = (cterm u, cterm t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   484
      (*Subgoal variables: make Free; lift type over params*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   485
      fun mk_subgoal_inst concl_vars (var as Var(v,T)) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   486
          if var mem concl_vars 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   487
	  then (var, true, free_of "METAHYP2_" (v,T))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   488
	  else (var, false,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   489
		free_of "METAHYP2_" (v, map #2 params --->T))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   490
      (*Instantiate subgoal vars by Free applied to params*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   491
      fun mk_ctpair (t,in_concl,u) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   492
	  if in_concl then (cterm t,  cterm u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   493
          else (cterm t,  cterm (list_comb (u,fparams)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   494
      (*Restore Vars with higher type and index*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   495
      fun mk_subgoal_swap_ctpair 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   496
		(t as Var((a,i),_), in_concl, u as Free(_,U)) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   497
	  if in_concl then (cterm u, cterm t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   498
          else (cterm u, cterm(Var((a, i+maxidx), U)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   499
      (*Embed B in the original context of params and hyps*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   500
      fun embed B = list_all_free (params, list_implies (hyps, B))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   501
      (*Strip the context using elimination rules*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   502
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   503
      (*Embed an ff pair in the original params*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   504
      fun embed_ff(t,u) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   505
	  mk_flexpair (list_abs_free (params, t), list_abs_free (params, u))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   506
      (*Remove parameter abstractions from the ff pairs*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   507
      fun elim_ff ff = flexpair_abs_elim_list cparams ff
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   508
      (*A form of lifting that discharges assumptions.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   509
      fun relift st = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   510
	let val prop = #prop(rep_thm st)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   511
	    val subgoal_vars = (*Vars introduced in the subgoals*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   512
		  foldr add_term_vars (strip_imp_prems prop, [])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   513
	    and concl_vars = add_term_vars (strip_imp_concl prop, [])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   514
	    val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   515
	    val st' = instantiate ([], map mk_ctpair subgoal_insts) st
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   516
	    val emBs = map (cterm o embed) (prems_of st')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   517
            and ffs = map (cterm o embed_ff) (tpairs_of st')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   518
	    val Cth  = implies_elim_list st' 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   519
			    (map (elim_ff o assume) ffs @
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   520
			     map (elim o assume) emBs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   521
	in  (*restore the unknowns to the hypotheses*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   522
	    free_instantiate (map swap_ctpair insts @
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   523
			      map mk_subgoal_swap_ctpair subgoal_insts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   524
		(*discharge assumptions from state in same order*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   525
		(implies_intr_list (ffs@emBs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   526
		  (forall_intr_list cparams (implies_intr_list chyps Cth)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   527
	end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   528
      val subprems = map (forall_elim_vars 0) hypths
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   529
      and st0 = trivial (cterm concl)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   530
      (*function to replace the current subgoal*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   531
      fun next st = bicompose false (false, relift st, nprems_of st)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   532
	            i state
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   533
  in  Sequence.flats (Sequence.maps next (tapply(tacf subprems, st0)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   534
  end);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   535
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   536
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   537
fun METAHYPS tacf = SUBGOAL (metahyps_aux_tac tacf);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   538
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   539
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   540
end;