src/HOL/Tools/try0_util.ML
author nipkow
Tue, 17 Jun 2025 14:11:40 +0200
changeset 82733 8b537e1af2ec
parent 82578 cf21066637d7
permissions -rw-r--r--
reinstated intersection of lists as inter_list_set
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 string_of_xref : Try0.xref -> string
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
    12
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    13
  type facts_config =
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    14
    {usings_as_params : bool,
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    15
     simps_prefix : string option,
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    16
     intros_prefix : string option,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    17
     elims_prefix : string option,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    18
     dests_prefix : string option}
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    19
  val full_attrs : facts_config
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    20
  val clas_attrs : facts_config
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    21
  val simp_attrs : facts_config
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    22
  val metis_attrs : facts_config
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    23
  val no_attrs : facts_config
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    24
  val apply_raw_named_method : string -> bool -> facts_config ->
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
    25
    (Proof.context -> Proof.context) -> Time.time option -> Try0.facts -> Proof.state ->
82363
3a7fc54b50ca tuned and moved configuration of auto_try0 to theory HOL
desharna
parents: 82361
diff changeset
    26
    Try0.result option
82360
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    27
end
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    28
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    29
structure Try0_Util : TRY0_UTIL = struct
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    30
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    31
fun string_of_xref ((xref, args) : Try0.xref) =
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    32
  (case xref of
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    33
    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
    34
  | _ =>
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    35
      Facts.string_of_ref xref) ^ implode
6a09257afd06 moved try0's HOL-specific stuff into own theory
desharna
parents:
diff changeset
    36
        (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
    37
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    38
type facts_config =
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    39
  {usings_as_params : bool,
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    40
   simps_prefix : string option,
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    41
   intros_prefix : string option,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    42
   elims_prefix : string option,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    43
   dests_prefix : string option}
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
    44
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    45
val no_attrs : facts_config =
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    46
  {usings_as_params = false,
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    47
   simps_prefix = NONE,
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    48
   intros_prefix = NONE,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    49
   elims_prefix = NONE,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    50
   dests_prefix = NONE}
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    51
val full_attrs : facts_config =
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    52
  {usings_as_params = false,
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    53
   simps_prefix = SOME "simp: ",
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    54
   intros_prefix = SOME "intro: ",
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    55
   elims_prefix = SOME "elim: ",
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    56
   dests_prefix = SOME "dest: "}
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    57
val clas_attrs : facts_config =
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    58
  {usings_as_params = false,
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    59
   simps_prefix = NONE,
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    60
   intros_prefix = SOME "intro: ",
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    61
   elims_prefix = SOME "elim: ",
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    62
   dests_prefix = SOME "dest: "}
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    63
val simp_attrs : facts_config =
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    64
  {usings_as_params = false,
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    65
   simps_prefix = SOME "add: ",
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    66
   intros_prefix = NONE,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    67
   elims_prefix = NONE,
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    68
   dests_prefix = NONE}
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    69
val metis_attrs : facts_config =
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    70
  {usings_as_params = true,
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
    71
   simps_prefix = SOME "",
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    72
   intros_prefix = SOME "",
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    73
   elims_prefix = SOME "",
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
    74
   dests_prefix = SOME ""}
82363
3a7fc54b50ca tuned and moved configuration of auto_try0 to theory HOL
desharna
parents: 82361
diff changeset
    75
82361
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    76
local
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    77
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    78
fun parse_method ctxt s =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    79
  enclose "(" ")" s
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    80
  |> Token.explode (Thy_Header.get_keywords' ctxt) Position.start
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    81
  |> filter Token.is_proper
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    82
  |> Scan.read Token.stopper Method.parse
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    83
  |> (fn SOME (Method.Source src, _) => src | _ => raise Fail "expected Source")
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    84
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    85
fun run_tac timeout_opt tac st =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    86
  let val with_timeout =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    87
    (case timeout_opt of SOME timeout => Timeout.apply_physical timeout | NONE => I)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    88
  in with_timeout (Seq.pull o tac) st |> Option.map fst end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    89
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    90
val num_goals = Thm.nprems_of o #goal o Proof.goal
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    91
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    92
fun apply_recursive recurse elapsed0 timeout_opt apply st =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    93
  (case Timing.timing (Option.join o try (run_tac timeout_opt apply)) st of
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    94
    ({elapsed, ...}, SOME st') =>
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    95
      if recurse andalso num_goals st' > 0 andalso num_goals st' < num_goals st then
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    96
        let val timeout_opt1 = (Option.map (fn timeout => timeout - elapsed) timeout_opt)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    97
        in apply_recursive recurse (elapsed0 + elapsed) timeout_opt1 apply st' end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    98
      else (elapsed0 + elapsed, st')
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
    99
   |_ => (elapsed0, st))
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   100
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   101
in
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   102
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
   103
fun apply_raw_named_method (name : string) all_goals (facts_config: facts_config)
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   104
  (silence_methods : Proof.context -> Proof.context) timeout_opt (facts : Try0.facts)
82363
3a7fc54b50ca tuned and moved configuration of auto_try0 to theory HOL
desharna
parents: 82361
diff changeset
   105
  (st : Proof.state) : Try0.result option =
82361
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   106
  let
82363
3a7fc54b50ca tuned and moved configuration of auto_try0 to theory HOL
desharna
parents: 82361
diff changeset
   107
    val st = Proof.map_contexts silence_methods st
82361
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   108
    val ctxt = Proof.context_of st
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   109
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   110
    val (unused_simps, simps_attrs) =
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   111
       if null (#simps facts) then
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   112
        ([], "")
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   113
      else
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
   114
        (case #simps_prefix facts_config of
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   115
          NONE =>  (#simps facts, "")
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   116
        | SOME prefix => ([], " " ^ prefix ^ implode_space (map string_of_xref (#simps facts))))
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   117
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   118
    val (unused_intros, intros_attrs) =
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   119
      if null (#intros facts) then
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   120
        ([], "")
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   121
      else
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
   122
        (case #intros_prefix facts_config of
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   123
          NONE =>  (#intros facts, "")
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   124
        | SOME prefix => ([], " " ^ prefix ^ implode_space (map string_of_xref (#intros facts))))
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   125
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   126
    val (unused_elims, elims_attrs) =
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   127
      if null (#elims facts) then
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   128
        ([], "")
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   129
      else
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
   130
        (case #elims_prefix facts_config of
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   131
          NONE =>  (#elims facts, "")
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   132
        | SOME prefix => ([], " " ^ prefix ^ implode_space (map string_of_xref (#elims facts))))
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   133
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   134
    val (unused_dests, dests_attrs) =
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   135
      if null (#dests facts) then
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   136
        ([], "")
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   137
      else
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
   138
        (case #dests_prefix facts_config of
82368
ef3ec45ded4d tuned stringification of proof method in try0
desharna
parents: 82364
diff changeset
   139
          NONE =>  (#dests facts, "")
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   140
        | SOME prefix => ([], " " ^ prefix ^ implode_space (map string_of_xref (#dests facts))))
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   141
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   142
    val (unused_usings, using_attrs) =
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   143
      if null (#usings facts) then
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   144
        ([], "")
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   145
      else if #usings_as_params facts_config then
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   146
        ([], " " ^ implode_space (map string_of_xref (#usings facts)))
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   147
      else
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   148
        (#usings facts, "")
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   149
82578
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   150
    val unused = unused_simps @ unused_intros @ unused_elims @ unused_dests @ unused_usings
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   151
cf21066637d7 pass "using" facts as parameters to metis in try0
desharna
parents: 82577
diff changeset
   152
    val attrs = simps_attrs ^ intros_attrs ^ elims_attrs ^ dests_attrs ^ using_attrs
82361
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   153
    val text =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   154
      name ^ attrs
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   155
      |> parse_method ctxt
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   156
      |> Method.method_cmd ctxt
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   157
      |> Method.Basic
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   158
      |> (fn m => Method.Combinator (Method.no_combinator_info, Method.Select_Goals 1, [m]))
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   159
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   160
    val apply =
82364
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   161
        Proof.using [Attrib.eval_thms ctxt unused |> map (rpair [] o single)]
5af097d05e99 tuned signature
desharna
parents: 82363
diff changeset
   162
        #> Proof.refine text #> Seq.filter_results
82361
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   163
    val num_before = num_goals st
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   164
    val multiple_goals = all_goals andalso num_before > 1
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   165
    val (time, st') = apply_recursive multiple_goals Time.zeroTime timeout_opt apply st
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   166
    val num_after = num_goals st'
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   167
    val select = "[" ^ string_of_int (num_before - num_after)  ^ "]"
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   168
    val unused = implode_space (unused |> map string_of_xref)
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   169
    val command =
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   170
      (if unused <> "" then "using " ^ unused ^ " " else "") ^
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   171
      (if num_after = 0 then "by " else "apply ") ^
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   172
      (name ^ attrs |> attrs <> "" ? enclose "(" ")") ^
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   173
      (if multiple_goals andalso num_after > 0 then select else "")
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   174
  in
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   175
    if num_before > num_after then
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   176
      SOME {name = name, command = command, time = time, state = st'}
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   177
    else NONE
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   178
  end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   179
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   180
end
0b5f1364606c tuned signature
desharna
parents: 82360
diff changeset
   181
82577
f3b3d49d84d7 clarified signature
desharna
parents: 82368
diff changeset
   182
end