src/HOL/Mirabelle/Tools/sledgehammer_tactic.ML
author blanchet
Tue Dec 21 10:18:56 2010 +0100 (2010-12-21)
changeset 41357 ae76960d86a2
parent 41243 15ba335d0ba2
permissions -rw-r--r--
added "sledgehammer_tac" as possible reconstructor in Mirabelle
bulwahn@40633
     1
(*  Title:      sledgehammer_tactics.ML
bulwahn@40633
     2
    Author:     Jasmin Blanchette, TU Muenchen
bulwahn@40633
     3
    Copyright   2010
bulwahn@40633
     4
bulwahn@40633
     5
Sledgehammer as a tactic.
bulwahn@40633
     6
*)
bulwahn@40633
     7
bulwahn@40633
     8
signature SLEDGEHAMMER_TACTICS =
bulwahn@40633
     9
sig
bulwahn@40633
    10
  val sledgehammer_with_metis_tac : Proof.context -> int -> tactic
blanchet@41357
    11
  val sledgehammer_as_unsound_oracle_tac : Proof.context -> int -> tactic
bulwahn@40633
    12
  val sledgehammer_as_oracle_tac : Proof.context -> int -> tactic
bulwahn@40633
    13
end;
bulwahn@40633
    14
bulwahn@40633
    15
structure Sledgehammer_Tactics : SLEDGEHAMMER_TACTICS =
bulwahn@40633
    16
struct
bulwahn@40633
    17
  
bulwahn@40633
    18
fun run_atp force_full_types timeout i n ctxt goal name =
bulwahn@40633
    19
  let
bulwahn@40921
    20
    val chained_ths = [] (* a tactic has no chained ths *)
blanchet@41138
    21
    val params as {type_sys, relevance_thresholds, max_relevant, ...} =
bulwahn@40633
    22
      ((if force_full_types then [("full_types", "true")] else [])
bulwahn@40921
    23
       @ [("timeout", Int.toString (Time.toSeconds timeout))])
blanchet@41357
    24
       (* @ [("overlord", "true")] *)
bulwahn@40635
    25
      |> Sledgehammer_Isar.default_params ctxt
blanchet@41087
    26
    val prover = Sledgehammer_Provers.get_prover ctxt false name
bulwahn@40921
    27
    val default_max_relevant =
blanchet@41087
    28
      Sledgehammer_Provers.default_max_relevant_for_prover ctxt name
bulwahn@40921
    29
    val is_built_in_const =
blanchet@41087
    30
      Sledgehammer_Provers.is_built_in_const_for_prover ctxt name
blanchet@41087
    31
    val relevance_fudge =
blanchet@41087
    32
      Sledgehammer_Provers.relevance_fudge_for_prover ctxt name
bulwahn@40921
    33
    val relevance_override = {add = [], del = [], only = false}
bulwahn@40921
    34
    val (_, hyp_ts, concl_t) = Sledgehammer_Util.strip_subgoal goal i
blanchet@41138
    35
    val no_dangerous_types =
blanchet@41138
    36
      Sledgehammer_ATP_Translate.types_dangerous_types type_sys
bulwahn@40921
    37
    val facts =
blanchet@41138
    38
      Sledgehammer_Filter.relevant_facts ctxt no_dangerous_types
blanchet@41138
    39
          relevance_thresholds
bulwahn@40921
    40
          (the_default default_max_relevant max_relevant) is_built_in_const
bulwahn@40921
    41
          relevance_fudge relevance_override chained_ths hyp_ts concl_t
bulwahn@40633
    42
    (* Check for constants other than the built-in HOL constants. If none of
bulwahn@40633
    43
       them appear (as should be the case for TPTP problems, unless "auto" or
bulwahn@40633
    44
       "simp" already did its "magic"), we can skip the relevance filter. *)
bulwahn@40633
    45
    val pure_goal =
bulwahn@40633
    46
      not (exists_Const (fn (s, _) => String.isSubstring "." s andalso
bulwahn@40633
    47
                                      not (String.isSubstring "HOL" s))
bulwahn@40633
    48
                        (prop_of goal))
bulwahn@40633
    49
    val problem =
blanchet@41090
    50
      {state = Proof.init ctxt, goal = goal, subgoal = i, subgoal_count = n,
blanchet@41243
    51
       facts = map Sledgehammer_Provers.Untranslated_Fact facts,
blanchet@41243
    52
       smt_head = NONE}
bulwahn@40633
    53
  in
bulwahn@40921
    54
    (case prover params (K "") problem of
bulwahn@40921
    55
      {outcome = NONE, used_facts, ...} => used_facts |> map fst |> SOME
bulwahn@40921
    56
    | _ => NONE)
bulwahn@40921
    57
      handle ERROR message => (warning ("Error: " ^ message ^ "\n"); NONE)
bulwahn@40633
    58
  end
bulwahn@40633
    59
bulwahn@40633
    60
fun to_period ("." :: _) = []
bulwahn@40633
    61
  | to_period ("" :: ss) = to_period ss
bulwahn@40633
    62
  | to_period (s :: ss) = s :: to_period ss
bulwahn@40633
    63
  | to_period [] = []
bulwahn@40633
    64
blanchet@41357
    65
val atp = "vampire" (* or "e" or "spass" etc. *)
bulwahn@40633
    66
bulwahn@40633
    67
fun thms_of_name ctxt name =
bulwahn@40633
    68
  let
bulwahn@40633
    69
    val lex = Keyword.get_lexicons
bulwahn@40633
    70
    val get = maps (ProofContext.get_fact ctxt o fst)
bulwahn@40633
    71
  in
bulwahn@40633
    72
    Source.of_string name
bulwahn@40635
    73
    |> Symbol.source
bulwahn@40633
    74
    |> Token.source {do_recover=SOME false} lex Position.start
bulwahn@40633
    75
    |> Token.source_proper
bulwahn@40633
    76
    |> Source.source Token.stopper (Parse_Spec.xthms1 >> get) NONE
bulwahn@40633
    77
    |> Source.exhaust
bulwahn@40633
    78
  end
bulwahn@40633
    79
bulwahn@40633
    80
fun sledgehammer_with_metis_tac ctxt i th =
bulwahn@40633
    81
  let
bulwahn@40921
    82
    val timeout = Time.fromSeconds 30
bulwahn@40921
    83
  in
bulwahn@40921
    84
    case run_atp false timeout i i ctxt th atp of
blanchet@41357
    85
      SOME facts =>
blanchet@41357
    86
      Metis_Tactics.metis_tac ctxt (maps (thms_of_name ctxt) facts) i th
bulwahn@40921
    87
    | NONE => Seq.empty
bulwahn@40921
    88
  end
bulwahn@40633
    89
blanchet@41357
    90
fun generic_sledgehammer_as_oracle_tac force_full_types ctxt i th =
bulwahn@40633
    91
  let
bulwahn@40633
    92
    val thy = ProofContext.theory_of ctxt
bulwahn@40921
    93
    val timeout = Time.fromSeconds 30
blanchet@41357
    94
    val xs = run_atp force_full_types timeout i i ctxt th atp
blanchet@41357
    95
  in if is_some xs then Skip_Proof.cheat_tac thy th else Seq.empty end
blanchet@41357
    96
blanchet@41357
    97
val sledgehammer_as_unsound_oracle_tac =
blanchet@41357
    98
  generic_sledgehammer_as_oracle_tac false
blanchet@41357
    99
val sledgehammer_as_oracle_tac = generic_sledgehammer_as_oracle_tac true
bulwahn@40633
   100
bulwahn@40633
   101
end;