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