src/HOL/Tools/Metis/metis_tactic.ML
author wenzelm
Tue Jun 02 09:16:19 2015 +0200 (2015-06-02)
changeset 60358 aebfbcab1eb8
parent 59632 5980e75a204e
child 60362 befdc10ebb42
permissions -rw-r--r--
clarified context;
blanchet@44651
     1
(*  Title:      HOL/Tools/Metis/metis_tactic.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@44651
    10
signature METIS_TACTIC =
wenzelm@23442
    11
sig
blanchet@39979
    12
  val trace : bool Config.T
blanchet@40665
    13
  val verbose : bool Config.T
blanchet@50705
    14
  val new_skolem : bool Config.T
blanchet@47039
    15
  val advisory_simp : bool Config.T
blanchet@55521
    16
  val metis_tac_unused : string list -> string -> Proof.context -> thm list -> int -> thm ->
blanchet@55521
    17
    thm list * thm Seq.seq
blanchet@54756
    18
  val metis_tac : string list -> string -> Proof.context -> thm list -> int -> tactic
blanchet@45521
    19
  val metis_lam_transs : string list
blanchet@45519
    20
  val parse_metis_options : (string list option * string option) parser
wenzelm@23442
    21
end
wenzelm@23442
    22
blanchet@44651
    23
structure Metis_Tactic : METIS_TACTIC =
wenzelm@23442
    24
struct
wenzelm@23442
    25
blanchet@46320
    26
open ATP_Problem_Generate
blanchet@46320
    27
open ATP_Proof_Reconstruct
blanchet@46320
    28
open Metis_Generate
blanchet@39497
    29
open Metis_Reconstruct
blanchet@35826
    30
blanchet@54756
    31
val new_skolem = Attrib.setup_config_bool @{binding metis_new_skolem} (K false)
blanchet@54756
    32
val advisory_simp = Attrib.setup_config_bool @{binding metis_advisory_simp} (K true)
wenzelm@23442
    33
blanchet@43134
    34
(* Designed to work also with monomorphic instances of polymorphic theorems. *)
wenzelm@60358
    35
fun have_common_thm ctxt ths1 ths2 =
wenzelm@60358
    36
  exists (member (Term.aconv_untyped o apply2 Thm.prop_of) ths1)
wenzelm@60358
    37
    (map (Meson.make_meta_clause ctxt) ths2)
wenzelm@23442
    38
wenzelm@32956
    39
(*Determining which axiom clauses are actually used*)
blanchet@39419
    40
fun used_axioms axioms (th, Metis_Proof.Axiom _) = SOME (lookth axioms th)
blanchet@43128
    41
  | used_axioms _ _ = NONE
paulson@24855
    42
blanchet@43129
    43
(* Lightweight predicate type information comes in two flavors, "t = t'" and
blanchet@43129
    44
   "t => t'", where "t" and "t'" are the same term modulo type tags.
blanchet@43129
    45
   In Isabelle, type tags are stripped away, so we are left with "t = t" or
blanchet@43159
    46
   "t => t". Type tag idempotence is also handled this way. *)
blanchet@52031
    47
fun reflexive_or_trivial_of_metis ctxt type_enc sym_tab concealed mth =
wenzelm@59632
    48
  (case hol_clause_of_metis ctxt type_enc sym_tab concealed mth of
wenzelm@59632
    49
    Const (@{const_name HOL.eq}, _) $ _ $ t =>
wenzelm@59632
    50
    let
wenzelm@59632
    51
      val ct = Thm.cterm_of ctxt t
wenzelm@59632
    52
      val cT = Thm.ctyp_of_cterm ct
wenzelm@59632
    53
    in refl |> Drule.instantiate' [SOME cT] [SOME ct] end
wenzelm@59632
    54
  | Const (@{const_name disj}, _) $ t1 $ t2 =>
wenzelm@59632
    55
    (if can HOLogic.dest_not t1 then t2 else t1)
wenzelm@59632
    56
    |> HOLogic.mk_Trueprop |> Thm.cterm_of ctxt |> Thm.trivial
wenzelm@59632
    57
  | _ => raise Fail "expected reflexive or trivial clause")
wenzelm@60358
    58
  |> Meson.make_meta_clause ctxt
blanchet@43129
    59
blanchet@52031
    60
fun lam_lifted_of_metis ctxt type_enc sym_tab concealed mth =
blanchet@45511
    61
  let
wenzelm@59498
    62
    val tac = rewrite_goals_tac ctxt @{thms lambda_def [abs_def]} THEN resolve_tac ctxt [refl] 1
blanchet@52031
    63
    val t = hol_clause_of_metis ctxt type_enc sym_tab concealed mth
wenzelm@59632
    64
    val ct = Thm.cterm_of ctxt (HOLogic.mk_Trueprop t)
wenzelm@60358
    65
  in Goal.prove_internal ctxt [] ct (K tac) |> Meson.make_meta_clause ctxt end
blanchet@45511
    66
blanchet@45570
    67
fun add_vars_and_frees (t $ u) = fold (add_vars_and_frees) [t, u]
blanchet@45570
    68
  | add_vars_and_frees (Abs (_, _, t)) = add_vars_and_frees t
blanchet@45570
    69
  | add_vars_and_frees (t as Var _) = insert (op =) t
blanchet@45570
    70
  | add_vars_and_frees (t as Free _) = insert (op =) t
blanchet@45570
    71
  | add_vars_and_frees _ = I
blanchet@45570
    72
blanchet@45569
    73
fun introduce_lam_wrappers ctxt th =
wenzelm@59582
    74
  if Meson_Clausify.is_quasi_lambda_free (Thm.prop_of th) then
blanchet@45511
    75
    th
blanchet@45511
    76
  else
blanchet@45511
    77
    let
blanchet@45570
    78
      fun conv first ctxt ct =
wenzelm@59582
    79
        if Meson_Clausify.is_quasi_lambda_free (Thm.term_of ct) then
blanchet@45511
    80
          Thm.reflexive ct
blanchet@57408
    81
        else
wenzelm@59582
    82
          (case Thm.term_of ct of
blanchet@57408
    83
            Abs (_, _, u) =>
blanchet@57408
    84
            if first then
blanchet@57408
    85
              (case add_vars_and_frees u [] of
blanchet@57408
    86
                [] =>
blanchet@57408
    87
                Conv.abs_conv (conv false o snd) ctxt ct
blanchet@57408
    88
                |> (fn th => Meson.first_order_resolve th @{thm Metis.eq_lambdaI})
blanchet@57408
    89
              | v :: _ =>
wenzelm@59582
    90
                Abs (Name.uu, fastype_of v, abstract_over (v, Thm.term_of ct)) $ v
wenzelm@59632
    91
                |> Thm.cterm_of ctxt
blanchet@57408
    92
                |> Conv.comb_conv (conv true ctxt))
blanchet@57408
    93
            else
blanchet@45570
    94
              Conv.abs_conv (conv false o snd) ctxt ct
blanchet@57408
    95
          | Const (@{const_name Meson.skolem}, _) $ _ => Thm.reflexive ct
blanchet@57408
    96
          | _ => Conv.comb_conv (conv true ctxt) ct)
wenzelm@59582
    97
      val eq_th = conv true ctxt (Thm.cprop_of th)
blanchet@45570
    98
      (* We replace the equation's left-hand side with a beta-equivalent term
blanchet@45570
    99
         so that "Thm.equal_elim" works below. *)
wenzelm@59582
   100
      val t0 $ _ $ t2 = Thm.prop_of eq_th
wenzelm@59632
   101
      val eq_ct = t0 $ Thm.prop_of th $ t2 |> Thm.cterm_of ctxt
wenzelm@59498
   102
      val eq_th' = Goal.prove_internal ctxt [] eq_ct (K (resolve_tac ctxt [eq_th] 1))
blanchet@45570
   103
    in Thm.equal_elim eq_th' th end
blanchet@45511
   104
blanchet@47039
   105
fun clause_params ordering =
blanchet@47039
   106
  {ordering = ordering,
blanchet@44492
   107
   orderLiterals = Metis_Clause.UnsignedLiteralOrder,
blanchet@39450
   108
   orderTerms = true}
blanchet@47039
   109
fun active_params ordering =
blanchet@47039
   110
  {clause = clause_params ordering,
blanchet@39450
   111
   prefactor = #prefactor Metis_Active.default,
blanchet@39450
   112
   postfactor = #postfactor Metis_Active.default}
blanchet@39450
   113
val waiting_params =
blanchet@39450
   114
  {symbolsWeight = 1.0,
blanchet@47047
   115
   variablesWeight = 0.05,
blanchet@47047
   116
   literalsWeight = 0.01,
blanchet@39450
   117
   models = []}
blanchet@47039
   118
fun resolution_params ordering =
blanchet@47039
   119
  {active = active_params ordering, waiting = waiting_params}
blanchet@47039
   120
blanchet@47039
   121
fun kbo_advisory_simp_ordering ord_info =
blanchet@47039
   122
  let
blanchet@47039
   123
    fun weight (m, _) =
blanchet@47039
   124
      AList.lookup (op =) ord_info (Metis_Name.toString m) |> the_default 1
blanchet@47039
   125
    fun precedence p =
wenzelm@59058
   126
      (case int_ord (apply2 weight p) of
blanchet@47039
   127
        EQUAL => #precedence Metis_KnuthBendixOrder.default p
blanchet@57408
   128
      | ord => ord)
blanchet@47039
   129
  in {weight = weight, precedence = precedence} end
blanchet@37573
   130
blanchet@55285
   131
fun metis_call type_enc lam_trans =
blanchet@55285
   132
  let
blanchet@55285
   133
    val type_enc =
blanchet@55285
   134
      (case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases type_enc of
blanchet@55285
   135
        [alias] => alias
blanchet@55285
   136
      | _ => type_enc)
blanchet@55285
   137
    val opts =
blanchet@55285
   138
      [] |> type_enc <> partial_typesN ? cons type_enc
blanchet@55285
   139
         |> lam_trans <> default_metis_lam_trans ? cons lam_trans
blanchet@55285
   140
  in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end
blanchet@55285
   141
blanchet@50875
   142
exception METIS_UNPROVABLE of unit
blanchet@50875
   143
blanchet@37516
   144
(* Main function to start Metis proof and reconstruction *)
blanchet@55521
   145
fun FOL_SOLVE unused (type_enc :: fallback_type_encs) lam_trans ctxt cls ths0 =
wenzelm@42361
   146
  let val thy = Proof_Context.theory_of ctxt
blanchet@50705
   147
      val new_skolem =
blanchet@50705
   148
        Config.get ctxt new_skolem orelse null (Meson.choice_theorems thy)
blanchet@46365
   149
      val do_lams =
blanchet@46365
   150
        (lam_trans = liftingN orelse lam_trans = lam_liftingN)
blanchet@46365
   151
        ? introduce_lam_wrappers ctxt
blanchet@35826
   152
      val th_cls_pairs =
blanchet@39894
   153
        map2 (fn j => fn th =>
blanchet@39894
   154
                (Thm.get_name_hint th,
blanchet@45570
   155
                 th |> Drule.eta_contraction_rule
blanchet@57263
   156
                    |> Meson_Clausify.cnf_axiom ctxt new_skolem (lam_trans = combsN) j
blanchet@45570
   157
                    ||> map do_lams))
blanchet@39894
   158
             (0 upto length ths0 - 1) ths0
blanchet@43092
   159
      val ths = maps (snd o snd) th_cls_pairs
blanchet@39938
   160
      val dischargers = map (fst o snd) th_cls_pairs
blanchet@45570
   161
      val cls = cls |> map (Drule.eta_contraction_rule #> do_lams)
blanchet@55521
   162
      val _ = trace_msg ctxt (K "FOL_SOLVE: CONJECTURE CLAUSES")
blanchet@39978
   163
      val _ = app (fn th => trace_msg ctxt (fn () => Display.string_of_thm ctxt th)) cls
blanchet@44411
   164
      val _ = trace_msg ctxt (fn () => "type_enc = " ^ type_enc)
blanchet@52031
   165
      val type_enc = type_enc_of_string Strict type_enc
blanchet@47039
   166
      val (sym_tab, axioms, ord_info, concealed) =
blanchet@57263
   167
        generate_metis_problem ctxt type_enc lam_trans cls ths
blanchet@43159
   168
      fun get_isa_thm mth Isa_Reflexive_or_Trivial =
blanchet@52031
   169
          reflexive_or_trivial_of_metis ctxt type_enc sym_tab concealed mth
blanchet@45511
   170
        | get_isa_thm mth Isa_Lambda_Lifted =
blanchet@52031
   171
          lam_lifted_of_metis ctxt type_enc sym_tab concealed mth
blanchet@45569
   172
        | get_isa_thm _ (Isa_Raw ith) = ith
blanchet@45569
   173
      val axioms = axioms |> map (fn (mth, ith) => (mth, get_isa_thm mth ith))
blanchet@55521
   174
      val _ = trace_msg ctxt (K "ISABELLE CLAUSES")
blanchet@45559
   175
      val _ = app (fn (_, ith) => trace_msg ctxt (fn () => Display.string_of_thm ctxt ith)) axioms
blanchet@55521
   176
      val _ = trace_msg ctxt (K "METIS CLAUSES")
blanchet@45559
   177
      val _ = app (fn (mth, _) => trace_msg ctxt (fn () => Metis_Thm.toString mth)) axioms
blanchet@55521
   178
      val _ = trace_msg ctxt (K "START METIS PROVE PROCESS")
blanchet@47039
   179
      val ordering =
blanchet@47039
   180
        if Config.get ctxt advisory_simp then
blanchet@47039
   181
          kbo_advisory_simp_ordering (ord_info ())
blanchet@47039
   182
        else
blanchet@47039
   183
          Metis_KnuthBendixOrder.default
blanchet@50875
   184
    fun fall_back () =
blanchet@50875
   185
      (verbose_warning ctxt
blanchet@55257
   186
           ("Falling back on " ^ quote (metis_call (hd fallback_type_encs) lam_trans) ^ "...");
blanchet@55521
   187
       FOL_SOLVE unused fallback_type_encs lam_trans ctxt cls ths0)
wenzelm@32956
   188
  in
wenzelm@59582
   189
    (case filter (fn t => Thm.prop_of t aconv @{prop False}) cls of
blanchet@55521
   190
       false_th :: _ => [false_th RS @{thm FalseE}]
blanchet@55521
   191
     | [] =>
blanchet@55521
   192
     (case Metis_Resolution.loop (Metis_Resolution.new (resolution_params ordering)
blanchet@55521
   193
         {axioms = axioms |> map fst, conjecture = []}) of
blanchet@55521
   194
       Metis_Resolution.Contradiction mth =>
blanchet@55521
   195
       let
blanchet@55521
   196
         val _ = trace_msg ctxt (fn () => "METIS RECONSTRUCTION START: " ^ Metis_Thm.toString mth)
wenzelm@59582
   197
         val ctxt' = fold Variable.declare_constraints (map Thm.prop_of cls) ctxt
blanchet@55521
   198
                      (*add constraints arising from converting goal to clause form*)
blanchet@55521
   199
         val proof = Metis_Proof.proof mth
blanchet@55521
   200
         val result = fold (replay_one_inference ctxt' type_enc concealed sym_tab) proof axioms
blanchet@55521
   201
         val used = map_filter (used_axioms axioms) proof
blanchet@55521
   202
         val _ = trace_msg ctxt (K "METIS COMPLETED; clauses actually used:")
blanchet@55521
   203
         val _ = app (fn th => trace_msg ctxt (fn () => Display.string_of_thm ctxt th)) used
blanchet@55521
   204
         val (used_th_cls_pairs, unused_th_cls_pairs) =
wenzelm@60358
   205
           List.partition (have_common_thm ctxt used o snd o snd) th_cls_pairs
blanchet@55521
   206
         val unused_ths = maps (snd o snd) unused_th_cls_pairs
blanchet@55521
   207
         val unused_names = map fst unused_th_cls_pairs
blanchet@55521
   208
       in
blanchet@55521
   209
         unused := unused_ths;
blanchet@55521
   210
         if not (null unused_names) then
blanchet@55521
   211
           verbose_warning ctxt ("Unused theorems: " ^ commas_quote unused_names)
blanchet@55521
   212
         else
blanchet@55521
   213
           ();
wenzelm@60358
   214
         if not (null cls) andalso not (have_common_thm ctxt used cls) then
blanchet@55521
   215
           verbose_warning ctxt "The assumptions are inconsistent"
blanchet@55521
   216
         else
blanchet@55521
   217
           ();
blanchet@55521
   218
         (case result of
blanchet@55521
   219
           (_, ith) :: _ =>
blanchet@55521
   220
           (trace_msg ctxt (fn () => "Success: " ^ Display.string_of_thm ctxt ith);
blanchet@55521
   221
            [discharge_skolem_premises ctxt dischargers ith])
blanchet@55521
   222
         | _ => (trace_msg ctxt (K "Metis: No result"); []))
blanchet@55521
   223
       end
blanchet@55521
   224
     | Metis_Resolution.Satisfiable _ =>
blanchet@55521
   225
       (trace_msg ctxt (K "Metis: No first-order proof with the supplied lemmas");
blanchet@55521
   226
        raise METIS_UNPROVABLE ()))
blanchet@55521
   227
    handle METIS_UNPROVABLE () => if null fallback_type_encs then [] else fall_back ()
blanchet@50875
   228
         | METIS_RECONSTRUCT (loc, msg) =>
blanchet@55521
   229
           if null fallback_type_encs then
blanchet@55521
   230
             (verbose_warning ctxt ("Failed to replay Metis proof\n" ^ loc ^ ": " ^ msg); [])
blanchet@55521
   231
           else
blanchet@55521
   232
             fall_back ())
blanchet@42733
   233
  end
wenzelm@23442
   234
blanchet@45508
   235
fun neg_clausify ctxt combinators =
blanchet@38028
   236
  single
blanchet@43964
   237
  #> Meson.make_clauses_unsorted ctxt
wenzelm@55236
   238
  #> combinators ? map (Meson_Clausify.introduce_combinators_in_theorem ctxt)
blanchet@38028
   239
  #> Meson.finish_cnf
blanchet@38028
   240
blanchet@39269
   241
fun preskolem_tac ctxt st0 =
blanchet@39269
   242
  (if exists (Meson.has_too_many_clauses ctxt)
wenzelm@59582
   243
             (Logic.prems_of_goal (Thm.prop_of st0) 1) then
wenzelm@51717
   244
     Simplifier.full_simp_tac (Meson_Clausify.ss_only @{thms not_all not_ex} ctxt) 1
wenzelm@55239
   245
     THEN CNF.cnfx_rewrite_tac ctxt 1
blanchet@39269
   246
   else
blanchet@39269
   247
     all_tac) st0
blanchet@39269
   248
blanchet@55521
   249
fun metis_tac_unused type_encs0 lam_trans ctxt ths i st0 =
blanchet@37926
   250
  let
blanchet@55521
   251
    val unused = Unsynchronized.ref []
blanchet@55520
   252
    val type_encs = if null type_encs0 then partial_type_encs else type_encs0
blanchet@39978
   253
    val _ = trace_msg ctxt (fn () =>
blanchet@55315
   254
      "Metis called with theorems\n" ^ cat_lines (map (Display.string_of_thm ctxt) ths))
blanchet@45519
   255
    val type_encs = type_encs |> maps unalias_type_enc
blanchet@55521
   256
    val combs = (lam_trans = combsN)
wenzelm@59498
   257
    fun tac clause = resolve_tac ctxt (FOL_SOLVE unused type_encs lam_trans ctxt clause ths) 1
blanchet@55521
   258
    val seq = Meson.MESON (preskolem_tac ctxt) (maps (neg_clausify ctxt combs)) tac ctxt i st0
wenzelm@32956
   259
  in
blanchet@55521
   260
    (!unused, seq)
wenzelm@32956
   261
  end
wenzelm@23442
   262
blanchet@55521
   263
fun metis_tac type_encs lam_trans ctxt ths i = snd o metis_tac_unused type_encs lam_trans ctxt ths i
blanchet@55521
   264
blanchet@55520
   265
(* Whenever "X" has schematic type variables, we treat "using X by metis" as "by (metis X)" to
blanchet@55520
   266
   prevent "Subgoal.FOCUS" from freezing the type variables. We don't do it for nonschematic facts
blanchet@55520
   267
   "X" because this breaks a few proofs (in the rare and subtle case where a proof relied on
blanchet@55520
   268
   extensionality not being applied) and brings few benefits. *)
wenzelm@59582
   269
val has_tvar = exists_type (exists_subtype (fn TVar _ => true | _ => false)) o Thm.prop_of
blanchet@43034
   270
blanchet@55315
   271
fun metis_method ((override_type_encs, lam_trans), ths) ctxt facts =
blanchet@55520
   272
  let val (schem_facts, nonschem_facts) = List.partition has_tvar facts in
blanchet@43099
   273
    HEADGOAL (Method.insert_tac nonschem_facts THEN'
blanchet@55520
   274
      CHANGED_PROP o metis_tac (these override_type_encs)
blanchet@55520
   275
        (the_default default_metis_lam_trans lam_trans) ctxt (schem_facts @ ths))
blanchet@43099
   276
  end
blanchet@43100
   277
blanchet@46365
   278
val metis_lam_transs = [hide_lamsN, liftingN, combsN]
blanchet@45519
   279
blanchet@45578
   280
fun set_opt _ x NONE = SOME x
blanchet@45578
   281
  | set_opt get x (SOME x0) =
blanchet@55523
   282
    error ("Cannot specify both " ^ quote (get x0) ^ " and " ^ quote (get x))
blanchet@54756
   283
blanchet@45519
   284
fun consider_opt s =
blanchet@54756
   285
  if member (op =) metis_lam_transs s then apsnd (set_opt I s) else apfst (set_opt hd [s])
blanchet@45514
   286
blanchet@45519
   287
val parse_metis_options =
blanchet@45519
   288
  Scan.optional
wenzelm@58831
   289
      (Args.parens (Args.name -- Scan.option (@{keyword ","} |-- Args.name))
blanchet@45519
   290
       >> (fn (s, s') =>
blanchet@45519
   291
              (NONE, NONE) |> consider_opt s
blanchet@45519
   292
                           |> (case s' of SOME s' => consider_opt s' | _ => I)))
blanchet@45519
   293
      (NONE, NONE)
blanchet@45519
   294
wenzelm@58818
   295
val _ =
wenzelm@58818
   296
  Theory.setup
wenzelm@58818
   297
    (Method.setup @{binding metis}
wenzelm@58818
   298
      (Scan.lift parse_metis_options -- Attrib.thms >> (METHOD oo metis_method))
wenzelm@58818
   299
      "Metis for FOL and HOL problems")
wenzelm@23442
   300
wenzelm@23442
   301
end;