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