Debian Codegraph @

stm/proofBlockDelimiter.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 Stm
12
13 module Util : sig
14
15 val simple_goal : Evd.evar_map -> Evar.t -> Evar.t list -> bool
16 val is_focused_goal_simple : doc:Stm.doc -> Stateid.t -> [ `Simple of Evar.t list | `Not ]
17
18 type 'a until = [ `Stop | `Found of static_block_declaration | `Cont of 'a ]
19
20 val crawl :
21 document_view -> ?init:document_node option ->
22 ('a -> document_node -> 'a until) -> 'a ->
23 static_block_declaration option
24
25 val unit_val : Stm.DynBlockData.t
26 val of_bullet_val : Proof_bullet.t -> Stm.DynBlockData.t
27 val to_bullet_val : Stm.DynBlockData.t -> Proof_bullet.t
28 val of_vernac_control_val : Vernacexpr.vernac_control -> Stm.DynBlockData.t
29 val to_vernac_control_val : Stm.DynBlockData.t -> Vernacexpr.vernac_control
30
31 end = struct
32
33 let unit_tag = DynBlockData.create "unit"
34 let unit_val = DynBlockData.Easy.inj () unit_tag
35
36 let of_bullet_val, to_bullet_val = DynBlockData.Easy.make_dyn "bullet"
37 let of_vernac_control_val, to_vernac_control_val = DynBlockData.Easy.make_dyn "vernac_control"
38
39 let simple_goal sigma g gs =
40 let open Evar in
41 let open Evd in
42 let open Evarutil in
43 let () = assert (not @@ Evd.is_defined sigma g) in
44 let evi = Evd.find_undefined sigma g in
45 Set.is_empty (evars_of_term sigma (Evd.evar_concl evi)) &&
46 Set.is_empty (evars_of_filtered_evar_info sigma (nf_evar_info sigma evi)) &&
47 not (List.exists (Proofview.depends_on sigma g) gs)
48
49 let is_focused_goal_simple ~doc id =
50 match state_of_id ~doc id with
51 | Expired | Error _ | Valid None -> `Not
52 | Valid (Some { interp = { Vernacstate.Interp.lemmas } }) ->
53 Option.cata (Vernacstate.LemmaStack.with_top ~f:(fun proof ->
54 let proof = Declare.Proof.get proof in
55 let Proof.{ goals=focused; stack=r1; sigma } = Proof.data proof in
56 let rest = List.(flatten (map (fun (x,y) -> x @ y) r1)) @ (Evd.shelf sigma) @ (Evar.Set.elements @@ Evd.given_up sigma) in
57 if List.for_all (fun x -> simple_goal sigma x rest) focused
58 then `Simple focused
59 else `Not)) `Not lemmas
60
61 type 'a until = [ `Stop | `Found of static_block_declaration | `Cont of 'a ]
62
63 let crawl { entry_point; prev_node } ?(init=Some entry_point) f acc =
64 let rec crawl node acc =
65 match node with
66 | None -> None
67 | Some node ->
68 match f acc node with
69 | `Stop -> None
70 | `Found x -> Some x
71 | `Cont acc -> crawl (prev_node node) acc in
72 crawl init acc
73
74 end
75
76 include Util
77
78 (* ****************** - foo - bar - baz *********************************** *)
79
80 let static_bullet ({ entry_point; prev_node } as view) =
81 let open Vernacexpr in
82 assert (not (Vernacprop.has_query_control entry_point.ast));
83 match entry_point.ast.CAst.v.expr with
84 | VernacSynPure (VernacBullet b) ->
85 let base = entry_point.indentation in
86 let last_tac = prev_node entry_point in
87 crawl view ~init:last_tac (fun prev node ->
88 if node.indentation < base then `Stop else
89 if node.indentation > base then `Cont node else
90 if Vernacprop.has_query_control node.ast then `Stop
91 else match node.ast.CAst.v.expr with
92 | VernacSynPure (VernacBullet b') when b = b' ->
93 `Found { block_stop = entry_point.id; block_start = prev.id;
94 dynamic_switch = node.id; carry_on_data = of_bullet_val b }
95 | _ -> `Stop) entry_point
96 | _ -> assert false
97
98 let dynamic_bullet doc { dynamic_switch = id; carry_on_data = b } =
99 match is_focused_goal_simple ~doc id with
100 | `Simple focused ->
101 ValidBlock {
102 base_state = id;
103 goals_to_admit = focused;
104 recovery_command = Some (CAst.make Vernacexpr.{ control = []; attrs = []; expr = VernacSynPure (VernacBullet (to_bullet_val b))})
105 }
106 | `Not -> Leaks
107
108 let () = register_proof_block_delimiter
109 "bullet" static_bullet dynamic_bullet
110
111 (* ******************** { block } ***************************************** *)
112
113 let static_curly_brace ({ entry_point; prev_node } as view) =
114 let open Vernacexpr in
115 assert(entry_point.ast.CAst.v.expr = VernacSynPure VernacEndSubproof);
116 crawl view (fun (nesting,prev) node ->
117 if Vernacprop.has_query_control node.ast then `Cont (nesting,node)
118 else match node.ast.CAst.v.expr with
119 | VernacSynPure (VernacSubproof _) when nesting = 0 ->
120 `Found { block_stop = entry_point.id; block_start = prev.id;
121 dynamic_switch = node.id; carry_on_data = unit_val }
122 | VernacSynPure (VernacSubproof _) ->
123 `Cont (nesting - 1,node)
124 | VernacSynPure VernacEndSubproof ->
125 `Cont (nesting + 1,node)
126 | _ -> `Cont (nesting,node)) (-1, entry_point)
127
128 let dynamic_curly_brace doc { dynamic_switch = id } =
129 match is_focused_goal_simple ~doc id with
130 | `Simple focused ->
131 ValidBlock {
132 base_state = id;
133 goals_to_admit = focused;
134 recovery_command = Some (CAst.make Vernacexpr.{ control = []; attrs = []; expr = VernacSynPure VernacEndSubproof })
135 }
136 | `Not -> Leaks
137
138 let () = register_proof_block_delimiter
139 "curly" static_curly_brace dynamic_curly_brace
140
141 (* ***************** par: ************************************************* *)
142
143 let static_par { entry_point; prev_node } =
144 match prev_node entry_point with
145 | None -> None
146 | Some { id = pid } ->
147 Some { block_stop = entry_point.id; block_start = pid;
148 dynamic_switch = pid; carry_on_data = unit_val }
149
150 let dynamic_par doc { dynamic_switch = id } =
151 match is_focused_goal_simple ~doc id with
152 | `Simple focused ->
153 ValidBlock {
154 base_state = id;
155 goals_to_admit = focused;
156 recovery_command = None;
157 }
158 | `Not -> Leaks
159
160 let () = register_proof_block_delimiter "par" static_par dynamic_par
161
162 (* ***************** simple indentation *********************************** *)
163
164 let static_indent ({ entry_point; prev_node } as view) =
165 Printf.eprintf "@%d\n" (Stateid.to_int entry_point.id);
166 match prev_node entry_point with
167 | None -> None
168 | Some last_tac ->
169 if last_tac.indentation <= entry_point.indentation then None
170 else
171 crawl view ~init:(Some last_tac) (fun prev node ->
172 if node.indentation >= last_tac.indentation then `Cont node
173 else
174 `Found { block_stop = entry_point.id; block_start = node.id;
175 dynamic_switch = node.id;
176 carry_on_data = of_vernac_control_val entry_point.ast }
177 ) last_tac
178
179 let dynamic_indent doc { dynamic_switch = id; carry_on_data = e } =
180 Printf.eprintf "%s\n" (Stateid.to_string id);
181 match is_focused_goal_simple ~doc id with
182 | `Simple [] -> Leaks
183 | `Simple focused ->
184 let but_last = List.tl (List.rev focused) in
185 ValidBlock {
186 base_state = id;
187 goals_to_admit = but_last;
188 recovery_command = Some (to_vernac_control_val e);
189 }
190 | `Not -> Leaks
191
192 let () = register_proof_block_delimiter "indent" static_indent dynamic_indent
193