Skip to content

Commit 7c62ec1

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 79544cf commit 7c62ec1

File tree

15 files changed

+209
-12
lines changed

15 files changed

+209
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154

155155
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
156156

157+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
158+
157159
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
158160

159161
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -377,6 +379,8 @@ Nothing else to report in this section
377379

378380
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
379381

382+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
383+
380384
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
381385

382386
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m
@@ -463,6 +467,10 @@ Nothing else to report in this section
463467
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
464468
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
465469

470+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
471+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
472+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
473+
466474
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
467475
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias
468476

check/threshold-1/threshold-1.ref

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159

160160
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
161161

162+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
163+
162164
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
163165

164166
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -382,6 +384,8 @@ Nothing else to report in this section
382384

383385
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
384386

387+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
388+
385389
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
386390
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected
387391
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected
@@ -468,6 +472,10 @@ Nothing else to report in this section
468472
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
469473
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
470474

475+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
476+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
477+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
478+
471479
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
472480
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Not detected
473481
./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used
@@ -1025,7 +1033,7 @@ Nothing else to report in this section
10251033
--------------------------------------------------------------------------------
10261034

10271035

1028-
Total: 870
1029-
Success: 862
1036+
Total: 875
1037+
Success: 867
10301038
Failed: 8
1031-
Ratio: 99.0804597701%
1039+
Ratio: 99.0857142857%

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154

155155
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
156156

157+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
158+
157159
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
158160

159161
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -515,6 +517,8 @@ Nothing else to report in this section
515517

516518
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
517519

520+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
521+
518522
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
519523

520524
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m
@@ -601,6 +605,10 @@ Nothing else to report in this section
601605
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
602606
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
603607

608+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
609+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
610+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
611+
604612
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
605613
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias
606614

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159

160160
./examples/docs/methods/code_constructs/object_type/object_type_bin.ml:4: push_n_times
161161

162+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times
163+
162164
./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias
163165

164166
./examples/using_dune/bin/use_preprocessed_lib/use_preprocessed.mli:1: mark_used
@@ -520,6 +522,8 @@ Nothing else to report in this section
520522

521523
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#unused
522524

525+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
526+
523527
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused
524528
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected
525529
./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected
@@ -606,6 +610,10 @@ Nothing else to report in this section
606610
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used
607611
./examples/docs/methods/code_constructs/inheritance/inheritance_lib.mli:2: parent#used_by_child
608612

613+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#peek
614+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop
615+
./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push
616+
609617
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used
610618
./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Not detected
611619
./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used
@@ -1424,7 +1432,7 @@ Nothing else to report in this section
14241432
--------------------------------------------------------------------------------
14251433

14261434

1427-
Total: 1187
1428-
Success: 1179
1435+
Total: 1192
1436+
Success: 1184
14291437
Failed: 8
1430-
Ratio: 99.3260320135%
1438+
Ratio: 99.3288590604%

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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 :
30+
object
31+
method push : 'a -> unit
32+
method pop : unit
33+
method peek : 'a option
34+
method reset : unit
35+
end
36+
```
37+
```OCaml
38+
(* polymorphic_class_lib.ml *)
39+
class ['a] stack =
40+
object
41+
val mutable l : 'a list = []
42+
method push x = l <- x::l
43+
method pop =
44+
match l with
45+
| [] -> ()
46+
| _::tl -> l <- tl
47+
method peek =
48+
match l with
49+
| [] -> None
50+
| hd::_ -> Some hd
51+
method reset = l <- []
52+
end
53+
```
54+
```OCaml
55+
(* polymorphic_class_bin.ml *)
56+
let push_n_times n stack =
57+
for i = 1 to n do
58+
stack#push i;
59+
done
60+
61+
let () =
62+
let int_stack = new Polymorphic_class_lib.stack in
63+
let n = 42 in
64+
push_n_times n int_stack;
65+
while int_stack#peek <> None do
66+
int_stack#pop;
67+
done
68+
```
69+
70+
Before looking at the analysis results, let's look at the code.
71+
72+
This example is very similar to the [Class](./CLASS.md) example, without the
73+
`unused_class` class. Instead of using an definitive `int_stack` class, the
74+
`Polymorphic_class_lib` defines a polymorphic `['a] stack` class.
75+
76+
Methods `push`, `peek`, and `pop` are used, leaving `reset` as the only unused method.
77+
78+
Compile and analyze:
79+
```
80+
$ make -C polymorphic_class/
81+
make: Entering directory '/tmp/docs/methods/code_constructs/polymorphic_class'
82+
ocamlopt -bin-annot polymorphic_class_lib.mli polymorphic_class_lib.ml polymorphic_class_bin.ml
83+
dead_code_analyzer --nothing -M all .
84+
Scanning files...
85+
[DONE]
86+
87+
.> UNUSED METHODS:
88+
=================
89+
/tmp/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset
90+
91+
Nothing else to report in this section
92+
--------------------------------------------------------------------------------
93+
94+
95+
make: Leaving directory '/tmp/docs/methods/code_constructs/polymorphic_class'
96+
```
97+
98+
As expected, the analyzer reports `stack#reset` as unused.
99+
100+
> [!NOTE]
101+
> The analyzer does not specify the type parameter of `stack`. This is because
102+
> it does not distinguish the monomorphizations of `stack` and its polymoprhic
103+
> definition.
104+
105+
After removing the reported unused methods, the analyzer will not find anymore
106+
unused method. Our work here is done.

0 commit comments

Comments
 (0)