Debian Codegraph @

engine/proofview_monad.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 (** This file defines the datatypes used as internal states by the
12 tactic monad, and specialises the [Logic_monad] to these type. *)
13
14 (** {6 Trees/forest for traces} *)
15
16 module Trace = struct
17
18 (** The intent is that an ['a forest] is a list of messages of type
19 ['a]. But messages can stand for a list of more precise
20 messages, hence the structure is organised as a tree. *)
21 type 'a forest = 'a tree list
22 and 'a tree = Seq of 'a * 'a forest
23
24 (** To build a trace incrementally, we use an intermediary data
25 structure on which we can define an S-expression like language
26 (like a simplified xml except the closing tags do not carry a
27 name). Note that nodes are built from right to left in ['a
28 incr], the result is mirrored when returning so that in the
29 exposed interface, the forest is read from left to right.
30
31 Concretely, we want to add a new tree to a forest: and we are
32 building it by adding new trees to the left of its left-most
33 subtrees which is built the same way. *)
34 type 'a incr = { head:'a forest ; opened: 'a tree list }
35
36 (** S-expression like language as ['a incr] transformers. It is the
37 responsibility of the library builder not to use [close] when no
38 tag is open. *)
39 let empty_incr = { head=[] ; opened=[] }
40 let opn a { head ; opened } = { head ; opened = Seq(a,[])::opened }
41 let close { head ; opened } =
42 match opened with
43 | [a] -> { head = a::head ; opened=[] }
44 | a::Seq(b,f)::opened -> { head ; opened=Seq(b,a::f)::opened }
45 | [] -> assert false
46 let leaf a s = close (opn a s)
47
48 (** Returning a forest. It is the responsibility of the library
49 builder to close all the tags. *)
50 (* spiwack: I may want to close the tags instead, to deal with
51 interruptions. *)
52 let rec mirror f = List.rev_map mirror_tree f
53 and mirror_tree (Seq(a,f)) = Seq(a,mirror f)
54
55 let to_tree = function
56 | { head ; opened=[] } -> mirror head
57 | { head ; opened=_::_} -> assert false
58
59 end
60
61
62
63 (** {6 State types} *)
64
65 (** We typically label nodes of [Trace.tree] with messages to
66 print. But we don't want to compute the result. *)
67 type lazy_msg = unit -> Pp.t
68
69 (** Info trace. *)
70 module Info = struct
71
72 (** The type of the tags for [info]. *)
73 type tag =
74 | Msg of lazy_msg (** A simple message *)
75 | Tactic of lazy_msg (** A tactic call *)
76 | Dispatch (** A call to [tclDISPATCH]/[tclEXTEND] *)
77 | DBranch (** A special marker to delimit individual branch of a dispatch. *)
78
79 type state = tag Trace.incr
80 type tree = tag Trace.forest
81
82 let pr_in_comments m = Pp.(str"(* "++ m () ++str" *)")
83
84 let unbranch = function
85 | Trace.Seq (DBranch,brs) -> brs
86 | _ -> assert false
87
88
89 let is_empty_branch = let open Trace in function
90 | Seq(DBranch,[]) -> true
91 | _ -> false
92
93 (** Dispatch with empty branches are (supposed to be) equivalent to
94 [idtac] which need not appear, so they are removed from the
95 trace. *)
96 let dispatch brs =
97 let open Trace in
98 if CList.for_all is_empty_branch brs then None
99 else Some (Seq(Dispatch,brs))
100
101 let constr = let open Trace in function
102 | Dispatch -> dispatch
103 | t -> fun br -> Some (Seq(t,br))
104
105 let rec compress_tree = let open Trace in function
106 | Seq(t,f) -> constr t (compress f)
107 and compress f =
108 CList.map_filter compress_tree f
109
110 (** [with_sep] is [true] when [Tactic m] must be printed with a
111 trailing semi-colon. *)
112 let rec pr_tree with_sep = let open Trace in function
113 | Seq (Msg m,[]) -> pr_in_comments m
114 | Seq (Tactic m,_) ->
115 let tail = if with_sep then Pp.str";" else Pp.mt () in
116 Pp.(m () ++ tail)
117 | Seq (Dispatch,brs) ->
118 let tail = if with_sep then Pp.str";" else Pp.mt () in
119 Pp.(pr_dispatch brs++tail)
120 | Seq (Msg _,_::_) | Seq (DBranch,_) -> assert false
121 and pr_dispatch brs =
122 let open Pp in
123 let brs = List.map unbranch brs in
124 match brs with
125 | [br] -> pr_forest br
126 | _ ->
127 let sep () = spc()++str"|"++spc() in
128 let branches = prlist_with_sep sep pr_forest brs in
129 str"[>"++spc()++branches++spc()++str"]"
130 and pr_forest = function
131 | [] -> Pp.mt ()
132 | [tr] -> pr_tree false tr
133 | tr::l -> Pp.(pr_tree true tr ++ pr_forest l)
134
135 let print _env _sigma f =
136 pr_forest (compress f)
137
138 let rec collapse_tree n t =
139 let open Trace in
140 match n , t with
141 | 0 , t -> [t]
142 | _ , (Seq(Tactic _,[]) as t) -> [t]
143 | n , Seq(Tactic _,f) -> collapse (pred n) f
144 | n , Seq(Dispatch,brs) -> [Seq(Dispatch, (collapse n brs))]
145 | n , Seq(DBranch,br) -> [Seq(DBranch, (collapse n br))]
146 | _ , (Seq(Msg _,_) as t) -> [t]
147 and collapse n f =
148 CList.map_append (collapse_tree n) f
149 end
150
151 module StateStore = Store.Make()
152
153 (* let (set_state, get_state) = StateDyn.Easy.make_dyn "goal_state" *)
154
155 type goal = Evar.t
156 type goal_with_state = Evar.t * StateStore.t
157
158 let drop_state = fst
159 let get_state = snd
160 let goal_with_state g s = (g, s)
161 let with_empty_state g = (g, StateStore.empty)
162 let map_goal_with_state f (g, s) = (f g, s)
163
164 (** Type of proof views: current [evar_map] together with the list of
165 focused goals. *)
166 type proofview = {
167 solution : Evd.evar_map;
168 comb : goal_with_state list;
169 }
170
171 (** {6 Instantiation of the logic monad} *)
172
173 (** Parameters of the logic monads *)
174 module P = struct
175
176 type s = proofview * Environ.env
177
178 (** Recording info trace (true) or not. *)
179 type e = { trace: bool; name : Names.Id.t; poly : PolyFlags.t }
180
181 (** Status (safe/unsafe) * shelved goals * given up *)
182 type w = bool
183
184 let wunit = true
185 let wprod b1 b2 = b1 && b2
186
187 type u = Info.state
188
189 let uunit = Trace.empty_incr
190
191 end
192
193 module Logical = Logic_monad.Logical(P)
194
195
196 (** {6 Lenses to access to components of the states} *)
197
198 module type State = sig
199 type t
200 val get : t Logical.t
201 val set : t -> unit Logical.t
202 val modify : (t->t) -> unit Logical.t
203 end
204
205 module type Reader = sig
206 type t
207 val get : t Logical.t
208 end
209
210 module type Writer = sig
211 type t
212 val put : t -> unit Logical.t
213 end
214
215 module Pv : State with type t := proofview = struct
216 let get = Logical.(map fst get)
217 let set p = Logical.modify (fun (_,e) -> (p,e))
218 let modify f= Logical.modify (fun (p,e) -> (f p,e))
219 end
220
221 module Solution : State with type t := Evd.evar_map = struct
222 let get = Logical.map (fun {solution} -> solution) Pv.get
223 let set s = Pv.modify (fun pv -> { pv with solution = s })
224 let modify f = Pv.modify (fun pv -> { pv with solution = f pv.solution })
225 end
226
227 module Comb : State with type t = goal_with_state list = struct
228 (* spiwack: I don't know why I cannot substitute ([:=]) [t] with a type expression. *)
229 type t = goal_with_state list
230 let get = Logical.map (fun {comb} -> comb) Pv.get
231 let set c = Pv.modify (fun pv -> { pv with comb = c })
232 let modify f = Pv.modify (fun pv -> { pv with comb = f pv.comb })
233 end
234
235 module Env : State with type t := Environ.env = struct
236 let get = Logical.(map snd get)
237 let set e = Logical.modify (fun (p,_) -> (p,e))
238 let modify f = Logical.modify (fun (p,e) -> (p,f e))
239 end
240
241 module Status : Writer with type t := bool = struct
242 let put s = Logical.put s
243 end
244
245 (** Lens and utilities pertaining to the info trace *)
246 module InfoL = struct
247 let recording = Logical.(map (fun {P.trace} -> trace) current)
248 let if_recording t =
249 let open Logical in
250 recording >>= fun r ->
251 if r then t else return ()
252
253 let record_trace t =
254 Logical.(
255 current >>= fun s ->
256 local {s with P.trace = true} t)
257
258 let raw_update = Logical.update
259 let update f = if_recording (raw_update f)
260 let opn a = update (Trace.opn a)
261 let close = update Trace.close
262 let leaf a = update (Trace.leaf a)
263
264 let tag a t =
265 let open Logical in
266 recording >>= fun r ->
267 if r then begin
268 raw_update (Trace.opn a) >>
269 t >>= fun a ->
270 raw_update Trace.close >>
271 return a
272 end else
273 t
274 end