-
Notifications
You must be signed in to change notification settings - Fork 2
/
draw_tree.ml
1779 lines (1543 loc) · 65.1 KB
/
draw_tree.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* prooftree --- proof tree display for Proof General
*
* Copyright (C) 2011 - 2024 Hendrik Tews
*
* This file is part of "prooftree".
*
* "prooftree" is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* "prooftree" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License in file COPYING in this or one of the parent
* directories for more details.
*
* You should have received a copy of the GNU General Public License
* along with "prooftree". If not, see <http://www.gnu.org/licenses/>.
*)
(** Layout and drawing of the elements of the proof tree.
Internally a proof tree is organized as an n-ary tree, where the
nodes are proof goals and proof commands and the vertices connect
them appropriately. This module is responsible for manipulating
and displaying these trees and for locating nodes (e.g., on mouse
clicks).
A real proof tree has a number of properties, about which this
module is completely ignorant. For instance, the root node is
always a proof goal; proof goal nodes have zero or more successor
nodes, all of which are proof commands; and, finally, every proof
command has at most one proof-goal successor. These properties
are neither assumed nor checked, they hopefully hold, because the
tree is created in the right way.
The common code of both proof-goal and proof-command nodes is in
the class {!class: Draw_tree.proof_tree_element}. The class for proof goals,
{!turnstile} and the class {!class: proof_command} are derived from it.
To work around the impossible down-casts, {!proof_tree_element}
contains some virtual method hooks for stuff that is really
specific for just one of its subclasses.
The tree layout functionallity has been designed such that its
running time is independent of the size of the complete tree. When
a new node is inserted into the tree, only its direct and indirect
parent nodes need to recompute their layout data. No sibling node
must be visited. To achieve this the nodes do not store absolut
positions. Instead, nodes only store the width and height of
themselves and of their subtrees.
Adjusting the tree layout when new elements are inserted works
bottom up. Drawing the tree or looking up nodes (for mouse events)
works top down. Therefore the nodes are organized in a
doubly-linked tree, where children nodes contain a link to their
parent. The doubly-linked tree functionality is in
{!class: doubly_linked_tree}.
*)
open Util
open Configuration
open Gtk_ext
(** {2 Utility types and functions} *)
(*****************************************************************************)
(*****************************************************************************)
(** {3 turnstile interface} *)
(*****************************************************************************)
(*****************************************************************************)
(** Abstract interface for class {!turnstile} for the existential variable
record. This class type is used to break the mutual dependency between
existential variable records and and the {!turnstile} class.
*)
class type turnstile_interface =
object
(** Sequent ID, debugging name for sequent elements.
*)
method id : string
end
(*****************************************************************************)
(*****************************************************************************)
(** {3 Existential variables} *)
(*****************************************************************************)
(*****************************************************************************)
(** The code for marking and displaying existential variables depends
on proper sharing of these records: For each proof-tree window
there must only be one record for each existential variable. The
same existential variable in different (cloned) proof trees must
have exactly one record for each proof-tree window.
The proof-tree record ({!Proof_tree.proof_tree}) contains a hash
table containing all existential variables for a given proof.
Changing the state of an existental variable and marking one in
the proof-tree display works by side effect: All proof tree nodes
refer to the very same instance and therefore see the state
change.
Sets of existential variables are stored as lists, whoose order is
usually not important. Therefore most functions that manipulate
lists of existential variables do not preserve the order.
*)
(** Status of an existential variable. The tree of existentials is
only scanned for redisplay. Therefore, a fully instantiated
existential might have state [Partially_instantiated] until the next scan.
*)
type existential_status =
| Uninstantiated (** open, not instantiated *)
| Partially_instantiated (** instantiated, but the
instantiation uses some
existentials that are still open *)
| Fully_instantiated (** fully instantiated *)
(** Representation of existential variables. The [status] field is
lazily updated in {!Proof_tree.update_existential_status}.
Therefore, a fully instantiated existential might have status
{!existential_status.Partially_instantiated} for some time.
The external name should only be present for non-instantiated
evars. The code however does neither enforce nor rely on this
invariant.
The field [evar_deps] builds the dependency or instantiation tree
of existential variables. This tree must be downward closed on
registered sequents. That is, any [(state, sequent)] pair present
in [evar_sequents] must also be present in all existentials in
[evar_deps]. This property is maintained in
{!Proof_tree.instantiate_existential} (via
{!Proof_tree.propagate_registered_sequents}) and in
{!Proof_tree.register_and_update_sequent}.
*)
type existential_variable = {
evar_internal_name : string; (** The internal Coq ID *)
mutable evar_external_name : string option;
(** The external name if
not instantiated. *)
mutable evar_status : existential_status;
(** instantiation status *)
mutable evar_mark : bool; (** [true] if this existential should
be marked in the proof-tree
display *)
mutable evar_inst_state : int;
(** State in which the evar got instantiated
or -1, also -1 for clones. Needed when a
sequent is registered for an instantiated
evar. *)
mutable evar_deps : existential_variable list;
(** The list of evars that are used
in the instantiation,
if instantiated *)
mutable evar_sequents : turnstile_interface list Int_map.t
(** Sequents that contain this evar and need to be
updated when this evar is instantiated. Empty for
clones and retired proof trees. The map key (int)
is an undo state, it is mapped to all turnstiles
that started to contain this evar in this state.
Thus for undo, one can chop off some upper
portion of this map. To get all turnstiles to
be updated, one has to iterate over all keys. *)
}
(** Filter the non-instantiated existentials from the argument.
*)
let filter_uninstantiated exl =
list_filter_rev (fun ex -> ex.evar_status = Uninstantiated) exl
(** Filter the partially instantiated existentials from the argument *)
let filter_partially_instantiated exl =
list_filter_rev (fun ex -> ex.evar_status = Partially_instantiated) exl
(** Derive the existential status for drawing a node or a connection
line in the proof tree.
*)
let combine_existential_status_for_tree exl =
if List.for_all (fun ex -> ex.evar_status = Fully_instantiated) exl
then Fully_instantiated
else if List.exists (fun ex -> ex.evar_status = Uninstantiated) exl
then Uninstantiated
else Partially_instantiated
(** Convert a set of existential variables into a single string for
display purposes.
*)
let string_of_existential_list exl =
String.concat " " (List.map (fun ex -> ex.evar_internal_name) exl)
(*****************************************************************************)
(*****************************************************************************)
(** {3 Misc types} *)
(*****************************************************************************)
(*****************************************************************************)
(** Kind of nodes in the proof-tree display. The two kinds correspond
to the two subclasses {!proof_command} and {!turnstile} of
{!proof_tree_element}.
*)
type node_kind =
| Proof_command (** proof command *)
| Turnstile (** sequent *)
(** Proof state of a node in the proof-tree display. *)
type branch_state_type =
| Unproven (** no finished yet *)
| CurrentNode (** current sequent in prover *)
| Current (** on the path from the current
sequent to the root of the tree *)
| Cheated (** proved, but with a cheating
command *)
| Proven (** proved *)
(*
* write doc when used
* let string_of_branch_state = function
* | Unproven -> "Unproven"
* | CurrentNode -> "CurrentNode"
* | Current -> "Current"
* | Cheated -> "Cheated"
* | Proven -> "Proven"
*)
(*****************************************************************************)
(*****************************************************************************)
(** {3 Graphics context color manipulations} *)
(*****************************************************************************)
(*****************************************************************************)
(** The following functions implement a simple save/restore feature
for the forground color of the graphics context. A saved state is
a color option. The value [None] means that the foreground color
has not been changed and that there is therefore no need to
restore it.
*)
(** Save the current foreground color in a value suitable for
{!restore_gc}.
*)
let save_gc drawable =
Some drawable#get_foreground
(** Restore the saved foreground color. Do nothing if the foreground
color has not been changed.
*)
let restore_gc drawable fc_opt = match fc_opt with
| None -> ()
| Some fc -> drawable#set_foreground (`COLOR fc)
(** [save_and_set_gc drawable state existentials] sets the foreground
color to one of the configured colors, depending on [state] and
[existentials]. The function returns a value suitable for
{!restore_gc} to restore the old foreground color.
*)
let save_and_set_gc drawable state existentials =
(*
* if List.exists (fun e -> e.existential_mark) existentials
* then begin
* let res = save_gc drawable in
* drawable#set_foreground (`COLOR !mark_subtree_gdk_color);
* res
* end else
*)
match state with
| Unproven -> None
| CurrentNode
| Current ->
let res = save_gc drawable in
drawable#set_foreground (`COLOR !current_gdk_color);
res
| Proven ->
let res = save_gc drawable in
let color = match combine_existential_status_for_tree existentials with
| Fully_instantiated -> !proved_complete_gdk_color
| Partially_instantiated -> !proved_partial_gdk_color
| Uninstantiated -> !proved_incomplete_gdk_color
in
drawable#set_foreground (`COLOR color);
res
| Cheated ->
let res = save_gc drawable in
drawable#set_foreground (`COLOR !cheated_gdk_color);
res
(*****************************************************************************)
(*****************************************************************************)
(** {3 Double linked trees} *)
(*****************************************************************************)
(*****************************************************************************)
(** The proof trees in the proof-tree display are organized as
doubly-linked trees, where children contain a link to their parent
nodes. This is needed, because, for efficiency, the tree layout
computation starts at the last inserted child and walks upwards to
the root of the tree.
*)
(** Abstract base class for doubly linked trees. Because of
type-checking problems the functionality for setting and clearing
children nodes is not inside the class but outside, in the
functions {!Draw_tree.set_children} and
{!Draw_tree.clear_children}.
*)
class virtual ['a] doubly_linked_tree =
object
(** The parent link. *)
val mutable parent = None
(** The childrens list. *)
val mutable children = []
(** Accessor method for the parent field. *)
method parent = parent
(** Low-level setter for the {!parent} field. To insert child nodes
into the tree, use {!Draw_tree.set_children}.
*)
method set_parent (p : 'a) = parent <- Some p
(** Another low-level setter for the parent field. To delete nodes
from the tree, use {!Draw_tree.clear_children} on the parent.
*)
method clear_parent = parent <- None
(** Accessor for the children field. *)
method children = children
(** Low-level setter for the children field. To insert child nodes
into the tree, use {!Draw_tree.set_children}.
*)
method set_children (cs : 'a list) =
children <- cs
(** Method to be called when the children list has been changed. *)
method virtual children_changed : unit
end
(** [set_children parent children] correctly insert [children] into
the doubly linked tree as children of node [parent]. After the
change {!doubly_linked_tree.children_changed} is called on
[parent]. Asserts that the children list of [parent] is empty.
*)
let set_children parent children =
assert(parent#children = []);
parent#set_children children;
List.iter (fun c -> c#set_parent parent) children;
parent#children_changed
(** [clear_children parent] removes all children from [parent] from
the doubly linked tree. After the change
{!doubly_linked_tree.children_changed} is called on [parent].
*)
let clear_children parent =
List.iter (fun c -> c#clear_parent) parent#children;
parent#set_children [];
parent#children_changed
(*
* let add_child parent child =
* parent#set_children (parent#children @ [child]);
* child#set_parent parent;
* parent#children_changed
*)
(*
* let remove_child child =
* match child#parent with
* | None -> ()
* | Some p ->
* p#set_children (List.filter (fun c -> c <> child) p#children);
* child#clear_parent;
* p#children_changed
*)
(*****************************************************************************)
(*****************************************************************************)
(** {3 Tree layer interface} *)
(*****************************************************************************)
(*****************************************************************************)
(** Abstract interface for {!class: Tree_layers.tree_layer} and
{!class: Tree_layers.tree_layer_stack}. Root nodes of proof trees and
layers contain a pointer to the layer or layer stack containing
them. This pointer is used to invalidate the size information in
these structures and to query location information. This class
type breaks the mutual dependency between root nodes and layers
and layers and the layer stack. The type parameter stands for the
structure containing the upward pointer, because it passes [self]
as first argument to {!child_offsets}.
*)
class type ['a] abstract_tree_container =
object
(** Invalidate the size information in this container and all bigger
structures containing it.
*)
method clear_size_cache : unit
(** Compute the left and top offset of this container relative to
the upper-left corner of the complete display.
*)
method left_top_offset : int * int
(** Compute the x and y offset of one child relative to the upper
left corner of this container.
*)
method child_offsets : 'a -> int * int
end
(*****************************************************************************)
(*****************************************************************************)
(** {3 External window interface} *)
(*****************************************************************************)
(*****************************************************************************)
(** Abstract class type for external {!class: Node_window.node_window}'s
containing just those methods that are needed here. This class
type is used to break the circular dependency between {!Draw_tree}
and {!Node_window}. All {!proof_tree_element}'s keep a list of
their external windows to update them. External node windows have
a pointer to proof-tree elements to deregister themselves when
they get deleted or orphaned. Before external node windows are
passed to functions in this module, they must be cast to this
class type.
*)
class type external_node_window =
object
(** Number of this node window. Used to correlate node windows with
the proof-tree display.
*)
method window_number : string
(** Update the content in the text buffer of this node window. The
argument is the updated {!proof_tree_element.sequent_text_history}.
*)
method update_content : string list -> unit
(** Reconfigure and redraw the node window. Needs to be called when
the configuration has been changed. Actually only the font of
the buffer text is changed.
*)
method configuration_updated : unit
(** Delete this node window if it is not sticky. Needs to be called
when the corresponding element in the proof-tree display is
deleted.
*)
method delete_attached_node_window : unit
end
(*****************************************************************************)
(*****************************************************************************)
(** {2 Generic tree element} *)
(*****************************************************************************)
(*****************************************************************************)
(** Abstract base class for turnstiles and proof commands. Contains
the code for (relativ) layout, (absolute) coordinates, locating
mouse button clicks, marking branches and the general drawing
functions.
Argument undo_state saves the undo state for the current proof.
It's value is arbitrary for cloned proof trees.
*)
class virtual proof_tree_element drawable
undo_state debug_name new_evars inst_evars =
object (self)
inherit [proof_tree_element] doubly_linked_tree
(***************************************************************************)
(***************************************************************************)
(** {2 Internal State Fields} *)
(***************************************************************************)
(***************************************************************************)
(** ID for debugging purposes *)
method debug_name = (debug_name : string)
(** The kind of this element. *)
method virtual node_kind : node_kind
(** The existentials created for this element. Only non-nil when
this is a proof command.
*)
method fresh_existentials = new_evars
(** The existentials instantiated by this element. Only non-nil when
this is a proof command.
*)
method inst_existentials : existential_variable list = inst_evars
(** Return the state for this sequent. *)
method undo_state = (undo_state : int)
(** The {!class: Gtk_ext.better_drawable} into which this element
draws itself.
*)
val drawable = drawable
(***************** inside proof_tree_element *)
(** The width of this node alone in pixels. Set in the initializer
of the heirs. *)
val mutable width = 0
(** The height of this node alone in pixels. Set in the initializer
of the heirs. *)
val mutable height = 0
(** The total width in pixels of the subtree which has this node as
root. Computed in
{!Draw_tree.proof_tree_element.update_subtree_size}. *)
val mutable subtree_width = 0
(** The x-offset of the left border of the first child. Or, in other
words, the distance (in pixels) between the left border of the
subtree which has this node as root and the the left border of
the subtree which has the first child as root. Always
non-negative. Zero if this node has no children. Usually zero,
non-zero only in unusual cases, for instance if the {!width} of
this node is larger than the total width of all children.
*)
val mutable first_child_offset = 0
(** The x-offset of the centre of this node. In other words the
distance (in pixels) between the left border of this node's
subtree and the x-coordinate of this node.
*)
val mutable x_offset = 0
(***************** inside proof_tree_element *)
(** The height of this nodes subtree, counted in tree levels. At
least 1, because this element occupies already some level.
*)
val mutable subtree_levels = 0
(** The proof state of this node. *)
val mutable branch_state = Unproven
(** [true] if this node is selected and displayed in the sequent
area of the proof-tree window.
*)
val mutable selected = false
(** The list of external node windows. *)
val mutable external_windows : external_node_window list = []
(** The set of all existentials for this node. *)
val mutable existential_variables = new_evars
(** Upward pointer to the layer containing this proof tree. Must be
set for root nodes.
*)
val mutable tree_layer =
(None : proof_tree_element abstract_tree_container option)
(** This field is really used only inside {!turnstile}. In a
turnstile element, it holds the list of all previous versions of
the sequent text without existential information, except for the
head, with contains the current sequent {b with} existential
info. For uniform treatment of external node windows, the
field is also used for proof commands. There, it just holds
one element, the proof command with existentials info.
This field holds the empty list for additionally spawned subgoals
for which the first update sequent command with their initial sequent
text has not arrived yet. These sequents are called incomplete.
Complete sequents can also receive update sequent commands, but these
only update the sequent text with respect to instantiated existential
variables. The distinction between complete/incomplete sequents is
necessary, because for an update sequent command for a complete sequent,
some actions on existential variables are not necessary any more. See
{Proof_tree.update_sequent_element}.
XXX reconsider including existential info everywhere, because
Coq delivers the information now, I believe.
The existential info is omitted from old versions of the sequent
text, because this info is incorrect for sequents that get
updated. The problem is that the exisitentials change already
with {!Proof_tree.add_new_goal}, which happens long before
{!Proof_tree.update_sequent}. A fix for this would require a
protocol change, which is a bit too much for this little
feature.
*)
val mutable sequent_text_history = []
(***************************************************************************)
(***************************************************************************)
(** {2 Accessors / Setters} *)
(***************************************************************************)
(***************************************************************************)
(***************** inside proof_tree_element *)
(** Accessor method of {!attribute: width}. *)
method width = width
(** Accessor method of {!attribute: height}. *)
method height = height
(** Accessor method of {!attribute: subtree_width}. *)
method subtree_width = subtree_width
(** Accessor method of {!attribute: subtree_levels}. *)
method subtree_levels = subtree_levels
(** Accessor method of {!attribute: x_offset}. *)
method x_offset = x_offset
(** Accessor method of {!attribute: branch_state}. *)
method branch_state = branch_state
(** Modification method of {!attribute: branch_state}. *)
method set_branch_state s = branch_state <- s
(** Accessor method of {!attribute: selected}. *)
method is_selected = selected
(** Modification method of {!attribute: selected}. *)
method selected b = selected <- b
(***************** inside proof_tree_element *)
(** Accessor method of {!attribute: existential_variables}. *)
method existential_variables = existential_variables
(** [inherit_existentials exl] sets this nodes {!attribute:
existential_variables} as union of {!new_evars} and
[exl].
*)
method inherit_existentials existentials =
existential_variables <- List.rev_append new_evars existentials
(** The original text content associated with this element. For
turnstiles this is the sequent text and for proof commands this is the
complete proof command.
*)
method virtual content : string
(** [true] if the proof command is abbreviated in the display.
Always [false] for turnstiles. Used to decide whether to display
tooltips for proof commands.
*)
method virtual content_shortened : bool
(** Return the sequent ID for turnstiles and the empty string for
proof commands. For turnstiles the sequent ID is used as
{!debug_name}. *)
method virtual id : string
(** Register the proof tree layer containing this root node. *)
method register_tree_layer tl =
assert(tree_layer = None);
tree_layer <- Some tl
(** Make {!sequent_text_history} accessible for cloning and for
reattaching external node windows.
*)
method sequent_text_history = sequent_text_history
(** This method is only used inside {!turnstile} but declared here to
avoid downcasting during cloning. Set the sequent text history,
used when cloning. *)
method set_sequent_text_history history =
sequent_text_history <- history
(***************************************************************************)
(***************************************************************************)
(** {2 Children Iterators} *)
(***************************************************************************)
(***************************************************************************)
(***************** inside proof_tree_element *)
(** General iterator for all children. [iter_children left y a f]
successively computes the [left] and [y] value of each child and
calls [f left y c a] for each child [c] (starting with the
leftmost child) until [f] returns [false]. The [a] value is an
accumulator. The returned [a] is passed to the invocation of [f]
for the next child. The last returned [a] is the result of the
total call of this function.
*)
method private iter_children :
'a . int -> int -> 'a ->
(int -> int -> 'a -> proof_tree_element -> ('a * bool)) -> 'a =
fun left y a f ->
let left = left + first_child_offset in
let y = y + !current_config.level_distance in
let rec doit left a = function
| [] -> a
| c::cs ->
let (na, cont) = f left y a c in
if cont
then doit (left + c#subtree_width) na cs
else na
in
doit left a children
(** Unit iterator for all children. Calls [f left y c] for each
child [c]. *)
method private iter_all_children_unit left y
(f : int -> int -> proof_tree_element -> unit) =
self#iter_children left y ()
(fun left y () c -> f left y c; ((), true))
(***************************************************************************)
(***************************************************************************)
(** {2 Layout and Size Computation} *)
(***************************************************************************)
(***************************************************************************)
(***************** inside proof_tree_element *)
(** Compute the height of the subtree of this element in pixels. *)
method subtree_height =
(subtree_levels - 1) * !current_config.level_distance +
2 * !current_config.turnstile_radius +
2 * !current_config.turnstile_line_width
(** Sets the {!width} and {!height} fields. Called in the
initializer of the heirs and when the configuration has been
updated.
*)
method private virtual set_node_size : unit
(** (Re-)compute all (relative) layout information for this node.
Computes and sets {!attribute: subtree_levels}, {!attribute:
subtree_width}, {!attribute: x_offset} and
{!first_child_offset}. *)
method private update_subtree_size =
let (children_width, max_levels, last_child) =
List.fold_left
(fun (sum_width, max_levels, _last_child) c ->
(*
* (if parent = None || (match parent with Some p -> p#parent = None)
* then Printf.fprintf (debugc())
* "USS child width %d\n%!" c#subtree_width);
*)
(sum_width + c#subtree_width,
(if c#subtree_levels > max_levels
then c#subtree_levels
else max_levels),
Some c))
(0, 0, None)
children
in
(***************** inside proof_tree_element *)
subtree_levels <- max_levels + 1;
subtree_width <- children_width;
x_offset <-
(match children with
| [] -> 0
| [c] -> c#x_offset
| first :: _ -> match last_child with
| None -> assert false
| Some last ->
let last_x_offset =
subtree_width - last#subtree_width + last#x_offset
in
(first#x_offset + last_x_offset) / 2
);
(*
* Printf.fprintf (debugc())
* "USS %s childrens width %d first x_offset %d\n%!"
* self#debug_name
* children_width
* x_offset;
*)
(* Now x_offset is nicely in the middle of all children nodes and
* subtree_width holds the width of all children nodes.
* However, the width of this node might be larger than all the
* children together, or it may be placed asymmetrically. In both
* cases it can happen that some part of this node is outside the
* boundaries of all the children. In this case we must increase
* the width of subtree and adjust the x_offset.
*)
if x_offset < width / 2
then begin
(* part of this node is left of leftmost child *)
first_child_offset <- width / 2 - x_offset;
x_offset <- x_offset + first_child_offset;
subtree_width <- subtree_width + first_child_offset;
end else begin
(* this node's left side is right of the left margin of the first child *)
first_child_offset <- 0;
end;
(***************** inside proof_tree_element *)
(* The real condition for the next if is
* subtree_width - x_offset < width / 2
* but it has rounding issues when width is odd.
*)
if 2 * (subtree_width - x_offset) < width
then begin
(* Part of this node is right of rightmost child.
* Need to increase subtree_width about the outside part,
* which is width / 2 - (subtree_width - x_offset).
* Now
* subtree_width + width / 2 - (subtree_width - x_offset) =
* x_offset + width / 2
*)
subtree_width <- x_offset + (width + 1) / 2;
end else begin
(* This node's right side is left of right margin of last child.
* Nothing to do.
*)
end;
(*
* Printf.fprintf (debugc())
* "USS %s END subtree width %d x_offset %d \
* first_child_offset %d height %d\n%!"
* self#debug_name
* subtree_width
* x_offset
* first_child_offset
* subtree_levels;
*)
(***************** inside proof_tree_element *)
(** Do {!update_subtree_size} in this element and all parent
elements up to the root of the tree.
*)
method update_sizes_in_branch =
(*
* let old_subtree_width = subtree_width in
* let old_x_offset = x_offset in
*)
self#update_subtree_size;
(*
* if x_offset <> old_x_offset || subtree_width <> old_subtree_width
* then
*)
match parent with
| None ->
(match tree_layer with
| None ->
(* during bottom-up clone copy there is no parent and the
* tree_layer will be installed later
*)
()
| Some sco -> sco#clear_size_cache
)
| Some p -> p#update_sizes_in_branch
(***************************************************************************)
(***************************************************************************)
(** {2 Coordinates} *)
(***************************************************************************)
(***************************************************************************)
(***************** inside proof_tree_element *)
(** Computes the left offset of [child] relative to the bounding box
of its parent, which must be this node. *)
method child_offset child =
self#iter_children 0 0 0 (fun left _y _a oc -> (left, child <> oc))
(** Computes the pair [(left_off, y_off)]. [left_off] is the offset
of the left hand side of the bounding box of this node's
subtree. [y_off] is the offset of the y-coordinate of this node.
The offsets are relative to the left and top of the layer stack,
respectively.
*)
method left_y_offsets =
match parent with
| None ->
(match tree_layer with
| None -> assert false
| Some tl ->
let (tl_left, tl_top) = tl#left_top_offset in
let (me_left, me_top) =
tl#child_offsets (self :> proof_tree_element) in
(tl_left + me_left, tl_top + me_top + height / 2)
)
| Some p ->
let (parent_left, parent_y) = p#left_y_offsets in
let y_off = parent_y + !current_config.level_distance in
let left_off =
parent_left + p#child_offset (self :> proof_tree_element)
in
(left_off, y_off)
(***************** inside proof_tree_element *)
(** Computes the bounding box (that is a 4-tuple [(x_low, x_high,
y_low, y_high)]) relative to the upper-left corner of the
complete display.
*)
method bounding_box_offsets =
let (left, y) = self#left_y_offsets in
let x = self#get_x_coordinate left in
(*
* Printf.fprintf (debugc())
* "BBO %s\n%!"
* self#debug_name;
*)
(*
* Printf.fprintf (debugc())
* "BBO left %d width %d height %d | x %d-%d y %d-%d\n%!"
* left width height
* left (left + width) (y - height / 2) (y + height / 2);
*)
(x - width / 2, x + width / 2, y - height / 2, y + height / 2)
(** [bounding_box left top] computes the bounding box (that is a
4-tuple [(x_low, x_high, y_low, y_high)]) of this node in
absolute values as floats. Arguments [left] and [top] specify
the upper left corner of the root node of the proof tree.
*)
method bounding_box left top =
let (x_l, x_u, y_l, y_u) = self#bounding_box_offsets in
(float_of_int (x_l + left),
float_of_int (x_u + left),
float_of_int (y_l + top),
float_of_int (y_u + top))
(***************** inside proof_tree_element *)
(** Computes the x-coordinate of this node. Argument [left] must be
the x-coordinate of the left side of the bounding box of this
node's subtree.
*)
method get_x_coordinate left = left + x_offset
(***************************************************************************)
(***************************************************************************)
(** {2 Drawing} *)
(***************************************************************************)
(***************************************************************************)
(** Draw just this element (without connecting lines) at the
indicated position. First argument [left] is the left border,
second argument [y] is the y-coordinate.
*)
method private virtual draw : int -> int -> unit
(** [line_offset inverse_slope] computes the start offset (as
[(x_off, y_off)]) for drawing a line that start or ends in this
node with inverse slope [inverse_slope]. These offsets are
needed to avoid overdrawing elements with connecting lines. The
parameter is the inverse slope, because it is always defined,
because we never draw horizontal lines. Vertical lines do
appear, for them the real slope is infinite.
*)
method virtual line_offset : float -> (int * int)
(***************** inside proof_tree_element *)
(** Draw the lines from this node to all its children.
@param left x-coordinate of the left side of the bounding box of
this node's subtree
@param y y-coordinate of this node
*)
method private draw_lines left y =
let x = self#get_x_coordinate left in
self#iter_all_children_unit left y
(fun left cy child ->
let cx = child#get_x_coordinate left in
let slope = float_of_int(cx - x) /. float_of_int(cy - y) in
let (d_x, d_y) = self#line_offset slope in
let (c_d_x, c_d_y) = child#line_offset slope in
let gc_opt =
save_and_set_gc drawable
child#branch_state child#existential_variables
in
drawable#line ~x:(x + d_x) ~y:(y + d_y)
~x:(cx - c_d_x) ~y:(cy - c_d_y);
restore_gc drawable gc_opt)
(***************** inside proof_tree_element *)
(** Draw this element's subtree given the left side of the bounding box
and the y-coordinate of this node. This is the internal draw method
that iterates through the tree.
@param left x-coordinate of the left side of the bounding box of