Debian Codegraph @

test/polymorphic_variants.ml

raw symbols SCIP
1 type a = [ `A ] [@@deriving hash]
2
3 type ab =
4 [ `A
5 | `B
6 ]
7 [@@deriving hash]
8
9 type abc_v1 =
10 [ ab
11 | `C
12 ]
13 [@@deriving hash]
14
15 type abc_v2 =
16 [ `A
17 | `B
18 | `C
19 ]
20 [@@deriving hash]
21
22 (* Check that the same polymorphic variant has the same hash regardless of which
23 type it's in. This property is relied on internally to implement hashing of types like
24 [abc_v1] (see above).
25 We could potentially use this property to justify support of open polymorphic
26 variants in the future. *)
27 let%expect_test "`A hashes the same regardless of which type it's in" =
28 let h1 = [%hash: a] `A in
29 let h2 = [%hash: ab] `A in
30 let h3 = [%hash: abc_v1] `A in
31 let h4 = [%hash: abc_v2] `A in
32 let h5 = [%hash: [ ab | `C ]] `A in
33 assert (h1 = h2);
34 assert (h2 = h3);
35 assert (h3 = h4);
36 assert (h4 = h5)
37 ;;
38
39 let%expect_test "Up to polymorphic variant functions behave exactly the same as fully \
40 bounded polymorphic variant functions"
41 =
42 let module H : sig
43 val h1 : [%hash: [ `A | `B ]]
44 val h2 : [%hash: [< `A | `B ]]
45 val h3 : [%hash: [< `A | `B > `A ]]
46 end = struct
47 let h1 = [%hash: [ `A | `B ]]
48 let h2 = [%hash: [< `A | `B ]]
49 let h3 = [%hash: [< `A | `B > `A ]]
50 end
51 in
52 let open H in
53 assert (h1 `A = h2 `A);
54 assert (h1 `A = h3 `A);
55 assert (h1 `B = h2 `B);
56 assert (h1 `B = h3 `B)
57 ;;