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