src/HOL/Tools/Sledgehammer/metis_tactics.ML
author blanchet
Wed Sep 29 23:06:02 2010 +0200 (2010-09-29)
changeset 39887 74939e2afb95
parent 39886 8a9f0c97d550
child 39890 a1695e2169d0
permissions -rw-r--r--
second step in introducing the new Skolemizer -- notably, added procedure for discharging Skolem assumptions
blanchet@35826
     1
(*  Title:      HOL/Tools/Sledgehammer/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 trace : bool Unsynchronized.ref
blanchet@39497
    13
  val type_lits : 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@39497
    26
fun trace_msg msg = if !trace then tracing (msg ()) else ()
wenzelm@32955
    27
wenzelm@36001
    28
val (type_lits, type_lits_setup) = Attrib.config_bool "metis_type_lits" (K true);
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@39887
    54
(* In principle, it should be sufficient to apply "assume_tac" to unify the
blanchet@39887
    55
   conclusion with one of the premises. However, in practice, this fails
blanchet@39887
    56
   horribly because of the mildly higher-order nature of the unification
blanchet@39887
    57
   problems. Typical constraints are of the form "?x a b =?= b", where "a" and
blanchet@39887
    58
   "b" are goal parameters. *)
blanchet@39887
    59
fun unify_one_prem_with_concl thy i th =
blanchet@39887
    60
  let
blanchet@39887
    61
    val goal = Logic.get_goal (prop_of th) i |> Envir.beta_eta_contract
blanchet@39887
    62
    val prems = Logic.strip_assums_hyp goal
blanchet@39887
    63
    val concl = Logic.strip_assums_concl goal
blanchet@39887
    64
    fun add_types Tp instT =
blanchet@39887
    65
      if exists (curry (op =) Tp) instT then instT
blanchet@39887
    66
      else Tp :: map (apsnd (typ_subst_atomic [Tp])) instT
blanchet@39887
    67
    fun unify_types (T, U) =
blanchet@39887
    68
      if T = U then
blanchet@39887
    69
        I
blanchet@39887
    70
      else case (T, U) of
blanchet@39887
    71
        (TVar _, _) => add_types (T, U)
blanchet@39887
    72
      | (_, TVar _) => add_types (U, T)
blanchet@39887
    73
      | (Type (s, Ts), Type (t, Us)) =>
blanchet@39887
    74
        if s = t andalso length Ts = length Us then fold unify_types (Ts ~~ Us)
blanchet@39887
    75
        else raise TYPE ("unify_types", [T, U], [])
blanchet@39887
    76
      | _ => raise TYPE ("unify_types", [T, U], [])
blanchet@39887
    77
    fun pair_untyped_aconv (t1, t2) (u1, u2) =
blanchet@39887
    78
      untyped_aconv t1 u1 andalso untyped_aconv t2 u2
blanchet@39887
    79
    fun add_terms tp inst =
blanchet@39887
    80
      if exists (pair_untyped_aconv tp) inst then inst
blanchet@39887
    81
      else tp :: map (apsnd (subst_atomic [tp])) inst
blanchet@39887
    82
    fun is_flex t =
blanchet@39887
    83
      case strip_comb t of
blanchet@39887
    84
        (Var _, args) => forall (is_Bound orf is_Var orf is_Free) args
blanchet@39887
    85
      | _ => false
blanchet@39887
    86
    fun unify_flex flex rigid =
blanchet@39887
    87
      case strip_comb flex of
blanchet@39887
    88
        (Var (z as (_, T)), args) =>
blanchet@39887
    89
        add_terms (Var z,
blanchet@39887
    90
          (* FIXME: reindex bound variables *)
blanchet@39887
    91
          fold_rev (curry absdummy) (take (length args) (binder_types T)) rigid)
blanchet@39887
    92
      | _ => raise TERM ("unify_flex: expected flex", [flex])
blanchet@39887
    93
    fun unify_potential_flex comb atom =
blanchet@39887
    94
      if is_flex comb then unify_flex comb atom
blanchet@39887
    95
      else if is_Var atom then add_terms (atom, comb)
blanchet@39887
    96
      else raise TERM ("unify_terms", [comb, atom])
blanchet@39887
    97
    fun unify_terms (t, u) =
blanchet@39887
    98
      case (t, u) of
blanchet@39887
    99
        (t1 $ t2, u1 $ u2) =>
blanchet@39887
   100
        if is_flex t then unify_flex t u
blanchet@39887
   101
        else if is_flex u then unify_flex u t
blanchet@39887
   102
        else fold unify_terms [(t1, u1), (t2, u2)]
blanchet@39887
   103
      | (_ $ _, _) => unify_potential_flex t u
blanchet@39887
   104
      | (_, _ $ _) => unify_potential_flex u t
blanchet@39887
   105
      | (Var _, _) => add_terms (t, u)
blanchet@39887
   106
      | (_, Var _) => add_terms (u, t)
blanchet@39887
   107
      | _ => if untyped_aconv t u then I else raise TERM ("unify_terms", [t, u])
blanchet@39887
   108
    fun unify_prem prem =
blanchet@39887
   109
      let
blanchet@39887
   110
        val inst = [] |> unify_terms (prem, concl)
blanchet@39887
   111
        val instT = fold (unify_types o pairself fastype_of) inst []
blanchet@39887
   112
        val inst = inst |> map (pairself (subst_atomic_types instT))
blanchet@39887
   113
        val cinstT = instT |> map (pairself (ctyp_of thy))
blanchet@39887
   114
        val cinst = inst |> map (pairself (cterm_of thy))
blanchet@39887
   115
      in th |> Thm.instantiate (cinstT, []) |> Thm.instantiate ([], cinst) end
blanchet@39887
   116
  in
blanchet@39887
   117
    case prems of
blanchet@39887
   118
      [prem] => unify_prem prem
blanchet@39887
   119
    | _ =>
blanchet@39887
   120
      case fold (fn prem => fn th as SOME _ => th
blanchet@39887
   121
                             | NONE => try unify_prem prem) prems NONE of
blanchet@39887
   122
        SOME th => th
blanchet@39887
   123
      | NONE => raise Fail "unify_one_prem_with_concl"
blanchet@39887
   124
  end
blanchet@39887
   125
blanchet@39887
   126
(* Attempts to derive the theorem "False" from a theorem of the form
blanchet@39887
   127
   "P1 ==> ... ==> Pn ==> False", where the "Pi"s are to be discharged using the
blanchet@39887
   128
   specified axioms. The axioms have leading "All" and "Ex" quantifiers, which
blanchet@39887
   129
   must be eliminated first. *)
blanchet@39887
   130
fun discharge_skolem_premises ctxt axioms premises_imp_false =
blanchet@39887
   131
  if prop_of premises_imp_false aconv @{prop False} then
blanchet@39887
   132
    premises_imp_false
blanchet@39887
   133
  else
blanchet@39887
   134
    let val thy = ProofContext.theory_of ctxt in
blanchet@39887
   135
      Goal.prove ctxt [] [] @{prop False}
blanchet@39887
   136
          (K (cut_rules_tac axioms 1
blanchet@39887
   137
              THEN TRY (REPEAT_ALL_NEW (etac @{thm exE}) 1)
blanchet@39887
   138
(* FIXME: THEN etac @{lemma "P ==> (P ==> P ==> Q) ==> Q" by fast} 1 *)
blanchet@39887
   139
              THEN TRY (REPEAT_ALL_NEW (etac @{thm allE}) 1)
blanchet@39887
   140
              THEN match_tac [premises_imp_false] 1
blanchet@39887
   141
              THEN DETERM_UNTIL_SOLVED
blanchet@39887
   142
                       (PRIMITIVE (unify_one_prem_with_concl thy 1)
blanchet@39887
   143
                        THEN assume_tac 1)))
blanchet@39887
   144
    end
blanchet@39887
   145
blanchet@37516
   146
(* Main function to start Metis proof and reconstruction *)
wenzelm@32956
   147
fun FOL_SOLVE mode ctxt cls ths0 =
wenzelm@32956
   148
  let val thy = ProofContext.theory_of ctxt
blanchet@39497
   149
      val type_lits = Config.get ctxt type_lits
blanchet@35826
   150
      val th_cls_pairs =
blanchet@39886
   151
        map (fn th => (Thm.get_name_hint th, Meson_Clausifier.cnf_axiom thy th))
blanchet@39886
   152
            ths0
blanchet@39887
   153
      val thss = map (snd o snd) th_cls_pairs
blanchet@39887
   154
      val dischargers = map_filter (fst o snd) th_cls_pairs
wenzelm@32956
   155
      val _ = trace_msg (fn () => "FOL_SOLVE: CONJECTURE CLAUSES")
wenzelm@32956
   156
      val _ = app (fn th => trace_msg (fn () => Display.string_of_thm ctxt th)) cls
wenzelm@32956
   157
      val _ = trace_msg (fn () => "THEOREM CLAUSES")
blanchet@39886
   158
      val _ = app (app (fn th => trace_msg (fn () => Display.string_of_thm ctxt th))) thss
blanchet@39886
   159
      val (mode, {axioms, tfrees, old_skolems}) =
blanchet@39886
   160
        build_logic_map mode ctxt type_lits cls thss
wenzelm@32956
   161
      val _ = if null tfrees then ()
wenzelm@32956
   162
              else (trace_msg (fn () => "TFREE CLAUSES");
blanchet@37643
   163
                    app (fn TyLitFree ((s, _), (s', _)) =>
blanchet@39887
   164
                            trace_msg (fn () => s ^ "(" ^ s' ^ ")")) tfrees)
wenzelm@32956
   165
      val _ = trace_msg (fn () => "CLAUSES GIVEN TO METIS")
wenzelm@32956
   166
      val thms = map #1 axioms
blanchet@39419
   167
      val _ = app (fn th => trace_msg (fn () => Metis_Thm.toString th)) thms
wenzelm@32956
   168
      val _ = trace_msg (fn () => "mode = " ^ string_of_mode mode)
wenzelm@32956
   169
      val _ = trace_msg (fn () => "START METIS PROVE PROCESS")
wenzelm@32956
   170
  in
wenzelm@33317
   171
      case filter (is_false o prop_of) cls of
wenzelm@32956
   172
          false_th::_ => [false_th RS @{thm FalseE}]
wenzelm@32956
   173
        | [] =>
blanchet@39497
   174
      case Metis_Resolution.new resolution_params {axioms = thms, conjecture = []}
blanchet@39497
   175
           |> Metis_Resolution.loop of
blanchet@39419
   176
          Metis_Resolution.Contradiction mth =>
wenzelm@32956
   177
            let val _ = trace_msg (fn () => "METIS RECONSTRUCTION START: " ^
blanchet@39419
   178
                          Metis_Thm.toString mth)
wenzelm@32956
   179
                val ctxt' = fold Variable.declare_constraints (map prop_of cls) ctxt
wenzelm@32956
   180
                             (*add constraints arising from converting goal to clause form*)
blanchet@39419
   181
                val proof = Metis_Proof.proof mth
blanchet@39886
   182
                val result =
blanchet@39886
   183
                  fold (replay_one_inference ctxt' mode old_skolems) proof axioms
wenzelm@32956
   184
                and used = map_filter (used_axioms axioms) proof
wenzelm@32956
   185
                val _ = trace_msg (fn () => "METIS COMPLETED...clauses actually used:")
wenzelm@32956
   186
                val _ = app (fn th => trace_msg (fn () => Display.string_of_thm ctxt th)) used
blanchet@39887
   187
                val unused = th_cls_pairs |> map_filter (fn (name, (_, cls)) =>
blanchet@39497
   188
                  if have_common_thm used cls then NONE else SOME name)
wenzelm@32956
   189
            in
blanchet@39497
   190
                if not (null cls) andalso not (have_common_thm used cls) then
blanchet@36383
   191
                  warning "Metis: The assumptions are inconsistent."
blanchet@36383
   192
                else
blanchet@36383
   193
                  ();
blanchet@36383
   194
                if not (null unused) then
blanchet@36230
   195
                  warning ("Metis: Unused theorems: " ^ commas_quote unused
blanchet@36230
   196
                           ^ ".")
blanchet@36230
   197
                else
blanchet@36230
   198
                  ();
wenzelm@32956
   199
                case result of
wenzelm@32956
   200
                    (_,ith)::_ =>
blanchet@36230
   201
                        (trace_msg (fn () => "Success: " ^ Display.string_of_thm ctxt ith);
blanchet@39887
   202
                         [discharge_skolem_premises ctxt dischargers ith])
blanchet@38097
   203
                  | _ => (trace_msg (fn () => "Metis: No result"); [])
wenzelm@32956
   204
            end
blanchet@39419
   205
        | Metis_Resolution.Satisfiable _ =>
wenzelm@32956
   206
            (trace_msg (fn () => "Metis: No first-order proof with the lemmas supplied");
blanchet@38097
   207
             [])
wenzelm@32956
   208
  end;
wenzelm@23442
   209
blanchet@38632
   210
(* Extensionalize "th", because that makes sense and that's what Sledgehammer
blanchet@38632
   211
   does, but also keep an unextensionalized version of "th" for backward
blanchet@38632
   212
   compatibility. *)
blanchet@38632
   213
fun also_extensionalize_theorem th =
blanchet@39720
   214
  let val th' = Meson_Clausifier.extensionalize_theorem th in
blanchet@38632
   215
    if Thm.eq_thm (th, th') then [th]
blanchet@38632
   216
    else th :: Meson.make_clauses_unsorted [th']
blanchet@38632
   217
  end
blanchet@38632
   218
blanchet@38028
   219
val neg_clausify =
blanchet@38028
   220
  single
blanchet@38028
   221
  #> Meson.make_clauses_unsorted
blanchet@38632
   222
  #> maps also_extensionalize_theorem
blanchet@39720
   223
  #> map Meson_Clausifier.introduce_combinators_in_theorem
blanchet@38028
   224
  #> Meson.finish_cnf
blanchet@38028
   225
blanchet@39269
   226
fun preskolem_tac ctxt st0 =
blanchet@39269
   227
  (if exists (Meson.has_too_many_clauses ctxt)
blanchet@39269
   228
             (Logic.prems_of_goal (prop_of st0) 1) then
blanchet@39269
   229
     cnf.cnfx_rewrite_tac ctxt 1
blanchet@39269
   230
   else
blanchet@39269
   231
     all_tac) st0
blanchet@39269
   232
blanchet@38652
   233
val type_has_top_sort =
blanchet@38652
   234
  exists_subtype (fn TFree (_, []) => true | TVar (_, []) => true | _ => false)
blanchet@38652
   235
blanchet@37516
   236
fun generic_metis_tac mode ctxt ths i st0 =
blanchet@37926
   237
  let
blanchet@37926
   238
    val _ = trace_msg (fn () =>
wenzelm@32956
   239
        "Metis called with theorems " ^ cat_lines (map (Display.string_of_thm ctxt) ths))
wenzelm@32956
   240
  in
blanchet@37626
   241
    if exists_type type_has_top_sort (prop_of st0) then
blanchet@37516
   242
      (warning ("Metis: Proof state contains the universal sort {}"); Seq.empty)
wenzelm@35568
   243
    else
blanchet@39594
   244
      Meson.MESON (preskolem_tac ctxt) (maps neg_clausify)
blanchet@39594
   245
                  (fn cls => resolve_tac (FOL_SOLVE mode ctxt cls ths) 1)
blanchet@39594
   246
                  ctxt i st0
wenzelm@32956
   247
  end
wenzelm@23442
   248
blanchet@37516
   249
val metis_tac = generic_metis_tac HO
blanchet@37516
   250
val metisF_tac = generic_metis_tac FO
blanchet@37516
   251
val metisFT_tac = generic_metis_tac FT
wenzelm@23442
   252
blanchet@38632
   253
(* Whenever "X" has schematic type variables, we treat "using X by metis" as
blanchet@38632
   254
   "by (metis X)", to prevent "Subgoal.FOCUS" from freezing the type variables.
blanchet@38632
   255
   We don't do it for nonschematic facts "X" because this breaks a few proofs
blanchet@38632
   256
   (in the rare and subtle case where a proof relied on extensionality not being
blanchet@38994
   257
   applied) and brings few benefits. *)
blanchet@38632
   258
val has_tvar =
blanchet@38632
   259
  exists_type (exists_subtype (fn TVar _ => true | _ => false)) o prop_of
blanchet@37516
   260
fun method name mode =
blanchet@37516
   261
  Method.setup name (Attrib.thms >> (fn ths => fn ctxt =>
blanchet@38632
   262
    METHOD (fn facts =>
blanchet@38632
   263
               let
blanchet@38632
   264
                 val (schem_facts, nonschem_facts) =
blanchet@38632
   265
                   List.partition has_tvar facts
blanchet@38632
   266
               in
blanchet@38632
   267
                 HEADGOAL (Method.insert_tac nonschem_facts THEN'
blanchet@38632
   268
                           CHANGED_PROP
blanchet@38632
   269
                           o generic_metis_tac mode ctxt (schem_facts @ ths))
blanchet@38632
   270
               end)))
wenzelm@23442
   271
wenzelm@32956
   272
val setup =
blanchet@37516
   273
  type_lits_setup
blanchet@37516
   274
  #> method @{binding metis} HO "Metis for FOL/HOL problems"
blanchet@37516
   275
  #> method @{binding metisF} FO "Metis for FOL problems"
blanchet@37516
   276
  #> method @{binding metisFT} FT
blanchet@37516
   277
            "Metis for FOL/HOL problems with fully-typed translation"
wenzelm@23442
   278
wenzelm@23442
   279
end;