src/HOL/Tools/Metis/metis_translate.ML
author blanchet
Thu, 22 Sep 2011 16:30:47 +0200
changeset 45907 89341b897412
parent 45650 ba4c0205267f
child 45908 7e1a73fc0c8b
permissions -rw-r--r--
better type reconstruction -- prevents ill-instantiations in proof replay
blanchet@40139
     1
(*  Title:      HOL/Tools/Metis/metis_translate.ML
blanchet@38261
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@39737
     3
    Author:     Kong W. Susanto, Cambridge University Computer Laboratory
blanchet@39737
     4
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@36393
     5
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     6
blanchet@39734
     7
Translation of HOL to FOL for Metis.
paulson@15347
     8
*)
paulson@15347
     9
blanchet@39734
    10
signature METIS_TRANSLATE =
wenzelm@24310
    11
sig
blanchet@45270
    12
  type type_enc = ATP_Translate.type_enc
blanchet@45270
    13
blanchet@44000
    14
  datatype isa_thm =
blanchet@44000
    15
    Isa_Reflexive_or_Trivial |
blanchet@44000
    16
    Isa_Raw of thm
blanchet@44000
    17
blanchet@43935
    18
  val metis_equal : string
blanchet@43935
    19
  val metis_predicator : string
blanchet@43935
    20
  val metis_app_op : string
blanchet@45347
    21
  val metis_systematic_type_tag : string
blanchet@45347
    22
  val metis_ad_hoc_type_tag : string
blanchet@42962
    23
  val metis_generated_var_prefix : string
blanchet@44072
    24
  val trace : bool Config.T
blanchet@44072
    25
  val verbose : bool Config.T
blanchet@44072
    26
  val trace_msg : Proof.context -> (unit -> string) -> unit
blanchet@44072
    27
  val verbose_warning : Proof.context -> string -> unit
blanchet@45347
    28
  val metis_name_table : ((string * int) * ((type_enc -> string) * bool)) list
blanchet@40067
    29
  val reveal_old_skolem_terms : (string * term) list -> term -> term
blanchet@40398
    30
  val prepare_metis_problem :
blanchet@45270
    31
    Proof.context -> type_enc -> thm list -> thm list
blanchet@44053
    32
    -> int Symtab.table * (Metis_Thm.thm * isa_thm) list * (string * term) list
wenzelm@24310
    33
end
paulson@15347
    34
blanchet@39734
    35
structure Metis_Translate : METIS_TRANSLATE =
paulson@15347
    36
struct
paulson@15347
    37
blanchet@43933
    38
open ATP_Problem
blanchet@43926
    39
open ATP_Translate
blanchet@43926
    40
blanchet@43935
    41
val metis_equal = "="
blanchet@43935
    42
val metis_predicator = "{}"
blanchet@45347
    43
val metis_app_op = Metis_Name.toString Metis_Term.appName
blanchet@45347
    44
val metis_systematic_type_tag =
blanchet@45347
    45
  Metis_Name.toString Metis_Term.hasTypeFunctionName
blanchet@45347
    46
val metis_ad_hoc_type_tag = "**"
blanchet@42962
    47
val metis_generated_var_prefix = "_"
blanchet@42962
    48
blanchet@44072
    49
val trace = Attrib.setup_config_bool @{binding metis_trace} (K false)
blanchet@44072
    50
val verbose = Attrib.setup_config_bool @{binding metis_verbose} (K true)
blanchet@44072
    51
blanchet@44072
    52
fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
blanchet@44072
    53
fun verbose_warning ctxt msg =
blanchet@44072
    54
  if Config.get ctxt verbose then warning ("Metis: " ^ msg) else ()
blanchet@44072
    55
blanchet@43935
    56
val metis_name_table =
blanchet@45347
    57
  [((tptp_equal, 2), (K metis_equal, false)),
blanchet@45347
    58
   ((tptp_old_equal, 2), (K metis_equal, false)),
blanchet@45347
    59
   ((prefixed_predicator_name, 1), (K metis_predicator, false)),
blanchet@45347
    60
   ((prefixed_app_op_name, 2), (K metis_app_op, false)),
blanchet@45347
    61
   ((prefixed_type_tag_name, 2),
blanchet@45650
    62
    (fn type_enc =>
blanchet@45650
    63
        if level_of_type_enc type_enc = All_Types then metis_systematic_type_tag
blanchet@45650
    64
        else metis_ad_hoc_type_tag, true))]
blanchet@43935
    65
blanchet@40077
    66
fun old_skolem_const_name i j num_T_args =
blanchet@40077
    67
  old_skolem_const_prefix ^ Long_Name.separator ^
wenzelm@41739
    68
  (space_implode Long_Name.separator (map string_of_int [i, j, num_T_args]))
blanchet@37577
    69
blanchet@40067
    70
fun conceal_old_skolem_terms i old_skolems t =
blanchet@40134
    71
  if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
blanchet@37577
    72
    let
blanchet@40067
    73
      fun aux old_skolems
blanchet@40134
    74
             (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
blanchet@37577
    75
          let
blanchet@40067
    76
            val (old_skolems, s) =
blanchet@37577
    77
              if i = ~1 then
blanchet@40067
    78
                (old_skolems, @{const_name undefined})
blanchet@40067
    79
              else case AList.find (op aconv) old_skolems t of
blanchet@40067
    80
                s :: _ => (old_skolems, s)
blanchet@37577
    81
              | [] =>
blanchet@37577
    82
                let
blanchet@40077
    83
                  val s = old_skolem_const_name i (length old_skolems)
blanchet@40077
    84
                                                (length (Term.add_tvarsT T []))
blanchet@40067
    85
                in ((s, t) :: old_skolems, s) end
blanchet@40067
    86
          in (old_skolems, Const (s, T)) end
blanchet@40067
    87
        | aux old_skolems (t1 $ t2) =
blanchet@37577
    88
          let
blanchet@40067
    89
            val (old_skolems, t1) = aux old_skolems t1
blanchet@40067
    90
            val (old_skolems, t2) = aux old_skolems t2
blanchet@40067
    91
          in (old_skolems, t1 $ t2) end
blanchet@40067
    92
        | aux old_skolems (Abs (s, T, t')) =
blanchet@40067
    93
          let val (old_skolems, t') = aux old_skolems t' in
blanchet@40067
    94
            (old_skolems, Abs (s, T, t'))
blanchet@37577
    95
          end
blanchet@40067
    96
        | aux old_skolems t = (old_skolems, t)
blanchet@40067
    97
    in aux old_skolems t end
blanchet@37577
    98
  else
blanchet@40067
    99
    (old_skolems, t)
blanchet@37577
   100
blanchet@40067
   101
fun reveal_old_skolem_terms old_skolems =
blanchet@37632
   102
  map_aterms (fn t as Const (s, _) =>
blanchet@40077
   103
                 if String.isPrefix old_skolem_const_prefix s then
blanchet@40067
   104
                   AList.lookup (op =) old_skolems s |> the
blanchet@44690
   105
                   |> map_types (map_type_tvar (K dummyT))
blanchet@37632
   106
                 else
blanchet@37632
   107
                   t
blanchet@37632
   108
               | t => t)
blanchet@37632
   109
blanchet@37577
   110
blanchet@39737
   111
(* ------------------------------------------------------------------------- *)
blanchet@39737
   112
(* Logic maps manage the interface between HOL and first-order logic.        *)
blanchet@39737
   113
(* ------------------------------------------------------------------------- *)
blanchet@39737
   114
blanchet@44000
   115
datatype isa_thm =
blanchet@44000
   116
  Isa_Reflexive_or_Trivial |
blanchet@44000
   117
  Isa_Raw of thm
blanchet@44000
   118
blanchet@44000
   119
val proxy_defs = map (fst o snd o snd) proxy_table
blanchet@44000
   120
val prepare_helper =
blanchet@44000
   121
  Meson.make_meta_clause #> rewrite_rule (map safe_mk_meta_eq proxy_defs)
blanchet@44000
   122
blanchet@45347
   123
fun metis_term_from_atp type_enc (ATerm (s, tms)) =
blanchet@43935
   124
  if is_tptp_variable s then
blanchet@44109
   125
    Metis_Term.Var (Metis_Name.fromString s)
blanchet@43935
   126
  else
blanchet@45347
   127
    (case AList.lookup (op =) metis_name_table (s, length tms) of
blanchet@45347
   128
       SOME (f, swap) => (f type_enc, swap)
blanchet@45347
   129
     | NONE => (s, false))
blanchet@45347
   130
    |> (fn (s, swap) =>
blanchet@45347
   131
           Metis_Term.Fn (Metis_Name.fromString s,
blanchet@45347
   132
                          tms |> map (metis_term_from_atp type_enc)
blanchet@45347
   133
                              |> swap ? rev))
blanchet@45347
   134
fun metis_atom_from_atp type_enc (AAtom tm) =
blanchet@45347
   135
    (case metis_term_from_atp type_enc tm of
blanchet@43945
   136
       Metis_Term.Fn x => x
blanchet@43945
   137
     | _ => raise Fail "non CNF -- expected function")
blanchet@45347
   138
  | metis_atom_from_atp _ _ = raise Fail "not CNF -- expected atom"
blanchet@45347
   139
fun metis_literal_from_atp type_enc (AConn (ANot, [phi])) =
blanchet@45347
   140
    (false, metis_atom_from_atp type_enc phi)
blanchet@45347
   141
  | metis_literal_from_atp type_enc phi =
blanchet@45347
   142
    (true, metis_atom_from_atp type_enc phi)
blanchet@45347
   143
fun metis_literals_from_atp type_enc (AConn (AOr, phis)) =
blanchet@45347
   144
    maps (metis_literals_from_atp type_enc) phis
blanchet@45347
   145
  | metis_literals_from_atp type_enc phi = [metis_literal_from_atp type_enc phi]
blanchet@45347
   146
fun metis_axiom_from_atp type_enc clauses (Formula (ident, _, phi, _, _)) =
blanchet@44014
   147
    let
blanchet@44014
   148
      fun some isa =
blanchet@45347
   149
        SOME (phi |> metis_literals_from_atp type_enc
blanchet@45347
   150
                  |> Metis_LiteralSet.fromList
blanchet@44014
   151
                  |> Metis_Thm.axiom, isa)
blanchet@44014
   152
    in
blanchet@44014
   153
      if ident = type_tag_idempotence_helper_name orelse
blanchet@45255
   154
         String.isPrefix tags_sym_formula_prefix ident then
blanchet@44014
   155
        Isa_Reflexive_or_Trivial |> some
blanchet@44153
   156
      else if String.isPrefix conjecture_prefix ident then
blanchet@44153
   157
        NONE
blanchet@44014
   158
      else if String.isPrefix helper_prefix ident then
blanchet@44035
   159
        case (String.isSuffix typed_helper_suffix ident,
blanchet@44035
   160
              space_explode "_" ident) of
blanchet@44035
   161
          (needs_fairly_sound, _ :: const :: j :: _) =>
blanchet@44035
   162
          nth ((const, needs_fairly_sound)
blanchet@44035
   163
               |> AList.lookup (op =) helper_table |> the)
blanchet@44014
   164
              (the (Int.fromString j) - 1)
blanchet@44035
   165
          |> prepare_helper
blanchet@44035
   166
          |> Isa_Raw |> some
blanchet@44014
   167
        | _ => raise Fail ("malformed helper identifier " ^ quote ident)
blanchet@44153
   168
      else case try (unprefix fact_prefix) ident of
blanchet@44014
   169
        SOME s =>
blanchet@44153
   170
        let
blanchet@44153
   171
          val j = s |> space_explode "_" |> List.last |> Int.fromString |> the
blanchet@44153
   172
        in Meson.make_meta_clause (snd (nth clauses j)) |> Isa_Raw |> some end
blanchet@44014
   173
      | NONE => TrueI |> Isa_Raw |> some
blanchet@44014
   174
    end
blanchet@45347
   175
  | metis_axiom_from_atp _ _ _ = raise Fail "not CNF -- expected formula"
blanchet@43933
   176
blanchet@39737
   177
(* Function to generate metis clauses, including comb and type clauses *)
blanchet@44493
   178
fun prepare_metis_problem ctxt type_enc conj_clauses fact_clauses =
blanchet@44053
   179
  let
blanchet@44153
   180
    val (conj_clauses, fact_clauses) =
blanchet@44493
   181
      if polymorphism_of_type_enc type_enc = Polymorphic then
blanchet@44153
   182
        (conj_clauses, fact_clauses)
blanchet@44153
   183
      else
blanchet@44153
   184
        conj_clauses @ fact_clauses
blanchet@44153
   185
        |> map (pair 0)
blanchet@44153
   186
        |> rpair ctxt
blanchet@44153
   187
        |-> Monomorph.monomorph atp_schematic_consts_of
blanchet@44153
   188
        |> fst |> chop (length conj_clauses)
blanchet@44153
   189
        |> pairself (maps (map (zero_var_indexes o snd)))
blanchet@44153
   190
    val num_conjs = length conj_clauses
blanchet@44053
   191
    val clauses =
blanchet@44153
   192
      map2 (fn j => pair (Int.toString j, Local))
blanchet@44153
   193
           (0 upto num_conjs - 1) conj_clauses @
blanchet@44153
   194
      (* "General" below isn't quite correct; the fact could be local. *)
blanchet@44153
   195
      map2 (fn j => pair (Int.toString (num_conjs + j), General))
blanchet@44153
   196
           (0 upto length fact_clauses - 1) fact_clauses
blanchet@44053
   197
    val (old_skolems, props) =
blanchet@44153
   198
      fold_rev (fn (name, th) => fn (old_skolems, props) =>
blanchet@44153
   199
                   th |> prop_of |> Logic.strip_imp_concl
blanchet@44153
   200
                      |> conceal_old_skolem_terms (length clauses) old_skolems
blanchet@44153
   201
                      ||> (fn prop => (name, prop) :: props))
blanchet@44153
   202
               clauses ([], [])
blanchet@44153
   203
    (*
blanchet@44153
   204
    val _ =
blanchet@45907
   205
      tracing ("PROPS:\n" ^
blanchet@45907
   206
               cat_lines (map (Syntax.string_of_term ctxt o snd) props))
blanchet@44153
   207
    *)
blanchet@44053
   208
    val (atp_problem, _, _, _, _, _, sym_tab) =
blanchet@45256
   209
      prepare_atp_problem ctxt CNF Hypothesis Axiom type_enc false no_lambdasN
blanchet@45256
   210
                          false false [] @{prop False} props
blanchet@44153
   211
    (*
blanchet@44153
   212
    val _ = tracing ("ATP PROBLEM: " ^
blanchet@44153
   213
                     cat_lines (tptp_lines_for_atp_problem CNF atp_problem))
blanchet@44153
   214
    *)
blanchet@45268
   215
    (* "rev" is for compatibility. *)
blanchet@44053
   216
    val axioms =
blanchet@45347
   217
      atp_problem
blanchet@45347
   218
      |> maps (map_filter (metis_axiom_from_atp type_enc clauses) o snd) |> rev
blanchet@44053
   219
  in (sym_tab, axioms, old_skolems) end
blanchet@39737
   220
paulson@15347
   221
end;