-
Notifications
You must be signed in to change notification settings - Fork 453
[draft][OxCaml] Tutorial for library parameter #12111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
maiste
wants to merge
6
commits into
ocaml:main
Choose a base branch
from
maiste:doc/parameter-explanation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c3e1cd
tmp: structure for documentation and first steps
maiste 8f843d0
tmp: complete the setup part
maiste fb8be06
tmp: complete the parameter part
maiste 0106436
tmp: complete the implement part
maiste 9a7478b
minor updates in wording
Sudha247 24fb410
Add an example for parameters_field
Sudha247 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
doc/tutorials/oxcaml-parameterized-library/implement.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Implement a Library Parameter | ||
|
|
||
| Now we have our parameter, we have our interface. In this section, we will | ||
| focus on how to write a library that implements our parameter. | ||
|
|
||
| ## Structure Our Project | ||
|
|
||
| As we did for the `parameter`, we are going to add a new directory in our | ||
| structure under `lib/`. We create a directory `impl/` under `lib/`. At the root | ||
| of the directory, we can run: | ||
|
|
||
| ```{code-block} shell | ||
| mkdir -p lib/impl | ||
| ``` | ||
|
|
||
| ## Creating an Implementation | ||
|
|
||
| As for the parameter, we start with our `lib/impl/dune` | ||
|
|
||
| :::{literalinclude} implement/lib/impl/dune | ||
| :language: dune | ||
| ::: | ||
|
|
||
| To create an implementation, we write two files: | ||
| - The first one is `lib/impl/impl.ml`. It contains the implementation. | ||
| - The second one is `lib/impl/impl.mli`. It contains the interface of the implementation. | ||
|
|
||
|
|
||
| We define an additional `create` function to send our element in the abstract | ||
| type. Our project now implements a parameter. | ||
|
|
||
| :::{literalinclude} implement/lib/impl/impl.mli | ||
| :language: ocaml | ||
| ::: | ||
|
|
||
| To have an abstract interface, we define an `mli` file. | ||
|
|
||
| :::{literalinclude} implement/lib/impl/impl.ml | ||
| :language: ocaml | ||
| ::: | ||
|
|
||
|
|
||
| ## Conclusion | ||
|
|
||
| In this section you have learned to implement a parameter. We have added the | ||
| following files: | ||
|
|
||
| ::::{dropdown} `lib/impl/dune` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/lib/impl/dune | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| ::::{dropdown} `lib/impl/impl.ml` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/lib/impl/impl.ml | ||
| :language: ocaml | ||
| ::: | ||
| :::: | ||
|
|
||
| ::::{dropdown} `lib/impl/impl.mli` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/lib/impl/impl.mli | ||
| :language: ocaml | ||
| ::: | ||
| :::: | ||
|
|
||
| You should also have the previous files: | ||
|
|
||
| ::::{dropdown} `dune-project` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/dune-project | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| ::::{dropdown} `test/dune` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/test/dune | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| :::: | ||
|
|
||
| ::::{dropdown} `test/simple.t` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/test/simple.t | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| ::::{dropdown} `lib/param/dune` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/lib/param/dune | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| ::::{dropdown} `lib/param/param.mli` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} implement/lib/param/param.mli | ||
| :language: ocaml | ||
| ::: | ||
| :::: | ||
|
|
||
| :::: | ||
|
|
||
| In the next section, we will see how we write a library parameterized by our | ||
| parameter. |
4 changes: 4 additions & 0 deletions
4
doc/tutorials/oxcaml-parameterized-library/implement/dune-project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| (lang dune 3.20) | ||
| (using oxcaml 0.1) | ||
|
|
||
| (package (name param)) |
3 changes: 3 additions & 0 deletions
3
doc/tutorials/oxcaml-parameterized-library/implement/lib/impl/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (library | ||
| (name impl) | ||
| (implement param)) |
5 changes: 5 additions & 0 deletions
5
doc/tutorials/oxcaml-parameterized-library/implement/lib/impl/impl.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| type t = int | ||
|
|
||
| let create t = t | ||
|
|
||
| let compare = Int.compare |
3 changes: 3 additions & 0 deletions
3
doc/tutorials/oxcaml-parameterized-library/implement/lib/impl/impl.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| type t | ||
| val create : int -> t | ||
| val compare: t -> t -> int |
2 changes: 2 additions & 0 deletions
2
doc/tutorials/oxcaml-parameterized-library/implement/lib/param/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| (library_parameter | ||
| (public_name param)) |
2 changes: 2 additions & 0 deletions
2
doc/tutorials/oxcaml-parameterized-library/implement/lib/param/param.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| type t | ||
| val compare: t -> t -> int |
3 changes: 3 additions & 0 deletions
3
doc/tutorials/oxcaml-parameterized-library/implement/test/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (cram | ||
| (applies_to :whole_subtree) | ||
| (enabled_if %{oxcaml_supported})) |
4 changes: 4 additions & 0 deletions
4
doc/tutorials/oxcaml-parameterized-library/implement/test/simple.t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| This test shows the behavior of the program and ensures it does not have | ||
| regression. | ||
|
|
||
| $ dune build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| --- | ||
| author: Etienne Marais | ||
| --- | ||
|
|
||
| OxCaml Parameterized Library With Dune | ||
| ====================================== | ||
|
|
||
| :::{warning} | ||
| Parameterized libraries are a feature only supported by the OxCaml branch of | ||
| the OCaml compiler. You need to install this version of the compiler to use it | ||
| within Dune. This feature should currently be considered as not stable. | ||
| ::: | ||
|
|
||
| This tutorial explains how to create a parameterized library in Dune. | ||
| Parameterized library in Dune are a concept similar to | ||
| {doc}`/virtual-libraries`. Parameterized libraries are the formalization within | ||
| the compiler of virtual libraries.A parameterized library is conceptually | ||
| equivalent to a functor. For more detailled explanation, see (TODO @maiste: | ||
| link to explanation). | ||
|
|
||
| By the end of the tutorial, you will know: | ||
| - how to create a library parameter, | ||
| - how to implement a library parameter, | ||
| - how to parameterize a library with a library parameter, | ||
| - how to instantiate a parameterized library with a a parameter implementation. | ||
|
|
||
| To get started make sure you have [oxcaml](https://oxcaml.org/) available in | ||
| your path and let's {doc}`setup` our project. | ||
|
|
||
| :::{toctree} | ||
| :hidden: | ||
| :maxdepth: 1 | ||
| setup | ||
| parameter | ||
| implement | ||
| parameterized_by | ||
| instantiate | ||
| ::: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Instantiate a Parameterized Library | ||
|
|
||
| TODO @maiste: instantiate the parameterized library with the implementation. |
101 changes: 101 additions & 0 deletions
101
doc/tutorials/oxcaml-parameterized-library/parameter.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # Create a Library Parameter | ||
|
|
||
| In this section, we are going to learn how to write a | ||
| {doc}`/reference/dune/library_parameter` | ||
|
|
||
| ## Declaring a parameter | ||
|
|
||
| We can either declare our parameter as private or public in Dune. In our case, | ||
| we are going to declare it as a public parameter. Indeed, Dune doesn't build | ||
| unecessary targets. It means it won't build anything that is not needed. In our | ||
| case, it would mean our parameter wouldn't get built until we use it. Making it | ||
| public informs Dune it is mandatory to build the target. | ||
|
|
||
| To build our parameter we are going to use the | ||
| {doc}`/reference/dune/library_parameter` stanza. | ||
|
|
||
| First, we need to create the directory where we are going to host our | ||
| parameter. At the root of our project, we create a `lib/` directory where we | ||
| are going to host the code. Inside the `lib/` directory, we create a `param/` | ||
| directory to host our parameter. We do this in a single command to save | ||
| instructions. | ||
|
|
||
| ```{code-block} shell | ||
| mkdir -p lib/param | ||
| ``` | ||
|
|
||
| In `lib/param/`, we create a new `dune` file: | ||
|
|
||
| :::{literalinclude} parameter/lib/param/dune | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| The `library_parameter` stanza takes care of the parameter generation under the | ||
| hood. It calls the OxCaml compiler with the correct compiler options and flags. | ||
|
|
||
| Still in `lib/param/`, we create the interface for our future libraries in `param.mli`: | ||
|
|
||
| :::{literalinclude} parameter/lib/param/param.mli | ||
| :language: ocaml | ||
| ::: | ||
| :::: | ||
|
|
||
| This interface is what our implementation library will need to provide and the | ||
| interface of our future parameterized library. | ||
|
|
||
| ## Conclusion | ||
|
|
||
| We have learned how to declare a parameter using Dune with its interface. | ||
|
|
||
| Your directory should now contains two additional files: | ||
|
|
||
| ::::{dropdown} `lib/param/dune` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} parameter/lib/param/dune | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| ::::{dropdown} `lib/param/param.mli` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} parameter/lib/param/param.mli | ||
| :language: ocaml | ||
| ::: | ||
| :::: | ||
|
|
||
| :::: | ||
|
|
||
| The file from the previous should be in the following state: | ||
|
|
||
| ::::{dropdown} `dune-project` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} parameter/dune-project | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| ::::{dropdown} `test/dune` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} parameter/test/dune | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| :::: | ||
|
|
||
| ::::{dropdown} `test/simple.t` | ||
| :icon: file-code | ||
|
|
||
| :::{literalinclude} parameter/test/simple.t | ||
| :language: dune | ||
| ::: | ||
| :::: | ||
|
|
||
| In the next part, we are going to learn how to write the implementation of our | ||
| parameter. | ||
|
|
4 changes: 4 additions & 0 deletions
4
doc/tutorials/oxcaml-parameterized-library/parameter/dune-project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| (lang dune 3.20) | ||
| (using oxcaml 0.1) | ||
|
|
||
| (package (name param)) |
2 changes: 2 additions & 0 deletions
2
doc/tutorials/oxcaml-parameterized-library/parameter/lib/param/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| (library_parameter | ||
| (public_name param)) |
2 changes: 2 additions & 0 deletions
2
doc/tutorials/oxcaml-parameterized-library/parameter/lib/param/param.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| type t | ||
| val compare: t -> t -> int |
3 changes: 3 additions & 0 deletions
3
doc/tutorials/oxcaml-parameterized-library/parameter/test/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (cram | ||
| (applies_to :whole_subtree) | ||
| (enabled_if %{oxcaml_supported})) |
4 changes: 4 additions & 0 deletions
4
doc/tutorials/oxcaml-parameterized-library/parameter/test/simple.t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| This test shows the behavior of the program and ensures it does not have | ||
| regression. | ||
|
|
||
| $ dune build |
3 changes: 3 additions & 0 deletions
3
doc/tutorials/oxcaml-parameterized-library/parameterized_by.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Parameterized a Library | ||
|
|
||
| TODO @maiste: Create a library using the parameter. |
1 change: 1 addition & 0 deletions
1
doc/tutorials/oxcaml-parameterized-library/parameters_field/a/a.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| val foo : string |
1 change: 1 addition & 0 deletions
1
doc/tutorials/oxcaml-parameterized-library/parameters_field/a/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (library_parameter (public_name param.a) (name a)) |
1 change: 1 addition & 0 deletions
1
doc/tutorials/oxcaml-parameterized-library/parameters_field/b/b.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| val bar : string |
1 change: 1 addition & 0 deletions
1
doc/tutorials/oxcaml-parameterized-library/parameters_field/b/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (library_parameter (name b)) |
1 change: 1 addition & 0 deletions
1
doc/tutorials/oxcaml-parameterized-library/parameters_field/dune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (library (name parameterised) (parameters param.a b)) |
1 change: 1 addition & 0 deletions
1
doc/tutorials/oxcaml-parameterized-library/parameters_field/p.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let test () = print_endline A.foo |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it meant to link some documentation about parameterized libraries? Is it available somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes 👍🏻