Plugins and packages
A package is a directory containing Elpi files and/or the OCaml code of a plugin that adds builtins to Elpi. Technically, a plugin is just an ordinary OCaml library that registers some builtins when loaded.
Naming a package
Package names start with +, and a program accumulates the plugin or
the files of a package via the accumulate directive.
accumulate "lib/util". % the file lib/util.elpi
accumulate "+foo/bar". % bar.elpi in the directory of package foo
accumulate "+foo.bar/baz". % baz.elpi in the directory of package foo.bar
accumulate "+/lib/util". % lib/util.elpi in the root of the current package
accumulate "+foo.bar". % the plugin of package foo.bar
A string with a / after the name of the package names a file in the
directory of the package; one with no / names the plugin of the package.
With no name, as in "+/lib/util", the file is looked up in the root of the
current package (see Projects). It is recommended to always use
+/.. paths within a package, since these are relative to the root of
the package and not the file that loads them, making files relocatable within
the project file structure.
For elpi and elpi-lsp a package name is a findlib package name and
it is rooted where ocamlfind query pkgname prints. For
example the package elpi-json, installed by the opam package of the same
name, gives the builtins of its plugin and a file of utilities,
utils.elpi:
code/json.elpi:
1accumulate "+elpi-json".
2accumulate "+elpi-json/utils".
3
4main :-
5 json.from_string "{ \"name\": \"elpi\", \"tags\": [1, 2] }" J ok,
6 json.lookup J "tags" T,
7 print T,
8 json.to_string J S ok,
9 print S.
json.list [json.int 1, json.int 2]
{"name":"elpi","tags":[1,2]}
elpi loads the OCaml code of elpi-json when it meets accumulate
"+elpi-json" (the file of utilities accumulates it too).
Projects
A project is the package being written: a directory with a file
elpi.toml at its root:
greet/
elpi.toml name = "greet"
lib/names.elpi
src/main.elpi accumulate "+/lib/names".
# the root of the project greet
name = "greet"
code/project/src/main.elpi:
1accumulate "+/lib/names". % lib/names.elpi at the root of the project
2
3main :- names.all L, std.forall L (n\ print "hello" n).
hello Alice
hello Bob
The project of a file opened by the LSP server is found
walking the file path backward and not among the installed packages:
"+greet/lib/names" is the same file as "+/lib/names" there. Another
project accumulates the files of greet as "+greet/..." once
greet is installed, as the findlib package greet with its
elpi.toml next to its META (see Using other packages).
Writing a plugin
The OCaml code of a plugin calls Plugin.register when it is loaded, with
the name of its findlib package and a list of declarations, the same as for
BuiltIn.declare (see Embedding and extending Elpi). This one, from the tests of Elpi,
is the plugin of the findlib package elpi-test-dyn:
(* a plugin dynlinked by the elpi command: the plugin of findlib
package elpi-test-dyn *)
open Elpi.API
let () =
let open BuiltIn in
let open BuiltInPredicate in
let open BuiltInPredicate.Notation in
ignore @@ Plugin.register ~name:"elpi-test-dyn" [
MLCode(Pred("dyn.double", In(BuiltInData.int, "N", Out(BuiltInData.int, "M", Easy "M is 2 * N")),
(fun n _ ~depth:_ -> !: (2 * n))), DocAbove);
]
A library of dune with a public_name is a findlib package, and dune
builds the .cmxs that elpi loads:
(library
(name elpi_test_dyn)
(public_name elpi-test-dyn)
(libraries elpi))
A program uses its builtins once it accumulates it, here with a file of the same package:
accumulate "+elpi-test-dyn".
accumulate "+elpi-test-dyn/util".
main :- dyn.double 21 X, util.triple 2 Y, print X Y.
Loading a plugin
elpi dynlinks the package of a plugin when it meets its accumulate,
if the plugin is not registered yet: loaded before (a package can be linked
in a host application) or later,
a plugin works the same way. The one exception: a plugin loaded after
Setup.init cannot declare state components (State.declare_component)
nor quotations. A plugin that needs to desclare state components or quotations
has to be listed in the preload key of the elpi.toml.
The stub of a plugin
By convention the file pkgname.builtins.elpi is the documentation of
the plugin as printed by BuiltIn.document_fmt. The LSP loads these
stubs instead of dynlinking their code.
For example the package elpi-json generates its stub elpi-json.builtins.elpi
when it is built and installs it next to its META, with its elpi.toml:
; the stub of the plugin elpi-json (its documentation), installed next to
; the META: accumulate "+elpi-json" reads it when the plugin is not loaded
; (elpi-lsp). The rule is promoted, like the one that generates the
; builtins of Elpi (src/dune): elpi-json.builtins.elpi is kept in the
; sources, and its diff shows how a change of the plugin changes its
; signatures
(rule
(targets elpi-json.builtins.elpi)
(mode promote)
(action (with-stdout-to elpi-json.builtins.elpi (run src/gen_stub.exe))))
; the files of the package: accumulate "+elpi-json/utils" ..., and the elpi.toml
; that names the project
(install
(package elpi-json)
(section lib)
(files elpi-json.builtins.elpi utils.elpi elpi.toml))
The program that prints it is one line:
(* prints the stub of the plugin, its documentation: elpi-json.builtins.elpi *)
let () = Elpi.API.BuiltIn.document_fmt Format.std_formatter Elpi_json.builtins
The project file
elpi.toml is read by elpi and by elpi-lsp.
- Location
elpireads the nearestelpi.tomlin the directory of the file it runs or above it. The option-config FILEnames another file, and-no-configreads none.elpi-lspreads the nearest one above a document when the document is opened, and again when it is opened afterelpi.tomlchanged. An entry of it that is wrong, such as a package ofusethat is not installed, is left out and its error is shown at the start of the document; the other entries count (elpistops at the first error instead).elpi-lsp -debug FILEprints the project ofFILEas the server reads it. The nearestelpi.tomlabove an accumulated file is also found when that file accumulates"+/...", whatever the options. The search stops at the root of a repository (a directory with.git,.hg,dune-workspace,_RocqProjector_CoqProject) and does not look in the home directory or above it, so that a stray~/elpi.tomlis not the project of every file.- Syntax
A subset of TOML: one
key = valueper line (a list can span several lines), where the value is a string or a list of strings, and comments from#to the end of the line. The only tables are the sections[paths]and[warnings](below), whose keys are on the lines after them; a key can be quoted ("a.b" = ...). A string is written between double quotes, with the escapes of TOML (\",\\,\n,\t,\uXXXX…), or between single quotes, with no escapes ('C:\dir').- Keys
Each key is given at most once, and only
nameis required.namea string, the findlib name of the package of the project (not empty, with no
/nor\), as in"+name/...": itselpi.tomlis installed in the directory of that package, which is checked when another project uses it and byelpi -check-package(Checking an installed package)usea list of packages, written
"+pkg", whoseelpi.toml(in the directory of the package) is used too: their[paths],preludeandpreloadare transitively set for the users, as described in Using other packages[paths]a section: each key is a prefix, and its value a directory or a list of directories, as Rocq’s
-Q. A host application can find the accumulated files its own way, with thefile_resolverofSetup.init, as Rocq does with its logical paths;[paths]replicates that resolver outside the application, so thatelpiandelpi-lspfind the same files.accumulate "a.b/x"readsx.elpiin the directories of the prefixa.b(written"a.b" = "+/src/b", quoted since it has a dot), in order. The prefix cannot be empty: a directory searched for any file would be searched also by the projects that use this one, and its files could hide theirs; such a directory is given with-I.paths = [...], a list, is an error[dev-only-paths]a section like
[paths], used only whenelpi.tomlis the one of the project being worked on: the nearest one above the file thatelpiruns or thatelpi-lspchecks, or the one given with-config. It is not used when another project readselpi.tomlthroughuse, nor byelpi -check-package, so it can name directories that are not installed, such as the tests of the project:rocq-elpihas"elpi.tests" = "+/tests"there. Its prefixes come after the ones of[paths]preludea list of strings found as the ones of
accumulateare, e.g."+elpi-json/utils": their files are given toSetup.initas builtins, so that their macros are seen by every file of the project."+pkg"is the plugin ofpkg, or its stub if the plugin is not loaded. A file of the prelude can accumulate other files; a file that a program accumulates and that is in the prelude already is not added againpreloada list of findlib packages whose plugins are loaded before
Setup.init, and not when a file accumulates them. A plugin can declare state components, or quotations that change how the files of the project are parsed ({{ ... }}inrocq-elpi-lsp-shim): these exist only if the plugin is loaded before the first file is read, so such a plugin has to be preloaded.elpi-lsploads them too, when a document of the project is opened[warnings]a section: each key is a kind of warnings, and its value
"off"(the warning is not shown),"on"(the default) or"error"(the warning is an error:elpistops,elpi-lspshows it as an error). The kinds arelinear-variable,undeclared-global,flex-clause,implication-precedenceandnested-accumulate;allis all of them, and a later key overrides an earlier one. The option-w SPECofelpichanges the level of one kind afterelpi.toml:+nameturns it on,-nameoff, and@namemakes it an error. It is repeated for several kinds, and a later one overrides an earlier one, e.g.-w -linear-variable -w @undeclared-global
A directory of [paths] or a file of prelude is written as in
accumulate: "+/dir" is dir in the root of the project (the
directory of elpi.toml), "+pkg/dir" is dir in the directory of
the package pkg. A relative or absolute path, such as "lib", is an error, so that an
elpi.toml means the same in the sources of a project and where its package
is installed. An unknown key, a duplicate one, or a value of the wrong kind
is an error located at its line of elpi.toml, such as elpi.toml: unknown
key plugins (name, use, prelude, preload and the sections [paths],
[dev-only-paths] and [warnings] are) on line 3. For example, the project of this test:
# the project of main.elpi, at its root
name = "proj"
prelude = ["+/pre"]
preload = ["elpi-test-dyn.state"]
[paths]
"proj.lib" = "+/lib" # proj.lib/x is lib/x
"proj.deep" = "+/src/deep" # proj.deep/x is src/deep/x
"dyn" = "+elpi-test-dyn" # dyn/x is x in the directory of package elpi-test-dyn
[dev-only-paths]
"proj.tests" = "+/tests" # only for proj itself, not for a project that uses it
has its main file accumulate a file found through [paths] (libfile,
in lib, proj.deep/more, in src/deep, and dyn/util, in the
directory of the package elpi-test-dyn), one found through
[dev-only-paths] (proj.tests/t, in tests), a preloaded plugin
and a file of the project, and use a macro of its prelude:
accumulate "proj.lib/libfile".
accumulate "+/pre". % in the prelude already: not added again
accumulate "+elpi-test-dyn.state".
accumulate "+/src/deep/answer".
accumulate "proj.deep/more".
accumulate "dyn/util".
accumulate "proj.tests/t".
main :- @hello, lib.answer A, A = @forty-two, deep.more 7, deep.answer D, dyn.tick N, dyn.tick M, util.triple 5 T, tests.check C, print A D N M T C.
Using other packages
The elpi.toml of a project is installed with its package, next to its
META, and the files that it names are installed where it expects them:
the installed tree mirrors the sources, so that "+/..." keeps its meaning.
A project that builds on other packages names them in use; their
elpi.toml gives the project their [paths], their prelude and their
preload, so that it need not repeat how their files accumulate one
another. The packages of use are followed transitively: the [paths]
of a used package come after the ones of the project, and its prelude and
preload before; an entry given twice counts once. [dev-only-paths]
and [warnings] are not used.
A package with no elpi.toml, or whose elpi.toml has a name that
is not the findlib name of the package, is an error at the line of use.
For example, the package elpi-test-dyn of the tests installs this
elpi.toml, with the files macros.elpi and lib/double.elpi:
# the project of the findlib package elpi-test-dyn, next to its META: a
# project that uses it gets its paths, prelude and preload
name = "elpi-test-dyn"
prelude = ["+/macros"]
preload = ["elpi-test-dyn.state"]
[paths]
"dyn.lib" = "+/lib" # dyn.lib/x is lib/x in the directory of the package
and a project that uses it:
# a project that uses the package elpi-test-dyn: its paths, its prelude and
# its preload are the ones of this project too
name = "useproj"
use = ["+elpi-test-dyn"]
finds lib/double.elpi through the prefix dyn.lib, sees the macro of
its prelude, and has its plugin elpi-test-dyn.state preloaded:
accumulate "dyn.lib/double". % through the [paths] of elpi-test-dyn
accumulate "+elpi-test-dyn.state". % preloaded by elpi-test-dyn
main :- dyn.double @seven D, dyn.tick N, print D N. % @seven: its prelude
In the same way the elpi.toml of rocq-elpi names the files of its
prelude and the logical paths (elpi_elpi, elpi.apps.derive.elpi)
that its files accumulate, and a library written with it, such as Trocq,
has use = ["+rocq-elpi"] and only its own [paths].
Checking an installed package
elpi -check-package pkg checks the package pkg as findlib finds it
installed, so that a file that was not installed shows up before another
project needs it. It checks:
its
elpi.toml, if it has one, read asusereads it: itsnameispkg, and its[dev-only-paths]are not used;its plugin, if it installs a stub
pkg.builtins.elpi: the plugin is dynlinked and registers under the namepkg, the stub compiles, and the stub is the documentation thatBuiltIn.document_fmtprints for the plugin, so it is not stale;each
.elpifile of its directory (the stubs aside), shown as the"+pkg/file"that accumulates it: the file is parsed and compiled aselpi-lspcompiles a document of a project that usespkg, with thepreload,preludeand[paths]of itselpi.toml. So every file that it accumulates, directly or not, must be installed.
It prints one line for each of these items, ok or the errors, and exits
with 1 if one of them failed. The install of dune, in
_build/install/default/lib, has the same layout as the installed
package, so a package can be checked before it is installed:
dune build @install
OCAMLPATH=$PWD/_build/install/default/lib elpi -check-package elpi-test-dyn
For the package elpi-test-dyn of the tests, whose macros.elpi is its
prelude, it prints:
elpi.toml ok (name elpi-test-dyn)
+elpi-test-dyn ok (plugin loaded, stub up to date)
+elpi-test-dyn/lib/double ok
+elpi-test-dyn/macros ok (in the prelude)
+elpi-test-dyn/util ok
In a host application
Setup.init ~packages gives Elpi the packages of the host: the directory of
a package, the root of the project that contains a directory (for
"+/...")
and, optionally, a function that loads the OCaml code of a package. Without
it, accumulate "+..." is an error, but a plugin registered by the host can
still be accumulated. The library elpi.dynload (module Elpi_dynload)
provides the ones of findlib and of elpi.toml: packages loads plugins
with dynlink, as elpi does, packages_stubs reads their stubs
instead, as elpi-lsp does; it also reads elpi.toml.