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