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