src/Pure/logic.ML
changeset 4318 9b672ea2dfe7
parent 4116 42606637f87f
child 4345 7e9436ffb813
equal deleted inserted replaced
4317:7264fa2ff2ec 4318:9b672ea2dfe7
   312   | unvarify t = t;
   312   | unvarify t = t;
   313 
   313 
   314 
   314 
   315 (*** term order ***)
   315 (*** term order ***)
   316 
   316 
   317 (* NB: non-linearity of the ordering is not a soundness problem *)
   317 fun dest_hd(Const(a,T)) = (a,T,0)
   318 
   318   | dest_hd(Free(a,T))  = (a,T,1)
   319 (* FIXME: "***ABSTRACTION***" is a hack and makes the ordering non-linear *)
   319   | dest_hd(Var(v,T))   = (Syntax.string_of_vname v, T, 2)
   320 fun string_of_hd(Const(a,_)) = a
   320   | dest_hd(Bound i)    = (string_of_int i,dummyT,3)
   321   | string_of_hd(Free(a,_))  = a
   321   | dest_hd(Abs(x,T,_)) = (x,T,4);
   322   | string_of_hd(Var(v,_))   = Syntax.string_of_vname v
   322 
   323   | string_of_hd(Bound i)    = string_of_int i
   323 (* assumes that vars/frees with the same name have same classes *)
   324   | string_of_hd(Abs _)      = "***ABSTRACTION***";
   324 fun typord(T,U) = if T=U then EQUAL else
   325 
   325  (case (T,U) of
   326 (* a strict (not reflexive) linear well-founded AC-compatible ordering
   326     (Type(a,Ts),Type(b,Us)) =>
       
   327       (case stringord(a,b) of EQUAL => lextypord(Ts,Us) | ord => ord)
       
   328   | (Type _, _) => GREATER
       
   329   | (TFree _,Type _) => LESS
       
   330   | (TFree(a,_),TFree(b,_)) => stringord(a,b)
       
   331   | (TFree _, TVar _) => GREATER
       
   332   | (TVar _,Type _) => LESS
       
   333   | (TVar _,TFree _) => LESS
       
   334   | (TVar(va,_),TVar(vb,_)) =>
       
   335       stringord(Syntax.string_of_vname va,Syntax.string_of_vname vb))
       
   336 and lextypord(T::Ts,U::Us) =
       
   337       (case typord(T,U) of
       
   338          EQUAL => lextypord(Ts,Us)
       
   339        | ord   => ord)
       
   340   | lextypord([],[]) = EQUAL
       
   341   | lextypord _ = error("lextypord");
       
   342 
       
   343 
       
   344 (* a linear well-founded AC-compatible ordering
   327  * for terms:
   345  * for terms:
   328  * s < t <=> 1. size(s) < size(t) or
   346  * s < t <=> 1. size(s) < size(t) or
   329              2. size(s) = size(t) and s=f(...) and t = g(...) and f<g or
   347              2. size(s) = size(t) and s=f(...) and t = g(...) and f<g or
   330              3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and
   348              3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and
   331                 (s1..sn) < (t1..tn) (lexicographically)
   349                 (s1..sn) < (t1..tn) (lexicographically)
   332  *)
   350  *)
   333 
   351 
   334 (* FIXME: should really take types into account as well.
   352 fun termord(Abs(x,T,t),Abs(y,U,u)) =
   335  * Otherwise non-linear *)
   353       (case termord(t,u) of EQUAL => typord(T,U) | ord => ord)
   336 fun termord(Abs(_,_,t),Abs(_,_,u)) = termord(t,u)
       
   337   | termord(t,u) =
   354   | termord(t,u) =
   338       (case intord(size_of_term t,size_of_term u) of
   355       (case intord(size_of_term t,size_of_term u) of
   339          EQUAL => let val (f,ts) = strip_comb t and (g,us) = strip_comb u
   356          EQUAL => let val (f,ts) = strip_comb t and (g,us) = strip_comb u
   340                   in case stringord(string_of_hd f, string_of_hd g) of
   357                       val (a,T,i) = dest_hd f and (b,U,j) = dest_hd g
   341                        EQUAL => lextermord(ts,us)
   358                   in case stringord(a,b) of
       
   359                        EQUAL => (case typord(T,U) of
       
   360                                    EQUAL => (case intord(i,j) of
       
   361                                                EQUAL => lextermord(ts,us)
       
   362                                              | ord => ord)
       
   363                                  | ord => ord)
   342                      | ord   => ord
   364                      | ord   => ord
   343                   end
   365                   end
   344        | ord => ord)
   366        | ord => ord)
   345 and lextermord(t::ts,u::us) =
   367 and lextermord(t::ts,u::us) =
   346       (case termord(t,u) of
   368       (case termord(t,u) of