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