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