Tactics Written in OCaml
(Part of the Coq FAQ)
Have a look at the skeleton “Hello World” tactic below. You also have some examples of tactics written in OCaml in the “plugins” directory of Coq's source.
The following steps describe how to write a simplistic “Hello world” OCaml tactic. This takes the form of a dynamically loadable OCaml module, which will be invoked from the Coq toplevel.
- In the plugins directory of the Coq source location, create a directory
hello
. Proceed to create a grammar and OCaml file, respectivelyplugins/hello/g_hello.ml4
andplugins/hello/coq_hello.ml
, containing:
in g_hello.ml4
:
(*i camlp4deps: "grammar/grammar.cma" i*)
TACTIC EXTEND Hello
| [ "hello" ] -> [ Coq_hello.printHello ]
END
in coq_hello.ml
:
let printHello gl =
Tacticals.tclIDTAC_MESSAGE (Pp.str "Hello world") gl
- Create a file
plugins/hello/hello_plugin.mllib
, containing the names of the OCaml modules bundled in the dynamic library:
Coq_hello
G_hello
- Append the following lines in
plugins/plugins{byte,opt}.itarget
:
in in pluginsopt.itarget
:
hello/hello_plugin.cmxa
in pluginsbyte.itarget
:
hello/hello_plugin.cma
-
In the root directory of the Coq source location, modify the file
Makefile.common
:- add
hello
to theSRCDIR
definition (second argument of theaddprefix
function); - in the section “Object and Source files”, add
HELLOCMA:=plugins/hello/hello_plugin.cma
; - add
$(HELLOCMA)
to thePLUGINSCMA
definition.
- add
-
Modify the file
Makefile.build
, adding in section “3) plugins” the line:
hello: $(HELLOCMA)
- From the command line, run
make hello
, thenmake plugins/hello/hello_plugin.cmxs
.
The call to the tactic hello
from a Coq script has to be preceded by Declare ML Module "hello_plugin"
,
which will load the dynamic object hello_plugin.cmxs
. For instance:
Declare ML Module "hello_plugin".
Variable A:Prop.
Goal A-> A.
Proof.
hello.
auto.
Qed.
To the extent possible under law, the contributors of “Cocorico!, the Coq wiki” have waived all copyright and related or neighboring rights to their contributions.
By contributing to Cocorico!, the Coq wiki, you agree that you hold the copyright and you agree to license your contribution under the CC0 license or you agree that you have permission to distribute your contribution under the CC0 license.