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