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