src/HOL/Tools/Metis/metis_translate.ML
author blanchet
Mon Jun 06 20:36:35 2011 +0200 (2011-06-06)
changeset 43173 b98daa96d043
parent 43160 d4f347508cd4
child 43174 f497a1e97d37
permissions -rw-r--r--
don't pass "~ " to new Metis
blanchet@39958
     1
(*  Title:      HOL/Tools/Metis/metis_translate.ML
blanchet@38027
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@39497
     3
    Author:     Kong W. Susanto, Cambridge University Computer Laboratory
blanchet@39497
     4
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@36393
     5
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     6
blanchet@39494
     7
Translation of HOL to FOL for Metis.
paulson@15347
     8
*)
paulson@15347
     9
blanchet@39494
    10
signature METIS_TRANSLATE =
wenzelm@24310
    11
sig
blanchet@43085
    12
  type type_literal = ATP_Translate.type_literal
blanchet@43102
    13
  type type_sys = ATP_Translate.type_sys
blanchet@37577
    14
blanchet@43103
    15
  datatype mode = FO | HO | FT | MX
blanchet@43085
    16
blanchet@43159
    17
  datatype isa_thm =
blanchet@43159
    18
    Isa_Reflexive_or_Trivial |
blanchet@43159
    19
    Isa_Raw of thm
blanchet@43159
    20
blanchet@40157
    21
  type metis_problem =
blanchet@43159
    22
    {axioms : (Metis_Thm.thm * isa_thm) list,
blanchet@43094
    23
     tfrees : type_literal list,
blanchet@43094
    24
     old_skolems : (string * term) list}
blanchet@39497
    25
blanchet@43094
    26
  val metis_equal : string
blanchet@43094
    27
  val metis_predicator : string
blanchet@43094
    28
  val metis_app_op : string
blanchet@43106
    29
  val metis_type_tag : string
blanchet@42098
    30
  val metis_generated_var_prefix : string
blanchet@43104
    31
  val metis_name_table : ((string * int) * (string * bool)) list
blanchet@39886
    32
  val reveal_old_skolem_terms : (string * term) list -> term -> term
blanchet@39497
    33
  val string_of_mode : mode -> string
blanchet@40157
    34
  val prepare_metis_problem :
blanchet@43102
    35
    Proof.context -> mode -> type_sys option -> thm list -> thm list
blanchet@43094
    36
    -> mode * int Symtab.table * metis_problem
wenzelm@24310
    37
end
paulson@15347
    38
blanchet@39494
    39
structure Metis_Translate : METIS_TRANSLATE =
paulson@15347
    40
struct
paulson@15347
    41
blanchet@43092
    42
open ATP_Problem
blanchet@43085
    43
open ATP_Translate
paulson@15347
    44
blanchet@43094
    45
val metis_equal = "="
blanchet@43094
    46
val metis_predicator = "{}"
blanchet@43094
    47
val metis_app_op = "."
blanchet@43104
    48
val metis_type_tag = ":"
blanchet@43085
    49
val metis_generated_var_prefix = "_"
quigley@17150
    50
blanchet@43094
    51
val metis_name_table =
blanchet@43104
    52
  [((tptp_equal, 2), (metis_equal, false)),
blanchet@43104
    53
   ((tptp_old_equal, 2), (metis_equal, false)),
blanchet@43104
    54
   ((const_prefix ^ predicator_name, 1), (metis_predicator, false)),
blanchet@43130
    55
   ((prefixed_app_op_name, 2), (metis_app_op, false)),
blanchet@43130
    56
   ((prefixed_type_tag_name, 2), (metis_type_tag, true))]
blanchet@43094
    57
blanchet@40145
    58
fun predicate_of thy ((@{const Not} $ P), pos) = predicate_of thy (P, not pos)
blanchet@40145
    59
  | predicate_of thy (t, pos) =
blanchet@40145
    60
    (combterm_from_term thy [] (Envir.eta_contract t), pos)
blanchet@37577
    61
blanchet@40145
    62
fun literals_of_term1 args thy (@{const Trueprop} $ P) =
blanchet@40145
    63
    literals_of_term1 args thy P
blanchet@40145
    64
  | literals_of_term1 args thy (@{const HOL.disj} $ P $ Q) =
blanchet@40145
    65
    literals_of_term1 (literals_of_term1 args thy P) thy Q
blanchet@40145
    66
  | literals_of_term1 (lits, ts) thy P =
blanchet@40145
    67
    let val ((pred, ts'), pol) = predicate_of thy (P, true) in
blanchet@43085
    68
      ((pol, pred) :: lits, union (op =) ts ts')
blanchet@37577
    69
    end
blanchet@37577
    70
val literals_of_term = literals_of_term1 ([], [])
blanchet@37577
    71
blanchet@39896
    72
fun old_skolem_const_name i j num_T_args =
blanchet@39896
    73
  old_skolem_const_prefix ^ Long_Name.separator ^
wenzelm@41491
    74
  (space_implode Long_Name.separator (map string_of_int [i, j, num_T_args]))
blanchet@37577
    75
blanchet@39886
    76
fun conceal_old_skolem_terms i old_skolems t =
blanchet@39953
    77
  if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
blanchet@37577
    78
    let
blanchet@39886
    79
      fun aux old_skolems
blanchet@39953
    80
             (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
blanchet@37577
    81
          let
blanchet@39886
    82
            val (old_skolems, s) =
blanchet@37577
    83
              if i = ~1 then
blanchet@39886
    84
                (old_skolems, @{const_name undefined})
blanchet@39886
    85
              else case AList.find (op aconv) old_skolems t of
blanchet@39886
    86
                s :: _ => (old_skolems, s)
blanchet@37577
    87
              | [] =>
blanchet@37577
    88
                let
blanchet@39896
    89
                  val s = old_skolem_const_name i (length old_skolems)
blanchet@39896
    90
                                                (length (Term.add_tvarsT T []))
blanchet@39886
    91
                in ((s, t) :: old_skolems, s) end
blanchet@39886
    92
          in (old_skolems, Const (s, T)) end
blanchet@39886
    93
        | aux old_skolems (t1 $ t2) =
blanchet@37577
    94
          let
blanchet@39886
    95
            val (old_skolems, t1) = aux old_skolems t1
blanchet@39886
    96
            val (old_skolems, t2) = aux old_skolems t2
blanchet@39886
    97
          in (old_skolems, t1 $ t2) end
blanchet@39886
    98
        | aux old_skolems (Abs (s, T, t')) =
blanchet@39886
    99
          let val (old_skolems, t') = aux old_skolems t' in
blanchet@39886
   100
            (old_skolems, Abs (s, T, t'))
blanchet@37577
   101
          end
blanchet@39886
   102
        | aux old_skolems t = (old_skolems, t)
blanchet@39886
   103
    in aux old_skolems t end
blanchet@37577
   104
  else
blanchet@39886
   105
    (old_skolems, t)
blanchet@37577
   106
blanchet@39886
   107
fun reveal_old_skolem_terms old_skolems =
blanchet@37632
   108
  map_aterms (fn t as Const (s, _) =>
blanchet@39896
   109
                 if String.isPrefix old_skolem_const_prefix s then
blanchet@39886
   110
                   AList.lookup (op =) old_skolems s |> the
blanchet@37632
   111
                   |> map_types Type_Infer.paramify_vars
blanchet@37632
   112
                 else
blanchet@37632
   113
                   t
blanchet@37632
   114
               | t => t)
blanchet@37632
   115
blanchet@37577
   116
blanchet@39497
   117
(* ------------------------------------------------------------------------- *)
blanchet@39497
   118
(* HOL to FOL  (Isabelle to Metis)                                           *)
blanchet@39497
   119
(* ------------------------------------------------------------------------- *)
blanchet@39497
   120
blanchet@43103
   121
(* first-order, higher-order, fully-typed, mode X (fleXible) *)
blanchet@43103
   122
datatype mode = FO | HO | FT | MX
blanchet@39497
   123
blanchet@39497
   124
fun string_of_mode FO = "FO"
blanchet@39497
   125
  | string_of_mode HO = "HO"
blanchet@39497
   126
  | string_of_mode FT = "FT"
blanchet@43103
   127
  | string_of_mode MX = "MX"
blanchet@39497
   128
blanchet@42745
   129
fun fn_isa_to_met_sublevel "equal" = "c_fequal"
blanchet@41139
   130
  | fn_isa_to_met_sublevel "c_False" = "c_fFalse"
blanchet@41139
   131
  | fn_isa_to_met_sublevel "c_True" = "c_fTrue"
blanchet@41139
   132
  | fn_isa_to_met_sublevel "c_Not" = "c_fNot"
blanchet@41139
   133
  | fn_isa_to_met_sublevel "c_conj" = "c_fconj"
blanchet@41139
   134
  | fn_isa_to_met_sublevel "c_disj" = "c_fdisj"
blanchet@41139
   135
  | fn_isa_to_met_sublevel "c_implies" = "c_fimplies"
blanchet@39497
   136
  | fn_isa_to_met_sublevel x = x
blanchet@42758
   137
blanchet@43094
   138
fun fn_isa_to_met_toplevel "equal" = metis_equal
blanchet@39497
   139
  | fn_isa_to_met_toplevel x = x
blanchet@39497
   140
blanchet@39497
   141
fun metis_lit b c args = (b, (c, args));
blanchet@39497
   142
blanchet@42562
   143
fun metis_term_from_typ (Type (s, Ts)) =
blanchet@42562
   144
    Metis_Term.Fn (make_fixed_type_const s, map metis_term_from_typ Ts)
blanchet@42562
   145
  | metis_term_from_typ (TFree (s, _)) =
blanchet@42562
   146
    Metis_Term.Fn (make_fixed_type_var s, [])
blanchet@42562
   147
  | metis_term_from_typ (TVar (x, _)) =
blanchet@42562
   148
    Metis_Term.Var (make_schematic_type_var x)
blanchet@39497
   149
blanchet@39497
   150
(*These two functions insert type literals before the real literals. That is the
blanchet@39497
   151
  opposite order from TPTP linkup, but maybe OK.*)
blanchet@39497
   152
blanchet@39497
   153
fun hol_term_to_fol_FO tm =
blanchet@39497
   154
  case strip_combterm_comb tm of
blanchet@42562
   155
      (CombConst ((c, _), _, Ts), tms) =>
blanchet@42562
   156
        let val tyargs = map metis_term_from_typ Ts
blanchet@42562
   157
            val args = map hol_term_to_fol_FO tms
blanchet@39497
   158
        in Metis_Term.Fn (c, tyargs @ args) end
blanchet@39497
   159
    | (CombVar ((v, _), _), []) => Metis_Term.Var v
blanchet@39497
   160
    | _ => raise Fail "non-first-order combterm"
blanchet@39497
   161
blanchet@42562
   162
fun hol_term_to_fol_HO (CombConst ((a, _), _, Ts)) =
blanchet@43094
   163
    Metis_Term.Fn (fn_isa_to_met_sublevel a, map metis_term_from_typ Ts)
blanchet@39497
   164
  | hol_term_to_fol_HO (CombVar ((s, _), _)) = Metis_Term.Var s
blanchet@39497
   165
  | hol_term_to_fol_HO (CombApp (tm1, tm2)) =
blanchet@43094
   166
    Metis_Term.Fn (metis_app_op, map hol_term_to_fol_HO [tm1, tm2])
blanchet@39497
   167
blanchet@39497
   168
(*The fully-typed translation, to avoid type errors*)
blanchet@42562
   169
fun tag_with_type tm T =
blanchet@43104
   170
  Metis_Term.Fn (metis_type_tag, [tm, metis_term_from_typ T])
blanchet@39497
   171
blanchet@41138
   172
fun hol_term_to_fol_FT (CombVar ((s, _), ty)) =
blanchet@41138
   173
    tag_with_type (Metis_Term.Var s) ty
blanchet@41138
   174
  | hol_term_to_fol_FT (CombConst ((a, _), ty, _)) =
blanchet@41138
   175
    tag_with_type (Metis_Term.Fn (fn_isa_to_met_sublevel a, [])) ty
blanchet@41138
   176
  | hol_term_to_fol_FT (tm as CombApp (tm1,tm2)) =
blanchet@43094
   177
    tag_with_type
blanchet@43094
   178
        (Metis_Term.Fn (metis_app_op, map hol_term_to_fol_FT [tm1, tm2]))
blanchet@43094
   179
        (combtyp_of tm)
blanchet@39497
   180
blanchet@43085
   181
fun hol_literal_to_fol FO (pos, tm) =
blanchet@42562
   182
      let
blanchet@42562
   183
        val (CombConst((p, _), _, Ts), tms) = strip_combterm_comb tm
blanchet@42562
   184
        val tylits = if p = "equal" then [] else map metis_term_from_typ Ts
blanchet@42562
   185
        val lits = map hol_term_to_fol_FO tms
blanchet@39497
   186
      in metis_lit pos (fn_isa_to_met_toplevel p) (tylits @ lits) end
blanchet@43085
   187
  | hol_literal_to_fol HO (pos, tm) =
blanchet@39497
   188
     (case strip_combterm_comb tm of
blanchet@39497
   189
          (CombConst(("equal", _), _, _), tms) =>
blanchet@43094
   190
            metis_lit pos metis_equal (map hol_term_to_fol_HO tms)
blanchet@43094
   191
        | _ => metis_lit pos metis_predicator [hol_term_to_fol_HO tm])
blanchet@43085
   192
  | hol_literal_to_fol FT (pos, tm) =
blanchet@39497
   193
     (case strip_combterm_comb tm of
blanchet@39497
   194
          (CombConst(("equal", _), _, _), tms) =>
blanchet@43094
   195
            metis_lit pos metis_equal (map hol_term_to_fol_FT tms)
blanchet@43094
   196
        | _ => metis_lit pos metis_predicator [hol_term_to_fol_FT tm])
blanchet@39497
   197
blanchet@40145
   198
fun literals_of_hol_term thy mode t =
blanchet@40145
   199
  let val (lits, types_sorts) = literals_of_term thy t in
blanchet@40145
   200
    (map (hol_literal_to_fol mode) lits, types_sorts)
blanchet@40145
   201
  end
blanchet@39497
   202
blanchet@39497
   203
(*Sign should be "true" for conjecture type constraints, "false" for type lits in clauses.*)
blanchet@39497
   204
fun metis_of_type_literals pos (TyLitVar ((s, _), (s', _))) =
blanchet@39497
   205
    metis_lit pos s [Metis_Term.Var s']
blanchet@39497
   206
  | metis_of_type_literals pos (TyLitFree ((s, _), (s', _))) =
blanchet@39497
   207
    metis_lit pos s [Metis_Term.Fn (s',[])]
blanchet@39497
   208
blanchet@42352
   209
fun has_default_sort _ (TVar _) = false
blanchet@42352
   210
  | has_default_sort ctxt (TFree (x, s)) =
blanchet@42352
   211
    (s = the_default [] (Variable.def_sort ctxt (x, ~1)));
blanchet@39497
   212
blanchet@39497
   213
fun metis_of_tfree tf =
blanchet@39497
   214
  Metis_Thm.axiom (Metis_LiteralSet.singleton (metis_of_type_literals true tf));
blanchet@39497
   215
blanchet@43090
   216
fun hol_thm_to_fol is_conjecture ctxt mode j old_skolems th =
blanchet@39497
   217
  let
wenzelm@42361
   218
    val thy = Proof_Context.theory_of ctxt
blanchet@39886
   219
    val (old_skolems, (mlits, types_sorts)) =
blanchet@39888
   220
     th |> prop_of |> Logic.strip_imp_concl
blanchet@39888
   221
        |> conceal_old_skolem_terms j old_skolems
blanchet@40145
   222
        ||> (HOLogic.dest_Trueprop #> literals_of_hol_term thy mode)
blanchet@39497
   223
  in
blanchet@39497
   224
    if is_conjecture then
blanchet@39497
   225
      (Metis_Thm.axiom (Metis_LiteralSet.fromList mlits),
blanchet@43085
   226
       raw_type_literals_for_types types_sorts, old_skolems)
blanchet@39497
   227
    else
blanchet@39497
   228
      let
blanchet@42352
   229
        val tylits = types_sorts |> filter_out (has_default_sort ctxt)
blanchet@43085
   230
                                 |> raw_type_literals_for_types
blanchet@43090
   231
        val mtylits = map (metis_of_type_literals false) tylits
blanchet@39497
   232
      in
blanchet@39497
   233
        (Metis_Thm.axiom (Metis_LiteralSet.fromList(mtylits @ mlits)), [],
blanchet@39886
   234
         old_skolems)
blanchet@39497
   235
      end
blanchet@39497
   236
  end;
blanchet@39497
   237
blanchet@39497
   238
(* ------------------------------------------------------------------------- *)
blanchet@39497
   239
(* Logic maps manage the interface between HOL and first-order logic.        *)
blanchet@39497
   240
(* ------------------------------------------------------------------------- *)
blanchet@39497
   241
blanchet@43159
   242
datatype isa_thm =
blanchet@43159
   243
  Isa_Reflexive_or_Trivial |
blanchet@43159
   244
  Isa_Raw of thm
blanchet@43159
   245
blanchet@40157
   246
type metis_problem =
blanchet@43159
   247
  {axioms : (Metis_Thm.thm * isa_thm) list,
blanchet@43094
   248
   tfrees : type_literal list,
blanchet@43094
   249
   old_skolems : (string * term) list}
blanchet@39497
   250
blanchet@39497
   251
fun is_quasi_fol_clause thy =
blanchet@39886
   252
  Meson.is_fol_term thy o snd o conceal_old_skolem_terms ~1 [] o prop_of
blanchet@39497
   253
blanchet@39497
   254
(*Extract TFree constraints from context to include as conjecture clauses*)
blanchet@39497
   255
fun init_tfrees ctxt =
blanchet@39497
   256
  let fun add ((a,i),s) Ts = if i = ~1 then TFree(a,s) :: Ts else Ts in
blanchet@39497
   257
    Vartab.fold add (#2 (Variable.constraints_of ctxt)) []
blanchet@43085
   258
    |> raw_type_literals_for_types
blanchet@39497
   259
  end;
blanchet@39497
   260
blanchet@39497
   261
fun const_in_metis c (pred, tm_list) =
blanchet@39497
   262
  let
blanchet@39497
   263
    fun in_mterm (Metis_Term.Var _) = false
blanchet@41156
   264
      | in_mterm (Metis_Term.Fn (nm, tm_list)) =
blanchet@41156
   265
        c = nm orelse exists in_mterm tm_list
blanchet@41156
   266
  in c = pred orelse exists in_mterm tm_list end
blanchet@39497
   267
blanchet@39497
   268
(* ARITY CLAUSE *)
blanchet@39497
   269
fun m_arity_cls (TConsLit ((c, _), (t, _), args)) =
blanchet@39497
   270
    metis_lit true c [Metis_Term.Fn(t, map (Metis_Term.Var o fst) args)]
blanchet@39497
   271
  | m_arity_cls (TVarLit ((c, _), (s, _))) =
blanchet@39497
   272
    metis_lit false c [Metis_Term.Var s]
blanchet@43125
   273
(* TrueI is returned as the Isabelle counterpart because there isn't any. *)
blanchet@43086
   274
fun arity_cls ({prem_lits, concl_lits, ...} : arity_clause) =
blanchet@39497
   275
  (TrueI,
blanchet@42895
   276
   Metis_Thm.axiom (Metis_LiteralSet.fromList
blanchet@42895
   277
                        (map m_arity_cls (concl_lits :: prem_lits))));
blanchet@39497
   278
blanchet@39497
   279
(* CLASSREL CLAUSE *)
blanchet@39497
   280
fun m_class_rel_cls (subclass, _) (superclass, _) =
blanchet@43091
   281
  [metis_lit false subclass [Metis_Term.Var "T"],
blanchet@43091
   282
   metis_lit true superclass [Metis_Term.Var "T"]]
blanchet@43086
   283
fun class_rel_cls ({subclass, superclass, ...} : class_rel_clause) =
blanchet@43091
   284
  (TrueI, m_class_rel_cls subclass superclass
blanchet@43091
   285
          |> Metis_LiteralSet.fromList |> Metis_Thm.axiom)
blanchet@39497
   286
blanchet@39497
   287
fun type_ext thy tms =
blanchet@43091
   288
  let
blanchet@43091
   289
    val subs = tfree_classes_of_terms tms
blanchet@43091
   290
    val supers = tvar_classes_of_terms tms
blanchet@43091
   291
    val tycons = type_consts_of_terms thy tms
blanchet@43091
   292
    val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@43091
   293
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@43091
   294
  in map class_rel_cls class_rel_clauses @ map arity_cls arity_clauses end
blanchet@39497
   295
blanchet@43159
   296
val proxy_defs = map (fst o snd o snd) proxy_table
blanchet@43159
   297
val prepare_helper =
blanchet@43159
   298
  Meson.make_meta_clause #> rewrite_rule (map safe_mk_meta_eq proxy_defs)
blanchet@43159
   299
blanchet@43094
   300
fun metis_name_from_atp s ary =
blanchet@43104
   301
  AList.lookup (op =) metis_name_table (s, ary) |> the_default (s, false)
blanchet@43092
   302
fun metis_term_from_atp (ATerm (s, tms)) =
blanchet@43094
   303
  if is_tptp_variable s then
blanchet@43094
   304
    Metis_Term.Var s
blanchet@43094
   305
  else
blanchet@43104
   306
    let val (s, swap) = metis_name_from_atp s (length tms) in
blanchet@43104
   307
      Metis_Term.Fn (s, tms |> map metis_term_from_atp |> swap ? rev)
blanchet@43104
   308
    end
blanchet@43104
   309
fun metis_atom_from_atp (AAtom tm) =
blanchet@43104
   310
    (case metis_term_from_atp tm of
blanchet@43104
   311
       Metis_Term.Fn x => x
blanchet@43104
   312
     | _ => raise Fail "non CNF -- expected function")
blanchet@43092
   313
  | metis_atom_from_atp _ = raise Fail "not CNF -- expected atom"
blanchet@43092
   314
fun metis_literal_from_atp (AConn (ANot, [phi])) =
blanchet@43092
   315
    (false, metis_atom_from_atp phi)
blanchet@43092
   316
  | metis_literal_from_atp phi = (true, metis_atom_from_atp phi)
blanchet@43092
   317
fun metis_literals_from_atp (AConn (AOr, [phi1, phi2])) =
blanchet@43092
   318
    uncurry (union (op =)) (pairself metis_literals_from_atp (phi1, phi2))
blanchet@43092
   319
  | metis_literals_from_atp phi = [metis_literal_from_atp phi]
blanchet@43092
   320
fun metis_axiom_from_atp clauses (Formula (ident, _, phi, _, _)) =
blanchet@43173
   321
    let
blanchet@43173
   322
      fun some isa =
blanchet@43173
   323
        SOME (phi |> metis_literals_from_atp |> Metis_LiteralSet.fromList
blanchet@43173
   324
                  |> Metis_Thm.axiom, isa)
blanchet@43173
   325
    in
blanchet@43173
   326
      if ident = type_tag_idempotence_helper_name orelse
blanchet@43173
   327
         String.isPrefix lightweight_tags_sym_formula_prefix ident then
blanchet@43173
   328
        Isa_Reflexive_or_Trivial |> some
blanchet@43173
   329
      else if String.isPrefix helper_prefix ident then
blanchet@43173
   330
        case space_explode "_" ident  of
blanchet@43173
   331
          _ :: const :: j :: _ =>
blanchet@43173
   332
          nth (AList.lookup (op =) helper_table const |> the |> snd)
blanchet@43173
   333
              (the (Int.fromString j) - 1)
blanchet@43173
   334
          |> prepare_helper |> Isa_Raw |> some
blanchet@43173
   335
        | _ => raise Fail ("malformed helper identifier " ^ quote ident)
blanchet@43173
   336
      else case try (unprefix conjecture_prefix) ident of
blanchet@43173
   337
        SOME s =>
blanchet@43173
   338
        let val j = the (Int.fromString s) in
blanchet@43173
   339
          if j = length clauses then NONE
blanchet@43173
   340
          else Meson.make_meta_clause (nth clauses j) |> Isa_Raw |> some
blanchet@43173
   341
        end
blanchet@43173
   342
      | NONE => TrueI |> Isa_Raw |> some
blanchet@43173
   343
    end
blanchet@43092
   344
  | metis_axiom_from_atp _ _ = raise Fail "not CNF -- expected formula"
blanchet@43092
   345
blanchet@43128
   346
val default_type_sys = Preds (Polymorphic, Nonmonotonic_Types, Lightweight)
blanchet@43100
   347
blanchet@39497
   348
(* Function to generate metis clauses, including comb and type clauses *)
blanchet@43103
   349
fun prepare_metis_problem ctxt MX type_sys conj_clauses fact_clauses =
blanchet@43091
   350
    let
blanchet@43100
   351
      val type_sys = type_sys |> the_default default_type_sys
blanchet@43092
   352
      val explicit_apply = NONE
blanchet@43109
   353
      val clauses =
blanchet@43109
   354
        conj_clauses @ fact_clauses
blanchet@43109
   355
        |> (if polymorphism_of_type_sys type_sys = Polymorphic then
blanchet@43109
   356
              I
blanchet@43109
   357
            else
blanchet@43109
   358
              map (pair 0)
blanchet@43109
   359
              #> rpair ctxt
blanchet@43109
   360
              #-> Monomorph.monomorph Monomorph.all_schematic_consts_of
blanchet@43132
   361
              #> fst #> maps (map (zero_var_indexes o snd)))
blanchet@43094
   362
      val (atp_problem, _, _, _, _, _, sym_tab) =
blanchet@43098
   363
        prepare_atp_problem ctxt CNF Hypothesis Axiom type_sys explicit_apply
blanchet@43098
   364
                            false false (map prop_of clauses) @{prop False} []
blanchet@43092
   365
      val axioms =
blanchet@43173
   366
        atp_problem |> maps (map_filter (metis_axiom_from_atp clauses) o snd)
blanchet@43094
   367
    in
blanchet@43103
   368
      (MX, sym_tab,
blanchet@43094
   369
       {axioms = axioms, tfrees = [], old_skolems = [] (* FIXME ### *)})
blanchet@43094
   370
    end
blanchet@43100
   371
  | prepare_metis_problem ctxt mode _ conj_clauses fact_clauses =
blanchet@43087
   372
    let
blanchet@43087
   373
      val thy = Proof_Context.theory_of ctxt
blanchet@43087
   374
      (* The modes FO and FT are sticky. HO can be downgraded to FO. *)
blanchet@43087
   375
      val mode =
blanchet@43087
   376
        if mode = HO andalso
blanchet@43091
   377
           forall (forall (is_quasi_fol_clause thy))
blanchet@43092
   378
                  [conj_clauses, fact_clauses] then
blanchet@43087
   379
          FO
blanchet@43087
   380
        else
blanchet@43087
   381
          mode
blanchet@41139
   382
      fun add_thm is_conjecture (isa_ith, metis_ith)
blanchet@40157
   383
                  {axioms, tfrees, old_skolems} : metis_problem =
blanchet@39497
   384
        let
blanchet@39886
   385
          val (mth, tfree_lits, old_skolems) =
blanchet@43090
   386
            hol_thm_to_fol is_conjecture ctxt mode (length axioms) old_skolems
blanchet@43090
   387
                           metis_ith
blanchet@39497
   388
        in
blanchet@43159
   389
          {axioms = (mth, Isa_Raw isa_ith) :: axioms,
blanchet@43091
   390
           tfrees = union (op =) tfree_lits tfrees, old_skolems = old_skolems}
blanchet@39497
   391
        end;
blanchet@43091
   392
      fun add_type_thm (ith, mth) {axioms, tfrees, old_skolems} =
blanchet@43159
   393
        {axioms = (mth, Isa_Raw ith) :: axioms, tfrees = tfrees,
blanchet@43091
   394
         old_skolems = old_skolems}
blanchet@43091
   395
      fun add_tfrees {axioms, tfrees, old_skolems} =
blanchet@43159
   396
        {axioms = map (rpair (Isa_Raw TrueI) o metis_of_tfree)
blanchet@43129
   397
                      (distinct (op =) tfrees) @ axioms,
blanchet@43091
   398
         tfrees = tfrees, old_skolems = old_skolems}
blanchet@43091
   399
      val problem =
blanchet@43091
   400
        {axioms = [], tfrees = init_tfrees ctxt, old_skolems = []}
blanchet@43091
   401
        |> fold (add_thm true o `Meson.make_meta_clause) conj_clauses
blanchet@43091
   402
        |> add_tfrees
blanchet@43092
   403
        |> fold (add_thm false o `Meson.make_meta_clause) fact_clauses
blanchet@43091
   404
      val clause_lists = map (Metis_Thm.clause o #1) (#axioms problem)
blanchet@39497
   405
      fun is_used c =
blanchet@39497
   406
        exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
blanchet@43091
   407
      val problem =
blanchet@39497
   408
        if mode = FO then
blanchet@43091
   409
          problem
blanchet@39497
   410
        else
blanchet@39497
   411
          let
blanchet@39497
   412
            val helper_ths =
blanchet@43085
   413
              helper_table
blanchet@42561
   414
              |> filter (is_used o prefix const_prefix o fst)
blanchet@42894
   415
              |> maps (fn (_, (needs_full_types, thms)) =>
blanchet@41140
   416
                          if needs_full_types andalso mode <> FT then []
blanchet@43159
   417
                          else map (`prepare_helper) thms)
blanchet@43091
   418
          in problem |> fold (add_thm false) helper_ths end
blanchet@43092
   419
      val type_ths = type_ext thy (map prop_of (conj_clauses @ fact_clauses))
blanchet@43094
   420
    in (mode, Symtab.empty, fold add_type_thm type_ths problem) end
blanchet@39497
   421
paulson@15347
   422
end;