src/HOL/Tools/Sledgehammer/metis_translate.ML
author blanchet
Wed Sep 29 23:24:31 2010 +0200 (2010-09-29)
changeset 39888 40ef95149770
parent 39887 74939e2afb95
child 39890 a1695e2169d0
permissions -rw-r--r--
ignore Skolem assumption (if any)
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@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@39886
    48
  val new_skolem_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@39886
    71
    theory -> int -> (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@39886
    98
val skolem_prefix = "Sledgehammer" ^ Long_Name.separator ^ "Sko"
blanchet@39886
    99
val old_skolem_prefix = skolem_prefix ^ "o"
blanchet@39886
   100
val new_skolem_prefix = skolem_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@36493
   114
               (@{const_name fequal}, "fequal"),
blanchet@36493
   115
               (@{const_name COMBI}, "COMBI"),
blanchet@36493
   116
               (@{const_name COMBK}, "COMBK"),
blanchet@36493
   117
               (@{const_name COMBB}, "COMBB"),
blanchet@36493
   118
               (@{const_name COMBC}, "COMBC"),
blanchet@37479
   119
               (@{const_name 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@39499
   212
  if String.isPrefix skolem_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@39886
   218
  let
blanchet@39886
   219
    val ss = s |> space_explode Long_Name.separator
blanchet@39886
   220
    val n = length ss
blanchet@39886
   221
  in (nth ss (n - 2), nth ss (n - 3) |> Int.fromString |> the) end
blanchet@39886
   222
blanchet@39886
   223
blanchet@37624
   224
(**** Definitions and functions for FOL clauses for TPTP format output ****)
blanchet@36169
   225
blanchet@37624
   226
type name = string * string
paulson@15347
   227
paulson@15347
   228
(**** Isabelle FOL clauses ****)
paulson@15347
   229
blanchet@36556
   230
(* The first component is the type class; the second is a TVar or TFree. *)
blanchet@36556
   231
datatype type_literal =
blanchet@37643
   232
  TyLitVar of name * name |
blanchet@37643
   233
  TyLitFree of name * name
paulson@15347
   234
wenzelm@24310
   235
(*Make literals for sorted type variables*)
paulson@24940
   236
fun sorts_on_typs_aux (_, [])   = []
paulson@24940
   237
  | sorts_on_typs_aux ((x,i),  s::ss) =
paulson@24940
   238
      let val sorts = sorts_on_typs_aux ((x,i), ss)
paulson@22643
   239
      in
paulson@22643
   240
          if s = "HOL.type" then sorts
blanchet@37643
   241
          else if i = ~1 then TyLitFree (`make_type_class s, `make_fixed_type_var x) :: sorts
blanchet@37643
   242
          else TyLitVar (`make_type_class s, (make_schematic_type_var (x,i), x)) :: sorts
paulson@22643
   243
      end;
quigley@17150
   244
paulson@24940
   245
fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
paulson@24940
   246
  | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
paulson@24940
   247
paulson@24937
   248
(*Given a list of sorted type variables, return a list of type literals.*)
blanchet@36966
   249
fun type_literals_for_types Ts =
blanchet@36966
   250
  fold (union (op =)) (map sorts_on_typs Ts) []
mengj@20015
   251
mengj@20015
   252
(** make axiom and conjecture clauses. **)
mengj@20015
   253
paulson@15347
   254
(**** Isabelle arities ****)
paulson@15347
   255
blanchet@37643
   256
datatype arLit =
blanchet@37643
   257
  TConsLit of name * name * name list |
blanchet@37643
   258
  TVarLit of name * name
wenzelm@24310
   259
blanchet@35865
   260
datatype arity_clause =
blanchet@38105
   261
  ArityClause of {name: string, conclLit: arLit, premLits: arLit list}
paulson@15347
   262
paulson@15347
   263
paulson@18798
   264
fun gen_TVars 0 = []
paulson@18798
   265
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   266
paulson@18411
   267
fun pack_sort(_,[])  = []
blanchet@37643
   268
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort (tvar, srt)   (*IGNORE sort "type"*)
blanchet@37643
   269
  | pack_sort(tvar, cls::srt) =
blanchet@37643
   270
    (`make_type_class cls, (tvar, tvar)) :: pack_sort (tvar, srt)
wenzelm@24310
   271
paulson@18411
   272
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
blanchet@38105
   273
fun make_axiom_arity_clause (tcons, name, (cls,args)) =
blanchet@37643
   274
  let
blanchet@37643
   275
    val tvars = gen_TVars (length args)
blanchet@37643
   276
    val tvars_srts = ListPair.zip (tvars, args)
blanchet@37643
   277
  in
blanchet@39318
   278
    ArityClause {name = name,
blanchet@37643
   279
                 conclLit = TConsLit (`make_type_class cls,
blanchet@37643
   280
                                      `make_fixed_type_const tcons,
blanchet@37643
   281
                                      tvars ~~ tvars),
blanchet@37643
   282
                 premLits = map TVarLit (union_all (map pack_sort tvars_srts))}
blanchet@37643
   283
  end
paulson@15347
   284
paulson@15347
   285
paulson@15347
   286
(**** Isabelle class relations ****)
paulson@15347
   287
blanchet@37925
   288
datatype class_rel_clause =
blanchet@38105
   289
  ClassRelClause of {name: string, subclass: name, superclass: name}
wenzelm@24310
   290
paulson@21290
   291
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
blanchet@37498
   292
fun class_pairs _ [] _ = []
paulson@21432
   293
  | class_pairs thy subs supers =
blanchet@36218
   294
      let
blanchet@36218
   295
        val class_less = Sorts.class_less (Sign.classes_of thy)
blanchet@36218
   296
        fun add_super sub super = class_less (sub, super) ? cons (sub, super)
blanchet@36218
   297
        fun add_supers sub = fold (add_super sub) supers
blanchet@36218
   298
      in fold add_supers subs [] end
paulson@15347
   299
blanchet@37925
   300
fun make_class_rel_clause (sub,super) =
blanchet@38105
   301
  ClassRelClause {name = sub ^ "_" ^ super,
blanchet@37643
   302
                  subclass = `make_type_class sub,
blanchet@38105
   303
                  superclass = `make_type_class super}
paulson@15347
   304
blanchet@37925
   305
fun make_class_rel_clauses thy subs supers =
blanchet@37925
   306
  map make_class_rel_clause (class_pairs thy subs supers);
paulson@18868
   307
paulson@18868
   308
paulson@18868
   309
(** Isabelle arities **)
paulson@17845
   310
blanchet@37498
   311
fun arity_clause _ _ (_, []) = []
blanchet@37498
   312
  | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
blanchet@37498
   313
      arity_clause seen n (tcons,ars)
blanchet@37498
   314
  | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
haftmann@36692
   315
      if member (op =) seen class then (*multiple arities for the same tycon, class pair*)
blanchet@37572
   316
          make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
blanchet@37498
   317
          arity_clause seen (n+1) (tcons,ars)
paulson@21373
   318
      else
blanchet@37572
   319
          make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class, ar) ::
blanchet@37498
   320
          arity_clause (class::seen) n (tcons,ars)
paulson@17845
   321
blanchet@37498
   322
fun multi_arity_clause [] = []
blanchet@37498
   323
  | multi_arity_clause ((tcons, ars) :: tc_arlists) =
blanchet@37498
   324
      arity_clause [] 1 (tcons, ars) @ multi_arity_clause tc_arlists
paulson@17845
   325
paulson@22643
   326
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   327
  provided its arguments have the corresponding sorts.*)
paulson@21373
   328
fun type_class_pairs thy tycons classes =
paulson@21373
   329
  let val alg = Sign.classes_of thy
blanchet@36218
   330
      fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
blanchet@36218
   331
      fun add_class tycon class =
blanchet@36218
   332
        cons (class, domain_sorts tycon class)
blanchet@36218
   333
        handle Sorts.CLASS_ERROR _ => I
blanchet@36218
   334
      fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
paulson@21373
   335
  in  map try_classes tycons  end;
paulson@21373
   336
paulson@22643
   337
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
blanchet@37498
   338
fun iter_type_class_pairs _ _ [] = ([], [])
paulson@22643
   339
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   340
      let val cpairs = type_class_pairs thy tycons classes
haftmann@33040
   341
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
haftmann@33040
   342
            |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
wenzelm@24310
   343
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
haftmann@33042
   344
      in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
wenzelm@24310
   345
blanchet@37498
   346
fun make_arity_clauses thy tycons classes =
wenzelm@24310
   347
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
blanchet@37498
   348
  in  (classes', multi_arity_clause cpairs)  end;
paulson@18863
   349
blanchet@37577
   350
datatype combtyp =
blanchet@37924
   351
  CombTVar of name |
blanchet@37924
   352
  CombTFree of name |
blanchet@37924
   353
  CombType of name * combtyp list
blanchet@37577
   354
blanchet@37577
   355
datatype combterm =
blanchet@37577
   356
  CombConst of name * combtyp * combtyp list (* Const and Free *) |
blanchet@37577
   357
  CombVar of name * combtyp |
blanchet@37577
   358
  CombApp of combterm * combterm
blanchet@37577
   359
blanchet@37923
   360
datatype fol_literal = FOLLiteral of bool * combterm
blanchet@37577
   361
blanchet@37577
   362
(*********************************************************************)
blanchet@37577
   363
(* convert a clause with type Term.term to a clause with type clause *)
blanchet@37577
   364
(*********************************************************************)
blanchet@37577
   365
blanchet@37577
   366
(*Result of a function type; no need to check that the argument type matches.*)
blanchet@37924
   367
fun result_type (CombType (_, [_, tp2])) = tp2
blanchet@37577
   368
  | result_type _ = raise Fail "non-function type"
blanchet@37577
   369
blanchet@37994
   370
fun combtyp_of (CombConst (_, tp, _)) = tp
blanchet@37994
   371
  | combtyp_of (CombVar (_, tp)) = tp
blanchet@37994
   372
  | combtyp_of (CombApp (t1, _)) = result_type (combtyp_of t1)
blanchet@37577
   373
blanchet@37577
   374
(*gets the head of a combinator application, along with the list of arguments*)
blanchet@37577
   375
fun strip_combterm_comb u =
blanchet@37577
   376
    let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
blanchet@37577
   377
        |   stripc  x =  x
blanchet@37577
   378
    in stripc(u,[]) end
blanchet@37577
   379
blanchet@39497
   380
fun combtype_of (Type (a, Ts)) =
blanchet@39497
   381
    let val (folTypes, ts) = combtypes_of Ts in
blanchet@37924
   382
      (CombType (`make_fixed_type_const a, folTypes), ts)
blanchet@37577
   383
    end
blanchet@39497
   384
  | combtype_of (tp as TFree (a, _)) = (CombTFree (`make_fixed_type_var a), [tp])
blanchet@39497
   385
  | combtype_of (tp as TVar (x, _)) =
blanchet@37924
   386
    (CombTVar (make_schematic_type_var x, string_of_indexname x), [tp])
blanchet@39497
   387
and combtypes_of Ts =
blanchet@39497
   388
  let val (folTyps, ts) = ListPair.unzip (map combtype_of Ts) in
blanchet@39497
   389
    (folTyps, union_all ts)
blanchet@39497
   390
  end
blanchet@37577
   391
blanchet@37577
   392
(* same as above, but no gathering of sort information *)
blanchet@39497
   393
fun simple_combtype_of (Type (a, Ts)) =
blanchet@39497
   394
    CombType (`make_fixed_type_const a, map simple_combtype_of Ts)
blanchet@39497
   395
  | simple_combtype_of (TFree (a, _)) = CombTFree (`make_fixed_type_var a)
blanchet@39497
   396
  | simple_combtype_of (TVar (x, _)) =
blanchet@37924
   397
    CombTVar (make_schematic_type_var x, string_of_indexname x)
blanchet@37577
   398
blanchet@39886
   399
fun new_skolem_name th_no s num_T_args =
blanchet@39886
   400
  [new_skolem_prefix, string_of_int th_no, s, string_of_int num_T_args]
blanchet@39886
   401
  |> space_implode Long_Name.separator
blanchet@39886
   402
blanchet@37995
   403
(* Converts a term (with combinators) into a combterm. Also accummulates sort
blanchet@37995
   404
   infomation. *)
blanchet@39886
   405
fun combterm_from_term thy th_no bs (P $ Q) =
blanchet@39886
   406
      let val (P', tsP) = combterm_from_term thy th_no bs P
blanchet@39886
   407
          val (Q', tsQ) = combterm_from_term thy th_no bs Q
blanchet@37995
   408
      in  (CombApp (P', Q'), union (op =) tsP tsQ)  end
blanchet@39886
   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@39886
   413
          (if String.isPrefix old_skolem_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@39886
   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@39886
   424
  | combterm_from_term _ th_no _ (Var (v as (s, _), T)) =
blanchet@39886
   425
    let
blanchet@39886
   426
      val (tp, ts) = combtype_of T
blanchet@39886
   427
      val v' =
blanchet@39887
   428
        if String.isPrefix Meson_Clausifier.new_skolem_var_prefix s then
blanchet@39886
   429
          let
blanchet@39886
   430
            val tys = T |> strip_type |> swap |> op ::
blanchet@39886
   431
            val s' = new_skolem_name th_no 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@39886
   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@39886
   444
  | combterm_from_term _ _ _ (Abs _) = raise Fail "HOL clause: Abs"
blanchet@37577
   445
blanchet@39886
   446
fun predicate_of thy th_no ((@{const Not} $ P), pos) =
blanchet@39886
   447
    predicate_of thy th_no (P, not pos)
blanchet@39886
   448
  | predicate_of thy th_no (t, pos) =
blanchet@39886
   449
    (combterm_from_term thy th_no [] (Envir.eta_contract t), pos)
blanchet@37577
   450
blanchet@39886
   451
fun literals_of_term1 args thy th_no (@{const Trueprop} $ P) =
blanchet@39886
   452
    literals_of_term1 args thy th_no P
blanchet@39886
   453
  | literals_of_term1 args thy th_no (@{const HOL.disj} $ P $ Q) =
blanchet@39886
   454
    literals_of_term1 (literals_of_term1 args thy th_no P) thy th_no Q
blanchet@39886
   455
  | literals_of_term1 (lits, ts) thy th_no P =
blanchet@39886
   456
    let val ((pred, ts'), pol) = predicate_of thy th_no (P, true) in
blanchet@37923
   457
      (FOLLiteral (pol, pred) :: lits, union (op =) ts ts')
blanchet@37577
   458
    end
blanchet@37577
   459
val literals_of_term = literals_of_term1 ([], [])
blanchet@37577
   460
blanchet@39886
   461
fun old_skolem_name i j num_T_args =
blanchet@39886
   462
  old_skolem_prefix ^ Long_Name.separator ^
blanchet@39499
   463
  (space_implode Long_Name.separator (map Int.toString [i, j, num_T_args]))
blanchet@37577
   464
blanchet@39886
   465
fun conceal_old_skolem_terms i old_skolems t =
blanchet@39355
   466
  if exists_Const (curry (op =) @{const_name skolem} o fst) t then
blanchet@37577
   467
    let
blanchet@39886
   468
      fun aux old_skolems
blanchet@39355
   469
              (t as (Const (@{const_name skolem}, Type (_, [_, T])) $ _)) =
blanchet@37577
   470
          let
blanchet@39886
   471
            val (old_skolems, s) =
blanchet@37577
   472
              if i = ~1 then
blanchet@39886
   473
                (old_skolems, @{const_name undefined})
blanchet@39886
   474
              else case AList.find (op aconv) old_skolems t of
blanchet@39886
   475
                s :: _ => (old_skolems, s)
blanchet@37577
   476
              | [] =>
blanchet@37577
   477
                let
blanchet@39886
   478
                  val s = old_skolem_name i (length old_skolems)
blanchet@39886
   479
                                          (length (Term.add_tvarsT T []))
blanchet@39886
   480
                in ((s, t) :: old_skolems, s) end
blanchet@39886
   481
          in (old_skolems, Const (s, T)) end
blanchet@39886
   482
        | aux old_skolems (t1 $ t2) =
blanchet@37577
   483
          let
blanchet@39886
   484
            val (old_skolems, t1) = aux old_skolems t1
blanchet@39886
   485
            val (old_skolems, t2) = aux old_skolems t2
blanchet@39886
   486
          in (old_skolems, t1 $ t2) end
blanchet@39886
   487
        | aux old_skolems (Abs (s, T, t')) =
blanchet@39886
   488
          let val (old_skolems, t') = aux old_skolems t' in
blanchet@39886
   489
            (old_skolems, Abs (s, T, t'))
blanchet@37577
   490
          end
blanchet@39886
   491
        | aux old_skolems t = (old_skolems, t)
blanchet@39886
   492
    in aux old_skolems t end
blanchet@37577
   493
  else
blanchet@39886
   494
    (old_skolems, t)
blanchet@37577
   495
blanchet@39886
   496
fun reveal_old_skolem_terms old_skolems =
blanchet@37632
   497
  map_aterms (fn t as Const (s, _) =>
blanchet@39886
   498
                 if String.isPrefix old_skolem_prefix s then
blanchet@39886
   499
                   AList.lookup (op =) old_skolems s |> the
blanchet@37632
   500
                   |> map_types Type_Infer.paramify_vars
blanchet@37632
   501
                 else
blanchet@37632
   502
                   t
blanchet@37632
   503
               | t => t)
blanchet@37632
   504
blanchet@37577
   505
blanchet@37577
   506
(***************************************************************)
blanchet@37577
   507
(* Type Classes Present in the Axiom or Conjecture Clauses     *)
blanchet@37577
   508
(***************************************************************)
blanchet@37577
   509
blanchet@37577
   510
fun set_insert (x, s) = Symtab.update (x, ()) s
blanchet@37577
   511
blanchet@37577
   512
fun add_classes (sorts, cset) = List.foldl set_insert cset (flat sorts)
blanchet@37577
   513
blanchet@37577
   514
(*Remove this trivial type class*)
blanchet@37577
   515
fun delete_type cset = Symtab.delete_safe (the_single @{sort HOL.type}) cset;
blanchet@37577
   516
blanchet@37577
   517
fun tfree_classes_of_terms ts =
blanchet@37577
   518
  let val sorts_list = map (map #2 o OldTerm.term_tfrees) ts
blanchet@37577
   519
  in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
blanchet@37577
   520
blanchet@37577
   521
fun tvar_classes_of_terms ts =
blanchet@37577
   522
  let val sorts_list = map (map #2 o OldTerm.term_tvars) ts
blanchet@37577
   523
  in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
blanchet@37577
   524
blanchet@37577
   525
(*fold type constructors*)
blanchet@37577
   526
fun fold_type_consts f (Type (a, Ts)) x = fold (fold_type_consts f) Ts (f (a,x))
blanchet@37577
   527
  | fold_type_consts _ _ x = x;
blanchet@37577
   528
blanchet@37577
   529
(*Type constructors used to instantiate overloaded constants are the only ones needed.*)
blanchet@37577
   530
fun add_type_consts_in_term thy =
blanchet@37577
   531
  let
blanchet@38748
   532
    fun aux (Const x) =
blanchet@38748
   533
        fold (fold_type_consts set_insert) (Sign.const_typargs thy x)
blanchet@37577
   534
      | aux (Abs (_, _, u)) = aux u
blanchet@39355
   535
      | aux (Const (@{const_name skolem}, _) $ _) = I
blanchet@37577
   536
      | aux (t $ u) = aux t #> aux u
blanchet@37577
   537
      | aux _ = I
blanchet@37577
   538
  in aux end
blanchet@37577
   539
blanchet@37577
   540
fun type_consts_of_terms thy ts =
blanchet@37577
   541
  Symtab.keys (fold (add_type_consts_in_term thy) ts Symtab.empty);
blanchet@37577
   542
blanchet@39497
   543
(* ------------------------------------------------------------------------- *)
blanchet@39497
   544
(* HOL to FOL  (Isabelle to Metis)                                           *)
blanchet@39497
   545
(* ------------------------------------------------------------------------- *)
blanchet@39497
   546
blanchet@39497
   547
datatype mode = FO | HO | FT  (* first-order, higher-order, fully-typed *)
blanchet@39497
   548
blanchet@39497
   549
fun string_of_mode FO = "FO"
blanchet@39497
   550
  | string_of_mode HO = "HO"
blanchet@39497
   551
  | string_of_mode FT = "FT"
blanchet@39497
   552
blanchet@39497
   553
fun fn_isa_to_met_sublevel "equal" = "=" (* FIXME: "c_fequal" *)
blanchet@39497
   554
  | fn_isa_to_met_sublevel x = x
blanchet@39497
   555
fun fn_isa_to_met_toplevel "equal" = "="
blanchet@39497
   556
  | fn_isa_to_met_toplevel x = x
blanchet@39497
   557
blanchet@39497
   558
fun metis_lit b c args = (b, (c, args));
blanchet@39497
   559
blanchet@39497
   560
fun metis_term_from_combtyp (CombTVar (s, _)) = Metis_Term.Var s
blanchet@39497
   561
  | metis_term_from_combtyp (CombTFree (s, _)) = Metis_Term.Fn (s, [])
blanchet@39497
   562
  | metis_term_from_combtyp (CombType ((s, _), tps)) =
blanchet@39497
   563
    Metis_Term.Fn (s, map metis_term_from_combtyp tps);
blanchet@39497
   564
blanchet@39497
   565
(*These two functions insert type literals before the real literals. That is the
blanchet@39497
   566
  opposite order from TPTP linkup, but maybe OK.*)
blanchet@39497
   567
blanchet@39497
   568
fun hol_term_to_fol_FO tm =
blanchet@39497
   569
  case strip_combterm_comb tm of
blanchet@39497
   570
      (CombConst ((c, _), _, tys), tms) =>
blanchet@39497
   571
        let val tyargs = map metis_term_from_combtyp tys
blanchet@39497
   572
            val args   = map hol_term_to_fol_FO tms
blanchet@39497
   573
        in Metis_Term.Fn (c, tyargs @ args) end
blanchet@39497
   574
    | (CombVar ((v, _), _), []) => Metis_Term.Var v
blanchet@39497
   575
    | _ => raise Fail "non-first-order combterm"
blanchet@39497
   576
blanchet@39497
   577
fun hol_term_to_fol_HO (CombConst ((a, _), _, tylist)) =
blanchet@39497
   578
      Metis_Term.Fn (fn_isa_to_met_sublevel a, map metis_term_from_combtyp tylist)
blanchet@39497
   579
  | hol_term_to_fol_HO (CombVar ((s, _), _)) = Metis_Term.Var s
blanchet@39497
   580
  | hol_term_to_fol_HO (CombApp (tm1, tm2)) =
blanchet@39497
   581
       Metis_Term.Fn (".", map hol_term_to_fol_HO [tm1, tm2]);
blanchet@39497
   582
blanchet@39497
   583
(*The fully-typed translation, to avoid type errors*)
blanchet@39720
   584
fun wrap_type (tm, ty) =
blanchet@39720
   585
  Metis_Term.Fn (type_wrapper_name, [tm, metis_term_from_combtyp ty])
blanchet@39497
   586
blanchet@39497
   587
fun hol_term_to_fol_FT (CombVar ((s, _), ty)) = wrap_type (Metis_Term.Var s, ty)
blanchet@39497
   588
  | hol_term_to_fol_FT (CombConst((a, _), ty, _)) =
blanchet@39497
   589
      wrap_type (Metis_Term.Fn(fn_isa_to_met_sublevel a, []), ty)
blanchet@39497
   590
  | hol_term_to_fol_FT (tm as CombApp(tm1,tm2)) =
blanchet@39497
   591
       wrap_type (Metis_Term.Fn(".", map hol_term_to_fol_FT [tm1,tm2]),
blanchet@39497
   592
                  combtyp_of tm)
blanchet@39497
   593
blanchet@39497
   594
fun hol_literal_to_fol FO (FOLLiteral (pos, tm)) =
blanchet@39497
   595
      let val (CombConst((p, _), _, tys), tms) = strip_combterm_comb tm
blanchet@39497
   596
          val tylits = if p = "equal" then [] else map metis_term_from_combtyp tys
blanchet@39497
   597
          val lits = map hol_term_to_fol_FO tms
blanchet@39497
   598
      in metis_lit pos (fn_isa_to_met_toplevel p) (tylits @ lits) end
blanchet@39497
   599
  | hol_literal_to_fol HO (FOLLiteral (pos, tm)) =
blanchet@39497
   600
     (case strip_combterm_comb tm of
blanchet@39497
   601
          (CombConst(("equal", _), _, _), tms) =>
blanchet@39497
   602
            metis_lit pos "=" (map hol_term_to_fol_HO tms)
blanchet@39497
   603
        | _ => metis_lit pos "{}" [hol_term_to_fol_HO tm])   (*hBOOL*)
blanchet@39497
   604
  | hol_literal_to_fol FT (FOLLiteral (pos, tm)) =
blanchet@39497
   605
     (case strip_combterm_comb tm of
blanchet@39497
   606
          (CombConst(("equal", _), _, _), tms) =>
blanchet@39497
   607
            metis_lit pos "=" (map hol_term_to_fol_FT tms)
blanchet@39497
   608
        | _ => metis_lit pos "{}" [hol_term_to_fol_FT tm])   (*hBOOL*);
blanchet@39497
   609
blanchet@39886
   610
fun literals_of_hol_term thy th_no mode t =
blanchet@39886
   611
      let val (lits, types_sorts) = literals_of_term thy th_no t
blanchet@39497
   612
      in  (map (hol_literal_to_fol mode) lits, types_sorts) 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@39886
   626
fun hol_thm_to_fol is_conjecture th_no 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@39886
   632
        ||> (HOLogic.dest_Trueprop #> literals_of_hol_term thy th_no 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@39497
   650
  [("c_COMBI", (false, map (`I) @{thms COMBI_def})),
blanchet@39497
   651
   ("c_COMBK", (false, map (`I) @{thms COMBK_def})),
blanchet@39497
   652
   ("c_COMBB", (false, map (`I) @{thms COMBB_def})),
blanchet@39497
   653
   ("c_COMBC", (false, map (`I) @{thms COMBC_def})),
blanchet@39497
   654
   ("c_COMBS", (false, map (`I) @{thms 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@39886
   735
      fun add_thm th_no 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@39886
   739
            hol_thm_to_fol is_conjecture th_no 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@39886
   746
                 |> fold (add_thm 0 true o `I) cls
blanchet@39497
   747
                 |> add_tfrees
blanchet@39886
   748
                 |> fold (fn (th_no, ths) => fold (add_thm th_no false o `I) ths)
blanchet@39886
   749
                         (1 upto length thss ~~ thss)
blanchet@39497
   750
      val clause_lists = map (Metis_Thm.clause o #1) (#axioms lmap)
blanchet@39497
   751
      fun is_used c =
blanchet@39497
   752
        exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
blanchet@39497
   753
      val lmap =
blanchet@39497
   754
        if mode = FO then
blanchet@39497
   755
          lmap
blanchet@39497
   756
        else
blanchet@39497
   757
          let
blanchet@39497
   758
            val helper_ths =
blanchet@39497
   759
              helpers |> filter (is_used o fst)
blanchet@39497
   760
                      |> maps (fn (c, (needs_full_types, thms)) =>
blanchet@39497
   761
                                  if not (is_used c) orelse
blanchet@39497
   762
                                     needs_full_types andalso mode <> FT then
blanchet@39497
   763
                                    []
blanchet@39497
   764
                                  else
blanchet@39497
   765
                                    thms)
blanchet@39886
   766
          in lmap |> fold (add_thm ~1 false) helper_ths end
blanchet@39886
   767
  in
blanchet@39886
   768
    (mode, add_type_thm (type_ext thy (maps (map prop_of) (cls :: thss))) lmap)
blanchet@39886
   769
  end
blanchet@39497
   770
paulson@15347
   771
end;