Skip to content

Commit 29c9897

Browse files
committed
[docs][methods][13/n] add polymorphic class example
This is the same as the class example with a polymorphic stack and without the unnecessary classes.
1 parent 5340041 commit 29c9897

File tree

15 files changed

+223
-12
lines changed

15 files changed

+223
-12
lines changed

check/classic/classic.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ Nothing else to report in this section
169169

170170
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
171171

172+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
173+
172174
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
173175

174176
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m

check/classic/classic.ref

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ Nothing else to report in this section
174174

175175
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
176176

177+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
178+
177179
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
178180
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected
179181
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected
@@ -654,7 +656,7 @@ Nothing else to report in this section
654656
--------------------------------------------------------------------------------
655657

656658

657-
Total: 556
658-
Success: 549
659+
Total: 557
660+
Success: 550
659661
Failed: 7
660-
Ratio: 98.7410071942%
662+
Ratio: 98.7432675045%

check/internal/internal.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ Nothing else to report in this section
124124

125125
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
126126

127+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
128+
127129
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
128130

129131
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m

check/internal/internal.ref

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ Nothing else to report in this section
129129

130130
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
131131

132+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
133+
132134
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
133135
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected
134136
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected
@@ -609,7 +611,7 @@ Nothing else to report in this section
609611
--------------------------------------------------------------------------------
610612

611613

612-
Total: 514
613-
Success: 507
614+
Total: 515
615+
Success: 508
614616
Failed: 7
615-
Ratio: 98.6381322957%
617+
Ratio: 98.640776699%

check/threshold-1/threshold-1.exp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@
160160
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
161161
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:10: clear_stack
162162

163+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
164+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:7: clear_stack
165+
163166
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
164167

165168
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -383,6 +386,8 @@ Nothing else to report in this section
383386

384387
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
385388

389+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
390+
386391
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
387392

388393
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m
@@ -470,6 +475,10 @@ Nothing else to report in this section
470475
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
471476
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
472477

478+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
479+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
480+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
481+
473482
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
474483
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias
475484

check/threshold-1/threshold-1.ref

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@
165165
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
166166
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:10: clear_stack
167167

168+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
169+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:7: clear_stack
170+
168171
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
169172

170173
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -388,6 +391,8 @@ Nothing else to report in this section
388391

389392
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
390393

394+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
395+
391396
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
392397
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected
393398
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected
@@ -475,6 +480,10 @@ Nothing else to report in this section
475480
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
476481
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
477482

483+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
484+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
485+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
486+
478487
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
479488
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Not detected
480489
./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used
@@ -1032,7 +1041,7 @@ Nothing else to report in this section
10321041
--------------------------------------------------------------------------------
10331042

10341043

1035-
Total: 877
1036-
Success: 869
1044+
Total: 883
1045+
Success: 875
10371046
Failed: 8
1038-
Ratio: 99.0877993158%
1047+
Ratio: 99.093997735%

check/threshold-3-0.5/threshold-3-0.5.exp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@
160160
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
161161
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:10: clear_stack
162162

163+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
164+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:7: clear_stack
165+
163166
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
164167

165168
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -525,6 +528,8 @@ Nothing else to report in this section
525528

526529
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
527530

531+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
532+
528533
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
529534

530535
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m
@@ -612,6 +617,10 @@ Nothing else to report in this section
612617
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
613618
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
614619

620+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
621+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
622+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
623+
615624
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
616625
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias
617626

check/threshold-3-0.5/threshold-3-0.5.ref

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@
165165
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
166166
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:10: clear_stack
167167

168+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
169+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:7: clear_stack
170+
168171
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
169172

170173
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -530,6 +533,8 @@ Nothing else to report in this section
530533

531534
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
532535

536+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
537+
533538
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
534539
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected
535540
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected
@@ -617,6 +622,10 @@ Nothing else to report in this section
617622
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
618623
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
619624

625+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
626+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
627+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
628+
620629
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
621630
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Not detected
622631
./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used
@@ -1447,7 +1456,7 @@ Nothing else to report in this section
14471456
--------------------------------------------------------------------------------
14481457

14491458

1450-
Total: 1205
1451-
Success: 1197
1459+
Total: 1211
1460+
Success: 1203
14521461
Failed: 8
1453-
Ratio: 99.3360995851%
1462+
Ratio: 99.3393889348%

docs/methods/METHODS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ from the `.mli` if there is one and the `.ml`.
142142
- The [code constructs](./code_constructs) directory contains a collection of
143143
examples dedicated to specific code constructs :
144144
- [Class](./code_constructs/CLASS.md)
145+
- [Polymorphic class](./code_constructs/CLASS.md)
145146
- [Constructor](./code_constructs/CONSTRUCTOR.md)
146147
- [Class type](./code_constructs/CLASS_TYPE.md)
147148
- [Inheritance](./code_constructs/INHERITANCE.md)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
The reference files for this example are in the
2+
[polymorphic\_class](../../../examples/docs/methods/code_constructs/polymorphic_class) directory.
3+
4+
The reference takes place in `/tmp/docs/methods/code_constructs`, which
5+
is a copy of the [code\_constructs](../../../examples/docs/methods/code_constructs)
6+
directory. Reported locations may differ depending on the location of the source
7+
files.
8+
9+
The compilation command is :
10+
```
11+
make -C polymorphic_class build
12+
```
13+
14+
The analysis command is :
15+
```
16+
make -C polymorphic_class analyze
17+
```
18+
19+
The compile + analyze command is :
20+
```
21+
make -C polymorphic_class
22+
```
23+
24+
## First run
25+
26+
Code:
27+
```OCaml
28+
(* polymorphic_class_lib.mli *)
29+
class ['a] stack : object
30+
method push : 'a -> unit
31+
method pop : unit
32+
method peek : 'a option
33+
method reset : unit
34+
end
35+
```
36+
```OCaml
37+
(* polymorphic_class_lib.ml *)
38+
class ['a] stack = object
39+
val mutable l : 'a list = []
40+
41+
method push x = l <- x::l
42+
43+
method pop =
44+
match l with
45+
| [] -> ()
46+
| _::tl -> l <- tl
47+
48+
method peek =
49+
match l with
50+
| [] -> None
51+
| hd::_ -> Some hd
52+
53+
method reset = l <- []
54+
end
55+
```
56+
```OCaml
57+
(* polymorphic_class_bin.ml *)
58+
let push_n_times n stack =
59+
for i = 1 to n do
60+
stack#push i;
61+
done
62+
63+
let clear_stack stack =
64+
while stack#peek <> None do
65+
stack#pop;
66+
done
67+
68+
let () =
69+
let int_stack = new Polymorphic_class_lib.stack in
70+
let n = 42 in
71+
push_n_times n int_stack;
72+
clear_stack int_stack
73+
```
74+
75+
Before looking at the analysis results, let's look at the code.
76+
77+
This example is very similar to the [Class](./CLASS.md) example, without the
78+
`counter` and `unused_class` classes. Instead of using an definitive `int_stack`
79+
class, the `Polymorphic_class_lib` defines a polymorphic `['a] stack` class.
80+
81+
Methods `push`, `peek`, and `pop` are used, leaving `reset` as the only unused method.
82+
83+
Compile and analyze:
84+
```
85+
$ make -C polymorphic_class/
86+
make: Entering directory '/tmp/docs/methods/code_constructs/polymorphic_class'
87+
ocamlopt -bin-annot polymorphic_class_lib.mli polymorphic_class_lib.ml polymorphic_class_bin.ml
88+
dead_code_analyzer --nothing -M all .
89+
Scanning files...
90+
[DONE]
91+
92+
.> UNUSED METHODS:
93+
=================
94+
/tmp/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
95+
96+
Nothing else to report in this section
97+
--------------------------------------------------------------------------------
98+
99+
100+
make: Leaving directory '/tmp/docs/methods/code_constructs/polymorphic_class'
101+
```
102+
103+
As expected, the analyzer reports `stack#reset` as unused.
104+
105+
> [!NOTE]
106+
> The analyzer does not specify the type parameter of `stack`. This is because
107+
> it does not distinguish the monomorphizations of `stack` and its polymoprhic
108+
> definition.
109+
110+
After removing the reported unused methods, the analyzer will not find anymore
111+
unused method. Our work here is done.

0 commit comments

Comments
 (0)