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