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