src/Pure/logic.ML
author nipkow
Tue Oct 21 17:36:54 1997 +0200 (1997-10-21)
changeset 3963 29c5ec9ecbaa
parent 3915 0eb9b9dd4de6
child 4116 42606637f87f
permissions -rw-r--r--
Corrected alphabetical order of entries in signature.
clasohm@1460
     1
(*  Title: 	Pure/logic.ML
clasohm@0
     2
    ID:         $Id$
clasohm@1460
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   Cambridge University 1992
clasohm@0
     5
clasohm@0
     6
Supporting code for defining the abstract type "thm"
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
infix occs;
clasohm@0
    10
clasohm@0
    11
signature LOGIC = 
clasohm@0
    12
  sig
clasohm@1460
    13
  val assum_pairs	: term -> (term*term)list
clasohm@1460
    14
  val auto_rename	: bool ref   
clasohm@1460
    15
  val close_form	: term -> term   
clasohm@1460
    16
  val count_prems	: term * int -> int
clasohm@1460
    17
  val dest_equals	: term -> term * term
clasohm@1460
    18
  val dest_flexpair	: term -> term * term
clasohm@1460
    19
  val dest_implies	: term -> term * term
clasohm@1460
    20
  val dest_inclass	: term -> typ * class
clasohm@1460
    21
  val dest_type		: term -> typ
clasohm@1460
    22
  val flatten_params	: int -> term -> term
clasohm@1460
    23
  val incr_indexes	: typ list * int -> term -> term
nipkow@3963
    24
  val is_equals         : term -> bool
clasohm@1460
    25
  val lift_fns		: term * int -> (term -> term) * (term -> term)
clasohm@1460
    26
  val list_flexpairs	: (term*term)list * term -> term
clasohm@1460
    27
  val list_implies	: term list * term -> term
clasohm@0
    28
  val list_rename_params: string list * term -> term
nipkow@3893
    29
  val loops		: Sign.sg -> term list -> term -> term
nipkow@3893
    30
                          -> string option * bool
clasohm@1460
    31
  val mk_equals		: term * term -> term
clasohm@1460
    32
  val mk_flexpair	: term * term -> term
clasohm@1460
    33
  val mk_implies	: term * term -> term
clasohm@1460
    34
  val mk_inclass	: typ * class -> term
clasohm@1460
    35
  val mk_type		: typ -> term
clasohm@1460
    36
  val occs		: term * term -> bool
clasohm@1460
    37
  val rule_of		: (term*term)list * term list * term -> term
clasohm@1460
    38
  val set_rename_prefix	: string -> unit   
clasohm@1460
    39
  val skip_flexpairs	: term -> term
clasohm@0
    40
  val strip_assums_concl: term -> term
clasohm@1460
    41
  val strip_assums_hyp	: term -> term list
clasohm@1460
    42
  val strip_flexpairs	: term -> (term*term)list * term
clasohm@1460
    43
  val strip_horn	: term -> (term*term)list * term list * term
clasohm@1460
    44
  val strip_imp_concl	: term -> term
clasohm@1460
    45
  val strip_imp_prems	: term -> term list
clasohm@1460
    46
  val strip_params	: term -> (string * typ) list
clasohm@1460
    47
  val strip_prems	: int * term list * term -> term list * term
wenzelm@2508
    48
  val unvarify		: term -> term
wenzelm@2508
    49
  val varify		: term -> term
wenzelm@2508
    50
  val termord		: term * term -> order
wenzelm@2508
    51
  val lextermord	: term list * term list -> order
wenzelm@2508
    52
  val termless		: term * term -> bool
clasohm@0
    53
  end;
clasohm@0
    54
paulson@1500
    55
structure Logic : LOGIC =
clasohm@0
    56
struct
wenzelm@398
    57
clasohm@0
    58
(*** Abstract syntax operations on the meta-connectives ***)
clasohm@0
    59
clasohm@0
    60
(** equality **)
clasohm@0
    61
paulson@1835
    62
(*Make an equality.  DOES NOT CHECK TYPE OF u*)
lcp@64
    63
fun mk_equals(t,u) = equals(fastype_of t) $ t $ u;
clasohm@0
    64
clasohm@0
    65
fun dest_equals (Const("==",_) $ t $ u)  =  (t,u)
clasohm@0
    66
  | dest_equals t = raise TERM("dest_equals", [t]);
clasohm@0
    67
wenzelm@637
    68
fun is_equals (Const ("==", _) $ _ $ _) = true
wenzelm@637
    69
  | is_equals _ = false;
wenzelm@637
    70
wenzelm@637
    71
clasohm@0
    72
(** implies **)
clasohm@0
    73
clasohm@0
    74
fun mk_implies(A,B) = implies $ A $ B;
clasohm@0
    75
clasohm@0
    76
fun dest_implies (Const("==>",_) $ A $ B)  =  (A,B)
clasohm@0
    77
  | dest_implies A = raise TERM("dest_implies", [A]);
clasohm@0
    78
clasohm@0
    79
(** nested implications **)
clasohm@0
    80
clasohm@0
    81
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
clasohm@0
    82
fun list_implies ([], B) = B : term
clasohm@0
    83
  | list_implies (A::AS, B) = implies $ A $ list_implies(AS,B);
clasohm@0
    84
clasohm@0
    85
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
clasohm@0
    86
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
clasohm@0
    87
  | strip_imp_prems _ = [];
clasohm@0
    88
clasohm@0
    89
(* A1==>...An==>B  goes to B, where B is not an implication *)
clasohm@0
    90
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
clasohm@0
    91
  | strip_imp_concl A = A : term;
clasohm@0
    92
clasohm@0
    93
(*Strip and return premises: (i, [], A1==>...Ai==>B)
clasohm@1460
    94
    goes to   ([Ai, A(i-1),...,A1] , B) 	(REVERSED) 
clasohm@0
    95
  if  i<0 or else i too big then raises  TERM*)
clasohm@0
    96
fun strip_prems (0, As, B) = (As, B) 
clasohm@0
    97
  | strip_prems (i, As, Const("==>", _) $ A $ B) = 
clasohm@1460
    98
	strip_prems (i-1, A::As, B)
clasohm@0
    99
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
clasohm@0
   100
clasohm@0
   101
(*Count premises -- quicker than (length ostrip_prems) *)
clasohm@0
   102
fun count_prems (Const("==>", _) $ A $ B, n) = count_prems (B,n+1)
clasohm@0
   103
  | count_prems (_,n) = n;
clasohm@0
   104
clasohm@0
   105
(** flex-flex constraints **)
clasohm@0
   106
lcp@64
   107
(*Make a constraint.*)
lcp@64
   108
fun mk_flexpair(t,u) = flexpair(fastype_of t) $ t $ u;
clasohm@0
   109
clasohm@0
   110
fun dest_flexpair (Const("=?=",_) $ t $ u)  =  (t,u)
clasohm@0
   111
  | dest_flexpair t = raise TERM("dest_flexpair", [t]);
clasohm@0
   112
clasohm@0
   113
(*make flexflex antecedents: ( [(a1,b1),...,(an,bn)] , C )
clasohm@0
   114
    goes to (a1=?=b1) ==>...(an=?=bn)==>C *)
clasohm@0
   115
fun list_flexpairs ([], A) = A
clasohm@0
   116
  | list_flexpairs ((t,u)::pairs, A) =
clasohm@1460
   117
	implies $ (mk_flexpair(t,u)) $ list_flexpairs(pairs,A);
clasohm@0
   118
clasohm@0
   119
(*Make the object-rule tpairs==>As==>B   *)
clasohm@0
   120
fun rule_of (tpairs, As, B) = list_flexpairs(tpairs, list_implies(As, B));
clasohm@0
   121
clasohm@0
   122
(*Remove and return flexflex pairs: 
clasohm@1460
   123
    (a1=?=b1)==>...(an=?=bn)==>C  to  ( [(a1,b1),...,(an,bn)] , C )	
clasohm@0
   124
  [Tail recursive in order to return a pair of results] *)
clasohm@0
   125
fun strip_flex_aux (pairs, Const("==>", _) $ (Const("=?=",_)$t$u) $ C) =
clasohm@0
   126
        strip_flex_aux ((t,u)::pairs, C)
clasohm@0
   127
  | strip_flex_aux (pairs,C) = (rev pairs, C);
clasohm@0
   128
clasohm@0
   129
fun strip_flexpairs A = strip_flex_aux([], A);
clasohm@0
   130
clasohm@0
   131
(*Discard flexflex pairs*)
clasohm@0
   132
fun skip_flexpairs (Const("==>", _) $ (Const("=?=",_)$_$_) $ C) =
clasohm@1460
   133
	skip_flexpairs C
clasohm@0
   134
  | skip_flexpairs C = C;
clasohm@0
   135
clasohm@0
   136
(*strip a proof state (Horn clause): 
clasohm@0
   137
   (a1==b1)==>...(am==bm)==>B1==>...Bn==>C
clasohm@0
   138
    goes to   ( [(a1,b1),...,(am,bm)] , [B1,...,Bn] , C)    *)
clasohm@0
   139
fun strip_horn A =
clasohm@0
   140
  let val (tpairs,horn) = strip_flexpairs A 
clasohm@0
   141
  in  (tpairs, strip_imp_prems horn, strip_imp_concl horn)   end;
clasohm@0
   142
wenzelm@398
   143
(** types as terms **)
wenzelm@398
   144
wenzelm@398
   145
fun mk_type ty = Const ("TYPE", itselfT ty);
wenzelm@398
   146
wenzelm@398
   147
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   148
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   149
wenzelm@447
   150
(** class constraints **)
wenzelm@398
   151
wenzelm@398
   152
fun mk_inclass (ty, c) =
wenzelm@398
   153
  Const (Sign.const_of_class c, itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   154
wenzelm@398
   155
fun dest_inclass (t as Const (c_class, _) $ ty) =
wenzelm@398
   156
      ((dest_type ty, Sign.class_of_const c_class)
wenzelm@398
   157
        handle TERM _ => raise TERM ("dest_inclass", [t]))
wenzelm@398
   158
  | dest_inclass t = raise TERM ("dest_inclass", [t]);
wenzelm@398
   159
clasohm@0
   160
clasohm@0
   161
(*** Low-level term operations ***)
clasohm@0
   162
clasohm@0
   163
(*Does t occur in u?  Or is alpha-convertible to u?
clasohm@0
   164
  The term t must contain no loose bound variables*)
clasohm@0
   165
fun t occs u = (t aconv u) orelse 
clasohm@0
   166
      (case u of
clasohm@0
   167
          Abs(_,_,body) => t occs body
clasohm@1460
   168
	| f$t' => t occs f  orelse  t occs t'
clasohm@1460
   169
	| _ => false);
clasohm@0
   170
clasohm@0
   171
(*Close up a formula over all free variables by quantification*)
clasohm@0
   172
fun close_form A =
clasohm@0
   173
    list_all_free (map dest_Free (sort atless (term_frees A)),   
clasohm@1460
   174
		   A);
clasohm@0
   175
clasohm@0
   176
clasohm@0
   177
(*** Specialized operations for resolution... ***)
clasohm@0
   178
clasohm@0
   179
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   180
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
clasohm@0
   181
fun incr_indexes (Us: typ list, inc:int) t = 
clasohm@0
   182
  let fun incr (Var ((a,i), T), lev) = 
clasohm@1460
   183
		Unify.combound (Var((a, i+inc), Us---> incr_tvar inc T),
clasohm@1460
   184
				lev, length Us)
clasohm@1460
   185
	| incr (Abs (a,T,body), lev) =
clasohm@1460
   186
		Abs (a, incr_tvar inc T, incr(body,lev+1))
clasohm@1460
   187
	| incr (Const(a,T),_) = Const(a, incr_tvar inc T)
clasohm@1460
   188
	| incr (Free(a,T),_) = Free(a, incr_tvar inc T)
clasohm@1460
   189
	| incr (f$t, lev) = incr(f,lev) $ incr(t,lev)
clasohm@1460
   190
	| incr (t,lev) = t
clasohm@0
   191
  in  incr(t,0)  end;
clasohm@0
   192
clasohm@0
   193
(*Make lifting functions from subgoal and increment.
clasohm@0
   194
    lift_abs operates on tpairs (unification constraints)
clasohm@0
   195
    lift_all operates on propositions     *)
clasohm@0
   196
fun lift_fns (B,inc) =
clasohm@0
   197
  let fun lift_abs (Us, Const("==>", _) $ _ $ B) u = lift_abs (Us,B) u
clasohm@1460
   198
	| lift_abs (Us, Const("all",_)$Abs(a,T,t)) u =
clasohm@1460
   199
	      Abs(a, T, lift_abs (T::Us, t) u)
clasohm@1460
   200
	| lift_abs (Us, _) u = incr_indexes(rev Us, inc) u
clasohm@0
   201
      fun lift_all (Us, Const("==>", _) $ A $ B) u =
clasohm@1460
   202
	      implies $ A $ lift_all (Us,B) u
clasohm@1460
   203
	| lift_all (Us, Const("all",_)$Abs(a,T,t)) u = 
clasohm@1460
   204
	      all T $ Abs(a, T, lift_all (T::Us,t) u)
clasohm@1460
   205
	| lift_all (Us, _) u = incr_indexes(rev Us, inc) u;
clasohm@0
   206
  in  (lift_abs([],B), lift_all([],B))  end;
clasohm@0
   207
clasohm@0
   208
(*Strips assumptions in goal, yielding list of hypotheses.   *)
clasohm@0
   209
fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
clasohm@0
   210
  | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
clasohm@0
   211
  | strip_assums_hyp B = [];
clasohm@0
   212
clasohm@0
   213
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   214
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   215
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   216
  | strip_assums_concl B = B;
clasohm@0
   217
clasohm@0
   218
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   219
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   220
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   221
  | strip_params B = [];
clasohm@0
   222
clasohm@0
   223
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
clasohm@0
   224
    where j is the total number of parameters (precomputed) 
clasohm@0
   225
  If n>0 then deletes assumption n. *)
clasohm@0
   226
fun remove_params j n A = 
clasohm@0
   227
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   228
    else case A of
clasohm@0
   229
        Const("==>", _) $ H $ B => 
clasohm@1460
   230
	  if n=1 then                           (remove_params j (n-1) B)
clasohm@1460
   231
	  else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   232
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   233
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   234
             else A;
clasohm@0
   235
clasohm@0
   236
(** Auto-renaming of parameters in subgoals **)
clasohm@0
   237
clasohm@0
   238
val auto_rename = ref false
clasohm@0
   239
and rename_prefix = ref "ka";
clasohm@0
   240
clasohm@0
   241
(*rename_prefix is not exported; it is set by this function.*)
clasohm@0
   242
fun set_rename_prefix a =
clasohm@0
   243
    if a<>"" andalso forall is_letter (explode a)
clasohm@0
   244
    then  (rename_prefix := a;  auto_rename := true)
clasohm@0
   245
    else  error"rename prefix must be nonempty and consist of letters";
clasohm@0
   246
clasohm@0
   247
(*Makes parameters in a goal have distinctive names (not guaranteed unique!)
clasohm@0
   248
  A name clash could cause the printer to rename bound vars;
clasohm@0
   249
    then res_inst_tac would not work properly.*)
clasohm@0
   250
fun rename_vars (a, []) = []
clasohm@0
   251
  | rename_vars (a, (_,T)::vars) =
clasohm@0
   252
        (a,T) :: rename_vars (bump_string a, vars);
clasohm@0
   253
clasohm@0
   254
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   255
  if n>0 then deletes assumption n. *)
clasohm@0
   256
fun flatten_params n A =
clasohm@0
   257
    let val params = strip_params A;
clasohm@1460
   258
	val vars = if !auto_rename 
clasohm@1460
   259
		   then rename_vars (!rename_prefix, params)
paulson@2266
   260
		   else ListPair.zip (variantlist(map #1 params,[]),
paulson@2266
   261
				      map #2 params)
clasohm@0
   262
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   263
    end;
clasohm@0
   264
clasohm@0
   265
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   266
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   267
      implies $ A $ list_rename_params (cs, B)
clasohm@0
   268
  | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) = 
clasohm@0
   269
      all T $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   270
  | list_rename_params (cs, B) = B;
clasohm@0
   271
clasohm@0
   272
(*Strips assumptions in goal yielding  ( [Hn,...,H1], [xm,...,x1], B )
clasohm@0
   273
  where H1,...,Hn are the hypotheses and x1...xm are the parameters.   *)
clasohm@0
   274
fun strip_assums_aux (Hs, params, Const("==>", _) $ H $ B) = 
clasohm@1460
   275
	strip_assums_aux (H::Hs, params, B)
clasohm@0
   276
  | strip_assums_aux (Hs, params, Const("all",_)$Abs(a,T,t)) =
clasohm@1460
   277
	strip_assums_aux (Hs, (a,T)::params, t)
clasohm@0
   278
  | strip_assums_aux (Hs, params, B) = (Hs, params, B);
clasohm@0
   279
clasohm@0
   280
fun strip_assums A = strip_assums_aux ([],[],A);
clasohm@0
   281
clasohm@0
   282
clasohm@0
   283
(*Produces disagreement pairs, one for each assumption proof, in order.
clasohm@0
   284
  A is the first premise of the lifted rule, and thus has the form
clasohm@0
   285
    H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B) *)
clasohm@0
   286
fun assum_pairs A =
clasohm@0
   287
  let val (Hs, params, B) = strip_assums A
clasohm@0
   288
      val D = Unify.rlist_abs(params, B)
clasohm@0
   289
      fun pairrev ([],pairs) = pairs  
clasohm@0
   290
        | pairrev (H::Hs,pairs) = 
clasohm@1460
   291
	    pairrev(Hs, (Unify.rlist_abs(params,H), D) :: pairs)
clasohm@0
   292
  in  pairrev (Hs,[])   (*WAS:  map pair (rev Hs)  *)
clasohm@0
   293
  end;
clasohm@0
   294
clasohm@0
   295
clasohm@0
   296
(*Converts Frees to Vars and TFrees to TVars so that axioms can be written
clasohm@0
   297
  without (?) everywhere*)
clasohm@0
   298
fun varify (Const(a,T)) = Const(a, Type.varifyT T)
clasohm@0
   299
  | varify (Free(a,T)) = Var((a,0), Type.varifyT T)
clasohm@0
   300
  | varify (Var(ixn,T)) = Var(ixn, Type.varifyT T)
clasohm@0
   301
  | varify (Abs (a,T,body)) = Abs (a, Type.varifyT T, varify body)
clasohm@0
   302
  | varify (f$t) = varify f $ varify t
clasohm@0
   303
  | varify t = t;
clasohm@0
   304
lcp@546
   305
(*Inverse of varify.  Converts axioms back to their original form.*)
lcp@585
   306
fun unvarify (Const(a,T))    = Const(a, Type.unvarifyT T)
lcp@585
   307
  | unvarify (Var((a,0), T)) = Free(a, Type.unvarifyT T)
lcp@585
   308
  | unvarify (Var(ixn,T))    = Var(ixn, Type.unvarifyT T)  (*non-0 index!*)
lcp@585
   309
  | unvarify (Abs (a,T,body)) = Abs (a, Type.unvarifyT T, unvarify body)
lcp@546
   310
  | unvarify (f$t) = unvarify f $ unvarify t
lcp@546
   311
  | unvarify t = t;
lcp@546
   312
wenzelm@2508
   313
wenzelm@2508
   314
(*** term order ***)
wenzelm@2508
   315
wenzelm@2508
   316
(* NB: non-linearity of the ordering is not a soundness problem *)
wenzelm@2508
   317
wenzelm@2508
   318
(* FIXME: "***ABSTRACTION***" is a hack and makes the ordering non-linear *)
wenzelm@2508
   319
fun string_of_hd(Const(a,_)) = a
wenzelm@2508
   320
  | string_of_hd(Free(a,_))  = a
wenzelm@2508
   321
  | string_of_hd(Var(v,_))   = Syntax.string_of_vname v
wenzelm@2508
   322
  | string_of_hd(Bound i)    = string_of_int i
wenzelm@2508
   323
  | string_of_hd(Abs _)      = "***ABSTRACTION***";
wenzelm@2508
   324
wenzelm@2508
   325
(* a strict (not reflexive) linear well-founded AC-compatible ordering
wenzelm@2508
   326
 * for terms:
wenzelm@2508
   327
 * s < t <=> 1. size(s) < size(t) or
wenzelm@2508
   328
             2. size(s) = size(t) and s=f(...) and t = g(...) and f<g or
wenzelm@2508
   329
             3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and
wenzelm@2508
   330
                (s1..sn) < (t1..tn) (lexicographically)
wenzelm@2508
   331
 *)
wenzelm@2508
   332
wenzelm@2508
   333
(* FIXME: should really take types into account as well.
wenzelm@2508
   334
 * Otherwise non-linear *)
wenzelm@2508
   335
fun termord(Abs(_,_,t),Abs(_,_,u)) = termord(t,u)
wenzelm@2508
   336
  | termord(t,u) =
wenzelm@2508
   337
      (case intord(size_of_term t,size_of_term u) of
wenzelm@2508
   338
         EQUAL => let val (f,ts) = strip_comb t and (g,us) = strip_comb u
wenzelm@2508
   339
                  in case stringord(string_of_hd f, string_of_hd g) of
wenzelm@2508
   340
                       EQUAL => lextermord(ts,us)
wenzelm@2508
   341
                     | ord   => ord
wenzelm@2508
   342
                  end
wenzelm@2508
   343
       | ord => ord)
wenzelm@2508
   344
and lextermord(t::ts,u::us) =
wenzelm@2508
   345
      (case termord(t,u) of
wenzelm@2508
   346
         EQUAL => lextermord(ts,us)
wenzelm@2508
   347
       | ord   => ord)
wenzelm@2508
   348
  | lextermord([],[]) = EQUAL
wenzelm@2508
   349
  | lextermord _ = error("lextermord");
wenzelm@2508
   350
wenzelm@2508
   351
fun termless tu = (termord tu = LESS);
wenzelm@2508
   352
nipkow@3893
   353
(** Check for looping rewrite rules **)
nipkow@3893
   354
nipkow@3893
   355
fun vperm (Var _, Var _) = true
nipkow@3893
   356
  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
nipkow@3893
   357
  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
nipkow@3893
   358
  | vperm (t, u) = (t = u);
nipkow@3893
   359
nipkow@3893
   360
fun var_perm (t, u) =
nipkow@3893
   361
  vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
nipkow@3893
   362
nipkow@3893
   363
(*simple test for looping rewrite*)
nipkow@3893
   364
fun looptest sign prems lhs rhs =
nipkow@3893
   365
   is_Var (head_of lhs)
nipkow@3893
   366
  orelse
paulson@3915
   367
   (exists (apl (lhs, op occs)) (rhs :: prems))
nipkow@3893
   368
  orelse
nipkow@3893
   369
   (null prems andalso
nipkow@3893
   370
    Pattern.matches (#tsig (Sign.rep_sg sign)) (lhs, rhs));
nipkow@3893
   371
(*the condition "null prems" in the last case is necessary because
nipkow@3893
   372
  conditional rewrites with extra variables in the conditions may terminate
nipkow@3893
   373
  although the rhs is an instance of the lhs. Example:
nipkow@3893
   374
  ?m < ?n ==> f(?n) == f(?m)*)
nipkow@3893
   375
nipkow@3893
   376
fun loops sign prems lhs rhs =
nipkow@3893
   377
  let
nipkow@3893
   378
    val elhs = Pattern.eta_contract lhs;
nipkow@3893
   379
    val erhs = Pattern.eta_contract rhs;
nipkow@3893
   380
    val perm = var_perm (elhs, erhs) andalso not (elhs aconv erhs)
nipkow@3893
   381
               andalso not (is_Var elhs)
nipkow@3893
   382
  in (if not ((term_vars erhs) subset
nipkow@3893
   383
              (union_term (term_vars elhs, List.concat(map term_vars prems))))
nipkow@3893
   384
      then Some("extra Var(s) on rhs") else
nipkow@3893
   385
      if not perm andalso looptest sign prems elhs erhs
nipkow@3893
   386
      then Some("loops")
nipkow@3893
   387
      else None
nipkow@3893
   388
      ,perm)
nipkow@3893
   389
  end;
nipkow@3893
   390
clasohm@0
   391
end;