src/HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
author blanchet
Sun May 01 18:37:24 2011 +0200 (2011-05-01)
changeset 42562 f1d903f789b1
parent 42561 23ddc4e3d19c
child 42563 e70ffe3846d0
permissions -rw-r--r--
killed needless datatype "combtyp" in Metis
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@42548
    17
    Mangled of bool |
blanchet@42523
    18
    Args of bool |
blanchet@42548
    19
    Tags 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@42544
    24
  val boolify_base : string
blanchet@42544
    25
  val explicit_app_base : string
blanchet@42549
    26
  val type_pred_base : string
blanchet@42562
    27
  val tff_type_prefix : string
blanchet@42449
    28
  val is_type_system_sound : type_system -> bool
blanchet@42449
    29
  val type_system_types_dangerous_types : type_system -> bool
blanchet@41136
    30
  val num_atp_type_args : theory -> type_system -> string -> int
blanchet@42542
    31
  val unmangled_const : string -> string * string fo_term list
blanchet@41088
    32
  val translate_atp_fact :
blanchet@42544
    33
    Proof.context -> bool -> (string * 'a) * thm
blanchet@41091
    34
    -> translated_formula option * ((string * 'a) * thm)
blanchet@40059
    35
  val prepare_atp_problem :
blanchet@42521
    36
    Proof.context -> bool -> type_system -> bool -> term list -> term
blanchet@41091
    37
    -> (translated_formula option * ((string * 'a) * thm)) list
blanchet@42541
    38
    -> string problem * string Symtab.table * int * int
blanchet@42541
    39
       * (string * 'a) list vector
blanchet@41313
    40
  val atp_problem_weights : string problem -> (string * real) list
blanchet@38282
    41
end;
blanchet@38282
    42
blanchet@41140
    43
structure Sledgehammer_ATP_Translate : SLEDGEHAMMER_ATP_TRANSLATE =
blanchet@38282
    44
struct
blanchet@38282
    45
blanchet@38282
    46
open ATP_Problem
blanchet@39494
    47
open Metis_Translate
blanchet@38282
    48
open Sledgehammer_Util
blanchet@38282
    49
blanchet@42543
    50
val type_decl_prefix = "type_"
blanchet@42543
    51
val sym_decl_prefix = "sym_"
blanchet@40204
    52
val fact_prefix = "fact_"
blanchet@38282
    53
val conjecture_prefix = "conj_"
blanchet@38282
    54
val helper_prefix = "help_"
blanchet@42543
    55
val class_rel_clause_prefix = "crel_";
blanchet@38282
    56
val arity_clause_prefix = "arity_"
blanchet@39975
    57
val tfree_prefix = "tfree_"
blanchet@38282
    58
blanchet@42544
    59
val boolify_base = "hBOOL"
blanchet@42544
    60
val explicit_app_base = "hAPP"
blanchet@42542
    61
val type_pred_base = "is"
blanchet@42562
    62
val tff_type_prefix = "ty_"
blanchet@42531
    63
blanchet@42562
    64
fun make_tff_type s = tff_type_prefix ^ ascii_of s
blanchet@42531
    65
blanchet@42534
    66
(* official TPTP TFF syntax *)
blanchet@42543
    67
val tff_type_of_types = "$tType"
blanchet@42534
    68
val tff_bool_type = "$o"
blanchet@42534
    69
blanchet@38282
    70
(* Freshness almost guaranteed! *)
blanchet@38282
    71
val sledgehammer_weak_prefix = "Sledgehammer:"
blanchet@38282
    72
blanchet@40114
    73
type translated_formula =
blanchet@38752
    74
  {name: string,
blanchet@42525
    75
   kind: formula_kind,
blanchet@42562
    76
   combformula: (name, typ, combterm) formula,
blanchet@42562
    77
   atomic_types: typ list}
blanchet@38282
    78
blanchet@42558
    79
fun update_combformula f
blanchet@42562
    80
        ({name, kind, combformula, atomic_types} : translated_formula) =
blanchet@42542
    81
  {name = name, kind = kind, combformula = f combformula,
blanchet@42562
    82
   atomic_types = atomic_types} : translated_formula
blanchet@42542
    83
blanchet@42558
    84
fun fact_lift f ({combformula, ...} : translated_formula) = f combformula
blanchet@42558
    85
blanchet@41134
    86
datatype type_system =
blanchet@42523
    87
  Many_Typed |
blanchet@42548
    88
  Mangled of bool |
blanchet@42523
    89
  Args of bool |
blanchet@42548
    90
  Tags of bool |
blanchet@41134
    91
  No_Types
blanchet@41134
    92
blanchet@42523
    93
fun is_type_system_sound Many_Typed = true
blanchet@42548
    94
  | is_type_system_sound (Mangled full_types) = full_types
blanchet@42523
    95
  | is_type_system_sound (Args full_types) = full_types
blanchet@42548
    96
  | is_type_system_sound (Tags full_types) = full_types
blanchet@42523
    97
  | is_type_system_sound No_Types = false
blanchet@42449
    98
blanchet@42449
    99
fun type_system_types_dangerous_types (Tags _) = true
blanchet@42523
   100
  | type_system_types_dangerous_types type_sys = is_type_system_sound type_sys
blanchet@41138
   101
blanchet@42524
   102
fun dont_need_type_args type_sys s =
blanchet@42542
   103
  s <> type_pred_base andalso
blanchet@42542
   104
  (member (op =) [@{const_name HOL.eq}] s orelse
blanchet@42531
   105
   case type_sys of
blanchet@42533
   106
     Many_Typed => false
blanchet@42548
   107
   | Mangled _ => false
blanchet@42544
   108
   | Args _ => s = explicit_app_base
blanchet@42548
   109
   | Tags full_types => full_types orelse s = explicit_app_base
blanchet@42531
   110
   | No_Types => true)
blanchet@41136
   111
blanchet@42548
   112
datatype type_arg_policy = No_Type_Args | Mangled_Types | Explicit_Type_Args
blanchet@42227
   113
blanchet@42524
   114
fun type_arg_policy type_sys s =
blanchet@42533
   115
  if dont_need_type_args type_sys s then
blanchet@42533
   116
    No_Type_Args
blanchet@42533
   117
  else
blanchet@42533
   118
    case type_sys of
blanchet@42533
   119
      Many_Typed => Mangled_Types
blanchet@42533
   120
    | Mangled _ => Mangled_Types
blanchet@42533
   121
    | _ => Explicit_Type_Args
blanchet@42227
   122
blanchet@41136
   123
fun num_atp_type_args thy type_sys s =
blanchet@42557
   124
  if type_arg_policy type_sys s = Explicit_Type_Args then num_type_args thy s
blanchet@42557
   125
  else 0
blanchet@41136
   126
blanchet@42353
   127
fun atp_type_literals_for_types type_sys kind Ts =
blanchet@42353
   128
  if type_sys = No_Types then
blanchet@42353
   129
    []
blanchet@42353
   130
  else
blanchet@42353
   131
    Ts |> type_literals_for_types
blanchet@42353
   132
       |> filter (fn TyLitVar _ => kind <> Conjecture
blanchet@42353
   133
                   | TyLitFree _ => kind = Conjecture)
blanchet@41137
   134
blanchet@38282
   135
fun mk_anot phi = AConn (ANot, [phi])
blanchet@38282
   136
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
blanchet@42534
   137
fun mk_aconns c phis =
blanchet@42534
   138
  let val (phis', phi') = split_last phis in
blanchet@42534
   139
    fold_rev (mk_aconn c) phis' phi'
blanchet@42534
   140
  end
blanchet@38282
   141
fun mk_ahorn [] phi = phi
blanchet@42534
   142
  | mk_ahorn phis psi = AConn (AImplies, [mk_aconns AAnd phis, psi])
blanchet@42522
   143
fun mk_aquant _ [] phi = phi
blanchet@42522
   144
  | mk_aquant q xs (phi as AQuant (q', xs', phi')) =
blanchet@42522
   145
    if q = q' then AQuant (q, xs @ xs', phi') else AQuant (q, xs, phi)
blanchet@42522
   146
  | mk_aquant q xs phi = AQuant (q, xs, phi)
blanchet@38282
   147
blanchet@42522
   148
fun close_universally atom_vars phi =
blanchet@41145
   149
  let
blanchet@41145
   150
    fun formula_vars bounds (AQuant (_, xs, phi)) =
blanchet@42526
   151
        formula_vars (map fst xs @ bounds) phi
blanchet@41145
   152
      | formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
blanchet@42522
   153
      | formula_vars bounds (AAtom tm) =
blanchet@42526
   154
        union (op =) (atom_vars tm []
blanchet@42526
   155
                      |> filter_out (member (op =) bounds o fst))
blanchet@42522
   156
  in mk_aquant AForall (formula_vars [] phi []) phi end
blanchet@42522
   157
blanchet@42531
   158
fun combterm_vars (CombApp (tm1, tm2)) = fold combterm_vars [tm1, tm2]
blanchet@42522
   159
  | combterm_vars (CombConst _) = I
blanchet@42531
   160
  | combterm_vars (CombVar (name, ty)) = insert (op =) (name, SOME ty)
blanchet@42522
   161
val close_combformula_universally = close_universally combterm_vars
blanchet@42522
   162
blanchet@42522
   163
fun term_vars (ATerm (name as (s, _), tms)) =
blanchet@42531
   164
  is_atp_variable s ? insert (op =) (name, NONE)
blanchet@42526
   165
  #> fold term_vars tms
blanchet@42522
   166
val close_formula_universally = close_universally term_vars
blanchet@41145
   167
blanchet@42562
   168
fun fo_term_from_typ (Type (s, Ts)) =
blanchet@42562
   169
    ATerm (`make_fixed_type_const s, map fo_term_from_typ Ts)
blanchet@42562
   170
  | fo_term_from_typ (TFree (s, _)) =
blanchet@42562
   171
    ATerm (`make_fixed_type_var s, [])
blanchet@42562
   172
  | fo_term_from_typ (TVar ((x as (s, _)), _)) =
blanchet@42562
   173
    ATerm ((make_schematic_type_var x, s), [])
blanchet@42562
   174
blanchet@42562
   175
(* This shouldn't clash with anything else. *)
blanchet@42542
   176
val mangled_type_sep = "\000"
blanchet@42542
   177
blanchet@42562
   178
fun generic_mangled_type_name f (ATerm (name, [])) = f name
blanchet@42562
   179
  | generic_mangled_type_name f (ATerm (name, tys)) =
blanchet@42562
   180
    f name ^ "(" ^ commas (map (generic_mangled_type_name f) tys) ^ ")"
blanchet@42562
   181
val mangled_type_name =
blanchet@42562
   182
  fo_term_from_typ
blanchet@42562
   183
  #> (fn ty => (make_tff_type (generic_mangled_type_name fst ty),
blanchet@42562
   184
                generic_mangled_type_name snd ty))
blanchet@42542
   185
blanchet@42562
   186
fun generic_mangled_type_suffix f g tys =
blanchet@42542
   187
  fold_rev (curry (op ^) o g o prefix mangled_type_sep
blanchet@42562
   188
            o generic_mangled_type_name f) tys ""
blanchet@42562
   189
fun mangled_const_name T_args (s, s') =
blanchet@42562
   190
  let val ty_args = map fo_term_from_typ T_args in
blanchet@42562
   191
    (s ^ generic_mangled_type_suffix fst ascii_of ty_args,
blanchet@42562
   192
     s' ^ generic_mangled_type_suffix snd I ty_args)
blanchet@42562
   193
  end
blanchet@42542
   194
blanchet@42542
   195
val parse_mangled_ident =
blanchet@42542
   196
  Scan.many1 (not o member (op =) ["(", ")", ","]) >> implode
blanchet@42542
   197
blanchet@42542
   198
fun parse_mangled_type x =
blanchet@42542
   199
  (parse_mangled_ident
blanchet@42542
   200
   -- Scan.optional ($$ "(" |-- Scan.optional parse_mangled_types [] --| $$ ")")
blanchet@42542
   201
                    [] >> ATerm) x
blanchet@42542
   202
and parse_mangled_types x =
blanchet@42542
   203
  (parse_mangled_type ::: Scan.repeat ($$ "," |-- parse_mangled_type)) x
blanchet@42542
   204
blanchet@42542
   205
fun unmangled_type s =
blanchet@42542
   206
  s |> suffix ")" |> raw_explode
blanchet@42542
   207
    |> Scan.finite Symbol.stopper
blanchet@42542
   208
           (Scan.error (!! (fn _ => raise Fail ("unrecognized mangled type " ^
blanchet@42542
   209
                                                quote s)) parse_mangled_type))
blanchet@42542
   210
    |> fst
blanchet@42542
   211
blanchet@42561
   212
val unmangled_const_name = space_explode mangled_type_sep #> hd
blanchet@42542
   213
fun unmangled_const s =
blanchet@42542
   214
  let val ss = space_explode mangled_type_sep s in
blanchet@42542
   215
    (hd ss, map unmangled_type (tl ss))
blanchet@42542
   216
  end
blanchet@42542
   217
blanchet@42562
   218
fun combformula_from_prop thy eq_as_iff =
blanchet@38282
   219
  let
blanchet@41140
   220
    fun do_term bs t ts =
blanchet@41140
   221
      combterm_from_term thy bs (Envir.eta_contract t)
blanchet@41140
   222
      |>> AAtom ||> union (op =) ts
blanchet@38282
   223
    fun do_quant bs q s T t' =
blanchet@38518
   224
      let val s = Name.variant (map fst bs) s in
blanchet@38518
   225
        do_formula ((s, T) :: bs) t'
blanchet@42562
   226
        #>> mk_aquant q [(`make_bound_var s, SOME T)]
blanchet@38518
   227
      end
blanchet@38282
   228
    and do_conn bs c t1 t2 =
blanchet@38282
   229
      do_formula bs t1 ##>> do_formula bs t2
blanchet@42531
   230
      #>> uncurry (mk_aconn c)
blanchet@38282
   231
    and do_formula bs t =
blanchet@38282
   232
      case t of
blanchet@42531
   233
        @{const Not} $ t1 => do_formula bs t1 #>> mk_anot
blanchet@38282
   234
      | Const (@{const_name All}, _) $ Abs (s, T, t') =>
blanchet@38282
   235
        do_quant bs AForall s T t'
blanchet@38282
   236
      | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
blanchet@38282
   237
        do_quant bs AExists s T t'
haftmann@38795
   238
      | @{const HOL.conj} $ t1 $ t2 => do_conn bs AAnd t1 t2
haftmann@38795
   239
      | @{const HOL.disj} $ t1 $ t2 => do_conn bs AOr t1 t2
haftmann@38786
   240
      | @{const HOL.implies} $ t1 $ t2 => do_conn bs AImplies t1 t2
haftmann@38864
   241
      | Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
blanchet@41140
   242
        if eq_as_iff then do_conn bs AIff t1 t2 else do_term bs t
blanchet@41140
   243
      | _ => do_term bs t
blanchet@38282
   244
  in do_formula [] end
blanchet@38282
   245
blanchet@38618
   246
val presimplify_term = prop_of o Meson.presimplify oo Skip_Proof.make_thm
blanchet@38282
   247
wenzelm@41491
   248
fun concealed_bound_name j = sledgehammer_weak_prefix ^ string_of_int j
blanchet@38282
   249
fun conceal_bounds Ts t =
blanchet@38282
   250
  subst_bounds (map (Free o apfst concealed_bound_name)
blanchet@38282
   251
                    (0 upto length Ts - 1 ~~ Ts), t)
blanchet@38282
   252
fun reveal_bounds Ts =
blanchet@38282
   253
  subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
blanchet@38282
   254
                    (0 upto length Ts - 1 ~~ Ts))
blanchet@38282
   255
blanchet@38608
   256
(* Removes the lambdas from an equation of the form "t = (%x. u)".
blanchet@39890
   257
   (Cf. "extensionalize_theorem" in "Meson_Clausify".) *)
blanchet@38608
   258
fun extensionalize_term t =
blanchet@38608
   259
  let
blanchet@38608
   260
    fun aux j (@{const Trueprop} $ t') = @{const Trueprop} $ aux j t'
blanchet@38608
   261
      | aux j (t as Const (s, Type (_, [Type (_, [_, T']),
blanchet@38608
   262
                                        Type (_, [_, res_T])]))
blanchet@38608
   263
                    $ t2 $ Abs (var_s, var_T, t')) =
haftmann@38864
   264
        if s = @{const_name HOL.eq} orelse s = @{const_name "=="} then
blanchet@38608
   265
          let val var_t = Var ((var_s, j), var_T) in
blanchet@38608
   266
            Const (s, T' --> T' --> res_T)
blanchet@38608
   267
              $ betapply (t2, var_t) $ subst_bound (var_t, t')
blanchet@38608
   268
            |> aux (j + 1)
blanchet@38608
   269
          end
blanchet@38608
   270
        else
blanchet@38608
   271
          t
blanchet@38608
   272
      | aux _ t = t
blanchet@38608
   273
  in aux (maxidx_of_term t + 1) t end
blanchet@38608
   274
blanchet@38282
   275
fun introduce_combinators_in_term ctxt kind t =
wenzelm@42361
   276
  let val thy = Proof_Context.theory_of ctxt in
blanchet@38491
   277
    if Meson.is_fol_term thy t then
blanchet@38491
   278
      t
blanchet@38491
   279
    else
blanchet@38491
   280
      let
blanchet@38491
   281
        fun aux Ts t =
blanchet@38491
   282
          case t of
blanchet@38491
   283
            @{const Not} $ t1 => @{const Not} $ aux Ts t1
blanchet@38491
   284
          | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
blanchet@38491
   285
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38652
   286
          | (t0 as Const (@{const_name All}, _)) $ t1 =>
blanchet@38652
   287
            aux Ts (t0 $ eta_expand Ts t1 1)
blanchet@38491
   288
          | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
blanchet@38491
   289
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38652
   290
          | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
blanchet@38652
   291
            aux Ts (t0 $ eta_expand Ts t1 1)
haftmann@38795
   292
          | (t0 as @{const HOL.conj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38795
   293
          | (t0 as @{const HOL.disj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38786
   294
          | (t0 as @{const HOL.implies}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
haftmann@38864
   295
          | (t0 as Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])))
blanchet@38491
   296
              $ t1 $ t2 =>
blanchet@38491
   297
            t0 $ aux Ts t1 $ aux Ts t2
blanchet@38491
   298
          | _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
blanchet@38491
   299
                   t
blanchet@38491
   300
                 else
blanchet@38491
   301
                   t |> conceal_bounds Ts
blanchet@38491
   302
                     |> Envir.eta_contract
blanchet@38491
   303
                     |> cterm_of thy
blanchet@39890
   304
                     |> Meson_Clausify.introduce_combinators_in_cterm
blanchet@38491
   305
                     |> prop_of |> Logic.dest_equals |> snd
blanchet@38491
   306
                     |> reveal_bounds Ts
blanchet@39370
   307
        val (t, ctxt') = Variable.import_terms true [t] ctxt |>> the_single
blanchet@38491
   308
      in t |> aux [] |> singleton (Variable.export_terms ctxt' ctxt) end
blanchet@38491
   309
      handle THM _ =>
blanchet@38491
   310
             (* A type variable of sort "{}" will make abstraction fail. *)
blanchet@38613
   311
             if kind = Conjecture then HOLogic.false_const
blanchet@38613
   312
             else HOLogic.true_const
blanchet@38491
   313
  end
blanchet@38282
   314
blanchet@38282
   315
(* Metis's use of "resolve_tac" freezes the schematic variables. We simulate the
blanchet@42353
   316
   same in Sledgehammer to prevent the discovery of unreplayable proofs. *)
blanchet@38282
   317
fun freeze_term t =
blanchet@38282
   318
  let
blanchet@38282
   319
    fun aux (t $ u) = aux t $ aux u
blanchet@38282
   320
      | aux (Abs (s, T, t)) = Abs (s, T, aux t)
blanchet@38282
   321
      | aux (Var ((s, i), T)) =
blanchet@38282
   322
        Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
blanchet@38282
   323
      | aux t = t
blanchet@38282
   324
  in t |> exists_subterm is_Var t ? aux end
blanchet@38282
   325
blanchet@40204
   326
(* making fact and conjecture formulas *)
blanchet@42544
   327
fun make_formula ctxt eq_as_iff presimp name kind t =
blanchet@38282
   328
  let
wenzelm@42361
   329
    val thy = Proof_Context.theory_of ctxt
blanchet@38608
   330
    val t = t |> Envir.beta_eta_contract
blanchet@38652
   331
              |> transform_elim_term
blanchet@41211
   332
              |> Object_Logic.atomize_term thy
blanchet@38652
   333
    val need_trueprop = (fastype_of t = HOLogic.boolT)
blanchet@38652
   334
    val t = t |> need_trueprop ? HOLogic.mk_Trueprop
blanchet@38282
   335
              |> extensionalize_term
blanchet@38282
   336
              |> presimp ? presimplify_term thy
blanchet@38282
   337
              |> perhaps (try (HOLogic.dest_Trueprop))
blanchet@38282
   338
              |> introduce_combinators_in_term ctxt kind
blanchet@38613
   339
              |> kind <> Axiom ? freeze_term
blanchet@42562
   340
    val (combformula, atomic_types) =
blanchet@42562
   341
      combformula_from_prop thy eq_as_iff t []
blanchet@38282
   342
  in
blanchet@38752
   343
    {name = name, combformula = combformula, kind = kind,
blanchet@42562
   344
     atomic_types = atomic_types}
blanchet@38282
   345
  end
blanchet@38282
   346
blanchet@42561
   347
fun make_fact ctxt keep_trivial eq_as_iff presimp ((name, _), t) =
blanchet@42561
   348
  case (keep_trivial, make_formula ctxt eq_as_iff presimp name Axiom t) of
blanchet@41990
   349
    (false, {combformula = AAtom (CombConst (("c_True", _), _, _)), ...}) =>
blanchet@41990
   350
    NONE
blanchet@41990
   351
  | (_, formula) => SOME formula
blanchet@42561
   352
blanchet@42544
   353
fun make_conjecture ctxt ts =
blanchet@38613
   354
  let val last = length ts - 1 in
blanchet@42544
   355
    map2 (fn j => make_formula ctxt true true (string_of_int j)
blanchet@38613
   356
                               (if j = last then Conjecture else Hypothesis))
blanchet@38613
   357
         (0 upto last) ts
blanchet@38613
   358
  end
blanchet@38282
   359
blanchet@38282
   360
(** Helper facts **)
blanchet@38282
   361
blanchet@42542
   362
fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
blanchet@42542
   363
  | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
blanchet@42542
   364
  | formula_map f (AAtom tm) = AAtom (f tm)
blanchet@42542
   365
blanchet@42542
   366
fun formula_fold f (AQuant (_, _, phi)) = formula_fold f phi
blanchet@42542
   367
  | formula_fold f (AConn (_, phis)) = fold (formula_fold f) phis
blanchet@42542
   368
  | formula_fold f (AAtom tm) = f tm
blanchet@41313
   369
blanchet@42561
   370
fun ti_ti_helper_fact () =
blanchet@38282
   371
  let
blanchet@42561
   372
    fun var s = ATerm (`I s, [])
blanchet@42561
   373
    fun tag tm = ATerm (`I type_tag_name, [var "X", tm])
blanchet@38282
   374
  in
blanchet@42561
   375
    Formula (Fof, helper_prefix ^ ascii_of "ti_ti", Axiom,
blanchet@42561
   376
             AAtom (ATerm (`I "equal", [tag (tag (var "Y")), tag (var "Y")]))
blanchet@42561
   377
             |> close_formula_universally, NONE, NONE)
blanchet@38282
   378
  end
blanchet@38282
   379
blanchet@42562
   380
fun helper_facts_for_typed_const ctxt type_sys s (_, _, T) =
blanchet@42561
   381
  case strip_prefix_and_unascii const_prefix s of
blanchet@42561
   382
    SOME s'' =>
blanchet@42561
   383
    let
blanchet@42561
   384
      val thy = Proof_Context.theory_of ctxt
blanchet@42561
   385
      val unmangled_s = s'' |> unmangled_const_name
blanchet@42561
   386
      (* ### FIXME avoid duplicate names *)
blanchet@42561
   387
      fun dub_and_inst c needs_full_types (th, j) =
blanchet@42561
   388
        ((c ^ "_" ^ string_of_int j ^ (if needs_full_types then "ft" else ""),
blanchet@42561
   389
          false),
blanchet@42561
   390
         th |> prop_of
blanchet@42562
   391
            |> specialize_type thy (invert_const unmangled_s, T))
blanchet@42561
   392
      fun make_facts eq_as_iff =
blanchet@42561
   393
        map_filter (make_fact ctxt false eq_as_iff false)
blanchet@42561
   394
    in
blanchet@42561
   395
      metis_helpers
blanchet@42561
   396
      |> maps (fn (metis_s, (needs_full_types, ths)) =>
blanchet@42561
   397
                  if metis_s <> unmangled_s orelse
blanchet@42561
   398
                     (needs_full_types andalso
blanchet@42561
   399
                      not (type_system_types_dangerous_types type_sys)) then
blanchet@42561
   400
                    []
blanchet@42561
   401
                  else
blanchet@42561
   402
                    ths ~~ (1 upto length ths)
blanchet@42561
   403
                    |> map (dub_and_inst s needs_full_types)
blanchet@42561
   404
                    |> make_facts (not needs_full_types))
blanchet@42561
   405
    end
blanchet@42561
   406
  | NONE => []
blanchet@42561
   407
fun helper_facts_for_const ctxt type_sys (s, xs) =
blanchet@42561
   408
  maps (helper_facts_for_typed_const ctxt type_sys s) xs
blanchet@42561
   409
fun helper_facts ctxt type_sys typed_const_tab =
blanchet@42561
   410
  (Symtab.fold_rev (append o helper_facts_for_const ctxt type_sys)
blanchet@42561
   411
                   typed_const_tab [],
blanchet@42561
   412
   if type_sys = Tags false then [ti_ti_helper_fact ()] else [])
blanchet@42561
   413
blanchet@42544
   414
fun translate_atp_fact ctxt keep_trivial =
blanchet@42561
   415
  `(make_fact ctxt keep_trivial true true o apsnd prop_of)
blanchet@39004
   416
blanchet@41134
   417
fun translate_formulas ctxt type_sys hyp_ts concl_t rich_facts =
blanchet@38282
   418
  let
wenzelm@42361
   419
    val thy = Proof_Context.theory_of ctxt
blanchet@41091
   420
    val fact_ts = map (prop_of o snd o snd) rich_facts
blanchet@41091
   421
    val (facts, fact_names) =
blanchet@41091
   422
      rich_facts
blanchet@41091
   423
      |> map_filter (fn (NONE, _) => NONE
blanchet@41091
   424
                      | (SOME fact, (name, _)) => SOME (fact, name))
blanchet@41091
   425
      |> ListPair.unzip
blanchet@40204
   426
    (* Remove existing facts from the conjecture, as this can dramatically
blanchet@39005
   427
       boost an ATP's performance (for some reason). *)
blanchet@40204
   428
    val hyp_ts = hyp_ts |> filter_out (member (op aconv) fact_ts)
blanchet@38282
   429
    val goal_t = Logic.list_implies (hyp_ts, concl_t)
blanchet@42353
   430
    val all_ts = goal_t :: fact_ts
blanchet@42353
   431
    val subs = tfree_classes_of_terms all_ts
blanchet@42353
   432
    val supers = tvar_classes_of_terms all_ts
blanchet@42353
   433
    val tycons = type_consts_of_terms thy all_ts
blanchet@42544
   434
    val conjs = make_conjecture ctxt (hyp_ts @ [concl_t])
blanchet@41137
   435
    val (supers', arity_clauses) =
blanchet@41137
   436
      if type_sys = No_Types then ([], [])
blanchet@41137
   437
      else make_arity_clauses thy tycons supers
blanchet@38282
   438
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@38282
   439
  in
blanchet@41313
   440
    (fact_names |> map single, (conjs, facts, class_rel_clauses, arity_clauses))
blanchet@38282
   441
  end
blanchet@38282
   442
blanchet@42561
   443
val proxy_table =
blanchet@42561
   444
  [("c_False", ("c_fFalse", @{const_name Metis.fFalse})),
blanchet@42561
   445
   ("c_True", ("c_fTrue", @{const_name Metis.fTrue})),
blanchet@42561
   446
   ("c_Not", ("c_fNot", @{const_name Metis.fNot})),
blanchet@42561
   447
   ("c_conj", ("c_fconj", @{const_name Metis.fconj})),
blanchet@42561
   448
   ("c_disj", ("c_fdisj", @{const_name Metis.fdisj})),
blanchet@42561
   449
   ("c_implies", ("c_fimplies", @{const_name Metis.fimplies})),
blanchet@42561
   450
   ("equal", ("c_fequal", @{const_name Metis.fequal}))]
blanchet@42561
   451
blanchet@42561
   452
fun repair_combterm_consts type_sys =
blanchet@42561
   453
  let
blanchet@42561
   454
    fun aux top_level (CombApp (tm1, tm2)) =
blanchet@42561
   455
        CombApp (aux top_level tm1, aux false tm2)
blanchet@42561
   456
      | aux top_level (CombConst (name as (s, _), ty, ty_args)) =
blanchet@42561
   457
        (case strip_prefix_and_unascii const_prefix s of
blanchet@42561
   458
           NONE => (name, ty_args)
blanchet@42561
   459
         | SOME s'' =>
blanchet@42561
   460
           let val s'' = invert_const s'' in
blanchet@42561
   461
             case type_arg_policy type_sys s'' of
blanchet@42561
   462
               No_Type_Args => (name, [])
blanchet@42561
   463
             | Mangled_Types => (mangled_const_name ty_args name, [])
blanchet@42561
   464
             | Explicit_Type_Args => (name, ty_args)
blanchet@42561
   465
           end)
blanchet@42561
   466
        |> (fn (name as (s, s'), ty_args) =>
blanchet@42561
   467
               case AList.lookup (op =) proxy_table s of
blanchet@42561
   468
                 SOME proxy_name =>
blanchet@42561
   469
                 if top_level then
blanchet@42561
   470
                   (case s of
blanchet@42561
   471
                      "c_False" => ("$false", s')
blanchet@42561
   472
                    | "c_True" => ("$true", s')
blanchet@42561
   473
                    | _ => name, [])
blanchet@42561
   474
                  else
blanchet@42561
   475
                    (proxy_name, ty_args)
blanchet@42561
   476
                | NONE => (name, ty_args))
blanchet@42561
   477
        |> (fn (name, ty_args) => CombConst (name, ty, ty_args))
blanchet@42561
   478
      | aux _ tm = tm
blanchet@42561
   479
  in aux true end
blanchet@42561
   480
blanchet@41138
   481
fun tag_with_type ty t = ATerm (`I type_tag_name, [ty, t])
blanchet@38282
   482
blanchet@42562
   483
fun fo_literal_from_type_literal (TyLitVar (class, name)) =
blanchet@38282
   484
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@42562
   485
  | fo_literal_from_type_literal (TyLitFree (class, name)) =
blanchet@38282
   486
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@38282
   487
blanchet@42562
   488
fun formula_from_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
blanchet@38282
   489
blanchet@41138
   490
(* Finite types such as "unit", "bool", "bool * bool", and "bool => bool" are
blanchet@41138
   491
   considered dangerous because their "exhaust" properties can easily lead to
blanchet@41138
   492
   unsound ATP proofs. The checks below are an (unsound) approximation of
blanchet@41138
   493
   finiteness. *)
blanchet@41138
   494
blanchet@41138
   495
fun is_dtyp_dangerous _ (Datatype_Aux.DtTFree _) = true
blanchet@41138
   496
  | is_dtyp_dangerous ctxt (Datatype_Aux.DtType (s, Us)) =
blanchet@41138
   497
    is_type_constr_dangerous ctxt s andalso forall (is_dtyp_dangerous ctxt) Us
blanchet@41138
   498
  | is_dtyp_dangerous _ (Datatype_Aux.DtRec _) = false
blanchet@41138
   499
and is_type_dangerous ctxt (Type (s, Ts)) =
blanchet@41138
   500
    is_type_constr_dangerous ctxt s andalso forall (is_type_dangerous ctxt) Ts
blanchet@41140
   501
  | is_type_dangerous _ _ = false
blanchet@41138
   502
and is_type_constr_dangerous ctxt s =
wenzelm@42361
   503
  let val thy = Proof_Context.theory_of ctxt in
blanchet@41138
   504
    case Datatype_Data.get_info thy s of
blanchet@41138
   505
      SOME {descr, ...} =>
blanchet@41138
   506
      forall (fn (_, (_, _, constrs)) =>
blanchet@41138
   507
                 forall (forall (is_dtyp_dangerous ctxt) o snd) constrs) descr
blanchet@41138
   508
    | NONE =>
blanchet@41138
   509
      case Typedef.get_info ctxt s of
blanchet@41138
   510
        ({rep_type, ...}, _) :: _ => is_type_dangerous ctxt rep_type
blanchet@41138
   511
      | [] => true
blanchet@41138
   512
  end
blanchet@41138
   513
blanchet@42562
   514
fun should_tag_with_type ctxt (Tags full_types) T =
blanchet@42562
   515
    full_types orelse is_type_dangerous ctxt T
blanchet@41138
   516
  | should_tag_with_type _ _ _ = false
blanchet@41138
   517
blanchet@42562
   518
fun type_pred_combatom type_sys T tm =
blanchet@42562
   519
  CombApp (CombConst (`make_fixed_const type_pred_base,
blanchet@42562
   520
                      T --> HOLogic.boolT, [T]), tm)
blanchet@42542
   521
  |> repair_combterm_consts type_sys
blanchet@42534
   522
  |> AAtom
blanchet@42534
   523
blanchet@42562
   524
fun formula_from_combformula ctxt type_sys =
blanchet@38282
   525
  let
blanchet@42556
   526
    fun do_term top_level u =
blanchet@38282
   527
      let
blanchet@38282
   528
        val (head, args) = strip_combterm_comb u
blanchet@38282
   529
        val (x, ty_args) =
blanchet@38282
   530
          case head of
blanchet@42553
   531
            CombConst (name, _, ty_args) => (name, ty_args)
blanchet@38282
   532
          | CombVar (name, _) => (name, [])
blanchet@38282
   533
          | CombApp _ => raise Fail "impossible \"CombApp\""
blanchet@42562
   534
        val t = ATerm (x, map fo_term_from_typ ty_args @
blanchet@42556
   535
                          map (do_term false) args)
blanchet@41138
   536
        val ty = combtyp_of u
blanchet@42530
   537
      in
blanchet@42556
   538
        t |> (if not top_level andalso
blanchet@42556
   539
                 should_tag_with_type ctxt type_sys ty then
blanchet@42562
   540
                tag_with_type (fo_term_from_typ ty)
blanchet@42530
   541
              else
blanchet@42530
   542
                I)
blanchet@42530
   543
      end
blanchet@42531
   544
    val do_bound_type =
blanchet@42562
   545
      if type_sys = Many_Typed then SOME o mangled_type_name else K NONE
blanchet@42531
   546
    val do_out_of_bound_type =
blanchet@42548
   547
      if member (op =) [Mangled true, Args true] type_sys then
blanchet@42531
   548
        (fn (s, ty) =>
blanchet@42542
   549
            type_pred_combatom type_sys ty (CombVar (s, ty))
blanchet@42562
   550
            |> formula_from_combformula ctxt type_sys |> SOME)
blanchet@42531
   551
      else
blanchet@42531
   552
        K NONE
blanchet@42530
   553
    fun do_formula (AQuant (q, xs, phi)) =
blanchet@42531
   554
        AQuant (q, xs |> map (apsnd (fn NONE => NONE
blanchet@42531
   555
                                      | SOME ty => do_bound_type ty)),
blanchet@42534
   556
                (if q = AForall then mk_ahorn else fold_rev (mk_aconn AAnd))
blanchet@42531
   557
                    (map_filter
blanchet@42531
   558
                         (fn (_, NONE) => NONE
blanchet@42531
   559
                           | (s, SOME ty) => do_out_of_bound_type (s, ty)) xs)
blanchet@42531
   560
                    (do_formula phi))
blanchet@42530
   561
      | do_formula (AConn (c, phis)) = AConn (c, map do_formula phis)
blanchet@42556
   562
      | do_formula (AAtom tm) = AAtom (do_term true tm)
blanchet@42530
   563
  in do_formula end
blanchet@38282
   564
blanchet@41138
   565
fun formula_for_fact ctxt type_sys
blanchet@42562
   566
                     ({combformula, atomic_types, ...} : translated_formula) =
blanchet@42562
   567
  mk_ahorn (map (formula_from_fo_literal o fo_literal_from_type_literal)
blanchet@42562
   568
                (atp_type_literals_for_types type_sys Axiom atomic_types))
blanchet@42562
   569
           (formula_from_combformula ctxt type_sys
blanchet@42562
   570
                (close_combformula_universally combformula))
blanchet@42542
   571
  |> close_formula_universally
blanchet@38282
   572
blanchet@42538
   573
fun logic_for_type_sys Many_Typed = Tff
blanchet@42538
   574
  | logic_for_type_sys _ = Fof
blanchet@42538
   575
blanchet@42180
   576
(* Each fact is given a unique fact number to avoid name clashes (e.g., because
blanchet@42180
   577
   of monomorphization). The TPTP explicitly forbids name clashes, and some of
blanchet@42180
   578
   the remote provers might care. *)
blanchet@42545
   579
fun formula_line_for_fact ctxt prefix type_sys
blanchet@42180
   580
                          (j, formula as {name, kind, ...}) =
blanchet@42538
   581
  Formula (logic_for_type_sys type_sys,
blanchet@42538
   582
           prefix ^ string_of_int j ^ "_" ^ ascii_of name, kind,
blanchet@42538
   583
           formula_for_fact ctxt type_sys formula, NONE, NONE)
blanchet@38282
   584
blanchet@42545
   585
fun formula_line_for_class_rel_clause (ClassRelClause {name, subclass,
blanchet@38282
   586
                                                       superclass, ...}) =
blanchet@42542
   587
  let val ty_arg = ATerm (`I "T", []) in
blanchet@42538
   588
    Formula (Fof, class_rel_clause_prefix ^ ascii_of name, Axiom,
blanchet@42527
   589
             AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
blanchet@42542
   590
                               AAtom (ATerm (superclass, [ty_arg]))])
blanchet@42542
   591
             |> close_formula_universally, NONE, NONE)
blanchet@38282
   592
  end
blanchet@38282
   593
blanchet@42562
   594
fun fo_literal_from_arity_literal (TConsLit (c, t, args)) =
blanchet@38282
   595
    (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
blanchet@42562
   596
  | fo_literal_from_arity_literal (TVarLit (c, sort)) =
blanchet@38282
   597
    (false, ATerm (c, [ATerm (sort, [])]))
blanchet@38282
   598
blanchet@42545
   599
fun formula_line_for_arity_clause (ArityClause {name, conclLit, premLits,
blanchet@38282
   600
                                                ...}) =
blanchet@42538
   601
  Formula (Fof, arity_clause_prefix ^ ascii_of name, Axiom,
blanchet@42562
   602
           mk_ahorn (map (formula_from_fo_literal o apfst not
blanchet@42562
   603
                          o fo_literal_from_arity_literal) premLits)
blanchet@42562
   604
                    (formula_from_fo_literal
blanchet@42562
   605
                         (fo_literal_from_arity_literal conclLit))
blanchet@42542
   606
           |> close_formula_universally, NONE, NONE)
blanchet@38282
   607
blanchet@42545
   608
fun formula_line_for_conjecture ctxt type_sys
blanchet@40114
   609
        ({name, kind, combformula, ...} : translated_formula) =
blanchet@42538
   610
  Formula (logic_for_type_sys type_sys, conjecture_prefix ^ name, kind,
blanchet@42562
   611
           formula_from_combformula ctxt type_sys
blanchet@42562
   612
                                    (close_combformula_universally combformula)
blanchet@42542
   613
           |> close_formula_universally, NONE, NONE)
blanchet@38282
   614
blanchet@42562
   615
fun free_type_literals type_sys ({atomic_types, ...} : translated_formula) =
blanchet@42562
   616
  atomic_types |> atp_type_literals_for_types type_sys Conjecture
blanchet@42562
   617
               |> map fo_literal_from_type_literal
blanchet@38282
   618
blanchet@42545
   619
fun formula_line_for_free_type j lit =
blanchet@42538
   620
  Formula (Fof, tfree_prefix ^ string_of_int j, Hypothesis,
blanchet@42562
   621
           formula_from_fo_literal lit, NONE, NONE)
blanchet@42545
   622
fun formula_lines_for_free_types type_sys facts =
blanchet@38282
   623
  let
blanchet@42353
   624
    val litss = map (free_type_literals type_sys) facts
blanchet@38282
   625
    val lits = fold (union (op =)) litss []
blanchet@42545
   626
  in map2 formula_line_for_free_type (0 upto length lits - 1) lits end
blanchet@38282
   627
blanchet@38282
   628
(** "hBOOL" and "hAPP" **)
blanchet@38282
   629
blanchet@42544
   630
type repair_info = {pred_sym: bool, min_arity: int, max_arity: int}
blanchet@38282
   631
blanchet@42560
   632
fun add_combterm_to_sym_table explicit_apply =
blanchet@42558
   633
  let
blanchet@42558
   634
    fun aux top_level tm =
blanchet@42558
   635
      let val (head, args) = strip_combterm_comb tm in
blanchet@42558
   636
        (case head of
blanchet@42558
   637
           CombConst ((s, _), _, _) =>
blanchet@42558
   638
           if String.isPrefix bound_var_prefix s then
blanchet@42558
   639
             I
blanchet@42558
   640
           else
blanchet@42558
   641
             let val arity = length args in
blanchet@42558
   642
               Symtab.map_default
blanchet@42558
   643
                   (s, {pred_sym = true,
blanchet@42558
   644
                        min_arity = if explicit_apply then 0 else arity,
blanchet@42558
   645
                        max_arity = 0})
blanchet@42558
   646
                   (fn {pred_sym, min_arity, max_arity} =>
blanchet@42558
   647
                       {pred_sym = pred_sym andalso top_level,
blanchet@42558
   648
                        min_arity = Int.min (arity, min_arity),
blanchet@42558
   649
                        max_arity = Int.max (arity, max_arity)})
blanchet@42558
   650
            end
blanchet@42558
   651
         | _ => I)
blanchet@42558
   652
        #> fold (aux false) args
blanchet@42558
   653
      end
blanchet@42558
   654
  in aux true end
blanchet@38282
   655
blanchet@42560
   656
val add_fact_to_sym_table = fact_lift o formula_fold o add_combterm_to_sym_table
blanchet@38282
   657
blanchet@42539
   658
(* The "equal" entry is needed for helper facts if the problem otherwise does
blanchet@42557
   659
   not involve equality. The "$false" and $"true" entries are needed to ensure
blanchet@42557
   660
   that no "hBOOL" is introduced for them. The "hBOOL" entry is needed to ensure
blanchet@42557
   661
   that no "hAPP"s are introduced for passing arguments to it. *)
blanchet@42557
   662
val default_sym_table_entries =
blanchet@42544
   663
  [("equal", {pred_sym = true, min_arity = 2, max_arity = 2}),
blanchet@42557
   664
   ("$false", {pred_sym = true, min_arity = 0, max_arity = 0}),
blanchet@42557
   665
   ("$true", {pred_sym = true, min_arity = 0, max_arity = 0}),
blanchet@42544
   666
   (make_fixed_const boolify_base,
blanchet@42545
   667
    {pred_sym = true, min_arity = 1, max_arity = 1})]
blanchet@41140
   668
blanchet@42544
   669
fun sym_table_for_facts explicit_apply facts =
blanchet@42558
   670
  Symtab.empty |> fold Symtab.default default_sym_table_entries
blanchet@42560
   671
               |> fold (add_fact_to_sym_table explicit_apply) facts
blanchet@38282
   672
blanchet@42558
   673
fun min_arity_of sym_tab s =
blanchet@42558
   674
  case Symtab.lookup sym_tab s of
blanchet@42558
   675
    SOME ({min_arity, ...} : repair_info) => min_arity
blanchet@42558
   676
  | NONE =>
blanchet@42558
   677
    case strip_prefix_and_unascii const_prefix s of
blanchet@42547
   678
      SOME s =>
blanchet@42561
   679
      let val s = s |> unmangled_const_name |> invert_const in
blanchet@42547
   680
        if s = boolify_base then 1
blanchet@42547
   681
        else if s = explicit_app_base then 2
blanchet@42547
   682
        else if s = type_pred_base then 1
blanchet@42557
   683
        else 0
blanchet@42547
   684
      end
blanchet@42544
   685
    | NONE => 0
blanchet@38282
   686
blanchet@38282
   687
(* True if the constant ever appears outside of the top-level position in
blanchet@38282
   688
   literals, or if it appears with different arities (e.g., because of different
blanchet@38282
   689
   type instantiations). If false, the constant always receives all of its
blanchet@38282
   690
   arguments and is used as a predicate. *)
blanchet@42558
   691
fun is_pred_sym sym_tab s =
blanchet@42558
   692
  case Symtab.lookup sym_tab s of
blanchet@42558
   693
    SOME {pred_sym, min_arity, max_arity} =>
blanchet@42558
   694
    pred_sym andalso min_arity = max_arity
blanchet@42558
   695
  | NONE => false
blanchet@38282
   696
blanchet@42542
   697
val boolify_combconst =
blanchet@42562
   698
  CombConst (`make_fixed_const boolify_base, @{typ "bool => bool"}, [])
blanchet@42542
   699
fun boolify tm = CombApp (boolify_combconst, tm)
blanchet@42542
   700
blanchet@42544
   701
fun repair_pred_syms_in_combterm sym_tab tm =
blanchet@42542
   702
  case strip_combterm_comb tm of
blanchet@42542
   703
    (CombConst ((s, _), _, _), _) =>
blanchet@42544
   704
    if is_pred_sym sym_tab s then tm else boolify tm
blanchet@42542
   705
  | _ => boolify tm
blanchet@42542
   706
blanchet@42544
   707
fun list_app head args = fold (curry (CombApp o swap)) args head
blanchet@42544
   708
blanchet@42544
   709
fun explicit_app arg head =
blanchet@42544
   710
  let
blanchet@42562
   711
    val head_T = combtyp_of head
blanchet@42562
   712
    val (arg_T, res_T) = dest_funT head_T
blanchet@42544
   713
    val explicit_app =
blanchet@42562
   714
      CombConst (`make_fixed_const explicit_app_base, head_T --> head_T,
blanchet@42562
   715
                 [arg_T, res_T])
blanchet@42544
   716
  in list_app explicit_app [head, arg] end
blanchet@42544
   717
fun list_explicit_app head args = fold explicit_app args head
blanchet@38282
   718
blanchet@42557
   719
fun repair_apps_in_combterm sym_tab =
blanchet@42544
   720
  let
blanchet@42544
   721
    fun aux tm =
blanchet@42544
   722
      case strip_combterm_comb tm of
blanchet@42544
   723
        (head as CombConst ((s, _), _, _), args) =>
blanchet@42544
   724
        args |> map aux
blanchet@42557
   725
             |> chop (min_arity_of sym_tab s)
blanchet@42544
   726
             |>> list_app head
blanchet@42544
   727
             |-> list_explicit_app
blanchet@42544
   728
      | (head, args) => list_explicit_app head (map aux args)
blanchet@42544
   729
  in aux end
blanchet@38282
   730
blanchet@42557
   731
fun repair_combterm type_sys sym_tab =
blanchet@42556
   732
  repair_pred_syms_in_combterm sym_tab
blanchet@42557
   733
  #> repair_apps_in_combterm sym_tab
blanchet@42544
   734
  #> repair_combterm_consts type_sys
blanchet@42557
   735
val repair_combformula = formula_map oo repair_combterm
blanchet@42558
   736
val repair_fact = update_combformula oo repair_combformula
blanchet@42544
   737
blanchet@42544
   738
fun is_const_relevant type_sys sym_tab s =
blanchet@42542
   739
  not (String.isPrefix bound_var_prefix s) andalso s <> "equal" andalso
blanchet@42544
   740
  (type_sys = Many_Typed orelse not (is_pred_sym sym_tab s))
blanchet@38282
   741
blanchet@42544
   742
fun consider_combterm_consts type_sys sym_tab tm =
blanchet@42533
   743
  let val (head, args) = strip_combterm_comb tm in
blanchet@42533
   744
    (case head of
blanchet@42533
   745
       CombConst ((s, s'), ty, ty_args) =>
blanchet@42533
   746
       (* FIXME: exploit type subsumption *)
blanchet@42544
   747
       is_const_relevant type_sys sym_tab s
blanchet@42534
   748
       ? Symtab.insert_list (op =) (s, (s', ty_args, ty))
blanchet@42544
   749
     | _ => I)
blanchet@42544
   750
    #> fold (consider_combterm_consts type_sys sym_tab) args
blanchet@42533
   751
  end
blanchet@42533
   752
blanchet@42558
   753
fun consider_fact_consts type_sys sym_tab =
blanchet@42558
   754
  fact_lift (formula_fold (consider_combterm_consts type_sys sym_tab))
blanchet@42533
   755
blanchet@42545
   756
(* FIXME: needed? *)
blanchet@42561
   757
fun typed_const_table_for_facts type_sys sym_tab facts =
blanchet@42548
   758
  Symtab.empty |> member (op =) [Many_Typed, Mangled true, Args true] type_sys
blanchet@42544
   759
                  ? fold (consider_fact_consts type_sys sym_tab) facts
blanchet@42533
   760
blanchet@42562
   761
fun strip_and_map_type 0 f T = ([], f T)
blanchet@42562
   762
  | strip_and_map_type n f (Type (@{type_name fun}, [dom_T, ran_T])) =
blanchet@42562
   763
    strip_and_map_type (n - 1) f ran_T |>> cons (f dom_T)
blanchet@42562
   764
  | strip_and_map_type _ _ _ = raise Fail "unexpected non-function"
blanchet@42533
   765
blanchet@42562
   766
fun problem_line_for_typed_const ctxt type_sys sym_tab s j (s', ty_args, T) =
blanchet@42557
   767
  let val arity = min_arity_of sym_tab s in
blanchet@42544
   768
    if type_sys = Many_Typed then
blanchet@42544
   769
      let
blanchet@42562
   770
        val (arg_Ts, res_T) = strip_and_map_type arity mangled_type_name T
blanchet@42544
   771
        val (s, s') = (s, s') |> mangled_const_name ty_args
blanchet@42544
   772
      in
blanchet@42562
   773
        Decl (sym_decl_prefix ^ ascii_of s, (s, s'), arg_Ts,
blanchet@42561
   774
              (* ### FIXME: put that in typed_const_tab *)
blanchet@42562
   775
              if is_pred_sym sym_tab s then `I tff_bool_type else res_T)
blanchet@42544
   776
      end
blanchet@42544
   777
    else
blanchet@42544
   778
      let
blanchet@42562
   779
        val (arg_Ts, res_T) = strip_and_map_type arity I T
blanchet@42544
   780
        val bounds =
blanchet@42562
   781
          map (`I o make_bound_var o string_of_int) (1 upto length arg_Ts)
blanchet@42562
   782
          ~~ map SOME arg_Ts
blanchet@42544
   783
        val bound_tms =
blanchet@42562
   784
          map (fn (name, T) => CombConst (name, the T, [])) bounds
blanchet@42546
   785
        val freshener =
blanchet@42546
   786
          case type_sys of Args _ => string_of_int j ^ "_" | _ => ""
blanchet@42544
   787
      in
blanchet@42546
   788
        Formula (Fof, sym_decl_prefix ^ freshener ^ "_" ^ ascii_of s, Axiom,
blanchet@42562
   789
                 CombConst ((s, s'), T, ty_args)
blanchet@42544
   790
                 |> fold (curry (CombApp o swap)) bound_tms
blanchet@42562
   791
                 |> type_pred_combatom type_sys res_T
blanchet@42544
   792
                 |> mk_aquant AForall bounds
blanchet@42562
   793
                 |> formula_from_combformula ctxt type_sys,
blanchet@42544
   794
                 NONE, NONE)
blanchet@42544
   795
      end
blanchet@42544
   796
  end
blanchet@42561
   797
fun problem_lines_for_sym_decl ctxt type_sys sym_tab (s, xs) =
blanchet@42546
   798
  map2 (problem_line_for_typed_const ctxt type_sys sym_tab s)
blanchet@42546
   799
       (0 upto length xs - 1) xs
blanchet@42561
   800
fun problem_lines_for_sym_decls ctxt type_sys repaired_sym_tab typed_const_tab =
blanchet@42561
   801
  Symtab.fold_rev
blanchet@42561
   802
      (append o problem_lines_for_sym_decl ctxt type_sys repaired_sym_tab)
blanchet@42561
   803
      typed_const_tab []
blanchet@42543
   804
blanchet@42543
   805
fun add_tff_types_in_formula (AQuant (_, xs, phi)) =
blanchet@42543
   806
    union (op =) (map_filter snd xs) #> add_tff_types_in_formula phi
blanchet@42543
   807
  | add_tff_types_in_formula (AConn (_, phis)) =
blanchet@42543
   808
    fold add_tff_types_in_formula phis
blanchet@42543
   809
  | add_tff_types_in_formula (AAtom _) = I
blanchet@42539
   810
blanchet@42562
   811
fun add_tff_types_in_problem_line (Decl (_, _, arg_Ts, res_T)) =
blanchet@42562
   812
    union (op =) (res_T :: arg_Ts)
blanchet@42543
   813
  | add_tff_types_in_problem_line (Formula (_, _, _, phi, _, _)) =
blanchet@42543
   814
    add_tff_types_in_formula phi
blanchet@42543
   815
blanchet@42543
   816
fun tff_types_in_problem problem =
blanchet@42543
   817
  fold (fold add_tff_types_in_problem_line o snd) problem []
blanchet@42543
   818
blanchet@42545
   819
fun decl_line_for_tff_type (s, s') =
blanchet@42543
   820
  Decl (type_decl_prefix ^ ascii_of s, (s, s'), [], `I tff_type_of_types)
blanchet@42543
   821
blanchet@42543
   822
val type_declsN = "Types"
blanchet@42544
   823
val sym_declsN = "Symbol types"
blanchet@41157
   824
val factsN = "Relevant facts"
blanchet@41157
   825
val class_relsN = "Class relationships"
blanchet@42543
   826
val aritiesN = "Arities"
blanchet@41157
   827
val helpersN = "Helper facts"
blanchet@41157
   828
val conjsN = "Conjectures"
blanchet@41313
   829
val free_typesN = "Type variables"
blanchet@41157
   830
blanchet@41157
   831
fun offset_of_heading_in_problem _ [] j = j
blanchet@41157
   832
  | offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
blanchet@41157
   833
    if heading = needle then j
blanchet@41157
   834
    else offset_of_heading_in_problem needle problem (j + length lines)
blanchet@41157
   835
blanchet@42521
   836
fun prepare_atp_problem ctxt readable_names type_sys explicit_apply hyp_ts
blanchet@42521
   837
                        concl_t facts =
blanchet@38282
   838
  let
blanchet@41313
   839
    val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
blanchet@41134
   840
      translate_formulas ctxt type_sys hyp_ts concl_t facts
blanchet@42544
   841
    val sym_tab = sym_table_for_facts explicit_apply (conjs @ facts)
blanchet@42561
   842
    val conjs = conjs |> map (repair_fact type_sys sym_tab)
blanchet@42561
   843
    val facts = facts |> map (repair_fact type_sys sym_tab)
blanchet@42561
   844
    val repaired_sym_tab = sym_table_for_facts false (conjs @ facts)
blanchet@42561
   845
    val typed_const_tab =
blanchet@42561
   846
      typed_const_table_for_facts type_sys repaired_sym_tab (conjs @ facts)
blanchet@42561
   847
    val sym_decl_lines =
blanchet@42561
   848
      problem_lines_for_sym_decls ctxt type_sys repaired_sym_tab typed_const_tab
blanchet@42561
   849
    val (helpers, raw_helper_lines) = helper_facts ctxt type_sys typed_const_tab
blanchet@42561
   850
    val helpers = helpers |> map (repair_fact type_sys sym_tab)
blanchet@42522
   851
    (* Reordering these might confuse the proof reconstruction code or the SPASS
blanchet@42522
   852
       Flotter hack. *)
blanchet@38282
   853
    val problem =
blanchet@42561
   854
      [(sym_declsN, sym_decl_lines),
blanchet@42545
   855
       (factsN, map (formula_line_for_fact ctxt fact_prefix type_sys)
blanchet@42180
   856
                    (0 upto length facts - 1 ~~ facts)),
blanchet@42545
   857
       (class_relsN, map formula_line_for_class_rel_clause class_rel_clauses),
blanchet@42545
   858
       (aritiesN, map formula_line_for_arity_clause arity_clauses),
blanchet@42561
   859
       (helpersN, map (formula_line_for_fact ctxt helper_prefix type_sys)
blanchet@42561
   860
                      (0 upto length helpers - 1 ~~ helpers) @
blanchet@42561
   861
                  raw_helper_lines),
blanchet@42545
   862
       (conjsN, map (formula_line_for_conjecture ctxt type_sys) conjs),
blanchet@42545
   863
       (free_typesN, formula_lines_for_free_types type_sys (facts @ conjs))]
blanchet@42543
   864
    val problem =
blanchet@42561
   865
      problem
blanchet@42561
   866
      |> (if type_sys = Many_Typed then
blanchet@42561
   867
            cons (type_declsN,
blanchet@42561
   868
                  map decl_line_for_tff_type (tff_types_in_problem problem))
blanchet@42561
   869
          else
blanchet@42561
   870
            I)
blanchet@42561
   871
    val (problem, pool) = problem |> nice_atp_problem readable_names
blanchet@38282
   872
  in
blanchet@38282
   873
    (problem,
blanchet@38282
   874
     case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
blanchet@42541
   875
     offset_of_heading_in_problem factsN problem 0,
blanchet@41157
   876
     offset_of_heading_in_problem conjsN problem 0,
blanchet@41157
   877
     fact_names |> Vector.fromList)
blanchet@38282
   878
  end
blanchet@38282
   879
blanchet@41313
   880
(* FUDGE *)
blanchet@41313
   881
val conj_weight = 0.0
blanchet@41770
   882
val hyp_weight = 0.1
blanchet@41770
   883
val fact_min_weight = 0.2
blanchet@41313
   884
val fact_max_weight = 1.0
blanchet@41313
   885
blanchet@41313
   886
fun add_term_weights weight (ATerm (s, tms)) =
blanchet@41313
   887
  (not (is_atp_variable s) andalso s <> "equal") ? Symtab.default (s, weight)
blanchet@41313
   888
  #> fold (add_term_weights weight) tms
blanchet@42538
   889
fun add_problem_line_weights weight (Formula (_, _, _, phi, _, _)) =
blanchet@42542
   890
    formula_fold (add_term_weights weight) phi
blanchet@42528
   891
  | add_problem_line_weights _ _ = I
blanchet@41313
   892
blanchet@41313
   893
fun add_conjectures_weights [] = I
blanchet@41313
   894
  | add_conjectures_weights conjs =
blanchet@41313
   895
    let val (hyps, conj) = split_last conjs in
blanchet@41313
   896
      add_problem_line_weights conj_weight conj
blanchet@41313
   897
      #> fold (add_problem_line_weights hyp_weight) hyps
blanchet@41313
   898
    end
blanchet@41313
   899
blanchet@41313
   900
fun add_facts_weights facts =
blanchet@41313
   901
  let
blanchet@41313
   902
    val num_facts = length facts
blanchet@41313
   903
    fun weight_of j =
blanchet@41313
   904
      fact_min_weight + (fact_max_weight - fact_min_weight) * Real.fromInt j
blanchet@41313
   905
                        / Real.fromInt num_facts
blanchet@41313
   906
  in
blanchet@41313
   907
    map weight_of (0 upto num_facts - 1) ~~ facts
blanchet@41313
   908
    |> fold (uncurry add_problem_line_weights)
blanchet@41313
   909
  end
blanchet@41313
   910
blanchet@41313
   911
(* Weights are from 0.0 (most important) to 1.0 (least important). *)
blanchet@41313
   912
fun atp_problem_weights problem =
blanchet@41313
   913
  Symtab.empty
blanchet@41313
   914
  |> add_conjectures_weights (these (AList.lookup (op =) problem conjsN))
blanchet@41313
   915
  |> add_facts_weights (these (AList.lookup (op =) problem factsN))
blanchet@41313
   916
  |> Symtab.dest
blanchet@41726
   917
  |> sort (prod_ord Real.compare string_ord o pairself swap)
blanchet@41313
   918
blanchet@38282
   919
end;