src/HOL/Tools/Metis/metis_tactics.ML
author blanchet
Tue Nov 23 18:26:56 2010 +0100 (2010-11-23)
changeset 40665 1a65f0c74827
parent 40262 8403085384eb
child 42336 d63d43e85879
permissions -rw-r--r--
added "verbose" option to Metis to shut up its warnings if necessary
blanchet@39958
     1
(*  Title:      HOL/Tools/Metis/metis_tactics.ML
blanchet@38027
     2
    Author:     Kong W. Susanto, Cambridge University Computer Laboratory
blanchet@38027
     3
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@38027
     4
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@23442
     5
    Copyright   Cambridge University 2007
wenzelm@23447
     6
wenzelm@29266
     7
HOL setup for the Metis prover.
wenzelm@23442
     8
*)
wenzelm@23442
     9
blanchet@35826
    10
signature METIS_TACTICS =
wenzelm@23442
    11
sig
blanchet@39979
    12
  val trace : bool Config.T
blanchet@40665
    13
  val verbose : bool Config.T
blanchet@39497
    14
  val type_lits : bool Config.T
blanchet@39891
    15
  val new_skolemizer : bool Config.T
blanchet@39497
    16
  val metis_tac : Proof.context -> thm list -> int -> tactic
blanchet@39497
    17
  val metisF_tac : Proof.context -> thm list -> int -> tactic
blanchet@39497
    18
  val metisFT_tac : Proof.context -> thm list -> int -> tactic
blanchet@39497
    19
  val setup : theory -> theory
wenzelm@23442
    20
end
wenzelm@23442
    21
blanchet@35826
    22
structure Metis_Tactics : METIS_TACTICS =
wenzelm@23442
    23
struct
wenzelm@23442
    24
blanchet@39494
    25
open Metis_Translate
blanchet@39497
    26
open Metis_Reconstruct
blanchet@35826
    27
blanchet@39891
    28
val (type_lits, type_lits_setup) = Attrib.config_bool "metis_type_lits" (K true)
blanchet@39891
    29
val (new_skolemizer, new_skolemizer_setup) =
blanchet@39891
    30
  Attrib.config_bool "metis_new_skolemizer" (K false)
wenzelm@23442
    31
blanchet@39497
    32
fun is_false t = t aconv (HOLogic.mk_Trueprop HOLogic.false_const);
wenzelm@23442
    33
blanchet@39497
    34
fun have_common_thm ths1 ths2 =
blanchet@39497
    35
  exists (member Thm.eq_thm ths1) (map Meson.make_meta_clause ths2)
wenzelm@23442
    36
wenzelm@32956
    37
(*Determining which axiom clauses are actually used*)
blanchet@39419
    38
fun used_axioms axioms (th, Metis_Proof.Axiom _) = SOME (lookth axioms th)
wenzelm@32994
    39
  | used_axioms _ _ = NONE;
paulson@24855
    40
blanchet@39450
    41
val clause_params =
blanchet@39450
    42
  {ordering = Metis_KnuthBendixOrder.default,
blanchet@39450
    43
   orderLiterals = Metis_Clause.UnsignedLiteralOrder,
blanchet@39450
    44
   orderTerms = true}
blanchet@39450
    45
val active_params =
blanchet@39450
    46
  {clause = clause_params,
blanchet@39450
    47
   prefactor = #prefactor Metis_Active.default,
blanchet@39450
    48
   postfactor = #postfactor Metis_Active.default}
blanchet@39450
    49
val waiting_params =
blanchet@39450
    50
  {symbolsWeight = 1.0,
blanchet@39450
    51
   variablesWeight = 0.0,
blanchet@39450
    52
   literalsWeight = 0.0,
blanchet@39450
    53
   models = []}
blanchet@39497
    54
val resolution_params = {active = active_params, waiting = waiting_params}
blanchet@37573
    55
blanchet@37516
    56
(* Main function to start Metis proof and reconstruction *)
wenzelm@32956
    57
fun FOL_SOLVE mode ctxt cls ths0 =
wenzelm@32956
    58
  let val thy = ProofContext.theory_of ctxt
blanchet@39497
    59
      val type_lits = Config.get ctxt type_lits
blanchet@39901
    60
      val new_skolemizer =
blanchet@39950
    61
        Config.get ctxt new_skolemizer orelse null (Meson.choice_theorems thy)
blanchet@35826
    62
      val th_cls_pairs =
blanchet@39894
    63
        map2 (fn j => fn th =>
blanchet@39894
    64
                (Thm.get_name_hint th,
blanchet@39901
    65
                 Meson_Clausify.cnf_axiom ctxt new_skolemizer j th))
blanchet@39894
    66
             (0 upto length ths0 - 1) ths0
blanchet@39887
    67
      val thss = map (snd o snd) th_cls_pairs
blanchet@39938
    68
      val dischargers = map (fst o snd) th_cls_pairs
blanchet@39978
    69
      val _ = trace_msg ctxt (fn () => "FOL_SOLVE: CONJECTURE CLAUSES")
blanchet@39978
    70
      val _ = app (fn th => trace_msg ctxt (fn () => Display.string_of_thm ctxt th)) cls
blanchet@39978
    71
      val _ = trace_msg ctxt (fn () => "THEOREM CLAUSES")
blanchet@39978
    72
      val _ = app (app (fn th => trace_msg ctxt (fn () => Display.string_of_thm ctxt th))) thss
blanchet@39886
    73
      val (mode, {axioms, tfrees, old_skolems}) =
blanchet@40157
    74
        prepare_metis_problem mode ctxt type_lits cls thss
wenzelm@32956
    75
      val _ = if null tfrees then ()
blanchet@39978
    76
              else (trace_msg ctxt (fn () => "TFREE CLAUSES");
blanchet@37643
    77
                    app (fn TyLitFree ((s, _), (s', _)) =>
blanchet@39978
    78
                            trace_msg ctxt (fn () => s ^ "(" ^ s' ^ ")")) tfrees)
blanchet@39978
    79
      val _ = trace_msg ctxt (fn () => "CLAUSES GIVEN TO METIS")
wenzelm@32956
    80
      val thms = map #1 axioms
blanchet@39978
    81
      val _ = app (fn th => trace_msg ctxt (fn () => Metis_Thm.toString th)) thms
blanchet@39978
    82
      val _ = trace_msg ctxt (fn () => "mode = " ^ string_of_mode mode)
blanchet@39978
    83
      val _ = trace_msg ctxt (fn () => "START METIS PROVE PROCESS")
wenzelm@32956
    84
  in
wenzelm@33317
    85
      case filter (is_false o prop_of) cls of
wenzelm@32956
    86
          false_th::_ => [false_th RS @{thm FalseE}]
wenzelm@32956
    87
        | [] =>
blanchet@39497
    88
      case Metis_Resolution.new resolution_params {axioms = thms, conjecture = []}
blanchet@39497
    89
           |> Metis_Resolution.loop of
blanchet@39419
    90
          Metis_Resolution.Contradiction mth =>
blanchet@39978
    91
            let val _ = trace_msg ctxt (fn () => "METIS RECONSTRUCTION START: " ^
blanchet@39419
    92
                          Metis_Thm.toString mth)
wenzelm@32956
    93
                val ctxt' = fold Variable.declare_constraints (map prop_of cls) ctxt
wenzelm@32956
    94
                             (*add constraints arising from converting goal to clause form*)
blanchet@40259
    95
                val new_skolem_var_count = Unsynchronized.ref 1
blanchet@39419
    96
                val proof = Metis_Proof.proof mth
blanchet@39886
    97
                val result =
blanchet@40259
    98
                  fold (replay_one_inference ctxt' mode
blanchet@40259
    99
                              (old_skolems, new_skolem_var_count)) proof axioms
wenzelm@32956
   100
                and used = map_filter (used_axioms axioms) proof
blanchet@39978
   101
                val _ = trace_msg ctxt (fn () => "METIS COMPLETED...clauses actually used:")
blanchet@39978
   102
                val _ = app (fn th => trace_msg ctxt (fn () => Display.string_of_thm ctxt th)) used
blanchet@39887
   103
                val unused = th_cls_pairs |> map_filter (fn (name, (_, cls)) =>
blanchet@39497
   104
                  if have_common_thm used cls then NONE else SOME name)
wenzelm@32956
   105
            in
blanchet@39497
   106
                if not (null cls) andalso not (have_common_thm used cls) then
blanchet@40665
   107
                  verbose_warning ctxt "Metis: The assumptions are inconsistent"
blanchet@36383
   108
                else
blanchet@36383
   109
                  ();
blanchet@36383
   110
                if not (null unused) then
blanchet@40665
   111
                  verbose_warning ctxt ("Metis: Unused theorems: " ^
blanchet@40665
   112
                                        commas_quote unused)
blanchet@36230
   113
                else
blanchet@36230
   114
                  ();
wenzelm@32956
   115
                case result of
wenzelm@32956
   116
                    (_,ith)::_ =>
blanchet@39978
   117
                        (trace_msg ctxt (fn () => "Success: " ^ Display.string_of_thm ctxt ith);
blanchet@39887
   118
                         [discharge_skolem_premises ctxt dischargers ith])
blanchet@39978
   119
                  | _ => (trace_msg ctxt (fn () => "Metis: No result"); [])
wenzelm@32956
   120
            end
blanchet@39419
   121
        | Metis_Resolution.Satisfiable _ =>
blanchet@39978
   122
            (trace_msg ctxt (fn () => "Metis: No first-order proof with the lemmas supplied");
blanchet@38097
   123
             [])
wenzelm@32956
   124
  end;
wenzelm@23442
   125
blanchet@38632
   126
(* Extensionalize "th", because that makes sense and that's what Sledgehammer
blanchet@38632
   127
   does, but also keep an unextensionalized version of "th" for backward
blanchet@38632
   128
   compatibility. *)
blanchet@38632
   129
fun also_extensionalize_theorem th =
blanchet@39890
   130
  let val th' = Meson_Clausify.extensionalize_theorem th in
blanchet@38632
   131
    if Thm.eq_thm (th, th') then [th]
blanchet@38632
   132
    else th :: Meson.make_clauses_unsorted [th']
blanchet@38632
   133
  end
blanchet@38632
   134
blanchet@38028
   135
val neg_clausify =
blanchet@38028
   136
  single
blanchet@38028
   137
  #> Meson.make_clauses_unsorted
blanchet@38632
   138
  #> maps also_extensionalize_theorem
blanchet@39890
   139
  #> map Meson_Clausify.introduce_combinators_in_theorem
blanchet@38028
   140
  #> Meson.finish_cnf
blanchet@38028
   141
blanchet@39269
   142
fun preskolem_tac ctxt st0 =
blanchet@39269
   143
  (if exists (Meson.has_too_many_clauses ctxt)
blanchet@39269
   144
             (Logic.prems_of_goal (prop_of st0) 1) then
blanchet@39269
   145
     cnf.cnfx_rewrite_tac ctxt 1
blanchet@39269
   146
   else
blanchet@39269
   147
     all_tac) st0
blanchet@39269
   148
blanchet@38652
   149
val type_has_top_sort =
blanchet@38652
   150
  exists_subtype (fn TFree (_, []) => true | TVar (_, []) => true | _ => false)
blanchet@38652
   151
blanchet@37516
   152
fun generic_metis_tac mode ctxt ths i st0 =
blanchet@37926
   153
  let
blanchet@39978
   154
    val _ = trace_msg ctxt (fn () =>
wenzelm@32956
   155
        "Metis called with theorems " ^ cat_lines (map (Display.string_of_thm ctxt) ths))
wenzelm@32956
   156
  in
blanchet@37626
   157
    if exists_type type_has_top_sort (prop_of st0) then
blanchet@40665
   158
      (verbose_warning ctxt "Metis: Proof state contains the universal sort {}";
blanchet@40665
   159
       Seq.empty)
wenzelm@35568
   160
    else
blanchet@39594
   161
      Meson.MESON (preskolem_tac ctxt) (maps neg_clausify)
blanchet@40262
   162
                  (fn cls => resolve_tac (FOL_SOLVE mode ctxt cls ths) 1)
blanchet@39594
   163
                  ctxt i st0
wenzelm@32956
   164
  end
wenzelm@23442
   165
blanchet@37516
   166
val metis_tac = generic_metis_tac HO
blanchet@37516
   167
val metisF_tac = generic_metis_tac FO
blanchet@37516
   168
val metisFT_tac = generic_metis_tac FT
wenzelm@23442
   169
blanchet@38632
   170
(* Whenever "X" has schematic type variables, we treat "using X by metis" as
blanchet@38632
   171
   "by (metis X)", to prevent "Subgoal.FOCUS" from freezing the type variables.
blanchet@38632
   172
   We don't do it for nonschematic facts "X" because this breaks a few proofs
blanchet@38632
   173
   (in the rare and subtle case where a proof relied on extensionality not being
blanchet@38994
   174
   applied) and brings few benefits. *)
blanchet@38632
   175
val has_tvar =
blanchet@38632
   176
  exists_type (exists_subtype (fn TVar _ => true | _ => false)) o prop_of
blanchet@37516
   177
fun method name mode =
blanchet@37516
   178
  Method.setup name (Attrib.thms >> (fn ths => fn ctxt =>
blanchet@38632
   179
    METHOD (fn facts =>
blanchet@38632
   180
               let
blanchet@38632
   181
                 val (schem_facts, nonschem_facts) =
blanchet@38632
   182
                   List.partition has_tvar facts
blanchet@38632
   183
               in
blanchet@38632
   184
                 HEADGOAL (Method.insert_tac nonschem_facts THEN'
blanchet@38632
   185
                           CHANGED_PROP
blanchet@38632
   186
                           o generic_metis_tac mode ctxt (schem_facts @ ths))
blanchet@38632
   187
               end)))
wenzelm@23442
   188
wenzelm@32956
   189
val setup =
blanchet@37516
   190
  type_lits_setup
blanchet@39891
   191
  #> new_skolemizer_setup
blanchet@37516
   192
  #> method @{binding metis} HO "Metis for FOL/HOL problems"
blanchet@37516
   193
  #> method @{binding metisF} FO "Metis for FOL problems"
blanchet@37516
   194
  #> method @{binding metisFT} FT
blanchet@37516
   195
            "Metis for FOL/HOL problems with fully-typed translation"
wenzelm@23442
   196
wenzelm@23442
   197
end;