src/HOL/Mirabelle/Tools/mirabelle_sledgehammer.ML
author boehmes
Wed, 02 Sep 2009 16:23:53 +0200
changeset 32496 4ab00a2642c3
parent 32472 src/HOL/Tools/Mirabelle/Tools/mirabelle_sledgehammer.ML@7b92a8b8daaf
child 32498 1132c7c13f36
permissions -rw-r--r--
moved Mirabelle from HOL/Tools to HOL, added session HOL-Mirabelle
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     1
(* Title:  mirabelle_sledgehammer.ML
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     2
   Author: Jasmin Blanchette and Sascha Boehme
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     3
*)
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     4
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     5
structure Mirabelle_Sledgehammer : MIRABELLE_ACTION =
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     6
struct
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     7
32454
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
     8
fun thms_of_name ctxt name =
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
     9
  let
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    10
    val lex = OuterKeyword.get_lexicons
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    11
    val get = maps (ProofContext.get_fact ctxt o fst)
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    12
  in
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    13
    Source.of_string name
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    14
    |> Symbol.source {do_recover=false}
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    15
    |> OuterLex.source {do_recover=SOME false} lex Position.start
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    16
    |> OuterLex.source_proper
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    17
    |> Source.source OuterLex.stopper (SpecParse.xthms1 >> get) NONE
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    18
    |> Source.exhaust
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    19
  end
32452
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
    20
32455
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    21
fun safe init done f x =
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    22
  let
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    23
    val y = init x
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    24
    val z = Exn.capture f y
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    25
    val _ = done y
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    26
  in Exn.release z end
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    27
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    28
val proverK = "prover"
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    29
val keepK = "keep"
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    30
val metisK = "metis"
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    31
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    32
fun sledgehammer_action args {pre=st, timeout, log, ...} =
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    33
  let
32434
6b93b73a712b selectable prover for sledgehammer, proper environment lookup
boehmes
parents: 32385
diff changeset
    34
    val prover_name =
32455
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    35
      AList.lookup (op =) args proverK
32434
6b93b73a712b selectable prover for sledgehammer, proper environment lookup
boehmes
parents: 32385
diff changeset
    36
      |> the_default (hd (space_explode " " (AtpManager.get_atps ())))
6b93b73a712b selectable prover for sledgehammer, proper environment lookup
boehmes
parents: 32385
diff changeset
    37
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    38
    val thy = Proof.theory_of st
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    39
 
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    40
    val prover = the (AtpManager.get_prover prover_name thy)
32469
1ad7d4fc0954 Mirabelle: added preliminary documentation,
boehmes
parents: 32460
diff changeset
    41
    val atp_timeout = AtpManager.get_timeout () 
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    42
32452
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
    43
    (* run sledgehammer *)
32455
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    44
    fun init NONE = !AtpWrapper.destdir
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    45
      | init (SOME path) =
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    46
          let
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    47
            (* Warning: we implicitly assume single-threaded execution here! *)
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    48
            val old = !AtpWrapper.destdir
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    49
            val _ = AtpWrapper.destdir := path
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    50
          in old end
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    51
    fun done path = AtpWrapper.destdir := path
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    52
    fun sh _ =
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    53
      let
32469
1ad7d4fc0954 Mirabelle: added preliminary documentation,
boehmes
parents: 32460
diff changeset
    54
        val atp = prover atp_timeout NONE NONE prover_name 1
32452
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
    55
        val (success, (message, thm_names), _, _, _) =
32469
1ad7d4fc0954 Mirabelle: added preliminary documentation,
boehmes
parents: 32460
diff changeset
    56
          TimeLimit.timeLimit timeout atp (Proof.get_goal st)
32452
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
    57
      in (success, message, SOME thm_names) end
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
    58
      handle ResHolClause.TOO_TRIVIAL => (true, "trivial", SOME [])
32455
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    59
    val (success, message, thm_names) = safe init done sh
c71414177792 Mirabelle sledgehammer: added option to keep problem files, enabled "metis" switch again (was accidentally removed)
boehmes
parents: 32454
diff changeset
    60
      (AList.lookup (op =) args keepK)
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    61
    val _ =
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    62
      if success then log (quote prover_name ^ " succeeded:\n" ^ message)
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    63
      else log (prover_name ^ " failed")
32452
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
    64
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
    65
    (* try metis *)
32454
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    66
    fun get_thms ctxt = maps (thms_of_name ctxt)
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
    67
    fun metis thms ctxt = MetisTools.metis_tac ctxt thms
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    68
    fun log_metis s =
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    69
      log ("applying metis: " ^ s)
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    70
    fun apply_metis thms =
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    71
      if Mirabelle.can_apply timeout (metis thms) st
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    72
      then "succeeded" else "failed"
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    73
    val _ =
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    74
      if not (AList.defined (op =) args metisK) then ()
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    75
      else
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    76
        these thm_names
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    77
        |> get_thms (Proof.context_of st)
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    78
        |> apply_metis
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    79
        |> log_metis
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
    80
  in () end
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    81
32434
6b93b73a712b selectable prover for sledgehammer, proper environment lookup
boehmes
parents: 32385
diff changeset
    82
fun invoke args = Mirabelle.register ("sledgehammer", sledgehammer_action args)
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    83
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
    84
end