Skip to content

Commit 54352d0

Browse files
sabineCuihtlauac ALVARADO
authored andcommitted
editing
1 parent e0f8995 commit 54352d0

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

data/tutorials/language/1ms_01_functors.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,65 @@ id: functors
33
title: Functors
44
short_title: Functors
55
description: >
6-
Functors essentially work the same way as functions. The difference is that we are passing modules instead of values.
6+
In OCaml, a functor is a function at the module-level. Functors take modules as arguments and return a new module.
77
category: "Module System"
88
---
99

1010
## Introduction
1111

12-
In this tutorial, we look at how to use a functor, how to write a functor, and show a couple of use cases involving functors.
12+
In this tutorial, we look at how to apply functors and how to write functors. We also show some use cases involving functors.
1313

14-
As suggested by the name, a _functor_ is almost like a function. However, while functions are between values, functors are between modules. A functor takes a module as a parameter and returns a module as a result. A functor is a parametrised module.
15-
16-
In mathematics, [functor](https://en.wikipedia.org/wiki/Functor) means something different. You don't need to know about those functors to understand OCaml's.
14+
As suggested by the name, a _functor_ is almost like a function. However, while functions are between values, functors are between modules. A functor takes a module as a parameter and returns a module as a result. A functor in OCaml is a parametrised module, not to be confused with a [functor in mathematics](https://en.wikipedia.org/wiki/Functor).
1715

1816
**Prerequisites**: [Modules](/docs/modules).
1917

2018
## Project Setup
2119

22-
This tutorial uses the [Dune](https://dune.build) build tool. Make sure you have installed version 3.7 or later. We start by creating a fresh project. We need a folder named `funkt` with files `dune-project`, `dune`, and `funkt.ml`. The latter two are created empty.
20+
This tutorial uses the [Dune](https://dune.build) build tool. Make sure you have installed version 3.7 or later. We start by creating a fresh project. We need a folder named `funkt` with files `dune-project`, `dune`, and `funkt.ml`.
21+
2322
```shell
2423
$ mkdir funkt; cd funkt
2524
```
2625

27-
**`dune-project`**
26+
Place the following in the file **`dune-project`**:
2827
```lisp
2928
(lang dune 3.7)
3029
(package (name funkt))
3130
```
3231

33-
**`dune`**
32+
The content of the file **`dune`** should be this:
3433
```lisp
3534
(executable
3635
(name funkt)
3736
(public_name funkt)
3837
(libraries str))
3938
```
4039

41-
Check this works using the `dune exec funkt` command, it shouldn't do anything (the empty file is valid OCaml syntax) but it shouldn't fail either. The stanza `libraries str` will be used later.
40+
Create an empty file `funkt.ml`.
41+
42+
Check that this works using the `dune exec funkt` command. It shouldn't do anything (the empty file is valid OCaml syntax), but it shouldn't fail either. The stanza `libraries str` makes the `Str` module (which we will use later) available.
4243

4344
## Using an Existing Functor: `Set.Make`
4445

45-
The standard library contains a [`Set`](/api/Set.html) module providing a data structure that allows set operations like union and intersection. You may check the [Set](/docs/sets) tutorial to learn more about this module, but it is not required to follow the present tutorial. To use the provided type and its associated [functions](/api/Set.S.html), it's necessary to use the functor provided by `Set`. For reference only, here is a shortened version of the interface of `Set`:
46+
The standard library contains a [`Set`](/api/Set.html) module which is designed to handle sets. This module enables you to perform operations such as union, intersection, and difference on sets. You may check the [Set](/docs/sets) tutorial to learn more about this module, but it is not required to follow the present tutorial.
47+
48+
To create a set module for a given element type (which allows you to use the provided type and its associated [functions](/api/Set.S.html)), it's necessary to use the functor `Set.Make` provided by the `Set` module. For reference only, here is a shortened version of the interface of `Set`:
4649
```ocaml
4750
module type OrderedType = sig
4851
type t
4952
val compare : t -> t -> int
5053
end
5154
52-
module type S = sig
53-
(** This is the module's signature returned by applying `Make` *)
54-
end
55-
5655
module Make : functor (Ord : OrderedType) -> S
5756
```
5857

59-
Here is how this reads (starting from the bottom-up, then going up):
58+
Here is how this reads (starting from the bottom, then going up):
6059
* Like a function (indicated by the arrow `->`), the functor `Set.Make`
61-
- takes a module having `Set.OrderedType` as signature and
62-
- returns a module having `Set.S` as signature
63-
* The module type `Set.S` is the signature of some sort of set
64-
* The module type `Set.OrderedType` is the signature of elements of a
60+
- takes a module with signature `Set.OrderedType` and
61+
- returns a module with signature [`Set.S`](/api/Set.S.html)
62+
* The module type `Set.OrderedType` requires a type `t` and a function `compare`, which are used to perform the comparisons between elements of the set.
6563

66-
**Note**: Most set operation implementations must use a comparison function. Using `Stdlib.compare` would make it impossible to use a user-defined comparison algorithm. Passing the comparison function as a higher-order parameter, as done in `Array.sort`, for example, would add a lot of boilerplate code. Providing set operations as a functor allows specifying the comparison function only once.
64+
**Note**: Most set operations need to compare elements to check if they are the same. To allow using a user-defined comparison algorithm, the `Set.Make` functor takes a module the specifies both the element type `t` and the `compare` function. Passing the comparison function as a higher-order parameter, as done in `Array.sort`, for example, would add a lot of boilerplate code. Providing set operations as a functor allows specifying the comparison function only once.
6765

6866
Here is what it can look like in our project:
6967

0 commit comments

Comments
 (0)