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