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