src/HOL/Tools/Sledgehammer/sledgehammer_translate.ML
author blanchet
Fri Aug 20 10:58:01 2010 +0200 (2010-08-20)
changeset 38608 01ed56c46259
parent 38606 3003ddbd46d9
child 38610 5266689abbc1
permissions -rw-r--r--
beta eta contract the Sledgehammer conjecture (and also the axioms, although this might not be needed), just like Metis does (implicitly);
another consequence of the transition to FOF
blanchet@38282
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_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@38282
     6
Translation of HOL to FOL.
blanchet@38282
     7
*)
blanchet@38282
     8
blanchet@38282
     9
signature SLEDGEHAMMER_TRANSLATE =
blanchet@38282
    10
sig
blanchet@38282
    11
  type 'a problem = 'a ATP_Problem.problem
blanchet@38282
    12
blanchet@38282
    13
  val axiom_prefix : string
blanchet@38282
    14
  val conjecture_prefix : string
blanchet@38282
    15
  val helper_prefix : string
blanchet@38282
    16
  val class_rel_clause_prefix : string
blanchet@38282
    17
  val arity_clause_prefix : string
blanchet@38282
    18
  val tfrees_name : string
blanchet@38282
    19
  val prepare_problem :
blanchet@38282
    20
    Proof.context -> bool -> bool -> bool -> bool -> term list -> term
blanchet@38282
    21
    -> (string * thm) list
blanchet@38282
    22
    -> string problem * string Symtab.table * int * string Vector.vector
blanchet@38282
    23
end;
blanchet@38282
    24
blanchet@38282
    25
structure Sledgehammer_Translate : SLEDGEHAMMER_TRANSLATE =
blanchet@38282
    26
struct
blanchet@38282
    27
blanchet@38282
    28
open ATP_Problem
blanchet@38282
    29
open Metis_Clauses
blanchet@38282
    30
open Sledgehammer_Util
blanchet@38282
    31
blanchet@38282
    32
val axiom_prefix = "ax_"
blanchet@38282
    33
val conjecture_prefix = "conj_"
blanchet@38282
    34
val helper_prefix = "help_"
blanchet@38282
    35
val class_rel_clause_prefix = "clrel_";
blanchet@38282
    36
val arity_clause_prefix = "arity_"
blanchet@38282
    37
val tfrees_name = "tfrees"
blanchet@38282
    38
blanchet@38282
    39
(* Freshness almost guaranteed! *)
blanchet@38282
    40
val sledgehammer_weak_prefix = "Sledgehammer:"
blanchet@38282
    41
blanchet@38282
    42
datatype fol_formula =
blanchet@38282
    43
  FOLFormula of {name: string,
blanchet@38282
    44
                 kind: kind,
blanchet@38282
    45
                 combformula: (name, combterm) formula,
blanchet@38282
    46
                 ctypes_sorts: typ list}
blanchet@38282
    47
blanchet@38282
    48
fun mk_anot phi = AConn (ANot, [phi])
blanchet@38282
    49
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
blanchet@38282
    50
fun mk_ahorn [] phi = phi
blanchet@38282
    51
  | mk_ahorn (phi :: phis) psi =
blanchet@38282
    52
    AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
blanchet@38282
    53
blanchet@38282
    54
fun combformula_for_prop thy =
blanchet@38282
    55
  let
blanchet@38282
    56
    val do_term = combterm_from_term thy
blanchet@38282
    57
    fun do_quant bs q s T t' =
blanchet@38518
    58
      let val s = Name.variant (map fst bs) s in
blanchet@38518
    59
        do_formula ((s, T) :: bs) t'
blanchet@38518
    60
        #>> (fn phi => AQuant (q, [`make_bound_var s], phi))
blanchet@38518
    61
      end
blanchet@38282
    62
    and do_conn bs c t1 t2 =
blanchet@38282
    63
      do_formula bs t1 ##>> do_formula bs t2
blanchet@38282
    64
      #>> (fn (phi1, phi2) => AConn (c, [phi1, phi2]))
blanchet@38282
    65
    and do_formula bs t =
blanchet@38282
    66
      case t of
blanchet@38282
    67
        @{const Not} $ t1 =>
blanchet@38282
    68
        do_formula bs t1 #>> (fn phi => AConn (ANot, [phi]))
blanchet@38282
    69
      | Const (@{const_name All}, _) $ Abs (s, T, t') =>
blanchet@38282
    70
        do_quant bs AForall s T t'
blanchet@38282
    71
      | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
blanchet@38282
    72
        do_quant bs AExists s T t'
blanchet@38282
    73
      | @{const "op &"} $ t1 $ t2 => do_conn bs AAnd t1 t2
blanchet@38282
    74
      | @{const "op |"} $ t1 $ t2 => do_conn bs AOr t1 t2
blanchet@38282
    75
      | @{const "op -->"} $ t1 $ t2 => do_conn bs AImplies t1 t2
blanchet@38282
    76
      | Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
blanchet@38282
    77
        do_conn bs AIff t1 t2
blanchet@38282
    78
      | _ => (fn ts => do_term bs (Envir.eta_contract t)
blanchet@38282
    79
                       |>> AAtom ||> union (op =) ts)
blanchet@38282
    80
  in do_formula [] end
blanchet@38282
    81
blanchet@38282
    82
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38282
    83
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@38282
    84
   the conclusion variable to False. (Cf. "transform_elim_term" in
blanchet@38282
    85
   "ATP_Systems".) *)
blanchet@38282
    86
fun transform_elim_term t =
blanchet@38282
    87
  case Logic.strip_imp_concl t of
blanchet@38282
    88
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@38282
    89
    subst_Vars [(z, @{const False})] t
blanchet@38282
    90
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
blanchet@38282
    91
  | _ => t
blanchet@38282
    92
blanchet@38282
    93
fun presimplify_term thy =
blanchet@38282
    94
  Skip_Proof.make_thm thy
blanchet@38282
    95
  #> Meson.presimplify
blanchet@38282
    96
  #> prop_of
blanchet@38282
    97
blanchet@38282
    98
fun concealed_bound_name j = sledgehammer_weak_prefix ^ Int.toString j
blanchet@38282
    99
fun conceal_bounds Ts t =
blanchet@38282
   100
  subst_bounds (map (Free o apfst concealed_bound_name)
blanchet@38282
   101
                    (0 upto length Ts - 1 ~~ Ts), t)
blanchet@38282
   102
fun reveal_bounds Ts =
blanchet@38282
   103
  subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
blanchet@38282
   104
                    (0 upto length Ts - 1 ~~ Ts))
blanchet@38282
   105
blanchet@38608
   106
(* Removes the lambdas from an equation of the form "t = (%x. u)".
blanchet@38608
   107
   (Cf. "extensionalize_theorem" in "Clausifier".) *)
blanchet@38608
   108
fun extensionalize_term t =
blanchet@38608
   109
  let
blanchet@38608
   110
    fun aux j (@{const Trueprop} $ t') = @{const Trueprop} $ aux j t'
blanchet@38608
   111
      | aux j (t as Const (s, Type (_, [Type (_, [_, T']),
blanchet@38608
   112
                                        Type (_, [_, res_T])]))
blanchet@38608
   113
                    $ t2 $ Abs (var_s, var_T, t')) =
blanchet@38608
   114
        if s = @{const_name "op ="} orelse s = @{const_name "=="} then
blanchet@38608
   115
          let val var_t = Var ((var_s, j), var_T) in
blanchet@38608
   116
            Const (s, T' --> T' --> res_T)
blanchet@38608
   117
              $ betapply (t2, var_t) $ subst_bound (var_t, t')
blanchet@38608
   118
            |> aux (j + 1)
blanchet@38608
   119
          end
blanchet@38608
   120
        else
blanchet@38608
   121
          t
blanchet@38608
   122
      | aux _ t = t
blanchet@38608
   123
  in aux (maxidx_of_term t + 1) t end
blanchet@38608
   124
blanchet@38282
   125
fun introduce_combinators_in_term ctxt kind t =
blanchet@38491
   126
  let val thy = ProofContext.theory_of ctxt in
blanchet@38491
   127
    if Meson.is_fol_term thy t then
blanchet@38491
   128
      t
blanchet@38491
   129
    else
blanchet@38491
   130
      let
blanchet@38491
   131
        fun aux Ts t =
blanchet@38491
   132
          case t of
blanchet@38491
   133
            @{const Not} $ t1 => @{const Not} $ aux Ts t1
blanchet@38491
   134
          | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
blanchet@38491
   135
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38491
   136
          | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
blanchet@38491
   137
            t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38491
   138
          | (t0 as @{const "op &"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38491
   139
          | (t0 as @{const "op |"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38491
   140
          | (t0 as @{const "op -->"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38491
   141
          | (t0 as Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])))
blanchet@38491
   142
              $ t1 $ t2 =>
blanchet@38491
   143
            t0 $ aux Ts t1 $ aux Ts t2
blanchet@38491
   144
          | _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
blanchet@38491
   145
                   t
blanchet@38491
   146
                 else
blanchet@38491
   147
                   t |> conceal_bounds Ts
blanchet@38491
   148
                     |> Envir.eta_contract
blanchet@38491
   149
                     |> cterm_of thy
blanchet@38491
   150
                     |> Clausifier.introduce_combinators_in_cterm
blanchet@38491
   151
                     |> prop_of |> Logic.dest_equals |> snd
blanchet@38491
   152
                     |> reveal_bounds Ts
blanchet@38491
   153
        val ([t], ctxt') = Variable.import_terms true [t] ctxt
blanchet@38491
   154
      in t |> aux [] |> singleton (Variable.export_terms ctxt' ctxt) end
blanchet@38491
   155
      handle THM _ =>
blanchet@38491
   156
             (* A type variable of sort "{}" will make abstraction fail. *)
blanchet@38491
   157
             case kind of
blanchet@38491
   158
               Axiom => HOLogic.true_const
blanchet@38491
   159
             | Conjecture => HOLogic.false_const
blanchet@38491
   160
  end
blanchet@38282
   161
blanchet@38282
   162
(* Metis's use of "resolve_tac" freezes the schematic variables. We simulate the
blanchet@38282
   163
   same in Sledgehammer to prevent the discovery of unreplable proofs. *)
blanchet@38282
   164
fun freeze_term t =
blanchet@38282
   165
  let
blanchet@38282
   166
    fun aux (t $ u) = aux t $ aux u
blanchet@38282
   167
      | aux (Abs (s, T, t)) = Abs (s, T, aux t)
blanchet@38282
   168
      | aux (Var ((s, i), T)) =
blanchet@38282
   169
        Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
blanchet@38282
   170
      | aux t = t
blanchet@38282
   171
  in t |> exists_subterm is_Var t ? aux end
blanchet@38282
   172
blanchet@38604
   173
(* "Object_Logic.atomize_term" isn't as powerful as it could be; for example,
blanchet@38604
   174
    it leaves metaequalities over "prop"s alone. *)
blanchet@38605
   175
val atomize_term =
blanchet@38605
   176
  let
blanchet@38605
   177
    fun aux (@{const Trueprop} $ t1) = t1
blanchet@38605
   178
      | aux (Const (@{const_name all}, _) $ Abs (s, T, t')) =
blanchet@38605
   179
        HOLogic.all_const T $ Abs (s, T, aux t')
blanchet@38605
   180
      | aux (@{const "==>"} $ t1 $ t2) = HOLogic.mk_imp (pairself aux (t1, t2))
blanchet@38605
   181
      | aux (Const (@{const_name "=="}, Type (_, [@{typ prop}, _])) $ t1 $ t2) =
blanchet@38605
   182
        HOLogic.eq_const HOLogic.boolT $ aux t1 $ aux t2
blanchet@38605
   183
      | aux (Const (@{const_name "=="}, Type (_, [T, _])) $ t1 $ t2) =
blanchet@38605
   184
        HOLogic.eq_const T $ t1 $ t2
blanchet@38605
   185
      | aux _ = raise Fail "aux"
blanchet@38605
   186
  in perhaps (try aux) end
blanchet@38604
   187
blanchet@38282
   188
(* making axiom and conjecture formulas *)
blanchet@38282
   189
fun make_formula ctxt presimp (name, kind, t) =
blanchet@38282
   190
  let
blanchet@38282
   191
    val thy = ProofContext.theory_of ctxt
blanchet@38608
   192
    val t = t |> Envir.beta_eta_contract
blanchet@38608
   193
              |> transform_elim_term
blanchet@38604
   194
              |> atomize_term
blanchet@38282
   195
    val t = t |> fastype_of t = HOLogic.boolT ? HOLogic.mk_Trueprop
blanchet@38282
   196
              |> extensionalize_term
blanchet@38282
   197
              |> presimp ? presimplify_term thy
blanchet@38282
   198
              |> perhaps (try (HOLogic.dest_Trueprop))
blanchet@38282
   199
              |> introduce_combinators_in_term ctxt kind
blanchet@38282
   200
              |> kind = Conjecture ? freeze_term
blanchet@38282
   201
    val (combformula, ctypes_sorts) = combformula_for_prop thy t []
blanchet@38282
   202
  in
blanchet@38282
   203
    FOLFormula {name = name, combformula = combformula, kind = kind,
blanchet@38282
   204
                ctypes_sorts = ctypes_sorts}
blanchet@38282
   205
  end
blanchet@38282
   206
blanchet@38282
   207
fun make_axiom ctxt presimp (name, th) =
blanchet@38282
   208
  (name, make_formula ctxt presimp (name, Axiom, prop_of th))
blanchet@38282
   209
fun make_conjectures ctxt ts =
blanchet@38282
   210
  map2 (fn j => fn t => make_formula ctxt true (Int.toString j, Conjecture, t))
blanchet@38282
   211
       (0 upto length ts - 1) ts
blanchet@38282
   212
blanchet@38282
   213
(** Helper facts **)
blanchet@38282
   214
blanchet@38282
   215
fun count_combterm (CombConst ((s, _), _, _)) =
blanchet@38282
   216
    Symtab.map_entry s (Integer.add 1)
blanchet@38282
   217
  | count_combterm (CombVar _) = I
blanchet@38282
   218
  | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
blanchet@38282
   219
fun count_combformula (AQuant (_, _, phi)) = count_combformula phi
blanchet@38282
   220
  | count_combformula (AConn (_, phis)) = fold count_combformula phis
blanchet@38282
   221
  | count_combformula (AAtom tm) = count_combterm tm
blanchet@38282
   222
fun count_fol_formula (FOLFormula {combformula, ...}) =
blanchet@38282
   223
  count_combformula combformula
blanchet@38282
   224
blanchet@38282
   225
val optional_helpers =
blanchet@38282
   226
  [(["c_COMBI", "c_COMBK"], @{thms COMBI_def COMBK_def}),
blanchet@38282
   227
   (["c_COMBB", "c_COMBC"], @{thms COMBB_def COMBC_def}),
blanchet@38282
   228
   (["c_COMBS"], @{thms COMBS_def})]
blanchet@38282
   229
val optional_typed_helpers =
blanchet@38282
   230
  [(["c_True", "c_False"], @{thms True_or_False}),
blanchet@38282
   231
   (["c_If"], @{thms if_True if_False True_or_False})]
blanchet@38606
   232
val mandatory_helpers = @{thms fequal_def}
blanchet@38282
   233
blanchet@38282
   234
val init_counters =
blanchet@38282
   235
  Symtab.make (maps (maps (map (rpair 0) o fst))
blanchet@38282
   236
                    [optional_helpers, optional_typed_helpers])
blanchet@38282
   237
blanchet@38282
   238
fun get_helper_facts ctxt is_FO full_types conjectures axioms =
blanchet@38282
   239
  let
blanchet@38282
   240
    val ct = fold (fold count_fol_formula) [conjectures, axioms] init_counters
blanchet@38282
   241
    fun is_needed c = the (Symtab.lookup ct c) > 0
blanchet@38282
   242
  in
blanchet@38282
   243
    (optional_helpers
blanchet@38282
   244
     |> full_types ? append optional_typed_helpers
blanchet@38282
   245
     |> maps (fn (ss, ths) =>
blanchet@38282
   246
                 if exists is_needed ss then map (`Thm.get_name_hint) ths
blanchet@38282
   247
                 else [])) @
blanchet@38282
   248
    (if is_FO then [] else map (`Thm.get_name_hint) mandatory_helpers)
blanchet@38282
   249
    |> map (snd o make_axiom ctxt false)
blanchet@38282
   250
  end
blanchet@38282
   251
blanchet@38282
   252
fun meta_not t = @{const "==>"} $ t $ @{prop False}
blanchet@38282
   253
blanchet@38282
   254
fun prepare_formulas ctxt full_types hyp_ts concl_t axioms =
blanchet@38282
   255
  let
blanchet@38282
   256
    val thy = ProofContext.theory_of ctxt
blanchet@38282
   257
    val axiom_ts = map (prop_of o snd) axioms
blanchet@38282
   258
    val hyp_ts =
blanchet@38282
   259
      if null hyp_ts then
blanchet@38282
   260
        []
blanchet@38282
   261
      else
blanchet@38282
   262
        (* Remove existing axioms from the conjecture, as this can dramatically
blanchet@38282
   263
           boost an ATP's performance (for some reason). *)
blanchet@38282
   264
        let
blanchet@38282
   265
          val axiom_table = fold (Termtab.update o rpair ()) axiom_ts
blanchet@38282
   266
                                 Termtab.empty
blanchet@38282
   267
        in hyp_ts |> filter_out (Termtab.defined axiom_table) end
blanchet@38282
   268
    val goal_t = Logic.list_implies (hyp_ts, concl_t)
blanchet@38282
   269
    val is_FO = Meson.is_fol_term thy goal_t
blanchet@38282
   270
    val subs = tfree_classes_of_terms [goal_t]
blanchet@38282
   271
    val supers = tvar_classes_of_terms axiom_ts
blanchet@38282
   272
    val tycons = type_consts_of_terms thy (goal_t :: axiom_ts)
blanchet@38282
   273
    (* TFrees in the conjecture; TVars in the axioms *)
blanchet@38282
   274
    val conjectures = map meta_not hyp_ts @ [concl_t] |> make_conjectures ctxt
blanchet@38282
   275
    val (axiom_names, axioms) =
blanchet@38282
   276
      ListPair.unzip (map (make_axiom ctxt true) axioms)
blanchet@38282
   277
    val helper_facts = get_helper_facts ctxt is_FO full_types conjectures axioms
blanchet@38282
   278
    val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@38282
   279
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@38282
   280
  in
blanchet@38282
   281
    (Vector.fromList axiom_names,
blanchet@38282
   282
     (conjectures, axioms, helper_facts, class_rel_clauses, arity_clauses))
blanchet@38282
   283
  end
blanchet@38282
   284
blanchet@38282
   285
fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t])
blanchet@38282
   286
blanchet@38282
   287
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
blanchet@38282
   288
  | fo_term_for_combtyp (CombTFree name) = ATerm (name, [])
blanchet@38282
   289
  | fo_term_for_combtyp (CombType (name, tys)) =
blanchet@38282
   290
    ATerm (name, map fo_term_for_combtyp tys)
blanchet@38282
   291
blanchet@38282
   292
fun fo_literal_for_type_literal (TyLitVar (class, name)) =
blanchet@38282
   293
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@38282
   294
  | fo_literal_for_type_literal (TyLitFree (class, name)) =
blanchet@38282
   295
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@38282
   296
blanchet@38282
   297
fun formula_for_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
blanchet@38282
   298
blanchet@38282
   299
fun fo_term_for_combterm full_types =
blanchet@38282
   300
  let
blanchet@38282
   301
    fun aux top_level u =
blanchet@38282
   302
      let
blanchet@38282
   303
        val (head, args) = strip_combterm_comb u
blanchet@38282
   304
        val (x, ty_args) =
blanchet@38282
   305
          case head of
blanchet@38282
   306
            CombConst (name as (s, s'), _, ty_args) =>
blanchet@38496
   307
            let val ty_args = if full_types then [] else ty_args in
blanchet@38496
   308
              if s = "equal" then
blanchet@38496
   309
                if top_level andalso length args = 2 then (name, [])
blanchet@38496
   310
                else (("c_fequal", @{const_name fequal}), ty_args)
blanchet@38496
   311
              else if top_level then
blanchet@38496
   312
                case s of
blanchet@38496
   313
                  "c_False" => (("$false", s'), [])
blanchet@38496
   314
                | "c_True" => (("$true", s'), [])
blanchet@38496
   315
                | _ => (name, ty_args)
blanchet@38496
   316
              else
blanchet@38496
   317
                (name, ty_args)
blanchet@38496
   318
            end
blanchet@38282
   319
          | CombVar (name, _) => (name, [])
blanchet@38282
   320
          | CombApp _ => raise Fail "impossible \"CombApp\""
blanchet@38282
   321
        val t = ATerm (x, map fo_term_for_combtyp ty_args @
blanchet@38282
   322
                          map (aux false) args)
blanchet@38282
   323
    in
blanchet@38282
   324
      if full_types then wrap_type (fo_term_for_combtyp (combtyp_of u)) t else t
blanchet@38282
   325
    end
blanchet@38282
   326
  in aux true end
blanchet@38282
   327
blanchet@38282
   328
fun formula_for_combformula full_types =
blanchet@38282
   329
  let
blanchet@38282
   330
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
blanchet@38282
   331
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
blanchet@38282
   332
      | aux (AAtom tm) = AAtom (fo_term_for_combterm full_types tm)
blanchet@38282
   333
  in aux end
blanchet@38282
   334
blanchet@38282
   335
fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) =
blanchet@38282
   336
  mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
blanchet@38282
   337
                (type_literals_for_types ctypes_sorts))
blanchet@38282
   338
           (formula_for_combformula full_types combformula)
blanchet@38282
   339
blanchet@38282
   340
fun problem_line_for_fact prefix full_types
blanchet@38282
   341
                          (formula as FOLFormula {name, kind, ...}) =
blanchet@38282
   342
  Fof (prefix ^ ascii_of name, kind, formula_for_axiom full_types formula)
blanchet@38282
   343
blanchet@38282
   344
fun problem_line_for_class_rel_clause (ClassRelClause {name, subclass,
blanchet@38282
   345
                                                       superclass, ...}) =
blanchet@38282
   346
  let val ty_arg = ATerm (("T", "T"), []) in
blanchet@38282
   347
    Fof (class_rel_clause_prefix ^ ascii_of name, Axiom,
blanchet@38282
   348
         AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
blanchet@38282
   349
                           AAtom (ATerm (superclass, [ty_arg]))]))
blanchet@38282
   350
  end
blanchet@38282
   351
blanchet@38282
   352
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
blanchet@38282
   353
    (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
blanchet@38282
   354
  | fo_literal_for_arity_literal (TVarLit (c, sort)) =
blanchet@38282
   355
    (false, ATerm (c, [ATerm (sort, [])]))
blanchet@38282
   356
blanchet@38282
   357
fun problem_line_for_arity_clause (ArityClause {name, conclLit, premLits,
blanchet@38282
   358
                                                ...}) =
blanchet@38282
   359
  Fof (arity_clause_prefix ^ ascii_of name, Axiom,
blanchet@38282
   360
       mk_ahorn (map (formula_for_fo_literal o apfst not
blanchet@38282
   361
                      o fo_literal_for_arity_literal) premLits)
blanchet@38282
   362
                (formula_for_fo_literal
blanchet@38282
   363
                     (fo_literal_for_arity_literal conclLit)))
blanchet@38282
   364
blanchet@38282
   365
fun problem_line_for_conjecture full_types
blanchet@38282
   366
                                (FOLFormula {name, kind, combformula, ...}) =
blanchet@38282
   367
  Fof (conjecture_prefix ^ name, kind,
blanchet@38282
   368
       formula_for_combformula full_types combformula)
blanchet@38282
   369
blanchet@38282
   370
fun free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) =
blanchet@38282
   371
  map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
blanchet@38282
   372
blanchet@38282
   373
fun problem_line_for_free_type lit =
blanchet@38282
   374
  Fof (tfrees_name, Conjecture, mk_anot (formula_for_fo_literal lit))
blanchet@38282
   375
fun problem_lines_for_free_types conjectures =
blanchet@38282
   376
  let
blanchet@38282
   377
    val litss = map free_type_literals_for_conjecture conjectures
blanchet@38282
   378
    val lits = fold (union (op =)) litss []
blanchet@38282
   379
  in map problem_line_for_free_type lits end
blanchet@38282
   380
blanchet@38282
   381
(** "hBOOL" and "hAPP" **)
blanchet@38282
   382
blanchet@38282
   383
type const_info = {min_arity: int, max_arity: int, sub_level: bool}
blanchet@38282
   384
blanchet@38282
   385
fun consider_term top_level (ATerm ((s, _), ts)) =
blanchet@38282
   386
  (if is_tptp_variable s then
blanchet@38282
   387
     I
blanchet@38282
   388
   else
blanchet@38282
   389
     let val n = length ts in
blanchet@38282
   390
       Symtab.map_default
blanchet@38282
   391
           (s, {min_arity = n, max_arity = 0, sub_level = false})
blanchet@38282
   392
           (fn {min_arity, max_arity, sub_level} =>
blanchet@38282
   393
               {min_arity = Int.min (n, min_arity),
blanchet@38282
   394
                max_arity = Int.max (n, max_arity),
blanchet@38282
   395
                sub_level = sub_level orelse not top_level})
blanchet@38282
   396
     end)
blanchet@38282
   397
  #> fold (consider_term (top_level andalso s = type_wrapper_name)) ts
blanchet@38282
   398
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi
blanchet@38282
   399
  | consider_formula (AConn (_, phis)) = fold consider_formula phis
blanchet@38282
   400
  | consider_formula (AAtom tm) = consider_term true tm
blanchet@38282
   401
blanchet@38282
   402
fun consider_problem_line (Fof (_, _, phi)) = consider_formula phi
blanchet@38282
   403
fun consider_problem problem = fold (fold consider_problem_line o snd) problem
blanchet@38282
   404
blanchet@38282
   405
fun const_table_for_problem explicit_apply problem =
blanchet@38282
   406
  if explicit_apply then NONE
blanchet@38282
   407
  else SOME (Symtab.empty |> consider_problem problem)
blanchet@38282
   408
blanchet@38282
   409
fun min_arity_of thy full_types NONE s =
blanchet@38282
   410
    (if s = "equal" orelse s = type_wrapper_name orelse
blanchet@38282
   411
        String.isPrefix type_const_prefix s orelse
blanchet@38282
   412
        String.isPrefix class_prefix s then
blanchet@38282
   413
       16383 (* large number *)
blanchet@38282
   414
     else if full_types then
blanchet@38282
   415
       0
blanchet@38282
   416
     else case strip_prefix_and_undo_ascii const_prefix s of
blanchet@38282
   417
       SOME s' => num_type_args thy (invert_const s')
blanchet@38282
   418
     | NONE => 0)
blanchet@38282
   419
  | min_arity_of _ _ (SOME the_const_tab) s =
blanchet@38282
   420
    case Symtab.lookup the_const_tab s of
blanchet@38282
   421
      SOME ({min_arity, ...} : const_info) => min_arity
blanchet@38282
   422
    | NONE => 0
blanchet@38282
   423
blanchet@38282
   424
fun full_type_of (ATerm ((s, _), [ty, _])) =
blanchet@38282
   425
    if s = type_wrapper_name then ty else raise Fail "expected type wrapper"
blanchet@38282
   426
  | full_type_of _ = raise Fail "expected type wrapper"
blanchet@38282
   427
blanchet@38282
   428
fun list_hAPP_rev _ t1 [] = t1
blanchet@38282
   429
  | list_hAPP_rev NONE t1 (t2 :: ts2) =
blanchet@38282
   430
    ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
blanchet@38282
   431
  | list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
blanchet@38282
   432
    let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
blanchet@38282
   433
                         [full_type_of t2, ty]) in
blanchet@38282
   434
      ATerm (`I "hAPP", [wrap_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
blanchet@38282
   435
    end
blanchet@38282
   436
blanchet@38282
   437
fun repair_applications_in_term thy full_types const_tab =
blanchet@38282
   438
  let
blanchet@38282
   439
    fun aux opt_ty (ATerm (name as (s, _), ts)) =
blanchet@38282
   440
      if s = type_wrapper_name then
blanchet@38282
   441
        case ts of
blanchet@38282
   442
          [t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2])
blanchet@38282
   443
        | _ => raise Fail "malformed type wrapper"
blanchet@38282
   444
      else
blanchet@38282
   445
        let
blanchet@38282
   446
          val ts = map (aux NONE) ts
blanchet@38282
   447
          val (ts1, ts2) = chop (min_arity_of thy full_types const_tab s) ts
blanchet@38282
   448
        in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
blanchet@38282
   449
  in aux NONE end
blanchet@38282
   450
blanchet@38282
   451
fun boolify t = ATerm (`I "hBOOL", [t])
blanchet@38282
   452
blanchet@38282
   453
(* True if the constant ever appears outside of the top-level position in
blanchet@38282
   454
   literals, or if it appears with different arities (e.g., because of different
blanchet@38282
   455
   type instantiations). If false, the constant always receives all of its
blanchet@38282
   456
   arguments and is used as a predicate. *)
blanchet@38282
   457
fun is_predicate NONE s =
blanchet@38589
   458
    s = "equal" orelse s = "$false" orelse s = "$true" orelse
blanchet@38589
   459
    String.isPrefix type_const_prefix s orelse String.isPrefix class_prefix s
blanchet@38282
   460
  | is_predicate (SOME the_const_tab) s =
blanchet@38282
   461
    case Symtab.lookup the_const_tab s of
blanchet@38282
   462
      SOME {min_arity, max_arity, sub_level} =>
blanchet@38282
   463
      not sub_level andalso min_arity = max_arity
blanchet@38282
   464
    | NONE => false
blanchet@38282
   465
blanchet@38282
   466
fun repair_predicates_in_term const_tab (t as ATerm ((s, _), ts)) =
blanchet@38282
   467
  if s = type_wrapper_name then
blanchet@38282
   468
    case ts of
blanchet@38282
   469
      [_, t' as ATerm ((s', _), _)] =>
blanchet@38282
   470
      if is_predicate const_tab s' then t' else boolify t
blanchet@38282
   471
    | _ => raise Fail "malformed type wrapper"
blanchet@38282
   472
  else
blanchet@38282
   473
    t |> not (is_predicate const_tab s) ? boolify
blanchet@38282
   474
blanchet@38282
   475
fun close_universally phi =
blanchet@38282
   476
  let
blanchet@38282
   477
    fun term_vars bounds (ATerm (name as (s, _), tms)) =
blanchet@38282
   478
        (is_tptp_variable s andalso not (member (op =) bounds name))
blanchet@38282
   479
          ? insert (op =) name
blanchet@38282
   480
        #> fold (term_vars bounds) tms
blanchet@38282
   481
    fun formula_vars bounds (AQuant (q, xs, phi)) =
blanchet@38282
   482
        formula_vars (xs @ bounds) phi
blanchet@38282
   483
      | formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
blanchet@38282
   484
      | formula_vars bounds (AAtom tm) = term_vars bounds tm
blanchet@38282
   485
  in
blanchet@38282
   486
    case formula_vars [] phi [] of [] => phi | xs => AQuant (AForall, xs, phi)
blanchet@38282
   487
  end
blanchet@38282
   488
blanchet@38282
   489
fun repair_formula thy explicit_forall full_types const_tab =
blanchet@38282
   490
  let
blanchet@38282
   491
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
blanchet@38282
   492
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
blanchet@38282
   493
      | aux (AAtom tm) =
blanchet@38282
   494
        AAtom (tm |> repair_applications_in_term thy full_types const_tab
blanchet@38282
   495
                  |> repair_predicates_in_term const_tab)
blanchet@38282
   496
  in aux #> explicit_forall ? close_universally end
blanchet@38282
   497
blanchet@38282
   498
fun repair_problem_line thy explicit_forall full_types const_tab
blanchet@38282
   499
                        (Fof (ident, kind, phi)) =
blanchet@38282
   500
  Fof (ident, kind, repair_formula thy explicit_forall full_types const_tab phi)
blanchet@38282
   501
fun repair_problem_with_const_table thy =
blanchet@38282
   502
  map o apsnd o map ooo repair_problem_line thy
blanchet@38282
   503
blanchet@38282
   504
fun repair_problem thy explicit_forall full_types explicit_apply problem =
blanchet@38282
   505
  repair_problem_with_const_table thy explicit_forall full_types
blanchet@38282
   506
      (const_table_for_problem explicit_apply problem) problem
blanchet@38282
   507
blanchet@38282
   508
fun prepare_problem ctxt readable_names explicit_forall full_types
blanchet@38282
   509
                    explicit_apply hyp_ts concl_t axiom_ts =
blanchet@38282
   510
  let
blanchet@38282
   511
    val thy = ProofContext.theory_of ctxt
blanchet@38282
   512
    val (axiom_names, (conjectures, axioms, helper_facts, class_rel_clauses,
blanchet@38282
   513
                       arity_clauses)) =
blanchet@38282
   514
      prepare_formulas ctxt full_types hyp_ts concl_t axiom_ts
blanchet@38282
   515
    val axiom_lines = map (problem_line_for_fact axiom_prefix full_types) axioms
blanchet@38282
   516
    val helper_lines =
blanchet@38282
   517
      map (problem_line_for_fact helper_prefix full_types) helper_facts
blanchet@38282
   518
    val conjecture_lines =
blanchet@38282
   519
      map (problem_line_for_conjecture full_types) conjectures
blanchet@38282
   520
    val tfree_lines = problem_lines_for_free_types conjectures
blanchet@38282
   521
    val class_rel_lines =
blanchet@38282
   522
      map problem_line_for_class_rel_clause class_rel_clauses
blanchet@38282
   523
    val arity_lines = map problem_line_for_arity_clause arity_clauses
blanchet@38282
   524
    (* Reordering these might or might not confuse the proof reconstruction
blanchet@38282
   525
       code or the SPASS Flotter hack. *)
blanchet@38282
   526
    val problem =
blanchet@38282
   527
      [("Relevant facts", axiom_lines),
blanchet@38282
   528
       ("Class relationships", class_rel_lines),
blanchet@38282
   529
       ("Arity declarations", arity_lines),
blanchet@38282
   530
       ("Helper facts", helper_lines),
blanchet@38282
   531
       ("Conjectures", conjecture_lines),
blanchet@38282
   532
       ("Type variables", tfree_lines)]
blanchet@38282
   533
      |> repair_problem thy explicit_forall full_types explicit_apply
blanchet@38282
   534
    val (problem, pool) = nice_tptp_problem readable_names problem
blanchet@38282
   535
    val conjecture_offset =
blanchet@38282
   536
      length axiom_lines + length class_rel_lines + length arity_lines
blanchet@38282
   537
      + length helper_lines
blanchet@38282
   538
  in
blanchet@38282
   539
    (problem,
blanchet@38282
   540
     case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
blanchet@38282
   541
     conjecture_offset, axiom_names)
blanchet@38282
   542
  end
blanchet@38282
   543
blanchet@38282
   544
end;