src/HOL/Tools/Metis/metis_translate.ML
author blanchet
Tue Oct 26 10:57:04 2010 +0200 (2010-10-26)
changeset 40145 04a05b2a7a36
parent 39962 d42ddd7407ca
child 40157 a2f01956220e
permissions -rw-r--r--
no need to encode theorem number twice in skolem names
blanchet@39958
     1
(*  Title:      HOL/Tools/Metis/metis_translate.ML
blanchet@38027
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@39497
     3
    Author:     Kong W. Susanto, Cambridge University Computer Laboratory
blanchet@39497
     4
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@36393
     5
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     6
blanchet@39494
     7
Translation of HOL to FOL for Metis.
paulson@15347
     8
*)
paulson@15347
     9
blanchet@39494
    10
signature METIS_TRANSLATE =
wenzelm@24310
    11
sig
blanchet@37577
    12
  type name = string * string
blanchet@37577
    13
  datatype type_literal =
blanchet@37643
    14
    TyLitVar of name * name |
blanchet@37643
    15
    TyLitFree of name * name
blanchet@37577
    16
  datatype arLit =
blanchet@37643
    17
    TConsLit of name * name * name list |
blanchet@37643
    18
    TVarLit of name * name
blanchet@38028
    19
  datatype arity_clause =
blanchet@38105
    20
    ArityClause of {name: string, conclLit: arLit, premLits: arLit list}
blanchet@38028
    21
  datatype class_rel_clause =
blanchet@38105
    22
    ClassRelClause of {name: string, subclass: name, superclass: name}
blanchet@37577
    23
  datatype combtyp =
blanchet@37924
    24
    CombTVar of name |
blanchet@37924
    25
    CombTFree of name |
blanchet@37924
    26
    CombType of name * combtyp list
blanchet@37577
    27
  datatype combterm =
blanchet@37577
    28
    CombConst of name * combtyp * combtyp list (* Const and Free *) |
blanchet@37577
    29
    CombVar of name * combtyp |
blanchet@37577
    30
    CombApp of combterm * combterm
blanchet@37923
    31
  datatype fol_literal = FOLLiteral of bool * combterm
blanchet@37577
    32
blanchet@39497
    33
  datatype mode = FO | HO | FT
blanchet@39497
    34
  type logic_map =
blanchet@39497
    35
    {axioms: (Metis_Thm.thm * thm) list,
blanchet@39497
    36
     tfrees: type_literal list,
blanchet@39886
    37
     old_skolems: (string * term) list}
blanchet@39497
    38
blanchet@37509
    39
  val type_wrapper_name : string
blanchet@37995
    40
  val bound_var_prefix : string
wenzelm@24310
    41
  val schematic_var_prefix: string
wenzelm@24310
    42
  val fixed_var_prefix: string
wenzelm@24310
    43
  val tvar_prefix: string
wenzelm@24310
    44
  val tfree_prefix: string
wenzelm@24310
    45
  val const_prefix: string
blanchet@37643
    46
  val type_const_prefix: string
wenzelm@24310
    47
  val class_prefix: string
blanchet@39896
    48
  val new_skolem_const_prefix : string
blanchet@37572
    49
  val invert_const: string -> string
wenzelm@24310
    50
  val ascii_of: string -> string
blanchet@38748
    51
  val unascii_of: string -> string
blanchet@38748
    52
  val strip_prefix_and_unascii: string -> string -> string option
blanchet@37995
    53
  val make_bound_var : string -> string
wenzelm@24310
    54
  val make_schematic_var : string * int -> string
paulson@18868
    55
  val make_fixed_var : string -> string
paulson@18868
    56
  val make_schematic_type_var : string * int -> string
wenzelm@24310
    57
  val make_fixed_type_var : string -> string
blanchet@37498
    58
  val make_fixed_const : string -> string
blanchet@37498
    59
  val make_fixed_type_const : string -> string
paulson@18868
    60
  val make_type_class : string -> string
blanchet@37618
    61
  val num_type_args: theory -> string -> int
blanchet@39886
    62
  val new_skolem_var_from_const: string -> indexname
blanchet@36966
    63
  val type_literals_for_types : typ list -> type_literal list
blanchet@39499
    64
  val make_class_rel_clauses :
blanchet@39499
    65
    theory -> class list -> class list -> class_rel_clause list
blanchet@39499
    66
  val make_arity_clauses :
blanchet@39499
    67
    theory -> string list -> class list -> class list * arity_clause list
blanchet@37994
    68
  val combtyp_of : combterm -> combtyp
blanchet@37577
    69
  val strip_combterm_comb : combterm -> combterm * combterm list
blanchet@37995
    70
  val combterm_from_term :
blanchet@40145
    71
    theory -> (string * typ) list -> term -> combterm * typ list
blanchet@39886
    72
  val reveal_old_skolem_terms : (string * term) list -> term -> term
blanchet@37577
    73
  val tfree_classes_of_terms : term list -> string list
blanchet@37577
    74
  val tvar_classes_of_terms : term list -> string list
blanchet@37577
    75
  val type_consts_of_terms : theory -> term list -> string list
blanchet@39497
    76
  val string_of_mode : mode -> string
blanchet@39497
    77
  val build_logic_map :
blanchet@39886
    78
    mode -> Proof.context -> bool -> thm list -> thm list list
blanchet@39886
    79
    -> mode * logic_map
wenzelm@24310
    80
end
paulson@15347
    81
blanchet@39494
    82
structure Metis_Translate : METIS_TRANSLATE =
paulson@15347
    83
struct
paulson@15347
    84
blanchet@37509
    85
val type_wrapper_name = "ti"
blanchet@37509
    86
blanchet@37995
    87
val bound_var_prefix = "B_"
blanchet@37995
    88
val schematic_var_prefix = "V_"
blanchet@37995
    89
val fixed_var_prefix = "v_"
paulson@15347
    90
paulson@17230
    91
val tvar_prefix = "T_";
paulson@17230
    92
val tfree_prefix = "t_";
paulson@15347
    93
paulson@17230
    94
val const_prefix = "c_";
blanchet@37643
    95
val type_const_prefix = "tc_";
wenzelm@24310
    96
val class_prefix = "class_";
paulson@15347
    97
blanchet@39896
    98
val skolem_const_prefix = "Sledgehammer" ^ Long_Name.separator ^ "Sko"
blanchet@39896
    99
val old_skolem_const_prefix = skolem_const_prefix ^ "o"
blanchet@39896
   100
val new_skolem_const_prefix = skolem_const_prefix ^ "n"
blanchet@39886
   101
blanchet@36218
   102
fun union_all xss = fold (union (op =)) xss []
paulson@17775
   103
blanchet@36493
   104
(* Readable names for the more common symbolic functions. Do not mess with the
blanchet@37479
   105
   last nine entries of the table unless you know what you are doing. *)
paulson@15347
   106
val const_trans_table =
haftmann@37678
   107
  Symtab.make [(@{type_name Product_Type.prod}, "prod"),
haftmann@37678
   108
               (@{type_name Sum_Type.sum}, "sum"),
haftmann@38864
   109
               (@{const_name HOL.eq}, "equal"),
haftmann@38795
   110
               (@{const_name HOL.conj}, "and"),
haftmann@38795
   111
               (@{const_name HOL.disj}, "or"),
haftmann@38786
   112
               (@{const_name HOL.implies}, "implies"),
blanchet@38653
   113
               (@{const_name Set.member}, "member"),
blanchet@39962
   114
               (@{const_name Metis.fequal}, "fequal"),
blanchet@39953
   115
               (@{const_name Meson.COMBI}, "COMBI"),
blanchet@39953
   116
               (@{const_name Meson.COMBK}, "COMBK"),
blanchet@39953
   117
               (@{const_name Meson.COMBB}, "COMBB"),
blanchet@39953
   118
               (@{const_name Meson.COMBC}, "COMBC"),
blanchet@39953
   119
               (@{const_name Meson.COMBS}, "COMBS"),
blanchet@37479
   120
               (@{const_name True}, "True"),
blanchet@37479
   121
               (@{const_name False}, "False"),
blanchet@37643
   122
               (@{const_name If}, "If")]
paulson@15347
   123
blanchet@37572
   124
(* Invert the table of translations between Isabelle and ATPs. *)
blanchet@37572
   125
val const_trans_table_inv =
haftmann@38864
   126
  Symtab.update ("fequal", @{const_name HOL.eq})
blanchet@37572
   127
                (Symtab.make (map swap (Symtab.dest const_trans_table)))
blanchet@37572
   128
blanchet@37572
   129
val invert_const = perhaps (Symtab.lookup const_trans_table_inv)
paulson@15347
   130
paulson@15610
   131
(*Escaping of special characters.
paulson@15610
   132
  Alphanumeric characters are left unchanged.
paulson@15610
   133
  The character _ goes to __
paulson@15610
   134
  Characters in the range ASCII space to / go to _A to _P, respectively.
blanchet@38738
   135
  Other characters go to _nnn where nnn is the decimal ASCII code.*)
paulson@24183
   136
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   137
paulson@24183
   138
fun stringN_of_int 0 _ = ""
paulson@24183
   139
  | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
paulson@15610
   140
paulson@15347
   141
fun ascii_of_c c =
paulson@15610
   142
  if Char.isAlphaNum c then String.str c
paulson@15610
   143
  else if c = #"_" then "__"
wenzelm@24310
   144
  else if #" " <= c andalso c <= #"/"
paulson@15610
   145
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
blanchet@38738
   146
  else ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
paulson@15347
   147
paulson@15610
   148
val ascii_of = String.translate ascii_of_c;
paulson@15610
   149
paulson@24183
   150
(** Remove ASCII armouring from names in proof files **)
paulson@24183
   151
paulson@24183
   152
(*We don't raise error exceptions because this code can run inside the watcher.
paulson@24183
   153
  Also, the errors are "impossible" (hah!)*)
blanchet@38748
   154
fun unascii_aux rcs [] = String.implode(rev rcs)
blanchet@38748
   155
  | unascii_aux rcs [#"_"] = unascii_aux (#"_"::rcs) []  (*ERROR*)
paulson@24183
   156
      (*Three types of _ escapes: __, _A to _P, _nnn*)
blanchet@38748
   157
  | unascii_aux rcs (#"_" :: #"_" :: cs) = unascii_aux (#"_"::rcs) cs
blanchet@38748
   158
  | unascii_aux rcs (#"_" :: c :: cs) =
paulson@24183
   159
      if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
blanchet@38748
   160
      then unascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
wenzelm@24310
   161
      else
paulson@24183
   162
        let val digits = List.take (c::cs, 3) handle Subscript => []
wenzelm@24310
   163
        in
paulson@24183
   164
            case Int.fromString (String.implode digits) of
blanchet@38748
   165
                NONE => unascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
blanchet@38748
   166
              | SOME n => unascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
paulson@24183
   167
        end
blanchet@38748
   168
  | unascii_aux rcs (c::cs) = unascii_aux (c::rcs) cs
blanchet@38748
   169
val unascii_of = unascii_aux [] o String.explode
paulson@15347
   170
blanchet@37572
   171
(* If string s has the prefix s1, return the result of deleting it,
blanchet@37572
   172
   un-ASCII'd. *)
blanchet@38748
   173
fun strip_prefix_and_unascii s1 s =
blanchet@37572
   174
  if String.isPrefix s1 s then
blanchet@38748
   175
    SOME (unascii_of (String.extract (s, size s1, NONE)))
blanchet@37572
   176
  else
blanchet@37572
   177
    NONE
blanchet@37572
   178
paulson@16925
   179
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   180
fun trim_type_var s =
paulson@16925
   181
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   182
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   183
paulson@16903
   184
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   185
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   186
blanchet@37995
   187
fun make_bound_var x = bound_var_prefix ^ ascii_of x
blanchet@37995
   188
fun make_schematic_var v = schematic_var_prefix ^ ascii_of_indexname v
blanchet@37995
   189
fun make_fixed_var x = fixed_var_prefix ^ ascii_of x
paulson@15347
   190
wenzelm@24310
   191
fun make_schematic_type_var (x,i) =
paulson@16925
   192
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   193
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   194
blanchet@37498
   195
fun lookup_const c =
blanchet@37498
   196
  case Symtab.lookup const_trans_table c of
blanchet@37498
   197
    SOME c' => c'
blanchet@37498
   198
  | NONE => ascii_of c
paulson@23075
   199
haftmann@38864
   200
(* HOL.eq MUST BE "equal" because it's built into ATPs. *)
haftmann@38864
   201
fun make_fixed_const @{const_name HOL.eq} = "equal"
blanchet@37498
   202
  | make_fixed_const c = const_prefix ^ lookup_const c
paulson@18411
   203
blanchet@37643
   204
fun make_fixed_type_const c = type_const_prefix ^ lookup_const c
quigley@17150
   205
wenzelm@17261
   206
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   207
blanchet@37618
   208
(* The number of type arguments of a constant, zero if it's monomorphic. For
blanchet@37618
   209
   (instances of) Skolem pseudoconstants, this information is encoded in the
blanchet@37618
   210
   constant name. *)
blanchet@37618
   211
fun num_type_args thy s =
blanchet@39896
   212
  if String.isPrefix skolem_const_prefix s then
blanchet@39499
   213
    s |> space_explode Long_Name.separator |> 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@39886
   217
fun new_skolem_var_from_const s =
blanchet@40145
   218
  let val ss = s |> space_explode Long_Name.separator in
blanchet@40145
   219
    (nth ss (length ss - 2), 0)
blanchet@40145
   220
  end
blanchet@39886
   221
blanchet@39886
   222
blanchet@37624
   223
(**** Definitions and functions for FOL clauses for TPTP format output ****)
blanchet@36169
   224
blanchet@37624
   225
type name = string * string
paulson@15347
   226
paulson@15347
   227
(**** Isabelle FOL clauses ****)
paulson@15347
   228
blanchet@36556
   229
(* The first component is the type class; the second is a TVar or TFree. *)
blanchet@36556
   230
datatype type_literal =
blanchet@37643
   231
  TyLitVar of name * name |
blanchet@37643
   232
  TyLitFree of name * name
paulson@15347
   233
wenzelm@24310
   234
(*Make literals for sorted type variables*)
paulson@24940
   235
fun sorts_on_typs_aux (_, [])   = []
paulson@24940
   236
  | sorts_on_typs_aux ((x,i),  s::ss) =
paulson@24940
   237
      let val sorts = sorts_on_typs_aux ((x,i), ss)
paulson@22643
   238
      in
paulson@22643
   239
          if s = "HOL.type" then sorts
blanchet@37643
   240
          else if i = ~1 then TyLitFree (`make_type_class s, `make_fixed_type_var x) :: sorts
blanchet@37643
   241
          else TyLitVar (`make_type_class s, (make_schematic_type_var (x,i), x)) :: sorts
paulson@22643
   242
      end;
quigley@17150
   243
paulson@24940
   244
fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
paulson@24940
   245
  | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
paulson@24940
   246
paulson@24937
   247
(*Given a list of sorted type variables, return a list of type literals.*)
blanchet@36966
   248
fun type_literals_for_types Ts =
blanchet@36966
   249
  fold (union (op =)) (map sorts_on_typs Ts) []
mengj@20015
   250
mengj@20015
   251
(** make axiom and conjecture clauses. **)
mengj@20015
   252
paulson@15347
   253
(**** Isabelle arities ****)
paulson@15347
   254
blanchet@37643
   255
datatype arLit =
blanchet@37643
   256
  TConsLit of name * name * name list |
blanchet@37643
   257
  TVarLit of name * name
wenzelm@24310
   258
blanchet@35865
   259
datatype arity_clause =
blanchet@38105
   260
  ArityClause of {name: string, conclLit: arLit, premLits: arLit list}
paulson@15347
   261
paulson@15347
   262
paulson@18798
   263
fun gen_TVars 0 = []
paulson@18798
   264
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   265
paulson@18411
   266
fun pack_sort(_,[])  = []
blanchet@37643
   267
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort (tvar, srt)   (*IGNORE sort "type"*)
blanchet@37643
   268
  | pack_sort(tvar, cls::srt) =
blanchet@37643
   269
    (`make_type_class cls, (tvar, tvar)) :: pack_sort (tvar, srt)
wenzelm@24310
   270
paulson@18411
   271
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
blanchet@38105
   272
fun make_axiom_arity_clause (tcons, name, (cls,args)) =
blanchet@37643
   273
  let
blanchet@37643
   274
    val tvars = gen_TVars (length args)
blanchet@37643
   275
    val tvars_srts = ListPair.zip (tvars, args)
blanchet@37643
   276
  in
blanchet@39318
   277
    ArityClause {name = name,
blanchet@37643
   278
                 conclLit = TConsLit (`make_type_class cls,
blanchet@37643
   279
                                      `make_fixed_type_const tcons,
blanchet@37643
   280
                                      tvars ~~ tvars),
blanchet@37643
   281
                 premLits = map TVarLit (union_all (map pack_sort tvars_srts))}
blanchet@37643
   282
  end
paulson@15347
   283
paulson@15347
   284
paulson@15347
   285
(**** Isabelle class relations ****)
paulson@15347
   286
blanchet@37925
   287
datatype class_rel_clause =
blanchet@38105
   288
  ClassRelClause of {name: string, subclass: name, superclass: name}
wenzelm@24310
   289
paulson@21290
   290
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
blanchet@37498
   291
fun class_pairs _ [] _ = []
paulson@21432
   292
  | class_pairs thy subs supers =
blanchet@36218
   293
      let
blanchet@36218
   294
        val class_less = Sorts.class_less (Sign.classes_of thy)
blanchet@36218
   295
        fun add_super sub super = class_less (sub, super) ? cons (sub, super)
blanchet@36218
   296
        fun add_supers sub = fold (add_super sub) supers
blanchet@36218
   297
      in fold add_supers subs [] end
paulson@15347
   298
blanchet@37925
   299
fun make_class_rel_clause (sub,super) =
blanchet@38105
   300
  ClassRelClause {name = sub ^ "_" ^ super,
blanchet@37643
   301
                  subclass = `make_type_class sub,
blanchet@38105
   302
                  superclass = `make_type_class super}
paulson@15347
   303
blanchet@37925
   304
fun make_class_rel_clauses thy subs supers =
blanchet@37925
   305
  map make_class_rel_clause (class_pairs thy subs supers);
paulson@18868
   306
paulson@18868
   307
paulson@18868
   308
(** Isabelle arities **)
paulson@17845
   309
blanchet@37498
   310
fun arity_clause _ _ (_, []) = []
blanchet@37498
   311
  | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
blanchet@37498
   312
      arity_clause seen n (tcons,ars)
blanchet@37498
   313
  | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
haftmann@36692
   314
      if member (op =) seen class then (*multiple arities for the same tycon, class pair*)
blanchet@37572
   315
          make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
blanchet@37498
   316
          arity_clause seen (n+1) (tcons,ars)
paulson@21373
   317
      else
blanchet@37572
   318
          make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class, ar) ::
blanchet@37498
   319
          arity_clause (class::seen) n (tcons,ars)
paulson@17845
   320
blanchet@37498
   321
fun multi_arity_clause [] = []
blanchet@37498
   322
  | multi_arity_clause ((tcons, ars) :: tc_arlists) =
blanchet@37498
   323
      arity_clause [] 1 (tcons, ars) @ multi_arity_clause tc_arlists
paulson@17845
   324
paulson@22643
   325
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   326
  provided its arguments have the corresponding sorts.*)
paulson@21373
   327
fun type_class_pairs thy tycons classes =
paulson@21373
   328
  let val alg = Sign.classes_of thy
blanchet@36218
   329
      fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
blanchet@36218
   330
      fun add_class tycon class =
blanchet@36218
   331
        cons (class, domain_sorts tycon class)
blanchet@36218
   332
        handle Sorts.CLASS_ERROR _ => I
blanchet@36218
   333
      fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
paulson@21373
   334
  in  map try_classes tycons  end;
paulson@21373
   335
paulson@22643
   336
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
blanchet@37498
   337
fun iter_type_class_pairs _ _ [] = ([], [])
paulson@22643
   338
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   339
      let val cpairs = type_class_pairs thy tycons classes
haftmann@33040
   340
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
haftmann@33040
   341
            |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
wenzelm@24310
   342
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
haftmann@33042
   343
      in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
wenzelm@24310
   344
blanchet@37498
   345
fun make_arity_clauses thy tycons classes =
wenzelm@24310
   346
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
blanchet@37498
   347
  in  (classes', multi_arity_clause cpairs)  end;
paulson@18863
   348
blanchet@37577
   349
datatype combtyp =
blanchet@37924
   350
  CombTVar of name |
blanchet@37924
   351
  CombTFree of name |
blanchet@37924
   352
  CombType of name * combtyp list
blanchet@37577
   353
blanchet@37577
   354
datatype combterm =
blanchet@37577
   355
  CombConst of name * combtyp * combtyp list (* Const and Free *) |
blanchet@37577
   356
  CombVar of name * combtyp |
blanchet@37577
   357
  CombApp of combterm * combterm
blanchet@37577
   358
blanchet@37923
   359
datatype fol_literal = FOLLiteral of bool * combterm
blanchet@37577
   360
blanchet@37577
   361
(*********************************************************************)
blanchet@37577
   362
(* convert a clause with type Term.term to a clause with type clause *)
blanchet@37577
   363
(*********************************************************************)
blanchet@37577
   364
blanchet@37577
   365
(*Result of a function type; no need to check that the argument type matches.*)
blanchet@37924
   366
fun result_type (CombType (_, [_, tp2])) = tp2
blanchet@37577
   367
  | result_type _ = raise Fail "non-function type"
blanchet@37577
   368
blanchet@37994
   369
fun combtyp_of (CombConst (_, tp, _)) = tp
blanchet@37994
   370
  | combtyp_of (CombVar (_, tp)) = tp
blanchet@37994
   371
  | combtyp_of (CombApp (t1, _)) = result_type (combtyp_of t1)
blanchet@37577
   372
blanchet@37577
   373
(*gets the head of a combinator application, along with the list of arguments*)
blanchet@37577
   374
fun strip_combterm_comb u =
blanchet@37577
   375
    let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
blanchet@37577
   376
        |   stripc  x =  x
blanchet@37577
   377
    in stripc(u,[]) end
blanchet@37577
   378
blanchet@39497
   379
fun combtype_of (Type (a, Ts)) =
blanchet@39497
   380
    let val (folTypes, ts) = combtypes_of Ts in
blanchet@37924
   381
      (CombType (`make_fixed_type_const a, folTypes), ts)
blanchet@37577
   382
    end
blanchet@39497
   383
  | combtype_of (tp as TFree (a, _)) = (CombTFree (`make_fixed_type_var a), [tp])
blanchet@39497
   384
  | combtype_of (tp as TVar (x, _)) =
blanchet@37924
   385
    (CombTVar (make_schematic_type_var x, string_of_indexname x), [tp])
blanchet@39497
   386
and combtypes_of Ts =
blanchet@39497
   387
  let val (folTyps, ts) = ListPair.unzip (map combtype_of Ts) in
blanchet@39497
   388
    (folTyps, union_all ts)
blanchet@39497
   389
  end
blanchet@37577
   390
blanchet@37577
   391
(* same as above, but no gathering of sort information *)
blanchet@39497
   392
fun simple_combtype_of (Type (a, Ts)) =
blanchet@39497
   393
    CombType (`make_fixed_type_const a, map simple_combtype_of Ts)
blanchet@39497
   394
  | simple_combtype_of (TFree (a, _)) = CombTFree (`make_fixed_type_var a)
blanchet@39497
   395
  | simple_combtype_of (TVar (x, _)) =
blanchet@37924
   396
    CombTVar (make_schematic_type_var x, string_of_indexname x)
blanchet@37577
   397
blanchet@40145
   398
fun new_skolem_const_name s num_T_args =
blanchet@40145
   399
  [new_skolem_const_prefix, s, string_of_int num_T_args]
blanchet@39886
   400
  |> space_implode Long_Name.separator
blanchet@39886
   401
blanchet@40145
   402
(* Converts a term (with combinators) into a combterm. Also accumulates sort
blanchet@37995
   403
   infomation. *)
blanchet@40145
   404
fun combterm_from_term thy bs (P $ Q) =
blanchet@40145
   405
      let
blanchet@40145
   406
        val (P', tsP) = combterm_from_term thy bs P
blanchet@40145
   407
        val (Q', tsQ) = combterm_from_term thy bs Q
blanchet@40145
   408
      in (CombApp (P', Q'), union (op =) tsP tsQ) end
blanchet@40145
   409
  | combterm_from_term thy _ (Const (c, T)) =
blanchet@37577
   410
      let
blanchet@39497
   411
        val (tp, ts) = combtype_of T
blanchet@37577
   412
        val tvar_list =
blanchet@39896
   413
          (if String.isPrefix old_skolem_const_prefix c then
blanchet@37577
   414
             [] |> Term.add_tvarsT T |> map TVar
blanchet@37577
   415
           else
blanchet@37577
   416
             (c, T) |> Sign.const_typargs thy)
blanchet@39497
   417
          |> map simple_combtype_of
blanchet@37577
   418
        val c' = CombConst (`make_fixed_const c, tp, tvar_list)
blanchet@37577
   419
      in  (c',ts)  end
blanchet@40145
   420
  | combterm_from_term _ _ (Free (v, T)) =
blanchet@39497
   421
      let val (tp, ts) = combtype_of T
blanchet@37577
   422
          val v' = CombConst (`make_fixed_var v, tp, [])
blanchet@37577
   423
      in  (v',ts)  end
blanchet@40145
   424
  | combterm_from_term _ _ (Var (v as (s, _), T)) =
blanchet@39886
   425
    let
blanchet@39886
   426
      val (tp, ts) = combtype_of T
blanchet@39886
   427
      val v' =
blanchet@39890
   428
        if String.isPrefix Meson_Clausify.new_skolem_var_prefix s then
blanchet@39886
   429
          let
blanchet@39886
   430
            val tys = T |> strip_type |> swap |> op ::
blanchet@40145
   431
            val s' = new_skolem_const_name s (length tys)
blanchet@39886
   432
          in
blanchet@39886
   433
            CombConst (`make_fixed_const s', tp, map simple_combtype_of tys)
blanchet@39886
   434
          end
blanchet@39886
   435
        else
blanchet@39886
   436
          CombVar ((make_schematic_var v, string_of_indexname v), tp)
blanchet@39886
   437
    in (v', ts) end
blanchet@40145
   438
  | combterm_from_term _ bs (Bound j) =
blanchet@37995
   439
      let
blanchet@37995
   440
        val (s, T) = nth bs j
blanchet@39497
   441
        val (tp, ts) = combtype_of T
blanchet@37995
   442
        val v' = CombConst (`make_bound_var s, tp, [])
blanchet@37995
   443
      in (v', ts) end
blanchet@40145
   444
  | combterm_from_term _ _ (Abs _) = raise Fail "HOL clause: Abs"
blanchet@37577
   445
blanchet@40145
   446
fun predicate_of thy ((@{const Not} $ P), pos) = predicate_of thy (P, not pos)
blanchet@40145
   447
  | predicate_of thy (t, pos) =
blanchet@40145
   448
    (combterm_from_term thy [] (Envir.eta_contract t), pos)
blanchet@37577
   449
blanchet@40145
   450
fun literals_of_term1 args thy (@{const Trueprop} $ P) =
blanchet@40145
   451
    literals_of_term1 args thy P
blanchet@40145
   452
  | literals_of_term1 args thy (@{const HOL.disj} $ P $ Q) =
blanchet@40145
   453
    literals_of_term1 (literals_of_term1 args thy P) thy Q
blanchet@40145
   454
  | literals_of_term1 (lits, ts) thy P =
blanchet@40145
   455
    let val ((pred, ts'), pol) = predicate_of thy (P, true) in
blanchet@37923
   456
      (FOLLiteral (pol, pred) :: lits, union (op =) ts ts')
blanchet@37577
   457
    end
blanchet@37577
   458
val literals_of_term = literals_of_term1 ([], [])
blanchet@37577
   459
blanchet@39896
   460
fun old_skolem_const_name i j num_T_args =
blanchet@39896
   461
  old_skolem_const_prefix ^ Long_Name.separator ^
blanchet@39499
   462
  (space_implode Long_Name.separator (map Int.toString [i, j, num_T_args]))
blanchet@37577
   463
blanchet@39886
   464
fun conceal_old_skolem_terms i old_skolems t =
blanchet@39953
   465
  if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
blanchet@37577
   466
    let
blanchet@39886
   467
      fun aux old_skolems
blanchet@39953
   468
             (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
blanchet@37577
   469
          let
blanchet@39886
   470
            val (old_skolems, s) =
blanchet@37577
   471
              if i = ~1 then
blanchet@39886
   472
                (old_skolems, @{const_name undefined})
blanchet@39886
   473
              else case AList.find (op aconv) old_skolems t of
blanchet@39886
   474
                s :: _ => (old_skolems, s)
blanchet@37577
   475
              | [] =>
blanchet@37577
   476
                let
blanchet@39896
   477
                  val s = old_skolem_const_name i (length old_skolems)
blanchet@39896
   478
                                                (length (Term.add_tvarsT T []))
blanchet@39886
   479
                in ((s, t) :: old_skolems, s) end
blanchet@39886
   480
          in (old_skolems, Const (s, T)) end
blanchet@39886
   481
        | aux old_skolems (t1 $ t2) =
blanchet@37577
   482
          let
blanchet@39886
   483
            val (old_skolems, t1) = aux old_skolems t1
blanchet@39886
   484
            val (old_skolems, t2) = aux old_skolems t2
blanchet@39886
   485
          in (old_skolems, t1 $ t2) end
blanchet@39886
   486
        | aux old_skolems (Abs (s, T, t')) =
blanchet@39886
   487
          let val (old_skolems, t') = aux old_skolems t' in
blanchet@39886
   488
            (old_skolems, Abs (s, T, t'))
blanchet@37577
   489
          end
blanchet@39886
   490
        | aux old_skolems t = (old_skolems, t)
blanchet@39886
   491
    in aux old_skolems t end
blanchet@37577
   492
  else
blanchet@39886
   493
    (old_skolems, t)
blanchet@37577
   494
blanchet@39886
   495
fun reveal_old_skolem_terms old_skolems =
blanchet@37632
   496
  map_aterms (fn t as Const (s, _) =>
blanchet@39896
   497
                 if String.isPrefix old_skolem_const_prefix s then
blanchet@39886
   498
                   AList.lookup (op =) old_skolems s |> the
blanchet@37632
   499
                   |> map_types Type_Infer.paramify_vars
blanchet@37632
   500
                 else
blanchet@37632
   501
                   t
blanchet@37632
   502
               | t => t)
blanchet@37632
   503
blanchet@37577
   504
blanchet@37577
   505
(***************************************************************)
blanchet@37577
   506
(* Type Classes Present in the Axiom or Conjecture Clauses     *)
blanchet@37577
   507
(***************************************************************)
blanchet@37577
   508
blanchet@37577
   509
fun set_insert (x, s) = Symtab.update (x, ()) s
blanchet@37577
   510
blanchet@37577
   511
fun add_classes (sorts, cset) = List.foldl set_insert cset (flat sorts)
blanchet@37577
   512
blanchet@37577
   513
(*Remove this trivial type class*)
blanchet@37577
   514
fun delete_type cset = Symtab.delete_safe (the_single @{sort HOL.type}) cset;
blanchet@37577
   515
blanchet@37577
   516
fun tfree_classes_of_terms ts =
blanchet@37577
   517
  let val sorts_list = map (map #2 o OldTerm.term_tfrees) ts
blanchet@37577
   518
  in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
blanchet@37577
   519
blanchet@37577
   520
fun tvar_classes_of_terms ts =
blanchet@37577
   521
  let val sorts_list = map (map #2 o OldTerm.term_tvars) ts
blanchet@37577
   522
  in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
blanchet@37577
   523
blanchet@37577
   524
(*fold type constructors*)
blanchet@37577
   525
fun fold_type_consts f (Type (a, Ts)) x = fold (fold_type_consts f) Ts (f (a,x))
blanchet@37577
   526
  | fold_type_consts _ _ x = x;
blanchet@37577
   527
blanchet@37577
   528
(*Type constructors used to instantiate overloaded constants are the only ones needed.*)
blanchet@37577
   529
fun add_type_consts_in_term thy =
blanchet@37577
   530
  let
blanchet@38748
   531
    fun aux (Const x) =
blanchet@38748
   532
        fold (fold_type_consts set_insert) (Sign.const_typargs thy x)
blanchet@37577
   533
      | aux (Abs (_, _, u)) = aux u
blanchet@39953
   534
      | aux (Const (@{const_name Meson.skolem}, _) $ _) = I
blanchet@37577
   535
      | aux (t $ u) = aux t #> aux u
blanchet@37577
   536
      | aux _ = I
blanchet@37577
   537
  in aux end
blanchet@37577
   538
blanchet@37577
   539
fun type_consts_of_terms thy ts =
blanchet@37577
   540
  Symtab.keys (fold (add_type_consts_in_term thy) ts Symtab.empty);
blanchet@37577
   541
blanchet@39497
   542
(* ------------------------------------------------------------------------- *)
blanchet@39497
   543
(* HOL to FOL  (Isabelle to Metis)                                           *)
blanchet@39497
   544
(* ------------------------------------------------------------------------- *)
blanchet@39497
   545
blanchet@39497
   546
datatype mode = FO | HO | FT  (* first-order, higher-order, fully-typed *)
blanchet@39497
   547
blanchet@39497
   548
fun string_of_mode FO = "FO"
blanchet@39497
   549
  | string_of_mode HO = "HO"
blanchet@39497
   550
  | string_of_mode FT = "FT"
blanchet@39497
   551
blanchet@39497
   552
fun fn_isa_to_met_sublevel "equal" = "=" (* FIXME: "c_fequal" *)
blanchet@39497
   553
  | fn_isa_to_met_sublevel x = x
blanchet@39497
   554
fun fn_isa_to_met_toplevel "equal" = "="
blanchet@39497
   555
  | fn_isa_to_met_toplevel x = x
blanchet@39497
   556
blanchet@39497
   557
fun metis_lit b c args = (b, (c, args));
blanchet@39497
   558
blanchet@39497
   559
fun metis_term_from_combtyp (CombTVar (s, _)) = Metis_Term.Var s
blanchet@39497
   560
  | metis_term_from_combtyp (CombTFree (s, _)) = Metis_Term.Fn (s, [])
blanchet@39497
   561
  | metis_term_from_combtyp (CombType ((s, _), tps)) =
blanchet@39497
   562
    Metis_Term.Fn (s, map metis_term_from_combtyp tps);
blanchet@39497
   563
blanchet@39497
   564
(*These two functions insert type literals before the real literals. That is the
blanchet@39497
   565
  opposite order from TPTP linkup, but maybe OK.*)
blanchet@39497
   566
blanchet@39497
   567
fun hol_term_to_fol_FO tm =
blanchet@39497
   568
  case strip_combterm_comb tm of
blanchet@39497
   569
      (CombConst ((c, _), _, tys), tms) =>
blanchet@39497
   570
        let val tyargs = map metis_term_from_combtyp tys
blanchet@39497
   571
            val args   = map hol_term_to_fol_FO tms
blanchet@39497
   572
        in Metis_Term.Fn (c, tyargs @ args) end
blanchet@39497
   573
    | (CombVar ((v, _), _), []) => Metis_Term.Var v
blanchet@39497
   574
    | _ => raise Fail "non-first-order combterm"
blanchet@39497
   575
blanchet@39497
   576
fun hol_term_to_fol_HO (CombConst ((a, _), _, tylist)) =
blanchet@39497
   577
      Metis_Term.Fn (fn_isa_to_met_sublevel a, map metis_term_from_combtyp tylist)
blanchet@39497
   578
  | hol_term_to_fol_HO (CombVar ((s, _), _)) = Metis_Term.Var s
blanchet@39497
   579
  | hol_term_to_fol_HO (CombApp (tm1, tm2)) =
blanchet@39497
   580
       Metis_Term.Fn (".", map hol_term_to_fol_HO [tm1, tm2]);
blanchet@39497
   581
blanchet@39497
   582
(*The fully-typed translation, to avoid type errors*)
blanchet@39720
   583
fun wrap_type (tm, ty) =
blanchet@39720
   584
  Metis_Term.Fn (type_wrapper_name, [tm, metis_term_from_combtyp ty])
blanchet@39497
   585
blanchet@39497
   586
fun hol_term_to_fol_FT (CombVar ((s, _), ty)) = wrap_type (Metis_Term.Var s, ty)
blanchet@39497
   587
  | hol_term_to_fol_FT (CombConst((a, _), ty, _)) =
blanchet@39497
   588
      wrap_type (Metis_Term.Fn(fn_isa_to_met_sublevel a, []), ty)
blanchet@39497
   589
  | hol_term_to_fol_FT (tm as CombApp(tm1,tm2)) =
blanchet@39497
   590
       wrap_type (Metis_Term.Fn(".", map hol_term_to_fol_FT [tm1,tm2]),
blanchet@39497
   591
                  combtyp_of tm)
blanchet@39497
   592
blanchet@39497
   593
fun hol_literal_to_fol FO (FOLLiteral (pos, tm)) =
blanchet@39497
   594
      let val (CombConst((p, _), _, tys), tms) = strip_combterm_comb tm
blanchet@39497
   595
          val tylits = if p = "equal" then [] else map metis_term_from_combtyp tys
blanchet@39497
   596
          val lits = map hol_term_to_fol_FO tms
blanchet@39497
   597
      in metis_lit pos (fn_isa_to_met_toplevel p) (tylits @ lits) end
blanchet@39497
   598
  | hol_literal_to_fol HO (FOLLiteral (pos, tm)) =
blanchet@39497
   599
     (case strip_combterm_comb tm of
blanchet@39497
   600
          (CombConst(("equal", _), _, _), tms) =>
blanchet@39497
   601
            metis_lit pos "=" (map hol_term_to_fol_HO tms)
blanchet@39497
   602
        | _ => metis_lit pos "{}" [hol_term_to_fol_HO tm])   (*hBOOL*)
blanchet@39497
   603
  | hol_literal_to_fol FT (FOLLiteral (pos, tm)) =
blanchet@39497
   604
     (case strip_combterm_comb tm of
blanchet@39497
   605
          (CombConst(("equal", _), _, _), tms) =>
blanchet@39497
   606
            metis_lit pos "=" (map hol_term_to_fol_FT tms)
blanchet@39497
   607
        | _ => metis_lit pos "{}" [hol_term_to_fol_FT tm])   (*hBOOL*);
blanchet@39497
   608
blanchet@40145
   609
fun literals_of_hol_term thy mode t =
blanchet@40145
   610
  let val (lits, types_sorts) = literals_of_term thy t in
blanchet@40145
   611
    (map (hol_literal_to_fol mode) lits, types_sorts)
blanchet@40145
   612
  end
blanchet@39497
   613
blanchet@39497
   614
(*Sign should be "true" for conjecture type constraints, "false" for type lits in clauses.*)
blanchet@39497
   615
fun metis_of_type_literals pos (TyLitVar ((s, _), (s', _))) =
blanchet@39497
   616
    metis_lit pos s [Metis_Term.Var s']
blanchet@39497
   617
  | metis_of_type_literals pos (TyLitFree ((s, _), (s', _))) =
blanchet@39497
   618
    metis_lit pos s [Metis_Term.Fn (s',[])]
blanchet@39497
   619
blanchet@39497
   620
fun default_sort _ (TVar _) = false
blanchet@39497
   621
  | default_sort ctxt (TFree (x, s)) = (s = the_default [] (Variable.def_sort ctxt (x, ~1)));
blanchet@39497
   622
blanchet@39497
   623
fun metis_of_tfree tf =
blanchet@39497
   624
  Metis_Thm.axiom (Metis_LiteralSet.singleton (metis_of_type_literals true tf));
blanchet@39497
   625
blanchet@40145
   626
fun hol_thm_to_fol is_conjecture ctxt type_lits mode j old_skolems th =
blanchet@39497
   627
  let
blanchet@39497
   628
    val thy = ProofContext.theory_of ctxt
blanchet@39886
   629
    val (old_skolems, (mlits, types_sorts)) =
blanchet@39888
   630
     th |> prop_of |> Logic.strip_imp_concl
blanchet@39888
   631
        |> conceal_old_skolem_terms j old_skolems
blanchet@40145
   632
        ||> (HOLogic.dest_Trueprop #> literals_of_hol_term thy mode)
blanchet@39497
   633
  in
blanchet@39497
   634
    if is_conjecture then
blanchet@39497
   635
      (Metis_Thm.axiom (Metis_LiteralSet.fromList mlits),
blanchet@39886
   636
       type_literals_for_types types_sorts, old_skolems)
blanchet@39497
   637
    else
blanchet@39497
   638
      let
blanchet@39497
   639
        val tylits = filter_out (default_sort ctxt) types_sorts
blanchet@39497
   640
                     |> type_literals_for_types
blanchet@39497
   641
        val mtylits =
blanchet@39497
   642
          if type_lits then map (metis_of_type_literals false) tylits else []
blanchet@39497
   643
      in
blanchet@39497
   644
        (Metis_Thm.axiom (Metis_LiteralSet.fromList(mtylits @ mlits)), [],
blanchet@39886
   645
         old_skolems)
blanchet@39497
   646
      end
blanchet@39497
   647
  end;
blanchet@39497
   648
blanchet@39497
   649
val helpers =
blanchet@39953
   650
  [("c_COMBI", (false, map (`I) @{thms Meson.COMBI_def})),
blanchet@39953
   651
   ("c_COMBK", (false, map (`I) @{thms Meson.COMBK_def})),
blanchet@39953
   652
   ("c_COMBB", (false, map (`I) @{thms Meson.COMBB_def})),
blanchet@39953
   653
   ("c_COMBC", (false, map (`I) @{thms Meson.COMBC_def})),
blanchet@39953
   654
   ("c_COMBS", (false, map (`I) @{thms Meson.COMBS_def})),
blanchet@39497
   655
   ("c_fequal", (false, map (rpair @{thm equal_imp_equal})
blanchet@39497
   656
                            @{thms fequal_imp_equal equal_imp_fequal})),
blanchet@39497
   657
   ("c_True", (true, map (`I) @{thms True_or_False})),
blanchet@39497
   658
   ("c_False", (true, map (`I) @{thms True_or_False})),
blanchet@39497
   659
   ("c_If", (true, map (`I) @{thms if_True if_False True_or_False}))]
blanchet@39497
   660
blanchet@39497
   661
(* ------------------------------------------------------------------------- *)
blanchet@39497
   662
(* Logic maps manage the interface between HOL and first-order logic.        *)
blanchet@39497
   663
(* ------------------------------------------------------------------------- *)
blanchet@39497
   664
blanchet@39497
   665
type logic_map =
blanchet@39497
   666
  {axioms: (Metis_Thm.thm * thm) list,
blanchet@39497
   667
   tfrees: type_literal list,
blanchet@39886
   668
   old_skolems: (string * term) list}
blanchet@39497
   669
blanchet@39497
   670
fun is_quasi_fol_clause thy =
blanchet@39886
   671
  Meson.is_fol_term thy o snd o conceal_old_skolem_terms ~1 [] o prop_of
blanchet@39497
   672
blanchet@39497
   673
(*Extract TFree constraints from context to include as conjecture clauses*)
blanchet@39497
   674
fun init_tfrees ctxt =
blanchet@39497
   675
  let fun add ((a,i),s) Ts = if i = ~1 then TFree(a,s) :: Ts else Ts in
blanchet@39497
   676
    Vartab.fold add (#2 (Variable.constraints_of ctxt)) []
blanchet@39497
   677
    |> type_literals_for_types
blanchet@39497
   678
  end;
blanchet@39497
   679
blanchet@39497
   680
(*Insert non-logical axioms corresponding to all accumulated TFrees*)
blanchet@39886
   681
fun add_tfrees {axioms, tfrees, old_skolems} : logic_map =
blanchet@39497
   682
     {axioms = map (rpair TrueI o metis_of_tfree) (distinct (op =) tfrees) @
blanchet@39497
   683
               axioms,
blanchet@39886
   684
      tfrees = tfrees, old_skolems = old_skolems}
blanchet@39497
   685
blanchet@39497
   686
(*transform isabelle type / arity clause to metis clause *)
blanchet@39497
   687
fun add_type_thm [] lmap = lmap
blanchet@39886
   688
  | add_type_thm ((ith, mth) :: cls) {axioms, tfrees, old_skolems} =
blanchet@39497
   689
      add_type_thm cls {axioms = (mth, ith) :: axioms, tfrees = tfrees,
blanchet@39886
   690
                        old_skolems = old_skolems}
blanchet@39497
   691
blanchet@39497
   692
fun const_in_metis c (pred, tm_list) =
blanchet@39497
   693
  let
blanchet@39497
   694
    fun in_mterm (Metis_Term.Var _) = false
blanchet@39497
   695
      | in_mterm (Metis_Term.Fn (".", tm_list)) = exists in_mterm tm_list
blanchet@39497
   696
      | in_mterm (Metis_Term.Fn (nm, tm_list)) = c=nm orelse exists in_mterm tm_list
blanchet@39497
   697
  in  c = pred orelse exists in_mterm tm_list  end;
blanchet@39497
   698
blanchet@39497
   699
(* ARITY CLAUSE *)
blanchet@39497
   700
fun m_arity_cls (TConsLit ((c, _), (t, _), args)) =
blanchet@39497
   701
    metis_lit true c [Metis_Term.Fn(t, map (Metis_Term.Var o fst) args)]
blanchet@39497
   702
  | m_arity_cls (TVarLit ((c, _), (s, _))) =
blanchet@39497
   703
    metis_lit false c [Metis_Term.Var s]
blanchet@39497
   704
(*TrueI is returned as the Isabelle counterpart because there isn't any.*)
blanchet@39497
   705
fun arity_cls (ArityClause {conclLit, premLits, ...}) =
blanchet@39497
   706
  (TrueI,
blanchet@39497
   707
   Metis_Thm.axiom (Metis_LiteralSet.fromList (map m_arity_cls (conclLit :: premLits))));
blanchet@39497
   708
blanchet@39497
   709
(* CLASSREL CLAUSE *)
blanchet@39497
   710
fun m_class_rel_cls (subclass, _) (superclass, _) =
blanchet@39497
   711
  [metis_lit false subclass [Metis_Term.Var "T"], metis_lit true superclass [Metis_Term.Var "T"]];
blanchet@39497
   712
fun class_rel_cls (ClassRelClause {subclass, superclass, ...}) =
blanchet@39497
   713
  (TrueI, Metis_Thm.axiom (Metis_LiteralSet.fromList (m_class_rel_cls subclass superclass)));
blanchet@39497
   714
blanchet@39497
   715
fun type_ext thy tms =
blanchet@39497
   716
  let val subs = tfree_classes_of_terms tms
blanchet@39497
   717
      val supers = tvar_classes_of_terms tms
blanchet@39497
   718
      and tycons = type_consts_of_terms thy tms
blanchet@39497
   719
      val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@39497
   720
      val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@39497
   721
  in  map class_rel_cls class_rel_clauses @ map arity_cls arity_clauses
blanchet@39497
   722
  end;
blanchet@39497
   723
blanchet@39497
   724
(* Function to generate metis clauses, including comb and type clauses *)
blanchet@39886
   725
fun build_logic_map mode0 ctxt type_lits cls thss =
blanchet@39497
   726
  let val thy = ProofContext.theory_of ctxt
blanchet@39497
   727
      (*The modes FO and FT are sticky. HO can be downgraded to FO.*)
blanchet@39497
   728
      fun set_mode FO = FO
blanchet@39497
   729
        | set_mode HO =
blanchet@39886
   730
          if forall (forall (is_quasi_fol_clause thy)) (cls :: thss) then FO
blanchet@39886
   731
          else HO
blanchet@39497
   732
        | set_mode FT = FT
blanchet@39497
   733
      val mode = set_mode mode0
blanchet@39497
   734
      (*transform isabelle clause to metis clause *)
blanchet@40145
   735
      fun add_thm is_conjecture (metis_ith, isa_ith)
blanchet@39886
   736
                  {axioms, tfrees, old_skolems} : logic_map =
blanchet@39497
   737
        let
blanchet@39886
   738
          val (mth, tfree_lits, old_skolems) =
blanchet@40145
   739
            hol_thm_to_fol is_conjecture ctxt type_lits mode (length axioms)
blanchet@39886
   740
                           old_skolems metis_ith
blanchet@39497
   741
        in
blanchet@39497
   742
           {axioms = (mth, Meson.make_meta_clause isa_ith) :: axioms,
blanchet@39886
   743
            tfrees = union (op =) tfree_lits tfrees, old_skolems = old_skolems}
blanchet@39497
   744
        end;
blanchet@39886
   745
      val lmap = {axioms = [], tfrees = init_tfrees ctxt, old_skolems = []}
blanchet@40145
   746
                 |> fold (add_thm true o `I) cls
blanchet@39497
   747
                 |> add_tfrees
blanchet@40145
   748
                 |> fold (fold (add_thm false o `I)) thss
blanchet@39497
   749
      val clause_lists = map (Metis_Thm.clause o #1) (#axioms lmap)
blanchet@39497
   750
      fun is_used c =
blanchet@39497
   751
        exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
blanchet@39497
   752
      val lmap =
blanchet@39497
   753
        if mode = FO then
blanchet@39497
   754
          lmap
blanchet@39497
   755
        else
blanchet@39497
   756
          let
blanchet@39497
   757
            val helper_ths =
blanchet@39497
   758
              helpers |> filter (is_used o fst)
blanchet@39497
   759
                      |> maps (fn (c, (needs_full_types, thms)) =>
blanchet@39497
   760
                                  if not (is_used c) orelse
blanchet@39497
   761
                                     needs_full_types andalso mode <> FT then
blanchet@39497
   762
                                    []
blanchet@39497
   763
                                  else
blanchet@39497
   764
                                    thms)
blanchet@40145
   765
          in lmap |> fold (add_thm false) helper_ths end
blanchet@39886
   766
  in
blanchet@39886
   767
    (mode, add_type_thm (type_ext thy (maps (map prop_of) (cls :: thss))) lmap)
blanchet@39886
   768
  end
blanchet@39497
   769
paulson@15347
   770
end;