src/Pure/logic.ML
author clasohm
Thu, 16 Sep 1993 12:20:38 +0200
changeset 0 a5a9c433f639
child 64 0bbe5d86cb38
permissions -rw-r--r--
Initial revision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title: 	logic
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   Cambridge University 1992
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
Supporting code for defining the abstract type "thm"
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 occs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
signature LOGIC = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
  sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
  val assum_pairs: term -> (term*term)list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
  val auto_rename: bool ref   
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
  val close_form: term -> term   
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
  val count_prems: term * int -> int
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
  val dest_equals: term -> term * term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  val dest_flexpair: term -> term * term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  val dest_implies: term -> term * term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
  val flatten_params: int -> term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  val freeze_vars: term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  val incr_indexes: typ list * int -> term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
  val lift_fns: term * int -> (term -> term) * (term -> term)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
  val list_flexpairs: (term*term)list * term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
  val list_implies: term list * term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
  val list_rename_params: string list * term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
  val mk_equals: term * term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
  val mk_flexpair: term * term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
  val mk_implies: term * term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
  val occs: term * term -> bool
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
  val rule_of: (term*term)list * term list * term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
  val set_rename_prefix: string -> unit   
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
  val skip_flexpairs: term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
  val strip_assums_concl: term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
  val strip_assums_hyp: term -> term list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
  val strip_flexpairs: term -> (term*term)list * term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
  val strip_horn: term -> (term*term)list * term list * term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
  val strip_imp_concl: term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
  val strip_imp_prems: term -> term list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
  val strip_params: term -> (string * typ) list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
  val strip_prems: int * term list * term -> term list * term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
  val thaw_vars: term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
  val varify: term -> term  
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
functor LogicFun (structure Unify: UNIFY and Net:NET) : LOGIC  = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
structure Type = Unify.Sign.Type;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
(*** Abstract syntax operations on the meta-connectives ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
(** equality **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
(*Make an equality.  DOES NOT CHECK TYPE OF u! *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
fun mk_equals(t,u) = equals(type_of t) $ t $ u;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
fun dest_equals (Const("==",_) $ t $ u)  =  (t,u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
  | dest_equals t = raise TERM("dest_equals", [t]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
(** implies **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
fun mk_implies(A,B) = implies $ A $ B;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
fun dest_implies (Const("==>",_) $ A $ B)  =  (A,B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
  | dest_implies A = raise TERM("dest_implies", [A]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
(** nested implications **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
fun list_implies ([], B) = B : term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
  | list_implies (A::AS, B) = implies $ A $ list_implies(AS,B);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
  | strip_imp_prems _ = [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
(* A1==>...An==>B  goes to B, where B is not an implication *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
  | strip_imp_concl A = A : term;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
(*Strip and return premises: (i, [], A1==>...Ai==>B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
    goes to   ([Ai, A(i-1),...,A1] , B) 	(REVERSED) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
  if  i<0 or else i too big then raises  TERM*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
fun strip_prems (0, As, B) = (As, B) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
  | strip_prems (i, As, Const("==>", _) $ A $ B) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
	strip_prems (i-1, A::As, B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
(*Count premises -- quicker than (length ostrip_prems) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
fun count_prems (Const("==>", _) $ A $ B, n) = count_prems (B,n+1)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
  | count_prems (_,n) = n;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
(** flex-flex constraints **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
(*Make a constraint.  DOES NOT CHECK TYPE OF u! *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
fun mk_flexpair(t,u) = flexpair(type_of t) $ t $ u;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
fun dest_flexpair (Const("=?=",_) $ t $ u)  =  (t,u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
  | dest_flexpair t = raise TERM("dest_flexpair", [t]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
(*make flexflex antecedents: ( [(a1,b1),...,(an,bn)] , C )
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
    goes to (a1=?=b1) ==>...(an=?=bn)==>C *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
fun list_flexpairs ([], A) = A
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
  | list_flexpairs ((t,u)::pairs, A) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
	implies $ (mk_flexpair(t,u)) $ list_flexpairs(pairs,A);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
(*Make the object-rule tpairs==>As==>B   *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
fun rule_of (tpairs, As, B) = list_flexpairs(tpairs, list_implies(As, B));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
(*Remove and return flexflex pairs: 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
    (a1=?=b1)==>...(an=?=bn)==>C  to  ( [(a1,b1),...,(an,bn)] , C )	
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
  [Tail recursive in order to return a pair of results] *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
fun strip_flex_aux (pairs, Const("==>", _) $ (Const("=?=",_)$t$u) $ C) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
        strip_flex_aux ((t,u)::pairs, C)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
  | strip_flex_aux (pairs,C) = (rev pairs, C);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
fun strip_flexpairs A = strip_flex_aux([], A);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
(*Discard flexflex pairs*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
fun skip_flexpairs (Const("==>", _) $ (Const("=?=",_)$_$_) $ C) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
	skip_flexpairs C
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
  | skip_flexpairs C = C;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
(*strip a proof state (Horn clause): 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
   (a1==b1)==>...(am==bm)==>B1==>...Bn==>C
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
    goes to   ( [(a1,b1),...,(am,bm)] , [B1,...,Bn] , C)    *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
fun strip_horn A =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
  let val (tpairs,horn) = strip_flexpairs A 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
  in  (tpairs, strip_imp_prems horn, strip_imp_concl horn)   end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
(*** Low-level term operations ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
(*Does t occur in u?  Or is alpha-convertible to u?
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
  The term t must contain no loose bound variables*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
fun t occs u = (t aconv u) orelse 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
      (case u of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
          Abs(_,_,body) => t occs body
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
	| f$t' => t occs f  orelse  t occs t'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
	| _ => false);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
(*Close up a formula over all free variables by quantification*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
fun close_form A =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
    list_all_free (map dest_Free (sort atless (term_frees A)),   
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
		   A);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
(*Freeze all (T)Vars by turning them into (T)Frees*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
fun freeze_vars(Var(ixn,T)) = Free(Syntax.string_of_vname ixn,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
                                   Type.freeze_vars T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
  | freeze_vars(Const(a,T)) = Const(a,Type.freeze_vars T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
  | freeze_vars(Free(a,T))  = Free(a,Type.freeze_vars T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
  | freeze_vars(s$t)        = freeze_vars s $ freeze_vars t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
  | freeze_vars(Abs(a,T,t)) = Abs(a,Type.freeze_vars T,freeze_vars t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
  | freeze_vars(b)          = b;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
(*Reverse the effect of freeze_vars*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
fun thaw_vars(Const(a,T)) = Const(a,Type.thaw_vars T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
  | thaw_vars(Free(a,T))  =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
      let val T' = Type.thaw_vars T
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
      in case explode a of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
	   "?"::vn => let val (ixn,_) = Syntax.scan_varname vn
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
                      in Var(ixn,T') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
	 | _       => Free(a,T')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
      end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
  | thaw_vars(Abs(a,T,t)) = Abs(a,Type.thaw_vars T, thaw_vars t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
  | thaw_vars(s$t)        = thaw_vars s $ thaw_vars t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
  | thaw_vars(b)          = b;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
(*** Specialized operations for resolution... ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
(*For all variables in the term, increment indexnames and lift over the Us
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
fun incr_indexes (Us: typ list, inc:int) t = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
  let fun incr (Var ((a,i), T), lev) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
		Unify.combound (Var((a, i+inc), Us---> incr_tvar inc T),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
				lev, length Us)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
	| incr (Abs (a,T,body), lev) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
		Abs (a, incr_tvar inc T, incr(body,lev+1))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
	| incr (Const(a,T),_) = Const(a, incr_tvar inc T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
	| incr (Free(a,T),_) = Free(a, incr_tvar inc T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   183
	| incr (f$t, lev) = incr(f,lev) $ incr(t,lev)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
	| incr (t,lev) = t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
  in  incr(t,0)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
(*Make lifting functions from subgoal and increment.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
    lift_abs operates on tpairs (unification constraints)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
    lift_all operates on propositions     *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
fun lift_fns (B,inc) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
  let fun lift_abs (Us, Const("==>", _) $ _ $ B) u = lift_abs (Us,B) u
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
	| lift_abs (Us, Const("all",_)$Abs(a,T,t)) u =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
	      Abs(a, T, lift_abs (T::Us, t) u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
	| lift_abs (Us, _) u = incr_indexes(rev Us, inc) u
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
      fun lift_all (Us, Const("==>", _) $ A $ B) u =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
	      implies $ A $ lift_all (Us,B) u
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
	| lift_all (Us, Const("all",_)$Abs(a,T,t)) u = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
	      all T $ Abs(a, T, lift_all (T::Us,t) u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
	| lift_all (Us, _) u = incr_indexes(rev Us, inc) u;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
  in  (lift_abs([],B), lift_all([],B))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
(*Strips assumptions in goal, yielding list of hypotheses.   *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
  | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
  | strip_assums_hyp B = [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
(*Strips assumptions in goal, yielding conclusion.   *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
  | strip_assums_concl B = B;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
(*Make a list of all the parameters in a subgoal, even if nested*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
  | strip_params B = [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
    where j is the total number of parameters (precomputed) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
  If n>0 then deletes assumption n. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
fun remove_params j n A = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
    else case A of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
        Const("==>", _) $ H $ B => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
	  if n=1 then                           (remove_params j (n-1) B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
	  else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
      | _ => if n>0 then raise TERM("remove_params", [A])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
             else A;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
(** Auto-renaming of parameters in subgoals **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
val auto_rename = ref false
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
and rename_prefix = ref "ka";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
(*rename_prefix is not exported; it is set by this function.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
fun set_rename_prefix a =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
    if a<>"" andalso forall is_letter (explode a)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
    then  (rename_prefix := a;  auto_rename := true)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
    else  error"rename prefix must be nonempty and consist of letters";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
(*Makes parameters in a goal have distinctive names (not guaranteed unique!)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
  A name clash could cause the printer to rename bound vars;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
    then res_inst_tac would not work properly.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
fun rename_vars (a, []) = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
  | rename_vars (a, (_,T)::vars) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
        (a,T) :: rename_vars (bump_string a, vars);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
(*Move all parameters to the front of the subgoal, renaming them apart;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
  if n>0 then deletes assumption n. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
fun flatten_params n A =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
    let val params = strip_params A;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
	val vars = if !auto_rename 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
		   then rename_vars (!rename_prefix, params)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
		   else variantlist(map #1 params,[]) ~~ map #2 params
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
    in  list_all (vars, remove_params (length vars) n A)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
(*Makes parameters in a goal have the names supplied by the list cs.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
      implies $ A $ list_rename_params (cs, B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
  | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   262
      all T $ Abs(c, T, list_rename_params (cs, t))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
  | list_rename_params (cs, B) = B;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
(*Strips assumptions in goal yielding  ( [Hn,...,H1], [xm,...,x1], B )
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
  where H1,...,Hn are the hypotheses and x1...xm are the parameters.   *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   267
fun strip_assums_aux (Hs, params, Const("==>", _) $ H $ B) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
	strip_assums_aux (H::Hs, params, B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
  | strip_assums_aux (Hs, params, Const("all",_)$Abs(a,T,t)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
	strip_assums_aux (Hs, (a,T)::params, t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   271
  | strip_assums_aux (Hs, params, B) = (Hs, params, B);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
fun strip_assums A = strip_assums_aux ([],[],A);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
(*Produces disagreement pairs, one for each assumption proof, in order.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
  A is the first premise of the lifted rule, and thus has the form
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
    H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
fun assum_pairs A =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
  let val (Hs, params, B) = strip_assums A
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
      val D = Unify.rlist_abs(params, B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
      fun pairrev ([],pairs) = pairs  
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   283
        | pairrev (H::Hs,pairs) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   284
	    pairrev(Hs, (Unify.rlist_abs(params,H), D) :: pairs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
  in  pairrev (Hs,[])   (*WAS:  map pair (rev Hs)  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   289
(*Converts Frees to Vars and TFrees to TVars so that axioms can be written
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
  without (?) everywhere*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   291
fun varify (Const(a,T)) = Const(a, Type.varifyT T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
  | varify (Free(a,T)) = Var((a,0), Type.varifyT T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
  | varify (Var(ixn,T)) = Var(ixn, Type.varifyT T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
  | varify (Abs (a,T,body)) = Abs (a, Type.varifyT T, varify body)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   295
  | varify (f$t) = varify f $ varify t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
  | varify t = t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   297
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
end;