src/HOL/Tools/Metis/metis_translate.ML
author blanchet
Thu Aug 25 14:25:07 2011 +0200 (2011-08-25)
changeset 44492 a330c0608da8
parent 44411 e3629929b171
child 44768 a7bc1bdb8bb4
permissions -rw-r--r--
avoid using ":" for anything but systematic type tag annotations, because Hurd's Metis gives it that special semantics
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@44492
    62
    (fn Tags (_, All_Types, Uniform) => metis_systematic_type_tag
blanchet@44492
    63
      | _ => metis_ad_hoc_type_tag, true))]
blanchet@43094
    64
blanchet@39896
    65
fun old_skolem_const_name i j num_T_args =
blanchet@39896
    66
  old_skolem_const_prefix ^ Long_Name.separator ^
wenzelm@41491
    67
  (space_implode Long_Name.separator (map string_of_int [i, j, num_T_args]))
blanchet@37577
    68
blanchet@39886
    69
fun conceal_old_skolem_terms i old_skolems t =
blanchet@39953
    70
  if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
blanchet@37577
    71
    let
blanchet@39886
    72
      fun aux old_skolems
blanchet@39953
    73
             (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
blanchet@37577
    74
          let
blanchet@39886
    75
            val (old_skolems, s) =
blanchet@37577
    76
              if i = ~1 then
blanchet@39886
    77
                (old_skolems, @{const_name undefined})
blanchet@39886
    78
              else case AList.find (op aconv) old_skolems t of
blanchet@39886
    79
                s :: _ => (old_skolems, s)
blanchet@37577
    80
              | [] =>
blanchet@37577
    81
                let
blanchet@39896
    82
                  val s = old_skolem_const_name i (length old_skolems)
blanchet@39896
    83
                                                (length (Term.add_tvarsT T []))
blanchet@39886
    84
                in ((s, t) :: old_skolems, s) end
blanchet@39886
    85
          in (old_skolems, Const (s, T)) end
blanchet@39886
    86
        | aux old_skolems (t1 $ t2) =
blanchet@37577
    87
          let
blanchet@39886
    88
            val (old_skolems, t1) = aux old_skolems t1
blanchet@39886
    89
            val (old_skolems, t2) = aux old_skolems t2
blanchet@39886
    90
          in (old_skolems, t1 $ t2) end
blanchet@39886
    91
        | aux old_skolems (Abs (s, T, t')) =
blanchet@39886
    92
          let val (old_skolems, t') = aux old_skolems t' in
blanchet@39886
    93
            (old_skolems, Abs (s, T, t'))
blanchet@37577
    94
          end
blanchet@39886
    95
        | aux old_skolems t = (old_skolems, t)
blanchet@39886
    96
    in aux old_skolems t end
blanchet@37577
    97
  else
blanchet@39886
    98
    (old_skolems, t)
blanchet@37577
    99
blanchet@39886
   100
fun reveal_old_skolem_terms old_skolems =
blanchet@37632
   101
  map_aterms (fn t as Const (s, _) =>
blanchet@39896
   102
                 if String.isPrefix old_skolem_const_prefix s then
blanchet@39886
   103
                   AList.lookup (op =) old_skolems s |> the
blanchet@43826
   104
                   |> map_types (map_type_tvar (K dummyT))
blanchet@37632
   105
                 else
blanchet@37632
   106
                   t
blanchet@37632
   107
               | t => t)
blanchet@37632
   108
blanchet@37577
   109
blanchet@39497
   110
(* ------------------------------------------------------------------------- *)
blanchet@39497
   111
(* Logic maps manage the interface between HOL and first-order logic.        *)
blanchet@39497
   112
(* ------------------------------------------------------------------------- *)
blanchet@39497
   113
blanchet@43159
   114
datatype isa_thm =
blanchet@43159
   115
  Isa_Reflexive_or_Trivial |
blanchet@43159
   116
  Isa_Raw of thm
blanchet@43159
   117
blanchet@43159
   118
val proxy_defs = map (fst o snd o snd) proxy_table
blanchet@43159
   119
val prepare_helper =
blanchet@43159
   120
  Meson.make_meta_clause #> rewrite_rule (map safe_mk_meta_eq proxy_defs)
blanchet@43159
   121
blanchet@44492
   122
fun metis_term_from_atp type_enc (ATerm (s, tms)) =
blanchet@43094
   123
  if is_tptp_variable s then
blanchet@43268
   124
    Metis_Term.Var (Metis_Name.fromString s)
blanchet@43094
   125
  else
blanchet@44492
   126
    (case AList.lookup (op =) metis_name_table (s, length tms) of
blanchet@44492
   127
       SOME (f, swap) => (f type_enc, swap)
blanchet@44492
   128
     | NONE => (s, false))
blanchet@44492
   129
    |> (fn (s, swap) =>
blanchet@44492
   130
           Metis_Term.Fn (Metis_Name.fromString s,
blanchet@44492
   131
                          tms |> map (metis_term_from_atp type_enc)
blanchet@44492
   132
                              |> swap ? rev))
blanchet@44492
   133
fun metis_atom_from_atp type_enc (AAtom tm) =
blanchet@44492
   134
    (case metis_term_from_atp type_enc tm of
blanchet@43104
   135
       Metis_Term.Fn x => x
blanchet@43104
   136
     | _ => raise Fail "non CNF -- expected function")
blanchet@44492
   137
  | metis_atom_from_atp _ _ = raise Fail "not CNF -- expected atom"
blanchet@44492
   138
fun metis_literal_from_atp type_enc (AConn (ANot, [phi])) =
blanchet@44492
   139
    (false, metis_atom_from_atp type_enc phi)
blanchet@44492
   140
  | metis_literal_from_atp type_enc phi =
blanchet@44492
   141
    (true, metis_atom_from_atp type_enc phi)
blanchet@44492
   142
fun metis_literals_from_atp type_enc (AConn (AOr, phis)) =
blanchet@44492
   143
    maps (metis_literals_from_atp type_enc) phis
blanchet@44492
   144
  | metis_literals_from_atp type_enc phi = [metis_literal_from_atp type_enc phi]
blanchet@44492
   145
fun metis_axiom_from_atp type_enc clauses (Formula (ident, _, phi, _, _)) =
blanchet@43173
   146
    let
blanchet@43173
   147
      fun some isa =
blanchet@44492
   148
        SOME (phi |> metis_literals_from_atp type_enc
blanchet@44492
   149
                  |> Metis_LiteralSet.fromList
blanchet@43173
   150
                  |> Metis_Thm.axiom, isa)
blanchet@43173
   151
    in
blanchet@43173
   152
      if ident = type_tag_idempotence_helper_name orelse
blanchet@44396
   153
         String.isPrefix tags_sym_formula_prefix ident then
blanchet@43173
   154
        Isa_Reflexive_or_Trivial |> some
blanchet@43295
   155
      else if String.isPrefix conjecture_prefix ident then
blanchet@43295
   156
        NONE
blanchet@43173
   157
      else if String.isPrefix helper_prefix ident then
blanchet@43194
   158
        case (String.isSuffix typed_helper_suffix ident,
blanchet@43194
   159
              space_explode "_" ident) of
blanchet@43194
   160
          (needs_fairly_sound, _ :: const :: j :: _) =>
blanchet@43194
   161
          nth ((const, needs_fairly_sound)
blanchet@43194
   162
               |> AList.lookup (op =) helper_table |> the)
blanchet@43173
   163
              (the (Int.fromString j) - 1)
blanchet@43194
   164
          |> prepare_helper
blanchet@43194
   165
          |> Isa_Raw |> some
blanchet@43173
   166
        | _ => raise Fail ("malformed helper identifier " ^ quote ident)
blanchet@43295
   167
      else case try (unprefix fact_prefix) ident of
blanchet@43173
   168
        SOME s =>
blanchet@43295
   169
        let
blanchet@43295
   170
          val j = s |> space_explode "_" |> List.last |> Int.fromString |> the
blanchet@43295
   171
        in Meson.make_meta_clause (snd (nth clauses j)) |> Isa_Raw |> some end
blanchet@43173
   172
      | NONE => TrueI |> Isa_Raw |> some
blanchet@43173
   173
    end
blanchet@44492
   174
  | metis_axiom_from_atp _ _ _ = raise Fail "not CNF -- expected formula"
blanchet@43092
   175
blanchet@39497
   176
(* Function to generate metis clauses, including comb and type clauses *)
blanchet@43626
   177
fun prepare_metis_problem ctxt type_enc conj_clauses fact_clauses =
blanchet@43212
   178
  let
blanchet@43295
   179
    val (conj_clauses, fact_clauses) =
blanchet@43626
   180
      if polymorphism_of_type_enc type_enc = Polymorphic then
blanchet@43295
   181
        (conj_clauses, fact_clauses)
blanchet@43295
   182
      else
blanchet@43295
   183
        conj_clauses @ fact_clauses
blanchet@43295
   184
        |> map (pair 0)
blanchet@43295
   185
        |> rpair ctxt
blanchet@43295
   186
        |-> Monomorph.monomorph atp_schematic_consts_of
blanchet@43295
   187
        |> fst |> chop (length conj_clauses)
blanchet@43295
   188
        |> pairself (maps (map (zero_var_indexes o snd)))
blanchet@43295
   189
    val num_conjs = length conj_clauses
blanchet@43212
   190
    val clauses =
blanchet@43295
   191
      map2 (fn j => pair (Int.toString j, Local))
blanchet@43295
   192
           (0 upto num_conjs - 1) conj_clauses @
blanchet@43295
   193
      (* "General" below isn't quite correct; the fact could be local. *)
blanchet@43295
   194
      map2 (fn j => pair (Int.toString (num_conjs + j), General))
blanchet@43295
   195
           (0 upto length fact_clauses - 1) fact_clauses
blanchet@43212
   196
    val (old_skolems, props) =
blanchet@43295
   197
      fold_rev (fn (name, th) => fn (old_skolems, props) =>
blanchet@43295
   198
                   th |> prop_of |> Logic.strip_imp_concl
blanchet@43295
   199
                      |> conceal_old_skolem_terms (length clauses) old_skolems
blanchet@43295
   200
                      ||> (fn prop => (name, prop) :: props))
blanchet@43295
   201
               clauses ([], [])
blanchet@43295
   202
    (*
blanchet@43295
   203
    val _ =
blanchet@43295
   204
      tracing ("PROPS:\n" ^ cat_lines (map (Syntax.string_of_term ctxt) props))
blanchet@43295
   205
    *)
blanchet@43212
   206
    val (atp_problem, _, _, _, _, _, sym_tab) =
blanchet@44397
   207
      prepare_atp_problem ctxt CNF Hypothesis Axiom type_enc false no_lambdasN
blanchet@44397
   208
                          false false [] @{prop False} props
blanchet@43295
   209
    (*
blanchet@43295
   210
    val _ = tracing ("ATP PROBLEM: " ^
blanchet@43295
   211
                     cat_lines (tptp_lines_for_atp_problem CNF atp_problem))
blanchet@43295
   212
    *)
blanchet@44409
   213
    (* "rev" is for compatibility. *)
blanchet@43212
   214
    val axioms =
blanchet@44492
   215
      atp_problem
blanchet@44492
   216
      |> maps (map_filter (metis_axiom_from_atp type_enc clauses) o snd) |> rev
blanchet@43212
   217
  in (sym_tab, axioms, old_skolems) end
blanchet@39497
   218
paulson@15347
   219
end;