<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Thomas Letan's Blog - coqffi</title>
    <link>https://soap.coffee/~lthms/tags/coqffi.html</link>
    <description>Posts tagged "coqffi"</description>
    <atom:link href="https://soap.coffee/~lthms/tags/coqffi.xml" rel="self"
               type="application/rss+xml" />
    
    
    <item>
      <title>What happened in October and November 2022?</title>
      <link>https://soap.coffee/~lthms/posts/November2022.html</link>
      <guid>https://soap.coffee/~lthms/posts/November2022.html</guid>
      <pubDate>November 19, 2022</pubDate>
      <description>
        
        &lt;h1&gt;What happened in October and November 2022?&lt;/h1&gt;&lt;div id=&quot;tags-list&quot;&gt;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/spatial-shell.html&quot; class=&quot;tag hover-periwinkle&quot; marked=&quot;&quot;&gt;spatial-shell&lt;/a&gt; &lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/nanowrimo.html&quot; class=&quot;tag hover-peach&quot; marked=&quot;&quot;&gt;nanowrimo&lt;/a&gt; &lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/coqffi.html&quot; class=&quot;tag hover-coral&quot; marked=&quot;&quot;&gt;coqffi&lt;/a&gt; &lt;/div&gt;
&lt;p&gt;It is November 19 today, and I’m one month and 4 days late for the October
Retrospective! Truth is, &lt;code class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-variable&quot;&gt;$WORK&lt;/span&gt;&lt;/code&gt; has been intense lately, to a point
where I have not made much progress on my side projects. Anyway.&lt;/p&gt;
&lt;p&gt;I have implemented the last feature I was really missing in my daily
use of Spatial Sway: moving windows to adjacent workspaces. As a
result, I think I can say that Spatial Sway has really reached the
“Minimum Viable Product” stage, with a convenient UX, and a nice
enough UI. It is still lacking when it comes to configurability,
though. It is the next item of my TODO list, but I have no idea when I
will implement the support for a configuration file.&lt;/p&gt;
&lt;p&gt;Another highlight of the past two months was the
&lt;a href=&quot;https://nanowrimo.org&quot; class=&quot;hover-sky&quot; marked=&quot;&quot;&gt;NaNoWriMo&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#external-link&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;. I took the last week of October and the
first week of November off to plan and start writing a fiction project for it.
Writing again was really nice, and I even gave writing fiction in English a
shot. That made me uncover a bug in the English support of
&lt;a href=&quot;https://crates.io/crates/ogam&quot; class=&quot;hover-peach&quot; marked=&quot;&quot;&gt;ogam&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#external-link&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;, my markup language for fiction writers,
which led me to publish a fix on Crates.io. However, as soon as I came back to
&lt;code class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-variable&quot;&gt;$WORK&lt;/span&gt;&lt;/code&gt;, my writing spree ended. That’s okay, though. It gave me plenty
of ideas for future sessions. Thanks, NaNoWriMo! Sorry to quit so soon, and see
you next year, maybe.&lt;/p&gt;
&lt;p&gt;Finally, a nice surprise of the past month is that &lt;a href=&quot;https://github.com/ocaml/dune/pull/6489&quot; class=&quot;hover-periwinkle&quot; marked=&quot;&quot;&gt;someone has started working
on adding proper support for &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; to
&lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt;&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;, the build system for OCaml
and Coq! I’m thrilled by this. Thanks,
&lt;a href=&quot;https://github.com/Alizter&quot; class=&quot;hover-lemon&quot; marked=&quot;&quot;&gt;&lt;strong&gt;@Alizter&lt;/strong&gt;&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;This wraps up this retrospective. I hope I will have more interesting,
concrete news to share next month.&lt;/p&gt;
        
      </description>
    </item>
    
    
    
    <item>
      <title>Implementing an Echo Server in Coq with coqffi.1.0.0</title>
      <link>https://soap.coffee/~lthms/posts/CoqffiEcho.html</link>
      <guid>https://soap.coffee/~lthms/posts/CoqffiEcho.html</guid>
      <pubDate>December 10, 2020</pubDate>
      <description>
        
        &lt;h1&gt;Implementing an Echo Server in Coq with &lt;code class=&quot;hljs&quot;&gt;coqffi.1.0.0&lt;/code&gt;&lt;/h1&gt;&lt;div id=&quot;tags-list&quot;&gt;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/coq.html&quot; class=&quot;tag hover-coral&quot; marked=&quot;&quot;&gt;coq&lt;/a&gt; &lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/ocaml.html&quot; class=&quot;tag hover-peach&quot; marked=&quot;&quot;&gt;ocaml&lt;/a&gt; &lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/coqffi.html&quot; class=&quot;tag hover-lemon&quot; marked=&quot;&quot;&gt;coqffi&lt;/a&gt; &lt;/div&gt;
&lt;p&gt;In this article, we will demonstrate how &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; can be used to
implement an echo server, &lt;em&gt;i.e.&lt;/em&gt;, a TCP server which sends back any
input it receives from its clients.  In addition to &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;, you will need to
install &lt;code class=&quot;hljs&quot;&gt;coq-simple-io&lt;/code&gt;.  The latter is available in the &lt;a href=&quot;https://github.com/coq/opam-coq-archive&quot; class=&quot;hover-sky&quot; marked=&quot;&quot;&gt;&lt;code class=&quot;hljs&quot;&gt;released&lt;/code&gt; repository
of the Opam Coq Archive&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-bash&quot;&gt;opam install coq-coqffi coq-simple-io
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Besides,  you can download &lt;a href=&quot;/~lthms/files/coqffi-tutorial.tar.gz&quot; class=&quot;hover-mint&quot; marked=&quot;&quot;&gt;the source tree presented in this
article&lt;/a&gt; if you want to try to read the source
directly, or modify it to your taste.&lt;/p&gt;
&lt;h2&gt;Project Layout&lt;/h2&gt;
&lt;p&gt;Before diving too much into the implementation of our echo server, we
first give an overview of the resulting project’s layout. Since we aim
at implementing a program, we draw our inspiration from the idiomatic
way of organizing a OCaml project.&lt;/p&gt;
&lt;p&gt;We have three directories at the root of the project.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code class=&quot;hljs&quot;&gt;ffi/&lt;/code&gt; contains the low-level OCaml code:&lt;/strong&gt;
It provides an OCaml library (&lt;code class=&quot;hljs&quot;&gt;ffi&lt;/code&gt;), and a Coq theory (&lt;code class=&quot;hljs language-coq&quot;&gt;FFI&lt;/code&gt;) which
gathers the FFI modules generated by &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code class=&quot;hljs&quot;&gt;src/&lt;/code&gt; contains the Coq implementation of our echo server:&lt;/strong&gt; It provides a
Coq theory (&lt;code class=&quot;hljs language-coq&quot;&gt;Echo&lt;/code&gt;) which depends on the &lt;code class=&quot;hljs language-coq&quot;&gt;FFI&lt;/code&gt; theory the
&lt;code class=&quot;hljs language-coq&quot;&gt;SimpleIO&lt;/code&gt; theory of &lt;code class=&quot;hljs&quot;&gt;coq-simple~io&lt;/code&gt;. This theory provides the
implementation of our echo server in Coq.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code class=&quot;hljs&quot;&gt;bin/&lt;/code&gt; contains the pieces of code to get an executable program:&lt;/strong&gt; It
contains a Coq module (&lt;code class=&quot;hljs&quot;&gt;echo.v&lt;/code&gt;) which configures and uses the extraction
mechanism to generate an OCaml module (&lt;code class=&quot;hljs&quot;&gt;echo.ml&lt;/code&gt;). This OCaml module can be
compiled to get an executable program.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that we could have decided to only have one Coq theory. We could
also have added a fourth directory (&lt;code class=&quot;hljs&quot;&gt;theories/&lt;/code&gt;) for formal
verification specific code, but this is out of the scope of this
tutorial.&lt;/p&gt;
&lt;p&gt;Overall, we use &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; to compile and compose the different parts of
the echo server. &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; has a native —yet unstable at the time of
writing— support for building Coq projects, with very convenient
stanzas like &lt;code class=&quot;hljs&quot;&gt;coq.theory&lt;/code&gt; and &lt;code class=&quot;hljs&quot;&gt;coq.extraction&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The following graph summarizes the dependencies between each component
(plain arrows symbolize software dependencies).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;figure&gt;&lt;img src=&quot;/~lthms/img/echo-deps.svg&quot; alt=&quot;The echo server dependy graph&quot;&gt;&lt;figcaption&gt;&lt;p&gt;The echo server dependency graph. Dashed boxes are generated.&lt;/p&gt;&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We enable Coq-related stanza with &lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;using&lt;/span&gt; coq &lt;span class=&quot;hljs-number&quot;&gt;0.2&lt;/span&gt;)&lt;/code&gt; in the
&lt;code class=&quot;hljs language-dune&quot;&gt;dune-project&lt;/code&gt;. file.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;lang&lt;/span&gt; dune &lt;span class=&quot;hljs-number&quot;&gt;2.7&lt;/span&gt;)
(&lt;span class=&quot;hljs-name&quot;&gt;using&lt;/span&gt; coq &lt;span class=&quot;hljs-number&quot;&gt;0.2&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rest of this tutorial proceeds by diving into each directory.&lt;/p&gt;
&lt;h2&gt;FFI Bindings&lt;/h2&gt;
&lt;p&gt;Our objective is to implement an echo server, &lt;em&gt;i.e.&lt;/em&gt;, a server which
(1) accepts incoming connections, and (2) sends back any incoming
messages. We will consider two classes of effects. One is related to
creating and manipulating TCP sockets. The other is dedicated to
process management, more precisely to be able to fork when receiving
incoming connections.&lt;/p&gt;
&lt;p&gt;Therefore, the &lt;code class=&quot;hljs&quot;&gt;ffi&lt;/code&gt; library will provide two modules. Likewise, the
&lt;code class=&quot;hljs language-coq&quot;&gt;FFI&lt;/code&gt; theory will provide two analogous modules generated by &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the &lt;code class=&quot;hljs&quot;&gt;ffi/&lt;/code&gt; directory, we add the following stanza to the &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; file.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;library&lt;/span&gt;
  (&lt;span class=&quot;hljs-name&quot;&gt;name&lt;/span&gt; ffi)
  (&lt;span class=&quot;hljs-name&quot;&gt;libraries&lt;/span&gt; unix))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; will look for any &lt;code class=&quot;hljs&quot;&gt;.ml&lt;/code&gt; and &lt;code class=&quot;hljs&quot;&gt;.mli&lt;/code&gt; files within the directory and will
consider they belong to the &lt;code class=&quot;hljs&quot;&gt;ffi&lt;/code&gt; library. We use the
&lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html&quot; class=&quot;hover-peach&quot; marked=&quot;&quot;&gt;&lt;code class=&quot;hljs&quot;&gt;unix&lt;/code&gt;&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#external-link&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt; library
to implement the features we are looking for.&lt;/p&gt;
&lt;p&gt;Then, we add the following stanza to the &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; file of the &lt;code class=&quot;hljs&quot;&gt;ffi/&lt;/code&gt;
directory.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;coq&lt;/span&gt;.theory
  (&lt;span class=&quot;hljs-name&quot;&gt;name&lt;/span&gt; FFI))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This tells &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; to look for &lt;code class=&quot;hljs&quot;&gt;.v&lt;/code&gt; file within the &lt;code class=&quot;hljs&quot;&gt;ffi/&lt;/code&gt; directory,
in order to build them with Coq.  A nice feature of &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; is that if we
automatically generate Coq files, they will be automatically “attached” to this
theory.&lt;/p&gt;
&lt;h3&gt;Sockets&lt;/h3&gt;
&lt;p&gt;Sockets are boring. The following OCaml module interface provides the
necessary type and functions to manipulate them.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; socket_descr

&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; open_socket : &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; -&amp;gt; socket_descr
&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; listen : socket_descr -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;unit&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; recv : socket_descr -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; send : socket_descr -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; accept_connection : socket_descr -&amp;gt; socket_descr
&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; close_socket : socket_descr -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;unit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our focus is how to write the interface modules for &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;. Since the object
of this tutorial is not the implementation of an echo server in itself, the
implementation details of the &lt;code class=&quot;hljs&quot;&gt;ffi&lt;/code&gt; library will not be discussed, but is
provided at the end of this article.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; generates &lt;code class=&quot;hljs&quot;&gt;.cmi&lt;/code&gt; files for the &lt;code class=&quot;hljs&quot;&gt;.mli&lt;/code&gt; files of our library, and
provides the necessary bits to easily locate them. Besides, the
&lt;code class=&quot;hljs&quot;&gt;action&lt;/code&gt; stanza can be used here to tell to &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; how to generate the
module &lt;code class=&quot;hljs&quot;&gt;Socket.v&lt;/code&gt; from &lt;code class=&quot;hljs&quot;&gt;file.cmi&lt;/code&gt;. We add the following entry to
&lt;code class=&quot;hljs&quot;&gt;ffi/dune&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;rule&lt;/span&gt;
  (&lt;span class=&quot;hljs-name&quot;&gt;target&lt;/span&gt; Socket.v)
  (&lt;span class=&quot;hljs-name&quot;&gt;action&lt;/span&gt; (&lt;span class=&quot;hljs-name&quot;&gt;run&lt;/span&gt; coqffi %{cmi&lt;span class=&quot;hljs-symbol&quot;&gt;:socket&lt;/span&gt;} -o %{target})))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We call &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; without any feature-related command-line argument,
which means only the &lt;code class=&quot;hljs&quot;&gt;simple-io&lt;/code&gt; feature is enabled. As a consequence,
the &lt;code class=&quot;hljs&quot;&gt;socket_descr&lt;/code&gt; type is axiomatized in Coq, and in addition to a
&lt;code class=&quot;hljs&quot;&gt;MonadSocket&lt;/code&gt; monad, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; will generate an instance for this monad
for the &lt;code class=&quot;hljs&quot;&gt;IO&lt;/code&gt; monad of &lt;code class=&quot;hljs&quot;&gt;coq-simple-io&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The stanza generates the following Coq module.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;(* This file has been generated by coqffi. *)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Implicit&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Arguments&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Unset&lt;/span&gt; Strict &lt;span class=&quot;hljs-keyword&quot;&gt;Implicit&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Set&lt;/span&gt; Contextual &lt;span class=&quot;hljs-keyword&quot;&gt;Implicit&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Generalizable&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;All&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Variables&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Close&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Scope&lt;/span&gt; nat_scope.

&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; CoqFFI &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Export&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Extraction&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; SimpleIO &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; IO_Monad.

&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; socket_descr : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; socket_descr =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;Ffi.Socket.socket_descr&quot;&lt;/span&gt;.

&lt;span class=&quot;hljs-comment&quot;&gt;(** * Impure Primitives *)&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot;&gt;(** ** Monad Definition *)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;Class&lt;/span&gt; MonadSocket (m : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
  { open_socket : string -&amp;gt; i63 -&amp;gt; m socket_descr
  ; listen : socket_descr -&amp;gt; m unit
  ; recv : socket_descr -&amp;gt; m string
  ; send : socket_descr -&amp;gt; string -&amp;gt; m i63
  ; accept_connection : socket_descr -&amp;gt; m socket_descr
  ; close_socket : socket_descr -&amp;gt; m unit
  }.

&lt;span class=&quot;hljs-comment&quot;&gt;(** ** [IO] Instance *)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_open_socket : string -&amp;gt; i63 -&amp;gt; IO socket_descr.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_listen : socket_descr -&amp;gt; IO unit.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_recv : socket_descr -&amp;gt; IO string.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_send : socket_descr -&amp;gt; string -&amp;gt; IO i63.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_accept_connection : socket_descr -&amp;gt; IO socket_descr.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_close_socket : socket_descr -&amp;gt; IO unit.

&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; io_open_socket
  =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;(fun x1 x2 k__ -&amp;gt; k__ ((Ffi.Socket.open_socket x1 x2)))&quot;&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; io_listen =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;(fun x1 k__ -&amp;gt; k__ ((Ffi.Socket.listen x1)))&quot;&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; io_recv =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;(fun x1 k__ -&amp;gt; k__ ((Ffi.Socket.recv x1)))&quot;&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; io_send
  =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;(fun x1 x2 k__ -&amp;gt; k__ ((Ffi.Socket.send x1 x2)))&quot;&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; io_accept_connection
  =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;(fun x1 k__ -&amp;gt; k__ ((Ffi.Socket.accept_connection x1)))&quot;&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; io_close_socket
  =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;(fun x1 k__ -&amp;gt; k__ ((Ffi.Socket.close_socket x1)))&quot;&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Instance&lt;/span&gt; IO_MonadSocket : MonadSocket IO :=
  { open_socket := io_open_socket
  ; listen := io_listen
  ; recv := io_recv
  ; send := io_send
  ; accept_connection := io_accept_connection
  ; close_socket := io_close_socket
  }.

&lt;span class=&quot;hljs-comment&quot;&gt;(* The generated file ends here. *)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Process Management&lt;/h3&gt;
&lt;p&gt;In order to avoid a client to block the server by connecting to it
without sending anything, we can fork a new process for each client.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; identity = &lt;span class=&quot;hljs-type&quot;&gt;Parent&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; | &lt;span class=&quot;hljs-type&quot;&gt;Child&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; fork : &lt;span class=&quot;hljs-built_in&quot;&gt;unit&lt;/span&gt; -&amp;gt; identity
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This time, the &lt;code class=&quot;hljs&quot;&gt;proc.mli&lt;/code&gt; module interface introduces a transparent
type, /i.e./, it also provides its definition. This is a good use case
for the &lt;code class=&quot;hljs&quot;&gt;transparent-types&lt;/code&gt; feature of &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;. In the stanza for
generating &lt;code class=&quot;hljs&quot;&gt;Proc.v&lt;/code&gt;, we enable it with the &lt;code class=&quot;hljs&quot;&gt;-ftransparent-types&lt;/code&gt;
command-line argument, like this.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;rule&lt;/span&gt;
  (&lt;span class=&quot;hljs-name&quot;&gt;target&lt;/span&gt; Proc.v)
  (&lt;span class=&quot;hljs-name&quot;&gt;action&lt;/span&gt; (&lt;span class=&quot;hljs-name&quot;&gt;run&lt;/span&gt; coqffi -ftransparent-types %{cmi&lt;span class=&quot;hljs-symbol&quot;&gt;:proc&lt;/span&gt;} -o %{target})))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which generates the following Coq module.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;(* This file has been generated by coqffi. *)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Implicit&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Arguments&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Unset&lt;/span&gt; Strict &lt;span class=&quot;hljs-keyword&quot;&gt;Implicit&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Set&lt;/span&gt; Contextual &lt;span class=&quot;hljs-keyword&quot;&gt;Implicit&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Generalizable&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;All&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Variables&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Close&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Scope&lt;/span&gt; nat_scope.

&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; CoqFFI &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Export&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Extraction&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; SimpleIO &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; IO_Monad.

&lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; identity : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
| &lt;span class=&quot;hljs-type&quot;&gt;Parent&lt;/span&gt; (x0 : i63) : identity
| &lt;span class=&quot;hljs-type&quot;&gt;Child&lt;/span&gt; : identity.

&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; identity =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;Ffi.Proc.identity&quot;&lt;/span&gt;
  [ &lt;span class=&quot;hljs-string&quot;&gt;&quot;Ffi.Proc.Parent&quot;&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;Ffi.Proc.Child&quot;&lt;/span&gt; ].

&lt;span class=&quot;hljs-comment&quot;&gt;(** * Impure Primitives *)&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot;&gt;(** ** Monad Definition *)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;Class&lt;/span&gt; MonadProc (m : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; := { fork : unit -&amp;gt; m identity
                                             }.

&lt;span class=&quot;hljs-comment&quot;&gt;(** ** [IO] Instance *)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_fork : unit -&amp;gt; IO identity.

&lt;span class=&quot;hljs-keyword&quot;&gt;Extract&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Constant&lt;/span&gt; io_fork =&amp;gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;(fun x1 k__ -&amp;gt; k__ ((Ffi.Proc.fork x1)))&quot;&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Instance&lt;/span&gt; IO_MonadProc : MonadProc IO := { fork := io_fork
                                        }.

&lt;span class=&quot;hljs-comment&quot;&gt;(* The generated file ends here. *)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We now have everything we need to implement an echo server in Coq.&lt;/p&gt;
&lt;h2&gt;Implementing an Echo Server&lt;/h2&gt;
&lt;p&gt;Our implementation will be part of a dedicated Coq theory, called &lt;code class=&quot;hljs language-coq&quot;&gt;Echo&lt;/code&gt;.
This is done easily a &lt;code class=&quot;hljs language-coq&quot;&gt;dune&lt;/code&gt; file in the &lt;code class=&quot;hljs&quot;&gt;src/&lt;/code&gt; directory, with the
following content.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;coq&lt;/span&gt;.theory
  (&lt;span class=&quot;hljs-name&quot;&gt;name&lt;/span&gt; Echo)
  (&lt;span class=&quot;hljs-name&quot;&gt;theories&lt;/span&gt; FFI))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the rest of this section, we will discuss the content of the unique
module of this theory. Hopefully, readers familiar with programming
impurity by means of monads will not find anything particularly
surprising here.&lt;/p&gt;
&lt;p&gt;Let us start with the inevitable sequence of import commands. We use
the &lt;code class=&quot;hljs language-coq&quot;&gt;Monad&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;MonadFix&lt;/code&gt; typeclasses of &lt;code class=&quot;hljs language-coq&quot;&gt;ExtLib&lt;/code&gt;, and our
FFI modules from the &lt;code class=&quot;hljs language-coq&quot;&gt;FFI&lt;/code&gt; theory we have previously defined.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; ExtLib &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; Monad MonadFix.
&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; FFI &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; Proc Socket.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Letting Coq guess the type of unintroduced variables using the &lt;code class=&quot;hljs&quot;&gt;`&lt;/code&gt;
annotation (&lt;em&gt;e.g.&lt;/em&gt;, in presence of&lt;code class=&quot;hljs language-coq&quot;&gt; `{Monad m}&lt;/code&gt;, Coq understands &lt;code class=&quot;hljs&quot;&gt;m&lt;/code&gt;
is of type &lt;code class=&quot;hljs&quot;&gt;Type -&amp;gt; Type&lt;/code&gt;) is always nice, so we enable it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Generalizable&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;All&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Variables&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We enable the monad notation provided by &lt;code class=&quot;hljs&quot;&gt;ExtLib&lt;/code&gt;. In this article, we
prefer the &lt;code class=&quot;hljs&quot;&gt;let*&lt;/code&gt; notation (as recently introduced by OCaml) over the
&lt;code class=&quot;hljs&quot;&gt;&amp;lt;-&lt;/code&gt; notation of Haskell, but both are available.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; MonadLetNotation.
&lt;span class=&quot;hljs-keyword&quot;&gt;Open&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Scope&lt;/span&gt; monad_scope.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, we define a notation to be able to define local, monadic
recursive functions using the &lt;code class=&quot;hljs&quot;&gt;mfix&lt;/code&gt; combinator of the &lt;code class=&quot;hljs&quot;&gt;MonadFix&lt;/code&gt;
typeclass.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Notation&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;&apos;let_rec*&apos; f x &apos;:`&apos; p &apos;in&apos; q&quot;&lt;/span&gt; :`
  (&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; f :` mfix (&lt;span class=&quot;hljs-keyword&quot;&gt;fun&lt;/span&gt; f x `&amp;gt; p) &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; q)
    (&lt;span class=&quot;hljs-built_in&quot;&gt;at&lt;/span&gt; level &lt;span class=&quot;hljs-number&quot;&gt;61&lt;/span&gt;, x &lt;span class=&quot;hljs-built_in&quot;&gt;pattern&lt;/span&gt;, f name, q &lt;span class=&quot;hljs-built_in&quot;&gt;at&lt;/span&gt; next level, &lt;span class=&quot;hljs-built_in&quot;&gt;right&lt;/span&gt; associativity).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that &lt;code class=&quot;hljs&quot;&gt;mfix&lt;/code&gt; does /not/ check whether or not the defined function
will terminate (contrary to the &lt;code class=&quot;hljs&quot;&gt;fix&lt;/code&gt; keyword of Coq). This is
fortunate because in our case, we do not want our echo server to
converge, but rather to accept an infinite number of connections.&lt;/p&gt;
&lt;p&gt;We can demonstrate how this notation can be leveraged by defining a
generic TCP server, parameterized by a handler to deal with incoming
connections.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; tcp_srv `{Monad m, MonadFix m, MonadProc m, MonadSocket m}
    (handler : socket_descr -&amp;gt; m unit)
  : m unit :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt;* srv := open_socket &lt;span class=&quot;hljs-string&quot;&gt;&quot;127.0.0.1&quot;&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;8888&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt;
  listen srv;;

  let_rec* tcp_aux &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; :=
    &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt;* client := accept_connection srv &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt;* res := fork tt &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; res &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
    | &lt;span class=&quot;hljs-type&quot;&gt;Parent&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; =&amp;gt; close_socket client &amp;gt;&amp;gt;= tcp_aux
    | &lt;span class=&quot;hljs-type&quot;&gt;Child&lt;/span&gt; =&amp;gt;  handler client
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt;

  tcp_aux tt.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The handler for the echo server is straightforward: it just reads
incoming bytes from the socket, sends it back, and closes the socket.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; echo_handler `{Monad m, MonadSocket m} (sock : socket_descr)
  : m unit :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt;* msg := recv sock &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt;
  send sock msg;;
  close_socket sock.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Composing our generic TCP server with our echo handler gives us an
echo server.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; echo_server `{Monad m, MonadFix m, MonadProc m, MonadSocket m}
  : m unit :=
  tcp_srv echo_handler.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; has generated typeclasses for the impure primitives
of &lt;code class=&quot;hljs&quot;&gt;proc.mli&lt;/code&gt; and &lt;code class=&quot;hljs&quot;&gt;socket.mli&lt;/code&gt;, &lt;code class=&quot;hljs&quot;&gt;echo_server&lt;/code&gt; is polymorphic, and can
be instantiated for different monads. When it comes to extracting our
program, we will generally prefer the &lt;code class=&quot;hljs&quot;&gt;IO&lt;/code&gt; monad of &lt;code class=&quot;hljs&quot;&gt;coq-simple-io&lt;/code&gt;.
But we could also imagine verifying the client handler with FreeSpec,
or the generic TCP server with Interaction Trees (which support
diverging computations). Overall, we can have different verification
strategies for different parts of our program, by leveraging the most
relevant framework for each part, yet being able to extract it in an
efficient form.&lt;/p&gt;
&lt;p&gt;The next section shows how this last part is achieved using, once
again, a convenient stanza of dune.&lt;/p&gt;
&lt;h2&gt;Extracting and Building an Executable&lt;/h2&gt;
&lt;p&gt;The &lt;code class=&quot;hljs&quot;&gt;0.2&lt;/code&gt; version of the Coq-related stanzas of &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; provides the
&lt;code class=&quot;hljs&quot;&gt;coq.extraction&lt;/code&gt; stanza, which can be used to build a Coq module
expected to generate &lt;code class=&quot;hljs&quot;&gt;ml&lt;/code&gt; files.&lt;/p&gt;
&lt;p&gt;In our case, we will write &lt;code class=&quot;hljs&quot;&gt;bin/echo.v&lt;/code&gt; to extract the &lt;code class=&quot;hljs&quot;&gt;echo_server&lt;/code&gt;
in a &lt;code class=&quot;hljs&quot;&gt;echo.ml&lt;/code&gt; module, and uses the &lt;code class=&quot;hljs&quot;&gt;executable&lt;/code&gt; stanza of &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; to
get an executable from this file. To achieve this, the &lt;code class=&quot;hljs&quot;&gt;bin/dune&lt;/code&gt;
file simply requires these two stanzas.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-lisp&quot;&gt;(&lt;span class=&quot;hljs-name&quot;&gt;coq&lt;/span&gt;.extraction
  (&lt;span class=&quot;hljs-name&quot;&gt;prelude&lt;/span&gt; echo)
  (&lt;span class=&quot;hljs-name&quot;&gt;theories&lt;/span&gt; Echo)
  (&lt;span class=&quot;hljs-name&quot;&gt;extracted_modules&lt;/span&gt; echo))

(&lt;span class=&quot;hljs-name&quot;&gt;executable&lt;/span&gt;
  (&lt;span class=&quot;hljs-name&quot;&gt;name&lt;/span&gt; echo)
  (&lt;span class=&quot;hljs-name&quot;&gt;libraries&lt;/span&gt; ffi))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We are almost done. We now need to write the &lt;code class=&quot;hljs&quot;&gt;echo.v&lt;/code&gt; module, which
mostly consists of (1) providing a &lt;code class=&quot;hljs&quot;&gt;MonadFix&lt;/code&gt; instance for the &lt;code class=&quot;hljs&quot;&gt;IO&lt;/code&gt;
monad, (2) using the &lt;code class=&quot;hljs&quot;&gt;IO.unsafe_run&lt;/code&gt; function to escape the &lt;code class=&quot;hljs&quot;&gt;IO&lt;/code&gt;
monad, (3) calling the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Extraction&lt;/span&gt;&lt;/code&gt; command to wrap it up.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; Coq &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Extraction&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; ExtLib &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; MonadFix.
&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; SimpleIO &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; SimpleIO.
&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; Echo &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; Server.

&lt;span class=&quot;hljs-keyword&quot;&gt;Instance&lt;/span&gt; MonadFix_IO : MonadFix IO :=
  { mfix := @IO.fix_io }.

&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; main : io_unit :=
  IO.unsafe_run echo_server.

&lt;span class=&quot;hljs-keyword&quot;&gt;Extraction&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;echo.ml&quot;&lt;/span&gt; main.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since we are using the &lt;code class=&quot;hljs language-coq&quot;&gt;i63&lt;/code&gt; type (signed 63bits integers) of the
&lt;code class=&quot;hljs&quot;&gt;CoqFFI&lt;/code&gt; theory, and since &lt;code class=&quot;hljs language-coq&quot;&gt;i63&lt;/code&gt; is implemented under the hood with Coq
primitive integers, we &lt;em&gt;also&lt;/em&gt; need to provide a &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;Uint63&lt;/span&gt;&lt;/code&gt; module with a
&lt;code class=&quot;hljs language-ocaml&quot;&gt;of_int&lt;/code&gt; function. Fortunately, this module is straightforward to
write.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; of_int x = x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And &lt;em&gt;voilà&lt;/em&gt;. A call to &lt;code class=&quot;hljs&quot;&gt;dune&lt;/code&gt; at the root of the repository will
build everything (Coq and OCaml alike). Starting the echo server
is as simple as&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-bash&quot;&gt;dune &lt;span class=&quot;hljs-built_in&quot;&gt;exec&lt;/span&gt; bin/echo.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And connecting to it can be achieved with a program like &lt;code class=&quot;hljs&quot;&gt;telnet&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-console&quot;&gt;&lt;span class=&quot;hljs-meta prompt_&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;language-bash&quot;&gt;telnet 127.0.0.1 8888&lt;/span&gt;
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is &apos;^]&apos;.
hello, echo server!
hello, echo server!
Connection closed by foreign host.
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Appendix&lt;/h2&gt;
&lt;h3&gt;The &lt;code class=&quot;hljs&quot;&gt;Socket&lt;/code&gt; OCaml Module&lt;/h3&gt;
&lt;p&gt;There is not much to say, except that (as already stated) we use the
&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;&lt;/code&gt; module to manipulate sockets, and we attach to each socket a
buffer to store incoming bytes.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; buffer_size = &lt;span class=&quot;hljs-number&quot;&gt;1024&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; socket_descr = {
  fd : &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;.file_descr;
  recv_buffer : &lt;span class=&quot;hljs-built_in&quot;&gt;bytes&lt;/span&gt;;
}

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; from_fd fd =
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; rbuff = &lt;span class=&quot;hljs-type&quot;&gt;Bytes&lt;/span&gt;.create buffer_size &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;
  { fd ` fd; recv_buffer ` rbuff }

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; open_socket hostname port =
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;open&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; addr = inet_addr_of_string hostname &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; fd = socket &lt;span class=&quot;hljs-type&quot;&gt;PF_INET&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;SOCK_STREAM&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;
  setsockopt fd &lt;span class=&quot;hljs-type&quot;&gt;SO_REUSEADDR&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;true&lt;/span&gt;;
  bind fd (&lt;span class=&quot;hljs-type&quot;&gt;ADDR_INET&lt;/span&gt; (addr, port));
  from_fd fd

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; listen sock = &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;.listen sock.fd &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; recv sock =
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; s = &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;.read sock.fd sock.recv_buffer &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; buffer_size &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;
  &lt;span class=&quot;hljs-type&quot;&gt;Bytes&lt;/span&gt;.sub_string sock.recv_buffer &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; s

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; send sock msg =
  &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;.write_substring sock.fd msg &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt;.length msg)

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; accept_connection sock =
  &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;.accept sock.fd |&amp;gt; fst |&amp;gt; from_fd

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; close_socket sock = &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;.close sock.fd
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;The &lt;code class=&quot;hljs&quot;&gt;Proc&lt;/code&gt; OCaml Module&lt;/h3&gt;
&lt;p&gt;Thanks to the &lt;code class=&quot;hljs&quot;&gt;Unix&lt;/code&gt; module, the implementation is pretty straightforward.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; identity = &lt;span class=&quot;hljs-type&quot;&gt;Parent&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; | &lt;span class=&quot;hljs-type&quot;&gt;Child&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; fork x =
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Unix&lt;/span&gt;.fork x &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;Child&lt;/span&gt;
  | x -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;Parent&lt;/span&gt; x
&lt;/code&gt;&lt;/pre&gt;
        
      </description>
    </item>
    
    
    
    <item>
      <title>coqffi.1.0.0 In A Nutshell</title>
      <link>https://soap.coffee/~lthms/posts/Coqffi-1-0-0.html</link>
      <guid>https://soap.coffee/~lthms/posts/Coqffi-1-0-0.html</guid>
      <pubDate>December 10, 2020</pubDate>
      <description>
        
        &lt;h1&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi.1.0.0&lt;/code&gt; In A Nutshell&lt;/h1&gt;&lt;div id=&quot;tags-list&quot;&gt;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/coq.html&quot; class=&quot;tag hover-lemon&quot; marked=&quot;&quot;&gt;coq&lt;/a&gt; &lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/ocaml.html&quot; class=&quot;tag hover-sky&quot; marked=&quot;&quot;&gt;ocaml&lt;/a&gt; &lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/coqffi.html&quot; class=&quot;tag hover-lavender&quot; marked=&quot;&quot;&gt;coqffi&lt;/a&gt; &lt;/div&gt;
&lt;p&gt;For each entry of a &lt;code class=&quot;hljs&quot;&gt;cmi&lt;/code&gt; file (a &lt;em&gt;compiled&lt;/em&gt; &lt;code class=&quot;hljs&quot;&gt;mli&lt;/code&gt; file), &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;
tries to generate an equivalent (from the extraction mechanism
perspective) Coq definition. In this article, we walk through how
&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; works.&lt;/p&gt;
&lt;p&gt;Note that we do not dive into the vernacular commands &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;
generates. They are of no concern for users of &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Getting Started&lt;/h2&gt;
&lt;h3&gt;Requirements&lt;/h3&gt;
&lt;p&gt;The latest version of &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; (&lt;code class=&quot;hljs&quot;&gt;1.0.0~beta8&lt;/code&gt;)
is compatible with OCaml &lt;code class=&quot;hljs&quot;&gt;4.08&lt;/code&gt; up to &lt;code class=&quot;hljs&quot;&gt;4.14&lt;/code&gt;, and Coq &lt;code class=&quot;hljs&quot;&gt;8.12&lt;/code&gt; up top
&lt;code class=&quot;hljs&quot;&gt;8.13&lt;/code&gt;.  If you want to use &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;, but have incompatible
requirements of your own, feel free to
&lt;a href=&quot;https://github.com/coq-community/coqffi/issues&quot; class=&quot;hover-lemon&quot; marked=&quot;&quot;&gt;submit an issue&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Installing &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The recommended way to install &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; is through the
&lt;a href=&quot;https://coq.inria.fr/opam/www&quot; class=&quot;hover-rose&quot; marked=&quot;&quot;&gt;Opam Coq Archive&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#external-link&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;, in the &lt;code class=&quot;hljs&quot;&gt;released&lt;/code&gt;
repository.  If you haven’t activated this repository yet, you can use the
following bash command.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-bash&quot;&gt;opam repo add coq-released https://coq.inria.fr/opam/released
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, installing &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; is as simple as&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-bash&quot;&gt;opam install coq-coqffi
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also get the source from &lt;a href=&quot;https://github.com/coq-community/coqffi&quot; class=&quot;hover-sky&quot; marked=&quot;&quot;&gt;the upstream &lt;code class=&quot;hljs&quot;&gt;git&lt;/code&gt;
repository&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;. The &lt;code class=&quot;hljs&quot;&gt;README&lt;/code&gt; provides the
necessary pieces of information to build it from source.&lt;/p&gt;
&lt;h3&gt;Additional Dependencies&lt;/h3&gt;
&lt;p&gt;One major difference between Coq and OCaml is that the former is pure,
while the latter is not. Impurity can be modeled in pure languages,
and Coq does not lack of frameworks in this respect. &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; currently
supports two of them:
&lt;a href=&quot;https://github.com/Lysxia/coq-simple-io&quot; class=&quot;hover-coral&quot; marked=&quot;&quot;&gt;&lt;code class=&quot;hljs&quot;&gt;coq-simple-io&lt;/code&gt;&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt; and
&lt;a href=&quot;https://github.com/ANSSI-FR/FreeSpec&quot; class=&quot;hover-mint&quot; marked=&quot;&quot;&gt;FreeSpec&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;. It is also possible to use it
with &lt;a href=&quot;https://github.com/DeepSpec/InteractionTrees&quot; class=&quot;hover-rose&quot; marked=&quot;&quot;&gt;Interaction Trees&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;, albeit
in a less direct manner.&lt;/p&gt;
&lt;h3&gt;Primitive Types&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; supports a set of primitive types, &lt;em&gt;i.e.&lt;/em&gt;, a set of OCaml
types for which it knows an equivalent type in Coq. The list is the
following (the Coq types are fully qualified in the table, but not in
the generated Coq module as the necessary &lt;code class=&quot;hljs&quot;&gt;Import&lt;/code&gt; statements are
generated too).&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;OCaml type&lt;/th&gt;
&lt;th&gt;Coq type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;bool&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;Coq.Init.Datatypes.bool&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;char&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;Coq.Strings.Ascii.ascii&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;CoqFFI.Data.Int.i63&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;Coq.Init.Datatypes.list a&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Seq&lt;/span&gt;.t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;CoqFFI.Data.Seq.t&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; option&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;Coq.Init.Datatypes.option a&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;(&lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt;, &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;e&lt;/span&gt;) result&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;Coq.Init.Datatypes.sum&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;Coq.Strings.String.string&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;unit&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;Coq.Init.Datatypes.unit&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;exn&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code class=&quot;hljs&quot;&gt;CoqFFI.Exn&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The &lt;code class=&quot;hljs language-coq&quot;&gt;i63&lt;/code&gt; type is introduced by the &lt;code class=&quot;hljs language-coq&quot;&gt;CoqFFI&lt;/code&gt; theory to provide
signed primitive integers to Coq users. They are implemented on top of the
(unsigned) Coq native integers introduced in Coq &lt;code class=&quot;hljs&quot;&gt;8.13&lt;/code&gt;. The &lt;code class=&quot;hljs&quot;&gt;i63&lt;/code&gt; type will be
deprecated once the support for &lt;a href=&quot;https://github.com/coq/coq/pull/13559&quot; class=&quot;hover-peach&quot; marked=&quot;&quot;&gt;signed primitive
integers&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt; is implemented&lt;label for=&quot;fn1&quot; class=&quot;sidenote-number margin-toggle&quot;&gt;&lt;/label&gt;&lt;input id=&quot;fn1&quot; type=&quot;checkbox&quot; class=&quot;margin-toggle&quot;&gt;&lt;span class=&quot;note-right sidenote note&quot;&gt;&lt;span class=&quot;footnote-p&quot;&gt;This is actually one of the sources of incompatibility of &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;
with most recent versions of Coq. &lt;/span&gt;
&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;When processing the entries of a given interface model, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; will
check that they only use these types, or types introduced by the
interface module itself.&lt;/p&gt;
&lt;p&gt;Sometimes, you may encounter a situation where you have two interface
modules &lt;code class=&quot;hljs&quot;&gt;b.mli&lt;/code&gt; and &lt;code class=&quot;hljs&quot;&gt;b.mli&lt;/code&gt;, such that &lt;code class=&quot;hljs&quot;&gt;b.mli&lt;/code&gt; uses a type introduced
in &lt;code class=&quot;hljs&quot;&gt;a.mli&lt;/code&gt;.  To deal with this scenario, you can use the &lt;code class=&quot;hljs&quot;&gt;--witness&lt;/code&gt;
flag to generate &lt;code class=&quot;hljs&quot;&gt;A.v&lt;/code&gt;.  This will tell &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; to also generate
&lt;code class=&quot;hljs&quot;&gt;A.ffi&lt;/code&gt;; this file can then be used when generating &lt;code class=&quot;hljs&quot;&gt;B.v&lt;/code&gt; thanks to
the &lt;code class=&quot;hljs&quot;&gt;-I&lt;/code&gt; option.  Furthermore, for &lt;code class=&quot;hljs&quot;&gt;B.v&lt;/code&gt; to compile the &lt;code class=&quot;hljs&quot;&gt;--require&lt;/code&gt;
option needs to be used to ensure the &lt;code class=&quot;hljs&quot;&gt;A&lt;/code&gt; Coq library (&lt;code class=&quot;hljs&quot;&gt;A.v&lt;/code&gt;) is
required.&lt;/p&gt;
&lt;p&gt;To give a more concrete example, given ~a.mli~&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; t
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and &lt;code class=&quot;hljs&quot;&gt;b.mli&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; a = &lt;span class=&quot;hljs-type&quot;&gt;A&lt;/span&gt;.t
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To generate &lt;code class=&quot;hljs&quot;&gt;A.v&lt;/code&gt;, we can use the following commands:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-bash&quot;&gt;ocamlc a.mli
coqffi --witness -o A.v a.cmi
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which would generate the following axiom for &lt;code class=&quot;hljs&quot;&gt;t&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; t : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, generating &lt;code class=&quot;hljs&quot;&gt;B.v&lt;/code&gt; can be achieved as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-bash&quot;&gt;ocamlc b.mli
coqffi -I A.ffi -ftransparent-types -r A -o B.v b.cmi
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which results in the following output for &lt;code class=&quot;hljs&quot;&gt;v&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; A.

&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; u : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; := A.t.
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Code Generation&lt;/h2&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; distinguishes five types of entries: types, pure values,
impure primitives, asynchronous primitives, exceptions, and
modules. We now discuss how each one of them is handled.&lt;/p&gt;
&lt;h3&gt;Types&lt;/h3&gt;
&lt;p&gt;By default, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; generates axiomatized definitions for each type defined in
a &lt;code class=&quot;hljs&quot;&gt;.cmi&lt;/code&gt; file. This means that &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; t&lt;/code&gt; becomes &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; t : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;&lt;/code&gt;.
Polymorphism is supported, &lt;em&gt;i.e.&lt;/em&gt;, &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; t&lt;/code&gt; becomes &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; t : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;), &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is possible to provide a “model” for a type using the &lt;code class=&quot;hljs&quot;&gt;coq_model&lt;/code&gt;
annotation, for instance, for reasoning purposes. That is, we can specify
that a type is equivalent to a &lt;code class=&quot;hljs&quot;&gt;list&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; t [@@coq_model &lt;span class=&quot;hljs-string&quot;&gt;&quot;list&quot;&lt;/span&gt;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This generates the following Coq definition.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; t : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;), &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; := list.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is important to be careful when using the =coq_model= annotation. More
precisely, the fact that &lt;code class=&quot;hljs&quot;&gt;t&lt;/code&gt; is a &lt;code class=&quot;hljs&quot;&gt;list&lt;/code&gt; in the “Coq universe” shall not be
used while the implementation phase, only the verification phase.&lt;/p&gt;
&lt;p&gt;Unnamed polymorphic type parameters are also supported. In presence of
such parameters, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; will find it a name that is not already
used. For instance,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; (_, &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt;) ast
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; ast : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (b : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;), &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; has got an experimental feature called &lt;code class=&quot;hljs&quot;&gt;transparent-types&lt;/code&gt;
(enabled by using the &lt;code class=&quot;hljs&quot;&gt;-ftransparent-types&lt;/code&gt; command-line argument). If the type
definition is given in the module interface, then &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; tries to generate
an equivalent definition in Coq. For instance,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; llist =
  | &lt;span class=&quot;hljs-type&quot;&gt;LCons&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; * (&lt;span class=&quot;hljs-built_in&quot;&gt;unit&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; llist)
  | &lt;span class=&quot;hljs-type&quot;&gt;LNil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; llist (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
| &lt;span class=&quot;hljs-type&quot;&gt;LCons&lt;/span&gt; (x0 : a) (x1 : unit -&amp;gt; llist a) : llist a
| &lt;span class=&quot;hljs-type&quot;&gt;LNil&lt;/span&gt; : llist a.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Mutually recursive types are supported, so&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; even = &lt;span class=&quot;hljs-type&quot;&gt;Zero&lt;/span&gt; | &lt;span class=&quot;hljs-type&quot;&gt;ESucc&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; odd
&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; odd = &lt;span class=&quot;hljs-type&quot;&gt;OSucc&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; even
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; odd : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
| &lt;span class=&quot;hljs-type&quot;&gt;OSucc&lt;/span&gt; (x0 : even) : odd
&lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt; even : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
| &lt;span class=&quot;hljs-type&quot;&gt;Zero&lt;/span&gt; : even
| &lt;span class=&quot;hljs-type&quot;&gt;ESucc&lt;/span&gt; (x0 : odd) : even.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Besides, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; supports alias types, as suggested in this write-up
when we discuss witness files.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;hljs&quot;&gt;transparent-types&lt;/code&gt; feature is &lt;strong&gt;experimental&lt;/strong&gt;, and is currently
limited to variant types. It notably does not support records. Besides, it may
generate incorrect Coq types, because it does not check whether or not the
&lt;a href=&quot;https://coq.inria.fr/refman/language/core/inductive.html#positivity-condition&quot; class=&quot;hover-rose&quot; marked=&quot;&quot;&gt;positivity
condition&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#external-link&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;
is satisfied.&lt;/p&gt;
&lt;h3&gt;Pure values&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; decides whether or not a given OCaml value is pure or impure
with the following heuristics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Constants are pure&lt;/li&gt;
&lt;li&gt;Functions are impure by default&lt;/li&gt;
&lt;li&gt;Functions with a &lt;code class=&quot;hljs&quot;&gt;coq_model&lt;/code&gt; annotation are pure&lt;/li&gt;
&lt;li&gt;Functions marked with the &lt;code class=&quot;hljs&quot;&gt;pure&lt;/code&gt; annotation are pure&lt;/li&gt;
&lt;li&gt;If the &lt;code class=&quot;hljs&quot;&gt;pure-module&lt;/code&gt; feature is enabled (&lt;code class=&quot;hljs&quot;&gt;-fpure-module&lt;/code&gt;), then synchronous
functions (which do not live inside the
&lt;a href=&quot;https://ocsigen.org/lwt/5.3.0/manual/manual&quot; class=&quot;hover-periwinkle&quot; marked=&quot;&quot;&gt;~Lwt~&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#external-link&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt; monad) are pure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Similarly to types, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; generates axioms (or definitions if the
&lt;code class=&quot;hljs&quot;&gt;coq_model&lt;/code&gt; annotation is used) for pure values. Then,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; unpack : &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; -&amp;gt; (&lt;span class=&quot;hljs-built_in&quot;&gt;char&lt;/span&gt; * &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;) option [@@pure]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;Axiom&lt;/span&gt; unpack : &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; -&amp;gt; option (ascii * &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Polymorphic values are supported.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; map : (&lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;b&lt;/span&gt;) -&amp;gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;b&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt; [@@pure]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; map : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) (b : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;), (a -&amp;gt; b) -&amp;gt; list a -&amp;gt; list b.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, unnamed polymorphic typse are supported, so&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; ast_to_string : _ ast -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; [@@pure]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; ast_to_string : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;), string.
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Impure Primitives&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; reserves a special treatment for /impure/ OCaml functions.
Impurity is usually handled in pure programming languages by means of
monads, and &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; is no exception to the rule.&lt;/p&gt;
&lt;p&gt;Given the set of impure primitives declared in an interface module,
&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; will (1) generate a typeclass which gathers these primitives,
and (2) generate instances of this typeclass for supported backends.&lt;/p&gt;
&lt;p&gt;We illustrate the rest of this section with the following impure
primitives.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; echo : &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;unit&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; scan : &lt;span class=&quot;hljs-built_in&quot;&gt;unit&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code class=&quot;hljs&quot;&gt;echo&lt;/code&gt; allows writing something the standard output, and &lt;code class=&quot;hljs&quot;&gt;scan&lt;/code&gt;
to read the standard input.&lt;/p&gt;
&lt;p&gt;Assuming the processed module interface is named &lt;code class=&quot;hljs&quot;&gt;console.mli&lt;/code&gt;, the
following Coq typeclass is generated.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Class&lt;/span&gt; MonadConsole (m : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) := { echo : string -&amp;gt; m unit
                                         ; scan : unit -&amp;gt; m string
                                         }.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using this typeclass and with the additional support of an additional
&lt;code class=&quot;hljs&quot;&gt;Monad&lt;/code&gt; typeclass, we can specify impure computations which interacts
with the console. For instance, with the support of &lt;code class=&quot;hljs&quot;&gt;ExtLib&lt;/code&gt;, one can
write.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; pipe `{Monad m, MonadConsole m} : m unit :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt;* msg := scan () &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt;
  echo msg.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is no canonical way to model impurity in Coq, but over the years
several frameworks have been released to tackle this challenge.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; provides three features related to impure primitives.&lt;/p&gt;
&lt;h4&gt;&lt;code class=&quot;hljs&quot;&gt;simple-io&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;When this feature is enabled, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; generates an instance of the
typeclass for the =IO= monad introduced in the &lt;code class=&quot;hljs&quot;&gt;coq-simple-io&lt;/code&gt; package&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_echo : string -&amp;gt; IO unit.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; io_scan : unit -&amp;gt; IO string.

&lt;span class=&quot;hljs-keyword&quot;&gt;Instance&lt;/span&gt; IO_MonadConsole : MonadConsole IO := { echo := io_echo
                                              ; scan := io_scan
                                              }.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is enabled by default, but can be disabled using the
&lt;code class=&quot;hljs&quot;&gt;-fno-simple-io&lt;/code&gt; command-line argument.&lt;/p&gt;
&lt;h4&gt;&lt;code class=&quot;hljs&quot;&gt;interface&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;When this feature is enabled, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; generates an inductive type which
describes the set of primitives available, to be used with frameworks like
&lt;a href=&quot;https://github.com/lthms/FreeSpec&quot; class=&quot;hover-rose&quot; marked=&quot;&quot;&gt;FreeSpec&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt; or &lt;a href=&quot;https://github.com/DeepSpec/InteractionTrees&quot; class=&quot;hover-periwinkle&quot; marked=&quot;&quot;&gt;Interactions
Trees&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; CONSOLE : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
| &lt;span class=&quot;hljs-type&quot;&gt;Echo&lt;/span&gt; : string -&amp;gt; CONSOLE unit
| &lt;span class=&quot;hljs-type&quot;&gt;Scan&lt;/span&gt; : unit -&amp;gt; CONSOLE string.

&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; inj_echo `{Inject CONSOLE m} (x0 : string) : m unit :=
  inject (Echo x0).

&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; inj_scan `{Inject CONSOLE m} (x0 : unit) : m string :=
  inject (Scan x0).

&lt;span class=&quot;hljs-keyword&quot;&gt;Instance&lt;/span&gt; Inject_MonadConsole `{Inject CONSOLE m} : MonadConsole m :=
  { echo := inj_echo
  ; scan := inj_scan
  }.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Providing an instance of the form &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; i, Inject i M&lt;/code&gt; is enough for
your monad &lt;code class=&quot;hljs&quot;&gt;M&lt;/code&gt; to be compatible with this feature&lt;label for=&quot;fn2&quot; class=&quot;sidenote-number margin-toggle&quot;&gt;&lt;/label&gt;&lt;input id=&quot;fn2&quot; type=&quot;checkbox&quot; class=&quot;margin-toggle&quot;&gt;&lt;span class=&quot;note-left sidenote note&quot;&gt;&lt;span class=&quot;footnote-p&quot;&gt;See for instance &lt;a href=&quot;https://github.com/lthms/FreeSpec/blob/master/theories/FFI/FFI.v&quot; class=&quot;hover-lemon&quot; marked=&quot;&quot;&gt;how FreeSpec implements
it&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;). &lt;/span&gt;
&lt;/span&gt;.&lt;/p&gt;
&lt;h4&gt;&lt;code class=&quot;hljs&quot;&gt;freespec&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;When this feature in enabled, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; generates a semantics for the
inductive type generated by the &lt;code class=&quot;hljs&quot;&gt;interface&lt;/code&gt; feature.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; unsafe_echo : string -&amp;gt; unit.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; unsafe_scan : uint -&amp;gt; string.

&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; console_unsafe_semantics : semantics CONSOLE :=
  bootstrap (&lt;span class=&quot;hljs-keyword&quot;&gt;fun&lt;/span&gt; a e =&amp;gt;
    local &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; e &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; CONSOLE a &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;Echo&lt;/span&gt; x0 =&amp;gt; unsafe_echo x0
          | &lt;span class=&quot;hljs-type&quot;&gt;Scan&lt;/span&gt; x0 =&amp;gt; unsafe_scan x0
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;).
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Asynchronous Primitives&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; also reserves a special treatment for &lt;em&gt;asynchronous&lt;/em&gt;
primitives —&lt;em&gt;i.e.&lt;/em&gt;, functions which live inside the &lt;code class=&quot;hljs&quot;&gt;Lwt&lt;/code&gt; monad— when
the &lt;code class=&quot;hljs&quot;&gt;lwt&lt;/code&gt; feature is enabled.&lt;/p&gt;
&lt;p&gt;The treatment is very analogous to the one for impure primitives: (1)
a typeclass is generated (with the &lt;code class=&quot;hljs&quot;&gt;_Async&lt;/code&gt; suffix), and (2) an
instance for the &lt;code class=&quot;hljs&quot;&gt;Lwt&lt;/code&gt; monad is generated. Besides, an instance for
the “synchronous” primitives is also generated for &lt;code class=&quot;hljs&quot;&gt;Lwt&lt;/code&gt;. If the
&lt;code class=&quot;hljs&quot;&gt;interface&lt;/code&gt; feature is enabled, an interface datatype is generated,
which means you can potentially use Coq to reason about your
asynchronous programs (using FreeSpec and alike, although the
interleaving of asynchronous programs in not yet supported in
FreeSpec).&lt;/p&gt;
&lt;p&gt;By default, the type of the &lt;code class=&quot;hljs&quot;&gt;Lwt&lt;/code&gt; monad is &lt;code class=&quot;hljs&quot;&gt;Lwt.t&lt;/code&gt;. You can override
this setting using the &lt;code class=&quot;hljs&quot;&gt;--lwt-alias&lt;/code&gt; option.  This can be useful when
you are using an alias type in place of &lt;code class=&quot;hljs&quot;&gt;Lwt.t&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Exceptions&lt;/h3&gt;
&lt;p&gt;OCaml features an exception mechanism. Developers can define their
own exceptions using the &lt;code class=&quot;hljs&quot;&gt;exception&lt;/code&gt; keyword, whose syntax is similar
to the constructors’ definition. For instance,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;exception&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Foo&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; * &lt;span class=&quot;hljs-built_in&quot;&gt;bool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;introduces a new exception &lt;code class=&quot;hljs&quot;&gt;Foo&lt;/code&gt; which takes two parameters of type &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;&lt;/code&gt; and
&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;bool&lt;/span&gt;&lt;/code&gt;. &lt;code class=&quot;hljs&quot;&gt;Foo (x, y)&lt;/code&gt; constructs of value of type &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;exn&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For each new exception introduced in an OCaml module, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;
generates (1) a so-called “proxy type,” and (2) conversion functions
to and from this type.&lt;/p&gt;
&lt;p&gt;Coming back to our example, the “proxy type” generates by &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; is&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; FooExn : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
| &lt;span class=&quot;hljs-type&quot;&gt;MakeFooExn&lt;/span&gt; (x0 : i63) (x1 : bool) : FooExn.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; generates conversion functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; exn_of_foo : FooExn -&amp;gt; exn.
&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; foo_of_exn : exn -&amp;gt; option FooExn.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Besides, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; also generates an instance for the &lt;code class=&quot;hljs&quot;&gt;Exn&lt;/code&gt; typeclass
provided by the &lt;code class=&quot;hljs&quot;&gt;CoqFFI&lt;/code&gt; theory:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Instance&lt;/span&gt; FooExn_Exn : Exn FooExn :=
  { to_exn := exn_of_foo
  ; of_exn := foo_of_exn
  }.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Under the hood, &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;exn&lt;/span&gt;&lt;/code&gt; is an
&lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/extensiblevariants.html&quot; class=&quot;hover-lavender&quot; marked=&quot;&quot;&gt;extensible
datatype&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#external-link&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;,
and how &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; supports it will probably be generalized in future releases.&lt;/p&gt;
&lt;p&gt;Finally, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; has a minimal support for functions which may raise
exceptions. Since OCaml type system does not allow to identify such
functions, they need to be annotated explicitly, using the
=may_raise= annotation. In such a case, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; will change the
return type of the function to use the =sum= Coq inductive type.&lt;/p&gt;
&lt;p&gt;For instance,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; from_option : &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; option -&amp;gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; [@@may_raise] [@@pure]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; from_option : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;), option a -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;sum&lt;/span&gt; a exn.
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Modules&lt;/h3&gt;
&lt;p&gt;Lastly, &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; supports OCaml modules described within &lt;code class=&quot;hljs&quot;&gt;mli&lt;/code&gt; files,
when they are specified as &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;T&lt;/span&gt; : &lt;span class=&quot;hljs-keyword&quot;&gt;sig&lt;/span&gt; ... &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;/code&gt;. For instance,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;T&lt;/span&gt; : &lt;span class=&quot;hljs-keyword&quot;&gt;sig&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; t

  &lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; to_string : t -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; [@@pure]
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Module&lt;/span&gt; T.
  &lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; t : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;.

  &lt;span class=&quot;hljs-keyword&quot;&gt;Axiom&lt;/span&gt; to_string : t -&amp;gt; string.
&lt;span class=&quot;hljs-keyword&quot;&gt;End&lt;/span&gt; T.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As of now, the following construction is unfortunately &lt;em&gt;not&lt;/em&gt;
supported, and will be ignored by &lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;S&lt;/span&gt; = &lt;span class=&quot;hljs-keyword&quot;&gt;sig&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; t

  &lt;span class=&quot;hljs-keyword&quot;&gt;val&lt;/span&gt; to_string : t -&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; [@@pure]
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;T&lt;/span&gt; : &lt;span class=&quot;hljs-type&quot;&gt;S&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Moving Forward&lt;/h2&gt;
&lt;p&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; comes with a comprehensive man page. In addition, the
interested reader can proceed to the next article of this series,
which explains how &lt;a href=&quot;/~lthms/posts/CoqffiEcho.html&quot; class=&quot;hover-periwinkle&quot; marked=&quot;&quot;&gt;&lt;code class=&quot;hljs&quot;&gt;coqffi&lt;/code&gt; can be used to easily implement an echo
server in Coq&lt;/a&gt;.&lt;/p&gt;
        
      </description>
    </item>
    
    
  </channel>
</rss>
