src/HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
author blanchet
Sun May 01 18:37:23 2011 +0200 (2011-05-01)
changeset 42522 413b56894f82
parent 42521 02df3b78a438
child 42523 08346ea46a59
permissions -rw-r--r--
close ATP formulas universally earlier, so that we can add type predicates
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@41134
    16
    Tags of bool |
blanchet@42237
    17
    Args |
blanchet@42227
    18
    Mangled |
blanchet@41134
    19
    No_Types
blanchet@41134
    20
blanchet@42520
    21
  val risky_overloaded_args : bool Unsynchronized.ref
blanchet@40204
    22
  val fact_prefix : string
blanchet@38282
    23
  val conjecture_prefix : string
blanchet@42449
    24
  val is_type_system_sound : type_system -> bool
blanchet@42449
    25
  val type_system_types_dangerous_types : type_system -> bool
blanchet@41136
    26
  val num_atp_type_args : theory -> type_system -> string -> int
blanchet@41088
    27
  val translate_atp_fact :
blanchet@41990
    28
    Proof.context -> bool -> (string * 'a) * thm
blanchet@41091
    29
    -> translated_formula option * ((string * 'a) * thm)
blanchet@42227
    30
  val unmangled_const : string -> string * string fo_term list
blanchet@40059
    31
  val prepare_atp_problem :
blanchet@42521
    32
    Proof.context -> bool -> type_system -> bool -> term list -> term
blanchet@41091
    33
    -> (translated_formula option * ((string * 'a) * thm)) list
blanchet@38818
    34
    -> string problem * string Symtab.table * int * (string * 'a) list vector
blanchet@41313
    35
  val atp_problem_weights : string problem -> (string * real) list
blanchet@38282
    36
end;
blanchet@38282
    37
blanchet@41140
    38
structure Sledgehammer_ATP_Translate : SLEDGEHAMMER_ATP_TRANSLATE =
blanchet@38282
    39
struct
blanchet@38282
    40
blanchet@38282
    41
open ATP_Problem
blanchet@39494
    42
open Metis_Translate
blanchet@38282
    43
open Sledgehammer_Util
blanchet@38282
    44
blanchet@41138
    45
(* FIXME: Remove references once appropriate defaults have been determined
blanchet@41138
    46
   empirically. *)
blanchet@42520
    47
val risky_overloaded_args = Unsynchronized.ref false
blanchet@41138
    48
blanchet@40204
    49
val fact_prefix = "fact_"
blanchet@38282
    50
val conjecture_prefix = "conj_"
blanchet@38282
    51
val helper_prefix = "help_"
blanchet@38282
    52
val class_rel_clause_prefix = "clrel_";
blanchet@38282
    53
val arity_clause_prefix = "arity_"
blanchet@39975
    54
val tfree_prefix = "tfree_"
blanchet@38282
    55
blanchet@38282
    56
(* Freshness almost guaranteed! *)
blanchet@38282
    57
val sledgehammer_weak_prefix = "Sledgehammer:"
blanchet@38282
    58
blanchet@40114
    59
type translated_formula =
blanchet@38752
    60
  {name: string,
blanchet@38752
    61
   kind: kind,
blanchet@38752
    62
   combformula: (name, combterm) formula,
blanchet@38752
    63
   ctypes_sorts: typ list}
blanchet@38282
    64
blanchet@41134
    65
datatype type_system =
blanchet@41134
    66
  Tags of bool |
blanchet@42237
    67
  Args |
blanchet@42227
    68
  Mangled |
blanchet@41134
    69
  No_Types
blanchet@41134
    70
blanchet@42449
    71
fun is_type_system_sound (Tags true) = true
blanchet@42449
    72
  | is_type_system_sound _ = false
blanchet@42449
    73
blanchet@42449
    74
fun type_system_types_dangerous_types (Tags _) = true
blanchet@42449
    75
  | type_system_types_dangerous_types _ = false
blanchet@41138
    76
blanchet@41136
    77
(* This is an approximation. If it returns "true" for a constant that isn't
blanchet@41136
    78
   overloaded (i.e., that has one uniform definition), needless clutter is
blanchet@41136
    79
   generated; if it returns "false" for an overloaded constant, the ATP gets a
blanchet@41136
    80
   license to do unsound reasoning if the type system is "overloaded_args". *)
blanchet@41136
    81
fun is_overloaded thy s =
blanchet@41748
    82
  not (s = @{const_name HOL.eq}) andalso
blanchet@41748
    83
  not (s = @{const_name Metis.fequal}) andalso
blanchet@42520
    84
  (not (!risky_overloaded_args) orelse s = @{const_name finite} orelse
blanchet@41147
    85
   length (Defs.specifications_of (Theory.defs_of thy) s) > 1)
blanchet@41136
    86
blanchet@41136
    87
fun needs_type_args thy type_sys s =
blanchet@41136
    88
  case type_sys of
blanchet@41138
    89
    Tags full_types => not full_types andalso is_overloaded thy s
blanchet@42237
    90
  | Args => is_overloaded thy s
blanchet@42227
    91
  | Mangled => true
blanchet@41136
    92
  | No_Types => false
blanchet@41136
    93
blanchet@42227
    94
datatype type_arg_policy = No_Type_Args | Explicit_Type_Args | Mangled_Types
blanchet@42227
    95
blanchet@42227
    96
fun type_arg_policy thy type_sys s =
blanchet@42227
    97
  if needs_type_args thy type_sys s then
blanchet@42227
    98
    if type_sys = Mangled then Mangled_Types else Explicit_Type_Args
blanchet@42227
    99
  else
blanchet@42227
   100
    No_Type_Args
blanchet@42227
   101
blanchet@41136
   102
fun num_atp_type_args thy type_sys s =
blanchet@42227
   103
  if type_arg_policy thy type_sys s = Explicit_Type_Args then
blanchet@42227
   104
    num_type_args thy s
blanchet@42227
   105
  else
blanchet@42227
   106
    0
blanchet@41136
   107
blanchet@42353
   108
fun atp_type_literals_for_types type_sys kind Ts =
blanchet@42353
   109
  if type_sys = No_Types then
blanchet@42353
   110
    []
blanchet@42353
   111
  else
blanchet@42353
   112
    Ts |> type_literals_for_types
blanchet@42353
   113
       |> filter (fn TyLitVar _ => kind <> Conjecture
blanchet@42353
   114
                   | TyLitFree _ => kind = Conjecture)
blanchet@41137
   115
blanchet@38282
   116
fun mk_anot phi = AConn (ANot, [phi])
blanchet@38282
   117
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
blanchet@38282
   118
fun mk_ahorn [] phi = phi
blanchet@38282
   119
  | mk_ahorn (phi :: phis) psi =
blanchet@38282
   120
    AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
blanchet@42522
   121
fun mk_aquant _ [] phi = phi
blanchet@42522
   122
  | mk_aquant q xs (phi as AQuant (q', xs', phi')) =
blanchet@42522
   123
    if q = q' then AQuant (q, xs @ xs', phi') else AQuant (q, xs, phi)
blanchet@42522
   124
  | mk_aquant q xs phi = AQuant (q, xs, phi)
blanchet@38282
   125
blanchet@42522
   126
fun close_universally atom_vars phi =
blanchet@41145
   127
  let
blanchet@41145
   128
    fun formula_vars bounds (AQuant (_, xs, phi)) =
blanchet@41145
   129
        formula_vars (xs @ bounds) phi
blanchet@41145
   130
      | formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
blanchet@42522
   131
      | formula_vars bounds (AAtom tm) =
blanchet@42522
   132
        union (op =) (atom_vars tm [] |> subtract (op =) bounds)
blanchet@42522
   133
  in mk_aquant AForall (formula_vars [] phi []) phi end
blanchet@42522
   134
blanchet@42522
   135
fun combterm_vars (CombApp (ct, cu)) = fold combterm_vars [ct, cu]
blanchet@42522
   136
  | combterm_vars (CombConst _) = I
blanchet@42522
   137
  | combterm_vars (CombVar (name, _)) = insert (op =) name
blanchet@42522
   138
val close_combformula_universally = close_universally combterm_vars
blanchet@42522
   139
blanchet@42522
   140
fun term_vars (ATerm (name as (s, _), tms)) =
blanchet@42522
   141
  is_atp_variable s ? insert (op =) name #> fold term_vars tms
blanchet@42522
   142
val close_formula_universally = close_universally term_vars
blanchet@41145
   143
blanchet@41140
   144
fun combformula_for_prop thy eq_as_iff =
blanchet@38282
   145
  let
blanchet@41140
   146
    fun do_term bs t ts =
blanchet@41140
   147
      combterm_from_term thy bs (Envir.eta_contract t)
blanchet@41140
   148
      |>> AAtom ||> union (op =) ts
blanchet@38282
   149
    fun do_quant bs q s T t' =
blanchet@38518
   150
      let val s = Name.variant (map fst bs) s in
blanchet@38518
   151
        do_formula ((s, T) :: bs) t'
blanchet@38518
   152
        #>> (fn phi => AQuant (q, [`make_bound_var s], phi))
blanchet@38518
   153
      end
blanchet@38282
   154
    and do_conn bs c t1 t2 =
blanchet@38282
   155
      do_formula bs t1 ##>> do_formula bs t2
blanchet@38282
   156
      #>> (fn (phi1, phi2) => AConn (c, [phi1, phi2]))
blanchet@38282
   157
    and do_formula bs t =
blanchet@38282
   158
      case t of
blanchet@38282
   159
        @{const Not} $ t1 =>
blanchet@38282
   160
        do_formula bs t1 #>> (fn phi => AConn (ANot, [phi]))
blanchet@38282
   161
      | Const (@{const_name All}, _) $ Abs (s, T, t') =>
blanchet@38282
   162
        do_quant bs AForall s T t'
blanchet@38282
   163
      | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
blanchet@38282
   164
        do_quant bs AExists s T t'
haftmann@38795
   165
      | @{const HOL.conj} $ t1 $ t2 => do_conn bs AAnd t1 t2
haftmann@38795
   166
      | @{const HOL.disj} $ t1 $ t2 => do_conn bs AOr t1 t2
haftmann@38786
   167
      | @{const HOL.implies} $ t1 $ t2 => do_conn bs AImplies t1 t2
haftmann@38864
   168
      | Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
blanchet@41140
   169
        if eq_as_iff then do_conn bs AIff t1 t2 else do_term bs t
blanchet@41140
   170
      | _ => do_term bs t
blanchet@38282
   171
  in do_formula [] end
blanchet@38282
   172
blanchet@38618
   173
val presimplify_term = prop_of o Meson.presimplify oo Skip_Proof.make_thm
blanchet@38282
   174
wenzelm@41491
   175
fun concealed_bound_name j = sledgehammer_weak_prefix ^ string_of_int j
blanchet@38282
   176
fun conceal_bounds Ts t =
blanchet@38282
   177
  subst_bounds (map (Free o apfst concealed_bound_name)
blanchet@38282
   178
                    (0 upto length Ts - 1 ~~ Ts), t)
blanchet@38282
   179
fun reveal_bounds Ts =
blanchet@38282
   180
  subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
blanchet@38282
   181
                    (0 upto length Ts - 1 ~~ Ts))
blanchet@38282
   182
blanchet@38608
   183
(* Removes the lambdas from an equation of the form "t = (%x. u)".
blanchet@39890
   184
   (Cf. "extensionalize_theorem" in "Meson_Clausify".) *)
blanchet@38608
   185
fun extensionalize_term t =
blanchet@38608
   186
  let
blanchet@38608
   187
    fun aux j (@{const Trueprop} $ t') = @{const Trueprop} $ aux j t'
blanchet@38608
   188
      | aux j (t as Const (s, Type (_, [Type (_, [_, T']),
blanchet@38608
   189
                                        Type (_, [_, res_T])]))
blanchet@38608
   190
                    $ t2 $ Abs (var_s, var_T, t')) =
haftmann@38864
   191
        if s = @{const_name HOL.eq} orelse s = @{const_name "=="} then
blanchet@38608
   192
          let val var_t = Var ((var_s, j), var_T) in
blanchet@38608
   193
            Const (s, T' --> T' --> res_T)
blanchet@38608
   194
              $ betapply (t2, var_t) $ subst_bound (var_t, t')
blanchet@38608
   195
            |> aux (j + 1)
blanchet@38608
   196
          end
blanchet@38608
   197
        else
blanchet@38608
   198
          t
blanchet@38608
   199
      | aux _ t = t
blanchet@38608
   200
  in aux (maxidx_of_term t + 1) t end
blanchet@38608
   201
blanchet@38282
   202
fun introduce_combinators_in_term ctxt kind t =
wenzelm@42361
   203
  let val thy = Proof_Context.theory_of ctxt in
blanchet@38491
   204
    if Meson.is_fol_term thy t then
blanchet@38491
   205
      t
blanchet@38491
   206
    else
blanchet@38491
   207
      let
blanchet@38491
   208
        fun aux Ts t =
blanchet@38491
   209
          case t of
blanchet@38491
   210
            @{const Not} $ t1 => @{const Not} $ aux Ts t1
blanchet@38491
   211
          | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
blanchet@38491
   212
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38652
   213
          | (t0 as Const (@{const_name All}, _)) $ t1 =>
blanchet@38652
   214
            aux Ts (t0 $ eta_expand Ts t1 1)
blanchet@38491
   215
          | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
blanchet@38491
   216
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38652
   217
          | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
blanchet@38652
   218
            aux Ts (t0 $ eta_expand Ts t1 1)
haftmann@38795
   219
          | (t0 as @{const HOL.conj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38795
   220
          | (t0 as @{const HOL.disj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38786
   221
          | (t0 as @{const HOL.implies}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38864
   222
          | (t0 as Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])))
blanchet@38491
   223
              $ t1 $ t2 =>
blanchet@38491
   224
            t0 $ aux Ts t1 $ aux Ts t2
blanchet@38491
   225
          | _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
blanchet@38491
   226
                   t
blanchet@38491
   227
                 else
blanchet@38491
   228
                   t |> conceal_bounds Ts
blanchet@38491
   229
                     |> Envir.eta_contract
blanchet@38491
   230
                     |> cterm_of thy
blanchet@39890
   231
                     |> Meson_Clausify.introduce_combinators_in_cterm
blanchet@38491
   232
                     |> prop_of |> Logic.dest_equals |> snd
blanchet@38491
   233
                     |> reveal_bounds Ts
blanchet@39370
   234
        val (t, ctxt') = Variable.import_terms true [t] ctxt |>> the_single
blanchet@38491
   235
      in t |> aux [] |> singleton (Variable.export_terms ctxt' ctxt) end
blanchet@38491
   236
      handle THM _ =>
blanchet@38491
   237
             (* A type variable of sort "{}" will make abstraction fail. *)
blanchet@38613
   238
             if kind = Conjecture then HOLogic.false_const
blanchet@38613
   239
             else HOLogic.true_const
blanchet@38491
   240
  end
blanchet@38282
   241
blanchet@38282
   242
(* Metis's use of "resolve_tac" freezes the schematic variables. We simulate the
blanchet@42353
   243
   same in Sledgehammer to prevent the discovery of unreplayable proofs. *)
blanchet@38282
   244
fun freeze_term t =
blanchet@38282
   245
  let
blanchet@38282
   246
    fun aux (t $ u) = aux t $ aux u
blanchet@38282
   247
      | aux (Abs (s, T, t)) = Abs (s, T, aux t)
blanchet@38282
   248
      | aux (Var ((s, i), T)) =
blanchet@38282
   249
        Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
blanchet@38282
   250
      | aux t = t
blanchet@38282
   251
  in t |> exists_subterm is_Var t ? aux end
blanchet@38282
   252
blanchet@40204
   253
(* making fact and conjecture formulas *)
blanchet@41140
   254
fun make_formula ctxt eq_as_iff presimp name kind t =
blanchet@38282
   255
  let
wenzelm@42361
   256
    val thy = Proof_Context.theory_of ctxt
blanchet@38608
   257
    val t = t |> Envir.beta_eta_contract
blanchet@38652
   258
              |> transform_elim_term
blanchet@41211
   259
              |> Object_Logic.atomize_term thy
blanchet@38652
   260
    val need_trueprop = (fastype_of t = HOLogic.boolT)
blanchet@38652
   261
    val t = t |> need_trueprop ? HOLogic.mk_Trueprop
blanchet@38282
   262
              |> extensionalize_term
blanchet@38282
   263
              |> presimp ? presimplify_term thy
blanchet@38282
   264
              |> perhaps (try (HOLogic.dest_Trueprop))
blanchet@38282
   265
              |> introduce_combinators_in_term ctxt kind
blanchet@38613
   266
              |> kind <> Axiom ? freeze_term
blanchet@41140
   267
    val (combformula, ctypes_sorts) = combformula_for_prop thy eq_as_iff t []
blanchet@38282
   268
  in
blanchet@38752
   269
    {name = name, combformula = combformula, kind = kind,
blanchet@38752
   270
     ctypes_sorts = ctypes_sorts}
blanchet@38282
   271
  end
blanchet@38282
   272
blanchet@41990
   273
fun make_fact ctxt keep_trivial eq_as_iff presimp ((name, _), th) =
blanchet@41990
   274
  case (keep_trivial,
blanchet@41990
   275
        make_formula ctxt eq_as_iff presimp name Axiom (prop_of th)) of
blanchet@41990
   276
    (false, {combformula = AAtom (CombConst (("c_True", _), _, _)), ...}) =>
blanchet@41990
   277
    NONE
blanchet@41990
   278
  | (_, formula) => SOME formula
blanchet@38613
   279
fun make_conjecture ctxt ts =
blanchet@38613
   280
  let val last = length ts - 1 in
wenzelm@41491
   281
    map2 (fn j => make_formula ctxt true true (string_of_int j)
blanchet@38613
   282
                               (if j = last then Conjecture else Hypothesis))
blanchet@38613
   283
         (0 upto last) ts
blanchet@38613
   284
  end
blanchet@38282
   285
blanchet@38282
   286
(** Helper facts **)
blanchet@38282
   287
blanchet@41313
   288
fun fold_formula f (AQuant (_, _, phi)) = fold_formula f phi
blanchet@41313
   289
  | fold_formula f (AConn (_, phis)) = fold (fold_formula f) phis
blanchet@41313
   290
  | fold_formula f (AAtom tm) = f tm
blanchet@41313
   291
blanchet@41140
   292
fun count_term (ATerm ((s, _), tms)) =
blanchet@41140
   293
  (if is_atp_variable s then I
blanchet@41140
   294
   else Symtab.map_entry s (Integer.add 1))
blanchet@41140
   295
  #> fold count_term tms
wenzelm@41406
   296
fun count_formula x = fold_formula count_term x
blanchet@38282
   297
blanchet@38282
   298
val init_counters =
blanchet@41140
   299
  metis_helpers |> map fst |> sort_distinct string_ord |> map (rpair 0)
blanchet@41140
   300
  |> Symtab.make
blanchet@38282
   301
blanchet@42521
   302
fun get_helper_facts ctxt type_sys formulas =
blanchet@38282
   303
  let
blanchet@42449
   304
    val no_dangerous_types = type_system_types_dangerous_types type_sys
blanchet@41140
   305
    val ct = init_counters |> fold count_formula formulas
blanchet@41140
   306
    fun is_used s = the (Symtab.lookup ct s) > 0
blanchet@41140
   307
    fun dub c needs_full_types (th, j) =
blanchet@41140
   308
      ((c ^ "_" ^ string_of_int j ^ (if needs_full_types then "ft" else ""),
blanchet@41140
   309
        false), th)
blanchet@41990
   310
    fun make_facts eq_as_iff = map_filter (make_fact ctxt false eq_as_iff false)
blanchet@38282
   311
  in
blanchet@41145
   312
    (metis_helpers
blanchet@41145
   313
     |> filter (is_used o fst)
blanchet@41145
   314
     |> maps (fn (c, (needs_full_types, ths)) =>
blanchet@41145
   315
                 if needs_full_types andalso not no_dangerous_types then
blanchet@41145
   316
                   []
blanchet@41145
   317
                 else
blanchet@41145
   318
                   ths ~~ (1 upto length ths)
blanchet@41145
   319
                   |> map (dub c needs_full_types)
blanchet@41145
   320
                   |> make_facts (not needs_full_types)),
blanchet@41145
   321
     if type_sys = Tags false then
blanchet@41145
   322
       let
blanchet@41145
   323
         fun var s = ATerm (`I s, [])
blanchet@41145
   324
         fun tag tm = ATerm (`I type_tag_name, [var "X", tm])
blanchet@41145
   325
       in
blanchet@41145
   326
         [Fof (helper_prefix ^ ascii_of "ti_ti", Axiom,
blanchet@41145
   327
               AAtom (ATerm (`I "equal", [tag (tag (var "Y")), tag (var "Y")]))
blanchet@42522
   328
               |> close_formula_universally, NONE)]
blanchet@41145
   329
       end
blanchet@41145
   330
     else
blanchet@41145
   331
       [])
blanchet@38282
   332
  end
blanchet@38282
   333
blanchet@41990
   334
fun translate_atp_fact ctxt keep_trivial =
blanchet@41990
   335
  `(make_fact ctxt keep_trivial true true)
blanchet@39004
   336
blanchet@41134
   337
fun translate_formulas ctxt type_sys hyp_ts concl_t rich_facts =
blanchet@38282
   338
  let
wenzelm@42361
   339
    val thy = Proof_Context.theory_of ctxt
blanchet@41091
   340
    val fact_ts = map (prop_of o snd o snd) rich_facts
blanchet@41091
   341
    val (facts, fact_names) =
blanchet@41091
   342
      rich_facts
blanchet@41091
   343
      |> map_filter (fn (NONE, _) => NONE
blanchet@41091
   344
                      | (SOME fact, (name, _)) => SOME (fact, name))
blanchet@41091
   345
      |> ListPair.unzip
blanchet@40204
   346
    (* Remove existing facts from the conjecture, as this can dramatically
blanchet@39005
   347
       boost an ATP's performance (for some reason). *)
blanchet@40204
   348
    val hyp_ts = hyp_ts |> filter_out (member (op aconv) fact_ts)
blanchet@38282
   349
    val goal_t = Logic.list_implies (hyp_ts, concl_t)
blanchet@42353
   350
    val all_ts = goal_t :: fact_ts
blanchet@42353
   351
    val subs = tfree_classes_of_terms all_ts
blanchet@42353
   352
    val supers = tvar_classes_of_terms all_ts
blanchet@42353
   353
    val tycons = type_consts_of_terms thy all_ts
blanchet@41313
   354
    val conjs = make_conjecture ctxt (hyp_ts @ [concl_t])
blanchet@41137
   355
    val (supers', arity_clauses) =
blanchet@41137
   356
      if type_sys = No_Types then ([], [])
blanchet@41137
   357
      else make_arity_clauses thy tycons supers
blanchet@38282
   358
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@38282
   359
  in
blanchet@41313
   360
    (fact_names |> map single, (conjs, facts, class_rel_clauses, arity_clauses))
blanchet@38282
   361
  end
blanchet@38282
   362
blanchet@41138
   363
fun tag_with_type ty t = ATerm (`I type_tag_name, [ty, t])
blanchet@38282
   364
blanchet@38282
   365
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
blanchet@38282
   366
  | fo_term_for_combtyp (CombTFree name) = ATerm (name, [])
blanchet@38282
   367
  | fo_term_for_combtyp (CombType (name, tys)) =
blanchet@38282
   368
    ATerm (name, map fo_term_for_combtyp tys)
blanchet@38282
   369
blanchet@38282
   370
fun fo_literal_for_type_literal (TyLitVar (class, name)) =
blanchet@38282
   371
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@38282
   372
  | fo_literal_for_type_literal (TyLitFree (class, name)) =
blanchet@38282
   373
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@38282
   374
blanchet@38282
   375
fun formula_for_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
blanchet@38282
   376
blanchet@41138
   377
(* Finite types such as "unit", "bool", "bool * bool", and "bool => bool" are
blanchet@41138
   378
   considered dangerous because their "exhaust" properties can easily lead to
blanchet@41138
   379
   unsound ATP proofs. The checks below are an (unsound) approximation of
blanchet@41138
   380
   finiteness. *)
blanchet@41138
   381
blanchet@41138
   382
fun is_dtyp_dangerous _ (Datatype_Aux.DtTFree _) = true
blanchet@41138
   383
  | is_dtyp_dangerous ctxt (Datatype_Aux.DtType (s, Us)) =
blanchet@41138
   384
    is_type_constr_dangerous ctxt s andalso forall (is_dtyp_dangerous ctxt) Us
blanchet@41138
   385
  | is_dtyp_dangerous _ (Datatype_Aux.DtRec _) = false
blanchet@41138
   386
and is_type_dangerous ctxt (Type (s, Ts)) =
blanchet@41138
   387
    is_type_constr_dangerous ctxt s andalso forall (is_type_dangerous ctxt) Ts
blanchet@41140
   388
  | is_type_dangerous _ _ = false
blanchet@41138
   389
and is_type_constr_dangerous ctxt s =
wenzelm@42361
   390
  let val thy = Proof_Context.theory_of ctxt in
blanchet@41138
   391
    case Datatype_Data.get_info thy s of
blanchet@41138
   392
      SOME {descr, ...} =>
blanchet@41138
   393
      forall (fn (_, (_, _, constrs)) =>
blanchet@41138
   394
                 forall (forall (is_dtyp_dangerous ctxt) o snd) constrs) descr
blanchet@41138
   395
    | NONE =>
blanchet@41138
   396
      case Typedef.get_info ctxt s of
blanchet@41138
   397
        ({rep_type, ...}, _) :: _ => is_type_dangerous ctxt rep_type
blanchet@41138
   398
      | [] => true
blanchet@41138
   399
  end
blanchet@41138
   400
blanchet@41138
   401
fun is_combtyp_dangerous ctxt (CombType ((s, _), tys)) =
blanchet@41138
   402
    (case strip_prefix_and_unascii type_const_prefix s of
blanchet@41138
   403
       SOME s' => forall (is_combtyp_dangerous ctxt) tys andalso
blanchet@41138
   404
                  is_type_constr_dangerous ctxt (invert_const s')
blanchet@41138
   405
     | NONE => false)
blanchet@41138
   406
  | is_combtyp_dangerous _ _ = false
blanchet@41138
   407
blanchet@41138
   408
fun should_tag_with_type ctxt (Tags full_types) ty =
blanchet@41138
   409
    full_types orelse is_combtyp_dangerous ctxt ty
blanchet@41138
   410
  | should_tag_with_type _ _ _ = false
blanchet@41138
   411
blanchet@41140
   412
val fname_table =
blanchet@41140
   413
  [("c_False", (0, ("c_fFalse", @{const_name Metis.fFalse}))),
blanchet@41140
   414
   ("c_True", (0, ("c_fTrue", @{const_name Metis.fTrue}))),
blanchet@41140
   415
   ("c_Not", (1, ("c_fNot", @{const_name Metis.fNot}))),
blanchet@41140
   416
   ("c_conj", (2, ("c_fconj", @{const_name Metis.fconj}))),
blanchet@41140
   417
   ("c_disj", (2, ("c_fdisj", @{const_name Metis.fdisj}))),
blanchet@41140
   418
   ("c_implies", (2, ("c_fimplies", @{const_name Metis.fimplies}))),
blanchet@41140
   419
   ("equal", (2, ("c_fequal", @{const_name Metis.fequal})))]
blanchet@41140
   420
blanchet@42227
   421
(* We are crossing our fingers that it doesn't clash with anything else. *)
blanchet@42227
   422
val mangled_type_sep = "\000"
blanchet@42227
   423
blanchet@42227
   424
fun mangled_combtyp f (CombTFree name) = f name
blanchet@42236
   425
  | mangled_combtyp f (CombTVar name) =
blanchet@42236
   426
    f name (* FIXME: shouldn't happen *)
blanchet@42236
   427
    (* raise Fail "impossible schematic type variable" *)
blanchet@42227
   428
  | mangled_combtyp f (CombType (name, tys)) =
blanchet@42227
   429
    "(" ^ commas (map (mangled_combtyp f) tys) ^ ")" ^ f name
blanchet@42227
   430
blanchet@42227
   431
fun mangled_type_suffix f g tys =
blanchet@42227
   432
  fold_rev (curry (op ^) o g o prefix mangled_type_sep o mangled_combtyp f)
blanchet@42227
   433
           tys ""
blanchet@42227
   434
blanchet@42227
   435
val parse_mangled_ident =
blanchet@42227
   436
  Scan.many1 (not o member (op =) ["(", ")", ","]) >> implode
blanchet@42227
   437
blanchet@42227
   438
fun parse_mangled_type x =
blanchet@42227
   439
  ($$ "(" |-- Scan.optional parse_mangled_types [] --| $$ ")"
blanchet@42227
   440
      -- parse_mangled_ident >> (ATerm o swap)
blanchet@42227
   441
   || parse_mangled_ident >> (ATerm o rpair [])) x
blanchet@42227
   442
and parse_mangled_types x =
blanchet@42227
   443
  (parse_mangled_type ::: Scan.repeat ($$ "," |-- parse_mangled_type)) x
blanchet@42227
   444
blanchet@42227
   445
fun unmangled_type s =
blanchet@42227
   446
  s |> suffix ")" |> raw_explode
blanchet@42227
   447
    |> Scan.finite Symbol.stopper
blanchet@42227
   448
           (Scan.error (!! (fn _ => raise Fail ("unrecognized mangled type " ^
blanchet@42227
   449
                                                quote s)) parse_mangled_type))
blanchet@42227
   450
    |> fst
blanchet@42227
   451
blanchet@42227
   452
fun unmangled_const s =
blanchet@42227
   453
  let val ss = space_explode mangled_type_sep s in
blanchet@42227
   454
    (hd ss, map unmangled_type (tl ss))
blanchet@42227
   455
  end
blanchet@42227
   456
blanchet@41138
   457
fun fo_term_for_combterm ctxt type_sys =
blanchet@38282
   458
  let
wenzelm@42361
   459
    val thy = Proof_Context.theory_of ctxt
blanchet@38282
   460
    fun aux top_level u =
blanchet@38282
   461
      let
blanchet@38282
   462
        val (head, args) = strip_combterm_comb u
blanchet@38282
   463
        val (x, ty_args) =
blanchet@38282
   464
          case head of
blanchet@38282
   465
            CombConst (name as (s, s'), _, ty_args) =>
blanchet@41140
   466
            (case AList.lookup (op =) fname_table s of
blanchet@41140
   467
               SOME (n, fname) =>
blanchet@41150
   468
               (if top_level andalso length args = n then
blanchet@41150
   469
                  case s of
blanchet@41150
   470
                    "c_False" => ("$false", s')
blanchet@41150
   471
                  | "c_True" => ("$true", s')
blanchet@41150
   472
                  | _ => name
blanchet@41150
   473
                else
blanchet@41150
   474
                  fname, [])
blanchet@41140
   475
             | NONE =>
blanchet@41140
   476
               case strip_prefix_and_unascii const_prefix s of
blanchet@41140
   477
                 NONE => (name, ty_args)
blanchet@41140
   478
               | SOME s'' =>
blanchet@42227
   479
                 let val s'' = invert_const s'' in
blanchet@42227
   480
                   case type_arg_policy thy type_sys s'' of
blanchet@42227
   481
                     No_Type_Args => (name, [])
blanchet@42227
   482
                   | Explicit_Type_Args => (name, ty_args)
blanchet@42227
   483
                   | Mangled_Types =>
blanchet@42227
   484
                     ((s ^ mangled_type_suffix fst ascii_of ty_args,
blanchet@42227
   485
                       s' ^ mangled_type_suffix snd I ty_args), [])
blanchet@42227
   486
                 end)
blanchet@38282
   487
          | CombVar (name, _) => (name, [])
blanchet@38282
   488
          | CombApp _ => raise Fail "impossible \"CombApp\""
blanchet@41138
   489
        val t =
blanchet@41138
   490
          ATerm (x, map fo_term_for_combtyp ty_args @ map (aux false) args)
blanchet@41138
   491
        val ty = combtyp_of u
blanchet@38282
   492
    in
blanchet@41138
   493
      t |> (if should_tag_with_type ctxt type_sys ty then
blanchet@41138
   494
              tag_with_type (fo_term_for_combtyp ty)
blanchet@41134
   495
            else
blanchet@41134
   496
              I)
blanchet@38282
   497
    end
blanchet@38282
   498
  in aux true end
blanchet@38282
   499
blanchet@41138
   500
fun formula_for_combformula ctxt type_sys =
blanchet@38282
   501
  let
blanchet@38282
   502
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
blanchet@38282
   503
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
blanchet@41138
   504
      | aux (AAtom tm) = AAtom (fo_term_for_combterm ctxt type_sys tm)
blanchet@38282
   505
  in aux end
blanchet@38282
   506
blanchet@41138
   507
fun formula_for_fact ctxt type_sys
blanchet@40204
   508
                     ({combformula, ctypes_sorts, ...} : translated_formula) =
blanchet@38282
   509
  mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
blanchet@42353
   510
                (atp_type_literals_for_types type_sys Axiom ctypes_sorts))
blanchet@42522
   511
           (formula_for_combformula ctxt type_sys
blanchet@42522
   512
                                    (close_combformula_universally combformula))
blanchet@38282
   513
blanchet@42180
   514
(* Each fact is given a unique fact number to avoid name clashes (e.g., because
blanchet@42180
   515
   of monomorphization). The TPTP explicitly forbids name clashes, and some of
blanchet@42180
   516
   the remote provers might care. *)
blanchet@42180
   517
fun problem_line_for_fact ctxt prefix type_sys
blanchet@42180
   518
                          (j, formula as {name, kind, ...}) =
blanchet@42180
   519
  Fof (prefix ^ string_of_int j ^ "_" ^ ascii_of name, kind,
blanchet@42180
   520
       formula_for_fact ctxt type_sys formula, NONE)
blanchet@38282
   521
blanchet@38282
   522
fun problem_line_for_class_rel_clause (ClassRelClause {name, subclass,
blanchet@38282
   523
                                                       superclass, ...}) =
blanchet@38282
   524
  let val ty_arg = ATerm (("T", "T"), []) in
blanchet@38282
   525
    Fof (class_rel_clause_prefix ^ ascii_of name, Axiom,
blanchet@38282
   526
         AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
blanchet@41769
   527
                           AAtom (ATerm (superclass, [ty_arg]))]), NONE)
blanchet@38282
   528
  end
blanchet@38282
   529
blanchet@38282
   530
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
blanchet@38282
   531
    (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
blanchet@38282
   532
  | fo_literal_for_arity_literal (TVarLit (c, sort)) =
blanchet@38282
   533
    (false, ATerm (c, [ATerm (sort, [])]))
blanchet@38282
   534
blanchet@38282
   535
fun problem_line_for_arity_clause (ArityClause {name, conclLit, premLits,
blanchet@38282
   536
                                                ...}) =
blanchet@38282
   537
  Fof (arity_clause_prefix ^ ascii_of name, Axiom,
blanchet@38282
   538
       mk_ahorn (map (formula_for_fo_literal o apfst not
blanchet@38282
   539
                      o fo_literal_for_arity_literal) premLits)
blanchet@38282
   540
                (formula_for_fo_literal
blanchet@41769
   541
                     (fo_literal_for_arity_literal conclLit)), NONE)
blanchet@38282
   542
blanchet@41138
   543
fun problem_line_for_conjecture ctxt type_sys
blanchet@40114
   544
        ({name, kind, combformula, ...} : translated_formula) =
blanchet@38282
   545
  Fof (conjecture_prefix ^ name, kind,
blanchet@42522
   546
       formula_for_combformula ctxt type_sys
blanchet@42522
   547
                               (close_combformula_universally combformula),
blanchet@42522
   548
       NONE)
blanchet@38282
   549
blanchet@42353
   550
fun free_type_literals type_sys ({ctypes_sorts, ...} : translated_formula) =
blanchet@42353
   551
  ctypes_sorts |> atp_type_literals_for_types type_sys Conjecture
blanchet@41137
   552
               |> map fo_literal_for_type_literal
blanchet@38282
   553
blanchet@39975
   554
fun problem_line_for_free_type j lit =
blanchet@41769
   555
  Fof (tfree_prefix ^ string_of_int j, Hypothesis, formula_for_fo_literal lit,
blanchet@41769
   556
       NONE)
blanchet@42353
   557
fun problem_lines_for_free_types type_sys facts =
blanchet@38282
   558
  let
blanchet@42353
   559
    val litss = map (free_type_literals type_sys) facts
blanchet@38282
   560
    val lits = fold (union (op =)) litss []
blanchet@39975
   561
  in map2 problem_line_for_free_type (0 upto length lits - 1) lits end
blanchet@38282
   562
blanchet@38282
   563
(** "hBOOL" and "hAPP" **)
blanchet@38282
   564
blanchet@42520
   565
type sym_info = {min_arity: int, max_arity: int, fun_sym: bool}
blanchet@38282
   566
blanchet@38282
   567
fun consider_term top_level (ATerm ((s, _), ts)) =
blanchet@39452
   568
  (if is_atp_variable s then
blanchet@38282
   569
     I
blanchet@38282
   570
   else
blanchet@38282
   571
     let val n = length ts in
blanchet@38282
   572
       Symtab.map_default
blanchet@42520
   573
           (s, {min_arity = n, max_arity = 0, fun_sym = false})
blanchet@42520
   574
           (fn {min_arity, max_arity, fun_sym} =>
blanchet@38282
   575
               {min_arity = Int.min (n, min_arity),
blanchet@38282
   576
                max_arity = Int.max (n, max_arity),
blanchet@42520
   577
                fun_sym = fun_sym orelse not top_level})
blanchet@38282
   578
     end)
blanchet@41138
   579
  #> fold (consider_term (top_level andalso s = type_tag_name)) ts
blanchet@38282
   580
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi
blanchet@38282
   581
  | consider_formula (AConn (_, phis)) = fold consider_formula phis
blanchet@38282
   582
  | consider_formula (AAtom tm) = consider_term true tm
blanchet@38282
   583
blanchet@41769
   584
fun consider_problem_line (Fof (_, _, phi, _)) = consider_formula phi
blanchet@38282
   585
fun consider_problem problem = fold (fold consider_problem_line o snd) problem
blanchet@38282
   586
blanchet@41140
   587
(* needed for helper facts if the problem otherwise does not involve equality *)
blanchet@42520
   588
val equal_entry = ("equal", {min_arity = 2, max_arity = 2, fun_sym = false})
blanchet@41140
   589
blanchet@42520
   590
fun sym_table_for_problem explicit_apply problem =
blanchet@41140
   591
  if explicit_apply then
blanchet@41140
   592
    NONE
blanchet@41140
   593
  else
blanchet@41147
   594
    SOME (Symtab.empty |> Symtab.default equal_entry |> consider_problem problem)
blanchet@38282
   595
blanchet@41134
   596
fun min_arity_of thy type_sys NONE s =
blanchet@41138
   597
    (if s = "equal" orelse s = type_tag_name orelse
blanchet@38282
   598
        String.isPrefix type_const_prefix s orelse
blanchet@38282
   599
        String.isPrefix class_prefix s then
blanchet@38282
   600
       16383 (* large number *)
blanchet@38748
   601
     else case strip_prefix_and_unascii const_prefix s of
blanchet@42227
   602
       SOME s' =>
blanchet@42227
   603
       s' |> unmangled_const |> fst |> invert_const
blanchet@42227
   604
          |> num_atp_type_args thy type_sys
blanchet@38282
   605
     | NONE => 0)
blanchet@42520
   606
  | min_arity_of _ _ (SOME sym_tab) s =
blanchet@42520
   607
    case Symtab.lookup sym_tab s of
blanchet@42520
   608
      SOME ({min_arity, ...} : sym_info) => min_arity
blanchet@38282
   609
    | NONE => 0
blanchet@38282
   610
blanchet@38282
   611
fun full_type_of (ATerm ((s, _), [ty, _])) =
blanchet@41138
   612
    if s = type_tag_name then SOME ty else NONE
blanchet@41138
   613
  | full_type_of _ = NONE
blanchet@38282
   614
blanchet@38282
   615
fun list_hAPP_rev _ t1 [] = t1
blanchet@38282
   616
  | list_hAPP_rev NONE t1 (t2 :: ts2) =
blanchet@38282
   617
    ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
blanchet@38282
   618
  | list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
blanchet@41138
   619
    case full_type_of t2 of
blanchet@41138
   620
      SOME ty2 =>
blanchet@41138
   621
      let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
blanchet@41138
   622
                           [ty2, ty]) in
blanchet@41138
   623
        ATerm (`I "hAPP",
blanchet@41138
   624
               [tag_with_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
blanchet@41138
   625
      end
blanchet@41138
   626
    | NONE => list_hAPP_rev NONE t1 (t2 :: ts2)
blanchet@38282
   627
blanchet@42520
   628
fun repair_applications_in_term thy type_sys sym_tab =
blanchet@38282
   629
  let
blanchet@38282
   630
    fun aux opt_ty (ATerm (name as (s, _), ts)) =
blanchet@41138
   631
      if s = type_tag_name then
blanchet@38282
   632
        case ts of
blanchet@38282
   633
          [t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2])
blanchet@41138
   634
        | _ => raise Fail "malformed type tag"
blanchet@38282
   635
      else
blanchet@38282
   636
        let
blanchet@38282
   637
          val ts = map (aux NONE) ts
blanchet@42520
   638
          val (ts1, ts2) = chop (min_arity_of thy type_sys sym_tab s) ts
blanchet@38282
   639
        in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
blanchet@38282
   640
  in aux NONE end
blanchet@38282
   641
blanchet@38282
   642
fun boolify t = ATerm (`I "hBOOL", [t])
blanchet@38282
   643
blanchet@38282
   644
(* True if the constant ever appears outside of the top-level position in
blanchet@38282
   645
   literals, or if it appears with different arities (e.g., because of different
blanchet@38282
   646
   type instantiations). If false, the constant always receives all of its
blanchet@38282
   647
   arguments and is used as a predicate. *)
blanchet@42520
   648
fun is_pred_sym NONE s =
blanchet@38589
   649
    s = "equal" orelse s = "$false" orelse s = "$true" orelse
blanchet@38589
   650
    String.isPrefix type_const_prefix s orelse String.isPrefix class_prefix s
blanchet@42520
   651
  | is_pred_sym (SOME sym_tab) s =
blanchet@42520
   652
    case Symtab.lookup sym_tab s of
blanchet@42520
   653
      SOME {min_arity, max_arity, fun_sym} =>
blanchet@42520
   654
      not fun_sym andalso min_arity = max_arity
blanchet@38282
   655
    | NONE => false
blanchet@38282
   656
blanchet@42520
   657
fun repair_predicates_in_term pred_sym_tab (t as ATerm ((s, _), ts)) =
blanchet@41138
   658
  if s = type_tag_name then
blanchet@38282
   659
    case ts of
blanchet@38282
   660
      [_, t' as ATerm ((s', _), _)] =>
blanchet@42520
   661
      if is_pred_sym pred_sym_tab s' then t' else boolify t
blanchet@41138
   662
    | _ => raise Fail "malformed type tag"
blanchet@38282
   663
  else
blanchet@42520
   664
    t |> not (is_pred_sym pred_sym_tab s) ? boolify
blanchet@38282
   665
blanchet@42521
   666
fun repair_formula thy type_sys sym_tab =
blanchet@38282
   667
  let
blanchet@42520
   668
    val pred_sym_tab = case type_sys of Tags _ => NONE | _ => sym_tab
blanchet@38282
   669
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
blanchet@38282
   670
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
blanchet@38282
   671
      | aux (AAtom tm) =
blanchet@42520
   672
        AAtom (tm |> repair_applications_in_term thy type_sys sym_tab
blanchet@42520
   673
                  |> repair_predicates_in_term pred_sym_tab)
blanchet@42522
   674
  in aux #> close_formula_universally end
blanchet@38282
   675
blanchet@42521
   676
fun repair_problem_line thy type_sys sym_tab (Fof (ident, kind, phi, source)) =
blanchet@42521
   677
  Fof (ident, kind, repair_formula thy type_sys sym_tab phi, source)
blanchet@42521
   678
fun repair_problem thy = map o apsnd o map oo repair_problem_line thy
blanchet@38282
   679
blanchet@41140
   680
fun dest_Fof (Fof z) = z
blanchet@38282
   681
blanchet@41157
   682
val factsN = "Relevant facts"
blanchet@41157
   683
val class_relsN = "Class relationships"
blanchet@41157
   684
val aritiesN = "Arity declarations"
blanchet@41157
   685
val helpersN = "Helper facts"
blanchet@41157
   686
val conjsN = "Conjectures"
blanchet@41313
   687
val free_typesN = "Type variables"
blanchet@41157
   688
blanchet@41157
   689
fun offset_of_heading_in_problem _ [] j = j
blanchet@41157
   690
  | offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
blanchet@41157
   691
    if heading = needle then j
blanchet@41157
   692
    else offset_of_heading_in_problem needle problem (j + length lines)
blanchet@41157
   693
blanchet@42521
   694
fun prepare_atp_problem ctxt readable_names type_sys explicit_apply hyp_ts
blanchet@42521
   695
                        concl_t facts =
blanchet@38282
   696
  let
wenzelm@42361
   697
    val thy = Proof_Context.theory_of ctxt
blanchet@41313
   698
    val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
blanchet@41134
   699
      translate_formulas ctxt type_sys hyp_ts concl_t facts
blanchet@42522
   700
    (* Reordering these might confuse the proof reconstruction code or the SPASS
blanchet@42522
   701
       Flotter hack. *)
blanchet@38282
   702
    val problem =
blanchet@42180
   703
      [(factsN, map (problem_line_for_fact ctxt fact_prefix type_sys)
blanchet@42180
   704
                    (0 upto length facts - 1 ~~ facts)),
blanchet@41157
   705
       (class_relsN, map problem_line_for_class_rel_clause class_rel_clauses),
blanchet@41157
   706
       (aritiesN, map problem_line_for_arity_clause arity_clauses),
blanchet@41157
   707
       (helpersN, []),
blanchet@41313
   708
       (conjsN, map (problem_line_for_conjecture ctxt type_sys) conjs),
blanchet@42353
   709
       (free_typesN, problem_lines_for_free_types type_sys (facts @ conjs))]
blanchet@42520
   710
    val sym_tab = sym_table_for_problem explicit_apply problem
blanchet@42521
   711
    val problem = problem |> repair_problem thy type_sys sym_tab
blanchet@42521
   712
    val helper_facts =
blanchet@42521
   713
      get_helper_facts ctxt type_sys (maps (map (#3 o dest_Fof) o snd) problem)
blanchet@41157
   714
    val helper_lines =
blanchet@42521
   715
      helper_facts
blanchet@42180
   716
      |>> map (pair 0
blanchet@42180
   717
               #> problem_line_for_fact ctxt helper_prefix type_sys
blanchet@42521
   718
               #> repair_problem_line thy type_sys sym_tab)
blanchet@41157
   719
      |> op @
blanchet@41140
   720
    val (problem, pool) =
blanchet@41157
   721
      problem |> AList.update (op =) (helpersN, helper_lines)
blanchet@41140
   722
              |> nice_atp_problem readable_names
blanchet@38282
   723
  in
blanchet@38282
   724
    (problem,
blanchet@38282
   725
     case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
blanchet@41157
   726
     offset_of_heading_in_problem conjsN problem 0,
blanchet@41157
   727
     fact_names |> Vector.fromList)
blanchet@38282
   728
  end
blanchet@38282
   729
blanchet@41313
   730
(* FUDGE *)
blanchet@41313
   731
val conj_weight = 0.0
blanchet@41770
   732
val hyp_weight = 0.1
blanchet@41770
   733
val fact_min_weight = 0.2
blanchet@41313
   734
val fact_max_weight = 1.0
blanchet@41313
   735
blanchet@41313
   736
fun add_term_weights weight (ATerm (s, tms)) =
blanchet@41313
   737
  (not (is_atp_variable s) andalso s <> "equal") ? Symtab.default (s, weight)
blanchet@41313
   738
  #> fold (add_term_weights weight) tms
blanchet@41769
   739
fun add_problem_line_weights weight (Fof (_, _, phi, _)) =
blanchet@41384
   740
  fold_formula (add_term_weights weight) phi
blanchet@41313
   741
blanchet@41313
   742
fun add_conjectures_weights [] = I
blanchet@41313
   743
  | add_conjectures_weights conjs =
blanchet@41313
   744
    let val (hyps, conj) = split_last conjs in
blanchet@41313
   745
      add_problem_line_weights conj_weight conj
blanchet@41313
   746
      #> fold (add_problem_line_weights hyp_weight) hyps
blanchet@41313
   747
    end
blanchet@41313
   748
blanchet@41313
   749
fun add_facts_weights facts =
blanchet@41313
   750
  let
blanchet@41313
   751
    val num_facts = length facts
blanchet@41313
   752
    fun weight_of j =
blanchet@41313
   753
      fact_min_weight + (fact_max_weight - fact_min_weight) * Real.fromInt j
blanchet@41313
   754
                        / Real.fromInt num_facts
blanchet@41313
   755
  in
blanchet@41313
   756
    map weight_of (0 upto num_facts - 1) ~~ facts
blanchet@41313
   757
    |> fold (uncurry add_problem_line_weights)
blanchet@41313
   758
  end
blanchet@41313
   759
blanchet@41313
   760
(* Weights are from 0.0 (most important) to 1.0 (least important). *)
blanchet@41313
   761
fun atp_problem_weights problem =
blanchet@41313
   762
  Symtab.empty
blanchet@41313
   763
  |> add_conjectures_weights (these (AList.lookup (op =) problem conjsN))
blanchet@41313
   764
  |> add_facts_weights (these (AList.lookup (op =) problem factsN))
blanchet@41313
   765
  |> Symtab.dest
blanchet@41726
   766
  |> sort (prod_ord Real.compare string_ord o pairself swap)
blanchet@41313
   767
blanchet@38282
   768
end;