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