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