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