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