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