src/Tools/try.ML
author wenzelm
Fri Oct 31 11:18:17 2014 +0100 (2014-10-31)
changeset 58842 22b87ab47d3b
parent 56467 8d7d6f17c6a7
child 58843 521cea5fa777
permissions -rw-r--r--
discontinued Proof General;
blanchet@43018
     1
(*  Title:      Tools/try.ML
blanchet@33561
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@33561
     3
blanchet@43018
     4
Manager for tools that should be tried on conjectures.
blanchet@33561
     5
*)
blanchet@33561
     6
blanchet@43018
     7
signature TRY =
blanchet@33561
     8
sig
blanchet@43020
     9
  type tool =
wenzelm@52639
    10
    string * (int * string * (bool -> Proof.state -> bool * (string * Proof.state)))
blanchet@43020
    11
blanchet@43020
    12
  val tryN : string
blanchet@33561
    13
blanchet@43028
    14
  val serial_commas : string -> string list -> string list
blanchet@43028
    15
  val subgoal_count : Proof.state -> int
blanchet@43020
    16
  val get_tools : theory -> tool list
blanchet@43020
    17
  val try_tools : Proof.state -> (string * string) option
wenzelm@52641
    18
  val tool_setup : tool -> unit
blanchet@33561
    19
end;
blanchet@33561
    20
blanchet@43018
    21
structure Try : TRY =
blanchet@33561
    22
struct
blanchet@33561
    23
blanchet@43020
    24
type tool =
wenzelm@52639
    25
  string * (int * string * (bool -> Proof.state -> bool * (string * Proof.state)))
blanchet@43020
    26
blanchet@43020
    27
val tryN = "try"
blanchet@43020
    28
blanchet@43020
    29
blanchet@43028
    30
(* helpers *)
blanchet@43028
    31
blanchet@43028
    32
fun serial_commas _ [] = ["??"]
blanchet@43028
    33
  | serial_commas _ [s] = [s]
blanchet@43028
    34
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@43028
    35
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@43028
    36
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@43028
    37
blanchet@43028
    38
val subgoal_count = Logic.count_prems o prop_of o #goal o Proof.goal
blanchet@43028
    39
blanchet@43028
    40
blanchet@33561
    41
(* configuration *)
blanchet@33561
    42
blanchet@43061
    43
fun tool_ord ((name1, (weight1, _, _)), (name2, (weight2, _, _))) =
blanchet@43061
    44
  prod_ord int_ord string_ord ((weight1, name1), (weight2, name2))
blanchet@43028
    45
wenzelm@33600
    46
structure Data = Theory_Data
wenzelm@33600
    47
(
blanchet@43020
    48
  type T = tool list
blanchet@33561
    49
  val empty = []
blanchet@33561
    50
  val extend = I
blanchet@43028
    51
  fun merge data : T = Ord_List.merge tool_ord data
blanchet@33561
    52
)
blanchet@33561
    53
blanchet@43020
    54
val get_tools = Data.get
blanchet@43020
    55
blanchet@43028
    56
val register_tool = Data.map o Ord_List.insert tool_ord
blanchet@33561
    57
wenzelm@52641
    58
blanchet@43020
    59
(* try command *)
blanchet@43020
    60
blanchet@43020
    61
fun try_tools state =
blanchet@43028
    62
  if subgoal_count state = 0 then
blanchet@43028
    63
    (Output.urgent_message "No subgoal!"; NONE)
blanchet@43028
    64
  else
blanchet@43028
    65
    get_tools (Proof.theory_of state)
blanchet@43028
    66
    |> tap (fn tools =>
blanchet@43028
    67
               "Trying " ^ space_implode " "
blanchet@43028
    68
                    (serial_commas "and" (map (quote o fst) tools)) ^ "..."
blanchet@43028
    69
               |> Output.urgent_message)
blanchet@43028
    70
    |> Par_List.get_some
blanchet@43028
    71
           (fn (name, (_, _, tool)) =>
blanchet@43028
    72
               case try (tool false) state of
blanchet@43028
    73
                 SOME (true, (outcome_code, _)) => SOME (name, outcome_code)
blanchet@43028
    74
               | _ => NONE)
blanchet@43028
    75
    |> tap (fn NONE => Output.urgent_message "Tried in vain." | _ => ())
blanchet@43020
    76
blanchet@43020
    77
val _ =
wenzelm@46961
    78
  Outer_Syntax.improper_command @{command_spec "try"}
wenzelm@46961
    79
    "try a combination of automatic proving and disproving tools"
wenzelm@51557
    80
    (Scan.succeed (Toplevel.unknown_proof o Toplevel.keep (ignore o try_tools o Toplevel.proof_of)))
blanchet@43020
    81
blanchet@33561
    82
wenzelm@52641
    83
(* automatic try (TTY) *)
blanchet@33561
    84
blanchet@43020
    85
fun auto_try state =
blanchet@43020
    86
  get_tools (Proof.theory_of state)
wenzelm@52639
    87
  |> map_filter (fn (_, (_, auto, tool)) => if Options.default_bool auto then SOME tool else NONE)
blanchet@43020
    88
  |> Par_List.get_some (fn tool =>
blanchet@43020
    89
                           case try (tool true) state of
blanchet@43020
    90
                             SOME (true, (_, state)) => SOME state
blanchet@43020
    91
                           | _ => NONE)
blanchet@43020
    92
  |> the_default state
blanchet@40931
    93
blanchet@33561
    94
val _ = Context.>> (Specification.add_theorem_hook (fn interact => fn state =>
wenzelm@52639
    95
  let
wenzelm@56467
    96
    val auto_time_limit = Options.default_real @{system_option auto_time_limit}
wenzelm@52639
    97
  in
wenzelm@52639
    98
    if interact andalso not (!Toplevel.quiet) andalso auto_time_limit > 0.0 then
wenzelm@52639
    99
      TimeLimit.timeLimit (seconds auto_time_limit) auto_try state
wenzelm@52639
   100
      handle TimeLimit.TimeOut => state
wenzelm@52639
   101
    else
wenzelm@52639
   102
      state
wenzelm@52639
   103
  end))
blanchet@33561
   104
wenzelm@52641
   105
wenzelm@52641
   106
(* asynchronous print function (PIDE) *)
wenzelm@52641
   107
wenzelm@52641
   108
fun print_function ((name, (weight, auto, tool)): tool) =
wenzelm@53839
   109
  Command.print_function ("auto_" ^ name)
wenzelm@52850
   110
    (fn {command_name, ...} =>
wenzelm@52641
   111
      if Keyword.is_theory_goal command_name andalso Options.default_bool auto then
wenzelm@52651
   112
        SOME
wenzelm@56467
   113
         {delay = SOME (seconds (Options.default_real @{system_option auto_time_start})),
wenzelm@52651
   114
          pri = ~ weight,
wenzelm@52651
   115
          persistent = true,
wenzelm@52953
   116
          strict = true,
wenzelm@52651
   117
          print_fn = fn _ => fn st =>
wenzelm@52651
   118
            let
wenzelm@52651
   119
              val state = Toplevel.proof_of st
wenzelm@52652
   120
                |> Proof.map_context (Context_Position.set_visible false)
wenzelm@56467
   121
              val auto_time_limit = Options.default_real @{system_option auto_time_limit}
wenzelm@52651
   122
            in
wenzelm@52651
   123
              if auto_time_limit > 0.0 then
wenzelm@52651
   124
                (case TimeLimit.timeLimit (seconds auto_time_limit) (fn () => tool true state) () of
wenzelm@52651
   125
                  (true, (_, state')) =>
wenzelm@52651
   126
                    List.app Pretty.writeln (Proof.pretty_goal_messages state')
wenzelm@52651
   127
                | _ => ())
wenzelm@52651
   128
              else ()
wenzelm@52651
   129
            end handle exn => if Exn.is_interrupt exn then reraise exn else ()}
wenzelm@52648
   130
      else NONE)
wenzelm@52641
   131
wenzelm@52641
   132
wenzelm@52641
   133
(* hybrid tool setup *)
wenzelm@52641
   134
wenzelm@53171
   135
fun tool_setup tool = (Theory.setup (register_tool tool); print_function tool)
wenzelm@52641
   136
blanchet@33561
   137
end;