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