src/HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
author blanchet
Sun May 01 18:37:25 2011 +0200 (2011-05-01)
changeset 42576 a8a80a2a34be
parent 42575 ad700c4f2471
child 42577 78414ec6fa4e
permissions -rw-r--r--
merge symbol declarations that are type-instances of each other -- useful for type system "Args true" with monomorphization turned off
blanchet@40114
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
blanchet@38282
     2
    Author:     Fabian Immler, TU Muenchen
blanchet@38282
     3
    Author:     Makarius
blanchet@38282
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@38282
     5
blanchet@39494
     6
Translation of HOL to FOL for Sledgehammer.
blanchet@38282
     7
*)
blanchet@38282
     8
blanchet@40068
     9
signature SLEDGEHAMMER_ATP_TRANSLATE =
blanchet@38282
    10
sig
blanchet@42227
    11
  type 'a fo_term = 'a ATP_Problem.fo_term
blanchet@38282
    12
  type 'a problem = 'a ATP_Problem.problem
blanchet@40114
    13
  type translated_formula
blanchet@38282
    14
blanchet@41134
    15
  datatype type_system =
blanchet@42523
    16
    Many_Typed |
blanchet@42548
    17
    Mangled of bool |
blanchet@42523
    18
    Args of bool |
blanchet@42548
    19
    Tags of bool |
blanchet@41134
    20
    No_Types
blanchet@41134
    21
blanchet@42568
    22
  val readable_names : bool Unsynchronized.ref
blanchet@40204
    23
  val fact_prefix : string
blanchet@38282
    24
  val conjecture_prefix : string
blanchet@42568
    25
  val predicator_base : string
blanchet@42544
    26
  val explicit_app_base : string
blanchet@42549
    27
  val type_pred_base : string
blanchet@42562
    28
  val tff_type_prefix : string
blanchet@42449
    29
  val is_type_system_sound : type_system -> bool
blanchet@42449
    30
  val type_system_types_dangerous_types : type_system -> bool
blanchet@41136
    31
  val num_atp_type_args : theory -> type_system -> string -> int
blanchet@42542
    32
  val unmangled_const : string -> string * string fo_term list
blanchet@41088
    33
  val translate_atp_fact :
blanchet@42544
    34
    Proof.context -> bool -> (string * 'a) * thm
blanchet@41091
    35
    -> translated_formula option * ((string * 'a) * thm)
blanchet@40059
    36
  val prepare_atp_problem :
blanchet@42568
    37
    Proof.context -> type_system -> bool -> term list -> term
blanchet@41091
    38
    -> (translated_formula option * ((string * 'a) * thm)) list
blanchet@42541
    39
    -> string problem * string Symtab.table * int * int
blanchet@42541
    40
       * (string * 'a) list vector
blanchet@41313
    41
  val atp_problem_weights : string problem -> (string * real) list
blanchet@38282
    42
end;
blanchet@38282
    43
blanchet@41140
    44
structure Sledgehammer_ATP_Translate : SLEDGEHAMMER_ATP_TRANSLATE =
blanchet@38282
    45
struct
blanchet@38282
    46
blanchet@38282
    47
open ATP_Problem
blanchet@39494
    48
open Metis_Translate
blanchet@38282
    49
open Sledgehammer_Util
blanchet@38282
    50
blanchet@42568
    51
(* Readable names are often much shorter, especially if types are mangled in
blanchet@42568
    52
   names. For that reason, they are on by default. *)
blanchet@42568
    53
val readable_names = Unsynchronized.ref true
blanchet@42568
    54
blanchet@42543
    55
val type_decl_prefix = "type_"
blanchet@42543
    56
val sym_decl_prefix = "sym_"
blanchet@40204
    57
val fact_prefix = "fact_"
blanchet@38282
    58
val conjecture_prefix = "conj_"
blanchet@38282
    59
val helper_prefix = "help_"
blanchet@42543
    60
val class_rel_clause_prefix = "crel_";
blanchet@38282
    61
val arity_clause_prefix = "arity_"
blanchet@39975
    62
val tfree_prefix = "tfree_"
blanchet@38282
    63
blanchet@42568
    64
val predicator_base = "hBOOL"
blanchet@42544
    65
val explicit_app_base = "hAPP"
blanchet@42542
    66
val type_pred_base = "is"
blanchet@42562
    67
val tff_type_prefix = "ty_"
blanchet@42531
    68
blanchet@42562
    69
fun make_tff_type s = tff_type_prefix ^ ascii_of s
blanchet@42531
    70
blanchet@42568
    71
(* official TPTP syntax *)
blanchet@42568
    72
val tptp_tff_type_of_types = "$tType"
blanchet@42568
    73
val tptp_tff_bool_type = "$o"
blanchet@42568
    74
val tptp_false = "$false"
blanchet@42568
    75
val tptp_true = "$true"
blanchet@42534
    76
blanchet@38282
    77
(* Freshness almost guaranteed! *)
blanchet@38282
    78
val sledgehammer_weak_prefix = "Sledgehammer:"
blanchet@38282
    79
blanchet@42573
    80
fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
blanchet@42573
    81
  | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
blanchet@42573
    82
  | formula_map f (AAtom tm) = AAtom (f tm)
blanchet@42573
    83
blanchet@42573
    84
fun formula_fold f (AQuant (_, _, phi)) = formula_fold f phi
blanchet@42573
    85
  | formula_fold f (AConn (_, phis)) = fold (formula_fold f) phis
blanchet@42573
    86
  | formula_fold f (AAtom tm) = f tm
blanchet@42573
    87
blanchet@40114
    88
type translated_formula =
blanchet@38752
    89
  {name: string,
blanchet@42525
    90
   kind: formula_kind,
blanchet@42562
    91
   combformula: (name, typ, combterm) formula,
blanchet@42562
    92
   atomic_types: typ list}
blanchet@38282
    93
blanchet@42558
    94
fun update_combformula f
blanchet@42562
    95
        ({name, kind, combformula, atomic_types} : translated_formula) =
blanchet@42542
    96
  {name = name, kind = kind, combformula = f combformula,
blanchet@42562
    97
   atomic_types = atomic_types} : translated_formula
blanchet@42542
    98
blanchet@42558
    99
fun fact_lift f ({combformula, ...} : translated_formula) = f combformula
blanchet@42558
   100
blanchet@41134
   101
datatype type_system =
blanchet@42523
   102
  Many_Typed |
blanchet@42548
   103
  Mangled of bool |
blanchet@42523
   104
  Args of bool |
blanchet@42548
   105
  Tags of bool |
blanchet@41134
   106
  No_Types
blanchet@41134
   107
blanchet@42523
   108
fun is_type_system_sound Many_Typed = true
blanchet@42548
   109
  | is_type_system_sound (Mangled full_types) = full_types
blanchet@42523
   110
  | is_type_system_sound (Args full_types) = full_types
blanchet@42548
   111
  | is_type_system_sound (Tags full_types) = full_types
blanchet@42523
   112
  | is_type_system_sound No_Types = false
blanchet@42449
   113
blanchet@42449
   114
fun type_system_types_dangerous_types (Tags _) = true
blanchet@42523
   115
  | type_system_types_dangerous_types type_sys = is_type_system_sound type_sys
blanchet@41138
   116
blanchet@42574
   117
fun type_system_introduces_type_preds (Mangled true) = true
blanchet@42574
   118
  | type_system_introduces_type_preds (Args true) = true
blanchet@42574
   119
  | type_system_introduces_type_preds _ = false
blanchet@42574
   120
blanchet@42574
   121
fun type_system_declares_sym_types Many_Typed = true
blanchet@42574
   122
  | type_system_declares_sym_types type_sys =
blanchet@42574
   123
    type_system_introduces_type_preds type_sys
blanchet@42573
   124
blanchet@42572
   125
val boring_consts = [explicit_app_base, @{const_name Metis.fequal}]
blanchet@42572
   126
blanchet@42572
   127
fun should_omit_type_args type_sys s =
blanchet@42542
   128
  s <> type_pred_base andalso
blanchet@42572
   129
  (s = @{const_name HOL.eq} orelse
blanchet@42531
   130
   case type_sys of
blanchet@42533
   131
     Many_Typed => false
blanchet@42548
   132
   | Mangled _ => false
blanchet@42572
   133
   | No_Types => true
blanchet@42572
   134
   | Tags true => true
blanchet@42572
   135
   | _ => member (op =) boring_consts s)
blanchet@41136
   136
blanchet@42548
   137
datatype type_arg_policy = No_Type_Args | Mangled_Types | Explicit_Type_Args
blanchet@42227
   138
blanchet@42563
   139
fun general_type_arg_policy Many_Typed = Mangled_Types
blanchet@42563
   140
  | general_type_arg_policy (Mangled _) = Mangled_Types
blanchet@42563
   141
  | general_type_arg_policy _ = Explicit_Type_Args
blanchet@42563
   142
blanchet@42524
   143
fun type_arg_policy type_sys s =
blanchet@42572
   144
  if should_omit_type_args type_sys s then No_Type_Args
blanchet@42563
   145
  else general_type_arg_policy type_sys
blanchet@42227
   146
blanchet@41136
   147
fun num_atp_type_args thy type_sys s =
blanchet@42557
   148
  if type_arg_policy type_sys s = Explicit_Type_Args then num_type_args thy s
blanchet@42557
   149
  else 0
blanchet@41136
   150
blanchet@42353
   151
fun atp_type_literals_for_types type_sys kind Ts =
blanchet@42353
   152
  if type_sys = No_Types then
blanchet@42353
   153
    []
blanchet@42353
   154
  else
blanchet@42353
   155
    Ts |> type_literals_for_types
blanchet@42353
   156
       |> filter (fn TyLitVar _ => kind <> Conjecture
blanchet@42353
   157
                   | TyLitFree _ => kind = Conjecture)
blanchet@41137
   158
blanchet@38282
   159
fun mk_anot phi = AConn (ANot, [phi])
blanchet@38282
   160
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
blanchet@42534
   161
fun mk_aconns c phis =
blanchet@42534
   162
  let val (phis', phi') = split_last phis in
blanchet@42534
   163
    fold_rev (mk_aconn c) phis' phi'
blanchet@42534
   164
  end
blanchet@38282
   165
fun mk_ahorn [] phi = phi
blanchet@42534
   166
  | mk_ahorn phis psi = AConn (AImplies, [mk_aconns AAnd phis, psi])
blanchet@42522
   167
fun mk_aquant _ [] phi = phi
blanchet@42522
   168
  | mk_aquant q xs (phi as AQuant (q', xs', phi')) =
blanchet@42522
   169
    if q = q' then AQuant (q, xs @ xs', phi') else AQuant (q, xs, phi)
blanchet@42522
   170
  | mk_aquant q xs phi = AQuant (q, xs, phi)
blanchet@38282
   171
blanchet@42522
   172
fun close_universally atom_vars phi =
blanchet@41145
   173
  let
blanchet@41145
   174
    fun formula_vars bounds (AQuant (_, xs, phi)) =
blanchet@42526
   175
        formula_vars (map fst xs @ bounds) phi
blanchet@41145
   176
      | formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
blanchet@42522
   177
      | formula_vars bounds (AAtom tm) =
blanchet@42526
   178
        union (op =) (atom_vars tm []
blanchet@42526
   179
                      |> filter_out (member (op =) bounds o fst))
blanchet@42522
   180
  in mk_aquant AForall (formula_vars [] phi []) phi end
blanchet@42522
   181
blanchet@42531
   182
fun combterm_vars (CombApp (tm1, tm2)) = fold combterm_vars [tm1, tm2]
blanchet@42522
   183
  | combterm_vars (CombConst _) = I
blanchet@42574
   184
  | combterm_vars (CombVar (name, T)) = insert (op =) (name, SOME T)
blanchet@42522
   185
val close_combformula_universally = close_universally combterm_vars
blanchet@42522
   186
blanchet@42522
   187
fun term_vars (ATerm (name as (s, _), tms)) =
blanchet@42531
   188
  is_atp_variable s ? insert (op =) (name, NONE)
blanchet@42526
   189
  #> fold term_vars tms
blanchet@42522
   190
val close_formula_universally = close_universally term_vars
blanchet@41145
   191
blanchet@42562
   192
fun fo_term_from_typ (Type (s, Ts)) =
blanchet@42562
   193
    ATerm (`make_fixed_type_const s, map fo_term_from_typ Ts)
blanchet@42562
   194
  | fo_term_from_typ (TFree (s, _)) =
blanchet@42562
   195
    ATerm (`make_fixed_type_var s, [])
blanchet@42562
   196
  | fo_term_from_typ (TVar ((x as (s, _)), _)) =
blanchet@42562
   197
    ATerm ((make_schematic_type_var x, s), [])
blanchet@42562
   198
blanchet@42562
   199
(* This shouldn't clash with anything else. *)
blanchet@42542
   200
val mangled_type_sep = "\000"
blanchet@42542
   201
blanchet@42562
   202
fun generic_mangled_type_name f (ATerm (name, [])) = f name
blanchet@42562
   203
  | generic_mangled_type_name f (ATerm (name, tys)) =
blanchet@42562
   204
    f name ^ "(" ^ commas (map (generic_mangled_type_name f) tys) ^ ")"
blanchet@42562
   205
val mangled_type_name =
blanchet@42562
   206
  fo_term_from_typ
blanchet@42562
   207
  #> (fn ty => (make_tff_type (generic_mangled_type_name fst ty),
blanchet@42562
   208
                generic_mangled_type_name snd ty))
blanchet@42542
   209
blanchet@42574
   210
fun generic_mangled_type_suffix f g Ts =
blanchet@42542
   211
  fold_rev (curry (op ^) o g o prefix mangled_type_sep
blanchet@42574
   212
            o generic_mangled_type_name f) Ts ""
blanchet@42562
   213
fun mangled_const_name T_args (s, s') =
blanchet@42562
   214
  let val ty_args = map fo_term_from_typ T_args in
blanchet@42562
   215
    (s ^ generic_mangled_type_suffix fst ascii_of ty_args,
blanchet@42562
   216
     s' ^ generic_mangled_type_suffix snd I ty_args)
blanchet@42562
   217
  end
blanchet@42542
   218
blanchet@42542
   219
val parse_mangled_ident =
blanchet@42542
   220
  Scan.many1 (not o member (op =) ["(", ")", ","]) >> implode
blanchet@42542
   221
blanchet@42542
   222
fun parse_mangled_type x =
blanchet@42542
   223
  (parse_mangled_ident
blanchet@42542
   224
   -- Scan.optional ($$ "(" |-- Scan.optional parse_mangled_types [] --| $$ ")")
blanchet@42542
   225
                    [] >> ATerm) x
blanchet@42542
   226
and parse_mangled_types x =
blanchet@42542
   227
  (parse_mangled_type ::: Scan.repeat ($$ "," |-- parse_mangled_type)) x
blanchet@42542
   228
blanchet@42542
   229
fun unmangled_type s =
blanchet@42542
   230
  s |> suffix ")" |> raw_explode
blanchet@42542
   231
    |> Scan.finite Symbol.stopper
blanchet@42542
   232
           (Scan.error (!! (fn _ => raise Fail ("unrecognized mangled type " ^
blanchet@42542
   233
                                                quote s)) parse_mangled_type))
blanchet@42542
   234
    |> fst
blanchet@42542
   235
blanchet@42561
   236
val unmangled_const_name = space_explode mangled_type_sep #> hd
blanchet@42542
   237
fun unmangled_const s =
blanchet@42542
   238
  let val ss = space_explode mangled_type_sep s in
blanchet@42542
   239
    (hd ss, map unmangled_type (tl ss))
blanchet@42542
   240
  end
blanchet@42542
   241
blanchet@42568
   242
val introduce_proxies =
blanchet@42568
   243
  let
blanchet@42568
   244
    fun aux top_level (CombApp (tm1, tm2)) =
blanchet@42568
   245
        CombApp (aux top_level tm1, aux false tm2)
blanchet@42574
   246
      | aux top_level (CombConst (name as (s, s'), T, T_args)) =
blanchet@42570
   247
        (case proxify_const s of
blanchet@42568
   248
           SOME proxy_base =>
blanchet@42568
   249
           if top_level then
blanchet@42568
   250
             (case s of
blanchet@42568
   251
                "c_False" => (tptp_false, s')
blanchet@42568
   252
              | "c_True" => (tptp_true, s')
blanchet@42568
   253
              | _ => name, [])
blanchet@42569
   254
           else
blanchet@42574
   255
             (proxy_base |>> prefix const_prefix, T_args)
blanchet@42574
   256
          | NONE => (name, T_args))
blanchet@42574
   257
        |> (fn (name, T_args) => CombConst (name, T, T_args))
blanchet@42568
   258
      | aux _ tm = tm
blanchet@42568
   259
  in aux true end
blanchet@42568
   260
blanchet@42562
   261
fun combformula_from_prop thy eq_as_iff =
blanchet@38282
   262
  let
blanchet@42568
   263
    fun do_term bs t atomic_types =
blanchet@41140
   264
      combterm_from_term thy bs (Envir.eta_contract t)
blanchet@42568
   265
      |>> (introduce_proxies #> AAtom)
blanchet@42568
   266
      ||> union (op =) atomic_types
blanchet@38282
   267
    fun do_quant bs q s T t' =
blanchet@38518
   268
      let val s = Name.variant (map fst bs) s in
blanchet@38518
   269
        do_formula ((s, T) :: bs) t'
blanchet@42562
   270
        #>> mk_aquant q [(`make_bound_var s, SOME T)]
blanchet@38518
   271
      end
blanchet@38282
   272
    and do_conn bs c t1 t2 =
blanchet@38282
   273
      do_formula bs t1 ##>> do_formula bs t2
blanchet@42531
   274
      #>> uncurry (mk_aconn c)
blanchet@38282
   275
    and do_formula bs t =
blanchet@38282
   276
      case t of
blanchet@42531
   277
        @{const Not} $ t1 => do_formula bs t1 #>> mk_anot
blanchet@38282
   278
      | Const (@{const_name All}, _) $ Abs (s, T, t') =>
blanchet@38282
   279
        do_quant bs AForall s T t'
blanchet@38282
   280
      | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
blanchet@38282
   281
        do_quant bs AExists s T t'
haftmann@38795
   282
      | @{const HOL.conj} $ t1 $ t2 => do_conn bs AAnd t1 t2
haftmann@38795
   283
      | @{const HOL.disj} $ t1 $ t2 => do_conn bs AOr t1 t2
haftmann@38786
   284
      | @{const HOL.implies} $ t1 $ t2 => do_conn bs AImplies t1 t2
haftmann@38864
   285
      | Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
blanchet@41140
   286
        if eq_as_iff then do_conn bs AIff t1 t2 else do_term bs t
blanchet@41140
   287
      | _ => do_term bs t
blanchet@38282
   288
  in do_formula [] end
blanchet@38282
   289
blanchet@38618
   290
val presimplify_term = prop_of o Meson.presimplify oo Skip_Proof.make_thm
blanchet@38282
   291
wenzelm@41491
   292
fun concealed_bound_name j = sledgehammer_weak_prefix ^ string_of_int j
blanchet@38282
   293
fun conceal_bounds Ts t =
blanchet@38282
   294
  subst_bounds (map (Free o apfst concealed_bound_name)
blanchet@38282
   295
                    (0 upto length Ts - 1 ~~ Ts), t)
blanchet@38282
   296
fun reveal_bounds Ts =
blanchet@38282
   297
  subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
blanchet@38282
   298
                    (0 upto length Ts - 1 ~~ Ts))
blanchet@38282
   299
blanchet@38608
   300
(* Removes the lambdas from an equation of the form "t = (%x. u)".
blanchet@39890
   301
   (Cf. "extensionalize_theorem" in "Meson_Clausify".) *)
blanchet@38608
   302
fun extensionalize_term t =
blanchet@38608
   303
  let
blanchet@38608
   304
    fun aux j (@{const Trueprop} $ t') = @{const Trueprop} $ aux j t'
blanchet@38608
   305
      | aux j (t as Const (s, Type (_, [Type (_, [_, T']),
blanchet@38608
   306
                                        Type (_, [_, res_T])]))
blanchet@38608
   307
                    $ t2 $ Abs (var_s, var_T, t')) =
haftmann@38864
   308
        if s = @{const_name HOL.eq} orelse s = @{const_name "=="} then
blanchet@38608
   309
          let val var_t = Var ((var_s, j), var_T) in
blanchet@38608
   310
            Const (s, T' --> T' --> res_T)
blanchet@38608
   311
              $ betapply (t2, var_t) $ subst_bound (var_t, t')
blanchet@38608
   312
            |> aux (j + 1)
blanchet@38608
   313
          end
blanchet@38608
   314
        else
blanchet@38608
   315
          t
blanchet@38608
   316
      | aux _ t = t
blanchet@38608
   317
  in aux (maxidx_of_term t + 1) t end
blanchet@38608
   318
blanchet@38282
   319
fun introduce_combinators_in_term ctxt kind t =
wenzelm@42361
   320
  let val thy = Proof_Context.theory_of ctxt in
blanchet@38491
   321
    if Meson.is_fol_term thy t then
blanchet@38491
   322
      t
blanchet@38491
   323
    else
blanchet@38491
   324
      let
blanchet@38491
   325
        fun aux Ts t =
blanchet@38491
   326
          case t of
blanchet@38491
   327
            @{const Not} $ t1 => @{const Not} $ aux Ts t1
blanchet@38491
   328
          | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
blanchet@38491
   329
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38652
   330
          | (t0 as Const (@{const_name All}, _)) $ t1 =>
blanchet@38652
   331
            aux Ts (t0 $ eta_expand Ts t1 1)
blanchet@38491
   332
          | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
blanchet@38491
   333
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38652
   334
          | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
blanchet@38652
   335
            aux Ts (t0 $ eta_expand Ts t1 1)
haftmann@38795
   336
          | (t0 as @{const HOL.conj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38795
   337
          | (t0 as @{const HOL.disj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38786
   338
          | (t0 as @{const HOL.implies}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38864
   339
          | (t0 as Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])))
blanchet@38491
   340
              $ t1 $ t2 =>
blanchet@38491
   341
            t0 $ aux Ts t1 $ aux Ts t2
blanchet@38491
   342
          | _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
blanchet@38491
   343
                   t
blanchet@38491
   344
                 else
blanchet@38491
   345
                   t |> conceal_bounds Ts
blanchet@38491
   346
                     |> Envir.eta_contract
blanchet@38491
   347
                     |> cterm_of thy
blanchet@39890
   348
                     |> Meson_Clausify.introduce_combinators_in_cterm
blanchet@38491
   349
                     |> prop_of |> Logic.dest_equals |> snd
blanchet@38491
   350
                     |> reveal_bounds Ts
blanchet@39370
   351
        val (t, ctxt') = Variable.import_terms true [t] ctxt |>> the_single
blanchet@38491
   352
      in t |> aux [] |> singleton (Variable.export_terms ctxt' ctxt) end
blanchet@38491
   353
      handle THM _ =>
blanchet@38491
   354
             (* A type variable of sort "{}" will make abstraction fail. *)
blanchet@38613
   355
             if kind = Conjecture then HOLogic.false_const
blanchet@38613
   356
             else HOLogic.true_const
blanchet@38491
   357
  end
blanchet@38282
   358
blanchet@38282
   359
(* Metis's use of "resolve_tac" freezes the schematic variables. We simulate the
blanchet@42353
   360
   same in Sledgehammer to prevent the discovery of unreplayable proofs. *)
blanchet@38282
   361
fun freeze_term t =
blanchet@38282
   362
  let
blanchet@38282
   363
    fun aux (t $ u) = aux t $ aux u
blanchet@38282
   364
      | aux (Abs (s, T, t)) = Abs (s, T, aux t)
blanchet@38282
   365
      | aux (Var ((s, i), T)) =
blanchet@38282
   366
        Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
blanchet@38282
   367
      | aux t = t
blanchet@38282
   368
  in t |> exists_subterm is_Var t ? aux end
blanchet@38282
   369
blanchet@40204
   370
(* making fact and conjecture formulas *)
blanchet@42544
   371
fun make_formula ctxt eq_as_iff presimp name kind t =
blanchet@38282
   372
  let
wenzelm@42361
   373
    val thy = Proof_Context.theory_of ctxt
blanchet@38608
   374
    val t = t |> Envir.beta_eta_contract
blanchet@38652
   375
              |> transform_elim_term
blanchet@41211
   376
              |> Object_Logic.atomize_term thy
blanchet@42563
   377
    val need_trueprop = (fastype_of t = @{typ bool})
blanchet@38652
   378
    val t = t |> need_trueprop ? HOLogic.mk_Trueprop
blanchet@38282
   379
              |> extensionalize_term
blanchet@38282
   380
              |> presimp ? presimplify_term thy
blanchet@38282
   381
              |> perhaps (try (HOLogic.dest_Trueprop))
blanchet@38282
   382
              |> introduce_combinators_in_term ctxt kind
blanchet@38613
   383
              |> kind <> Axiom ? freeze_term
blanchet@42562
   384
    val (combformula, atomic_types) =
blanchet@42562
   385
      combformula_from_prop thy eq_as_iff t []
blanchet@38282
   386
  in
blanchet@38752
   387
    {name = name, combformula = combformula, kind = kind,
blanchet@42562
   388
     atomic_types = atomic_types}
blanchet@38282
   389
  end
blanchet@38282
   390
blanchet@42561
   391
fun make_fact ctxt keep_trivial eq_as_iff presimp ((name, _), t) =
blanchet@42561
   392
  case (keep_trivial, make_formula ctxt eq_as_iff presimp name Axiom t) of
blanchet@41990
   393
    (false, {combformula = AAtom (CombConst (("c_True", _), _, _)), ...}) =>
blanchet@41990
   394
    NONE
blanchet@41990
   395
  | (_, formula) => SOME formula
blanchet@42561
   396
blanchet@42544
   397
fun make_conjecture ctxt ts =
blanchet@38613
   398
  let val last = length ts - 1 in
blanchet@42544
   399
    map2 (fn j => make_formula ctxt true true (string_of_int j)
blanchet@38613
   400
                               (if j = last then Conjecture else Hypothesis))
blanchet@38613
   401
         (0 upto last) ts
blanchet@38613
   402
  end
blanchet@38282
   403
blanchet@42573
   404
(** "hBOOL" and "hAPP" **)
blanchet@41313
   405
blanchet@42574
   406
type sym_info =
blanchet@42563
   407
  {pred_sym : bool, min_ary : int, max_ary : int, typ : typ option}
blanchet@42563
   408
blanchet@42574
   409
fun add_combterm_syms_to_table explicit_apply =
blanchet@42558
   410
  let
blanchet@42558
   411
    fun aux top_level tm =
blanchet@42558
   412
      let val (head, args) = strip_combterm_comb tm in
blanchet@42558
   413
        (case head of
blanchet@42563
   414
           CombConst ((s, _), T, _) =>
blanchet@42558
   415
           if String.isPrefix bound_var_prefix s then
blanchet@42558
   416
             I
blanchet@42558
   417
           else
blanchet@42563
   418
             let val ary = length args in
blanchet@42558
   419
               Symtab.map_default
blanchet@42558
   420
                   (s, {pred_sym = true,
blanchet@42563
   421
                        min_ary = if explicit_apply then 0 else ary,
blanchet@42563
   422
                        max_ary = 0, typ = SOME T})
blanchet@42563
   423
                   (fn {pred_sym, min_ary, max_ary, typ} =>
blanchet@42558
   424
                       {pred_sym = pred_sym andalso top_level,
blanchet@42563
   425
                        min_ary = Int.min (ary, min_ary),
blanchet@42563
   426
                        max_ary = Int.max (ary, max_ary),
blanchet@42563
   427
                        typ = if typ = SOME T then typ else NONE})
blanchet@42558
   428
            end
blanchet@42558
   429
         | _ => I)
blanchet@42558
   430
        #> fold (aux false) args
blanchet@42558
   431
      end
blanchet@42558
   432
  in aux true end
blanchet@42574
   433
val add_fact_syms_to_table =
blanchet@42574
   434
  fact_lift o formula_fold o add_combterm_syms_to_table
blanchet@38282
   435
blanchet@42557
   436
val default_sym_table_entries =
blanchet@42563
   437
  [("equal", {pred_sym = true, min_ary = 2, max_ary = 2, typ = NONE}),
blanchet@42568
   438
   (make_fixed_const predicator_base,
blanchet@42563
   439
    {pred_sym = true, min_ary = 1, max_ary = 1, typ = NONE})] @
blanchet@42568
   440
  ([tptp_false, tptp_true]
blanchet@42563
   441
   |> map (rpair {pred_sym = true, min_ary = 0, max_ary = 0, typ = NONE}))
blanchet@41140
   442
blanchet@42544
   443
fun sym_table_for_facts explicit_apply facts =
blanchet@42568
   444
  Symtab.empty |> fold Symtab.default default_sym_table_entries
blanchet@42574
   445
               |> fold (add_fact_syms_to_table explicit_apply) facts
blanchet@38282
   446
blanchet@42558
   447
fun min_arity_of sym_tab s =
blanchet@42558
   448
  case Symtab.lookup sym_tab s of
blanchet@42574
   449
    SOME ({min_ary, ...} : sym_info) => min_ary
blanchet@42558
   450
  | NONE =>
blanchet@42558
   451
    case strip_prefix_and_unascii const_prefix s of
blanchet@42547
   452
      SOME s =>
blanchet@42570
   453
      let val s = s |> unmangled_const_name |> invert_const in
blanchet@42568
   454
        if s = predicator_base then 1
blanchet@42547
   455
        else if s = explicit_app_base then 2
blanchet@42547
   456
        else if s = type_pred_base then 1
blanchet@42557
   457
        else 0
blanchet@42547
   458
      end
blanchet@42544
   459
    | NONE => 0
blanchet@38282
   460
blanchet@38282
   461
(* True if the constant ever appears outside of the top-level position in
blanchet@38282
   462
   literals, or if it appears with different arities (e.g., because of different
blanchet@38282
   463
   type instantiations). If false, the constant always receives all of its
blanchet@38282
   464
   arguments and is used as a predicate. *)
blanchet@42558
   465
fun is_pred_sym sym_tab s =
blanchet@42558
   466
  case Symtab.lookup sym_tab s of
blanchet@42574
   467
    SOME ({pred_sym, min_ary, max_ary, ...} : sym_info) =>
blanchet@42574
   468
    pred_sym andalso min_ary = max_ary
blanchet@42558
   469
  | NONE => false
blanchet@38282
   470
blanchet@42568
   471
val predicator_combconst =
blanchet@42568
   472
  CombConst (`make_fixed_const predicator_base, @{typ "bool => bool"}, [])
blanchet@42568
   473
fun predicator tm = CombApp (predicator_combconst, tm)
blanchet@42542
   474
blanchet@42568
   475
fun introduce_predicators_in_combterm sym_tab tm =
blanchet@42542
   476
  case strip_combterm_comb tm of
blanchet@42542
   477
    (CombConst ((s, _), _, _), _) =>
blanchet@42568
   478
    if is_pred_sym sym_tab s then tm else predicator tm
blanchet@42568
   479
  | _ => predicator tm
blanchet@42542
   480
blanchet@42544
   481
fun list_app head args = fold (curry (CombApp o swap)) args head
blanchet@42544
   482
blanchet@42544
   483
fun explicit_app arg head =
blanchet@42544
   484
  let
blanchet@42562
   485
    val head_T = combtyp_of head
blanchet@42562
   486
    val (arg_T, res_T) = dest_funT head_T
blanchet@42544
   487
    val explicit_app =
blanchet@42562
   488
      CombConst (`make_fixed_const explicit_app_base, head_T --> head_T,
blanchet@42562
   489
                 [arg_T, res_T])
blanchet@42544
   490
  in list_app explicit_app [head, arg] end
blanchet@42544
   491
fun list_explicit_app head args = fold explicit_app args head
blanchet@38282
   492
blanchet@42565
   493
fun introduce_explicit_apps_in_combterm sym_tab =
blanchet@42544
   494
  let
blanchet@42544
   495
    fun aux tm =
blanchet@42544
   496
      case strip_combterm_comb tm of
blanchet@42544
   497
        (head as CombConst ((s, _), _, _), args) =>
blanchet@42544
   498
        args |> map aux
blanchet@42557
   499
             |> chop (min_arity_of sym_tab s)
blanchet@42544
   500
             |>> list_app head
blanchet@42544
   501
             |-> list_explicit_app
blanchet@42544
   502
      | (head, args) => list_explicit_app head (map aux args)
blanchet@42544
   503
  in aux end
blanchet@38282
   504
blanchet@42573
   505
fun impose_type_arg_policy_in_combterm type_sys =
blanchet@42573
   506
  let
blanchet@42573
   507
    fun aux (CombApp tmp) = CombApp (pairself aux tmp)
blanchet@42574
   508
      | aux (CombConst (name as (s, _), T, T_args)) =
blanchet@42573
   509
        (case strip_prefix_and_unascii const_prefix s of
blanchet@42574
   510
           NONE => (name, T_args)
blanchet@42573
   511
         | SOME s'' =>
blanchet@42573
   512
           let val s'' = invert_const s'' in
blanchet@42573
   513
             case type_arg_policy type_sys s'' of
blanchet@42573
   514
               No_Type_Args => (name, [])
blanchet@42574
   515
             | Mangled_Types => (mangled_const_name T_args name, [])
blanchet@42574
   516
             | Explicit_Type_Args => (name, T_args)
blanchet@42573
   517
           end)
blanchet@42574
   518
        |> (fn (name, T_args) => CombConst (name, T, T_args))
blanchet@42573
   519
      | aux tm = tm
blanchet@42573
   520
  in aux end
blanchet@42573
   521
blanchet@42573
   522
fun repair_combterm type_sys sym_tab =
blanchet@42565
   523
  introduce_explicit_apps_in_combterm sym_tab
blanchet@42568
   524
  #> introduce_predicators_in_combterm sym_tab
blanchet@42573
   525
  #> impose_type_arg_policy_in_combterm type_sys
blanchet@42573
   526
val repair_fact = update_combformula o formula_map oo repair_combterm
blanchet@42573
   527
blanchet@42573
   528
(** Helper facts **)
blanchet@42573
   529
blanchet@42573
   530
fun ti_ti_helper_fact () =
blanchet@42573
   531
  let
blanchet@42573
   532
    fun var s = ATerm (`I s, [])
blanchet@42573
   533
    fun tag tm = ATerm (`I type_tag_name, [var "X", tm])
blanchet@42573
   534
  in
blanchet@42573
   535
    Formula (Fof, helper_prefix ^ ascii_of "ti_ti", Axiom,
blanchet@42573
   536
             AAtom (ATerm (`I "equal", [tag (tag (var "Y")), tag (var "Y")]))
blanchet@42573
   537
             |> close_formula_universally, NONE, NONE)
blanchet@42573
   538
  end
blanchet@42573
   539
blanchet@42574
   540
fun helper_facts_for_sym ctxt type_sys (s, {typ, ...} : sym_info) =
blanchet@42573
   541
  case strip_prefix_and_unascii const_prefix s of
blanchet@42573
   542
    SOME mangled_s =>
blanchet@42573
   543
    let
blanchet@42573
   544
      val thy = Proof_Context.theory_of ctxt
blanchet@42573
   545
      val unmangled_s = mangled_s |> unmangled_const_name
blanchet@42573
   546
      fun dub_and_inst c needs_full_types (th, j) =
blanchet@42573
   547
        ((c ^ "_" ^ string_of_int j ^ (if needs_full_types then "ft" else ""),
blanchet@42573
   548
          false),
blanchet@42573
   549
         let val t = th |> prop_of in
blanchet@42573
   550
           t |> (general_type_arg_policy type_sys = Mangled_Types andalso
blanchet@42573
   551
                 not (null (Term.hidden_polymorphism t)))
blanchet@42573
   552
                ? (case typ of
blanchet@42573
   553
                     SOME T => specialize_type thy (invert_const unmangled_s, T)
blanchet@42573
   554
                   | NONE => I)
blanchet@42573
   555
         end)
blanchet@42573
   556
      fun make_facts eq_as_iff =
blanchet@42573
   557
        map_filter (make_fact ctxt false eq_as_iff false)
blanchet@42573
   558
    in
blanchet@42573
   559
      metis_helpers
blanchet@42573
   560
      |> maps (fn (metis_s, (needs_full_types, ths)) =>
blanchet@42573
   561
                  if metis_s <> unmangled_s orelse
blanchet@42573
   562
                     (needs_full_types andalso
blanchet@42573
   563
                      not (type_system_types_dangerous_types type_sys)) then
blanchet@42573
   564
                    []
blanchet@42573
   565
                  else
blanchet@42573
   566
                    ths ~~ (1 upto length ths)
blanchet@42573
   567
                    |> map (dub_and_inst mangled_s needs_full_types)
blanchet@42573
   568
                    |> make_facts (not needs_full_types))
blanchet@42573
   569
    end
blanchet@42573
   570
  | NONE => []
blanchet@42573
   571
fun helper_facts_for_sym_table ctxt type_sys sym_tab =
blanchet@42573
   572
  Symtab.fold_rev (append o helper_facts_for_sym ctxt type_sys) sym_tab []
blanchet@42573
   573
blanchet@42573
   574
fun translate_atp_fact ctxt keep_trivial =
blanchet@42573
   575
  `(make_fact ctxt keep_trivial true true o apsnd prop_of)
blanchet@42573
   576
blanchet@42573
   577
fun translate_formulas ctxt type_sys hyp_ts concl_t rich_facts =
blanchet@42573
   578
  let
blanchet@42573
   579
    val thy = Proof_Context.theory_of ctxt
blanchet@42573
   580
    val fact_ts = map (prop_of o snd o snd) rich_facts
blanchet@42573
   581
    val (facts, fact_names) =
blanchet@42573
   582
      rich_facts
blanchet@42573
   583
      |> map_filter (fn (NONE, _) => NONE
blanchet@42573
   584
                      | (SOME fact, (name, _)) => SOME (fact, name))
blanchet@42573
   585
      |> ListPair.unzip
blanchet@42573
   586
    (* Remove existing facts from the conjecture, as this can dramatically
blanchet@42573
   587
       boost an ATP's performance (for some reason). *)
blanchet@42573
   588
    val hyp_ts = hyp_ts |> filter_out (member (op aconv) fact_ts)
blanchet@42573
   589
    val goal_t = Logic.list_implies (hyp_ts, concl_t)
blanchet@42573
   590
    val all_ts = goal_t :: fact_ts
blanchet@42573
   591
    val subs = tfree_classes_of_terms all_ts
blanchet@42573
   592
    val supers = tvar_classes_of_terms all_ts
blanchet@42573
   593
    val tycons = type_consts_of_terms thy all_ts
blanchet@42573
   594
    val conjs = make_conjecture ctxt (hyp_ts @ [concl_t])
blanchet@42573
   595
    val (supers', arity_clauses) =
blanchet@42573
   596
      if type_sys = No_Types then ([], [])
blanchet@42573
   597
      else make_arity_clauses thy tycons supers
blanchet@42573
   598
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@42573
   599
  in
blanchet@42573
   600
    (fact_names |> map single, (conjs, facts, class_rel_clauses, arity_clauses))
blanchet@42573
   601
  end
blanchet@42573
   602
blanchet@42574
   603
fun tag_with_type ty tm = ATerm (`I type_tag_name, [ty, tm])
blanchet@42573
   604
blanchet@42573
   605
fun fo_literal_from_type_literal (TyLitVar (class, name)) =
blanchet@42573
   606
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@42573
   607
  | fo_literal_from_type_literal (TyLitFree (class, name)) =
blanchet@42573
   608
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@42573
   609
blanchet@42573
   610
fun formula_from_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
blanchet@42573
   611
blanchet@42573
   612
(* Finite types such as "unit", "bool", "bool * bool", and "bool => bool" are
blanchet@42573
   613
   considered dangerous because their "exhaust" properties can easily lead to
blanchet@42573
   614
   unsound ATP proofs. The checks below are an (unsound) approximation of
blanchet@42573
   615
   finiteness. *)
blanchet@42573
   616
blanchet@42573
   617
fun is_dtyp_dangerous _ (Datatype_Aux.DtTFree _) = true
blanchet@42573
   618
  | is_dtyp_dangerous ctxt (Datatype_Aux.DtType (s, Us)) =
blanchet@42573
   619
    is_type_constr_dangerous ctxt s andalso forall (is_dtyp_dangerous ctxt) Us
blanchet@42573
   620
  | is_dtyp_dangerous _ (Datatype_Aux.DtRec _) = false
blanchet@42573
   621
and is_type_dangerous ctxt (Type (s, Ts)) =
blanchet@42573
   622
    is_type_constr_dangerous ctxt s andalso forall (is_type_dangerous ctxt) Ts
blanchet@42573
   623
  | is_type_dangerous _ _ = false
blanchet@42573
   624
and is_type_constr_dangerous ctxt s =
blanchet@42573
   625
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42573
   626
    case Datatype_Data.get_info thy s of
blanchet@42573
   627
      SOME {descr, ...} =>
blanchet@42573
   628
      forall (fn (_, (_, _, constrs)) =>
blanchet@42573
   629
                 forall (forall (is_dtyp_dangerous ctxt) o snd) constrs) descr
blanchet@42573
   630
    | NONE =>
blanchet@42573
   631
      case Typedef.get_info ctxt s of
blanchet@42573
   632
        ({rep_type, ...}, _) :: _ => is_type_dangerous ctxt rep_type
blanchet@42573
   633
      | [] => true
blanchet@42573
   634
  end
blanchet@42573
   635
blanchet@42573
   636
fun should_tag_with_type ctxt (Tags full_types) T =
blanchet@42573
   637
    full_types orelse is_type_dangerous ctxt T
blanchet@42573
   638
  | should_tag_with_type _ _ _ = false
blanchet@42573
   639
blanchet@42573
   640
fun type_pred_combatom type_sys T tm =
blanchet@42573
   641
  CombApp (CombConst (`make_fixed_const type_pred_base, T --> @{typ bool}, [T]),
blanchet@42573
   642
           tm)
blanchet@42573
   643
  |> impose_type_arg_policy_in_combterm type_sys
blanchet@42573
   644
  |> AAtom
blanchet@42573
   645
blanchet@42573
   646
fun formula_from_combformula ctxt type_sys =
blanchet@42573
   647
  let
blanchet@42573
   648
    fun do_term top_level u =
blanchet@42573
   649
      let
blanchet@42573
   650
        val (head, args) = strip_combterm_comb u
blanchet@42574
   651
        val (x, T_args) =
blanchet@42573
   652
          case head of
blanchet@42574
   653
            CombConst (name, _, T_args) => (name, T_args)
blanchet@42573
   654
          | CombVar (name, _) => (name, [])
blanchet@42573
   655
          | CombApp _ => raise Fail "impossible \"CombApp\""
blanchet@42574
   656
        val t = ATerm (x, map fo_term_from_typ T_args @
blanchet@42573
   657
                          map (do_term false) args)
blanchet@42574
   658
        val T = combtyp_of u
blanchet@42573
   659
      in
blanchet@42574
   660
        t |> (if not top_level andalso should_tag_with_type ctxt type_sys T then
blanchet@42574
   661
                tag_with_type (fo_term_from_typ T)
blanchet@42573
   662
              else
blanchet@42573
   663
                I)
blanchet@42573
   664
      end
blanchet@42573
   665
    val do_bound_type =
blanchet@42573
   666
      if type_sys = Many_Typed then SOME o mangled_type_name else K NONE
blanchet@42573
   667
    fun do_out_of_bound_type (s, T) =
blanchet@42574
   668
      if type_system_introduces_type_preds type_sys then
blanchet@42573
   669
        type_pred_combatom type_sys T (CombVar (s, T))
blanchet@42573
   670
        |> do_formula |> SOME
blanchet@42573
   671
      else
blanchet@42573
   672
        NONE
blanchet@42573
   673
    and do_formula (AQuant (q, xs, phi)) =
blanchet@42573
   674
        AQuant (q, xs |> map (apsnd (fn NONE => NONE
blanchet@42574
   675
                                      | SOME T => do_bound_type T)),
blanchet@42573
   676
                (if q = AForall then mk_ahorn else fold_rev (mk_aconn AAnd))
blanchet@42573
   677
                    (map_filter
blanchet@42573
   678
                         (fn (_, NONE) => NONE
blanchet@42574
   679
                           | (s, SOME T) => do_out_of_bound_type (s, T)) xs)
blanchet@42573
   680
                    (do_formula phi))
blanchet@42573
   681
      | do_formula (AConn (c, phis)) = AConn (c, map do_formula phis)
blanchet@42573
   682
      | do_formula (AAtom tm) = AAtom (do_term true tm)
blanchet@42573
   683
  in do_formula end
blanchet@42573
   684
blanchet@42573
   685
fun formula_for_fact ctxt type_sys
blanchet@42573
   686
                     ({combformula, atomic_types, ...} : translated_formula) =
blanchet@42573
   687
  mk_ahorn (map (formula_from_fo_literal o fo_literal_from_type_literal)
blanchet@42573
   688
                (atp_type_literals_for_types type_sys Axiom atomic_types))
blanchet@42573
   689
           (formula_from_combformula ctxt type_sys
blanchet@42573
   690
                (close_combformula_universally combformula))
blanchet@42573
   691
  |> close_formula_universally
blanchet@42573
   692
blanchet@42573
   693
fun logic_for_type_sys Many_Typed = Tff
blanchet@42573
   694
  | logic_for_type_sys _ = Fof
blanchet@42573
   695
blanchet@42573
   696
(* Each fact is given a unique fact number to avoid name clashes (e.g., because
blanchet@42573
   697
   of monomorphization). The TPTP explicitly forbids name clashes, and some of
blanchet@42573
   698
   the remote provers might care. *)
blanchet@42573
   699
fun formula_line_for_fact ctxt prefix type_sys
blanchet@42573
   700
                          (j, formula as {name, kind, ...}) =
blanchet@42573
   701
  Formula (logic_for_type_sys type_sys,
blanchet@42573
   702
           prefix ^ string_of_int j ^ "_" ^ ascii_of name, kind,
blanchet@42573
   703
           formula_for_fact ctxt type_sys formula, NONE, NONE)
blanchet@42573
   704
blanchet@42573
   705
fun formula_line_for_class_rel_clause (ClassRelClause {name, subclass,
blanchet@42573
   706
                                                       superclass, ...}) =
blanchet@42573
   707
  let val ty_arg = ATerm (`I "T", []) in
blanchet@42573
   708
    Formula (Fof, class_rel_clause_prefix ^ ascii_of name, Axiom,
blanchet@42573
   709
             AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
blanchet@42573
   710
                               AAtom (ATerm (superclass, [ty_arg]))])
blanchet@42573
   711
             |> close_formula_universally, NONE, NONE)
blanchet@42573
   712
  end
blanchet@42573
   713
blanchet@42573
   714
fun fo_literal_from_arity_literal (TConsLit (c, t, args)) =
blanchet@42573
   715
    (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
blanchet@42573
   716
  | fo_literal_from_arity_literal (TVarLit (c, sort)) =
blanchet@42573
   717
    (false, ATerm (c, [ATerm (sort, [])]))
blanchet@42573
   718
blanchet@42573
   719
fun formula_line_for_arity_clause (ArityClause {name, conclLit, premLits,
blanchet@42573
   720
                                                ...}) =
blanchet@42573
   721
  Formula (Fof, arity_clause_prefix ^ ascii_of name, Axiom,
blanchet@42573
   722
           mk_ahorn (map (formula_from_fo_literal o apfst not
blanchet@42573
   723
                          o fo_literal_from_arity_literal) premLits)
blanchet@42573
   724
                    (formula_from_fo_literal
blanchet@42573
   725
                         (fo_literal_from_arity_literal conclLit))
blanchet@42573
   726
           |> close_formula_universally, NONE, NONE)
blanchet@42573
   727
blanchet@42573
   728
fun formula_line_for_conjecture ctxt type_sys
blanchet@42573
   729
        ({name, kind, combformula, ...} : translated_formula) =
blanchet@42573
   730
  Formula (logic_for_type_sys type_sys, conjecture_prefix ^ name, kind,
blanchet@42573
   731
           formula_from_combformula ctxt type_sys
blanchet@42573
   732
                                    (close_combformula_universally combformula)
blanchet@42573
   733
           |> close_formula_universally, NONE, NONE)
blanchet@42573
   734
blanchet@42573
   735
fun free_type_literals type_sys ({atomic_types, ...} : translated_formula) =
blanchet@42573
   736
  atomic_types |> atp_type_literals_for_types type_sys Conjecture
blanchet@42573
   737
               |> map fo_literal_from_type_literal
blanchet@42573
   738
blanchet@42573
   739
fun formula_line_for_free_type j lit =
blanchet@42573
   740
  Formula (Fof, tfree_prefix ^ string_of_int j, Hypothesis,
blanchet@42573
   741
           formula_from_fo_literal lit, NONE, NONE)
blanchet@42573
   742
fun formula_lines_for_free_types type_sys facts =
blanchet@42573
   743
  let
blanchet@42573
   744
    val litss = map (free_type_literals type_sys) facts
blanchet@42573
   745
    val lits = fold (union (op =)) litss []
blanchet@42573
   746
  in map2 formula_line_for_free_type (0 upto length lits - 1) lits end
blanchet@42573
   747
blanchet@42573
   748
(** Symbol declarations **)
blanchet@42544
   749
blanchet@42574
   750
fun should_declare_sym type_sys pred_sym s =
blanchet@42542
   751
  not (String.isPrefix bound_var_prefix s) andalso s <> "equal" andalso
blanchet@42574
   752
  (type_sys = Many_Typed orelse not pred_sym)
blanchet@38282
   753
blanchet@42574
   754
fun add_combterm_syms_to_decl_table type_sys repaired_sym_tab =
blanchet@42574
   755
  let
blanchet@42576
   756
    fun declare_sym (decl as (_, _, T, _, _)) decls =
blanchet@42576
   757
      if exists (curry Type.raw_instance T o #3) decls then decls
blanchet@42576
   758
      else decl :: filter_out ((fn T' => Type.raw_instance (T', T)) o #3) decls
blanchet@42576
   759
    fun do_term tm =
blanchet@42574
   760
      let val (head, args) = strip_combterm_comb tm in
blanchet@42574
   761
        (case head of
blanchet@42574
   762
           CombConst ((s, s'), T, T_args) =>
blanchet@42574
   763
           let val pred_sym = is_pred_sym repaired_sym_tab s in
blanchet@42574
   764
             if should_declare_sym type_sys pred_sym s then
blanchet@42576
   765
               Symtab.map_default (s, [])
blanchet@42576
   766
                   (declare_sym (s', T_args, T, pred_sym, length args))
blanchet@42574
   767
             else
blanchet@42574
   768
               I
blanchet@42574
   769
           end
blanchet@42574
   770
         | _ => I)
blanchet@42576
   771
        #> fold do_term args
blanchet@42574
   772
      end
blanchet@42576
   773
  in do_term end
blanchet@42574
   774
fun add_fact_syms_to_decl_table type_sys repaired_sym_tab =
blanchet@42574
   775
  fact_lift (formula_fold
blanchet@42574
   776
      (add_combterm_syms_to_decl_table type_sys repaired_sym_tab))
blanchet@42574
   777
fun sym_decl_table_for_facts type_sys repaired_sym_tab facts =
blanchet@42574
   778
  Symtab.empty |> type_system_declares_sym_types type_sys
blanchet@42574
   779
                  ? fold (add_fact_syms_to_decl_table type_sys repaired_sym_tab)
blanchet@42574
   780
                         facts
blanchet@42533
   781
blanchet@42574
   782
fun n_ary_strip_type 0 T = ([], T)
blanchet@42574
   783
  | n_ary_strip_type n (Type (@{type_name fun}, [dom_T, ran_T])) =
blanchet@42574
   784
    n_ary_strip_type (n - 1) ran_T |>> cons dom_T
blanchet@42574
   785
  | n_ary_strip_type _ _ = raise Fail "unexpected non-function"
blanchet@42533
   786
blanchet@42575
   787
fun problem_line_for_sym_decl ctxt type_sys need_bound_types s j
blanchet@42574
   788
                              (s', T_args, T, pred_sym, ary) =
blanchet@42574
   789
  if type_sys = Many_Typed then
blanchet@42574
   790
    let val (arg_Ts, res_T) = n_ary_strip_type ary T in
blanchet@42574
   791
      Decl (sym_decl_prefix ^ ascii_of s, (s, s'),
blanchet@42574
   792
            map mangled_type_name arg_Ts,
blanchet@42574
   793
            if pred_sym then `I tptp_tff_bool_type else mangled_type_name res_T)
blanchet@42574
   794
    end
blanchet@42574
   795
  else
blanchet@42574
   796
    let
blanchet@42574
   797
      val (arg_Ts, res_T) = n_ary_strip_type ary T
blanchet@42574
   798
      val bound_names =
blanchet@42574
   799
        1 upto length arg_Ts |> map (`I o make_bound_var o string_of_int)
blanchet@42574
   800
      val bound_tms =
blanchet@42574
   801
        bound_names ~~ arg_Ts |> map (fn (name, T) => CombConst (name, T, []))
blanchet@42575
   802
      val bound_Ts = arg_Ts |> map (if need_bound_types then SOME else K NONE)
blanchet@42574
   803
      val freshener =
blanchet@42574
   804
        case type_sys of Args _ => string_of_int j ^ "_" | _ => ""
blanchet@42574
   805
    in
blanchet@42574
   806
      Formula (Fof, sym_decl_prefix ^ freshener ^ "_" ^ ascii_of s, Axiom,
blanchet@42574
   807
               CombConst ((s, s'), T, T_args)
blanchet@42574
   808
               |> fold (curry (CombApp o swap)) bound_tms
blanchet@42574
   809
               |> type_pred_combatom type_sys res_T
blanchet@42574
   810
               |> mk_aquant AForall (bound_names ~~ bound_Ts)
blanchet@42574
   811
               |> formula_from_combformula ctxt type_sys,
blanchet@42574
   812
               NONE, NONE)
blanchet@42574
   813
    end
blanchet@42574
   814
fun problem_lines_for_sym_decls ctxt type_sys (s, decls) =
blanchet@42575
   815
  let
blanchet@42575
   816
    val n = length decls
blanchet@42575
   817
    fun result_type_of_decl (_, _, T, _, ary) = n_ary_strip_type ary T |> snd
blanchet@42575
   818
    val need_bound_types =
blanchet@42575
   819
      case decls of
blanchet@42575
   820
        decl :: (decls as _ :: _) =>
blanchet@42575
   821
        exists (curry (op <>) (result_type_of_decl decl)
blanchet@42575
   822
                o result_type_of_decl) decls
blanchet@42575
   823
      | _ => false
blanchet@42575
   824
  in
blanchet@42575
   825
    map2 (problem_line_for_sym_decl ctxt type_sys need_bound_types s)
blanchet@42575
   826
         (0 upto n - 1) decls
blanchet@42544
   827
  end
blanchet@42574
   828
fun problem_lines_for_sym_decl_table ctxt type_sys sym_decl_tab =
blanchet@42574
   829
  Symtab.fold_rev (append o problem_lines_for_sym_decls ctxt type_sys)
blanchet@42574
   830
                  sym_decl_tab []
blanchet@42543
   831
blanchet@42543
   832
fun add_tff_types_in_formula (AQuant (_, xs, phi)) =
blanchet@42543
   833
    union (op =) (map_filter snd xs) #> add_tff_types_in_formula phi
blanchet@42543
   834
  | add_tff_types_in_formula (AConn (_, phis)) =
blanchet@42543
   835
    fold add_tff_types_in_formula phis
blanchet@42543
   836
  | add_tff_types_in_formula (AAtom _) = I
blanchet@42539
   837
blanchet@42562
   838
fun add_tff_types_in_problem_line (Decl (_, _, arg_Ts, res_T)) =
blanchet@42562
   839
    union (op =) (res_T :: arg_Ts)
blanchet@42543
   840
  | add_tff_types_in_problem_line (Formula (_, _, _, phi, _, _)) =
blanchet@42543
   841
    add_tff_types_in_formula phi
blanchet@42543
   842
blanchet@42543
   843
fun tff_types_in_problem problem =
blanchet@42543
   844
  fold (fold add_tff_types_in_problem_line o snd) problem []
blanchet@42543
   845
blanchet@42545
   846
fun decl_line_for_tff_type (s, s') =
blanchet@42568
   847
  Decl (type_decl_prefix ^ ascii_of s, (s, s'), [], `I tptp_tff_type_of_types)
blanchet@42543
   848
blanchet@42543
   849
val type_declsN = "Types"
blanchet@42544
   850
val sym_declsN = "Symbol types"
blanchet@41157
   851
val factsN = "Relevant facts"
blanchet@41157
   852
val class_relsN = "Class relationships"
blanchet@42543
   853
val aritiesN = "Arities"
blanchet@41157
   854
val helpersN = "Helper facts"
blanchet@41157
   855
val conjsN = "Conjectures"
blanchet@41313
   856
val free_typesN = "Type variables"
blanchet@41157
   857
blanchet@41157
   858
fun offset_of_heading_in_problem _ [] j = j
blanchet@41157
   859
  | offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
blanchet@41157
   860
    if heading = needle then j
blanchet@41157
   861
    else offset_of_heading_in_problem needle problem (j + length lines)
blanchet@41157
   862
blanchet@42568
   863
fun prepare_atp_problem ctxt type_sys explicit_apply hyp_ts concl_t facts =
blanchet@38282
   864
  let
blanchet@41313
   865
    val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
blanchet@41134
   866
      translate_formulas ctxt type_sys hyp_ts concl_t facts
blanchet@42563
   867
    val sym_tab = conjs @ facts |> sym_table_for_facts explicit_apply
blanchet@42565
   868
    val (conjs, facts) =
blanchet@42573
   869
      (conjs, facts) |> pairself (map (repair_fact type_sys sym_tab))
blanchet@42573
   870
    val repaired_sym_tab = conjs @ facts |> sym_table_for_facts false
blanchet@42561
   871
    val sym_decl_lines =
blanchet@42574
   872
      conjs @ facts
blanchet@42574
   873
      |> sym_decl_table_for_facts type_sys repaired_sym_tab
blanchet@42574
   874
      |> problem_lines_for_sym_decl_table ctxt type_sys
blanchet@42573
   875
    val helpers =
blanchet@42573
   876
      helper_facts_for_sym_table ctxt type_sys repaired_sym_tab
blanchet@42573
   877
      |> map (repair_fact type_sys sym_tab)
blanchet@42522
   878
    (* Reordering these might confuse the proof reconstruction code or the SPASS
blanchet@42522
   879
       Flotter hack. *)
blanchet@38282
   880
    val problem =
blanchet@42561
   881
      [(sym_declsN, sym_decl_lines),
blanchet@42545
   882
       (factsN, map (formula_line_for_fact ctxt fact_prefix type_sys)
blanchet@42180
   883
                    (0 upto length facts - 1 ~~ facts)),
blanchet@42545
   884
       (class_relsN, map formula_line_for_class_rel_clause class_rel_clauses),
blanchet@42545
   885
       (aritiesN, map formula_line_for_arity_clause arity_clauses),
blanchet@42561
   886
       (helpersN, map (formula_line_for_fact ctxt helper_prefix type_sys)
blanchet@42563
   887
                      (0 upto length helpers - 1 ~~ helpers)
blanchet@42563
   888
                  |> (if type_sys = Tags false then cons (ti_ti_helper_fact ())
blanchet@42563
   889
                      else I)),
blanchet@42545
   890
       (conjsN, map (formula_line_for_conjecture ctxt type_sys) conjs),
blanchet@42545
   891
       (free_typesN, formula_lines_for_free_types type_sys (facts @ conjs))]
blanchet@42543
   892
    val problem =
blanchet@42561
   893
      problem
blanchet@42561
   894
      |> (if type_sys = Many_Typed then
blanchet@42561
   895
            cons (type_declsN,
blanchet@42561
   896
                  map decl_line_for_tff_type (tff_types_in_problem problem))
blanchet@42561
   897
          else
blanchet@42561
   898
            I)
blanchet@42568
   899
    val (problem, pool) = problem |> nice_atp_problem (!readable_names)
blanchet@38282
   900
  in
blanchet@38282
   901
    (problem,
blanchet@38282
   902
     case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
blanchet@42541
   903
     offset_of_heading_in_problem factsN problem 0,
blanchet@41157
   904
     offset_of_heading_in_problem conjsN problem 0,
blanchet@41157
   905
     fact_names |> Vector.fromList)
blanchet@38282
   906
  end
blanchet@38282
   907
blanchet@41313
   908
(* FUDGE *)
blanchet@41313
   909
val conj_weight = 0.0
blanchet@41770
   910
val hyp_weight = 0.1
blanchet@41770
   911
val fact_min_weight = 0.2
blanchet@41313
   912
val fact_max_weight = 1.0
blanchet@41313
   913
blanchet@41313
   914
fun add_term_weights weight (ATerm (s, tms)) =
blanchet@41313
   915
  (not (is_atp_variable s) andalso s <> "equal") ? Symtab.default (s, weight)
blanchet@41313
   916
  #> fold (add_term_weights weight) tms
blanchet@42538
   917
fun add_problem_line_weights weight (Formula (_, _, _, phi, _, _)) =
blanchet@42542
   918
    formula_fold (add_term_weights weight) phi
blanchet@42528
   919
  | add_problem_line_weights _ _ = I
blanchet@41313
   920
blanchet@41313
   921
fun add_conjectures_weights [] = I
blanchet@41313
   922
  | add_conjectures_weights conjs =
blanchet@41313
   923
    let val (hyps, conj) = split_last conjs in
blanchet@41313
   924
      add_problem_line_weights conj_weight conj
blanchet@41313
   925
      #> fold (add_problem_line_weights hyp_weight) hyps
blanchet@41313
   926
    end
blanchet@41313
   927
blanchet@41313
   928
fun add_facts_weights facts =
blanchet@41313
   929
  let
blanchet@41313
   930
    val num_facts = length facts
blanchet@41313
   931
    fun weight_of j =
blanchet@41313
   932
      fact_min_weight + (fact_max_weight - fact_min_weight) * Real.fromInt j
blanchet@41313
   933
                        / Real.fromInt num_facts
blanchet@41313
   934
  in
blanchet@41313
   935
    map weight_of (0 upto num_facts - 1) ~~ facts
blanchet@41313
   936
    |> fold (uncurry add_problem_line_weights)
blanchet@41313
   937
  end
blanchet@41313
   938
blanchet@41313
   939
(* Weights are from 0.0 (most important) to 1.0 (least important). *)
blanchet@41313
   940
fun atp_problem_weights problem =
blanchet@41313
   941
  Symtab.empty
blanchet@41313
   942
  |> add_conjectures_weights (these (AList.lookup (op =) problem conjsN))
blanchet@41313
   943
  |> add_facts_weights (these (AList.lookup (op =) problem factsN))
blanchet@41313
   944
  |> Symtab.dest
blanchet@41726
   945
  |> sort (prod_ord Real.compare string_ord o pairself swap)
blanchet@41313
   946
blanchet@38282
   947
end;