Debian Codegraph @

proofs/proof_bullet.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Proof
12
13 type t =
14 | Dash of int
15 | Star of int
16 | Plus of int
17
18 let bullet_eq b1 b2 = match b1, b2 with
19 | Dash n1, Dash n2 -> n1 = n2
20 | Star n1, Star n2 -> n1 = n2
21 | Plus n1, Plus n2 -> n1 = n2
22 | _ -> false
23
24 let pr_bullet b =
25 match b with
26 | Dash n -> Pp.(str (String.make n '-'))
27 | Star n -> Pp.(str (String.make n '*'))
28 | Plus n -> Pp.(str (String.make n '+'))
29
30
31 type behavior = {
32 name : string;
33 put : Proof.t -> t -> Proof.t;
34 suggest: Proof.t -> Pp.t
35 }
36
37 let behaviors = Hashtbl.create 4
38 let register_behavior b = Hashtbl.add behaviors b.name b
39
40 (*** initial modes ***)
41 let none = {
42 name = "None";
43 put = (fun x _ -> x);
44 suggest = (fun _ -> Pp.mt ())
45 }
46 let _ = register_behavior none
47
48 module Strict = struct
49 type suggestion =
50 | Suggest of t (* this bullet is mandatory here *)
51 | Unfinished of t (* no mandatory bullet here, but this bullet is unfinished *)
52 | NoBulletInUse (* No mandatory bullet (or brace) here, no bullet pending,
53 some focused goals exists. *)
54 | NeedClosingBrace (* Some unfocussed goal exists "{" needed to focus them *)
55 | ProofFinished (* No more goal anywhere *)
56
57 (* give a message only if more informative than the standard rocq message *)
58 let suggest_on_solved_goal sugg =
59 match sugg with
60 | NeedClosingBrace -> Pp.(str"Try unfocusing with \"}\".")
61 | NoBulletInUse -> Pp.mt ()
62 | ProofFinished -> Pp.mt ()
63 | Suggest b -> Pp.(str"Focus next goal with bullet " ++ pr_bullet b ++ str".")
64 | Unfinished b -> Pp.(str"The current bullet " ++ pr_bullet b ++ str" is unfinished.")
65
66 (* give always a message. *)
67 let suggest_on_error sugg =
68 match sugg with
69 | NeedClosingBrace -> Pp.(str"Try unfocusing with \"}\".")
70 | NoBulletInUse -> assert false (* This should never raise an error. *)
71 | ProofFinished -> Pp.(str"No more goals.")
72 | Suggest b -> Pp.(str"Expecting " ++ pr_bullet b ++ str".")
73 | Unfinished b -> Pp.(str"Current bullet " ++ pr_bullet b ++ str" is not finished.")
74
75 exception FailedBullet of t * suggestion
76
77 let _ =
78 CErrors.register_handler
79 (function
80 | FailedBullet (b,sugg) ->
81 let prefix = Pp.(str"Wrong bullet " ++ pr_bullet b ++ str": ") in
82 Some Pp.(str "[Focus]" ++ spc () ++ prefix ++ suggest_on_error sugg)
83 | _ -> None)
84
85
86 (* spiwack: we need only one focus kind as we keep a stack of (distinct!) bullets *)
87 let bullet_kind = (new_focus_kind "bullet_kind" : t list focus_kind)
88 let bullet_cond = done_cond ~loose_end:true bullet_kind
89
90 (* spiwack: as it is bullets are reset (locally) by *any* non-bullet focusing command
91 experience will tell if this is the right discipline of if we want to be finer and
92 reset them only for a choice of bullets. *)
93 let get_bullets pr =
94 if is_last_focus bullet_kind pr then
95 get_at_focus bullet_kind pr
96 else
97 []
98
99 let has_bullet bul pr =
100 let rec has_bullet = function
101 | b'::_ when bullet_eq bul b' -> true
102 | _::l -> has_bullet l
103 | [] -> false
104 in
105 has_bullet (get_bullets pr)
106
107 (* pop a bullet from proof [pr]. There should be at least one
108 bullet in use. If pop impossible (pending proofs on this level
109 of bullet or higher) then raise [Proof.CannotUnfocusThisWay]. *)
110 let pop pr =
111 match get_bullets pr with
112 | b::_ -> Some (unfocus bullet_kind pr (), b)
113 | _ -> None
114
115 let push (b:t) pr =
116 focus bullet_cond (b::get_bullets pr) 1 pr
117
118 let suggest_bullet (prf : Proof.t): suggestion =
119 if is_done prf then ProofFinished
120 else if not (no_focused_goal prf)
121 then (* No suggestion if a bullet is not mandatory, look for an unfinished bullet *)
122 match get_bullets prf with
123 | b::_ -> Unfinished b
124 | _ -> NoBulletInUse
125 else (* There is no goal under focus but some are unfocussed,
126 let us look at the bullet needed. *)
127 let rec loop prf =
128 match pop prf with
129 | Some (prf, b) ->
130 (* pop went well, this means that there are no more goals
131 *under this* bullet b, see if a new b can be pushed. *)
132 begin
133 try ignore (push b prf); Suggest b
134 with e when CErrors.noncritical e ->
135 (* b could not be pushed, so we must look for a outer bullet *)
136 loop prf
137 end
138 | None ->
139 (* No pop was possible, but there are still
140 subgoals somewhere: there must be a "}" to use. *)
141 NeedClosingBrace
142 in
143 loop prf
144
145 let rec pop_until (prf : Proof.t) bul : Proof.t =
146 let prf', b = Option.get (pop prf) in
147 if bullet_eq bul b then prf'
148 else pop_until prf' bul
149
150 let put p bul =
151 try
152 if not (has_bullet bul p) then
153 (* bullet is not in use, so pushing it is always ok unless
154 no goal under focus. *)
155 push bul p
156 else
157 match suggest_bullet p with
158 | Suggest suggested_bullet when bullet_eq bul suggested_bullet
159 -> (* suggested_bullet is mandatory and you gave the right one *)
160 let p' = pop_until p bul in
161 push bul p'
162 (* the bullet you gave is in use but not the right one *)
163 | sugg -> raise (FailedBullet (bul,sugg))
164 with NoSuchGoals _ -> (* push went bad *)
165 raise (FailedBullet (bul,suggest_bullet p))
166
167 let strict = {
168 name = "Strict Subproofs";
169 put = put;
170 suggest = (fun prf -> suggest_on_solved_goal (suggest_bullet prf))
171
172 }
173 let _ = register_behavior strict
174 end
175
176 (* Current bullet behavior, controlled by the option *)
177 let { Goptions.get = current_behavior } =
178 Goptions.declare_interpreted_string_option_and_ref
179 ~key:["Bullet";"Behavior"]
180 ~value:Strict.strict
181 (fun n ->
182 try Hashtbl.find behaviors n
183 with Not_found ->
184 CErrors.user_err Pp.(str ("Unknown bullet behavior: \"" ^ n ^ "\".")))
185 (fun v -> v.name)
186 ()
187
188 let put p b =
189 (current_behavior ()).put p b
190
191 let suggest p =
192 (current_behavior ()).suggest p
193
194 let _ = CErrors.register_handler begin function
195 | Proof.SuggestNoSuchGoals(n,proof) ->
196 let suffix = suggest proof in
197 Some (Pp.(str "No such " ++ str (CString.plural n "goal") ++ str "." ++
198 pr_non_empty_arg (fun x -> x) suffix))
199 | _ -> None
200 end