this repo has no description
1opam-version: "2.0"
2maintainer: "Gabriel Radanne <drupyog@zoho.com>"
3authors: [ "Thomas Gazagnaire"
4 "Anil Madhavapeddy"
5 "Dave Scott"
6 "Thomas Leonard"
7 "Gabriel Radanne" ]
8homepage: "https://github.com/mirage/functoria"
9bug-reports: "https://github.com/mirage/functoria/issues"
10dev-repo: "git+https://github.com/mirage/functoria.git"
11doc: "https://mirage.github.io/functoria/"
12license: "ISC"
13tags: ["org:mirage"]
14
15build: [
16 ["jbuilder" "subst" "-p" name] {dev}
17 ["jbuilder" "build" "-p" name "-j" jobs]
18]
19depends: [
20 "ocaml" {>= "4.03" & < "4.12"}
21 "jbuilder" {>= "1.0+beta10"}
22 "cmdliner" {>= "0.9.8"}
23 "fmt"
24 "functoria" {with-test & >= "2.2.0" & < "3.0.0"}
25 "alcotest" {with-test}
26]
27synopsis: "A DSL to organize functor applications"
28description: """
29[](https://travis-ci.org/mirage/functoria)
30[](https://mirage.github.io/functoria/index.html)
31
32## What is this for?
33
34Functoria is a DSL to describe a set of modules and functors, their types and how to apply them in order to produce a complete application.
35
36The main use case is mirage. See the [mirage][] repository for details.
37
38## How to write a configuration file?
39
40There are numerous examples of configuration files in [mirage-skeleton][]. Most of them should be fairly general and understandable, even outside the context of mirage. We can distinguish two parts in a `config.ml`: Defining new modules and using them.
41
42In order to define a new module, we use the `foreign` function. Among its various arguments, it takes a module name and a type. The type is assembled with the DSL's combinators and the `@->` operator, which symbols a functor arrow.
43
44```ocaml
45let main = foreign "Unikernel.Main" (console @-> job)
46```
47
48Here, we declare the functor `Unikernel.Main` that takes a module that should be a `console` and returns a module that is a `job`. It is up to the user to ensure that the declaration matches the implementation (or be punished by a compiler error later on). If the declaration is correct, everything that follows will be.
49
50We can now use this declaration:
51
52```ocaml
53let () = register "console" [main $ default_console]
54```
55
56Here, we register a new application with the `register` function. This function should only be called once and takes as argument the name of the application and a list of jobs. We use the `$` operator to apply the functor `main` (aka `Unikernel.Main`) to the default console.
57
58Now that everything is ready, you can use the `configure` subcommand!
59
60### What is a job?
61
62A job is a module containing a function `start`. This function will receive one argument per functor argument and one per dependency, in this order. `foreign` assumes the function `start` returns `unit`.
63
64### Defining new keys
65
66A key is composed of:
67
68- _name_ : The name of the value in the program.
69- _description_ : How it should be displayed/serialized.
70- _stage_ : Is the key available only at runtime, at configure time or both?
71- _documentation_ : It is not optional so you should really write it.
72
73Consider a multilingual application: we want to pass the default language as a parameter. We will use a simple string, so we can use the predefined description `Key.Desc.string`. We want to be able to define it both at configure and run time, so we use the stage `` `Both``. This gives us the following code:
74
75```ocaml
76let lang_key =
77 let doc = Key.Arg.info
78 ~doc:"The default language for the application." [ "l" ; "lang" ]
79 in
80 Key.create "language" @@ Key.Arg.(opt ~stage:`Both string "en" doc)
81```
82
83Here, we defined both a long option `--lang` and a short one `-l` (the format is similar to the one used by [Cmdliner][cmdliner]).
84In the application code, the value is retrieved with `Key_gen.language ()`.
85
86The option is also documented in the `--help` option for both the `configure` subcommand (at configure time) and `./my_application` (at startup time).
87
88```
89 -l VAL, --lang=VAL (absent=en)
90 The default language for the application.
91```
92
93[cmdliner]: http://erratique.ch/software/cmdliner
94
95### Using switching keys
96
97We can do much more with keys: we can use them to switch implementation at configure time. Imagine we want to completely change some implementation based on the language. Finns are special snowflakes, they deserve their special application!
98
99First, we have to compute a boolean value from `lang`:
100
101```ocaml
102let is_fi = Key.(pure ((=) "fi") $ value lang_key)
103```
104
105We can use the `if_impl` combinator to choose between two implementations depending on the value of the key:
106
107```ocaml
108let dynamic_storage =
109 if_impl is_fi
110 finnish_implementation
111 not_finnish_implementation
112```
113
114This distinction will be visible using the `describe` subcommand and a dot diagram is available with the `--dot` option!
115
116## Internals
117
118### Phases
119
120Configuration is separated into phases:
121
1221. Specialized DSL keys
123 The specialized DSL's keys (along with functoria's keys) are resolved.
1242. Compilation and dynlink of the config file.
1253. Registering.
126 When the `register` function is called, the list of jobs is recorded and
127 immediately transformed into a graph.
1284. Switching keys and tree evaluation.
129 The switching keys are the keys inside the [If].
130 Those keys are resolved and the graph is simplified. At this point,
131 the actual modules used are fully known.
132 Note: for the `describe` command, Only _partial_ evaluation is done, which
133 means decision nodes are resolved only if the value was given on the command
134 line, disregarding default values.
1355. Full Key resolution.
136 Once the actual modules are known, we can resolve all the keys and figure out
137 libraries and packages.
1386. Dependency handling, configuration and code emission.
139
140Phases 1. to 4. are also applied for the `clean` command.
141
142
143
144[mirage]: https://github.com/mirage/mirage
145[mirage-skeleton]: https://github.com/mirage/mirage-skeleton"""
146url {
147 src:
148 "https://github.com/mirage/functoria/releases/download/2.2.1/functoria-2.2.1.tbz"
149 checksum: [
150 "sha256=e2c883d5a0a2c3cea59cffa9989c5ee78259aee9a199048cb730d19076e668c5"
151 "md5=ec768d65f4070dd76c369b4a32a1ac32"
152 ]
153}
154available: opam-version >= "2.2.0"
155flags: deprecated