src/HOL/Tools/Metis/metis_translate.ML
author blanchet
Thu Jul 14 15:14:38 2011 +0200 (2011-07-14)
changeset 43826 2b094d17f432
parent 43626 a867ebb12209
child 43828 e07a2c4cbad8
permissions -rw-r--r--
fix subtle type inference bug in Metis -- different occurrences of the same Skolem might need to be typed differently, using paramify_vars overconstraints the typing problem
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@43159
    12
  datatype isa_thm =
blanchet@43159
    13
    Isa_Reflexive_or_Trivial |
blanchet@43159
    14
    Isa_Raw of thm
blanchet@43159
    15
blanchet@43094
    16
  val metis_equal : string
blanchet@43094
    17
  val metis_predicator : string
blanchet@43094
    18
  val metis_app_op : string
blanchet@43106
    19
  val metis_type_tag : string
blanchet@42098
    20
  val metis_generated_var_prefix : string
blanchet@43231
    21
  val trace : bool Config.T
blanchet@43231
    22
  val verbose : bool Config.T
blanchet@43231
    23
  val trace_msg : Proof.context -> (unit -> string) -> unit
blanchet@43231
    24
  val verbose_warning : Proof.context -> string -> unit
blanchet@43104
    25
  val metis_name_table : ((string * int) * (string * bool)) list
blanchet@39886
    26
  val reveal_old_skolem_terms : (string * term) list -> term -> term
blanchet@40157
    27
  val prepare_metis_problem :
blanchet@43212
    28
    Proof.context -> string -> thm list -> thm list
blanchet@43212
    29
    -> int Symtab.table * (Metis_Thm.thm * isa_thm) list * (string * term) list
wenzelm@24310
    30
end
paulson@15347
    31
blanchet@39494
    32
structure Metis_Translate : METIS_TRANSLATE =
paulson@15347
    33
struct
paulson@15347
    34
blanchet@43092
    35
open ATP_Problem
blanchet@43085
    36
open ATP_Translate
paulson@15347
    37
blanchet@43094
    38
val metis_equal = "="
blanchet@43094
    39
val metis_predicator = "{}"
blanchet@43094
    40
val metis_app_op = "."
blanchet@43104
    41
val metis_type_tag = ":"
blanchet@43085
    42
val metis_generated_var_prefix = "_"
quigley@17150
    43
blanchet@43231
    44
val trace = Attrib.setup_config_bool @{binding metis_trace} (K false)
blanchet@43231
    45
val verbose = Attrib.setup_config_bool @{binding metis_verbose} (K true)
blanchet@43231
    46
blanchet@43231
    47
fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
blanchet@43231
    48
fun verbose_warning ctxt msg =
blanchet@43231
    49
  if Config.get ctxt verbose then warning ("Metis: " ^ msg) else ()
blanchet@43231
    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@43174
    54
   ((prefixed_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@39896
    58
fun old_skolem_const_name i j num_T_args =
blanchet@39896
    59
  old_skolem_const_prefix ^ Long_Name.separator ^
wenzelm@41491
    60
  (space_implode Long_Name.separator (map string_of_int [i, j, num_T_args]))
blanchet@37577
    61
blanchet@39886
    62
fun conceal_old_skolem_terms i old_skolems t =
blanchet@39953
    63
  if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
blanchet@37577
    64
    let
blanchet@39886
    65
      fun aux old_skolems
blanchet@39953
    66
             (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
blanchet@37577
    67
          let
blanchet@39886
    68
            val (old_skolems, s) =
blanchet@37577
    69
              if i = ~1 then
blanchet@39886
    70
                (old_skolems, @{const_name undefined})
blanchet@39886
    71
              else case AList.find (op aconv) old_skolems t of
blanchet@39886
    72
                s :: _ => (old_skolems, s)
blanchet@37577
    73
              | [] =>
blanchet@37577
    74
                let
blanchet@39896
    75
                  val s = old_skolem_const_name i (length old_skolems)
blanchet@39896
    76
                                                (length (Term.add_tvarsT T []))
blanchet@39886
    77
                in ((s, t) :: old_skolems, s) end
blanchet@39886
    78
          in (old_skolems, Const (s, T)) end
blanchet@39886
    79
        | aux old_skolems (t1 $ t2) =
blanchet@37577
    80
          let
blanchet@39886
    81
            val (old_skolems, t1) = aux old_skolems t1
blanchet@39886
    82
            val (old_skolems, t2) = aux old_skolems t2
blanchet@39886
    83
          in (old_skolems, t1 $ t2) end
blanchet@39886
    84
        | aux old_skolems (Abs (s, T, t')) =
blanchet@39886
    85
          let val (old_skolems, t') = aux old_skolems t' in
blanchet@39886
    86
            (old_skolems, Abs (s, T, t'))
blanchet@37577
    87
          end
blanchet@39886
    88
        | aux old_skolems t = (old_skolems, t)
blanchet@39886
    89
    in aux old_skolems t end
blanchet@37577
    90
  else
blanchet@39886
    91
    (old_skolems, t)
blanchet@37577
    92
blanchet@39886
    93
fun reveal_old_skolem_terms old_skolems =
blanchet@37632
    94
  map_aterms (fn t as Const (s, _) =>
blanchet@39896
    95
                 if String.isPrefix old_skolem_const_prefix s then
blanchet@39886
    96
                   AList.lookup (op =) old_skolems s |> the
blanchet@43826
    97
                   |> map_types (map_type_tvar (K dummyT))
blanchet@37632
    98
                 else
blanchet@37632
    99
                   t
blanchet@37632
   100
               | t => t)
blanchet@37632
   101
blanchet@37577
   102
blanchet@39497
   103
(* ------------------------------------------------------------------------- *)
blanchet@39497
   104
(* Logic maps manage the interface between HOL and first-order logic.        *)
blanchet@39497
   105
(* ------------------------------------------------------------------------- *)
blanchet@39497
   106
blanchet@43159
   107
datatype isa_thm =
blanchet@43159
   108
  Isa_Reflexive_or_Trivial |
blanchet@43159
   109
  Isa_Raw of thm
blanchet@43159
   110
blanchet@43159
   111
val proxy_defs = map (fst o snd o snd) proxy_table
blanchet@43159
   112
val prepare_helper =
blanchet@43159
   113
  Meson.make_meta_clause #> rewrite_rule (map safe_mk_meta_eq proxy_defs)
blanchet@43159
   114
blanchet@43092
   115
fun metis_term_from_atp (ATerm (s, tms)) =
blanchet@43094
   116
  if is_tptp_variable s then
blanchet@43268
   117
    Metis_Term.Var (Metis_Name.fromString s)
blanchet@43094
   118
  else
blanchet@43268
   119
    let
blanchet@43268
   120
      val (s, swap) = AList.lookup (op =) metis_name_table (s, length tms)
blanchet@43268
   121
                      |> the_default (s, false)
blanchet@43268
   122
    in
blanchet@43268
   123
      Metis_Term.Fn (Metis_Name.fromString s,
blanchet@43268
   124
                     tms |> map metis_term_from_atp |> swap ? rev)
blanchet@43104
   125
    end
blanchet@43104
   126
fun metis_atom_from_atp (AAtom tm) =
blanchet@43104
   127
    (case metis_term_from_atp tm of
blanchet@43104
   128
       Metis_Term.Fn x => x
blanchet@43104
   129
     | _ => raise Fail "non CNF -- expected function")
blanchet@43092
   130
  | metis_atom_from_atp _ = raise Fail "not CNF -- expected atom"
blanchet@43092
   131
fun metis_literal_from_atp (AConn (ANot, [phi])) =
blanchet@43092
   132
    (false, metis_atom_from_atp phi)
blanchet@43092
   133
  | metis_literal_from_atp phi = (true, metis_atom_from_atp phi)
blanchet@43268
   134
fun metis_literals_from_atp (AConn (AOr, phis)) =
blanchet@43268
   135
    maps metis_literals_from_atp phis
blanchet@43092
   136
  | metis_literals_from_atp phi = [metis_literal_from_atp phi]
blanchet@43092
   137
fun metis_axiom_from_atp clauses (Formula (ident, _, phi, _, _)) =
blanchet@43173
   138
    let
blanchet@43173
   139
      fun some isa =
blanchet@43173
   140
        SOME (phi |> metis_literals_from_atp |> Metis_LiteralSet.fromList
blanchet@43173
   141
                  |> Metis_Thm.axiom, isa)
blanchet@43173
   142
    in
blanchet@43173
   143
      if ident = type_tag_idempotence_helper_name orelse
blanchet@43173
   144
         String.isPrefix lightweight_tags_sym_formula_prefix ident then
blanchet@43173
   145
        Isa_Reflexive_or_Trivial |> some
blanchet@43295
   146
      else if String.isPrefix conjecture_prefix ident then
blanchet@43295
   147
        NONE
blanchet@43173
   148
      else if String.isPrefix helper_prefix ident then
blanchet@43194
   149
        case (String.isSuffix typed_helper_suffix ident,
blanchet@43194
   150
              space_explode "_" ident) of
blanchet@43194
   151
          (needs_fairly_sound, _ :: const :: j :: _) =>
blanchet@43194
   152
          nth ((const, needs_fairly_sound)
blanchet@43194
   153
               |> AList.lookup (op =) helper_table |> the)
blanchet@43173
   154
              (the (Int.fromString j) - 1)
blanchet@43194
   155
          |> prepare_helper
blanchet@43194
   156
          |> Isa_Raw |> some
blanchet@43173
   157
        | _ => raise Fail ("malformed helper identifier " ^ quote ident)
blanchet@43295
   158
      else case try (unprefix fact_prefix) ident of
blanchet@43173
   159
        SOME s =>
blanchet@43295
   160
        let
blanchet@43295
   161
          val j = s |> space_explode "_" |> List.last |> Int.fromString |> the
blanchet@43295
   162
        in Meson.make_meta_clause (snd (nth clauses j)) |> Isa_Raw |> some end
blanchet@43173
   163
      | NONE => TrueI |> Isa_Raw |> some
blanchet@43173
   164
    end
blanchet@43092
   165
  | metis_axiom_from_atp _ _ = raise Fail "not CNF -- expected formula"
blanchet@43092
   166
blanchet@39497
   167
(* Function to generate metis clauses, including comb and type clauses *)
blanchet@43626
   168
fun prepare_metis_problem ctxt type_enc conj_clauses fact_clauses =
blanchet@43212
   169
  let
blanchet@43626
   170
    val type_enc = type_enc_from_string type_enc
blanchet@43295
   171
    val (conj_clauses, fact_clauses) =
blanchet@43626
   172
      if polymorphism_of_type_enc type_enc = Polymorphic then
blanchet@43295
   173
        (conj_clauses, fact_clauses)
blanchet@43295
   174
      else
blanchet@43295
   175
        conj_clauses @ fact_clauses
blanchet@43295
   176
        |> map (pair 0)
blanchet@43295
   177
        |> rpair ctxt
blanchet@43295
   178
        |-> Monomorph.monomorph atp_schematic_consts_of
blanchet@43295
   179
        |> fst |> chop (length conj_clauses)
blanchet@43295
   180
        |> pairself (maps (map (zero_var_indexes o snd)))
blanchet@43295
   181
    val num_conjs = length conj_clauses
blanchet@43212
   182
    val clauses =
blanchet@43295
   183
      map2 (fn j => pair (Int.toString j, Local))
blanchet@43295
   184
           (0 upto num_conjs - 1) conj_clauses @
blanchet@43295
   185
      (* "General" below isn't quite correct; the fact could be local. *)
blanchet@43295
   186
      map2 (fn j => pair (Int.toString (num_conjs + j), General))
blanchet@43295
   187
           (0 upto length fact_clauses - 1) fact_clauses
blanchet@43212
   188
    val (old_skolems, props) =
blanchet@43295
   189
      fold_rev (fn (name, th) => fn (old_skolems, props) =>
blanchet@43295
   190
                   th |> prop_of |> Logic.strip_imp_concl
blanchet@43295
   191
                      |> conceal_old_skolem_terms (length clauses) old_skolems
blanchet@43295
   192
                      ||> (fn prop => (name, prop) :: props))
blanchet@43295
   193
               clauses ([], [])
blanchet@43295
   194
    (*
blanchet@43295
   195
    val _ =
blanchet@43295
   196
      tracing ("PROPS:\n" ^ cat_lines (map (Syntax.string_of_term ctxt) props))
blanchet@43295
   197
    *)
blanchet@43212
   198
    val (atp_problem, _, _, _, _, _, sym_tab) =
blanchet@43626
   199
      prepare_atp_problem ctxt CNF Hypothesis Axiom type_enc true false false
blanchet@43572
   200
                          false [] @{prop False} props
blanchet@43295
   201
    (*
blanchet@43295
   202
    val _ = tracing ("ATP PROBLEM: " ^
blanchet@43295
   203
                     cat_lines (tptp_lines_for_atp_problem CNF atp_problem))
blanchet@43295
   204
    *)
blanchet@43212
   205
    (* "rev" is for compatibility *)
blanchet@43212
   206
    val axioms =
blanchet@43212
   207
      atp_problem |> maps (map_filter (metis_axiom_from_atp clauses) o snd)
blanchet@43212
   208
                  |> rev
blanchet@43212
   209
  in (sym_tab, axioms, old_skolems) end
blanchet@39497
   210
paulson@15347
   211
end;