src/HOL/Tools/try0_util.ML
author desharna
Thu, 27 Mar 2025 13:40:33 +0100
changeset 82361 0b5f1364606c
parent 82360 6a09257afd06
child 82363 3a7fc54b50ca
permissions -rw-r--r--
tuned signature
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
82360
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     1
(* Title:      HOL/Tools/try0_hol.ML
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     2
   Author:     Jasmin Blanchette, LMU Muenchen
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     3
   Author:     Martin Desharnais, LMU Muenchen
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     4
   Author:     Fabian Huch, TU Muenchen
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     5
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     6
General-purpose functions used by Try0.
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     7
*)
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     8
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
     9
signature TRY0_UTIL =
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    10
sig
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    11
  val silence_methods : bool -> Proof.context -> Proof.context
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    12
  val string_of_xref : Try0.xref -> string
82361
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    13
  val apply_raw_named_method : string -> bool -> (Try0.modifier * string) list ->
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    14
    Time.time option -> Try0.tagged_xref list -> Proof.state -> Try0.result option
82360
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    15
end
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    16
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    17
structure Try0_Util : TRY0_UTIL = struct
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    18
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    19
(* Makes reconstructor tools as silent as possible. The "set_visible" calls suppresses "Unification
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    20
   bound exceeded" warnings and the like. *)
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    21
fun silence_methods debug =
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    22
  Config.put Metis_Tactic.verbose debug
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    23
  #> not debug ? (fn ctxt =>
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    24
      ctxt
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    25
      |> Simplifier_Trace.disable
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    26
      |> Context_Position.set_visible false
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    27
      |> Config.put Unify.unify_trace false
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    28
      |> Config.put Argo_Tactic.trace "none")
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    29
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    30
fun string_of_xref ((xref, args) : Try0.xref) =
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    31
  (case xref of
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    32
    Facts.Fact literal => literal |> Symbol_Pos.explode0 |> Symbol_Pos.implode |> cartouche
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    33
  | _ =>
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    34
      Facts.string_of_ref xref) ^ implode
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    35
        (map (enclose "[" "]" o Pretty.unformatted_string_of o Token.pretty_src \<^context>) args)
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    36
82361
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    37
local
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    38
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    39
fun add_attr_text (tagged : Try0.tagged_xref list) (tag, src) s =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    40
  let
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    41
    val fs = tagged
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    42
      |> map_filter (fn (xref, tags) =>
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    43
        if member (op =) tags tag then
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    44
          SOME (string_of_xref xref)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    45
        else
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    46
          NONE)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    47
  in if null fs then s else s ^ " " ^ (if src = "" then "" else src ^ ": ") ^ implode_space fs end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    48
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    49
fun attrs_text tags (tagged : Try0.tagged_xref list) =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    50
  "" |> fold (add_attr_text tagged) tags
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    51
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    52
fun parse_method ctxt s =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    53
  enclose "(" ")" s
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    54
  |> Token.explode (Thy_Header.get_keywords' ctxt) Position.start
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    55
  |> filter Token.is_proper
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    56
  |> Scan.read Token.stopper Method.parse
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    57
  |> (fn SOME (Method.Source src, _) => src | _ => raise Fail "expected Source")
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    58
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    59
fun run_tac timeout_opt tac st =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    60
  let val with_timeout =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    61
    (case timeout_opt of SOME timeout => Timeout.apply_physical timeout | NONE => I)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    62
  in with_timeout (Seq.pull o tac) st |> Option.map fst end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    63
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    64
val num_goals = Thm.nprems_of o #goal o Proof.goal
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    65
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    66
fun apply_recursive recurse elapsed0 timeout_opt apply st =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    67
  (case Timing.timing (Option.join o try (run_tac timeout_opt apply)) st of
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    68
    ({elapsed, ...}, SOME st') =>
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    69
      if recurse andalso num_goals st' > 0 andalso num_goals st' < num_goals st then
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    70
        let val timeout_opt1 = (Option.map (fn timeout => timeout - elapsed) timeout_opt)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    71
        in apply_recursive recurse (elapsed0 + elapsed) timeout_opt1 apply st' end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    72
      else (elapsed0 + elapsed, st')
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    73
   |_ => (elapsed0, st))
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    74
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    75
in
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    76
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    77
fun apply_raw_named_method (name : string) all_goals attrs timeout_opt
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    78
  (tagged : Try0.tagged_xref list) (st : Proof.state) : Try0.result option =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    79
  let
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    80
    val st = Proof.map_contexts (silence_methods false) st
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    81
    val unused =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    82
      tagged
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    83
      |> filter
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    84
        (fn (_, tags) => not (null tags) andalso null (inter (op =) tags (attrs |> map fst)))
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    85
      |> map fst
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    86
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    87
    val attrs = attrs_text attrs tagged
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    88
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    89
    val ctxt = Proof.context_of st
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    90
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    91
    val text =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    92
      name ^ attrs
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    93
      |> parse_method ctxt
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    94
      |> Method.method_cmd ctxt
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    95
      |> Method.Basic
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    96
      |> (fn m => Method.Combinator (Method.no_combinator_info, Method.Select_Goals 1, [m]))
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    97
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    98
    val apply =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    99
      Proof.using [Attrib.eval_thms ctxt unused |> map (rpair [] o single)]
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   100
      #> Proof.refine text #> Seq.filter_results
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   101
    val num_before = num_goals st
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   102
    val multiple_goals = all_goals andalso num_before > 1
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   103
    val (time, st') = apply_recursive multiple_goals Time.zeroTime timeout_opt apply st
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   104
    val num_after = num_goals st'
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   105
    val select = "[" ^ string_of_int (num_before - num_after)  ^ "]"
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   106
    val unused = implode_space (unused |> map string_of_xref)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   107
    val command =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   108
      (if unused <> "" then "using " ^ unused ^ " " else "") ^
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   109
      (if num_after = 0 then "by " else "apply ") ^
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   110
      (name ^ attrs |> attrs <> "" ? enclose "(" ")") ^
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   111
      (if multiple_goals andalso num_after > 0 then select else "")
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   112
  in
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   113
    if num_before > num_after then
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   114
      SOME {name = name, command = command, time = time, state = st'}
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   115
    else NONE
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   116
  end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   117
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   118
end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   119
82360
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
   120
end