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