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