src/HOL/Tools/Metis/metis_generate.ML
author blanchet
Tue, 26 Jun 2012 11:14:39 +0200
changeset 49145 defbcdc60fd6
parent 48961 33afcfad3f8d
child 49146 1016664b8feb
permissions -rw-r--r--
tuning
     1 (*  Title:      HOL/Tools/Metis/metis_generate.ML
     2     Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
     3     Author:     Kong W. Susanto, Cambridge University Computer Laboratory
     4     Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
     5     Author:     Jasmin Blanchette, TU Muenchen
     6 
     7 Translation of HOL to FOL for Metis.
     8 *)
     9 
    10 signature METIS_GENERATE =
    11 sig
    12   type type_enc = ATP_Problem_Generate.type_enc
    13 
    14   datatype isa_thm =
    15     Isa_Reflexive_or_Trivial |
    16     Isa_Lambda_Lifted |
    17     Isa_Raw of thm
    18 
    19   val metis_equal : string
    20   val metis_predicator : string
    21   val metis_app_op : string
    22   val metis_systematic_type_tag : string
    23   val metis_ad_hoc_type_tag : string
    24   val metis_generated_var_prefix : string
    25   val trace : bool Config.T
    26   val verbose : bool Config.T
    27   val trace_msg : Proof.context -> (unit -> string) -> unit
    28   val verbose_warning : Proof.context -> string -> unit
    29   val metis_name_table : ((string * int) * ((type_enc -> string) * bool)) list
    30   val reveal_old_skolem_terms : (string * term) list -> term -> term
    31   val reveal_lam_lifted : (string * term) list -> term -> term
    32   val prepare_metis_problem :
    33     Proof.context -> type_enc -> string -> thm list -> thm list
    34     -> int Symtab.table * (Metis_Thm.thm * isa_thm) list
    35        * (unit -> (string * int) list)
    36        * ((string * term) list * (string * term) list)
    37 end
    38 
    39 structure Metis_Generate : METIS_GENERATE =
    40 struct
    41 
    42 open ATP_Problem
    43 open ATP_Problem_Generate
    44 
    45 val metis_equal = "="
    46 val metis_predicator = "{}"
    47 val metis_app_op = Metis_Name.toString Metis_Term.appName
    48 val metis_systematic_type_tag =
    49   Metis_Name.toString Metis_Term.hasTypeFunctionName
    50 val metis_ad_hoc_type_tag = "**"
    51 val metis_generated_var_prefix = "_"
    52 
    53 val trace = Attrib.setup_config_bool @{binding metis_trace} (K false)
    54 val verbose = Attrib.setup_config_bool @{binding metis_verbose} (K true)
    55 
    56 fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
    57 fun verbose_warning ctxt msg =
    58   if Config.get ctxt verbose then warning ("Metis: " ^ msg) else ()
    59 
    60 val metis_name_table =
    61   [((tptp_equal, 2), (K metis_equal, false)),
    62    ((tptp_old_equal, 2), (K metis_equal, false)),
    63    ((prefixed_predicator_name, 1), (K metis_predicator, false)),
    64    ((prefixed_app_op_name, 2), (K metis_app_op, false)),
    65    ((prefixed_type_tag_name, 2),
    66     (fn type_enc =>
    67         if level_of_type_enc type_enc = All_Types then metis_systematic_type_tag
    68         else metis_ad_hoc_type_tag, true))]
    69 
    70 fun old_skolem_const_name i j num_T_args =
    71   old_skolem_const_prefix ^ Long_Name.separator ^
    72   (space_implode Long_Name.separator (map string_of_int [i, j, num_T_args]))
    73 
    74 fun conceal_old_skolem_terms i old_skolems t =
    75   if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
    76     let
    77       fun aux old_skolems
    78              (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
    79           let
    80             val (old_skolems, s) =
    81               if i = ~1 then
    82                 (old_skolems, @{const_name undefined})
    83               else case AList.find (op aconv) old_skolems t of
    84                 s :: _ => (old_skolems, s)
    85               | [] =>
    86                 let
    87                   val s = old_skolem_const_name i (length old_skolems)
    88                                                 (length (Term.add_tvarsT T []))
    89                 in ((s, t) :: old_skolems, s) end
    90           in (old_skolems, Const (s, T)) end
    91         | aux old_skolems (t1 $ t2) =
    92           let
    93             val (old_skolems, t1) = aux old_skolems t1
    94             val (old_skolems, t2) = aux old_skolems t2
    95           in (old_skolems, t1 $ t2) end
    96         | aux old_skolems (Abs (s, T, t')) =
    97           let val (old_skolems, t') = aux old_skolems t' in
    98             (old_skolems, Abs (s, T, t'))
    99           end
   100         | aux old_skolems t = (old_skolems, t)
   101     in aux old_skolems t end
   102   else
   103     (old_skolems, t)
   104 
   105 fun reveal_old_skolem_terms old_skolems =
   106   map_aterms (fn t as Const (s, _) =>
   107                  if String.isPrefix old_skolem_const_prefix s then
   108                    AList.lookup (op =) old_skolems s |> the
   109                    |> map_types (map_type_tvar (K dummyT))
   110                  else
   111                    t
   112                | t => t)
   113 
   114 fun reveal_lam_lifted lambdas =
   115   map_aterms (fn t as Const (s, _) =>
   116                  if String.isPrefix lam_lifted_prefix s then
   117                    case AList.lookup (op =) lambdas s of
   118                      SOME t =>
   119                      Const (@{const_name Metis.lambda}, dummyT)
   120                      $ map_types (map_type_tvar (K dummyT))
   121                                  (reveal_lam_lifted lambdas t)
   122                    | NONE => t
   123                  else
   124                    t
   125                | t => t)
   126 
   127 
   128 (* ------------------------------------------------------------------------- *)
   129 (* Logic maps manage the interface between HOL and first-order logic.        *)
   130 (* ------------------------------------------------------------------------- *)
   131 
   132 datatype isa_thm =
   133   Isa_Reflexive_or_Trivial |
   134   Isa_Lambda_Lifted |
   135   Isa_Raw of thm
   136 
   137 val proxy_defs = map (fst o snd o snd) proxy_table
   138 val prepare_helper =
   139   Meson.make_meta_clause #> rewrite_rule (map safe_mk_meta_eq proxy_defs)
   140 
   141 fun metis_term_from_atp type_enc (ATerm (s, tms)) =
   142   if is_tptp_variable s then
   143     Metis_Term.Var (Metis_Name.fromString s)
   144   else
   145     (case AList.lookup (op =) metis_name_table (s, length tms) of
   146        SOME (f, swap) => (f type_enc, swap)
   147      | NONE => (s, false))
   148     |> (fn (s, swap) =>
   149            Metis_Term.Fn (Metis_Name.fromString s,
   150                           tms |> map (metis_term_from_atp type_enc)
   151                               |> swap ? rev))
   152 fun metis_atom_from_atp type_enc (AAtom tm) =
   153     (case metis_term_from_atp type_enc tm of
   154        Metis_Term.Fn x => x
   155      | _ => raise Fail "non CNF -- expected function")
   156   | metis_atom_from_atp _ _ = raise Fail "not CNF -- expected atom"
   157 fun metis_literal_from_atp type_enc (AConn (ANot, [phi])) =
   158     (false, metis_atom_from_atp type_enc phi)
   159   | metis_literal_from_atp type_enc phi =
   160     (true, metis_atom_from_atp type_enc phi)
   161 fun metis_literals_from_atp type_enc (AConn (AOr, phis)) =
   162     maps (metis_literals_from_atp type_enc) phis
   163   | metis_literals_from_atp type_enc phi = [metis_literal_from_atp type_enc phi]
   164 fun metis_axiom_from_atp type_enc clauses (Formula (ident, _, phi, _, _)) =
   165     let
   166       fun some isa =
   167         SOME (phi |> metis_literals_from_atp type_enc
   168                   |> Metis_LiteralSet.fromList
   169                   |> Metis_Thm.axiom, isa)
   170     in
   171       if String.isPrefix tags_sym_formula_prefix ident then
   172         Isa_Reflexive_or_Trivial |> some
   173       else if String.isPrefix conjecture_prefix ident then
   174         NONE
   175       else if String.isPrefix helper_prefix ident then
   176         case (String.isSuffix typed_helper_suffix ident,
   177               space_explode "_" ident) of
   178           (needs_fairly_sound, _ :: const :: j :: _) =>
   179           nth (AList.lookup (op =) helper_table (const, needs_fairly_sound)
   180                |> the)
   181               (the (Int.fromString j) - 1)
   182           |> snd |> prepare_helper
   183           |> Isa_Raw |> some
   184         | _ => raise Fail ("malformed helper identifier " ^ quote ident)
   185       else case try (unprefix fact_prefix) ident of
   186         SOME s =>
   187         let val s = s |> space_explode "_" |> tl |> space_implode "_"
   188           in
   189           case Int.fromString s of
   190             SOME j =>
   191             Meson.make_meta_clause (snd (nth clauses j)) |> Isa_Raw |> some
   192           | NONE =>
   193             if String.isPrefix lam_fact_prefix (unascii_of s) then
   194               Isa_Lambda_Lifted |> some
   195             else
   196               raise Fail ("malformed fact identifier " ^ quote ident)
   197         end
   198       | NONE => TrueI |> Isa_Raw |> some
   199     end
   200   | metis_axiom_from_atp _ _ _ = raise Fail "not CNF -- expected formula"
   201 
   202 fun eliminate_lam_wrappers (Const (@{const_name Metis.lambda}, _) $ t) =
   203     eliminate_lam_wrappers t
   204   | eliminate_lam_wrappers (t $ u) =
   205     eliminate_lam_wrappers t $ eliminate_lam_wrappers u
   206   | eliminate_lam_wrappers (Abs (s, T, t)) =
   207     Abs (s, T, eliminate_lam_wrappers t)
   208   | eliminate_lam_wrappers t = t
   209 
   210 (* Function to generate metis clauses, including comb and type clauses *)
   211 fun prepare_metis_problem ctxt type_enc lam_trans conj_clauses fact_clauses =
   212   let
   213     val (conj_clauses, fact_clauses) =
   214       if polymorphism_of_type_enc type_enc = Raw_Polymorphic then
   215         (conj_clauses, fact_clauses)
   216       else
   217         conj_clauses @ fact_clauses
   218         |> map (pair 0)
   219         |> rpair (ctxt |> Config.put Monomorph.keep_partial_instances false)
   220         |-> Monomorph.monomorph atp_schematic_consts_of
   221         |> fst |> chop (length conj_clauses)
   222         |> pairself (maps (map (zero_var_indexes o snd)))
   223     val num_conjs = length conj_clauses
   224     (* Pretend every clause is a "simp" rule, to guide the term ordering. *)
   225     val clauses =
   226       map2 (fn j => pair (Int.toString j, (Local, Simp)))
   227            (0 upto num_conjs - 1) conj_clauses @
   228       map2 (fn j => pair (Int.toString (num_conjs + j), (Local, Simp)))
   229            (0 upto length fact_clauses - 1) fact_clauses
   230     val (old_skolems, props) =
   231       fold_rev (fn (name, th) => fn (old_skolems, props) =>
   232                    th |> prop_of |> Logic.strip_imp_concl
   233                       |> conceal_old_skolem_terms (length clauses) old_skolems
   234                       ||> (lam_trans = liftingN orelse lam_trans = lam_liftingN)
   235                           ? eliminate_lam_wrappers
   236                       ||> (fn prop => (name, prop) :: props))
   237                clauses ([], [])
   238     (*
   239     val _ =
   240       tracing ("PROPS:\n" ^
   241                cat_lines (map (Syntax.string_of_term ctxt o snd) props))
   242     *)
   243     val lam_trans = if lam_trans = combsN then no_lamsN else lam_trans
   244     val (atp_problem, _, _, lifted, sym_tab) =
   245       prepare_atp_problem ctxt CNF Hypothesis type_enc Metis lam_trans false
   246                           false false [] @{prop False} props
   247     (*
   248     val _ = tracing ("ATP PROBLEM: " ^
   249                      cat_lines (lines_for_atp_problem CNF atp_problem))
   250     *)
   251     (* "rev" is for compatibility with existing proof scripts. *)
   252     val axioms =
   253       atp_problem
   254       |> maps (map_filter (metis_axiom_from_atp type_enc clauses) o snd) |> rev
   255     fun ord_info () = atp_problem_term_order_info atp_problem
   256   in (sym_tab, axioms, ord_info, (lifted, old_skolems)) end
   257 
   258 end;