Skip to content

Commit 02cd216

Browse files
committed
Rewrote set tutorial from scratch.
This is intended to address the issue mentioned in ocaml/ocaml.org#598 This version of the tutorial now demonstrates how to use Set with arbitrary types. It also provides a demonstration on how to reason about the behavior of functions based on their type signatures.
1 parent 1ce0c62 commit 02cd216

File tree

1 file changed

+217
-39
lines changed

1 file changed

+217
-39
lines changed

site/learn/tutorials/set.md

+217-39
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,252 @@
33

44
# Set
55

6-
## Module Set
7-
To make a set of strings:
6+
`Set` is a functor, which means that it is a module that is parameterized
7+
by another module. More concretely, this means you cannot directly create
8+
a set; instead, you must first specify what type of elements your set will
9+
contain.
10+
11+
The `Set` functor provides a function `Make` which accepts a module as a
12+
parameter, and returns a new module representing a set whose elements have
13+
the type that you passed in. For example, if you want to work with sets of
14+
strings, you can invoke `Set.Make(String)` which will return you a new module
15+
which you can assign the name `SS` (short for "String Set"). Note: Be sure to
16+
pay attention to the case; you need to type `Set.Make(String)` and not
17+
`Set.Make(string)`. The reason behind this is explained in the
18+
"Technical Details" section at the bottom.
19+
20+
Doing this in the OCaml's top level will yield a lot of output:
821

922
```ocamltop
1023
module SS = Set.Make(String);;
1124
```
12-
To create a set you need to start somewhere so here is the empty set:
25+
26+
What happened here is that after assigning your newly created module to the name
27+
`SS`, OCaml's top level then displayed the module, which in this case contains
28+
a large number of convenience functions for working with sets (for example `is_empty`
29+
for checking if you set is empty, `add` to add an element to your set, `remove` to
30+
remove an element from your set, and so on).
31+
32+
Note also that this module defines two types: `type elt = String.t` representing
33+
the type of the elements, and `type t = Set.Make(String).t` representing the type of
34+
the set itself. It's important to note this, because these types are used in the
35+
signatures of many of the functions defined in this module.
36+
37+
For example, the `add` function has the signature `elt -> t -> t`, which means
38+
that it expects an element (a String), and a set of strings, and will return to you
39+
a set of strings. As you gain more experience in OCaml and other function languages,
40+
the type signature of functions are often the most convenient form of documentation
41+
on how to use those functions.
42+
43+
## Creating a Set
44+
45+
You've created your module representing a set of strings, but now you actually want
46+
to create an instance of a set of strings. So how do we go about doing this? Well, you
47+
could search through the documentation for the original `Set` functor to try and
48+
find what function or value you should use to do this, but this is an excellent
49+
opportunity to practice reading the type signatures and inferring the answer from them.
50+
51+
You want to create a new set (as opposed to modifying an existing set). So you should
52+
look for functions whose return result has type `t` (the type representing the set),
53+
and which *does not* require a parameter of type `t`.
54+
55+
Skimming through the list of functions in the module, there's only a handful of functions
56+
that match that criteria: `empty: t`, `singleton : elt -> t`, `of_list : elt list -> t`
57+
and `of_seq : elt Seq.t -> t`.
58+
59+
Perhaps you already know how to work with lists and sequences in OCaml or
60+
perhaps you don't. For now, let's assume you don't know, and so we'll focus
61+
our attention on the first two functions in that list: `empty` and `singleton`.
62+
63+
The type signature for `empty` says that it simply returns `t`, i.e. an instance
64+
of our set, without requiring any parameters at all. By intuition, you might
65+
guess that the only reasonable set that a library function could return when
66+
given zero parameters is the empty set. And the fact that the function is named
67+
`empty` reinforces this theory.
68+
69+
Is there a way to test this theory? Perhaps if we had a function which
70+
could print out the size of a set, then we could check if the set we get
71+
from `empty` has a size of zero. In other words, we want a function which
72+
receives a set as a parameter, and returns an integer as a result. Again,
73+
skimming through the list of functions in the module, we see there is a
74+
function which matches this signature: `cardinal : t -> int`. If you're
75+
not familiar with the word "cardinal", you can look it up on Wikipedia
76+
and notice that it basically refers to the size of sets, so this reinforces
77+
the idea that this is exactly the function we want.
78+
79+
So let's test our hypothesis:
1380

1481
```ocamltop
1582
let s = SS.empty;;
83+
SS.cardinal s;;
1684
```
17-
Alternatively if we know an element to start with we can create a set
18-
like
85+
86+
Excellent, it looks like `SS.empty` does indeed create an empty set,
87+
and `SS.cardinal` does indeed print out the size of a set.
88+
89+
What about that other function we saw, `singleton : elt -> t`? Again,
90+
using our intuition, if we provide the function with a single element,
91+
and the function returns a set, then probably the function will return
92+
a set containing that element (or else what else would it do with the
93+
parameter we gave it?). The name of the function is `singleton`, and
94+
again if you're unfamiliar with what word, you can look it up on
95+
Wikipedia and see that the word means "a set with exactly one element".
96+
It sounds like we're on the right track again. Let's test our theory.
1997

2098
```ocamltop
2199
let s = SS.singleton "hello";;
100+
SS.cardinal s;;
22101
```
23-
To add some elements to the set we can do.
24102

25-
```ocamltop
26-
let s =
27-
List.fold_right SS.add ["hello"; "world"; "community"; "manager";
28-
"stuff"; "blue"; "green"] s;;
29-
```
30-
Now if we are playing around with sets we will probably want to see what
31-
is in the set that we have created. To do this we can write a function
32-
that will print the set out.
103+
It looks like we were right again!
104+
105+
## Working with Sets
106+
107+
Now let's say we want to build bigger and more complex sets. Specifically,
108+
let's say we want to add another element to our existing set. So we're
109+
looking for a function with two parameters: One of the parameters should
110+
be the element we wish to add, and the other parameter should be the set
111+
that we're adding to. For the return value, we would expect it to either
112+
return unit (if the function modifies the set in place), or it returns a
113+
new set representing the result of adding the new element. So we're
114+
looking for signatures that look something like `elt -> t -> unit` or
115+
`t -> elt -> unit` (since we don't know what order the two parameters
116+
should appear in), or `elt -> t -> t` or `t -> elt -> t`.
117+
118+
Skimming through the list, we see 2 functions with matching signatures:
119+
`add : elt -> t -> t` and `remove : elt -> t -> t`. Based on their names,
120+
`add` is probably the function we're looking for. `remove` probably removes
121+
an element from a set, and using our intuition again, it does seem like
122+
the type signature makes sense: To remove an element from a set, you need
123+
to tell it what set you want to perform the removal on and what element
124+
you want to remove; and the return result will be the resulting set after
125+
the removal.
126+
127+
Furthermore, because we see that these functions return `t` and not `unit`,
128+
we can infer that these functions do not modify the set in place, but
129+
instead return a new set. Again, we can test this theory:
33130

34131
```ocamltop
35-
(* Prints a new line "\n" after each string is printed *)
36-
let print_set s =
37-
SS.iter print_endline s;;
132+
let firstSet = SS.singleton "hello";;
133+
let secondSet = SS.add "world" firstSet;;
134+
SS.cardinal firstSet;;
135+
SS.cardinal secondSet;;
38136
```
39-
If we want to remove a specific element of a set there is a remove
40-
function. However if we want to remove several elements at once we could
41-
think of it as doing a 'filter'. Let's filter out all words that are
42-
longer than 5 characters.
43137

44-
This can be written as:
138+
It looks like our theories were correct!
139+
140+
## Sets of With Custom Comparators
141+
142+
The `SS` module we created uses the built-in comparison function provided
143+
by the `String` module, which performs a case-sensitive comparison. We
144+
can test that with the following code:
45145

46146
```ocamltop
47-
let my_filter str =
48-
String.length str <= 5;;
49-
let s2 = SS.filter my_filter s;;
147+
let firstSet = SS.singleton "hello";;
148+
let secondSet = SS.add "HELLO" firstSet;;
149+
SS.cardinal firstSet;;
150+
SS.cardinal secondSet;;
50151
```
51-
or using an anonymous function:
152+
153+
As we can see, the `secondSet` has a cardinality of 2, indicating that
154+
`"hello"` and `"HELLO"` are considered two distinct elements.
155+
156+
Let's say we want to create a set which performs a case-insensitive
157+
comparison instead. To do this, we simply have to change the parameter
158+
that we pass to the `Set.Make` function.
159+
160+
The `Set.Make` function expects a struct with two fields: a type `t`
161+
that represents the type of the element, and a function `compare`
162+
whose signature is `t -> t -> int` and essentially returns 0 if two
163+
values are equal, and non-zero if they are non-equal. It just so happens
164+
that the `String` module matches that structure, which is why we could
165+
directly pass `String` as a parameter to `Set.Make`. Incidentally, many
166+
other modules also have that structure, including `Int` and `Float`,
167+
and so they too can be directly passed into `Set.Make` to construct a
168+
set of integers, or a set of floating point numbers.
169+
170+
For our use case, we still want our elements to be of type string, but
171+
we want to change the comparison function to ignore the case of the
172+
strings. We can accomplish this by directly passing in a literal struct
173+
to the `Set.Make` function:
52174

53175
```ocamltop
54-
let s2 = SS.filter (fun str -> String.length str <= 5) s;;
176+
module CISS = Set.Make(struct
177+
type t = string
178+
let compare a b = compare (String.lowercase_ascii a) (String.lowercase_ascii b)
179+
end);;
55180
```
56-
If we want to check and see if an element is in the set it might look
57-
like this.
181+
182+
We name the resulting module CISS (short for "Case Insensitive String Set").
183+
We can now test whether this module has the desired behavior:
184+
58185

59186
```ocamltop
60-
SS.mem "hello" s2;;
187+
let firstSet = CISS.singleton "hello";;
188+
let secondSet = CISS.add "HELLO" firstSet;;
189+
CISS.cardinal firstSet;;
190+
CISS.cardinal secondSet;;
61191
```
62192

63-
The Set module also provides the set theoretic operations union,
64-
intersection and difference. For example, the difference of the original
65-
set and the set with short strings (≤ 5 characters) is the set of long
66-
strings:
193+
Success! `secondSet` has a cardinality of 1, showing that `"hello"`
194+
and `"HELLO"` are now considered to be the same element in this set.
195+
We now have a set of strings whose compare function performs a case
196+
insensitive comparison.
197+
198+
Note that this technique can also be used to allow arbitrary types
199+
to be used as the element type for set, as long as you can define a
200+
meaningful compare operation:
67201

68202
```ocamltop
69-
print_set (SS.diff s s2);;
203+
type color = Red | Green | Blue;;
204+
205+
module SC = Set.Make(struct
206+
type t = color
207+
let compare a b =
208+
match (a, b) with
209+
| (Red, Red) -> 0
210+
| (Red, Green) -> 1
211+
| (Red, Blue) -> 1
212+
| (Green, Red) -> -1
213+
| (Green, Green) -> 0
214+
| (Green, Blue) -> 1
215+
| (Blue, Red) -> -1
216+
| (Blue, Green) -> -1
217+
| (Blue, Blue) -> 0
218+
end);;
70219
```
71-
Note that the Set module provides a purely functional data structure:
72-
removing an element from a set does not alter that set but, rather,
73-
returns a new set that is very similar to (and shares much of its
74-
internals with) the original set.
220+
221+
## Technical Details
222+
223+
### Set.Make, types and modules
224+
225+
As mentioned in a previous section, the `Set.Make` function accepts a structure
226+
with two specific fields, `t` and `compare`. Modules have structure, and thus
227+
it's possible (but not guaranteed) for a module to have the structure that
228+
`Set.Make` expects. On the other hand, types do not have structure, and so you
229+
can never pass a type to the `Set.Make` function. In OCaml, modules start with
230+
an upper case letter and types start with a lower case letter. This is why
231+
when creating a set of strings, you have to use `Set.Make(String)` (passing in
232+
the module named `String`), and not `Set.Make(string)` (which would be attempting
233+
to pass in the type named `string`, which will not work).
234+
235+
### Purely Functional Data Structures
236+
237+
The data structure implemented by the Set functor is a purely functional one.
238+
What exactly that means is a big topic in itself (feel free to search for
239+
"Purely Functional Data Structure" in Google or Wikipedia to learn more). As a
240+
short oversimplification, this means that all instances of the data structure
241+
that you create are immutable. The functions like `add` and `remove` do not
242+
actually modify the set you pass in, but instead return a new set representing
243+
the results of having performed the corresponding operation.
244+
245+
### Full API documentation
246+
247+
This tutorial focused on teaching how to quickly find a function that does what
248+
you want by looking at the type signature. This is often the quickest and most
249+
convenient way to discover useful functions. However, sometimes you do want to
250+
see the formal documentation for the API provided by a module. For sets, the
251+
API documentation you probably want to look at is at
252+
https://ocaml.org/api/Set.Make.html
75253

76254

0 commit comments

Comments
 (0)