7de3c542ecca36af3e8f6306f0b271e8b9a90aa8
[bpt/coccinelle.git] / bundles / menhirLib / menhir-20120123 / src / Fix.ml
1 (**************************************************************************)
2 (* *)
3 (* Menhir *)
4 (* *)
5 (* François Pottier, INRIA Rocquencourt *)
6 (* Yann Régis-Gianas, PPS, Université Paris Diderot *)
7 (* *)
8 (* Copyright 2005-2008 Institut National de Recherche en Informatique *)
9 (* et en Automatique. All rights reserved. This file is distributed *)
10 (* under the terms of the Q Public License version 1.0, with the change *)
11 (* described in file LICENSE. *)
12 (* *)
13 (**************************************************************************)
14
15 (* -------------------------------------------------------------------------- *)
16
17 (* Maps. *)
18
19 (* We require imperative maps, that is, maps that can be updated in place.
20 An implementation of persistent maps, such as the one offered by ocaml's
21 standard library, can easily be turned into an implementation of imperative
22 maps, so this is a weak requirement. *)
23
24 module type IMPERATIVE_MAPS = sig
25 type key
26 type 'data t
27 val create: unit -> 'data t
28 val clear: 'data t -> unit
29 val add: key -> 'data -> 'data t -> unit
30 val find: key -> 'data t -> 'data
31 val iter: (key -> 'data -> unit) -> 'data t -> unit
32 end
33
34 (* -------------------------------------------------------------------------- *)
35
36 (* Properties. *)
37
38 (* Properties must form a partial order, equipped with a least element, and
39 must satisfy the ascending chain condition: every monotone sequence
40 eventually stabilizes. *)
41
42 (* [is_maximal] determines whether a property [p] is maximal with respect to
43 the partial order. Only a conservative check is required: in any event, it
44 is permitted for [is_maximal p] to return [false]. If [is_maximal p]
45 returns [true], then [p] must have no upper bound other than itself. In
46 particular, if properties form a lattice, then [p] must be the top
47 element. This feature, not described in the paper, enables a couple of
48 minor optimizations. *)
49
50 module type PROPERTY = sig
51 type property
52 val bottom: property
53 val equal: property -> property -> bool
54 val is_maximal: property -> bool
55 end
56
57 (* -------------------------------------------------------------------------- *)
58
59 (* The dynamic dependency graph. *)
60
61 (* An edge from [node1] to [node2] means that [node1] depends on [node2], or
62 (equivalently) that [node1] observes [node2]. Then, an update of the
63 current property at [node2] causes a signal to be sent to [node1]. A node
64 can observe itself. *)
65
66 (* This module could be placed in a separate file, but is included here in
67 order to make [Fix] self-contained. *)
68
69 module Graph : sig
70
71 (* This module provides a data structure for maintaining and modifying
72 a directed graph. Each node is allowed to carry a piece of client
73 data. There are functions for creating a new node, looking up a
74 node's data, looking up a node's predecessors, and setting or
75 clearing a node's successors (all at once). *)
76 type 'data node
77
78 (* [create data] creates a new node, with no incident edges, with
79 client information [data]. Time complexity: constant. *)
80 val create: 'data -> 'data node
81
82 (* [data node] returns the client information associated with
83 the node [node]. Time complexity: constant. *)
84 val data: 'data node -> 'data
85
86 (* [predecessors node] returns a list of [node]'s predecessors.
87 Amortized time complexity: linear in the length of the output
88 list. *)
89 val predecessors: 'data node -> 'data node list
90
91 (* [set_successors src dsts] creates an edge from the node [src] to
92 each of the nodes in the list [dsts]. Duplicate elements in the
93 list [dsts] are removed, so that no duplicate edges are created. It
94 is assumed that [src] initially has no successors. Time complexity:
95 linear in the length of the input list. *)
96 val set_successors: 'data node -> 'data node list -> unit
97
98 (* [clear_successors node] removes all of [node]'s outgoing edges.
99 Time complexity: linear in the number of edges that are removed. *)
100 val clear_successors: 'data node -> unit
101
102 (* That's it. *)
103 end
104 = struct
105
106 (* Using doubly-linked adjacency lists, one could implement [predecessors]
107 in worst-case linear time with respect to the length of the output list,
108 [set_successors] in worst-case linear time with respect to the length of
109 the input list, and [clear_successors] in worst-case linear time with
110 respect to the number of edges that are removed. We use a simpler
111 implementation, based on singly-linked adjacency lists, with deferred
112 removal of edges. It achieves the same complexity bounds, except
113 [predecessors] only offers an amortized complexity bound. This is good
114 enough for our purposes, and, in practice, is more efficient by a
115 constant factor. This simplification was suggested by Arthur
116 Charguéraud. *)
117
118 type 'data node = {
119
120 (* The client information associated with this node. *)
121
122 data: 'data;
123
124 (* This node's incoming and outgoing edges. *)
125
126 mutable outgoing: 'data edge list;
127 mutable incoming: 'data edge list;
128
129 (* A transient mark, always set to [false], except when checking
130 against duplicate elements in a successor list. *)
131
132 mutable marked: bool;
133
134 }
135
136 and 'data edge = {
137
138 (* This edge's nodes. Edges are symmetric: source and destination
139 are not distinguished. Thus, an edge appears both in the outgoing
140 edge list of its source node and in the incoming edge list of its
141 destination node. This allows edges to be easily marked as
142 destroyed. *)
143
144 node1: 'data node;
145 node2: 'data node;
146
147 (* Edges that are destroyed are marked as such, but are not
148 immediately removed from the adjacency lists. *)
149
150 mutable destroyed: bool;
151
152 }
153
154 let create (data : 'data) : 'data node = {
155 data = data;
156 outgoing = [];
157 incoming = [];
158 marked = false;
159 }
160
161 let data (node : 'data node) : 'data =
162 node.data
163
164 (* [follow src edge] returns the node that is connected to [src]
165 by [edge]. Time complexity: constant. *)
166
167 let follow src edge =
168 if edge.node1 == src then
169 edge.node2
170 else begin
171 assert (edge.node2 == src);
172 edge.node1
173 end
174
175 (* The [predecessors] function removes edges that have been marked
176 destroyed. The cost of removing these has already been paid for,
177 so the amortized time complexity of [predecessors] is linear in
178 the length of the output list. *)
179
180 let predecessors (node : 'data node) : 'data node list =
181 let predecessors = List.filter (fun edge -> not edge.destroyed) node.incoming in
182 node.incoming <- predecessors;
183 List.map (follow node) predecessors
184
185 (* [link src dst] creates a new edge from [src] to [dst], together
186 with its reverse edge. Time complexity: constant. *)
187
188 let link (src : 'data node) (dst : 'data node) : unit =
189 let edge = {
190 node1 = src;
191 node2 = dst;
192 destroyed = false;
193 } in
194 src.outgoing <- edge :: src.outgoing;
195 dst.incoming <- edge :: dst.incoming
196
197 let set_successors (src : 'data node) (dsts : 'data node list) : unit =
198 assert (src.outgoing = []);
199 let rec loop = function
200 | [] ->
201 ()
202 | dst :: dsts ->
203 if dst.marked then
204 loop dsts (* skip duplicate elements *)
205 else begin
206 dst.marked <- true;
207 link src dst;
208 loop dsts;
209 dst.marked <- false
210 end
211 in
212 loop dsts
213
214 let clear_successors (node : 'data node) : unit =
215 List.iter (fun edge ->
216 assert (not edge.destroyed);
217 edge.destroyed <- true;
218 ) node.outgoing;
219 node.outgoing <- []
220
221 end
222
223 (* -------------------------------------------------------------------------- *)
224
225 (* The code is parametric in an implementation of maps over variables and in
226 an implementation of properties. *)
227
228 module Make
229 (M : IMPERATIVE_MAPS)
230 (P : PROPERTY)
231 = struct
232
233 type variable =
234 M.key
235
236 type property =
237 P.property
238
239 type valuation =
240 variable -> property
241
242 type rhs =
243 valuation -> property
244
245 type equations =
246 variable -> rhs
247
248 (* -------------------------------------------------------------------------- *)
249
250 (* Data. *)
251
252 (* Each node in the dependency graph carries information about a fixed
253 variable [v]. *)
254
255 type node =
256 data Graph.node
257
258 and data = {
259
260 (* This is the result of the application of [rhs] to the variable [v]. It
261 must be stored in order to guarantee that this application is performed
262 at most once. *)
263 rhs: rhs;
264
265 (* This is the current property at [v]. It evolves monotonically with
266 time. *)
267 mutable property: property;
268
269 (* That's it! *)
270 }
271
272 (* [property node] returns the current property at [node]. *)
273
274 let property node =
275 (Graph.data node).property
276
277 (* -------------------------------------------------------------------------- *)
278
279 (* Many definitions must be made within the body of the function [lfp].
280 For greater syntactic convenience, we place them in a local module. *)
281
282 let lfp (eqs : equations) : valuation =
283 let module LFP = struct
284
285 (* -------------------------------------------------------------------------- *)
286
287 (* The workset. *)
288
289 (* When the algorithm is inactive, the workset is empty. *)
290
291 (* Our workset is based on a Queue, but it could just as well be based on a
292 Stack. A textual replacement is possible. It could also be based on a
293 priority queue, provided a sensible way of assigning priorities could
294 be found. *)
295
296 module Workset : sig
297
298 (* [insert node] inserts [node] into the workset. [node] must have no
299 successors. *)
300 val insert: node -> unit
301
302 (* [repeat f] repeatedly applies [f] to a node extracted out of the
303 workset, until the workset becomes empty. [f] is allowed to use
304 [insert]. *)
305 val repeat: (node -> unit) -> unit
306
307 (* That's it! *)
308 end
309 = struct
310
311 (* Initialize the workset. *)
312
313 let workset =
314 Queue.create()
315
316 let insert node =
317 Queue.push node workset
318
319 let repeat f =
320 while not (Queue.is_empty workset) do
321 f (Queue.pop workset)
322 done
323
324 end
325
326 (* -------------------------------------------------------------------------- *)
327
328 (* Signals. *)
329
330 (* A node in the workset has no successors. (It can have predecessors.) In
331 other words, a predecessor (an observer) of some node is never in the
332 workset. Furthermore, a node never appears twice in the workset. *)
333
334 (* When a variable broadcasts a signal, all of its predecessors (observers)
335 receive the signal. Any variable that receives the signal loses all of its
336 successors (that is, it ceases to observe anything) and is inserted into
337 the workset. This preserves the above invariant. *)
338
339 let signal subject =
340 List.iter (fun observer ->
341 Graph.clear_successors observer;
342 Workset.insert observer
343 ) (Graph.predecessors subject)
344 (* At this point, [subject] has no predecessors. This plays no role in
345 the correctness proof, though. *)
346
347 (* -------------------------------------------------------------------------- *)
348
349 (* Tables. *)
350
351 (* The permanent table maps variables that have reached a fixed point
352 to properties. It persists forever. *)
353
354 let permanent : property M.t =
355 M.create()
356
357 (* The transient table maps variables that have not yet reached a
358 fixed point to nodes. (A node contains not only a property, but
359 also a memoized right-hand side, and carries edges.) At the
360 beginning of a run, it is empty. It fills up during a run. At the
361 end of a run, it is copied into the permanent table and cleared. *)
362
363 let transient : node M.t =
364 M.create()
365
366 (* [freeze()] copies the transient table into the permanent table, and
367 empties the transient table. This allows all nodes to be reclaimed
368 by the garbage collector. *)
369
370 let freeze () =
371 M.iter (fun v node ->
372 M.add v (property node) permanent
373 ) transient;
374 M.clear transient
375
376 (* -------------------------------------------------------------------------- *)
377
378 (* Workset processing. *)
379
380
381 (* [solve node] re-evaluates the right-hand side at [node]. If this leads to
382 a change, then the current property is updated, and [node] emits a signal
383 towards its observers. *)
384
385 (* When [solve node] is invoked, [node] has no subjects. Indeed, when [solve]
386 is invoked by [node_for], [node] is newly created; when [solve] is invoked by
387 [Workset.repeat], [node] has just been extracted out of the workset, and a
388 node in the workset has no subjects. *)
389
390 (* [node] must not be in the workset. *)
391
392 (* In short, when [solve node] is invoked, [node] is neither awake nor asleep.
393 When [solve node] finishes, [node] is either awake or asleep again. (Chances
394 are, it is asleep, unless it is its own observer; then, it is awakened by the
395 final call to [signal node].) *)
396
397 let rec solve (node : node) : unit =
398
399 (* Retrieve the data record carried by this node. *)
400 let data = Graph.data node in
401
402 (* Prepare to compute an updated value at this node. This is done by
403 invoking the client's right-hand side function. *)
404
405 (* The flag [alive] is used to prevent the client from invoking [request]
406 after this interaction phase is over. In theory, this dynamic check seems
407 required in order to argue that [request] behaves like a pure function.
408 In practice, this check is not very useful: only a bizarre client would
409 store a [request] function and invoke it after it has become stale. *)
410 let alive = ref true
411 and subjects = ref [] in
412
413 (* We supply the client with [request], a function that provides access to
414 the current valuation, and dynamically records dependencies. This yields
415 a set of dependencies that is correct by construction. *)
416 let request (v : variable) : property =
417 assert !alive;
418 try
419 M.find v permanent
420 with Not_found ->
421 let subject = node_for v in
422 let p = property subject in
423 if not (P.is_maximal p) then
424 subjects := subject :: !subjects;
425 p
426 in
427
428 (* Give control to the client. *)
429 let new_property = data.rhs request in
430
431 (* From now on, prevent any invocation of this instance of [request]
432 the client. *)
433 alive := false;
434
435 (* At this point, [node] has no subjects, as noted above. Thus, the
436 precondition of [set_successors] is met. We can install [data.subjects]
437 as the new set of subjects for this node. *)
438
439 (* If we have gathered no subjects in the list [data.subjects], then
440 this node must have stabilized. If [new_property] is maximal,
441 then this node must have stabilized. *)
442
443 (* If this node has stabilized, then it need not observe any more, so the
444 call to [set_successors] is skipped. In practice, this seems to be a
445 minor optimization. In the particular case where every node stabilizes at
446 the very first call to [rhs], this means that no edges are ever
447 built. This particular case is unlikely, as it means that we are just
448 doing memoization, not a true fixed point computation. *)
449
450 (* One could go further and note that, if this node has stabilized, then it
451 could immediately be taken out of the transient table and copied into the
452 permanent table. This would have the beneficial effect of allowing the
453 detection of further nodes that have stabilized. Furthermore, it would
454 enforce the property that no node in the transient table has a maximal
455 value, hence the call to [is_maximal] above would become useless. *)
456
457 if not (!subjects = [] || P.is_maximal new_property) then
458 Graph.set_successors node !subjects;
459
460 (* If the updated value differs from the previous value, record
461 the updated value and send a signal to all observers of [node]. *)
462 if not (P.equal data.property new_property) then begin
463 data.property <- new_property;
464 signal node
465 end
466 (* Note that equality of the two values does not imply that this node has
467 stabilized forever. *)
468
469 (* -------------------------------------------------------------------------- *)
470
471 (* [node_for v] returns the graph node associated with the variable [v]. It is
472 assumed that [v] does not appear in the permanent table. If [v] appears in
473 the transient table, the associated node is returned. Otherwise, [v] is a
474 newly discovered variable: a new node is created on the fly, and the
475 transient table is grown. The new node can either be inserted into the
476 workset (it is then awake) or handled immediately via a recursive call to
477 [solve] (it is then asleep, unless it observes itself). *)
478
479 (* The recursive call to [solve node] can be replaced, if desired, by a call
480 to [Workset.insert node]. Using a recursive call to [solve] permits eager
481 top-down discovery of new nodes. This can save a constant factor, because
482 it allows new nodes to move directly from [bottom] to a good first
483 approximation, without sending any signals, since [node] has no observers
484 when [solve node] is invoked. In fact, if the dependency graph is acyclic,
485 the algorithm discovers nodes top-down, performs computation on the way
486 back up, and runs without ever inserting a node into the workset!
487 Unfortunately, this causes the stack to grow as deep as the longest path in
488 the dependency graph, which can blow up the stack. *)
489
490 and node_for (v : variable) : node =
491 try
492 M.find v transient
493 with Not_found ->
494 let node = Graph.create { rhs = eqs v; property = P.bottom } in
495 (* Adding this node to the transient table prior to calling [solve]
496 recursively is mandatory, otherwise [solve] might loop, creating
497 an infinite number of nodes for the same variable. *)
498 M.add v node transient;
499 solve node; (* or: Workset.insert node *)
500 node
501
502 (* -------------------------------------------------------------------------- *)
503
504 (* Invocations of [get] trigger the fixed point computation. *)
505
506 (* The flag [inactive] prevents reentrant calls by the client. *)
507
508 let inactive =
509 ref true
510
511 let get (v : variable) : property =
512 try
513 M.find v permanent
514 with Not_found ->
515 assert !inactive;
516 inactive := false;
517 let node = node_for v in
518 Workset.repeat solve;
519 freeze();
520 inactive := true;
521 property node
522
523 (* -------------------------------------------------------------------------- *)
524
525 (* Close the local module [LFP]. *)
526
527 end
528 in LFP.get
529
530 end