src/HOL/Tools/Sledgehammer/metis_translate.ML
author blanchet
Thu Sep 16 16:12:02 2010 +0200 (2010-09-16)
changeset 39494 bf7dd4902321
parent 39355 src/HOL/Tools/Sledgehammer/metis_clauses.ML@104a6d9e493e
child 39497 fa16349939b7
permissions -rw-r--r--
rename "Metis_Clauses" to "Metis_Translate" for consistency with "Sledgehammer_Translate"
     1 (*  Title:      HOL/Tools/Sledgehammer/metis_translate.ML
     2     Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
     3     Author:     Jasmin Blanchette, TU Muenchen
     4 
     5 Translation of HOL to FOL for Metis.
     6 *)
     7 
     8 signature METIS_TRANSLATE =
     9 sig
    10   type name = string * string
    11   datatype type_literal =
    12     TyLitVar of name * name |
    13     TyLitFree of name * name
    14   datatype arLit =
    15     TConsLit of name * name * name list |
    16     TVarLit of name * name
    17   datatype arity_clause =
    18     ArityClause of {name: string, conclLit: arLit, premLits: arLit list}
    19   datatype class_rel_clause =
    20     ClassRelClause of {name: string, subclass: name, superclass: name}
    21   datatype combtyp =
    22     CombTVar of name |
    23     CombTFree of name |
    24     CombType of name * combtyp list
    25   datatype combterm =
    26     CombConst of name * combtyp * combtyp list (* Const and Free *) |
    27     CombVar of name * combtyp |
    28     CombApp of combterm * combterm
    29   datatype fol_literal = FOLLiteral of bool * combterm
    30 
    31   val type_wrapper_name : string
    32   val bound_var_prefix : string
    33   val schematic_var_prefix: string
    34   val fixed_var_prefix: string
    35   val tvar_prefix: string
    36   val tfree_prefix: string
    37   val const_prefix: string
    38   val type_const_prefix: string
    39   val class_prefix: string
    40   val invert_const: string -> string
    41   val ascii_of: string -> string
    42   val unascii_of: string -> string
    43   val strip_prefix_and_unascii: string -> string -> string option
    44   val make_bound_var : string -> string
    45   val make_schematic_var : string * int -> string
    46   val make_fixed_var : string -> string
    47   val make_schematic_type_var : string * int -> string
    48   val make_fixed_type_var : string -> string
    49   val make_fixed_const : string -> string
    50   val make_fixed_type_const : string -> string
    51   val make_type_class : string -> string
    52   val skolem_theory_name: string
    53   val skolem_prefix: string
    54   val skolem_infix: string
    55   val is_skolem_const_name: string -> bool
    56   val num_type_args: theory -> string -> int
    57   val type_literals_for_types : typ list -> type_literal list
    58   val make_class_rel_clauses: theory -> class list -> class list -> class_rel_clause list
    59   val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
    60   val combtyp_of : combterm -> combtyp
    61   val strip_combterm_comb : combterm -> combterm * combterm list
    62   val combterm_from_term :
    63     theory -> (string * typ) list -> term -> combterm * typ list
    64   val literals_of_term : theory -> term -> fol_literal list * typ list
    65   val conceal_skolem_terms :
    66     int -> (string * term) list -> term -> (string * term) list * term
    67   val reveal_skolem_terms : (string * term) list -> term -> term
    68   val tfree_classes_of_terms : term list -> string list
    69   val tvar_classes_of_terms : term list -> string list
    70   val type_consts_of_terms : theory -> term list -> string list
    71 end
    72 
    73 structure Metis_Translate : METIS_TRANSLATE =
    74 struct
    75 
    76 val type_wrapper_name = "ti"
    77 
    78 val bound_var_prefix = "B_"
    79 val schematic_var_prefix = "V_"
    80 val fixed_var_prefix = "v_"
    81 
    82 val tvar_prefix = "T_";
    83 val tfree_prefix = "t_";
    84 
    85 val const_prefix = "c_";
    86 val type_const_prefix = "tc_";
    87 val class_prefix = "class_";
    88 
    89 fun union_all xss = fold (union (op =)) xss []
    90 
    91 (* Readable names for the more common symbolic functions. Do not mess with the
    92    last nine entries of the table unless you know what you are doing. *)
    93 val const_trans_table =
    94   Symtab.make [(@{type_name Product_Type.prod}, "prod"),
    95                (@{type_name Sum_Type.sum}, "sum"),
    96                (@{const_name HOL.eq}, "equal"),
    97                (@{const_name HOL.conj}, "and"),
    98                (@{const_name HOL.disj}, "or"),
    99                (@{const_name HOL.implies}, "implies"),
   100                (@{const_name Set.member}, "member"),
   101                (@{const_name fequal}, "fequal"),
   102                (@{const_name COMBI}, "COMBI"),
   103                (@{const_name COMBK}, "COMBK"),
   104                (@{const_name COMBB}, "COMBB"),
   105                (@{const_name COMBC}, "COMBC"),
   106                (@{const_name COMBS}, "COMBS"),
   107                (@{const_name True}, "True"),
   108                (@{const_name False}, "False"),
   109                (@{const_name If}, "If")]
   110 
   111 (* Invert the table of translations between Isabelle and ATPs. *)
   112 val const_trans_table_inv =
   113   Symtab.update ("fequal", @{const_name HOL.eq})
   114                 (Symtab.make (map swap (Symtab.dest const_trans_table)))
   115 
   116 val invert_const = perhaps (Symtab.lookup const_trans_table_inv)
   117 
   118 (*Escaping of special characters.
   119   Alphanumeric characters are left unchanged.
   120   The character _ goes to __
   121   Characters in the range ASCII space to / go to _A to _P, respectively.
   122   Other characters go to _nnn where nnn is the decimal ASCII code.*)
   123 val A_minus_space = Char.ord #"A" - Char.ord #" ";
   124 
   125 fun stringN_of_int 0 _ = ""
   126   | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
   127 
   128 fun ascii_of_c c =
   129   if Char.isAlphaNum c then String.str c
   130   else if c = #"_" then "__"
   131   else if #" " <= c andalso c <= #"/"
   132        then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
   133   else ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
   134 
   135 val ascii_of = String.translate ascii_of_c;
   136 
   137 (** Remove ASCII armouring from names in proof files **)
   138 
   139 (*We don't raise error exceptions because this code can run inside the watcher.
   140   Also, the errors are "impossible" (hah!)*)
   141 fun unascii_aux rcs [] = String.implode(rev rcs)
   142   | unascii_aux rcs [#"_"] = unascii_aux (#"_"::rcs) []  (*ERROR*)
   143       (*Three types of _ escapes: __, _A to _P, _nnn*)
   144   | unascii_aux rcs (#"_" :: #"_" :: cs) = unascii_aux (#"_"::rcs) cs
   145   | unascii_aux rcs (#"_" :: c :: cs) =
   146       if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
   147       then unascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
   148       else
   149         let val digits = List.take (c::cs, 3) handle Subscript => []
   150         in
   151             case Int.fromString (String.implode digits) of
   152                 NONE => unascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
   153               | SOME n => unascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
   154         end
   155   | unascii_aux rcs (c::cs) = unascii_aux (c::rcs) cs
   156 val unascii_of = unascii_aux [] o String.explode
   157 
   158 (* If string s has the prefix s1, return the result of deleting it,
   159    un-ASCII'd. *)
   160 fun strip_prefix_and_unascii s1 s =
   161   if String.isPrefix s1 s then
   162     SOME (unascii_of (String.extract (s, size s1, NONE)))
   163   else
   164     NONE
   165 
   166 (*Remove the initial ' character from a type variable, if it is present*)
   167 fun trim_type_var s =
   168   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   169   else error ("trim_type: Malformed type variable encountered: " ^ s);
   170 
   171 fun ascii_of_indexname (v,0) = ascii_of v
   172   | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
   173 
   174 fun make_bound_var x = bound_var_prefix ^ ascii_of x
   175 fun make_schematic_var v = schematic_var_prefix ^ ascii_of_indexname v
   176 fun make_fixed_var x = fixed_var_prefix ^ ascii_of x
   177 
   178 fun make_schematic_type_var (x,i) =
   179       tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
   180 fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
   181 
   182 fun lookup_const c =
   183   case Symtab.lookup const_trans_table c of
   184     SOME c' => c'
   185   | NONE => ascii_of c
   186 
   187 (* HOL.eq MUST BE "equal" because it's built into ATPs. *)
   188 fun make_fixed_const @{const_name HOL.eq} = "equal"
   189   | make_fixed_const c = const_prefix ^ lookup_const c
   190 
   191 fun make_fixed_type_const c = type_const_prefix ^ lookup_const c
   192 
   193 fun make_type_class clas = class_prefix ^ ascii_of clas;
   194 
   195 val skolem_theory_name = "Sledgehammer" ^ Long_Name.separator ^ "Sko"
   196 val skolem_prefix = "sko_"
   197 val skolem_infix = "$"
   198 
   199 (* Hack: Could return false positives (e.g., a user happens to declare a
   200    constant called "SomeTheory.sko_means_shoe_in_$wedish". *)
   201 val is_skolem_const_name =
   202   Long_Name.base_name
   203   #> String.isPrefix skolem_prefix andf String.isSubstring skolem_infix
   204 
   205 (* The number of type arguments of a constant, zero if it's monomorphic. For
   206    (instances of) Skolem pseudoconstants, this information is encoded in the
   207    constant name. *)
   208 fun num_type_args thy s =
   209   if String.isPrefix skolem_theory_name s then
   210     s |> unprefix skolem_theory_name
   211       |> space_explode skolem_infix |> hd
   212       |> space_explode "_" |> List.last |> Int.fromString |> the
   213   else
   214     (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
   215 
   216 (**** Definitions and functions for FOL clauses for TPTP format output ****)
   217 
   218 type name = string * string
   219 
   220 (**** Isabelle FOL clauses ****)
   221 
   222 (* The first component is the type class; the second is a TVar or TFree. *)
   223 datatype type_literal =
   224   TyLitVar of name * name |
   225   TyLitFree of name * name
   226 
   227 (*Make literals for sorted type variables*)
   228 fun sorts_on_typs_aux (_, [])   = []
   229   | sorts_on_typs_aux ((x,i),  s::ss) =
   230       let val sorts = sorts_on_typs_aux ((x,i), ss)
   231       in
   232           if s = "HOL.type" then sorts
   233           else if i = ~1 then TyLitFree (`make_type_class s, `make_fixed_type_var x) :: sorts
   234           else TyLitVar (`make_type_class s, (make_schematic_type_var (x,i), x)) :: sorts
   235       end;
   236 
   237 fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
   238   | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
   239 
   240 (*Given a list of sorted type variables, return a list of type literals.*)
   241 fun type_literals_for_types Ts =
   242   fold (union (op =)) (map sorts_on_typs Ts) []
   243 
   244 (** make axiom and conjecture clauses. **)
   245 
   246 (**** Isabelle arities ****)
   247 
   248 datatype arLit =
   249   TConsLit of name * name * name list |
   250   TVarLit of name * name
   251 
   252 datatype arity_clause =
   253   ArityClause of {name: string, conclLit: arLit, premLits: arLit list}
   254 
   255 
   256 fun gen_TVars 0 = []
   257   | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
   258 
   259 fun pack_sort(_,[])  = []
   260   | pack_sort(tvar, "HOL.type"::srt) = pack_sort (tvar, srt)   (*IGNORE sort "type"*)
   261   | pack_sort(tvar, cls::srt) =
   262     (`make_type_class cls, (tvar, tvar)) :: pack_sort (tvar, srt)
   263 
   264 (*Arity of type constructor tcon :: (arg1,...,argN)res*)
   265 fun make_axiom_arity_clause (tcons, name, (cls,args)) =
   266   let
   267     val tvars = gen_TVars (length args)
   268     val tvars_srts = ListPair.zip (tvars, args)
   269   in
   270     ArityClause {name = name,
   271                  conclLit = TConsLit (`make_type_class cls,
   272                                       `make_fixed_type_const tcons,
   273                                       tvars ~~ tvars),
   274                  premLits = map TVarLit (union_all (map pack_sort tvars_srts))}
   275   end
   276 
   277 
   278 (**** Isabelle class relations ****)
   279 
   280 datatype class_rel_clause =
   281   ClassRelClause of {name: string, subclass: name, superclass: name}
   282 
   283 (*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
   284 fun class_pairs _ [] _ = []
   285   | class_pairs thy subs supers =
   286       let
   287         val class_less = Sorts.class_less (Sign.classes_of thy)
   288         fun add_super sub super = class_less (sub, super) ? cons (sub, super)
   289         fun add_supers sub = fold (add_super sub) supers
   290       in fold add_supers subs [] end
   291 
   292 fun make_class_rel_clause (sub,super) =
   293   ClassRelClause {name = sub ^ "_" ^ super,
   294                   subclass = `make_type_class sub,
   295                   superclass = `make_type_class super}
   296 
   297 fun make_class_rel_clauses thy subs supers =
   298   map make_class_rel_clause (class_pairs thy subs supers);
   299 
   300 
   301 (** Isabelle arities **)
   302 
   303 fun arity_clause _ _ (_, []) = []
   304   | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
   305       arity_clause seen n (tcons,ars)
   306   | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
   307       if member (op =) seen class then (*multiple arities for the same tycon, class pair*)
   308           make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
   309           arity_clause seen (n+1) (tcons,ars)
   310       else
   311           make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class, ar) ::
   312           arity_clause (class::seen) n (tcons,ars)
   313 
   314 fun multi_arity_clause [] = []
   315   | multi_arity_clause ((tcons, ars) :: tc_arlists) =
   316       arity_clause [] 1 (tcons, ars) @ multi_arity_clause tc_arlists
   317 
   318 (*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
   319   provided its arguments have the corresponding sorts.*)
   320 fun type_class_pairs thy tycons classes =
   321   let val alg = Sign.classes_of thy
   322       fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
   323       fun add_class tycon class =
   324         cons (class, domain_sorts tycon class)
   325         handle Sorts.CLASS_ERROR _ => I
   326       fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
   327   in  map try_classes tycons  end;
   328 
   329 (*Proving one (tycon, class) membership may require proving others, so iterate.*)
   330 fun iter_type_class_pairs _ _ [] = ([], [])
   331   | iter_type_class_pairs thy tycons classes =
   332       let val cpairs = type_class_pairs thy tycons classes
   333           val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
   334             |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
   335           val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
   336       in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
   337 
   338 fun make_arity_clauses thy tycons classes =
   339   let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
   340   in  (classes', multi_arity_clause cpairs)  end;
   341 
   342 datatype combtyp =
   343   CombTVar of name |
   344   CombTFree of name |
   345   CombType of name * combtyp list
   346 
   347 datatype combterm =
   348   CombConst of name * combtyp * combtyp list (* Const and Free *) |
   349   CombVar of name * combtyp |
   350   CombApp of combterm * combterm
   351 
   352 datatype fol_literal = FOLLiteral of bool * combterm
   353 
   354 (*********************************************************************)
   355 (* convert a clause with type Term.term to a clause with type clause *)
   356 (*********************************************************************)
   357 
   358 (*Result of a function type; no need to check that the argument type matches.*)
   359 fun result_type (CombType (_, [_, tp2])) = tp2
   360   | result_type _ = raise Fail "non-function type"
   361 
   362 fun combtyp_of (CombConst (_, tp, _)) = tp
   363   | combtyp_of (CombVar (_, tp)) = tp
   364   | combtyp_of (CombApp (t1, _)) = result_type (combtyp_of t1)
   365 
   366 (*gets the head of a combinator application, along with the list of arguments*)
   367 fun strip_combterm_comb u =
   368     let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
   369         |   stripc  x =  x
   370     in stripc(u,[]) end
   371 
   372 fun type_of (Type (a, Ts)) =
   373     let val (folTypes,ts) = types_of Ts in
   374       (CombType (`make_fixed_type_const a, folTypes), ts)
   375     end
   376   | type_of (tp as TFree (a, _)) = (CombTFree (`make_fixed_type_var a), [tp])
   377   | type_of (tp as TVar (x, _)) =
   378     (CombTVar (make_schematic_type_var x, string_of_indexname x), [tp])
   379 and types_of Ts =
   380     let val (folTyps, ts) = ListPair.unzip (map type_of Ts) in
   381       (folTyps, union_all ts)
   382     end
   383 
   384 (* same as above, but no gathering of sort information *)
   385 fun simp_type_of (Type (a, Ts)) =
   386       CombType (`make_fixed_type_const a, map simp_type_of Ts)
   387   | simp_type_of (TFree (a, _)) = CombTFree (`make_fixed_type_var a)
   388   | simp_type_of (TVar (x, _)) =
   389     CombTVar (make_schematic_type_var x, string_of_indexname x)
   390 
   391 (* Converts a term (with combinators) into a combterm. Also accummulates sort
   392    infomation. *)
   393 fun combterm_from_term thy bs (P $ Q) =
   394       let val (P', tsP) = combterm_from_term thy bs P
   395           val (Q', tsQ) = combterm_from_term thy bs Q
   396       in  (CombApp (P', Q'), union (op =) tsP tsQ)  end
   397   | combterm_from_term thy _ (Const (c, T)) =
   398       let
   399         val (tp, ts) = type_of T
   400         val tvar_list =
   401           (if String.isPrefix skolem_theory_name c then
   402              [] |> Term.add_tvarsT T |> map TVar
   403            else
   404              (c, T) |> Sign.const_typargs thy)
   405           |> map simp_type_of
   406         val c' = CombConst (`make_fixed_const c, tp, tvar_list)
   407       in  (c',ts)  end
   408   | combterm_from_term _ _ (Free (v, T)) =
   409       let val (tp,ts) = type_of T
   410           val v' = CombConst (`make_fixed_var v, tp, [])
   411       in  (v',ts)  end
   412   | combterm_from_term _ _ (Var (v, T)) =
   413       let val (tp,ts) = type_of T
   414           val v' = CombVar ((make_schematic_var v, string_of_indexname v), tp)
   415       in  (v',ts)  end
   416   | combterm_from_term _ bs (Bound j) =
   417       let
   418         val (s, T) = nth bs j
   419         val (tp, ts) = type_of T
   420         val v' = CombConst (`make_bound_var s, tp, [])
   421       in (v', ts) end
   422   | combterm_from_term _ _ (Abs _) = raise Fail "HOL clause: Abs"
   423 
   424 fun predicate_of thy ((@{const Not} $ P), pos) = predicate_of thy (P, not pos)
   425   | predicate_of thy (t, pos) =
   426     (combterm_from_term thy [] (Envir.eta_contract t), pos)
   427 
   428 fun literals_of_term1 args thy (@{const Trueprop} $ P) =
   429     literals_of_term1 args thy P
   430   | literals_of_term1 args thy (@{const HOL.disj} $ P $ Q) =
   431     literals_of_term1 (literals_of_term1 args thy P) thy Q
   432   | literals_of_term1 (lits, ts) thy P =
   433     let val ((pred, ts'), pol) = predicate_of thy (P, true) in
   434       (FOLLiteral (pol, pred) :: lits, union (op =) ts ts')
   435     end
   436 val literals_of_term = literals_of_term1 ([], [])
   437 
   438 fun skolem_name i j num_T_args =
   439   skolem_prefix ^ (space_implode "_" (map Int.toString [i, j, num_T_args])) ^
   440   skolem_infix ^ "g"
   441 
   442 fun conceal_skolem_terms i skolems t =
   443   if exists_Const (curry (op =) @{const_name skolem} o fst) t then
   444     let
   445       fun aux skolems
   446               (t as (Const (@{const_name skolem}, Type (_, [_, T])) $ _)) =
   447           let
   448             val (skolems, s) =
   449               if i = ~1 then
   450                 (skolems, @{const_name undefined})
   451               else case AList.find (op aconv) skolems t of
   452                 s :: _ => (skolems, s)
   453               | [] =>
   454                 let
   455                   val s = skolem_theory_name ^ "." ^
   456                           skolem_name i (length skolems)
   457                                         (length (Term.add_tvarsT T []))
   458                 in ((s, t) :: skolems, s) end
   459           in (skolems, Const (s, T)) end
   460         | aux skolems (t1 $ t2) =
   461           let
   462             val (skolems, t1) = aux skolems t1
   463             val (skolems, t2) = aux skolems t2
   464           in (skolems, t1 $ t2) end
   465         | aux skolems (Abs (s, T, t')) =
   466           let val (skolems, t') = aux skolems t' in
   467             (skolems, Abs (s, T, t'))
   468           end
   469         | aux skolems t = (skolems, t)
   470     in aux skolems t end
   471   else
   472     (skolems, t)
   473 
   474 fun reveal_skolem_terms skolems =
   475   map_aterms (fn t as Const (s, _) =>
   476                  if String.isPrefix skolem_theory_name s then
   477                    AList.lookup (op =) skolems s |> the
   478                    |> map_types Type_Infer.paramify_vars
   479                  else
   480                    t
   481                | t => t)
   482 
   483 
   484 (***************************************************************)
   485 (* Type Classes Present in the Axiom or Conjecture Clauses     *)
   486 (***************************************************************)
   487 
   488 fun set_insert (x, s) = Symtab.update (x, ()) s
   489 
   490 fun add_classes (sorts, cset) = List.foldl set_insert cset (flat sorts)
   491 
   492 (*Remove this trivial type class*)
   493 fun delete_type cset = Symtab.delete_safe (the_single @{sort HOL.type}) cset;
   494 
   495 fun tfree_classes_of_terms ts =
   496   let val sorts_list = map (map #2 o OldTerm.term_tfrees) ts
   497   in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
   498 
   499 fun tvar_classes_of_terms ts =
   500   let val sorts_list = map (map #2 o OldTerm.term_tvars) ts
   501   in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
   502 
   503 (*fold type constructors*)
   504 fun fold_type_consts f (Type (a, Ts)) x = fold (fold_type_consts f) Ts (f (a,x))
   505   | fold_type_consts _ _ x = x;
   506 
   507 (*Type constructors used to instantiate overloaded constants are the only ones needed.*)
   508 fun add_type_consts_in_term thy =
   509   let
   510     fun aux (Const x) =
   511         fold (fold_type_consts set_insert) (Sign.const_typargs thy x)
   512       | aux (Abs (_, _, u)) = aux u
   513       | aux (Const (@{const_name skolem}, _) $ _) = I
   514       | aux (t $ u) = aux t #> aux u
   515       | aux _ = I
   516   in aux end
   517 
   518 fun type_consts_of_terms thy ts =
   519   Symtab.keys (fold (add_type_consts_in_term thy) ts Symtab.empty);
   520 
   521 end;