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