<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Coq blog - Guillaume Claret</title>
    <description></description>
    <link>https://rocq-blog.clarus.me/</link>
    <lastBuildDate>Tue, 27 Oct 2026 13:22:38 +0000</lastBuildDate>
    <atom:link href="https://rocq-blog.clarus.me/rss.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Beginning of verification for the parsing of smart-contracts</title>
        <pubDate>Mon, 22 Feb 2021 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;In this blog post, we will show how we &lt;a href=&quot;https://en.wikipedia.org/wiki/Formal_verification&quot;&gt;formally verify&lt;/a&gt; in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; some properties about the parser of &lt;a href=&quot;https://en.wikipedia.org/wiki/Smart_contract&quot;&gt;smart-contracts&lt;/a&gt; for the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. The &lt;a href=&quot;https://gitlab.com/nomadic-labs/coq-tezos-of-ocaml/-/tree/master/src/Proto_alpha/Proofs/Script_ir_translator&quot;&gt;proofs of this formal verification&lt;/a&gt; are hosted in our project &lt;a href=&quot;https://gitlab.com/nomadic-labs/coq-tezos-of-ocaml&quot;&gt;coq-tezos-of-ocaml&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To reason about the &lt;a href=&quot;https://gitlab.com/tezos/tezos/-/blob/master/src/proto_alpha/lib_protocol/script_ir_translator.ml&quot;&gt;implementation of the parser&lt;/a&gt;, written in &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt;, we use the tool &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;coq-of-ocaml&lt;/a&gt; to convert it automatically to Coq. We will talk about the tricks we used to get this conversion to work, in particular for the GADTs and the mutually recursive functions. We will also present the properties which we verified and how we did it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;How do we convert the OCaml code to Coq&lt;/h2&gt;

&lt;p&gt;The code which interests us is in the file &lt;a href=&quot;https://gitlab.com/tezos/tezos/-/blob/master/src/proto_alpha/lib_protocol/script_ir_translator.ml&quot;&gt;&lt;code&gt;script_ir_translator.ml&lt;/code&gt;&lt;/a&gt;. This is a long file, containing in particular the parser and type-checker of the &lt;a href=&quot;https://wiki.tezosagora.org/files/language.html#michelson&quot;&gt;Michelson&lt;/a&gt; language for smart-contracts. We are interested into the functions &amp;ldquo;parse something&amp;rdquo; and &amp;ldquo;unparse something&amp;rdquo; to show that there are compatible. We use the tool coq-of-ocaml to convert the code of this file. Since it depends on other files of the code of Tezos (for type definitions or primitives), we need to have the Coq definition of all its dependencies too. This is done in the project &lt;a href=&quot;https://gitlab.com/nomadic-labs/coq-tezos-of-ocaml&quot;&gt;coq-tezos-of-ocaml&lt;/a&gt;. The translation of &lt;code&gt;script_ir_translator.ml&lt;/code&gt; is in &lt;a href=&quot;https://gitlab.com/nomadic-labs/coq-tezos-of-ocaml/-/blob/master/src/Proto_alpha/Script_ir_translator.v&quot;&gt;src/Proto_alpha/Script_ir_translator.v&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Mutually recursive functions&lt;/h3&gt;

&lt;p&gt;Mutually recursive functions are a challenge in Coq because of the syntactic constraints to make sure that each function terminates. Indeed, termination is important for Coq because a non-terminating functions would make the whole system logically inconsistent. Moreover, OCaml programs may not follow the syntactic constraints for termination of Coq as the OCaml compiler does not check for termination. Thus we use several techniques to translate the OCaml code to Coq without modifying too much the source:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;disabling if needed the termination checker of Coq with the &lt;a href=&quot;https://coq.inria.fr/refman/proof-engine/vernacular-commands.html#coq:flag.Guard-Checking&quot;&gt;&lt;code&gt;Guard Checking&lt;/code&gt;&lt;/a&gt; flag;&lt;/li&gt;
&lt;li&gt;introducing &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/docs/attributes&quot;&gt;OCaml attributes&lt;/a&gt; to guide the translation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two main attributes useful to translate recursive functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;@coq_struct &amp;quot;ident&amp;quot;&lt;/code&gt; to specify the &lt;code&gt;{struct ident}&lt;/code&gt; parameter of the &lt;a href=&quot;https://coq.inria.fr/refman/language/core/inductive.html#coq:cmd.Fixpoint&quot;&gt;&lt;code&gt;Fixpoint&lt;/code&gt;&lt;/a&gt; command of Coq;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@coq_mutual_as_notation&lt;/code&gt; to force some mutual functions to be defined as notations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, let us we take the function &lt;code&gt;parse_ty&lt;/code&gt; to parse types, defined in OCaml as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let rec parse_ty =
 fun ctxt
    ~legacy
    ~allow_lazy_storage
    ~allow_operation
    ~allow_contract
    ~allow_ticket
    node -&amp;gt;
  Gas.consume ctxt Typecheck_costs.parse_type_cycle
  &amp;gt;&amp;gt;? fun ctxt -&amp;gt;
  match node with
  | Prim (_loc, T_unit, [], _annot) -&amp;gt;
      ok (Ex_ty Unit_t, ctxt)
  | Prim (_loc, T_int, [], _annot) -&amp;gt;
      ok (Ex_ty Int_t, ctxt)
  | Prim (_loc, T_nat, [], _annot) -&amp;gt;
      ok (Ex_ty Nat_t, ctxt)
  | Prim (_loc, T_string, [], _annot) -&amp;gt;
      ok (Ex_ty String_t, ctxt)
  | Prim (_loc, T_bytes, [], _annot) -&amp;gt;
      ok (Ex_ty Bytes_t, ctxt)
  | Prim (_loc, T_mutez, [], _annot) -&amp;gt;
      ok (Ex_ty Mutez_t, ctxt)
  | Prim (_loc, T_bool, [], _annot) -&amp;gt;
      ok (Ex_ty Bool_t, ctxt)
  [...]

and parse_parameter_ty =
 fun ctxt ~legacy -&amp;gt;
  parse_ty
    ctxt
    ~legacy
    ~allow_lazy_storage:true
    ~allow_operation:false
    ~allow_contract:true
    ~allow_ticket:true

and parse_any_ty =
  [...]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We have one main function &lt;code&gt;parse_ty&lt;/code&gt; iterating over the parameter &lt;code&gt;node&lt;/code&gt; and several auxiliary functions such as &lt;code&gt;parse_parameter_ty&lt;/code&gt; which are exposed at top-level. We represent these auxiliary functions as notations in Coq, so that they are simpler to reason about compared to mutual fixpoints. We annotate the &lt;code&gt;parse_parameter_ty&lt;/code&gt; function by the &lt;code&gt;@coq_mutual_as_notation&lt;/code&gt; attribute so that we generate in Coq:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Reserved Notation &amp;quot;&amp;#39;parse_parameter_ty&amp;quot;.
Reserved Notation &amp;quot;&amp;#39;parse_any_ty&amp;quot;.
[...]

Fixpoint parse_ty
  (ctxt : Alpha_context.context) (legacy : bool) (allow_lazy_storage : bool)
  (allow_operation : bool) (allow_contract : bool) (allow_ticket : bool)
  (node : Alpha_context.Script.node) {struct node}
  : M? (ex_ty * Alpha_context.context) :=
  let parse_parameter_ty := &amp;#39;parse_parameter_ty in
  let parse_any_ty := &amp;#39;parse_any_ty in
  [...]
  let? ctxt := Alpha_context.Gas.consume ctxt Typecheck_costs.parse_type_cycle
    in
  match
    (node,
      match node with
      | Micheline.Prim loc Michelson_v1_primitives.T_big_map args _annot =&amp;gt;
        allow_lazy_storage
      | _ =&amp;gt; false
      end,
      match node with
      |
        Micheline.Prim _loc Michelson_v1_primitives.T_sapling_state
          (cons memo_size []) _annot =&amp;gt; allow_lazy_storage
      | _ =&amp;gt; false
      end) with
  | (Micheline.Prim _loc Michelson_v1_primitives.T_unit [] _annot, _, _) =&amp;gt;
    return? ((Ex_ty Script_typed_ir.Unit_t), ctxt)
  | (Micheline.Prim _loc Michelson_v1_primitives.T_int [] _annot, _, _) =&amp;gt;
    return? ((Ex_ty Script_typed_ir.Int_t), ctxt)
  | (Micheline.Prim _loc Michelson_v1_primitives.T_nat [] _annot, _, _) =&amp;gt;
    return? ((Ex_ty Script_typed_ir.Nat_t), ctxt)
  | (Micheline.Prim _loc Michelson_v1_primitives.T_string [] _annot, _, _) =&amp;gt;
    return? ((Ex_ty Script_typed_ir.String_t), ctxt)
  [...]

where &amp;quot;&amp;#39;parse_parameter_ty&amp;quot; :=
  (fun (ctxt : Alpha_context.context) (legacy : bool) =&amp;gt;
    parse_ty ctxt legacy true false true true)

and &amp;quot;&amp;#39;parse_any_ty&amp;quot; :=
  [...]

Definition parse_parameter_ty := &amp;#39;parse_parameter_ty.
Definition parse_any_ty := &amp;#39;parse_any_ty.
[...]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We define &lt;code&gt;parse_parameter_ty&lt;/code&gt; as a notation &lt;code&gt;&amp;#39;parse_parameter_ty&lt;/code&gt;. We introduce an alias &lt;code&gt;parse_parameter_ty&lt;/code&gt; as a standard definition at the end, so that the code depending on &lt;code&gt;parse_parameter_ty&lt;/code&gt; does not need to know that this is actually a notation. Using the notation, we consider &lt;code&gt;parse_parameter_ty&lt;/code&gt; as a shorthand to call &lt;code&gt;parse_ty&lt;/code&gt; rather than a whole new function. This can also simplifies our proofs as &lt;code&gt;parse_ty&lt;/code&gt; is now a single recursive function.
With the &lt;code&gt;@coq_struct&lt;/code&gt; attribute, we specify that the parameter &lt;code&gt;node&lt;/code&gt; is the one to recurse on. Even if the function &lt;code&gt;parse_ty&lt;/code&gt; is not syntactically terminating for Coq (due to some flattening when parsing pair elements), it is important for the proofs to have a &amp;ldquo;resonable&amp;rdquo; &lt;code&gt;struct&lt;/code&gt; parameter. This prevents the &lt;code&gt;simpl&lt;/code&gt; tactic to diverge when doing proofs by symbolic evaluation. Here we choose the parameter &lt;code&gt;node&lt;/code&gt; as it is different on each recursive call.&lt;/p&gt;

&lt;h3&gt;GADTs&lt;/h3&gt;

&lt;p&gt;Our current approach to translate &lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/gadts.html&quot;&gt;GADTs&lt;/a&gt; to Coq is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;erase the type parameters;&lt;/li&gt;
&lt;li&gt;use OCaml attributes to force the generation of dynamic casts in Coq when needed (these dynamic casts are axioms).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fortunately, for our experiment we did not need to use dynamic casts and the code generated without the type parameters for the GADTs was compiling just fine!&lt;/p&gt;

&lt;h2&gt;The proofs&lt;/h2&gt;

&lt;p&gt;All the proofs are accessible online on &lt;a href=&quot;https://gitlab.com/nomadic-labs/coq-tezos-of-ocaml/-/tree/master/src/Proto_alpha/Proofs/Script_ir_translator&quot;&gt;coq-tezos-of-ocaml/src/Proto_alpha/Proofs/Script_ir_translator&lt;/a&gt;. We wanted to verify the following property:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; forall term, parse (unparse term) = term
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;for the terms of type &lt;code&gt;comparable_ty&lt;/code&gt; and &lt;code&gt;ty&lt;/code&gt;. For the type &lt;code&gt;ty&lt;/code&gt;, we express this property as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Lemma parse_unparse_ty
  ctxt
  legacy
  allow_lazy_storage allow_operation allow_contract allow_ticket
  ty
  (H_ty
    : Script_typed_ir.Ty.is_valid
        legacy
        allow_lazy_storage allow_operation allow_contract allow_ticket
        ty =
      true
  )
  : let unlimited_ctxt := Raw_context.with_unlimited_gas ctxt in
    (let? &amp;#39;(node, ctxt) := Script_ir_translator.unparse_ty unlimited_ctxt ty in
    Script_ir_translator.parse_ty
      ctxt
      legacy
      allow_lazy_storage allow_operation allow_contract allow_ticket
      node) =
    return? (Script_ir_translator.Ex_ty ty, unlimited_ctxt).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The parameters &lt;code&gt;legacy&lt;/code&gt;, &lt;code&gt;allow_lazy_storage&lt;/code&gt;, &lt;code&gt;allow_operation&lt;/code&gt;, &lt;code&gt;allow_contract&lt;/code&gt;, &lt;code&gt;allow_ticket&lt;/code&gt; are boolean flags of the parsing function. They are mainly there to allow or forbid some elements, such as the contracts or the tickets. The context &lt;code&gt;ctxt&lt;/code&gt; represents the current state, and in particular contains a reference to the block-chain state. We replace it by:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let unlimited_ctxt := Raw_context.with_unlimited_gas ctxt in
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which is the same context with an unlimited gas value. The gas is there to compute the execution cost for smart-contracts. By setting the gas as unlimited, we avoid having to reason about failures due to gas exhaustion, without too much loss of generality. The &lt;code&gt;unparse_ty&lt;/code&gt; and &lt;code&gt;parse_ty&lt;/code&gt; functions are in the error monad, whose basic operators are &lt;code&gt;return?&lt;/code&gt; to return a success value and &lt;code&gt;let?&lt;/code&gt; to bind two operations.&lt;/p&gt;

&lt;p&gt;We recursively express the pre-condition &lt;code&gt;Script_typed_ir.Ty.is_valid&lt;/code&gt; on the type &lt;code&gt;ty&lt;/code&gt;. This pre-condition depends on the same flags as the parsing function. It checks that the forbidden operations are not present, and that all the integers are in the expected intervals.&lt;/p&gt;

&lt;p&gt;We do the proof by induction on the value &lt;code&gt;ty&lt;/code&gt;. The most complex case is the case of pairs because we flatten the pairs to lists of elements, so the induction is not direct. Apart from that case, most of the proof is dedicated to handling the error monad and unfolding the definitions. To get an idea, here is an extract of the Coq proof to handle the general case:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;destruct ty; unfold simple_unparse_ty; simpl; try reflexivity;
  repeat (rewrite simple_unparse_ty_eq; simpl);
  repeat (rewrite Comparable_ty.parse_unparse_comparable_ty; simpl);
  repeat (rewrite (parse_simple_unparse_ty ctxt); simpl);
  trivial;
  simpl in H_ty; try (rewrite Bool.andb_true_iff in H_ty; destruct H_ty);
  trivial.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For each case, we try to do some symbolic evaluation with &lt;code&gt;simpl&lt;/code&gt;, and apply the inductive hypothesis &lt;code&gt;parse_simple_unparse_ty&lt;/code&gt; or lemma such as &lt;code&gt;parse_unparse_comparable_ty&lt;/code&gt;. We also do some basic boolean manipulations with &lt;code&gt;andb_true_iff&lt;/code&gt; for the pre-condition.&lt;/p&gt;

&lt;p&gt;Finally, we check that the pre-condition &lt;code&gt;Script_typed_ir.Ty.is_valid&lt;/code&gt; is true for all the terms parsed by the function &lt;code&gt;parse_ty&lt;/code&gt;. We express this property as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Lemma parse_is_valid
  ctxt
  legacy
  allow_lazy_storage allow_operation allow_contract allow_ticket
  node
  : let result :=
      Script_ir_translator.parse_ty
        ctxt
        legacy
        allow_lazy_storage allow_operation allow_contract allow_ticket
        node in
    match result with
    | Pervasives.Ok (Script_ir_translator.Ex_ty ty, _) =&amp;gt;
      Script_typed_ir.Ty.is_valid
        legacy
        allow_lazy_storage allow_operation allow_contract allow_ticket
        ty =
      true
    | _ =&amp;gt; True
    end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The proof proceeds by induction on the parameter &lt;code&gt;node&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We have seen that we can write basic formal proofs about the parser of smart-contracts of Tezos. We do that first by automatically translating the OCaml code to Coq, and then by doing the proofs in Coq.&lt;/p&gt;

&lt;p&gt;We will next focus on writing proofs on the parsing functions for the data of smart-contracts. These functions are slightly more involved than the parsing functions on the types, but follow the same structure.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/beginning-of-verification-for-the-parsing-of-smart-contracts.html</link>
        <guid>https://rocq-blog.clarus.me/beginning-of-verification-for-the-parsing-of-smart-contracts.html</guid>
      </item>
    
      <item>
        <title>Removing existential types from modules in coq-of-ocaml</title>
        <pubDate>Mon, 08 Feb 2021 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;The tool &lt;a href=&quot;https://github.com/clarus/coq-of-ocaml&quot;&gt;coq-of-ocaml&lt;/a&gt; translates &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; programs to &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; programs using a shallow embedding. To translate the &lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/moduleexamples.html&quot;&gt;modules and functors&lt;/a&gt; of OCaml we use polymorphic &lt;a href=&quot;https://coq.inria.fr/refman/language/core/records.html&quot;&gt;records&lt;/a&gt; in Coq. With this representation, we are also able to translate &lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/firstclassmodules.html&quot;&gt;first-class modules&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We originally used existential types to represent abstract module types. This could be a source of complexity for the reasoning on the generated code. Indeed, existential types require to do frequent &lt;a href=&quot;https://coq.inria.fr/library/Coq.Init.Specif.html#projT2&quot;&gt;projections&lt;/a&gt; and &lt;a href=&quot;https://coq.inria.fr/library/Coq.Init.Specif.html#existT&quot;&gt;wrapping&lt;/a&gt; in Coq. In this blog post, we show how we removed the need of existential types in most non-first-class modules.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;We take the following OCaml code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module F (X : Source) : Target with type t2 = X.t = struct
  type t1 = string
  type t2 = X.t
  let y = X.x
end

module FM = F (M)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We assume that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the signature &lt;code&gt;Source&lt;/code&gt; has one abstract type &lt;code&gt;t&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;the signature &lt;code&gt;Target&lt;/code&gt; has two abstract types &lt;code&gt;t1&lt;/code&gt; and &lt;code&gt;t2&lt;/code&gt;, the type &lt;code&gt;t2&lt;/code&gt; being explicitly specified as &lt;code&gt;X.t&lt;/code&gt; in this example.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Now&lt;/h3&gt;

&lt;p&gt;We show how we now translate this OCaml example with the latest changes in coq-of-ocaml. In particular, we show how we avoid using existential types for the abstract module types. We start by using a &lt;a href=&quot;https://coq.inria.fr/refman/addendum/type-classes.html&quot;&gt;Coq type class&lt;/a&gt; to represent the functor parameters as explained in a &lt;a href=&quot;http://coq-blog.clarus.me/improvements-of-coq-of-ocaml-for-functors-and-signatures.html&quot;&gt;previous article&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module F.
  Class FArgs {X_t : Set} := {
    X : Source (t := X_t);
  }.
  Arguments Build_FArgs {_}.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We represent the abstract type &lt;code&gt;t&lt;/code&gt; of &lt;code&gt;X&lt;/code&gt; with a parameter &lt;code&gt;X_t&lt;/code&gt; of the class &lt;code&gt;FArgs&lt;/code&gt;. This type is implicit (notation&amp;nbsp;&lt;code&gt;{}&lt;/code&gt;) since we can infer it from the module &lt;code&gt;X&lt;/code&gt;. We then define the various fields of the functor, including a special field&amp;nbsp;&lt;code&gt;functor&lt;/code&gt; which is the record representing the result of the functor:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  Definition t1 `{FArgs} : Set := string.

  Definition t2 `{FArgs} : Set := X.(Source.t).

  Definition y `{FArgs} : X.(Source.t) := X.(Source.x).

  Definition functor `{FArgs} :=
    {|
      Target.y := y
    |}.
End F.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, we wrap the functor &lt;code&gt;F.functor&lt;/code&gt; into a function &lt;code&gt;F&lt;/code&gt; without type classes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition F {X_t : Set} (X : Source (t := X_t))
  : Target (t1 := _) (t2 := X.(Source.t)) :=
  let &amp;#39;_ := F.Build_FArgs X in
  F.functor.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We keep &lt;code&gt;X_t&lt;/code&gt; as an implicit parameter. The type &lt;code&gt;X_t&lt;/code&gt; is a parameter rather than an existential type. For the abstract type &lt;code&gt;t1&lt;/code&gt;, we let Coq infer its value with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Target (t1 := _) (t2 := X.(Source.t))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since Coq has access to the definition of&amp;nbsp;&lt;code&gt;F&lt;/code&gt;, it is able to guess the value of &lt;code&gt;t1&lt;/code&gt;, and we do not need an existential type there. For the type &lt;code&gt;t2&lt;/code&gt;, we directly give its value&amp;nbsp;&lt;code&gt;X.(Source.t)&lt;/code&gt; like in the OCaml source. We think it is better to always give an explicit type value when possible. Indeed, the expression inferred by Coq may be too large and have caused performance issues in some of our examples.&lt;/p&gt;

&lt;p&gt;To apply the functor&amp;nbsp;&lt;code&gt;F&lt;/code&gt; on&amp;nbsp;&lt;code&gt;M&lt;/code&gt; we simply do a function application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition FM := F M.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Coq infers all the missing abstract type values for us.&lt;/p&gt;

&lt;h3&gt;Before&lt;/h3&gt;

&lt;p&gt;Before these recent changes, we were wrapping all the modules into one existential type for each abstract type. On this example, we would have generated the following Coq code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module F.
  Class FArgs := {
    X : {t : Set &amp;amp; Source.signature (t := t)};
  }.

  Definition t1 `{FArgs} : Set := string.

  Definition t2 `{FArgs} : Set := (|X|).(Source.t).

  Definition y `{FArgs} : (|X|).(Source.t) := (|X|).(Source.x).

  Definition functor `(FArgs)
    : {t1 : Set &amp;amp; Target.signature (t1 := t1) (t2 := (|X|).(Source.t))} :=
    existT (A := Set) _ t1
      {|
        Target.y := y
      |}.
End F.
Definition F X := F.functor {| F.X := X |}.

Definition FM := F (existT (A := Set) _ _ (|M|)).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We wrap the abstract type &lt;code&gt;t&lt;/code&gt; of &lt;code&gt;X&lt;/code&gt; in the existential:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{t : Set &amp;amp; Source.signature (t := t)}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We wrap the abstract type &lt;code&gt;t1&lt;/code&gt; in the existential:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;existT (A := Set) _ t1 {| ... |}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To access the fields of each module we first project the existential types with the notation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Notation &amp;quot;(| M |)&amp;quot; := (projT2 M).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To apply the functor&amp;nbsp;&lt;code&gt;F&lt;/code&gt; to&amp;nbsp;&lt;code&gt;M&lt;/code&gt; we also wrap &lt;code&gt;M&lt;/code&gt; into an existential in order to be sure to have the correct number of existential types. We believe that the use of existential types associated with frequent projections or wrapping was a source of complexity for proofs made on the generated Coq code.&lt;/p&gt;

&lt;h2&gt;Strategy&lt;/h2&gt;

&lt;p&gt;We now describe the general strategy we use to remove the need of existential types in common use cases.&lt;/p&gt;

&lt;h3&gt;Definitions&lt;/h3&gt;

&lt;p&gt;When we define a module&amp;nbsp;&lt;code&gt;M&lt;/code&gt; with some abstract types in the signature, we let Coq infer their values. When we define a functor&amp;nbsp;&lt;code&gt;F&lt;/code&gt; parametrized by some modules:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;M_1, ..., M_n
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;with the abstract types:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;M_1 : t_1_1, t_1_2, ...
...
M_n : t_n_1, t_n_2, ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and returning a module of signature&amp;nbsp;&lt;code&gt;S&lt;/code&gt; with the abstract types:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;S : t_1, t_2, ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we push all the abstract types in front, so that the type of the function representing the functor &lt;code&gt;F&lt;/code&gt; in Coq is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;F :
  (* The abstract types could also have a higher arity such as Set -&amp;gt; Set *)
  forall {t_1_1 t_1_2 ... t_n_1 t_n_2 ... : Set},
  forall (M1 : M_1_signature (t_1_1 := t_1_1) (t_1_2 := t_1_2) ...),
  ...
  forall (M_n : M_n_signature (t_n_1 := t_n_1) (t_n_2 := t_n_2) ...),
  S
    (t_1 := either _ if abstract or some type expression if specified)
    ...
    (t_n := ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We only support functors whose parameters are all modules. Since we support modules containing functors, it is possible to wrap a functor into a module to pass it to another functor. However the functors in modules cannot return some abstract types (we had &lt;a href=&quot;http://adam.chlipala.net/cpdt/html/Universes.html&quot;&gt;universe level&lt;/a&gt; issues with that). We also expect the functors to be applied on all their parameters at once (no &lt;a href=&quot;https://en.wikipedia.org/wiki/Currying&quot;&gt;currying&lt;/a&gt;). Indeed, we need to infer all the abstract types of the parameters at once.&lt;/p&gt;

&lt;p&gt;An example of a functor type from the &lt;a href=&quot;https://gitlab.com/nomadic-labs/coq-tezos-of-ocaml&quot;&gt;Coq code generated from Tezos&lt;/a&gt; is the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition Make_single_data_storage {C_t V_t : Set}
  (R : Storage_sigs.REGISTER) (C : Raw_context.T (t := C_t))
  (N : Storage_sigs.NAME) (V : Storage_sigs.VALUE (t := V_t))
  : Storage_sigs.Single_data_storage (t := C.(Raw_context.T.t))
    (value := V.(Storage_sigs.VALUE.t)) :=
  (* ... the definition *)
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Axioms&lt;/h3&gt;

&lt;p&gt;We convert&amp;nbsp;&lt;code&gt;.mli&lt;/code&gt; files to lists of axioms. This raises an issue as we cannot infer the abstract types in a module or in the output module of a functor anymore. This inference is not possible because we do not have access to the definition of axioms. We solve this issue by adding one more axiom for each type to infer.&lt;/p&gt;

&lt;p&gt;For example, in OCaml the module&amp;nbsp;&lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/libref/Map.html&quot;&gt;&lt;code&gt;Map&lt;/code&gt;&lt;/a&gt; is declared as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module type S = sig
  (** The type of the map keys. *)
  type key

  (** The type of maps from type [key] to type [&amp;#39;a]. *)
  type +&amp;#39;a t

  (* ... *)
end

module Make: functor (Ord : OrderedType) -&amp;gt; S with type key = Ord.t
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The signature&amp;nbsp;&lt;code&gt;S&lt;/code&gt; has two abstract types&amp;nbsp;&lt;code&gt;key&lt;/code&gt; and&amp;nbsp;&lt;code&gt;t&lt;/code&gt;. The signature&amp;nbsp;&lt;code&gt;OrderedType&lt;/code&gt; has one abstract type&amp;nbsp;&lt;code&gt;t&lt;/code&gt;. We transform the declaration of the functor&amp;nbsp;&lt;code&gt;Make&lt;/code&gt; to the axioms:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Parameter Make_t :
  forall {Ord_t : Set} (Ord : OrderedType (t := Ord_t)), Set -&amp;gt; Set.

Parameter Make :
  forall {Ord_t : Set},
  forall (Ord : OrderedType (t := Ord_t)),
  S (key := Ord.(OrderedType.t)) (t := Make_t Ord).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since the abstract type&amp;nbsp;&lt;code&gt;t&lt;/code&gt; of the signature&amp;nbsp;&lt;code&gt;S&lt;/code&gt; is unspecified, we introduce an additional axiom&amp;nbsp;&lt;code&gt;Make_t&lt;/code&gt; to describe its value. This axiom takes the same arguments as the functor&amp;nbsp;&lt;code&gt;Make&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;First-class modules&lt;/h3&gt;

&lt;p&gt;We keep using existential types for the &lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/firstclassmodules.html&quot;&gt;first-class modules&lt;/a&gt;. This is because first-class module values can appear at any position in a program (in a function, in a data structure, &amp;hellip;). Thus the methods above would not be enough to eliminate the need of existential types in all cases.&lt;/p&gt;

&lt;p&gt;We use the following strategy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we add the existential types when we go from a module to a value;&lt;/li&gt;
&lt;li&gt;we remove the existential types (by projection) when we go from a value to a module;&lt;/li&gt;
&lt;li&gt;when we access to the field of a first-class module, we consider that the existential types have already been removed, since it has already been converted to a module.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of OCaml code with first-class modules, from the code of Tezos:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type (&amp;#39;key, &amp;#39;value) map =
  (module Boxed_map with type key = &amp;#39;key and type value = &amp;#39;value)

let map_set : type a b. a -&amp;gt; b -&amp;gt; (a, b) map -&amp;gt; (a, b) map =
fun k v (module Box) -&amp;gt;
  ( module struct
    type key = a

    type value = b

    let key_ty = Box.key_ty

    module OPS = Box.OPS

    let boxed =
      let (map, size) = Box.boxed in
      (Box.OPS.add k v map, if Box.OPS.mem k map then size else size + 1)
  end )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This implements a map type which is polymorphic in the type of the keys. One needs to give the comparison function for the keys when initializing an empty map. We translate this code to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition map (key value : Set) : Set :=
  {OPS_t : Set -&amp;gt; Set @
    Boxed_map (key := key) (value := value) (OPS_t := OPS_t)}.

Definition map_set {a b : Set} (k : a) (v : b) (Box : map a b)
  : map a b :=
  let &amp;#39;existS _ _ Box := Box in
  existS (A := Set -&amp;gt; Set) _ _
    (let key : Set := a in
    let value : Set := b in
    let key_ty := Box.(Boxed_map.key_ty) in
    let OPS := Box.(Boxed_map.OPS) in
    let boxed :=
      let &amp;#39;(map, size) := Box.(Boxed_map.boxed) in
      ((Box.(Boxed_map.OPS).(S.MAP.add) k v map),
        (if Box.(Boxed_map.OPS).(S.MAP.mem) k map then
          size
        else
          size +i 1)) in
    {|
      Boxed_map.key_ty := key_ty;
      Boxed_map.OPS := OPS;
      Boxed_map.boxed := boxed
    |}).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We use an existential type to define the type&amp;nbsp;&lt;code&gt;map&lt;/code&gt;. Note that we use an existential type in the sort&amp;nbsp;&lt;code&gt;Set&lt;/code&gt; with the &lt;a href=&quot;https://github.com/coq/coq/wiki/Impredicative-Set&quot;&gt;imprediative Set&lt;/a&gt; option of Coq enabled. This allows us to avoid any universe level issue by always staying in the sort&amp;nbsp;&lt;code&gt;Set&lt;/code&gt;. We define the existential in&amp;nbsp;&lt;code&gt;Set&lt;/code&gt; like the ones in&amp;nbsp;&lt;code&gt;Prop&lt;/code&gt; or&amp;nbsp;&lt;code&gt;Type&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive sigS (A : Type) (P : A -&amp;gt; Set) : Set :=
| existS : forall (x : A), P x -&amp;gt; sigS P.

Notation &amp;quot;{ x : A @ P }&amp;quot; := (sigS (A := A) (fun x =&amp;gt; P)) : type_scope.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To open the first-class module value&amp;nbsp;&lt;code&gt;Box&lt;/code&gt; as a module we use the pattern:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun k v (module Box) -&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in OCaml. We convert this pattern to an existential projection:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let &amp;#39;existS _ _ Box := Box in
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in Coq. Then to close the module and convert it back to a value, we wrap it into an existential with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;existS (A := Set -&amp;gt; Set) _ _ (...)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We hope that the effort we made into removing existential types from the generated Coq code will help to do simpler proofs on OCaml programs using a lot of modules.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/removing-existential-types-from-modules-in-coq-of-ocaml.html</link>
        <guid>https://rocq-blog.clarus.me/removing-existential-types-from-modules-in-coq-of-ocaml.html</guid>
      </item>
    
      <item>
        <title>Improvements of coq-of-ocaml for functors and signatures</title>
        <pubDate>Tue, 20 Oct 2020 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;With &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;coq-of-ocaml&lt;/a&gt; we can translate many &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; constructs to an equivalent in the &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; language. Based on the code which we encounter, we continue to update coq-of-ocaml to handle more OCaml programming patterns. In this post, we will show:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how we changed the representation of functors to have a clearer generated code;&lt;/li&gt;
&lt;li&gt;how we handled the anonymous sub-signatures.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Functors like plain modules&lt;/h2&gt;

&lt;p&gt;Th module system of OCaml allows to represent modules parametrized by others. Such parametrized modules are called &lt;a href=&quot;https://ocaml.org/releases/4.11/htmlman/moduleexamples.html#s:functors&quot;&gt;functors&lt;/a&gt;. We represent modules with a named signature by &lt;a href=&quot;https://coq.inria.fr/refman/language/core/records.html&quot;&gt;records&lt;/a&gt; in Coq. Then we encode functors by functions over dependent records. We like this representation because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we can also encode &lt;a href=&quot;https://ocaml.org/releases/4.11/htmlman/firstclassmodules.html&quot;&gt;first-class modules&lt;/a&gt; (modules used in the context of values in OCaml);&lt;/li&gt;
&lt;li&gt;it does not depend on the functor system of Coq, but only on dependent types. The less Coq features from the kernel we use, the safer we feel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An issue we had was expressing lemmas about items of a functor. For example, with the following OCaml code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module type Source = sig
  type t
  val x : t
  val id : &amp;#39;a -&amp;gt; &amp;#39;a
end

module type Target = sig
  type t
  val y : t
end

module F (X : Source) : Target with type t = X.t = struct
  type t = X.t
  let y = X.x
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we would generate the following Coq code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module Source.
  Record signature {t : Set} : Set := {
    t := t;
    x : t;
  }.
End Source.

Module Target.
  Record signature {t : Set} : Set := {
    t := t;
    y : t;
  }.
End Target.

Definition F :=
  fun (X : {t : Set &amp;amp; Source.signature (t := t)}) =&amp;gt;
    ((let t : Set := (|X|).(Source.t) in
    let y := (|X|).(Source.x) in
    existT (A := unit) (fun _ =&amp;gt; _) tt
      {|
        Target.y := y
      |}) : {_ : unit &amp;amp; Target.signature (t := (|X|).(Source.t))}).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are two module types &lt;code&gt;Source&lt;/code&gt; and &lt;code&gt;Target&lt;/code&gt; which we represent by record definitions. We parametrize these records by their abstract types. We represent the functor &lt;code&gt;F&lt;/code&gt; by a function from the type of record &lt;code&gt;Source&lt;/code&gt; to the type of record &lt;code&gt;Target&lt;/code&gt;. Here the whole definition of &lt;code&gt;F&lt;/code&gt; is packed into a single function. We define the resulting record using local &lt;code&gt;let&lt;/code&gt; declarations. We changed that to have top-level definitions for each item of the functor (&lt;code&gt;t&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;). By representing the functor parameters as a type-class, we now generate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module F.
  Class FArgs := {
    X : {t : Set &amp;amp; Source.signature (t := t)};
  }.

  Definition t `{FArgs} : Set := (|X|).(Source.t).

  Definition y `{FArgs} : (|X|).(Source.t) := (|X|).(Source.x).

  Definition functor `(FArgs)
    : {_ : unit &amp;amp; Target.signature (t := (|X|).(Source.t))} :=
    existT (A := unit) (fun _ =&amp;gt; _) tt
      {|
        Target.y := y
      |}.
End F.
Definition F X := F.functor {| F.X := X |}.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The class &lt;code&gt;FArgs&lt;/code&gt; contains the functor arguments. This class has one field per functor parameter (in this case only &lt;code&gt;X&lt;/code&gt;). We give to each declaration an implicit parameter &lt;code&gt;`{FArgs}&lt;/code&gt; so that the parameter &lt;code&gt;X&lt;/code&gt; is always accessible. At the end of the module, we materialize the functor as a function &lt;code&gt;functor&lt;/code&gt; from an instance of the class of parameters, to the record of the resulting module. Eventually, we define the functor &lt;code&gt;F&lt;/code&gt; as a function taking parameters in order and convert them to an instance of the class &lt;code&gt;FArgs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remark: we do not use the &lt;a href=&quot;https://coq.inria.fr/refman/language/core/sections.html&quot;&gt;section mechanism&lt;/a&gt;, because this would not compose. Indeed, we cannot create modules inside sections in the current version of Coq. Thus we can only represent flat functors with sections, and cannot represent functors with sub-modules.&lt;/p&gt;

&lt;p&gt;For modules represented by records we do the same, without the &lt;code&gt;FArgs&lt;/code&gt; parameter. For example, we translate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module M : Source = struct
  type t = int
  let x = 12
  let id x = x
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module M.
  Definition t : Set := int.

  Definition x : int := 12.

  Definition id {A : Set} (x : A) : A := x.

  Definition module :=
    existT (A := Set) _ t
      {|
        Source.x := x;
        Source.id _ := id
      |}.
End M.
Definition M := M.module.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We hope that this presentation is cleaner on the Coq side. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we can directly talk about individual items without referencing the whole resulting record;&lt;/li&gt;
&lt;li&gt;we can talk about intermediate items which may not be exported at the end;&lt;/li&gt;
&lt;li&gt;we can have plain sub-modules for large functors (which cannot be represented as records with the current system when there are no named signatures);&lt;/li&gt;
&lt;li&gt;we can define new types as we would at top-level.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Anonymous sub-signatures&lt;/h2&gt;

&lt;p&gt;For large signatures, we tend to use sub-signatures in order to group items going together. For example, we can define in OCaml:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module type T_encoding = sig
  type t

  val encoding : t list
end

module type Validator = sig
  module Ciphertext : sig
    include T_encoding

    val get_memo_size : t -&amp;gt; int
  end

  module CV : T_encoding

  type t = Ciphertext.t
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since we represent signatures by records, we cannot directly represent the sub-signature for &lt;code&gt;Ciphertext&lt;/code&gt; in the signature &lt;code&gt;Validator&lt;/code&gt;. Indeed, there are no notions of sub-records in Coq. Thus, we were generating the following error message:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;--- foo.ml:8:23 ------------------------------------------------------------ not_supported (1/1) ---

   6 | 
   7 | module type Validator = sig
&amp;gt;  8 |   module Ciphertext : sig
&amp;gt;  9 |     include T_encoding
&amp;gt; 10 | 
&amp;gt; 11 |     val get_memo_size : t -&amp;gt; int
&amp;gt; 12 |   end
  13 | 
  14 |   module CV : T_encoding
  15 | 


Anonymous definition of signatures is not handled
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we inline the sub-modules by prefixing the name of their fields, so that we generate a single flat record in Coq. For the OCaml code above, we generate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module T_encoding.
  Record signature {t : Set} : Set := {
    t := t;
    encoding : list t;
  }.
End T_encoding.

Module Validator.
  Record signature {Ciphertext_t CV_t : Set} : Set := {
    Ciphertext_t := Ciphertext_t;
    Ciphertext_encoding : list Ciphertext_t;
    Ciphertext_get_memo_size : Ciphertext_t -&amp;gt; int;
    CV : T_encoding.signature (t := CV_t);
    t := Ciphertext_t;
  }.
End Validator.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We prefix all the fields of the sub-module &lt;code&gt;Ciphertext&lt;/code&gt; by &lt;code&gt;Ciphertext_&lt;/code&gt;. We propagate the naming of the types. For example the type &lt;code&gt;t&lt;/code&gt; from &lt;code&gt;Ciphertext&lt;/code&gt; is renamed as &lt;code&gt;Ciphertext_t&lt;/code&gt; in all the subsequent expressions. For all other fields which involve this type &lt;code&gt;t&lt;/code&gt;, we mark it as &lt;code&gt;Ciphertext_t&lt;/code&gt;. We keep using nested records for sub-signatures with a name. For example, the sub-module &lt;code&gt;CV&lt;/code&gt; is a field of type &lt;code&gt;T_encoding.signature&lt;/code&gt;, which is itself a record. The two variables &lt;code&gt;Ciphertext_t&lt;/code&gt; and &lt;code&gt;CV_t&lt;/code&gt; are the two abstract types of the signature. As with flat signatures, we represent them using existential types when unknown.&lt;/p&gt;

&lt;p&gt;We also changed the way we translate the identifiers to reference items in signatures. For example, if we have a functor using the field &lt;code&gt;Ciphertext.get_memo_size&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module F (V : Validator) = struct
  let get = V.Ciphertext.get_memo_size
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we now generate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module F.
  Class FArgs := {
    V :
      {&amp;#39;[Ciphertext_t, CV_t] : [Set ** Set] &amp;amp;
        Validator.signature (Ciphertext_t := Ciphertext_t) (CV_t := CV_t)};
  }.

  Definition get `{FArgs} : (|V|).(Validator.Ciphertext_t) -&amp;gt; int :=
    (|V|).(Validator.Ciphertext_get_memo_size).
End F.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we represent &lt;code&gt;V.Ciphertext.get_memo_size&lt;/code&gt; by &lt;code&gt;(|V|).(Validator.Ciphertext_get_memo_size)&lt;/code&gt;. We use the notation to access record fields with &lt;code&gt;record.(field)&lt;/code&gt;. We project the potential existential variables with the notation &lt;code&gt;(|...|)&lt;/code&gt;. Here are the steps we followed to translate this identifier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;check if &lt;code&gt;V&lt;/code&gt; has a known named signature (in this case we find the signature &lt;code&gt;Validator&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;check if &lt;code&gt;Ciphertext&lt;/code&gt; has a known signature (we do not find one);&lt;/li&gt;
&lt;li&gt;conclude that the &lt;code&gt;get_memo_size&lt;/code&gt; field has probably been inlined in the signature definition;&lt;/li&gt;
&lt;li&gt;generate a record access on &lt;code&gt;V&lt;/code&gt; on the field &lt;code&gt;Ciphertext_get_memo_size&lt;/code&gt;, which is the concatenation of &lt;code&gt;Ciphertext&lt;/code&gt; and &lt;code&gt;get_memo_size&lt;/code&gt; according to the way we prefix field names in inlining.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The handling of anonymous sub-signatures is not completely mandatory. Indeed, we could also give a name to the sub-signatures in the OCaml code. However we encountered some cases were this was convenient, in order not to modify the OCaml code too much.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We have shown two new translation strategies which, we hope, will polish the experience of using &lt;a href=&quot;https://github.com/clarus/coq-of-ocaml&quot;&gt;coq-of-ocaml&lt;/a&gt;. This work was mainly directed by an experiment at handling some code from the &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt; codebase. The aim was to verify some parts of the &lt;a href=&quot;https://blog.nomadic-labs.com/sapling-integration-in-tezos-tech-preview.html&quot;&gt;sapling&lt;/a&gt; project. This code is outside of the protocol, which is the part we are the most used to work with. Then, we met some new OCaml constructs and patterns. In particular, we encountered large functors with anonymous sub-signatures, plain sub-modules, and new type definitions.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/improvements-of-coq-of-ocaml-for-functors-and-signatures.html</link>
        <guid>https://rocq-blog.clarus.me/improvements-of-coq-of-ocaml-for-functors-and-signatures.html</guid>
      </item>
    
      <item>
        <title>GADTs with type erasure in coq-of-ocaml</title>
        <pubDate>Fri, 19 Jun 2020 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;&lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;coq-of-ocaml&lt;/a&gt; is a compiler from the &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; language to &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;. It now supports the conversion of the &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/examples/tezos/&quot;&gt;full Tezos protocol&lt;/a&gt;, composed of around 35.000 lines of code. The &lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/gadts.html&quot;&gt;OCaml&amp;rsquo;s GADTs&lt;/a&gt; are a challenge to compile, as they have no direct equivalent in Coq. We present how we currently convert the GADTs, and especially the new mechanism of type erasure propagation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Let us look at a short example of GADT which we cannot directly compile to Coq. If we define the annotated trees of integers with a&amp;nbsp;&lt;code&gt;sum&lt;/code&gt; function for trees of two elements:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type _ t =
  | Leaf : int -&amp;gt; int t
  | Node : &amp;#39;a t * &amp;#39;b t -&amp;gt; (&amp;#39;a * &amp;#39;b) t

let sum (tree : (int * int) t) : int =
  match tree with
  | Node (Leaf x, Leaf y) -&amp;gt; x + y
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;a direct translation to Coq could be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive t : Set -&amp;gt; Set :=
| Leaf : Z -&amp;gt; t Z
| Node : forall {A B : Set}, t A -&amp;gt; t B -&amp;gt; t (A * B).

Definition sum (tree : t (Z * Z)) : Z :=
  match tree with
  | Node (Leaf x) (Leaf y) =&amp;gt; x + y
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but this causes the following error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Error: Non exhaustive pattern-matching: no clause found for pattern Leaf _
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Coq cannot decide that this pattern-matching is exhaustive because types are not comparable in Coq. One could even add an axiom stating that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Z = Z * Z
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and we could not derive a contradiction as these two types have the same cardinality. See this &lt;a href=&quot;https://coq.discourse.group/t/inequality-between-types/106&quot;&gt;discussion&lt;/a&gt; on type equality for more information. A solution with dependent types could be to use tags (which are values) to annotate the type&amp;nbsp;&lt;code&gt;t&lt;/code&gt;. We can say that with dependent types we &amp;ldquo;match values&amp;rdquo; while with GADTs we &amp;ldquo;unify types&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;Another issue is the fact that OCaml unifies polymorphic type variables of other values in the&amp;nbsp;&lt;code&gt;match&lt;/code&gt;. In Coq we can use the &lt;a href=&quot;http://adam.chlipala.net/cpdt/html/MoreDep.html&quot;&gt;convoy pattern&lt;/a&gt; to achieve similar results. Finally, having a type appearing as a type parameter for itself in a GADT definition may break the strict positivity constraint and make Coq to refuse the type definition.&lt;/p&gt;

&lt;p&gt;The main idea of our solution is to erase the type parameters of the GADTs. When there is a match with a GADT value, we need a way to get back the information we lost erasing the types. Using OCaml attributes to guide coq-of-ocaml, the user can add in the generated code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;code&gt;match&lt;/code&gt; branch for the impossible cases;&lt;/li&gt;
&lt;li&gt;some casts on the variables introduced by the &lt;code&gt;match&lt;/code&gt; patterns;&lt;/li&gt;
&lt;li&gt;a cast on the results of the &lt;code&gt;match&lt;/code&gt; branches.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We define the impossible &lt;code&gt;match&lt;/code&gt; branch result and the casts operators in Coq using axioms.&lt;/p&gt;

&lt;p&gt;We transitively propagate the type erasures from GADTs and phantom types. Erasing unused type variables is important in order not to block the type inference mechanism of Coq. Indeed, when a type variable is not used:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition id_nat {A : Set} (n : nat) : nat := n.

Definition one := id_nat 1.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Coq often reacts with the following error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; Definition one := id_nat 1.
&amp;gt;                   ^^^^^^
Error: Cannot infer the implicit parameter A of id_nat whose type is &amp;quot;Set&amp;quot;.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;even if any set could fit for &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Definition of types&lt;/h2&gt;

&lt;p&gt;We consider an algebraic type definition in OCaml to be a GADT if the return type parameters of some constructors are not (different) polymorphic type variables. Here is an example of GADT:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type _ expr =
  | Int : int -&amp;gt; int expr
  | Couple : &amp;#39;a expr * &amp;#39;b expr -&amp;gt; (&amp;#39;a * &amp;#39;b) expr
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is not a GADT:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type &amp;#39;loc ast =
  | Const : int * &amp;#39;loc -&amp;gt; &amp;#39;loc ast
  | Add : &amp;#39;loc ast * &amp;#39;loc ast * &amp;#39;loc -&amp;gt; &amp;#39;loc ast
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We could also write &lt;code&gt;&amp;#39;loc ast&lt;/code&gt; with the &lt;code&gt;of&lt;/code&gt; syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type &amp;#39;loc ast =
  | Const of int * &amp;#39;loc
  | Add of &amp;#39;loc ast * &amp;#39;loc ast * &amp;#39;loc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We do not consider the &lt;code&gt;printable&lt;/code&gt; type to be a GADT:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type printable =
  | Printable : &amp;#39;a * (&amp;#39;a -&amp;gt; string) -&amp;gt; printable
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The reason why we define the type &lt;code&gt;printable&lt;/code&gt; with the GADT syntax in OCaml is because there is an existential type variable &lt;code&gt;&amp;#39;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We transform the previous types into the following Coq code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive expr : Set :=
| Int : int -&amp;gt; expr
| Couple : expr -&amp;gt; expr -&amp;gt; expr.

Inductive ast (loc : Set) : Set :=
| Const : int -&amp;gt; loc -&amp;gt; ast loc
| Add : ast loc -&amp;gt; ast loc -&amp;gt; loc -&amp;gt; ast loc.

Arguments Const {_}.
Arguments Add {_}.

Inductive printable : Set :=
| Printable : forall {a : Set}, a -&amp;gt; (a -&amp;gt; string) -&amp;gt; printable.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the &lt;code&gt;expr&lt;/code&gt; type we remove the type parameter. Note that it does not change the information available at runtime. We transform the &lt;code&gt;ast&lt;/code&gt; type keeping the type parameter &lt;code&gt;loc&lt;/code&gt;. We use a &lt;code&gt;forall&lt;/code&gt; quantifier in the &lt;code&gt;Printable&lt;/code&gt; constructor to encode the existential type variable &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Type expressions&lt;/h2&gt;

&lt;p&gt;We apply the erasure of unused types to type expressions. For example, with the following OCaml code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type &amp;#39;a num = int

type &amp;#39;a num_with_label = &amp;#39;a num * string

let add_label (n : &amp;#39;a num) =
  (n, string_of_int n)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we generate the Coq code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition num : Set := int.

Definition num_with_label : Set := num * string.

Definition add_label (n : num) : num_with_label :=
  (n, (OCaml.Stdlib.string_of_int n)).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We consider a type parameter to be unused if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it does not appear in the type expression (such as in &lt;code&gt;num&lt;/code&gt;), or;&lt;/li&gt;
&lt;li&gt;is a GADT type parameter, or;&lt;/li&gt;
&lt;li&gt;is only used by types which do not use their argument (such as in &lt;code&gt;num_with_label&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks to this propagation of erasure, we limit the number of type parameters appearing in the generated types. This is helpful because with the erasure of GADT parameters many types become useless in Coq and clutter the output. It also reduces the number of type errors during type inference for unused implicit type parameters, as we have seen in the introduction.&lt;/p&gt;

&lt;h2&gt;Pattern matching&lt;/h2&gt;

&lt;p&gt;By default, we only do a syntactic transformation from pattern matching in OCaml to pattern matching in Coq. We encode the &lt;code&gt;when&lt;/code&gt; clauses with an additional boolean parameter:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let is_positive x =
  match x with
  | Ok n when n &amp;gt;= 0 -&amp;gt; true
  | _ -&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is transformed to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition is_positive {A : Set} (x : sum int A) : bool :=
  match
    (x,
      match x with
      | Stdlib.Ok n =&amp;gt; OCaml.Stdlib.ge n 0
      | _ =&amp;gt; false
      end) with
  | (Stdlib.Ok n, true) =&amp;gt; true
  | (_, _) =&amp;gt; false
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We rename the existential type variables introduced by some constructors to their name in OCaml. The OCaml compiler typically names these variables &lt;code&gt;$something&lt;/code&gt;. We can see the existential type names of OCaml in error messages or using &lt;a href=&quot;https://github.com/ocaml/merlin&quot;&gt;Merlin&lt;/a&gt; to inspect the type of an expression. Even if not necessary in most cases, having this renaming is helpful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in case some sub-expressions in a &lt;code&gt;match&lt;/code&gt; branch cite the existential types in type annotations;&lt;/li&gt;
&lt;li&gt;for debugging, so that the type names on the Coq side are the same as on the OCaml side.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We do this renaming by doing a trick consisting into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;building an &lt;code&gt;existT&lt;/code&gt; value;&lt;/li&gt;
&lt;li&gt;destructuring this value right after, giving a name to the existential types at this moment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, with our previous &lt;code&gt;printable&lt;/code&gt; type example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type printable =
  | Printable : &amp;#39;a * (&amp;#39;a -&amp;gt; string) -&amp;gt; printable

let pretty_print (x : printable) : string =
  let Printable (v, to_string) = x in
  to_string v
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we get:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive printable : Set :=
| Printable : forall {a : Set}, a -&amp;gt; (a -&amp;gt; string) -&amp;gt; printable.

Definition pretty_print (x : printable) : string :=
  let &amp;#39;Printable v to_string := x in
  let &amp;#39;existT _ __Printable_&amp;#39;a [v, to_string] :=
    existT (A := Set)
      (fun __Printable_&amp;#39;a =&amp;gt; [__Printable_&amp;#39;a ** __Printable_&amp;#39;a -&amp;gt; string]) _
      [v, to_string] in
  to_string v.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;because in OCaml the existential type is named &lt;code&gt;$Printable_&amp;#39;a&lt;/code&gt; in this case (we replace the &lt;code&gt;$&lt;/code&gt; symbol by &lt;code&gt;__&lt;/code&gt; to get accepted by Coq).&lt;/p&gt;

&lt;h2&gt;Pattern matching with axioms&lt;/h2&gt;

&lt;p&gt;Sometimes, doing a syntactic transformation for the pattern matching is not enough in case of GADTs. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type _ expr =
  | Int : int -&amp;gt; int expr
  | Couple : &amp;#39;a expr * &amp;#39;b expr -&amp;gt; (&amp;#39;a * &amp;#39;b) expr

let left_and_right (e : (int * &amp;#39;a) expr) : int * &amp;#39;a expr =
  match e with
  | Couple (Int n, e) -&amp;gt; (n, e)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;generates:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive expr : Set :=
| Int : int -&amp;gt; expr
| Couple : expr -&amp;gt; expr -&amp;gt; expr.

Definition left_and_right (e : expr) : int * expr :=
  let &amp;#39;Couple (Int n) e := e in
  (n, e).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which is ill-typed in Coq:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Error: Non exhaustive pattern-matching: no clause found for pattern Int _
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Default branch&lt;/h3&gt;

&lt;p&gt;We can require a default &lt;code&gt;match&lt;/code&gt; branch with the OCaml attribute &lt;code&gt;@coq_match_with_default&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let left_and_right (e : (int * &amp;#39;a) expr) : int * &amp;#39;a expr =
  match[@coq_match_with_default] e with
  | Couple (Int n, e) -&amp;gt; (n, e)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which generates:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition left_and_right (e : expr) : int * expr :=
  match e with
  | Couple (Int n) e =&amp;gt; (n, e)
  | _ =&amp;gt; unreachable_gadt_branch
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where &lt;code&gt;unreachable_gadt_branch&lt;/code&gt; is an axiom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Parameter unreachable_gadt_branch : forall {A : Set}, A.
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Casting at the entry of branches&lt;/h3&gt;

&lt;p&gt;Sometimes we need to cast the free variables introduced by patterns:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type &amp;#39;a int_or_bool =
  | Int : int int_or_bool
  | Bool : bool int_or_bool

let to_int (type a) (kind : a int_or_bool) (x : a) : int =
  match[@coq_match_gadt] (kind, x) with
  | (Int, (x : int)) -&amp;gt; x
  | (Bool, (x : bool)) -&amp;gt; if x then 1 else 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;generates:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive int_or_bool : Set :=
| Int : int_or_bool
| Bool : int_or_bool.

Definition to_int {a : Set} (kind : int_or_bool) (x : a) : Z :=
  match (kind, x) with
  | (Int, _ as x) =&amp;gt;
    let x := cast Z x in
    x
  | (Bool, _ as x) =&amp;gt;
    let x := cast bool x in
    if x then
      1
    else
      0
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where we cast each pattern variable as explicitly stated in the OCaml code. Without type annotations the cast would be towards the type &lt;code&gt;a&lt;/code&gt;, what is correct but unhelpful to type-check the Coq code. The &lt;code&gt;cast&lt;/code&gt; operator is an axiom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Axiom cast : forall {A : Set} (B : Set), A -&amp;gt; B.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that this axiom is unsound as we can inhabit&amp;nbsp;&lt;code&gt;Empty_set&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cast Empty_set tt : Empty_set
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We suppose the use of the&amp;nbsp;&lt;code&gt;cast&lt;/code&gt; to be valid as we follow the types computed by the OCaml compiler. To eliminate the&amp;nbsp;&lt;code&gt;cast&lt;/code&gt; axiom is proofs, we can use the following evaluation axiom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Axiom cast_eval : forall {A : Set} {x : A}, cast A x = x.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For example, to show that&amp;nbsp;&lt;code&gt;to_int&lt;/code&gt; is idempotent over integers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Lemma to_int_idempotent (n : Z) : to_int Int (to_int Int n) = n.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we first evaluate the left-hand side expression:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;unfold to_int; simpl.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;what gives us:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  n : Z
  ============================
  cast Z (cast Z n) = n
1 subgoal
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By rewriting two times with&amp;nbsp;&lt;code&gt;cast_eval&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;do 2 rewrite cast_eval.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we obtain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1 subgoal

  n : Z
  ============================
  n = n
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and then by reflexivity the proof is completed. If we give incorrect parameters to&amp;nbsp;&lt;code&gt;to_int&lt;/code&gt;, such as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;to_int Int false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;the&amp;nbsp;&lt;code&gt;cast_eval&lt;/code&gt; axiom does not apply. Still, this term cannot be a valid OCaml code, so we believe that the&amp;nbsp;&lt;code&gt;cast_eval&lt;/code&gt; axiom covers all the interesting cases.&lt;/p&gt;

&lt;h3&gt;Casting the result of branches&lt;/h3&gt;

&lt;p&gt;In case there is a need to also cast the result value of each branch there is the &lt;code&gt;@coq_match_gadt_with_result&lt;/code&gt; attribute:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let incr_if_int (type a) (kind : a int_or_bool) (x : a) : a =
  match[@coq_match_gadt_with_result] (kind, x) with
  | (Int, (x : int)) -&amp;gt; x + 1
  | (Bool, (x : bool)) -&amp;gt; x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;generates:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition incr_if_int {a : Set} (kind : int_or_bool) (x : a) : a :=
  match (kind, x) with
  | (Int, _ as x) =&amp;gt;
    let x := cast Z x in
    cast a (Z.add x 1)
  | (Bool, _ as x) =&amp;gt;
    let x := cast bool x in
    cast a x
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Casting with existential variables&lt;/h3&gt;

&lt;p&gt;When there are existential type variables in GADTs, we introduce them with an extended form of cast axiom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Axiom cast_exists : forall {A : Set} {Es : Type} (T : Es -&amp;gt; Set),
  A -&amp;gt; {vs : Es &amp;amp; T vs}.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where &lt;code&gt;Es&lt;/code&gt; is typically a tuple of types. For example, to sum a tree of integers with some type witness, we transform:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type _ ty =
  | Int : int ty
  | Couple : &amp;#39;a ty * &amp;#39;b ty -&amp;gt; (&amp;#39;a * &amp;#39;b) ty

let[@coq_struct &amp;quot;t&amp;quot;] rec sum : type a. a ty -&amp;gt; a -&amp;gt; int
  = fun t e -&amp;gt;
  match[@coq_match_gadt] (t, e) with
  | (Int, (n : int)) -&amp;gt; n
  | (Couple (t1, t2), (e1e2 : _ * _)) -&amp;gt;
    let (e1, e2) = e1e2 in
    sum t1 e1 + sum t2 e2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Unset Guard Checking. (* needed to disable termination check *)

Inductive ty : Set :=
| Int : ty
| Couple : ty -&amp;gt; ty -&amp;gt; ty.

Fixpoint sum {a : Set} (t : ty) (e : a) {struct t} : int :=
  match (t, e) with
  | (Int, _ as n) =&amp;gt;
    let n := cast int n in
    n
  | (Couple t1 t2, _ as e1e2) =&amp;gt;
    let &amp;#39;existT _ [__0, __1] [t1, t2, e1e2] :=
      cast_exists (Es := [Set ** Set])
        (fun &amp;#39;[__0, __1] =&amp;gt; [ty ** ty ** __0 * __1]) [t1, t2, e1e2] in
    let &amp;#39;(e1, e2) := e1e2 in
    Z.add (sum t1 e1) (sum t2 e2)
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we need to introduce the existential variables with the cast to be able to say that &lt;code&gt;e1e2&lt;/code&gt; is a couple of type &lt;code&gt;__1 * __2&lt;/code&gt;, for &lt;em&gt;some&lt;/em&gt; types &lt;code&gt;__1&lt;/code&gt; and &lt;code&gt;__2&lt;/code&gt;. Like for pattern matching without GADTs, we reuse the names generated by OCaml, replacing the &lt;code&gt;$&lt;/code&gt; symbol by &lt;code&gt;__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As a result, with all the techniques above, we can translate the whole Tezos protocol to Coq. This includes the interpreter and type-checker of the smart-contract language &lt;a href=&quot;https://tezos.gitlab.io/whitedoc/michelson.html&quot;&gt;Michelson&lt;/a&gt;, which relies heavily on GADTs. We needed to annotate some of the functions manipulating GADTs, even if we tried to reduce the amount of annotations to a minimum. Eventually, we would like a system where we keep all the type information during the translation, so that we do not need axioms in the generated code.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/gadts-with-type-erasure-in-coq-of-ocaml.html</link>
        <guid>https://rocq-blog.clarus.me/gadts-with-type-erasure-in-coq-of-ocaml.html</guid>
      </item>
    
      <item>
        <title>Latest updates of coq-of-ocaml for the Tezos protocol</title>
        <pubDate>Mon, 17 Feb 2020 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;We recently made a lot of progress on the formalization in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; of the &lt;a href=&quot;https://gitlab.com/tezos/tezos/-/tree/master/src/proto_alpha/lib_protocol&quot;&gt;Tezos economic protocol&lt;/a&gt;, written in &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt;, using &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;coq-of-ocaml&lt;/a&gt;. We present here some of the new features of coq-of-ocaml and the results on the formalization of Tezos.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Functors&lt;/h2&gt;

&lt;p&gt;Previously, we were using dependent records to represent first-class modules. We now use dependent records whenever possible, even for non-first-class modules. This enables us to convert &lt;a href=&quot;https://dev.realworldocaml.org/functors.html&quot;&gt;OCaml functors&lt;/a&gt; to Coq functions over dependent records. As an example, we translate this OCaml program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(* Signature of the parameter of the functor *)
module type Source = sig
  type t
  val x : t
end

(* Signature of the result of the functor *)
module type Target = sig
  type t
  val y : t
end

(* Definition of the functor *)
module F (X : Source) : Target with type t = X.t = struct
  type t = X.t
  let y = X.x
end

(* Application to the module [M] *)
module M : Source = struct
  type t = int
  let x = 12
end

module N = F (M)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to the following Coq code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module Source.
  Record signature {t : Set} := {
    t := t;
    x : t;
  }.
  Arguments signature : clear implicits.
End Source.

Module Target.
  Record signature {t : Set} := {
    t := t;
    y : t;
  }.
  Arguments signature : clear implicits.
End Target.

Definition F :=
  fun (X : {t : _ &amp;amp; Source.signature t}) =&amp;gt;
    (let t := (|X|).(Source.t) in
    let y := (|X|).(Source.x) in
    existT (fun _ =&amp;gt; _) tt
      {|
        Target.y := y
      |} : {_ : unit &amp;amp; Target.signature (|X|).(Source.t)}).

Definition M :=
  let t := Z in
  let x := 12 in
  existT _ _
    {|
      Source.x := x
    |}.

Definition N :=
  F
    (existT _ _
      {|
        Source.x := (|M|).(Source.x)
      |}).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We note &lt;code&gt;(|M|)&lt;/code&gt; the projection &lt;code&gt;projT2&lt;/code&gt; to get the second component of an existential type &lt;code&gt;{x : A &amp;amp; P x}&lt;/code&gt;. We represent signatures by records parametrized by their abstract types. We wrap these records with existential types for the abstract types which are not precised by a &lt;code&gt;with type t = ...&lt;/code&gt; construct. Functors are then plain Coq functions over dependent records. For more details, see the documentation on the &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/docs/module-system&quot;&gt;module system&lt;/a&gt; in coq-of-ocaml.&lt;/p&gt;

&lt;p&gt;Our convention is to represent a module as a dependent record if there exists a name for its signature. In this case, we use its signature name for its record&amp;rsquo;s type name. We consider the other modules as namespaces, and represent them with standard Coq modules.&lt;/p&gt;

&lt;h2&gt;GADTs and existential types&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/manual033.html&quot;&gt;GADTs&lt;/a&gt;, an advanced form of algebraic datatypes, are used a lot in the Tezos protocol. They help to ensure safety properties, like the soundness of the type-checker of the smart-contracts.&lt;/p&gt;

&lt;h3&gt;GADTs&lt;/h3&gt;

&lt;p&gt;As we did not find a general way to convert GADTs to Coq, we chose to erase the type annotations and introduce unsafe casts. Here is an example of OCaml code with a GADT:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type &amp;#39;a int_or_string =
  | Int : int int_or_string
  | String : string int_or_string

let to_string (type a) (kind : a int_or_string) (x : a) : string =
  match[@coq_match_gadt] kind, x with
  | Int, (x : int) -&amp;gt; string_of_int x
  | String, (x : string) -&amp;gt; x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the &lt;code&gt;[@coq_match_gadt]&lt;/code&gt; on the &lt;code&gt;match&lt;/code&gt; and the type annotations on the variable &lt;code&gt;x&lt;/code&gt; in the patterns. We import this code in Coq to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Reserved Notation &amp;quot;&amp;#39;int_or_string&amp;quot;.

Inductive int_or_string_gadt : Set :=
| Int : int_or_string_gadt
| String : int_or_string_gadt

where &amp;quot;&amp;#39;int_or_string&amp;quot; := (fun (_ : Set) =&amp;gt; int_or_string_gadt).

Definition int_or_string := &amp;#39;int_or_string.

Definition to_string {A : Set} (kind : int_or_string A) (x : A) : string :=
  match (kind, x) with
  | (Int, _ as x) =&amp;gt;
    let &amp;#39;existT _ tt x := cast_exists (fun _ =&amp;gt; Z) x in
    cast string (OCaml.Stdlib.string_of_int x)
  | (String, _ as x) =&amp;gt;
    let &amp;#39;existT _ tt x := cast_exists (fun _ =&amp;gt; string) x in
    cast string x
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We convert the GADT &lt;code&gt;int_or_string&lt;/code&gt; to an inductive &lt;code&gt;int_or_string_gadt&lt;/code&gt; without annotations. We generate two axioms in each branch of the &lt;code&gt;match&lt;/code&gt;, to cast:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the variables introduced by the pattern;&lt;/li&gt;
&lt;li&gt;the result of the branch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The axiom &lt;code&gt;cast&lt;/code&gt; is the equivalent in Coq of the OCaml&amp;rsquo;s &lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/libref/Obj.html&quot;&gt;Obj.magic&lt;/a&gt; cast. It has the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Axiom cast : forall {A : Set} (B : Set), A -&amp;gt; B.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We say that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cast&lt;/code&gt; behaves as the identity function when &lt;code&gt;A&lt;/code&gt; is equal to &lt;code&gt;B&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;is undefined in other cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To specify this behavior, we use the following axiom:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Axiom cast_eval : forall {A : Set} {x : A}, cast A x = x.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While doing proofs, we must use the axiom &lt;code&gt;cast_eval&lt;/code&gt; to evaluate &lt;code&gt;cast&lt;/code&gt; by proving that the types &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; are the same. Doing so, we also verify that the type unifications of the type-checker of OCaml are indeed correct. The &lt;code&gt;cast_exists&lt;/code&gt; axiom is like &lt;code&gt;cast&lt;/code&gt; with the ability to introduce some existential variables when needed.&lt;/p&gt;

&lt;h3&gt;Existential variables&lt;/h3&gt;

&lt;p&gt;Types with existential variables are a special case of GADTs, where the type parameters are the same for all the constructors. For example, to represent a value which can be converted to a &lt;code&gt;string&lt;/code&gt;, we can use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type printable = Printable : &amp;#39;a * (&amp;#39;a -&amp;gt; string) -&amp;gt; printable

let printable_to_string (x : printable) : string =
  let Printable (value, print) = x in
  print value
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example, &lt;code&gt;&amp;#39;a&lt;/code&gt; is an existential variable. Coq also support existential variables in algebraic types. Here is what we generate for this example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive printable : Set :=
| Printable : forall {a : Set}, a -&amp;gt; (a -&amp;gt; string) -&amp;gt; printable.

Definition printable_to_string (x : printable) : string :=
  let &amp;#39;Printable value print := x in
  let &amp;#39;existT _ __Printable_&amp;#39;a [value, print] :=
    existT
      (fun __Printable_&amp;#39;a : Set =&amp;gt;
        [__Printable_&amp;#39;a ** (__Printable_&amp;#39;a -&amp;gt; string)]) _ [value, print] in
  print value.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We do not need any axioms. The &lt;code&gt;let &amp;#39;existT _ __Printable_&amp;#39;a [value, print] :=&lt;/code&gt; block is there to rename the existential variables generated by Coq to the names of the OCaml compiler. We replace the forbidden symbol &lt;code&gt;$&lt;/code&gt; by &lt;code&gt;__&lt;/code&gt;, so that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$Printable_&amp;#39;a
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;becomes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;__Printable_&amp;#39;a
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Having well named existential variables helps to get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cleaner error messages;&lt;/li&gt;
&lt;li&gt;type annotations on sub-expressions using these existential variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Documentation website&lt;/h2&gt;

&lt;p&gt;We added a &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website for coq-of-ocaml&lt;/a&gt; to have a central place with documentation. We used &lt;a href=&quot;https://docusaurus.io/&quot;&gt;Docusaurus&lt;/a&gt; to generate the website. We chose Docusaurus for the following reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;open-source;&lt;/li&gt;
&lt;li&gt;good defaults;&lt;/li&gt;
&lt;li&gt;generates static HTML;&lt;/li&gt;
&lt;li&gt;we can write the documentation in Markdown.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;We converted most of the protocol&lt;/h2&gt;

&lt;p&gt;At the time of writing, 57% of the Coq code generated by coq-of-ocaml for the Tezos protocol compiles. This includes the interpreter of the smart-contracts, and amounts to around 30.000 lines of valid Coq code. We still ignore many features, like the side-effects or the extensible types. The main missing files are the &lt;code&gt;*_services.ml&lt;/code&gt; files and the type-checker of smart-contracts (6.000 lines of OCaml, the largest file of the protocol).&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/latest-updates-of-coq-of-ocaml-for-the-tezos-protocol.html</link>
        <guid>https://rocq-blog.clarus.me/latest-updates-of-coq-of-ocaml-for-the-tezos-protocol.html</guid>
      </item>
    
      <item>
        <title>Formalization of the Tezos protocol&#39;s interface in Coq</title>
        <pubDate>Wed, 15 Jan 2020 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;The protocol of&amp;nbsp;&lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt; is written in the&amp;nbsp;&lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; language. It uses a&amp;nbsp;&lt;a href=&quot;https://gitlab.com/tezos/tezos/tree/master/src/lib_protocol_environment/sigs/v1&quot;&gt;restricted&amp;nbsp;OCaml interface&lt;/a&gt; of a few thousands lines of code to access to its primitives. Using the compiler&amp;nbsp;&lt;a href=&quot;https://github.com/clarus/coq-of-ocaml&quot;&gt;coq-of-ocaml&lt;/a&gt;, we generate a&amp;nbsp;&lt;a href=&quot;static/artifacts/tezos-interface-in-coq/v1_mli.html&quot;&gt;Coq formalization of most of the&amp;nbsp;Tezos protocol&amp;rsquo;s interface&lt;/a&gt;&amp;nbsp;(the missing&amp;nbsp;OCaml constructs are listed&amp;nbsp;&lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/examples/tezos-interface/&quot;&gt;here&lt;/a&gt;). We hope this work to be a first step to enable formal reasoning on the implementation of the&amp;nbsp;Tezos protocol. In this blog post, we present what we added to&amp;nbsp;&lt;code&gt;coq-of-ocaml&lt;/code&gt; in order to support the&amp;nbsp;Tezos interface.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Polymorphic variants&lt;/h2&gt;

&lt;p&gt;In&amp;nbsp;OCaml, we can tag sum types with the backquote operator as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type json =
  [ `O of (string * json) list
  | `Bool of bool
  | `Float of float
  | `A of json list
  | `Null
  | `String of string ]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are no direct equivalents in&amp;nbsp;&lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; and we generate a warning message when we encounter tags. Still, we generate some code as a first approximation. For type definitions we generate a&amp;nbsp;Coq inductive:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive json : Set :=
| Bool : bool -&amp;gt; json
| Null : json
| O : list (string * json) -&amp;gt; json
| Float : Z -&amp;gt; json
| String : string -&amp;gt; json
| A : list json -&amp;gt; json.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and for inlined variants:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val fold :
  t -&amp;gt;
  key -&amp;gt;
  init:&amp;#39;a -&amp;gt;
  f:([`Key of key | `Dir of key] -&amp;gt; &amp;#39;a -&amp;gt; &amp;#39;a Lwt.t) -&amp;gt;
  &amp;#39;a Lwt.t
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we generate a sum type with the tags as comments:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Parameter fold : forall {a : Set},
  t -&amp;gt; key -&amp;gt; a -&amp;gt; ((* `Dir *) key + (* `Key *) key -&amp;gt; a -&amp;gt; Lwt.t a) -&amp;gt;
  Lwt.t a.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Types and values collisions&lt;/h2&gt;

&lt;p&gt;In&amp;nbsp;Coq, in contrast to&amp;nbsp;OCaml, types and values live in the same namespace. Thus there are often name collisions in the generated code. Typically, this is due to the use of values having the same name as their type. We prevent that for the common cases&amp;nbsp;(&lt;code&gt;list&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, &amp;hellip;) by translating the value names with a&amp;nbsp;&lt;code&gt;_value&lt;/code&gt; suffix. For example, we convert:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val string : string encoding
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Parameter __string_value : encoding string.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Modules&lt;/h2&gt;

&lt;p&gt;We added the import of the definitions of module types in the&amp;nbsp;&lt;code&gt;.mli&lt;/code&gt; files. The code to do so is the same as for&amp;nbsp;&lt;code&gt;.ml&lt;/code&gt; files. For example, we import:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module type COMPARABLE = sig
  type t

  val compare : t -&amp;gt; t -&amp;gt; int
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module COMPARABLE.
  Record signature {t : Set} := {
    t := t;
    compare : t -&amp;gt; t -&amp;gt; Z;
  }.
  Arguments signature : clear implicits.
End COMPARABLE.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We handle the import of modules declared with a signature by unfolding the signature. We do so as we do not represent signatures in&amp;nbsp;Coq unless for first-class modules&amp;nbsp;(using a record type). For example, we import:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module String : COMPARABLE with type t = string
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module String.
  Definition t := string.

  Parameter compare : t -&amp;gt; t -&amp;gt; Z.
End String.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This idea is due to&amp;nbsp;&lt;a href=&quot;https://fr.linkedin.com/in/mehdibouaziz&quot;&gt;Mehdi Bouaziz&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We also handle the&amp;nbsp;&lt;code&gt;include&lt;/code&gt; keyword by doing a similar unfolding. The unfolding of&amp;nbsp;&lt;code&gt;include&lt;/code&gt; significantly increases the size of the&amp;nbsp;Coq code for the Tezos protocol&amp;rsquo;s interface. Indeed, the number of generated lines for the interface was multiplied by two once we converted the&amp;nbsp;&lt;code&gt;include&lt;/code&gt; occurences.&lt;/p&gt;

&lt;h2&gt;First-class modules&lt;/h2&gt;

&lt;p&gt;We improved the detection of first-class modules. The detection of first-class modules is a challenge in&amp;nbsp;&lt;code&gt;coq-of-ocaml&lt;/code&gt;. Indeed, we need to distinguish between first-class and plain modules in order to generated either a dependent record or a&amp;nbsp;Coq module. Moreover, there are no builtin ways to translate between a record and a module in&amp;nbsp;Coq.&lt;/p&gt;

&lt;p&gt;When we access to the field of a module, we consider this module as a&amp;nbsp;Coq record when it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;has a named signature (to have a corresponding named record definition);&lt;/li&gt;
&lt;li&gt;is locally opened in an expression.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We added the constraint of being locally opened in order to filter out some plain modules with a named signature. We may change this rule in the future and convert more plain modules to records by default, as this could factorize the generated code and help to handle functors.&lt;/p&gt;

&lt;p&gt;We added the support of first-class modules with polymorphic abstract types. For that, we mark the arity of the abstract types. For example, for a map signature in&amp;nbsp;OCaml:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module type MAP = sig
  type key
  type +&amp;#39;a t
  val empty : &amp;#39;a t
  val is_empty : &amp;#39;a t -&amp;gt; bool
  val mem : key -&amp;gt; &amp;#39;a t -&amp;gt; bool
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we generate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module MAP.
  Record signature {key : Set} {t : Set -&amp;gt; Set} := {
    key := key;
    t := t;
    empty : forall {a : Set}, t a;
    is_empty : forall {a : Set}, t a -&amp;gt; bool;
    mem : forall {a : Set}, key -&amp;gt; t a -&amp;gt; bool;
  }.
  Arguments signature : clear implicits.
End MAP.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Records&lt;/h2&gt;

&lt;p&gt;In&amp;nbsp;Coq, the fields of a record are projection functions which live in the same namespace as the values. Thus&amp;nbsp;Coq is more prone to name collisions for record fields than&amp;nbsp;OCaml. We solve this issue by putting the record definitions into modules of the same name. For example, for the record:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type descr = {name : string; descr : string option}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we generate the following&amp;nbsp;Coq code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module descr.
  Record record := {
    name : string;
    descr : option string }.
End descr.
Definition descr := descr.record.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We thus prevent a name collision between the record type&amp;nbsp;&lt;code&gt;descr&lt;/code&gt; and the record field&amp;nbsp;&lt;code&gt;descr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In subsequent accesses to the record fields, we prefix the projections by the record name. For example, to access to the&amp;nbsp;&lt;code&gt;name&lt;/code&gt; field of a record instance&amp;nbsp;&lt;code&gt;r&lt;/code&gt; of&amp;nbsp;&lt;code&gt;descr&lt;/code&gt;, we write in&amp;nbsp;Coq:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;r.(descr.name)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Pretty-printing&lt;/h2&gt;

&lt;p&gt;We improved the pretty-printing for the types. In particular:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we limit the number of parenthesis by taking into account the precedence of the operators;&lt;/li&gt;
&lt;li&gt;we use a more consistent indentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As an example, we moved from the following generated&amp;nbsp;Coq:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;| FLambda : Z -&amp;gt; (list ((Context.binder_annot Names.Name.t) * Constr.constr)) -&amp;gt;
  Constr.constr -&amp;gt; (Esubst.subs fconstr) -&amp;gt; fterm
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;| FLambda :
  Z -&amp;gt; list (Context.binder_annot Names.Name.t * Constr.constr) -&amp;gt;
  Constr.constr -&amp;gt; Esubst.subs fconstr -&amp;gt; fterm
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We generated around six thousands valid&amp;nbsp;Coq lines. Some&amp;nbsp;OCaml constructs were missing, such as the extensible types. We may not support them as we see no direct equivalents in&amp;nbsp;Coq. There is also a lot of code duplication due to the repetitive inclusion of similar module signatures. We will try to find solutions to factorize the generated code.&lt;/p&gt;

&lt;p&gt;Finally, we will see if the code for the&amp;nbsp;Tezos interface is usable when applying&amp;nbsp;&lt;code&gt;coq-of-ocaml&lt;/code&gt; to the protocol implementation.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/formalization-of-the-tezos-protocol-s-interface-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/formalization-of-the-tezos-protocol-s-interface-in-coq.html</guid>
      </item>
    
      <item>
        <title>First-class modules in coq-of-ocaml</title>
        <pubDate>Mon, 04 Nov 2019 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;For now, &lt;a href=&quot;https://github.com/clarus/coq-of-ocaml&quot;&gt;coq-of-ocaml&lt;/a&gt; only supports plain modules used as namespaces (no functors). &lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/manual028.html&quot;&gt;First-class modules&lt;/a&gt; are an important construction to abstract code in &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; as there are flexible and used heavily in some programs (including in &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;). We present our strategy to import first-class modules to dependent record in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;. We show that it works for the set example extracted from the Tezos source code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Strategy&lt;/h2&gt;

&lt;p&gt;Here is an example of a first-class module in OCaml. We say that a type&amp;nbsp;&lt;code&gt;t&lt;/code&gt; is&amp;nbsp;&lt;code&gt;Printable&lt;/code&gt; when it provides a function&amp;nbsp;&lt;code&gt;to_string&lt;/code&gt; to convert it in&amp;nbsp;&lt;code&gt;string&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module type Printable = sig
  type t
  val to_string : t -&amp;gt; string
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Given a printable type, we can write a generic function to print it on the terminal:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let print_on_the_terminal
  (type a)
  (module PrintableInstance : Printable with type t = a)
  (x : a)
  : unit =
  print_endline (PrintableInstance.to_string x)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The module&amp;nbsp;&lt;code&gt;Printable&lt;/code&gt; encapsulates both a type&amp;nbsp;&lt;code&gt;t&lt;/code&gt; and some associated data&amp;nbsp;&lt;code&gt;to_string&lt;/code&gt;. We model that with a dependent record in&amp;nbsp;Coq, that is to say a record mixing types and values:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module Printable.
  Record signature {t : Type} := {
    t := t;
    to_string : t -&amp;gt; string;
  }.
  Arguments signature : clear implicits.
End Printable.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since the type&amp;nbsp;&lt;code&gt;t&lt;/code&gt; is not known at this point, we model it with a type parameter&amp;nbsp;&lt;code&gt;t&lt;/code&gt;. Later on, when someone uses the syntax&amp;nbsp;&lt;code&gt;with type t = ...&lt;/code&gt;, we call the&amp;nbsp;&lt;code&gt;signature&lt;/code&gt; with the value for&amp;nbsp;&lt;code&gt;t&lt;/code&gt;. We define a synonym field &amp;nbsp;&lt;code&gt;t := t&lt;/code&gt; to have a uniform way to access to the fields of the module. We put the whole record into a module&amp;nbsp;&lt;code&gt;Printable&lt;/code&gt; to namespace the projections and prevent name collisions.&lt;/p&gt;

&lt;p&gt;We generate the following Coq code for the&amp;nbsp;&lt;code&gt;print_on_the_terminal&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition print_on_the_terminal {A : Type}
  (PrintableInstance : {_ : unit &amp;amp; Printable.signature A}) : A -&amp;gt; unit :=
  let PrintableInstance := projT2 PrintableInstance in
  fun x =&amp;gt;
    print_endline (PrintableInstance.(Printable.to_string) x).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The&amp;nbsp;&lt;code&gt;print_endline&lt;/code&gt; function is an axiom, as side-effects are forbidden in&amp;nbsp;Coq. The curly braces&amp;nbsp;&lt;code&gt;{}&lt;/code&gt; set implicit the type parameter&amp;nbsp;&lt;code&gt;A&lt;/code&gt;, because type variables are implicit in OCaml. The&amp;nbsp;&lt;code&gt;PrintableInstance&lt;/code&gt; value is a dependent pair, since it may contain a list of type values for the abstract types of the module. Here the only abstract type&amp;nbsp;&lt;code&gt;t&lt;/code&gt; is already filled with a value&amp;nbsp;&lt;code&gt;A&lt;/code&gt;, thus there are no type values&amp;nbsp;(&lt;code&gt;_ : unit&lt;/code&gt;). We open the dependent pair with&amp;nbsp;&lt;code&gt;projT2&lt;/code&gt;. We use the projection&amp;nbsp;&lt;code&gt;Printable.to_string&lt;/code&gt; to access to the&amp;nbsp;&lt;code&gt;to_string&lt;/code&gt; function of the module.&lt;/p&gt;

&lt;p&gt;We often use types with a first-class module in a boxed form, where we associate both a module type and its value:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module type BoxedPrintable = sig
  module Printable : Printable
  val value : Printable.t
end

let print_boxed_printable (module BoxedPrintable : BoxedPrintable) : unit =
  print_endline (BoxedPrintable.Printable.to_string BoxedPrintable.value)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In&amp;nbsp;Coq, we proceed as for the previous module and propagate the abstract type&amp;nbsp;&lt;code&gt;t&lt;/code&gt; of the sub-module&amp;nbsp;&lt;code&gt;Printable&lt;/code&gt; to the signature of&amp;nbsp;&lt;code&gt;BoxedPrintable&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module BoxedPrintable.
  Record signature {Printable_t : Type} := {
    Printable : Printable.signature Printable_t;
    value : Printable.(Printable.t);
  }.
  Arguments signature : clear implicits.
End BoxedPrintable.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the function&amp;nbsp;&lt;code&gt;print_boxed_printable&lt;/code&gt;, we use a parameter&amp;nbsp;&lt;code&gt;Printable_t&lt;/code&gt; in the dependent pair&amp;nbsp;&lt;code&gt;BoxedPrintable&lt;/code&gt; as the abstract type&amp;nbsp;&lt;code&gt;t&lt;/code&gt; is not known at this point. More generally, the abstract types are universally quantified in the definition of signatures and existentially quantified in values.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition print_boxed_printable
  (BoxedPrintable : {Printable_t : _ &amp;amp; BoxedPrintable.signature Printable_t})
  : unit :=
  let BoxedPrintable := projT2 BoxedPrintable in
  print_endline
    (BoxedPrintable.(BoxedPrintable.Printable).(Printable.to_string)
      BoxedPrintable.(BoxedPrintable.value)).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the path to access the&amp;nbsp;&lt;code&gt;to_string&lt;/code&gt; function in&amp;nbsp;Coq is more verbose and more explicit than in&amp;nbsp;OCaml.&lt;/p&gt;

&lt;h2&gt;What we support&lt;/h2&gt;

&lt;p&gt;We support first-class modules with values, abstract types and type synonyms. We do not support first-class modules with other kinds of fields, such as the definition of new algebraic data types. We do not support functors (although we support first-class functions on first-class modules).&lt;/p&gt;

&lt;p&gt;A difficulty is to be able to distinguish between first-class modules and plain modules. This is necessary because we import first-class modules to dependent records and plain modules to&amp;nbsp;Coq modules. For example, for projections, the syntax in&amp;nbsp;OCaml is the same in both cases but different in&amp;nbsp;Coq. When accessing a field of a module, we consider the module to be first-class if there exists a signature of the same shape. Once we found the name of the signature, we generate a call to the corresponding projection in&amp;nbsp;Coq. If there are more than one signature corresponding to a module we generate an error. This can be the case because&amp;nbsp;OCaml modules are not generative by default (we can make generative&amp;nbsp;OCaml modules by&amp;nbsp;&lt;a href=&quot;https://softwareengineering.stackexchange.com/questions/326304/what-is-the-difference-between-applicative-and-generative-modules-and-type-clas&quot;&gt;adding a&amp;nbsp;&lt;code&gt;()&lt;/code&gt; parameter&lt;/a&gt;). Our strategy to decide if a module is first-class is a heuristic, we may reconsider it later.&lt;/p&gt;

&lt;h2&gt;Set example&lt;/h2&gt;

&lt;p&gt;Here is the definition of sets as first-class modules, extracted from the&amp;nbsp;&lt;a href=&quot;https://gitlab.com/tezos/tezos/&quot;&gt;Tezos source code&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module S = struct
  module type SET = sig
    type elt
    type t
    val empty: t
    val is_empty: t -&amp;gt; bool
    val mem: elt -&amp;gt; t -&amp;gt; bool
    val add: elt -&amp;gt; t -&amp;gt; t
    val remove: elt -&amp;gt; t -&amp;gt; t
  end
end

type &amp;#39;a comparable_ty

module type Boxed_set = sig
  type elt
  val elt_ty : elt comparable_ty
  module OPS : S.SET with type elt = elt
  val boxed : OPS.t
  val size : int
end

type &amp;#39;elt set = (module Boxed_set with type elt = &amp;#39;elt)

let set_update
  : type a. a -&amp;gt; bool -&amp;gt; a set -&amp;gt; a set
  = fun v b (module Box) -&amp;gt;
  (module struct
    type elt = a
    let elt_ty = Box.elt_ty
    module OPS = Box.OPS
    let boxed =
      if b
      then Box.OPS.add v Box.boxed
      else Box.OPS.remove v Box.boxed
    let size =
      let mem = Box.OPS.mem v Box.boxed in
    if mem
    then if b then Box.size else Box.size - 1
    else if b then Box.size + 1 else Box.size
  end)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We successfully generate the following valid&amp;nbsp;Coq code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module S.
  Module SET.
    Record signature {elt t : Type} := {
      elt := elt;
      t := t;
      empty : t;
      is_empty : t -&amp;gt; bool;
      mem : elt -&amp;gt; t -&amp;gt; bool;
      add : elt -&amp;gt; t -&amp;gt; t;
      remove : elt -&amp;gt; t -&amp;gt; t;
    }.
    Arguments signature : clear implicits.
  End SET.
End S.

Parameter comparable_ty : forall (a : Type), Type.

Module Boxed_set.
  Record signature {elt OPS_t : Type} := {
    elt := elt;
    elt_ty : comparable_ty elt;
    OPS : S.SET.signature elt OPS_t;
    boxed : OPS.(S.SET.t);
    size : Z;
  }.
  Arguments signature : clear implicits.
End Boxed_set.

Definition set (elt : Type) := {OPS_t : _ &amp;amp; Boxed_set.signature elt OPS_t}.

Definition set_update {a : Type} (v : a) (b : bool) (Box : set a) : set a :=
  let Box := projT2 Box in
  existT _ _
    {|
      Boxed_set.elt_ty := Box.(Boxed_set.elt_ty);
      Boxed_set.OPS := Box.(Boxed_set.OPS);
      Boxed_set.boxed :=
        if b then
          Box.(Boxed_set.OPS).(S.SET.add) v Box.(Boxed_set.boxed)
        else
          Box.(Boxed_set.OPS).(S.SET.remove) v Box.(Boxed_set.boxed);
      Boxed_set.size :=
        let mem := Box.(Boxed_set.OPS).(S.SET.mem) v Box.(Boxed_set.boxed) in
        if mem then
          if b then
            Box.(Boxed_set.size)
          else
            Z.sub Box.(Boxed_set.size) 1
        else
          if b then
            Z.add Box.(Boxed_set.size) 1
          else
            Box.(Boxed_set.size)
      |}.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We use&amp;nbsp;&lt;code&gt;existT _ _&lt;/code&gt; to instantiate a dependent pair. We rely on the inference mechanism of&amp;nbsp;Coq to fill the existential type variable&amp;nbsp;&lt;code&gt;OPS_t&lt;/code&gt; in this pair.&lt;/p&gt;

&lt;h2&gt;Future work and opinions&lt;/h2&gt;

&lt;p&gt;Things are not perfect yet. We still need to test the implementation more, debug, and add some features such as polymorphic abstract types.&lt;/p&gt;

&lt;p&gt;As a matter of taste, we prefer to import&amp;nbsp;OCaml code to dependent records rather than functors. Indeed, we believe that records are safer than functors. The implementation of functors in the&amp;nbsp;Coq kernel is complex as we have heard, while dependent records are already given by the dependent types. Moreover,&amp;nbsp;Coq functors are generative while&amp;nbsp;OCaml ones are not. We hope that we will not need&amp;nbsp;Coq functors to import the code we wish to verify.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/first-class-modules-in-coq-of-ocaml.html</link>
        <guid>https://rocq-blog.clarus.me/first-class-modules-in-coq-of-ocaml.html</guid>
      </item>
    
      <item>
        <title>Importing mutually recursive types from OCaml to Coq</title>
        <pubDate>Sat, 28 Sep 2019 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;In order to make&amp;nbsp;&lt;a href=&quot;https://github.com/clarus/coq-of-ocaml&quot;&gt;coq-of-ocaml&lt;/a&gt; usable on a maximum of&amp;nbsp;&lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; programs, we should handle mutually recursive types. We show how we import these types to&amp;nbsp;&lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; and the main differences between the two languages. As a result, more&amp;nbsp;OCaml programming patterns should be supported by&amp;nbsp;coq-of-ocaml.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;Take the following mutually recursive definition of a tree in&amp;nbsp;OCaml:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type &amp;#39;a tree = Tree of &amp;#39;a node list

and &amp;#39;a node =
  | Leaf of &amp;#39;a leaf
  | Node of &amp;#39;a tree

and &amp;#39;content leaf = string * &amp;#39;content
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By applying&amp;nbsp;&lt;a href=&quot;https://github.com/clarus/coq-of-ocaml&quot;&gt;coq-of-ocaml&lt;/a&gt; on this code we get:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Reserved Notation &amp;quot;&amp;#39;leaf&amp;quot;.

Inductive tree (a : Type) : Type :=
| Tree : (list (node a)) -&amp;gt; tree a

with node (a : Type) : Type :=
| Leaf : (&amp;#39;leaf a) -&amp;gt; node a
| Node : (tree a) -&amp;gt; node a

where &amp;quot;&amp;#39;leaf&amp;quot; := (fun (content : Type) =&amp;gt; string * content).

Definition leaf := &amp;#39;leaf.

Arguments Tree {_}.
Arguments Leaf {_}.
Arguments Node {_}.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We transform algebraic data types to&amp;nbsp;Coq&amp;rsquo;s inductive types. We use a notation to implement the type synonym&amp;nbsp;&lt;code&gt;leaf&lt;/code&gt;. This is a trick because Coq only supports inductive types in mutual definitions. See the &lt;a href=&quot;https://coq.inria.fr/refman/user-extensions/syntax-extensions.html?highlight=notation#reserving-notations&quot;&gt;documentation on notations&lt;/a&gt; for more information. We rename the&amp;nbsp;&lt;code&gt;leaf&lt;/code&gt; type parameter&amp;nbsp;&lt;code&gt;&amp;#39;content&lt;/code&gt; instead of&amp;nbsp;&lt;code&gt;&amp;#39;a&lt;/code&gt; to avoid a name collision in&amp;nbsp;Coq. The type parameters of the constructors are set implicit with the command&amp;nbsp;&lt;code&gt;Arguments&lt;/code&gt; to keep the behavior of&amp;nbsp;OCaml.&lt;/p&gt;

&lt;h2&gt;General mechanism&lt;/h2&gt;

&lt;p&gt;We handle&amp;nbsp;&lt;code&gt;type ... and ...&lt;/code&gt; definitions with algebraic data types (including&amp;nbsp;GADTs, &amp;ldquo;Generalized Algebraic Data Types&amp;rdquo;) and type synonyms. We do not handle abstract types. For records in mutual definitions, one can use a type synonym to a more generic record. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type expression =
  | Number of int
  | Operation of operation

and operation = {
  name : string;
  parameters : expression list }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;can be rewritten as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type &amp;#39;a operation_skeleton = {
  name : string;
  parameters : &amp;#39;a list }

type expression =
  | Number of int
  | Operation of operation

and operation = expression operation_skeleton
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in order to be imported into&amp;nbsp;Coq.&lt;/p&gt;

&lt;p&gt;In&amp;nbsp;Coq, there is a distinction between type variables on the left (type parameters) and on the right of the&amp;nbsp;&lt;code&gt;:&lt;/code&gt; (type indices):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive t (A1 A2 ... : Type) : forall (B1 B2 ... : Type), Type :=
| Constr1 : ... -&amp;gt; t A1 A2 ... C1 C2 ...
| ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The variables&amp;nbsp;&lt;code&gt;Ai&lt;/code&gt; do not behave the same as the variables&amp;nbsp;&lt;code&gt;Bi&lt;/code&gt;. Type parameters have the constraint of being the same for each constructor. When used, they simplify the typing of the pattern matching. Typically, in&amp;nbsp;GADTs, type variables are type indices while in non-generalized algebraic data types they are type parameters. In mutually recursive inductive types there is one more constraint: the type parameters must be the same for each type. We consider a type variable to be a parameter if it appears with the same name in each&amp;nbsp;OCaml constructor and in the type definition. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type (&amp;#39;a, &amp;#39;b) arith =
  | Int : &amp;#39;a * int -&amp;gt; (&amp;#39;a, int) arith
  | Eq : &amp;#39;a * (&amp;#39;a, int) arith * (&amp;#39;a, int) arith -&amp;gt; (&amp;#39;a, bool) arith
  | Plus : &amp;#39;a * (&amp;#39;a, int) arith * (&amp;#39;a, int) arith -&amp;gt; (&amp;#39;a, int) arith
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is imported to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive arith (a : Type) : forall (b : Type), Type :=
| Int : a -&amp;gt; Z -&amp;gt; arith a Z
| Eq : a -&amp;gt; (arith a Z) -&amp;gt; (arith a Z) -&amp;gt; arith a bool
| Plus : a -&amp;gt; (arith a Z) -&amp;gt; (arith a Z) -&amp;gt; arith a Z.

Arguments Int {_}.
Arguments Eq {_}.
Arguments Plus {_}.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we transform &lt;code&gt;&amp;#39;a&lt;/code&gt; to a type parameter and &lt;code&gt;&amp;#39;b&lt;/code&gt; to a type index.&lt;/p&gt;

&lt;h2&gt;Limitations&lt;/h2&gt;

&lt;p&gt;Many&amp;nbsp;OCaml programs define all types as mutually recursive by default. In&amp;nbsp;Coq this is usually very difficult as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;only algebraic and synonym types can be mutual;&lt;/li&gt;
&lt;li&gt;all type parameters must be the same;&lt;/li&gt;
&lt;li&gt;the &amp;ldquo;strictly positive&amp;rdquo; constraint in&amp;nbsp;Coq prevents some constructions (such as having a type as a type parameter for another);&lt;/li&gt;
&lt;li&gt;the proofs or the definition of recursive functions on mutually recursive types is more complicated than with simple recursive types.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In practice, I would recommend to find a way to avoid mutually recursive types when possible. For cases they are used, I hope this import mechanism to be useful.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/importing-mutually-recursive-types-from-ocaml-to-coq.html</link>
        <guid>https://rocq-blog.clarus.me/importing-mutually-recursive-types-from-ocaml-to-coq.html</guid>
      </item>
    
      <item>
        <title>Multiple error messages in coq-of-ocaml</title>
        <pubDate>Fri, 13 Sep 2019 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;The &lt;a href=&quot;https://github.com/clarus/coq-of-ocaml&quot;&gt;coq-of-ocaml&lt;/a&gt; compiler transforms &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; programs to &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; ones. There usually are many errors in each file to import (the Coq language tends to be stricter than OCaml and we do not want to import code with too much encoding). Fixing errors may be the most time consuming part of an import. We present a system to display all the errors at once instead of one by one. We believe that it helps to get a quick idea of how difficult a file is to translate, while having a sense of progress when fixing the errors.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We develop &lt;a href=&quot;(https://clarus.github.io/coq-of-ocaml/)&quot;&gt;coq-of-ocaml&lt;/a&gt; at &lt;a href=&quot;https://www.nomadic-labs.com/&quot;&gt;🐙&amp;nbsp;Nomadic&amp;nbsp;Labs&lt;/a&gt; with the aim to formally verify OCaml programs, and in particular the implementation of the crypto-currency &lt;a href=&quot;https://tezos.com/&quot;&gt;Tezos&lt;/a&gt;. If you want to use this tool for your own projects, please do not hesitate to look at our &lt;a href=&quot;https://clarus.github.io/coq-of-ocaml/&quot;&gt;website&lt;/a&gt; or &lt;a href=&quot;mailto:contact@nomadic-labs.com&quot;&gt;contact us&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;static/images/coq-of-ocaml-multiple-errors/report.png&quot; alt=&quot;Multiple errors report&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Multiple errors report&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;Take this perfectly valid OCaml program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let foo x =
  if x then
    assert false (* no assert in Coq *)
  else begin
    print_endline &amp;quot;bar&amp;quot;; (* no sequencing of side-effects in Coq *)
    true
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We would like to report that both the &lt;code&gt;assert&lt;/code&gt; and the sequencing of effects with &lt;code&gt;;&lt;/code&gt; are not available in Coq.&lt;/p&gt;

&lt;p&gt;To import programs, &lt;code&gt;coq-of-ocaml&lt;/code&gt; runs a single pass on the OCaml&amp;rsquo;s typed abstract syntax tree. During the recursive exploration of the syntax tree, we would like to explore both branches of the &lt;code&gt;if&lt;/code&gt; to report both errors. We need a way to accumulate errors along the way and not block at the first mistake. Since &lt;code&gt;coq-of-ocaml&lt;/code&gt; has a single pass, we hope to get most of the errors doing so.&lt;/p&gt;

&lt;h2&gt;An error monad&lt;/h2&gt;

&lt;p&gt;To encapsulate the error handling mechanism, we define the following &lt;a href=&quot;https://stackoverflow.com/a/13388966/3873794&quot;&gt;free monad&lt;/a&gt; in OCaml (&lt;code&gt;coq-of-ocaml&lt;/code&gt; is implemented in OCaml):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module Command = struct
  type &amp;#39;a t =
    | GetEnv : Env.t t
    | Raise : Error.Category.t * string -&amp;gt; &amp;#39;a t
end

module Wrapper = struct
  type t =
    | SetEnv of Env.t
    | SetLoc of Loc.t
end

type &amp;#39;a t =
  | All : &amp;#39;a t * &amp;#39;b t -&amp;gt; (&amp;#39;a * &amp;#39;b) t
  | Bind : &amp;#39;b t * (&amp;#39;b -&amp;gt; &amp;#39;a t) -&amp;gt; &amp;#39;a t
  | Command of &amp;#39;a Command.t
  | Return of &amp;#39;a
  | Wrapper of Wrapper.t * &amp;#39;a t
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are three main constructs to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;All&lt;/code&gt; which combines the results of two computations, both of which may fail. It allows to accumulate errors in each branch of the syntax tree;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Command&lt;/code&gt;, especially the case &lt;code&gt;Raise&lt;/code&gt; to create an error at the current code location;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Wrapper&lt;/code&gt;, especially the case &lt;code&gt;SetLoc&lt;/code&gt; to set the current code location in (and only in) the following computation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also have primitives &lt;code&gt;GetEnv&lt;/code&gt; and &lt;code&gt;SetEnv&lt;/code&gt; to manipulate the current OCaml environment from the AST. The &lt;code&gt;Bind&lt;/code&gt; and &lt;code&gt;Return&lt;/code&gt; are the standard monadic primitives. We chose a free-monad in order to isolate the definition of side-effects and, one day, import &lt;code&gt;coq-of-ocaml&lt;/code&gt; to Coq. &lt;/p&gt;

&lt;p&gt;We rewrote &lt;code&gt;coq-of-ocaml&lt;/code&gt; using the &lt;code&gt;All&lt;/code&gt; primitive as much as possible (typically when two computations were not depending on each other). Since the current OCaml environment and location are handled by the monad, we cleaned the code to propagate their values. Having done that, retrieving the complete list of errors was mostly given for free.&lt;/p&gt;

&lt;h2&gt;A nice output&lt;/h2&gt;

&lt;p&gt;In order to present a nice output to the user, we decided to do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add colors;&lt;/li&gt;
&lt;li&gt;show the code extract where the error comes from, using a presentation similar to the JavaScript&amp;rsquo;s &lt;a href=&quot;https://babeljs.io/docs/en/next/babel-code-frame.html&quot;&gt;@babel/code-frame&lt;/a&gt; (for now an error location is a line; we plan to precise the column and add multiline support);&lt;/li&gt;
&lt;li&gt;clearly separate errors with a long line and some spaces;&lt;/li&gt;
&lt;li&gt;add an error category (side-effect, dependency not found, &amp;hellip;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of these changes were inspired by the &lt;a href=&quot;https://elm-lang.org/&quot;&gt;Elm&lt;/a&gt; blog post on producing &lt;a href=&quot;https://elm-lang.org/news/compiler-errors-for-humans&quot;&gt;Compiler Errors for Humans&lt;/a&gt;.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/multiple-error-messages-in-coq-of-ocaml.html</link>
        <guid>https://rocq-blog.clarus.me/multiple-error-messages-in-coq-of-ocaml.html</guid>
      </item>
    
      <item>
        <title>Fixing flaky makefiles in opam Coq packages</title>
        <pubDate>Wed, 04 Sep 2019 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;In &lt;a href=&quot;https://opam.ocaml.org/&quot;&gt;opam&lt;/a&gt;, some packages fail to install &lt;em&gt;sometimes&lt;/em&gt;. This is a problem because these bugs are hard to reproduce. Yet the user may get the impression that some packages are broken. We show how we listed and then corrected these bugs. The &lt;a href=&quot;https://github.com/coq/opam-coq-archive&quot;&gt;opam Coq repository&lt;/a&gt; should now be more stable.&lt;/p&gt;

&lt;h2&gt;Round 1&lt;/h2&gt;

&lt;p&gt;At first we (I) did not know how to fix these installation bugs. They would typically result in OCaml errors such as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;The following actions will be performed:
  - install coq-rational 8.6.0
[...]
- File &amp;quot;Rewrite/LeibnizRewrite/AC/aC.ml&amp;quot;, line 1:
- Error: Corrupted compiled interface
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in &lt;code&gt;coq-rational.8.6.0&lt;/code&gt;. As we ran opam with the option &lt;code&gt;-j1&lt;/code&gt; (one parallel build), this did not seem like a concurrency bug. We decided to add a &lt;a href=&quot;https://github.com/coq-bench/make-html/blob/master/black_list.rb&quot;&gt;black-list file&lt;/a&gt; to the &lt;a href=&quot;https://coq-bench.github.io/&quot;&gt;Coq opam bench&lt;/a&gt; to list out and hide these bugs. This was not a perfect solution, but at least it did not flood the results with non-reproducible errors.&lt;/p&gt;

&lt;h2&gt;Round 2&lt;/h2&gt;

&lt;p&gt;As noticed by &lt;a href=&quot;https://setoid.com/&quot;&gt;Karl Palmskog&lt;/a&gt;, the opam builds were running in parallel despite the &lt;code&gt;-j1&lt;/code&gt; option. As a proof, the presence in the installation traces of:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;make[2]: *** Waiting for unfinished jobs....
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This was due to a bug in opam, &lt;a href=&quot;https://github.com/ocaml/opam/blob/2.0.5/CHANGES#L10&quot;&gt;corrected in version &lt;code&gt;2.0.5&lt;/code&gt;&lt;/a&gt;. Thus the fix was just to replace:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[make &amp;quot;-j%{jobs}%&amp;quot;]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;by:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[make]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in each package definition with parallel build issues.&lt;/p&gt;

&lt;p&gt;These errors occurred in most of the packages using &lt;a href=&quot;https://coq.inria.fr/refman/practical-tools/utilities.html#building-a-coq-project-with-coq-makefile&quot;&gt;coq_makefile&lt;/a&gt; and mixing Coq and OCaml code. The versions &lt;code&gt;4.02&lt;/code&gt; and &lt;code&gt;4.05&lt;/code&gt; of OCaml were impacted, but the version &lt;code&gt;4.07&lt;/code&gt; seems free of bugs. The most frequent error message is &lt;code&gt;Corrupted compiled interface&lt;/code&gt;. Apparently this happens when both &lt;code&gt;byte&lt;/code&gt; and &lt;code&gt;opt&lt;/code&gt; compilations run in parallel, modifying &lt;code&gt;.cmi&lt;/code&gt; files at the same time. The &lt;a href=&quot;https://dune.build/&quot;&gt;Dune&lt;/a&gt; build system may be a more robust alternative to &lt;code&gt;coq_makefile&lt;/code&gt;, but I have no data about it.&lt;/p&gt;

&lt;h2&gt;Result&lt;/h2&gt;

&lt;p&gt;Most of the Coq packages with flaky makefiles are now corrected, and the respective maintainers contacted. We will continue to fix these bugs as they occur.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/fixing-flaky-makefiles-in-opam-coq-packages.html</link>
        <guid>https://rocq-blog.clarus.me/fixing-flaky-makefiles-in-opam-coq-packages.html</guid>
      </item>
    
      <item>
        <title>Connecting the opam bench to Gitter</title>
        <pubDate>Tue, 03 Sep 2019 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;The &lt;a href=&quot;https://coq-bench.github.io/&quot;&gt;Coq opam bench system&lt;/a&gt; detects bugs in &lt;a href=&quot;https://opam.ocaml.org/&quot;&gt;opam&lt;/a&gt; packages from the &lt;a href=&quot;https://github.com/coq/opam-coq-archive&quot;&gt;Coq repository&lt;/a&gt;. However these bugs may be hard to follow. Indeed, they are in a table one needs to consult regularly. We present a system to post results in a &lt;a href=&quot;https://gitter.im/&quot;&gt;Gitter&lt;/a&gt; channel &lt;a href=&quot;https://gitter.im/coq/opam-bench-reports&quot;&gt;coq/opam-bench-reports&lt;/a&gt;. Thanks to this channel, we can more easily track down the latest build failures.&lt;/p&gt;

&lt;p&gt;The idea of publishing results in Gitter was given by &lt;a href=&quot;https://www.cri.ensmp.fr/people/gallego/&quot;&gt;Emilio Jesús Gallego Arias&lt;/a&gt; in &lt;a href=&quot;https://github.com/coq/coq/issues/10418&quot;&gt;coq/issues/10418&lt;/a&gt;. Improvements were added from suggestions from Emilio Jesús Gallego Arias and &lt;a href=&quot;https://setoid.com/&quot;&gt;Karl Palmskog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;static/images/opam-bench-gitter/report.png&quot; alt=&quot;Bench report in Gitter&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Bench report in Gitter&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;h2&gt;Implementation&lt;/h2&gt;

&lt;p&gt;The code to publish results in Gitter is at &lt;a href=&quot;https://github.com/coq-bench/make-html&quot;&gt;github.com/coq-bench/make-html&lt;/a&gt;. The implementation is in &lt;a href=&quot;https://www.ruby-lang.org/&quot;&gt;Ruby&lt;/a&gt; and shares code with the bench website generator. We extract the results from a CSV database generated by the project &lt;a href=&quot;https://github.com/coq-bench/run&quot;&gt;github.com/coq-bench/run&lt;/a&gt; which tests the packages.&lt;/p&gt;

&lt;p&gt;To connect to Gitter, we use the &lt;a href=&quot;https://github.com/kristenmills/ruby-gitter&quot;&gt;ruby-gitter&lt;/a&gt; package. The code goes as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Use the developer token from the Gitter documentation
# (what is possible as very few messages are sent)
client = Gitter::Client.new(token)

# Find the room id
room = client.rooms.find {|room| room.name == &amp;quot;coq/opam-bench-reports&amp;quot;}
room_id = room.id

# Construct the message with the results
message = &amp;quot;...&amp;quot;

# Actually send the message
client.send_message(message, room_id)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We use a dedicated &lt;a href=&quot;https://github.com/coq-opam-bench-gitter-bot&quot;&gt;coq-opam-bench-gitter-bot&lt;/a&gt; user to publish on Gitter and sign the messages. We retrieve the results of the past 72 hours, and run the code every 72 hours with a shell command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;while true; do sleep 72h; date; git pull; ruby push_to_gitter.rb ../database gitter-token coq/opam-bench-reports 72; done
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the &lt;code&gt;git pull&lt;/code&gt; of hot code swapping! This is useful to integrate small bench updates without breaking the 72 hours rythm.&lt;/p&gt;

&lt;p&gt;We either display the list of packages which failed, or a success message.&lt;/p&gt;

&lt;h2&gt;Results&lt;/h2&gt;

&lt;p&gt;We went from dozens of errors every 72 hours (when setting up the system) to just a few these days. Probably, this was mainly due to the bug corrections themselves but the Gitter reports helped. The Gitter reports also created a channel to talk about opam bugs.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/connecting-the-opam-bench-to-gitter.html</link>
        <guid>https://rocq-blog.clarus.me/connecting-the-opam-bench-to-gitter.html</guid>
      </item>
    
      <item>
        <title>Continuous testing for Coq projects</title>
        <pubDate>Thu, 25 Jul 2019 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;Testing that a&amp;nbsp;&lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; project works with different&amp;nbsp;Coq versions may be time consuming. This is necessary to deliver with confidence new project releases.&amp;nbsp;&lt;a href=&quot;https://travis-ci.com/&quot;&gt;Travis&amp;nbsp;CI&lt;/a&gt; is a service to run tests on&amp;nbsp;&lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt; projects and is free for open-source code. We show a setup to run automated testing with&amp;nbsp;Travis&amp;nbsp;CI using pre-compiled&amp;nbsp;Coq instances and &lt;a href=&quot;https://opam.ocaml.org/&quot;&gt;opam&lt;/a&gt;. Thanks to that setup, we can automatically and quickly check new pull-requests and commits.&lt;/p&gt;

&lt;p&gt;This configuration is directly inspired by the &lt;a href=&quot;https://github.com/coq-community/docker-coq/wiki/CI-setup&quot;&gt;CI setup&lt;/a&gt; documentation written by &lt;a href=&quot;https://github.com/erikmd&quot;&gt;Erik Martin-Dorel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;static/images/travis-ci/build-report.png&quot; alt=&quot;Travis CI report on a pull-request&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Travis CI report on a pull-request&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;h2&gt;Setting up Travis CI&lt;/h2&gt;

&lt;p&gt;To show the setup&amp;nbsp;&lt;a href=&quot;https://travis-ci.com/&quot;&gt;Travis&amp;nbsp;CI&lt;/a&gt;, we take the example of the &lt;a href=&quot;https://github.com/coq-io/system&quot;&gt;github.com/coq-io/system&lt;/a&gt; project. We need to create two files at the root of the project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;coq-io-system.opam&lt;/code&gt; to describe the dependencies;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.travis.yml&lt;/code&gt; to configure and activate&amp;nbsp;Travis&amp;nbsp;CI.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Describing the dependencies&lt;/h2&gt;

&lt;p&gt;We create an&amp;nbsp;&lt;a href=&quot;https://opam.ocaml.org/&quot;&gt;opam&lt;/a&gt; file&amp;nbsp;&lt;code&gt;coq-io-system.opam&lt;/code&gt; with the following content:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;version: &amp;quot;dev&amp;quot;

opam-version: &amp;quot;2.0&amp;quot;
maintainer: &amp;quot;dev@clarus.me&amp;quot;
homepage: &amp;quot;https://github.com/clarus/io-system&amp;quot;
dev-repo: &amp;quot;git+https://github.com/clarus/io-system.git&amp;quot;
bug-reports: &amp;quot;https://github.com/clarus/io-system/issues&amp;quot;
authors: [&amp;quot;Guillaume Claret&amp;quot;]
license: &amp;quot;MIT&amp;quot;
build: [
  [&amp;quot;./configure.sh&amp;quot;]
  [make &amp;quot;-j%{jobs}%&amp;quot;]
]
install: [
  [make &amp;quot;install&amp;quot;]
]
depends: [
  &amp;quot;ocaml&amp;quot;
  &amp;quot;coq&amp;quot; {&amp;gt;= &amp;quot;8.5&amp;quot;}
  &amp;quot;coq-function-ninjas&amp;quot;
  &amp;quot;coq-list-string&amp;quot; {&amp;gt;= &amp;quot;2.0.0&amp;quot;}
  &amp;quot;coq-io&amp;quot; {&amp;gt;= &amp;quot;4.0.0&amp;quot;}
  &amp;quot;coq-io-system-ocaml&amp;quot; {&amp;gt;= &amp;quot;2.3.0&amp;quot;}
]
tags: [
  &amp;quot;keyword:effects&amp;quot;
  &amp;quot;keyword:extraction&amp;quot;
  &amp;quot;logpath:Io/System&amp;quot;
]
synopsis: &amp;quot;System effects for Coq&amp;quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This file describes the dependencies of the project. It can list any other opam packages published on the &lt;a href=&quot;https://opam.ocaml.org/&quot;&gt;OCaml repository&lt;/a&gt; or the &lt;a href=&quot;https://github.com/coq/opam-coq-archive&quot;&gt;Coq repository&lt;/a&gt;. We also give the build and install commands. To check that this file is correct:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# linting to check for typos
opam lint coq-io-system.opam
# adding the Coq repository if not already done
opam repo add coq-released https://coq.inria.fr/opam/released
# installing the package
opam pin add coq-io-system . --kind=path -y
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Later on, you may want to publish your package in opam. You can just take this file content and create a pull-request on the &lt;a href=&quot;https://github.com/coq/opam-coq-archive&quot;&gt;Coq repository&lt;/a&gt;. Since this opam file is used for continuous testing, you can be pretty confident that your package is correct.&lt;/p&gt;

&lt;h2&gt;Configuring Travis CI&lt;/h2&gt;

&lt;p&gt;We add a&amp;nbsp;&lt;a href=&quot;https://travis-ci.com/&quot;&gt;Travis&amp;nbsp;CI&lt;/a&gt; file&amp;nbsp;&lt;code&gt;.travis.yml&lt;/code&gt; with the following content:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;dist: bionic
language: generic

services:
  - docker

env:
  global:
  - PACKAGE_NAME=&amp;quot;coq-io-system&amp;quot;
  matrix:
  - COQ_IMAGE=&amp;quot;coqorg/coq:8.4&amp;quot;  SHOULD_SUPPORT=&amp;quot;false&amp;quot;
  - COQ_IMAGE=&amp;quot;coqorg/coq:8.5&amp;quot;  SHOULD_SUPPORT=&amp;quot;true&amp;quot;
  - COQ_IMAGE=&amp;quot;coqorg/coq:8.6&amp;quot;  SHOULD_SUPPORT=&amp;quot;true&amp;quot;
  - COQ_IMAGE=&amp;quot;coqorg/coq:8.7&amp;quot;  SHOULD_SUPPORT=&amp;quot;true&amp;quot;
  - COQ_IMAGE=&amp;quot;coqorg/coq:8.8&amp;quot;  SHOULD_SUPPORT=&amp;quot;true&amp;quot;
  - COQ_IMAGE=&amp;quot;coqorg/coq:8.9&amp;quot;  SHOULD_SUPPORT=&amp;quot;true&amp;quot;
  - COQ_IMAGE=&amp;quot;coqorg/coq:8.10&amp;quot; SHOULD_SUPPORT=&amp;quot;true&amp;quot;

install: |
  # Prepare the COQ container
  docker pull ${COQ_IMAGE}
  docker run -d -i --init --name=COQ -v ${TRAVIS_BUILD_DIR}:/home/project -w /home/project ${COQ_IMAGE}
  docker exec COQ /bin/bash --login -c &amp;quot;
    # This bash script is double-quoted to interpolate Travis CI env vars:
    echo \&amp;quot;Build triggered by ${TRAVIS_EVENT_TYPE}\&amp;quot;
    export PS4=&amp;#39;+ \e[33;1m(\$0 @ line \$LINENO) \$\e[0m &amp;#39;
    set -ex  # -e = exit on failure; -x = trace for debug
    opam update
    opam pin add ${PACKAGE_NAME} . --kind=path --no-action -y
    opam config list; opam repo list; opam pin list; opam list
    &amp;quot; install

script:
- echo -e &amp;quot;${ANSI_YELLOW}Building...${ANSI_RESET}&amp;quot; &amp;amp;&amp;amp; echo -en &amp;#39;travis_fold:start:script\\r&amp;#39;
- |
  docker exec COQ /bin/bash --login -c &amp;quot;
    export PS4=&amp;#39;+ \e[33;1m(\$0 @ line \$LINENO) \$\e[0m &amp;#39;
    set -ex
    sudo chown -R coq:coq /home/project
    # Check if the package is compatible with the current environment
    if [ &amp;quot;${SHOULD_SUPPORT}&amp;quot; = &amp;quot;true&amp;quot; ] || opam install ${PACKAGE_NAME} --show-action -y; then
      # First install the dependencies
      opam install ${PACKAGE_NAME} --deps-only -y
      opam list
      # Then install the package itself in verbose mode
      opam install ${PACKAGE_NAME} -v -y
    fi;
    &amp;quot; script
- echo -en &amp;#39;travis_fold:end:script\\r&amp;#39;

after_script:
- docker stop COQ  # optional
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This YAML file explains to&amp;nbsp;Travis&amp;nbsp;CI what to do to check the project. We use the &lt;a href=&quot;https://hub.docker.com/r/coqorg/coq&quot;&gt;coqorg/coq&lt;/a&gt; Docker images. These images have pre-compiled versions of Coq with opam, what is crucial to speedup the tests. For each architecture from Coq &lt;code&gt;8.4&lt;/code&gt; to Coq &lt;code&gt;8.9&lt;/code&gt; we:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;check if either:

&lt;ul&gt;
&lt;li&gt;the platform is supposed to be supported &lt;code&gt;if [ &amp;quot;${SHOULD_SUPPORT}&amp;quot; = &amp;quot;true&amp;quot; ]&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;or if opam considers the platform as compatible &lt;code&gt;opam install ${PACKAGE_NAME} --show-action -y&lt;/code&gt;;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;install the dependencies &lt;code&gt;opam install ${PACKAGE_NAME} --deps-only -y&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;install the project in verbose mode &lt;code&gt;opam install ${PACKAGE_NAME} -v -y&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Using Travis CI&lt;/h2&gt;

&lt;p&gt;You may need to activate Travis CI for your project in the &lt;a href=&quot;https://travis-ci.com/account/repositories&quot;&gt;settings page of&amp;nbsp;Travis&amp;nbsp;CI&lt;/a&gt;. Then for each pull-request or commit you will see a green or red mark precising if the project passed the tests, along with detailed logs when you click on it:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;static/images/travis-ci/pull-request.png&quot; alt=&quot;Commits in a pull-request&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Commits in a pull-request&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;static/images/travis-ci/error-logs.png&quot; alt=&quot;Error logs&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Error logs&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/continuous-testing-for-coq-projects.html</link>
        <guid>https://rocq-blog.clarus.me/continuous-testing-for-coq-projects.html</guid>
      </item>
    
      <item>
        <title>Approximating GADTs in Flow</title>
        <pubDate>Tue, 24 Jul 2018 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Generalized_algebraic_data_type#Higher-order_abstract_syntax&quot;&gt;GADTs (Generalized algebraic data types)&lt;/a&gt; are a generalization of sum types where the type parameter can change in each case. GADTs are not available in &lt;a href=&quot;https://flow.org/&quot;&gt;Flow&lt;/a&gt; but we show a technique to approximate them. We take inspiration from &lt;a href=&quot;http://code.slipthrough.net/2016/08/10/approximating-gadts-in-purescript/&quot;&gt;Approximating GADTs in PureScript&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Use case&lt;/h2&gt;

&lt;p&gt;Let us say we want to implement an evaluator for simple arithmetic expressions. We define the type of expressions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type Expr = {
  type: &amp;#39;I&amp;#39;,
  value: number
} | {
  type: &amp;#39;B&amp;#39;,
  value: boolean
} | {
  type: &amp;#39;Add&amp;#39;,
  x: Expr,
  y: Expr
} | {
  type: &amp;#39;Mul&amp;#39;,
  x: Expr,
  y: Expr
} | {
  type: &amp;#39;Eq&amp;#39;,
  x: Expr,
  y: Expr
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and an evaluation function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function evaluate(expr: Expr): boolean | number {
  switch (expr.type) {
    case &amp;#39;I&amp;#39;:
      return expr.value;
    case &amp;#39;B&amp;#39;:
      return expr.value;
    case &amp;#39;Add&amp;#39;:
      return evaluate(expr.x) + evaluate(expr.y);
    case &amp;#39;Mul&amp;#39;:
      return evaluate(expr.x) * evaluate(expr.y);
    case &amp;#39;Eq&amp;#39;:
      return evaluate(expr.x) === evaluate(expr.y);
    default:
      return expr;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but we get a Flow error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;32:  return evaluate(expr.x) * evaluate(expr.y);
            ^ Cannot perform arithmetic operation because boolean [1] is not a number.
References:
23: function evaluate(expr: Expr): boolean | number {
                                   ^ [1]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This error occurs because Flow does not know if we multiply booleans or numbers since the return type of &lt;code&gt;evaluate&lt;/code&gt; is &lt;code&gt;boolean | number&lt;/code&gt;. Flow is right to warn us because the type &lt;code&gt;Expr&lt;/code&gt; does not prevent to write expressions multiplying booleans:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const foo: Expr = {
  type: &amp;#39;Mul&amp;#39;,
  x: {type: &amp;#39;B&amp;#39;, value: true},
  y: {type: &amp;#39;B&amp;#39;, value: false}
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To force the multiplication to be over numeric expressions, we would like to distinguish expressions which evaluate to a &lt;code&gt;number&lt;/code&gt; from expressions which evaluate to a &lt;code&gt;boolean&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;GADTs to the rescue&lt;/h2&gt;

&lt;p&gt;GADTs allow to parametrize the expression type &lt;code&gt;Expr&lt;/code&gt; by the return type of the evaluation (either &lt;code&gt;boolean&lt;/code&gt; or &lt;code&gt;number&lt;/code&gt;). In a language with GADTs like &lt;a href=&quot;https://www.haskell.org/&quot;&gt;Haskell&lt;/a&gt; we can define a type &lt;code&gt;Expr a&lt;/code&gt; (with &lt;code&gt;a&lt;/code&gt; the type of the evaluation result) as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;-- In Haskell:
data Expr a where
  B :: Bool -&amp;gt; Expr Bool
  I :: Int -&amp;gt; Expr Int
  Add :: Expr Int -&amp;gt; Expr Int -&amp;gt; Expr Int
  Mult :: Expr Int -&amp;gt; Expr Int -&amp;gt; Expr Int
  Eq :: Expr Int -&amp;gt; Expr Int -&amp;gt; Expr Bool

evaluate :: Expr a -&amp;gt; a
evaluate expr = ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately, Flow has no syntax to express this kind of types:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type Expr&amp;lt;A&amp;gt; = {
  type: &amp;#39;I&amp;#39;, // Where do we say this is Expr&amp;lt;number&amp;gt;?
  value: number
} | {
  type: &amp;#39;B&amp;#39;, // Where do we say this is Expr&amp;lt;boolean&amp;gt;?
  value: boolean
} | {
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Encoding in Flow&lt;/h2&gt;

&lt;p&gt;We use a trick to encode GATDs in Flow. We express that in the case &lt;code&gt;type: &amp;#39;I&amp;#39;&lt;/code&gt; of an expression of type &lt;code&gt;Expr&amp;lt;A&amp;gt;&lt;/code&gt; the type &lt;code&gt;A&lt;/code&gt; is actually a &lt;code&gt;number&lt;/code&gt; by adding an &lt;em&gt;equality witness&lt;/em&gt; &lt;code&gt;_eq&lt;/code&gt; between &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;number&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type Expr&amp;lt;A&amp;gt; = {
  type: &amp;#39;I&amp;#39;,
  _eq: number =&amp;gt; A,
  value: number
} | {
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To make the equality witness &lt;code&gt;_eq&lt;/code&gt; valid, we must define it with the identity function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function i(value: number): Expr&amp;lt;number&amp;gt; {
  return {type: &amp;#39;I&amp;#39;, _eq: x =&amp;gt; x, value};
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The full defintion:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type Expr&amp;lt;A&amp;gt; = {
  type: &amp;#39;I&amp;#39;,
  _eq: number =&amp;gt; A,
  value: number
} | {
  type: &amp;#39;B&amp;#39;,
  _eq: boolean =&amp;gt; A,
  value: boolean
} | {
  type: &amp;#39;Add&amp;#39;,
  _eq: number =&amp;gt; A,
  x: Expr&amp;lt;number&amp;gt;,
  y: Expr&amp;lt;number&amp;gt;
} | {
  type: &amp;#39;Mul&amp;#39;,
  _eq: number =&amp;gt; A,
  x: Expr&amp;lt;number&amp;gt;,
  y: Expr&amp;lt;number&amp;gt;
} | {
  type: &amp;#39;Eq&amp;#39;,
  _eq: boolean =&amp;gt; A,
  x: Expr&amp;lt;number&amp;gt;,
  y: Expr&amp;lt;number&amp;gt;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and all the wrappers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function i(value: number): Expr&amp;lt;number&amp;gt; {
  return {type: &amp;#39;I&amp;#39;, _eq: x =&amp;gt; x, value};
}

function b(value: boolean): Expr&amp;lt;boolean&amp;gt; {
  return {type: &amp;#39;B&amp;#39;, _eq: x =&amp;gt; x, value};
}

function add(x: Expr&amp;lt;number&amp;gt;, y: Expr&amp;lt;number&amp;gt;): Expr&amp;lt;number&amp;gt; {
  return {type: &amp;#39;Add&amp;#39;, _eq: x =&amp;gt; x, x, y};
}

function mul(x: Expr&amp;lt;number&amp;gt;, y: Expr&amp;lt;number&amp;gt;): Expr&amp;lt;number&amp;gt; {
  return {type: &amp;#39;Mul&amp;#39;, _eq: x =&amp;gt; x, x, y};
}

function eq(x: Expr&amp;lt;number&amp;gt;, y: Expr&amp;lt;number&amp;gt;): Expr&amp;lt;boolean&amp;gt; {
  return {type: &amp;#39;Eq&amp;#39;, _eq: x =&amp;gt; x, x, y};
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can then define a well-typed &lt;code&gt;evaluate&lt;/code&gt; function by applying the &lt;code&gt;_eq&lt;/code&gt; witness to use the type equalities when needed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function evaluate&amp;lt;A&amp;gt;(expr: Expr&amp;lt;A&amp;gt;): A {
  switch (expr.type) {
    case &amp;#39;I&amp;#39;:
      return expr._eq(expr.value);
    case &amp;#39;B&amp;#39;:
      return expr._eq(expr.value);
    case &amp;#39;Add&amp;#39;:
      return expr._eq(evaluate(expr.x) + evaluate(expr.y));
    case &amp;#39;Mul&amp;#39;:
      return expr._eq(evaluate(expr.x) * evaluate(expr.y));
    case &amp;#39;Eq&amp;#39;:
      return expr._eq(evaluate(expr.x) === evaluate(expr.y));
    default:
      return expr;
  }
}

const e1: Expr&amp;lt;number&amp;gt; = add(i(2), i(4));
const e2: Expr&amp;lt;number&amp;gt; = mul(i(2), i(3));
const e3: Expr&amp;lt;boolean&amp;gt; = eq(e1, e2);
console.log(evaluate(e1), evaluate(e2), evaluate(e3));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;_eq&lt;/code&gt; has no runtime value, is only here for typing and could be eliminated given a clever enough compiler / interpreter.&lt;/p&gt;

&lt;h2&gt;Robustness&lt;/h2&gt;

&lt;p&gt;We test the effectiveness of this encoding by introducing some errors in our code. In the definition of expressions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;mistake:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const e1: Expr&amp;lt;boolean&amp;gt; = add(i(2), i(4));
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flow output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const e1: Expr&amp;lt;boolean&amp;gt; = add(i(2), i(4));
                          ^ Cannot assign `add(...)` to `e1` because number [1] is incompatible with boolean [2] in type argument `A` [3].
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the definition of &lt;code&gt;evaluate&lt;/code&gt;, if we return a &lt;code&gt;boolean&lt;/code&gt; where we expect a &lt;code&gt;number&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;mistake:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;case &amp;#39;Mul&amp;#39;:
  return expr._eq(evaluate(expr.x) === evaluate(expr.y));
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flow output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;return expr._eq(evaluate(expr.x) === evaluate(expr.y));
                ^ Cannot call `expr._eq` with `evaluate(...) === evaluate(...)` bound to the first parameter because boolean [1] is incompatible with number [2].
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we do not use &lt;code&gt;_eq&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;mistake:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;case &amp;#39;Mul&amp;#39;:
  return evaluate(expr.x) * evaluate(expr.y);
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flow output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;return evaluate(expr.x) * evaluate(expr.y);
       ^ Cannot return `evaluate(...) * evaluate(...)` because number [1] is incompatible with `A` [2].
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A weakness of this encoding is that we must enforce by hand that &lt;code&gt;_eq&lt;/code&gt; is always &lt;code&gt;x =&amp;gt; x&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Related&lt;/h2&gt;

&lt;p&gt;The idea of using type equalities is taken from &lt;a href=&quot;http://code.slipthrough.net/2016/08/10/approximating-gadts-in-purescript/&quot;&gt;Approximating GADTs in PureScript&lt;/a&gt;. In &lt;a href=&quot;http://www.purescript.org/&quot;&gt;PureScript&lt;/a&gt; the type system almost enforces that &lt;code&gt;_eq&lt;/code&gt; is the identity (it could also be a non-terminating function). There is a &lt;a href=&quot;https://github.com/facebook/flow/issues/1356&quot;&gt;thread&lt;/a&gt; on GitHub issues discussing the addition of GADTs to Flow.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/approximating-gadts-in-flow.html</link>
        <guid>https://rocq-blog.clarus.me/approximating-gadts-in-flow.html</guid>
      </item>
    
      <item>
        <title>Simple unit testing in Coq</title>
        <pubDate>Sat, 25 Jul 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;Even if we can prove things in &lt;a href=&quot;http://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/Unit_testing&quot;&gt;unit testing&lt;/a&gt; remains an extremely simple and powerful technique to verify what we write. Thanks to the dependent types, we can run unit tests in Coq at compile time by computing into the types. For example, in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition test_pred : pred 5 = 4 :=
  eq_refl.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we check that the predecessor of &lt;code&gt;5&lt;/code&gt; is &lt;code&gt;4&lt;/code&gt; by saying that they are logically equal. The proof of the equality is its only constructor &lt;code&gt;eq_refl&lt;/code&gt;. We force the type-checker of Coq to reduce both &lt;code&gt;pred 5&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt; to check that they are the same.&lt;/p&gt;

&lt;p&gt;Usually we want to check many values, what we can do with a list:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Require Import Coq.Lists.List.

Import ListNotations.

Definition test_pred :
  List.map pred [0; 1; 2; 5] = [0; 0; 1; 4] :=
  eq_refl.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We map the &lt;code&gt;pred&lt;/code&gt; function on the values &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt; and check the results.&lt;/p&gt;

&lt;h2&gt;The CUnit package&lt;/h2&gt;

&lt;p&gt;For functions with several parameters, the trick of the &lt;code&gt;List.map&lt;/code&gt; function does not work well because of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Currying&quot;&gt;currying&lt;/a&gt;. What need instead is a map over lists of tuples. I made the package &lt;a href=&quot;https://github.com/clarus/coq-cunit&quot;&gt;CUnit&lt;/a&gt; to provide such a map. Install it with &lt;a href=&quot;use-opam-for-coq.html&quot;&gt;OPAM&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-released https://coq.inria.fr/opam/released
opam install coq-cunit
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can test the &lt;code&gt;plus&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Require Import Coq.Lists.List.
Require Import CUnit.All.

Import ListNotations.

Definition test_plus : List.map_pair plus
  [(0, 0); (0, 3); (4, 0); (4, 3)] =
  [0; 3; 4; 7] :=
  eq_refl.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The complete list of maps in the package &lt;a href=&quot;https://github.com/clarus/coq-cunit&quot;&gt;CUnit&lt;/a&gt; is the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;List.map_pair {A B C} (f : A -&amp;gt; B -&amp;gt; C) (l : list (A * B)) : list C&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;List.map_triple {A B C D} (f : A -&amp;gt; B -&amp;gt; C -&amp;gt; D) (l : list (A * B * C)) : list D&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;List.map_quad {A B C D E} (f : A -&amp;gt; B -&amp;gt; C -&amp;gt; D -&amp;gt; E) (l : list (A * B * C * D)) : list E&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Edit: this list of functions can be generalized to an arbitrary number of arguments. See the post &lt;a href=&quot;http://gallais.github.io/blog/canonical-structures-currying.html&quot;&gt;Currying using Canonical Structures&lt;/a&gt; of &lt;a href=&quot;http://gallais.github.io/&quot;&gt;Guillaume Allais&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/simple-unit-testing-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/simple-unit-testing-in-coq.html</guid>
      </item>
    
      <item>
        <title>Handle errors in Coq</title>
        <pubDate>Thu, 23 Jul 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;Many programming languages handle errors with an exceptions mechanism. There are no exceptions in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;, since this is a pure programming language. We mainly get two alternatives:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;to extend Coq with an effect system for exceptions, implemented with monads or alike;&lt;/li&gt;
&lt;li&gt;to use explicit sum types.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Even if the first option seems more powerful, from my experience an effect system is too heavy for the gains it brings compared the use of sum types with combinators. Sum types are just &lt;em&gt;simple&lt;/em&gt; and &lt;em&gt;ubiquitous&lt;/em&gt;. This is in fact the way errors are handled in &lt;a href=&quot;http://blog.burntsushi.net/rust-error-handling/&quot;&gt;Rust&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are two basic sum types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;option A&lt;/code&gt;: either &lt;code&gt;Some value&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt; for an error&lt;/li&gt;
&lt;li&gt;&lt;code&gt;A + E&lt;/code&gt;: either &lt;code&gt;inl value&lt;/code&gt; or &lt;code&gt;inr err&lt;/code&gt; for an error&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The ErrorHandlers package&lt;/h2&gt;

&lt;p&gt;Since there are no error combinators in the standard library, I wrote the &lt;a href=&quot;https://github.com/clarus/coq-error-handlers&quot;&gt;ErrorHandlers&lt;/a&gt; package:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-released https://coq.inria.fr/opam/released
opam install -j4 coq-error-handlers
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It provides the basic combinators &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;default&lt;/code&gt;. The &lt;code&gt;bind&lt;/code&gt; sequences two computations with errors:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Require Import Coq.Lists.List.
Require Import ErrorHandlers.All.

Import ListNotations.

Compute
  Option.bind (List.hd_error [2; 4; 5]) (fun n =&amp;gt;
  Some (n + 1)).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;prints &lt;code&gt;Some 3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;map&lt;/code&gt; is a particular case for when the second expression does not return errors:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Compute Option.map (List.hd_error [2; 4; 5]) (fun n =&amp;gt; n + 1).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;prints &lt;code&gt;Some 3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;default&lt;/code&gt; replaces an error with a default value:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Compute Option.default (Some 3) 0.
Compute Option.default None 0.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;prints &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To summarize, here are the types of the combinators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Option.bind : forall {A B}, option A -&amp;gt; (A -&amp;gt; option B) -&amp;gt; option B&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Option.map : forall {A B}, option A -&amp;gt; (A -&amp;gt; B) -&amp;gt; option B&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Option.default : forall {A}, option A -&amp;gt; A -&amp;gt; A&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Sum.bind : forall {E A B}, A + E -&amp;gt; (A -&amp;gt; B + E) -&amp;gt; B + E&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Sum.map : forall {E A B}, A + E -&amp;gt; (A -&amp;gt; B) -&amp;gt; B + E&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Sum.default : forall {E A}, A + E -&amp;gt; A -&amp;gt; A&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are no notations to keep things simple and non-intrusive.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/handle-errors-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/handle-errors-in-coq.html</guid>
      </item>
    
      <item>
        <title>Launch of the Coq.io website</title>
        <pubDate>Tue, 21 Jul 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;&lt;a href=&quot;http://coq.io/&quot;&gt;Coq.io&lt;/a&gt; is a library for writing and proving concurrent applications with inputs&amp;ndash;outputs in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;. In order to centralize the informations about it, I have setup a &lt;a href=&quot;http://coq.io/&quot;&gt;website on http://coq.io/&lt;/a&gt;. I have been lucky to get this domain name, since this gives a direct reference to the &lt;a href=&quot;https://wiki.haskell.org/IO_inside&quot;&gt;IO monad&lt;/a&gt; of &lt;a href=&quot;https://www.haskell.org/&quot;&gt;Haskell&lt;/a&gt;, which popularized the idea of clean imperative programming in a functional language.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href=&quot;http://coq.io/getting_started.html&quot;&gt;Getting started&lt;/a&gt; page with some basic examples and an introduction to the technique of formal specification by use cases, and links to some reference documentation. The library is still evolving, with three upcoming improvements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a type &lt;a href=&quot;https://github.com/coq-io/io/blob/master/src/C.v&quot;&gt;&lt;code&gt;C.I.t&lt;/code&gt;&lt;/a&gt; for infinite (co-inductive) computations;&lt;/li&gt;
&lt;li&gt;a type &lt;a href=&quot;https://github.com/coq-io/io/blob/master/src/Trace.v&quot;&gt;&lt;code&gt;Trace.t&lt;/code&gt;&lt;/a&gt; for whose who want to separate the specifications from the proofs of use cases;&lt;/li&gt;
&lt;li&gt;a &lt;a href=&quot;https://github.com/coq-io/lwt&quot;&gt;&lt;code&gt;Lwt.E&lt;/code&gt;&lt;/a&gt; effect to add arbitrary &lt;a href=&quot;http://ocsigen.org/lwt/&quot;&gt;Lwt&lt;/a&gt; commands.&lt;/li&gt;
&lt;/ul&gt;
</description>
        <link>https://rocq-blog.clarus.me/launch-of-the-coq-io-website.html</link>
        <guid>https://rocq-blog.clarus.me/launch-of-the-coq-io-website.html</guid>
      </item>
    
      <item>
        <title>Implementation of promises for Coq</title>
        <pubDate>Mon, 16 Mar 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;&lt;a href=&quot;http://coq-blog.clarus.me/concurrency-with-promises-in-coq.html&quot;&gt;Last time&lt;/a&gt;, we presented two primitives &lt;code&gt;Join&lt;/code&gt; and &lt;code&gt;First&lt;/code&gt; to write and specify concurrent programs with &lt;a href=&quot;http://coq.io/&quot;&gt;Coq.io&lt;/a&gt;, using the concept of &lt;a href=&quot;http://en.wikipedia.org/wiki/Futures_and_promises&quot;&gt;promises&lt;/a&gt;. We will explain how we implemented these primitives and give examples of concurrent programs.&lt;/p&gt;

&lt;h2&gt;Implementation&lt;/h2&gt;

&lt;p&gt;The promises are available in the package &lt;a href=&quot;https://github.com/coq-io/io&quot;&gt;coq-io&lt;/a&gt;, starting from the version &lt;code&gt;2.1.0&lt;/code&gt;. This package symbolically describes the primitive &lt;code&gt;Join&lt;/code&gt; and &lt;code&gt;First&lt;/code&gt;. They must be implemented for each  set of effects.&lt;/p&gt;

&lt;p&gt;For example, in &lt;a href=&quot;https://github.com/coq-io/system&quot;&gt;coq-io-system&lt;/a&gt; (which defines effects to interact with the system), we compile all the external calls and the computation primitives to &lt;a href=&quot;http://ocsigen.org/lwt/&quot;&gt;Lwt&lt;/a&gt; in &lt;a href=&quot;https://github.com/coq-io/system/blob/master/src/Extraction.v&quot;&gt;coq-io/src/Extraction.v&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Fixpoint eval {A : Type} (x : C.t System.effects A) : Lwt.t A.
  destruct x as [A x | command | A B x f | A B x y | A B x y].
  - exact (Lwt.ret x).
  - exact (eval_command command).
  - exact (Lwt.bind (eval _ x) (fun x =&amp;gt; eval _ (f x))).
  - exact (Lwt.join (eval _ x) (eval _ y)).
  - exact (
      Lwt.bind (Lwt.first (eval _ x) (eval _ y)) (fun s =&amp;gt;
      Lwt.ret @@ Sum.to_coq s)).
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code is written in proof mode, so that we do not care about the &lt;code&gt;match&lt;/code&gt;/&lt;code&gt;with&lt;/code&gt; parameters and compile with both Coq 8.4 and 8.5 patterns. The functions &lt;code&gt;join&lt;/code&gt; and &lt;code&gt;first&lt;/code&gt; are implemented in &lt;a href=&quot;https://github.com/coq-io/system-ocaml/blob/master/ioSystem.ml&quot;&gt;coq-io-system-ocaml/ioSystem.ml&lt;/a&gt; using Lwt primitives. The Lwt primitives needed some wrappers because their types are:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Lwt.join : unit Lwt.t list -&amp;gt; unit Lwt.t
Lwt.pick : &amp;#39;a Lwt.t list -&amp;gt; &amp;#39;a list Lwt.t
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where we needed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;join : &amp;#39;a Lwt.t -&amp;gt; &amp;#39;b Lwt.t -&amp;gt; (&amp;#39;a * &amp;#39;b) Lwt.t
first : &amp;#39;a Lwt.t -&amp;gt; &amp;#39;b Lwt.t -&amp;gt; (&amp;#39;a, &amp;#39;b) Sum.t Lwt.t
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Hello World&lt;/h2&gt;

&lt;p&gt;You can see a simple example of concurrent program in &lt;a href=&quot;https://github.com/coq-io/hello-world&quot;&gt;coq-hello-world&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition concurrent_hello_world (argv : list LString.t)
  : C.t System.effects unit :=
  let! _ : unit * unit := join
    (System.log (LString.s &amp;quot;Hello&amp;quot;))
    (System.log (LString.s &amp;quot;World&amp;quot;)) in
  ret tt.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can compile this program by replacing the line:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition main := Extraction.run hello_world.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition main := Extraction.run concurrent_hello_world.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and following the instructions given in the &lt;a href=&quot;https://github.com/coq-io/hello-world/blob/master/README.md&quot;&gt;README.md&lt;/a&gt;. It will display either:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Hello
World
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;World
Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The specification of this program stays that it does exactly one thing: displaying concurrently &lt;code&gt;Hello&lt;/code&gt; and &lt;code&gt;World&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition concurrent_hello_world_ok (argv : list LString.t)
  : Run.t (concurrent_hello_world argv) tt.
  apply (Run.Let (Run.Join
    (Run.log_ok (LString.s &amp;quot;Hello&amp;quot;))
    (Run.log_ok (LString.s &amp;quot;World&amp;quot;)))).
  apply Run.Ret.
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Larger example and drawbacks&lt;/h2&gt;

&lt;p&gt;In the branch &lt;a href=&quot;https://github.com/clarus/repos2web/tree/join&quot;&gt;#join&lt;/a&gt; of &lt;a href=&quot;https://github.com/clarus/repos2web&quot;&gt;repos2web&lt;/a&gt;, we modified our website generator for &lt;a href=&quot;http://opam.ocamlpro.com/&quot;&gt;OPAM&lt;/a&gt; repositories to read the packages descriptions concurrently. Our main modification is to use the &lt;code&gt;Join&lt;/code&gt; operator in the recursive functions to make concurrent recursive calls:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Fixpoint get_packages (repository : LString.t) (packages : Packages.t)
  : C FullPackages.t :=
  match packages with
  | [] =&amp;gt; ret []
  | package :: packages =&amp;gt;
    let! full_package_full_packages := join
      (get_package repository package)
      (get_packages repository packages) in
    let (full_package, full_packages) := full_package_full_packages in
    ret (full_package :: full_packages)
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The specifications are updated similarly.&lt;/p&gt;

&lt;p&gt;We tried to run this program on large generated repositories, to see if we gained performances. Unfortunately, after a couple of thousands of packages, we hit the limit of number of files we can open at the same time. We could have a finer control of the concurrency with, for example, a bounded pool of light-weight threads, or by handling the &lt;code&gt;EMFILE&lt;/code&gt; exception (&amp;ldquo;too many open files&amp;rdquo;) to retry again later.&lt;/p&gt;

&lt;p&gt;We feel that the specification of the Linux system API is quite complex, with many corner cases in practice (like this limit on the number of opened files). These corner cases are hard to be spotted by formal specifications, since the specification of Linux seems complex. Thus programming on top of Linux does not benefit as much as we could hope from formal specifications.&lt;/p&gt;

&lt;p&gt;Next, we would like to investigate more the &lt;a href=&quot;http://www.xenproject.org/&quot;&gt;Xen&lt;/a&gt; API which seems cleaner, completely event-driven and available in &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; thanks to the &lt;a href=&quot;http://www.openmirage.org/&quot;&gt;MirageOS&lt;/a&gt; project.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/implementation-of-promises-for-coq.html</link>
        <guid>https://rocq-blog.clarus.me/implementation-of-promises-for-coq.html</guid>
      </item>
    
      <item>
        <title>Concurrency with promises in Coq</title>
        <pubDate>Sat, 14 Mar 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;We will present two primitives to write concurrent (and interactive) programs in &lt;a href=&quot;http://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;, using the concept of &lt;a href=&quot;http://en.wikipedia.org/wiki/Futures_and_promises&quot;&gt;promises&lt;/a&gt; in the library &lt;a href=&quot;http://coq.io/&quot;&gt;Coq.io&lt;/a&gt;. We will also show how to formally specify programs written using promises.&lt;/p&gt;

&lt;h2&gt;Primitives for concurrency&lt;/h2&gt;

&lt;p&gt;To write an interactive application we quickly need a way to do non-blocking inputs&amp;ndash;outputs operations. There are many approaches to do non-blocking operations or concurrency, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;blocking calls in system threads&lt;/li&gt;
&lt;li&gt;a main event loop&lt;/li&gt;
&lt;li&gt;callbacks in lightweight threads&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Futures_and_promises&quot;&gt;promises&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;the &lt;a href=&quot;http://en.wikipedia.org/wiki/Actor_model&quot;&gt;actor model&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We chose to use promises, in the style of the &lt;a href=&quot;http://ocsigen.org/lwt/&quot;&gt;Lwt library&lt;/a&gt; for &lt;a href=&quot;http://en.wikipedia.org/wiki/Light-weight_process&quot;&gt;lightweight threads&lt;/a&gt; in &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt;. The promises can be implemented efficiently using lightweight threads, while being simpler to use than an event loop or callbacks. The actor model is another interesting approach which we will study later.&lt;/p&gt;

&lt;h3&gt;Sequential computations&lt;/h3&gt;

&lt;p&gt;We recall the definition of our interactive sequential computations (see the &lt;a href=&quot;https://github.com/coq-io/io&quot;&gt;coq-io&lt;/a&gt; package):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module C.
  Inductive t (E : Effects.t) : Type -&amp;gt; Type :=
  | Ret : forall {A : Type} (x : A), t E A
  | Call : forall (command : Effects.command E), t E (Effects.answer E command)
  | Let : forall {A B : Type}, t E A -&amp;gt; (A -&amp;gt; t E B) -&amp;gt; t E B.
End C.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where the effects are defined as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module Effects.
  Record t := New {
    command : Type;
    answer : command -&amp;gt; Type }.
End Effects.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The effects are a type of &lt;code&gt;command&lt;/code&gt; and a type of &lt;code&gt;answer&lt;/code&gt;, dependent on the values of the commands. A computation of type &lt;code&gt;C.t E A&lt;/code&gt; is a computation returning a value of type &lt;code&gt;A&lt;/code&gt; with the effects &lt;code&gt;E&lt;/code&gt;. It can be of three forms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Ret x&lt;/code&gt;, the pure value &lt;code&gt;x&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Call command&lt;/code&gt;, the call to a command&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Let x f&lt;/code&gt;, the evaluation of &lt;code&gt;x&lt;/code&gt;, followed by the evaluation of &lt;code&gt;f&lt;/code&gt; applied to the result of &lt;code&gt;x&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;Let&lt;/code&gt; operator is also called the &lt;em&gt;bind&lt;/em&gt; in the terminology of &lt;a href=&quot;http://en.wikipedia.org/wiki/Monad_%28functional_programming%29&quot;&gt;monads&lt;/a&gt;. The effects &lt;code&gt;E&lt;/code&gt; typically represent external calls to the system. You can for example look at the &lt;a href=&quot;http://clarus.github.io/doc/io-system/Io.System.System.html&quot;&gt;API&lt;/a&gt; of the &lt;a href=&quot;https://github.com/coq-io/system&quot;&gt;coq-io-system&lt;/a&gt; library, which provides basic calls to manipulates files and the terminal.&lt;/p&gt;

&lt;h3&gt;Join&lt;/h3&gt;

&lt;p&gt;We add the &lt;code&gt;Join&lt;/code&gt; operator to the computations:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;| Join : forall {A B : Type}, t E A -&amp;gt; t E B -&amp;gt; t E (A * B)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The program &lt;code&gt;Join x y&lt;/code&gt; runs the two computations &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; in parallel and returns the couple of their results. Since the two computations are launched concurrently, blocking calls made in &lt;code&gt;x&lt;/code&gt; will not block calls in &lt;code&gt;y&lt;/code&gt;, and reciprocally. This is the main operator we will use to write concurrent programs.&lt;/p&gt;

&lt;p&gt;Note that since our programs are pure, there are no shared states between &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;. We will see in a future post how to represent a shared state in a concurrent program in Coq.&lt;/p&gt;

&lt;h3&gt;First&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;First&lt;/code&gt; operator is defined as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;| First : forall {A B : Type}, t E A -&amp;gt; t E B -&amp;gt; t E (A + B)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The program &lt;code&gt;First x y&lt;/code&gt; runs the two computations &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; in parallel and returns the result of the first which terminated. The other may be canceled.&lt;/p&gt;

&lt;p&gt;This operator is dangerous because one of our computations can get canceled. The &lt;code&gt;First&lt;/code&gt; primitive is mainly there to implement timeouts. If we want to run a computation &lt;code&gt;slow&lt;/code&gt; which may not terminate or may take too much time, we can combine this computation with a timeout by writing something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;First slow (sleep 10)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to make sure the program terminates after 10 seconds (we suppose that &lt;code&gt;sleep 10&lt;/code&gt; is the computation which does nothing but terminating after 10 seconds).&lt;/p&gt;

&lt;h2&gt;Specification of promises&lt;/h2&gt;

&lt;p&gt;We specify our computations using &lt;a href=&quot;http://en.wikipedia.org/wiki/Use_case&quot;&gt;use-cases&lt;/a&gt; described by runs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Module Run.
  Inductive t : forall {E : Effects.t} {A : Type}, C.t E A -&amp;gt; A -&amp;gt; Type :=
  | Ret : forall {E A} (x : A), t (C.Ret (E := E) x) x
  | Call : forall E (command : Effects.command E) (answer : Effects.answer E command),
    t (C.Call E command) answer
  | Let : forall {E A B} {c_x : C.t E B} {x : B} {c_f : B -&amp;gt; C.t E A} {y : A},
    t c_x x -&amp;gt; t (c_f x) y -&amp;gt; t (C.Let c_x c_f) y.
End Run.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see an example of specification using runs in &lt;a href=&quot;http://coq-blog.clarus.me/formally-verify-a-script-in-coq.html&quot;&gt;Formally verify a script in Coq&lt;/a&gt;. The main idea is that a universally quantified run can be viewed as the formal specification of a use-case. This kind of specification is verified just by typing, using lemma on equalities when the type-checker fails.&lt;/p&gt;

&lt;p&gt;A run can be a run of the computation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Ret x&lt;/code&gt;, the pure value &lt;code&gt;x&lt;/code&gt; returned by the computation&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Call command&lt;/code&gt;, an answer to this command&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Let x f&lt;/code&gt;, a run of &lt;code&gt;x&lt;/code&gt; and a run of &lt;code&gt;f&lt;/code&gt; applied to the result of the run of &lt;code&gt;x&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will extend our definition of runs with new cases for the primitives &lt;code&gt;Join&lt;/code&gt; and &lt;code&gt;First&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Join&lt;/h3&gt;

&lt;p&gt;We define the run of a &lt;code&gt;Join&lt;/code&gt; by:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;| Join : forall {E A B} {c_x : C.t E A} {x : A} {c_y : C.t E B} {y : B},
  t c_x x -&amp;gt; t c_y y -&amp;gt; t (C.Join c_x c_y) (x, y)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This means that a run of &lt;code&gt;Join x y&lt;/code&gt; is a couple of runs for &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;. Equivalently, a specification of &lt;code&gt;Join x y&lt;/code&gt; is a couple of specifications for &lt;code&gt;x&lt;/code&gt; and for &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A priori&lt;/em&gt;, the interactions of the threads &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are not specified. This is up to the user to express these interactions in the specification, if there are some.&lt;/p&gt;

&lt;h3&gt;First&lt;/h3&gt;

&lt;p&gt;There are two ways to run a &lt;code&gt;First&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;| Left : forall {E A B} {c_x : C.t E A} {x : A} {c_y : C.t E B},
  t c_x x -&amp;gt; t (C.First c_x c_y) (inl x)
| Right : forall {E A B} {c_x : C.t E A} {c_y : C.t E B} {y : B},
  t c_y y -&amp;gt; t (C.First c_x c_y) (inr y)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A run of &lt;code&gt;First x y&lt;/code&gt; is either a run of &lt;code&gt;x&lt;/code&gt; (the &lt;code&gt;Left&lt;/code&gt; case) or a run of &lt;code&gt;y&lt;/code&gt; (the &lt;code&gt;Right&lt;/code&gt; case). Equivalently, a specification of &lt;code&gt;First x y&lt;/code&gt; is a specification of &lt;code&gt;x&lt;/code&gt; or a specification of &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We could ask: yes, but sometimes both &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are actually executed! And if we choose the &lt;code&gt;Left&lt;/code&gt; case for example, we should program &lt;code&gt;x&lt;/code&gt; instead of &lt;code&gt;First x y&lt;/code&gt; to start with. These are the same concerns we may have for the definition of the &lt;em&gt;logical disjunction&lt;/em&gt; in Coq:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive or (A B : Prop) : Prop :=
| or_introl : A -&amp;gt; A \/ B
| or_intror : B -&amp;gt; A \/ B

where &amp;quot;A \/ B&amp;quot; := (or A B) : type_scope.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and the reasons of these definitions are similar.&lt;/p&gt;

&lt;h2&gt;Next time&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://coq-blog.clarus.me/implementation-of-promises-for-coq.html&quot;&gt;Next time&lt;/a&gt; we will see how to implement the primitives &lt;code&gt;Join&lt;/code&gt; and &lt;code&gt;First&lt;/code&gt;, to generate efficient and non-blocking interactive programs written in Coq.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/concurrency-with-promises-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/concurrency-with-promises-in-coq.html</guid>
      </item>
    
      <item>
        <title>Formally verify a script in Coq</title>
        <pubDate>Thu, 05 Mar 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;Last time, in &lt;a href=&quot;http://coq-blog.clarus.me/write-a-script-in-coq.html&quot;&gt;Write a script in Coq&lt;/a&gt;, we explained how to implement scripts in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; using &lt;a href=&quot;http://coq.io/&quot;&gt;Coq.io&lt;/a&gt; with the example of &lt;a href=&quot;https://github.com/clarus/repos2web&quot;&gt;repos2web&lt;/a&gt;, a website generator for &lt;a href=&quot;http://opam.ocaml.org/&quot;&gt;OPAM&lt;/a&gt; repositories. We will see how to specify and prove correct this script.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There is now a newer OPAM website generator &lt;a href=&quot;https://github.com/coq-io/opam-website&quot;&gt;opam-website&lt;/a&gt;. See the results on &lt;a href=&quot;http://coq.io/opam/&quot;&gt;coq.io/opam&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Unit testing, revisited&lt;/h2&gt;

&lt;p&gt;A common practice to check programs is to write &lt;a href=&quot;http://en.wikipedia.org/wiki/Unit_testing&quot;&gt;unit tests&lt;/a&gt;. For each function, a test is written to execute it on some particular inputs, and to check that the results are what we expect. On more complex programs, we also need to simulate the inputs&amp;ndash;outputs with the users, a database, the network, &amp;hellip; To solve this problem, programmers invented solutions like the &lt;a href=&quot;http://en.wikipedia.org/wiki/Mock_object&quot;&gt;mock objects&lt;/a&gt; which are basically implementations of fake execution environments.&lt;/p&gt;

&lt;p&gt;Unfortunately tests are not exhaustive since the set of program inputs is usually infinite. Methods like &lt;a href=&quot;http://en.wikipedia.org/wiki/Random_testing&quot;&gt;random testing&lt;/a&gt; can extend the range of tested configurations, but are always limited to a finite set of inputs.&lt;/p&gt;

&lt;p&gt;We will explain how to write exhaustive tests in Coq by writing &lt;em&gt;scenarios&lt;/em&gt; which formalize the notion of &lt;a href=&quot;http://en.wikipedia.org/wiki/Use_case&quot;&gt;use cases&lt;/a&gt;. The completeness of the scenarios is given us for free, thanks to the strict type-system of Coq: in our settings, a scenario is correct if it is well-typed so that there is no need to run it on its (infinite) set of parameters.&lt;/p&gt;

&lt;p&gt;To the best of our knowledge, Coq is the only language with formal verification of use cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remark:&lt;/strong&gt; some people are used to specify programs with pre/post-conditions or invariants, rather than by &lt;a href=&quot;http://en.wikipedia.org/wiki/Use_case&quot;&gt;use cases&lt;/a&gt;. We believe that these approaches are pertinent for algorithms but not for interactive programs, because (from our experience) these specifications are less natural than the specifications by use cases. This seems counter-intuitive because use-cases do not cover all the execution paths. But we have to remember that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the Coq type system protects us from bad behaviors in all execution paths,&lt;/li&gt;
&lt;li&gt;there are often hypothesis on the environment, so all execution paths may not be interesting,&lt;/li&gt;
&lt;li&gt;the quality of a specification depends on its clarity and its similarities with the human intuition.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Write the scenarios&lt;/h2&gt;

&lt;p&gt;We write our scenarios in &lt;a href=&quot;https://github.com/clarus/repos2web/blob/master/src/Scenarios.v&quot;&gt;src/Scenarios.v&lt;/a&gt;. There is at least one scenario per function, for the cases in which the environment has no bugs (no file system errors) and the repository architecture is well-formed. For example, for the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition get_packages (repository : LString.t) : C (option Packages.t) :=
  let! names := list_coq_files repository in
  match names with
  | None =&amp;gt; ret None
  | Some names =&amp;gt; get_packages_of_names repository names
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we write the following scenario:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition get_packages_ok (repository : LString.t) (packages : Packages.t)
  : Run.t (get_packages repository) (Some packages).
  apply (Let (list_coq_files_ok _ (Packages.to_folders packages))).
  apply (get_packages_of_names_ok repository packages).
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;What does this mean?&lt;/h3&gt;

&lt;p&gt;This scenario is parametrized by any &lt;code&gt;repository&lt;/code&gt; folder names and any lists of OPAM &lt;code&gt;packages&lt;/code&gt;. It runs the function &lt;code&gt;get_packages&lt;/code&gt; on &lt;code&gt;repository&lt;/code&gt; in a &amp;ldquo;valid&amp;rdquo; environment and ensures that the result is &lt;code&gt;Some packages&lt;/code&gt;, meaning that the repository was successfully parsed.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;get_package&lt;/code&gt; function starts by a &lt;code&gt;let!&lt;/code&gt; to call &lt;code&gt;list_coq_files&lt;/code&gt;. We first reuse the scenario &lt;code&gt;list_coq_files_ok&lt;/code&gt;. This describes a use case in which a list of folders starting by &lt;code&gt;coq-&lt;/code&gt; is correctly listed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;apply (Let (list_coq_files_ok _ (Packages.to_folders packages))).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since this scenario states that the result is &lt;code&gt;Some files&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition list_coq_files_ok (folder : LString.t) (files : list Name.t)
  : Run.t (list_coq_files folder) (Some files).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we know that the function &lt;code&gt;get_packages&lt;/code&gt; will call the function &lt;code&gt;get_packages_of_names&lt;/code&gt;. We then apply its scenario:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;apply (get_packages_of_names_ok repository packages).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Coq tells us there nothing more to do, so we conclude by:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The Coq type-checker accepts our scenario which means it is valid: we do not need to run it on every &lt;code&gt;(repository, packages)&lt;/code&gt; tuples. Even if we used the tactical mode to define the scenario, we did not write any proofs: the scenario is valid &lt;em&gt;by-construction&lt;/em&gt;. However, in some cases, proofs are required to help the type-checker with non-trivial equalities. See for example the &lt;code&gt;list_coq_files_ok&lt;/code&gt; scenario. Over 13 scenarios, only 2 required proofs to help the type-checker.&lt;/p&gt;

&lt;h3&gt;What did we prove?&lt;/h3&gt;

&lt;p&gt;We proved that, for any list of OPAM packages, given the &amp;ldquo;right answers&amp;rdquo; from the file system, the function &lt;code&gt;get_packages&lt;/code&gt; will terminate without errors and will return &lt;code&gt;Some packages&lt;/code&gt;. The &amp;ldquo;right answers&amp;rdquo; are defined by giving the &amp;ldquo;right answers&amp;rdquo; to &lt;code&gt;list_coq_files&lt;/code&gt; and then giving the &amp;ldquo;right answers&amp;rdquo; to &lt;code&gt;get_packages_of_names&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Next time&lt;/h2&gt;

&lt;p&gt;We have seen how to formally verify an interactive program in Coq. &lt;a href=&quot;http://coq-blog.clarus.me/concurrency-with-promises-in-coq.html&quot;&gt;Next time&lt;/a&gt; we will see how to optimize this program using concurrency.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/formally-verify-a-script-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/formally-verify-a-script-in-coq.html</guid>
      </item>
    
      <item>
        <title>Write a script in Coq</title>
        <pubDate>Wed, 04 Mar 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;We will explain how to write scripts in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; using the library &lt;a href=&quot;http://coq.io/&quot;&gt;Coq.io&lt;/a&gt; with the example of &lt;a href=&quot;https://github.com/clarus/repos2web&quot;&gt;repos2web&lt;/a&gt;, a website generator. This generator parses an &lt;a href=&quot;http://opam.ocaml.org/&quot;&gt;OPAM&lt;/a&gt; repository with Coq packages and generates an HTML page.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There is now a newer OPAM website generator &lt;a href=&quot;https://github.com/coq-io/opam-website&quot;&gt;opam-website&lt;/a&gt;. See the results on &lt;a href=&quot;http://coq.io/opam/&quot;&gt;coq.io/opam&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Get started&lt;/h2&gt;

&lt;p&gt;Install the &lt;a href=&quot;https://github.com/coq-io/system&quot;&gt;coq-io-system&lt;/a&gt; package with OPAM to enable the system effects. See &lt;a href=&quot;http://coq-blog.clarus.me/use-opam-for-coq.html&quot;&gt;Use OPAM for Coq&lt;/a&gt; to configure OPAM for Coq.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-released https://coq.inria.fr/opam/released
opam install coq-io-system
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Create an empty Coq project by adding the following files in a fresh directory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;configure.sh&lt;/code&gt;, the configure script:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/sh

coq_makefile -f Make -o Makefile
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Make&lt;/code&gt;, the &lt;code&gt;coq_makefile&lt;/code&gt; project file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;-R src Repos2Web

src/Main.v
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;src/Main.v&lt;/code&gt;, the main source file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Require Import Coq.Lists.List.
Require Import Io.All.
Require Import Io.System.All.
Require Import ListString.All.

Import ListNotations.
Import C.Notations.

(** The main function. *)
Definition main (argv : list LString.t) : C.t System.effects unit :=
  System.log (LString.s &amp;quot;test&amp;quot;).

(** The extracted program. *)
Definition repos2web := Extraction.run main.
Extraction &amp;quot;extraction/repos2web&amp;quot; repos2web.
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;extraction/Makefile&lt;/code&gt;, the Makefile for the extracted program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;build:
    ocamlbuild repos2web.native -use-ocamlfind -package io-system

clean:
    ocamlbuild -clean
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compile your Coq code to &lt;a href=&quot;http://ocaml.org/&quot;&gt;OCaml&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./configure.sh
make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compile and run the generated OCaml:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd extraction/
make
./repos2web.native
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This should print you the message &lt;code&gt;test&lt;/code&gt; on the terminal!&lt;/p&gt;

&lt;h2&gt;Parse the OPAM repository&lt;/h2&gt;

&lt;p&gt;To write our script we need to understand the basis of how OPAM for Coq repositories are organized (for example in &lt;a href=&quot;https://github.com/coq/opam-coq-archive&quot;&gt;opam-coq-archive&lt;/a&gt;). All the packages are in the &lt;code&gt;packages&lt;/code&gt; folders. There is one folder per package name, all prefixed by &lt;code&gt;coq-&lt;/code&gt; because we are in the Coq namespace. In each package folder, there is one folder per version of the package with three files &lt;code&gt;descr&lt;/code&gt;, &lt;code&gt;opam&lt;/code&gt; and &lt;code&gt;url&lt;/code&gt; to describe the package.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;packages/
  coq-list-string/
    coq-list-string.1.0.0/
      descr
      opam
      url
    coq-list-string.2.0.0/
      ...
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We define the data type of an OPAM repository in &lt;a href=&quot;https://github.com/clarus/repos2web/blob/master/src/Model.v&quot;&gt;src/Model.v&lt;/a&gt;. In a first pass, we will generate an element of type &lt;code&gt;Packages.t&lt;/code&gt;. This is a list of packages described by a name and a list of versions. In a second pass, we will generate an element of type &lt;code&gt;FullPackages.t&lt;/code&gt;, by adding the description of each version and by computing each latest version using the (complex) &lt;a href=&quot;https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version&quot;&gt;Debian ordering&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;First pass&lt;/h3&gt;

&lt;p&gt;The first pass is described in &lt;a href=&quot;https://github.com/clarus/repos2web/blob/master/src/Main.v&quot;&gt;src/Main.v&lt;/a&gt; in the &lt;code&gt;Basic&lt;/code&gt; module. The function &lt;code&gt;list_coq_files&lt;/code&gt; lists the files/folders which are starting with the &lt;code&gt;coq-&lt;/code&gt; prefix in a given &lt;code&gt;folder&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition list_coq_files (folder : LString.t) : C (option (list Name.t)) :=
  let! folders := System.list_files folder in
  match folders with
  | None =&amp;gt;
    do! log (LString.s &amp;quot;The folder &amp;quot; ++ folder ++ LString.s &amp;quot; cannot be listed.&amp;quot;) in
    ret None
  | Some folders =&amp;gt; ret (Some (Name.of_strings folders))
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let us precise the return type &lt;code&gt;C (option (list Name.t))&lt;/code&gt;. We defined &lt;code&gt;C&lt;/code&gt; as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition C := C.t System.effects.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;for convenience. This means than &lt;code&gt;C&lt;/code&gt; is the type of computations doing interactions with the system. We need to use this special type because Coq is a purely functional language: without using the type &lt;code&gt;C&lt;/code&gt;, functions cannot do inputs&amp;ndash;outputs.&lt;/p&gt;

&lt;p&gt;The type &lt;code&gt;C&lt;/code&gt; is parametrized by &lt;code&gt;option (list Name.t)&lt;/code&gt;, the result type of the function. Thus, the result of &lt;code&gt;list_coq_files&lt;/code&gt; can be either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;None&lt;/code&gt; in case of error,&lt;/li&gt;
&lt;li&gt;some list of names in case of success.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As described in &lt;a href=&quot;http://coq-blog.clarus.me/tutorial-a-hello-world-in-coq.html&quot;&gt;Tutorial: a Hello World in Coq&lt;/a&gt;, we use the &lt;code&gt;let!&lt;/code&gt; operator to combine computations and the &lt;code&gt;ret&lt;/code&gt; operator to return a pure value. We interact with the system by calling the following functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;list_files : LString.t -&amp;gt; C (option (list LString.t))&lt;/code&gt;: list the content of a folder&lt;/li&gt;
&lt;li&gt;&lt;code&gt;log : LString.t -&amp;gt; C unit&lt;/code&gt;: print a message on the terminal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The complete list of system functions is available on &lt;a href=&quot;http://clarus.github.io/doc/io-system/Io.System.System.html&quot;&gt;system API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We continue by defining more functions and conclude with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition get_packages (repository : LString.t) : C (option Packages.t) :=
  let! names := list_coq_files repository in
  match names with
  | None =&amp;gt; ret None
  | Some names =&amp;gt; get_packages_of_names repository names
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to get the list of packages in a repository folder (or &lt;code&gt;None&lt;/code&gt; in case of error).&lt;/p&gt;

&lt;h3&gt;Second pass&lt;/h3&gt;

&lt;p&gt;The second pass follows the same structure as the first one. The main trick is the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(** Return the latest version, using Debian `dpkg` for comparison. *)
Definition max_version (version1 version2 : Version.t) : C (option Version.t) :=
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which uses the &lt;code&gt;dpkg&lt;/code&gt; command line tool to compare two versions numbers according to the &lt;a href=&quot;https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version&quot;&gt;Debian ordering&lt;/a&gt;. If you want to test it, the &lt;code&gt;dpkg&lt;/code&gt; tool should be available on most Linux distribution, even on those which are not based on &lt;a href=&quot;https://www.debian.org/&quot;&gt;Debian&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Render the HTML&lt;/h2&gt;

&lt;p&gt;We define the HTML rendering in &lt;a href=&quot;https://github.com/clarus/repos2web/blob/master/src/View.v&quot;&gt;src/View.v&lt;/a&gt;. The last function is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition index (packages : FullPackages.t) : LString.t :=
  header ++ title packages ++ table packages ++ footer.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which pretty-prints a list of packages to HTML. This function is pure (no inputs&amp;ndash;outputs), because the return type is not &lt;code&gt;C&lt;/code&gt; of something but &lt;code&gt;LString.t&lt;/code&gt;. There is nothing special about the pretty-printing, and we generate the page using the &lt;a href=&quot;http://getbootstrap.com/&quot;&gt;Bootstrap&lt;/a&gt; CSS framework to get a nice rendering.&lt;/p&gt;

&lt;p&gt;The final &lt;code&gt;main&lt;/code&gt; function in &lt;a href=&quot;https://github.com/clarus/repos2web/blob/master/src/Main.v&quot;&gt;src/Main.v&lt;/a&gt; combines the parsing and the rendering to write the output file in &lt;code&gt;html/index.html&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition main (argv : list LString.t) : C unit :=
  match argv with
  | [_; repository] =&amp;gt;
    let repository := repository ++ LString.s &amp;quot;/packages&amp;quot; in
    let! packages := Basic.get_packages repository in
    match packages with
    | None =&amp;gt; log (LString.s &amp;quot;The packages cannot be listed.&amp;quot;)
    | Some packages =&amp;gt;
      let! full_packages := Full.get_packages repository packages in
      let index_content := View.index full_packages in
      let index_name := LString.s &amp;quot;html/index.html&amp;quot; in
      let! is_success := System.write_file index_name index_content in
      if is_success then
        log (index_name ++ LString.s &amp;quot; generated.&amp;quot;)
      else
        log (LString.s &amp;quot;Cannot generate &amp;quot; ++ index_name ++ LString.s &amp;quot;.&amp;quot;)
    end
  | _ =&amp;gt; log (LString.s &amp;quot;Exactly one argument expected (the repository folder).&amp;quot;)
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We use the list of command line arguments &lt;code&gt;argv&lt;/code&gt; to get the folder in which the OPAM repository is stored. You can add a Coq-ish theme downloading this Bootstrap CSS:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;curl -L https://github.com/clarus/coq-red-css/releases/download/coq-blog.1.0.2/style.min.css &amp;gt;html/style.min.css
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Next time&lt;/h2&gt;

&lt;p&gt;We have seen how to write a script in Coq doing file manipulations. &lt;a href=&quot;http://coq-blog.clarus.me/formally-verify-a-script-in-coq.html&quot;&gt;Next time&lt;/a&gt; we will see how to specify this script and prove it correct, using a reasoning by &lt;a href=&quot;http://en.wikipedia.org/wiki/Use_case&quot;&gt;use cases&lt;/a&gt;.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/write-a-script-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/write-a-script-in-coq.html</guid>
      </item>
    
      <item>
        <title>Tutorial: a Hello World in Coq</title>
        <pubDate>Fri, 20 Feb 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;We will present the classic &lt;a href=&quot;http://en.wikipedia.org/wiki/%22Hello,_world!%22_program&quot;&gt;Hello World&lt;/a&gt; program in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;. We will explain how to compile, run, and certify interactive programs in Coq. We will use the library &lt;a href=&quot;http://coq.io/&quot;&gt;Coq.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Hello World&lt;/em&gt; program exists in almost every languages, including &lt;a href=&quot;http://en.wikipedia.org/wiki/List_of_Hello_world_program_examples#W&quot;&gt;in White Space&lt;/a&gt;. In Coq this is more complicated because the language is purely functional. This means that no effects can be done, in particular no inputs-outputs. This constraint is there to preserve the logical consistency of the system. However, we can still encode inputs-outputs by defining a &lt;a href=&quot;http://en.wikipedia.org/wiki/Monad_%28functional_programming%29&quot;&gt;monad&lt;/a&gt;. This technique was popularized by the &lt;a href=&quot;http://en.wikipedia.org/wiki/Haskell_%28programming_language%29&quot;&gt;Haskell&lt;/a&gt; programming language.&lt;/p&gt;

&lt;h2&gt;Hello World&lt;/h2&gt;

&lt;p&gt;Using &lt;a href=&quot;http://coq-blog.clarus.me/use-opam-for-coq.html&quot;&gt;OPAM for Coq&lt;/a&gt;, install the package &lt;a href=&quot;https://github.com/coq-io/system&quot;&gt;coq-io-system&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-released https://coq.inria.fr/opam/released
opam install coq-io-system
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can now write the &lt;em&gt;Hello World&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Require Import Coq.Lists.List.
Require Import Io.All.
Require Import Io.System.All.
Require Import ListString.All.

Import ListNotations.
Import C.Notations.

(** The classic Hello World program. *)
Definition hello_world (argv : list LString.t) : C.t System.effect unit :=
  System.log (LString.s &amp;quot;Hello world!&amp;quot;).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We load some libraries and write the &lt;code&gt;hello_world&lt;/code&gt; program by calling the &lt;code&gt;System.log&lt;/code&gt; function. The return type is &lt;code&gt;C.t System.effect unit&lt;/code&gt;, which means this is a &lt;em&gt;impure&lt;/em&gt; computation with &lt;em&gt;System&lt;/em&gt; effects and returning a value of type &lt;code&gt;unit&lt;/code&gt;. The command line arguments are given in the list of strings &lt;code&gt;argv&lt;/code&gt;, but not used here.&lt;/p&gt;

&lt;p&gt;To compile the &lt;code&gt;hello_world&lt;/code&gt; program we add:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition main := Extraction.launch hello_world.
Extraction &amp;quot;extraction/main&amp;quot; main.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to generate an &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; file &lt;code&gt;main.ml&lt;/code&gt;. We compile and run this file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ocamlbuild main.native -use-ocamlfind -package io-system
./main.native
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This should display &lt;code&gt;Hello world!&lt;/code&gt; on the terminal.&lt;/p&gt;

&lt;h3&gt;Specification&lt;/h3&gt;

&lt;p&gt;The specification of this program is very straightforward: &lt;em&gt;the program only prints &amp;ldquo;Hello world!&amp;rdquo; and quits&lt;/em&gt; (here the specification is as long as the program itself, but this is not always the case). A simple way to write this specification is to describe an environment of the program which reacts to a single event: the printing of the message &amp;ldquo;Hello world!&amp;rdquo; on the terminal. You can express this environment as a program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(** The Hello World program only says hello. *)
Definition hello_world_ok (argv : list LString.t) : Run.t (hello_world argv) tt.
  apply (Run.log_ok (LString.s &amp;quot;Hello world!&amp;quot;)).
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The specification &lt;code&gt;hello_world_ok&lt;/code&gt; is of type &lt;code&gt;Run.t (hello_world argv) tt&lt;/code&gt;: this runs the program &lt;code&gt;hello_world&lt;/code&gt; on an argument &lt;code&gt;argv&lt;/code&gt; and returns the result &lt;code&gt;tt&lt;/code&gt;. We just apply the function &lt;code&gt;Run.log_ok&lt;/code&gt; of the &lt;a href=&quot;https://github.com/coq-io/system&quot;&gt;coq-io-system&lt;/a&gt; library which exactly describes an environment reacting to a single printing event.&lt;/p&gt;

&lt;p&gt;This specification needs &lt;em&gt;no&lt;/em&gt; proofs because it is valid &lt;em&gt;by construction&lt;/em&gt;. We do not need any SMT solver, model checker or manual proof. The specification is valid because it is well-typed. Of course this example is very simple, so we will see a slightly more complex one.&lt;/p&gt;

&lt;h2&gt;What is your name?&lt;/h2&gt;

&lt;p&gt;This program asks for your name and replies with your name:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(** Ask for the user name and answer hello. *)
Definition your_name (argv : list LString.t) : C.t System.effect unit :=
  do! System.log (LString.s &amp;quot;What is your name?&amp;quot;) in
  let! name := System.read_line in (* Ask the name. *)
  match name with
  | None =&amp;gt; ret tt (* In case of error do nothing. *)
  | Some name =&amp;gt; System.log (LString.s &amp;quot;Hello &amp;quot; ++ name ++ LString.s &amp;quot;!&amp;quot;)
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We see here how to compose impure computations in sequence with the &lt;code&gt;do!&lt;/code&gt; and &lt;code&gt;let!&lt;/code&gt; keywords. The construct:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let! x := e1 in e2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;executes &lt;code&gt;e1&lt;/code&gt;, assigns the result to &lt;code&gt;x&lt;/code&gt; and then executes &lt;code&gt;e2&lt;/code&gt;. The &lt;code&gt;do!&lt;/code&gt; is a syntactic sugar for a &lt;code&gt;let!&lt;/code&gt; with an empty variable name. We use the &lt;code&gt;System.read_line&lt;/code&gt; function which gets a new line on the standard input. If the &lt;code&gt;read_line&lt;/code&gt; operation fails we returns the pure value &lt;code&gt;tt&lt;/code&gt; using the &lt;code&gt;ret&lt;/code&gt; operator, else we print the user name on the terminal.&lt;/p&gt;

&lt;p&gt;You can run this program as before by compilation to OCaml.&lt;/p&gt;

&lt;h3&gt;Specification&lt;/h3&gt;

&lt;p&gt;We have two &lt;a href=&quot;http://en.wikipedia.org/wiki/Use_case&quot;&gt;use cases&lt;/a&gt; for the &lt;code&gt;your_name&lt;/code&gt; program:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when the user enters a name,&lt;/li&gt;
&lt;li&gt;when the standard input is broken.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the first use case:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(** The `your_name` program answers something when you give your name. *)
Definition your_name_ok (argv : list LString.t) (name : LString.t)
  : Run.t (your_name argv) tt.
  apply (Run.Let (Run.log_ok _)).
  apply (Run.Let (Run.read_line_ok name)).
  apply (Run.log_ok _).
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The specification is parametrized by a &lt;code&gt;name&lt;/code&gt; which could be any string. The environment does exactly three steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;display something on the terminal (the underscore &lt;code&gt;_&lt;/code&gt; means any value)&lt;/li&gt;
&lt;li&gt;answer &lt;code&gt;name&lt;/code&gt; to the event &lt;code&gt;read_line&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;display something on the terminal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;Run.Let&lt;/code&gt; command composes two steps. For a failing standard input:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(** The `your_name` program does nothing more in case of error on stdin. *)
Definition your_name_error (argv : list LString.t) : Run.t (your_name argv) tt.
  apply (Run.Let (Run.log_ok _)).
  apply (Run.Let Run.read_line_error).
  apply Run.Ret.
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we also give three steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;display something on the terminal&lt;/li&gt;
&lt;li&gt;answer by an error to the event &lt;code&gt;read_line&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;terminate (the command &lt;code&gt;Run.Ret&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again, we do not need to prove anything because the specifications are correct by construction.&lt;/p&gt;

&lt;p&gt;Next time we will continue with more tutorials, to help you build and certify realistic interactive applications in Coq.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/tutorial-a-hello-world-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/tutorial-a-hello-world-in-coq.html</guid>
      </item>
    
      <item>
        <title>A bench system for the Coq packages</title>
        <pubDate>Tue, 17 Feb 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;We will present the &lt;a href=&quot;http://coq-bench.github.io/&quot;&gt;bench system&lt;/a&gt; for the &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; packages.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://opam.ocamlpro.com/&quot;&gt;OPAM&lt;/a&gt; is the package manager for Coq. The number of packages is growing, so we needed a bench system to automatically check that all packages are compiling. The bench system will help both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;packages developers, to check that their programs are compiling with correct dependency constraints;&lt;/li&gt;
&lt;li&gt;Coq developers, to monitor the changes breaking compatibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Use the bench&lt;/h2&gt;

&lt;p&gt;The results of the bench are available on &lt;a href=&quot;http://coq-bench.github.io/&quot;&gt;coq-bench.github.io&lt;/a&gt;, using the two installation strategies &lt;code&gt;clean&lt;/code&gt; and &lt;code&gt;tree&lt;/code&gt;. The results are presented in a colored table with the installation times for valid packages. The &lt;code&gt;best&lt;/code&gt; column contains the best score obtained for each package.&lt;/p&gt;

&lt;p&gt;We check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;that packages are well-formed, using our &lt;a href=&quot;https://github.com/coq-bench/run/blob/master/lint.rb&quot;&gt;lint&lt;/a&gt; checker&lt;/li&gt;
&lt;li&gt;the installation of dependencies (with a timeout of 5 minutes)&lt;/li&gt;
&lt;li&gt;the installation of the package (with a timeout of 30 minutes)&lt;/li&gt;
&lt;li&gt;the removing of the package (by comparing the list of files before installation and after removing)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We record the duration of each operation and compute the installation size.&lt;/p&gt;

&lt;p&gt;Because your dependencies may evolve, it is good practice to regularly check the bench results for your packages. The &lt;code&gt;clean&lt;/code&gt; strategy installs each package in a clean environment whereas the &lt;code&gt;tree&lt;/code&gt; strategy installs as many packages as possible before to remove incompatible ones. In practice, the &lt;code&gt;tree&lt;/code&gt; strategy yields more errors because packages are tested in an environment polluted by many other packages.&lt;/p&gt;

&lt;h2&gt;Architecture&lt;/h2&gt;

&lt;p&gt;The bench system is hosted on GitHub in the organization &lt;a href=&quot;https://github.com/coq-bench&quot;&gt;coq-bench&lt;/a&gt;. There are four projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/coq-bench/run&quot;&gt;run&lt;/a&gt;: run the benchmarks&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/coq-bench/database&quot;&gt;database&lt;/a&gt;: the backup of the benchmarks&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/coq-bench/make-html&quot;&gt;make-html&lt;/a&gt;: generate the web pages&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/coq-bench/coq-bench.github.io&quot;&gt;coq-bench.github.io&lt;/a&gt;: the website itself&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;&lt;a href=&quot;https://github.com/coq-bench/run&quot;&gt;run&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;This program runs the benchmarks and is written in Ruby. The entry-point is &lt;a href=&quot;https://github.com/coq-bench/run/blob/master/long_run.rb&quot;&gt;long_run.rb&lt;/a&gt;. For each bench a clean &lt;a href=&quot;https://www.docker.com/&quot;&gt;Docker&lt;/a&gt; image is generated from the parametrized &lt;a href=&quot;https://github.com/coq-bench/run/blob/master/Dockerfile.erb&quot;&gt;Dockerfile.erb&lt;/a&gt;. Then OCaml, OPAM and Coq are installed, and each package is tested. The packages can be from the stable repository or both the stable and unstable repositories. The results are then saved into the database.&lt;/p&gt;

&lt;h3&gt;&lt;a href=&quot;https://github.com/coq-bench/database&quot;&gt;database&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;The database is in the &lt;a href=&quot;http://en.wikipedia.org/wiki/Comma-separated_values&quot;&gt;CSV&lt;/a&gt; format. There is one file per bench and one row per package. The first row gives a legend for each of the 31 columns.&lt;/p&gt;

&lt;h3&gt;&lt;a href=&quot;https://github.com/coq-bench/make-html&quot;&gt;make-html&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;This program generates the static HTML pages from the CSV database and is written in Ruby. For example, to generate the pages of the &lt;code&gt;clean&lt;/code&gt; results:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ruby make_html.rb ../database/clean html/clean
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;&lt;a href=&quot;https://github.com/coq-bench/coq-bench.github.io&quot;&gt;coq-bench.github.io&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;These are the static HTML pages of the bench website. &lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt; provides us a nice and simple way to host web pages from a Git repository using &lt;a href=&quot;https://pages.github.com/&quot;&gt;GitHub Pages&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Strategies&lt;/h2&gt;

&lt;p&gt;There are many possible strategies with respect to the installation order of the packages. The installation order is important because OPAM installs different dependencies in different contexts.&lt;/p&gt;

&lt;p&gt;We would like to optimize the installation order to reduce the total execution time of the bench, by always installing and testing the dependencies first, so that no packages are compiled twice. Unfortunately this is not possible. Here is a simple counter example. With the following list of packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A.1.0.0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;A.2.0.0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;B.1.0.0&lt;/code&gt; depending on &lt;code&gt;A&lt;/code&gt; (any versions)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;C.1.0.0&lt;/code&gt; depending on &lt;code&gt;A.1.0.0&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;C.2.0.0&lt;/code&gt; depending on &lt;code&gt;A.2.0.0&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we must compile &lt;code&gt;B&lt;/code&gt; twice (once with &lt;code&gt;A.1.0.0&lt;/code&gt; and once with &lt;code&gt;A.2.0.0&lt;/code&gt;) to test both &lt;code&gt;C.1.0.0&lt;/code&gt; and &lt;code&gt;C.2.0.0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We provide the two following strategies.&lt;/p&gt;

&lt;h3&gt;Clean&lt;/h3&gt;

&lt;p&gt;This is the simplest strategy. We install each package in a fresh environment. This is the most robust and reproducible strategy. But this is not really optimal for packages with big dependencies because they are always reinstalled from scratch.&lt;/p&gt;

&lt;h3&gt;Tree&lt;/h3&gt;

&lt;p&gt;This is a more complex strategy. We install as many packages as possible until all new packages are incompatible with the current environment. The main source of incompatibility is the fact that we cannot install two packages with the same name but different version numbers. Once we are blocked, we roll-back until new packages are installable.&lt;/p&gt;

&lt;p&gt;This strategy is more clever but also more fragile. We use the branch mechanism of Git on the &lt;code&gt;.opam&lt;/code&gt; folder to switch efficiently between OPAM states. At the end of the process, we obtain a tree of all the Git branches used to explore the packages space. For example, for the stable repository:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;* b99f693 coq-concurrency-pluto.1.0.0
* a1ff2f1 coq-concurrency-system.1.0.0
* 27c7b93 coq-moment.1.0.0
* d950f53 coq-list-string.2.0.0
| * 4817264 coq-flocq.2.2.0
| | * 3d4105d coq-flocq.2.3.0
| |/  
| | * 544de40 coq-concurrency-proxy.1.0.0
| | * 9a1989e coq-coqeal:refinements.0.9.1
| | * 0bfd44f coq-fpmods.0.2.0
| | * bda2af7 coq-coqeal:theory.0.9.1
| | * 27aa320 coq-plouffe.1.0.0
| | * 35961d5 coq-coquelicot.2.0.1
| | * 89db498 coq-error-handlers.1.0.0
| | * aa85f8c coq-flocq.2.4.0
| |/  
| * 48f33a4 coq-iterable.1.0.0
| * 0a48735 coq-function-ninjas.1.0.0
| * e2980d1 coq-list-plus.1.0.0
| * 4122f1a coq-list-string.1.0.0
|/  
* 89705ad coq-math-classes.1.0.2
* 5d3ec5d coq-math-comp.1.5.0
* 470eefb coq-ssreflect.1.5.0
* b0205c8 Initial files.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Related work&lt;/h2&gt;

&lt;p&gt;The OPAM for OCaml community did some work to obtain a bench system too. There are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://ows.irill.org/&quot;&gt;OPAM Weather Service&lt;/a&gt;: do not install the packages, only check the dependency constraints&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.recoil.org/%7Eavsm/opam-bulk/&quot;&gt;OPAM Bulk&lt;/a&gt;: a future bench system&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/ocamllabs/ocamlot&quot;&gt;OCamlot&lt;/a&gt;: a bench system (abandoned?)&lt;/li&gt;
&lt;/ul&gt;
</description>
        <link>https://rocq-blog.clarus.me/a-bench-system-for-the-coq-packages.html</link>
        <guid>https://rocq-blog.clarus.me/a-bench-system-for-the-coq-packages.html</guid>
      </item>
    
      <item>
        <title>A blog engine written and proven in Coq</title>
        <pubDate>Fri, 16 Jan 2015 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;I present ChickBlog (&lt;a href=&quot;https://github.com/clarus/coq-chick-blog&quot;&gt;sources&lt;/a&gt; on GitHub), a blog engine written and proven in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is a demo blog engine where a user can login (no passwords), add, edit or delete posts. The code is written mostly in Coq, compiled to &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; and linked to the &lt;a href=&quot;https://github.com/mirage/ocaml-cohttp&quot;&gt;CoHTTP&lt;/a&gt; library to handle the HTTP protocol.&lt;/p&gt;

&lt;p&gt;The aim of this project is to demonstrate that applications with I/Os can be written and specified naturally using the (new) concept of &lt;a href=&quot;http://coq-blog.clarus.me/checking-concurrent-programs-with-symbolic-simulations.html&quot;&gt;symbolic simulations in Coq&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Run&lt;/h2&gt;

&lt;p&gt;Get the sources:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone https://github.com/clarus/coq-chick-blog.git
cd coq-chick-blog/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Add the Coq repositories with &lt;a href=&quot;https://opam.ocaml.org/&quot;&gt;OPAM&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-released https://coq.inria.fr/opam/released
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Install the dependencies:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam install --jobs=4 coq-list-string coq-error-handlers coq-function-ninjas coq-moment
opam install --jobs=4 lwt cohttp
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Download the CSS:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;curl -L https://github.com/clarus/coq-red-css/releases/download/coq-blog.1.0.2/style.min.css &amp;gt;extraction/static/style.min.css
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./configure.sh &amp;amp;&amp;amp; make
cd extraction/
make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Run on &lt;a href=&quot;http://localhost:8008/&quot;&gt;localhost:8008&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./chickBlog.native
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Dockerfile&lt;/h3&gt;

&lt;p&gt;A Dockerfile is provided to run ChickBlog in an isolated environment using &lt;a href=&quot;https://www.docker.com/&quot;&gt;Docker&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;docker build --tag=chick-blog .
docker run -ti -p 8008:8008 chick-blog
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Specification&lt;/h2&gt;

&lt;p&gt;The blog is defined in &lt;code&gt;Main.v&lt;/code&gt; as the function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition server (path : Path.t) (cookies : Cookies.t) : C.t Response.t.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It handles an HTTP request and generate an answer using system calls to the file system. The type &lt;code&gt;C.t A&lt;/code&gt; represents a computation doing I/O operations:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive t (A : Type) : Type :=
| Ret : forall (x : A), t A
| Call : forall (command : Command.t), (Command.answer command -&amp;gt; t A) -&amp;gt; t A.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A computation can either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;return a pure value of type &lt;code&gt;A&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;call an external command and wait for its result&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The purity of Coq ensures that each request is answered exactly once in finite time. We specify the behavior of the server in &lt;code&gt;Spec.v&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Scenarios&lt;/h3&gt;

&lt;p&gt;A scenario is a set of runs of the server. A type-checking scenario shows that the server behaves as expected in a certain use case. For example, we check that when we create, edit and view a post we get the same result as what we entered. You can think of a scenario as a unit test with universally quantified variables.&lt;/p&gt;

&lt;p&gt;Here is a simple check of the execution of the index page:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(** The index page when the list of posts is available. *)
Definition index_ok (cookies : Cookies.t) (post_headers : list Post.Header.t)
  : Run.t (Main.server Path.Index cookies).
  (* The handler asks the list of available posts. We return `post_headers`. *)
  apply (Call (Command.ListPosts _ ) (Some post_headers)).
  (* The handler terminates without other system calls. *)
  apply (Ret (Response.Index (Cookies.is_logged cookies) post_headers)).
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Given any &lt;code&gt;cookies&lt;/code&gt; and &lt;code&gt;post_headers&lt;/code&gt;, we execute the server handler on the page &lt;code&gt;Request.Path.Index&lt;/code&gt;. The handler does exactly one system call, to which we answer &lt;code&gt;Some post_headers&lt;/code&gt;, playing the role of the system. The final response of the server is then &lt;code&gt;Response.Public.Index post_headers&lt;/code&gt;. Note that we do not need to execute &lt;code&gt;index_ok&lt;/code&gt; on every instances of &lt;code&gt;cookies&lt;/code&gt; and &lt;code&gt;post_headers&lt;/code&gt;: since the type-system of Coq is supposed sound, it is enough to type-check &lt;code&gt;index_ok&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Privacy&lt;/h3&gt;

&lt;p&gt;We check that, for any runs of a program, an unauthenticated user cannot access private pages (like edit) or modify the file system with system calls.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;More comments on &lt;a href=&quot;https://news.ycombinator.com/item?id=9037115&quot;&gt;Hacker News&lt;/a&gt;, &lt;a href=&quot;https://www.reddit.com/r/programming/comments/2vn3xa/a_blog_engine_written_and_proven_in_coq/&quot;&gt;Reddit&lt;/a&gt; and &lt;a href=&quot;https://lobste.rs/s/7ugonf/a_blog_engine_written_and_proven_in_coq/comments/4oholx&quot;&gt;Lobsters&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/a-blog-engine-written-and-proven-in-coq.html</link>
        <guid>https://rocq-blog.clarus.me/a-blog-engine-written-and-proven-in-coq.html</guid>
      </item>
    
      <item>
        <title>Checking concurrent programs with symbolic simulations</title>
        <pubDate>Thu, 18 Dec 2014 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;Concurrent programs with inputs/outputs are hard to test: they are non-deterministic and interact with the outside world. They are even harder to formally specify completely, and prove correct. We present (what we believe to be) a novel approach to check such programs doing symbolic simulations in &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Symbolic simulations are like deterministic simulations one concrete data, using symbolic values to run and check the results of a &lt;em&gt;full set&lt;/em&gt; of simulations. Here is a good talk about testing real systems by simulation: &lt;a href=&quot;https://foundationdb.com/videos/testing-distributed-systems-with-deterministic-simulation&quot;&gt;Testing Distributed Systems with Deterministic Simulation&lt;/a&gt;. We use the Coq&amp;rsquo;s pure programming language Gallina to describe the behavior of a program&amp;rsquo;s environment, which can include the user, the operating system, other connected computers, &amp;hellip; The environment is responsible for two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;feeding data to the program&lt;/li&gt;
&lt;li&gt;checking that its answers verify some properties&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We hope this approach will provide a good compromise between full specifications and testing on single data instances. We will define a simple concurrent calculus and give some examples.&lt;/p&gt;

&lt;p&gt;All the following code extracts are in Coq and available on &lt;a href=&quot;https://github.com/coq-concurrency/system/tree/simulation-callbacks&quot;&gt;coq-concurrency/system#simulation-callbacks&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Calculus with handlers&lt;/h2&gt;

&lt;p&gt;To allow simpler reasoning, we want our calculus to be somehow &amp;ldquo;deterministic&amp;rdquo;. A first way is to remove shared mutable variables. Then the only source of non-determinism is the order of the input events. Having shared mutable states is often useful, typically to implement a data storage. Still, we will try to go as far as possible without mutable states.&lt;/p&gt;

&lt;p&gt;An second way to reduce non-determinism is to restrict how we are waiting for events and doing threads. We define a calculus with only asynchronous calls and forks (without joins):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive t : Type :=
| Ret : t
| Par : t -&amp;gt; t -&amp;gt; t
| Send : forall (command : Command.t), Command.request command -&amp;gt;
  (Command.answer command -&amp;gt; t) -&amp;gt; t.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A computation can do three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Ret&lt;/code&gt;: do nothing&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Par c1 c2&lt;/code&gt;: start &lt;code&gt;c1&lt;/code&gt; and &lt;code&gt;c2&lt;/code&gt; in parallel&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Send command request (fun answer =&amp;gt; c)&lt;/code&gt;: emit a request &lt;code&gt;request&lt;/code&gt; of kind &lt;code&gt;command&lt;/code&gt; with a handler for the answer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the classical HelloWorld:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition hello_world : C.t :=
  do! Command.Write @ LString.s &amp;quot;Hello world!&amp;quot; in
  C.Ret.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We use the notation &lt;code&gt;do! command @ request in e&lt;/code&gt; for &lt;code&gt;Send command request (fun _ =&amp;gt; e)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is a simple echo program repeating one user input:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition echo : C.t :=
  let! message := Command.Read @ tt in
  do! Command.Write @ message in
  C.Ret.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;let!&lt;/code&gt; is like the &lt;code&gt;do!&lt;/code&gt; but naming the argument of the handler.&lt;/p&gt;

&lt;p&gt;These examples are sequential programs. Note that in practice they may not terminate, if the operating system never answers to the requests. There can be at most one answer per request. We use a fixpoint and the &lt;code&gt;Par&lt;/code&gt; operator to handle several requests in parallel:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Fixpoint echo_par (fuel : nat) : C.t :=
  match fuel with
  | O =&amp;gt; C.Ret
  | S fuel =&amp;gt; C.Par echo (echo_par fuel)
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This program concurrently waits for &lt;code&gt;fuel&lt;/code&gt; user inputs, prints them and stops.&lt;/p&gt;

&lt;h2&gt;Simulations&lt;/h2&gt;

&lt;p&gt;A simulation, or a run, is a co-program over a concurrent and interactive program. It answers to the requests of the program, playing the role of the environment. A simulation is defined by induction over the program&amp;rsquo;s structure. This has two advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;by construction, a simulation must give exactly one answer per request&lt;/li&gt;
&lt;li&gt;you can construct the simulation following the structure of the program&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The type of a simulation is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Inductive t : C.t -&amp;gt; Type :=
| Ret : t C.Ret
| Par : forall {c1 c2 : C.t}, t c1 -&amp;gt; t c2 -&amp;gt; t (C.Par c1 c2)
| Send : forall (command : Command.t) (request : Command.request command)
  (answer : Command.answer command) {handler : Command.answer command -&amp;gt; C.t},
  t (handler answer) -&amp;gt; t (C.Send command request handler).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For convenience, we use the tactic mode of Coq to build simulations. Doing so, we get a kind of interactive and symbolic debugger for our programs. This helps writing meaningful simulations. Here is the simulation of HelloWorld:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition hello_world_run : Run.t hello_world.
  apply (Run.Send Command.Write (LString.s &amp;quot;Hello world!&amp;quot;) tt).
  exact Run.Ret.
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This simulation checks that the program &lt;code&gt;hello_world&lt;/code&gt; does exactly one thing: sending the message &lt;code&gt;&amp;quot;Hello world!&amp;quot;&lt;/code&gt;. For the echo of one message:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition run_echo (message : LString.t) : Run.t echo.
  apply (Run.Send Command.Read tt message).
  apply (Run.Send Command.Write message tt).
  exact Run.Ret.
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We check that, for any message entered by the user, the program will only print this message. Given a list of messages, we can construct a set of simulations for the concurrent echo:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Fixpoint run_echo_par (messages : list LString.t)
  : Run.t (echo_par (List.length messages)).
  destruct messages as [|message messages].
  - exact Run.Ret.
  - apply Run.Par.
    * exact (run_echo message).
    * exact (run_echo_par messages).
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This simulation is recursive to follow the shape of the simulated program. It reuses the simulation &lt;code&gt;run_echo&lt;/code&gt; of &lt;code&gt;echo&lt;/code&gt;. The simulation is defined for any order of interleaving of the message events.&lt;/p&gt;

&lt;h2&gt;Time server&lt;/h2&gt;

&lt;p&gt;In the file &lt;a href=&quot;https://github.com/coq-concurrency/system/blob/simulation-callbacks/Simulation.v&quot;&gt;Simulation.v&lt;/a&gt; we give an example of a simple time server. The clients can connect and get the current time of the server. Unlike for the previous examples, we do not cover all possible runs. Instead we give two sets of simulations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a first one where the server socket cannot be bound&lt;/li&gt;
&lt;li&gt;a second one where both the server and clients sockets never return an error&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We do not cover all the cases, for example the case of a client socket which cannot be written to. We believe this is both a weakness and a strength of this approach. Contrary to a full specification, not all the execution paths are tested. But simulations are simple to write, while covering more cases than traditional tests.&lt;/p&gt;

&lt;h2&gt;Future work&lt;/h2&gt;

&lt;p&gt;We want to extend this method to check more complex programs, like a database or a chat-server. For that we should first extend the expressiveness of the calculus, and so of the simulations.&lt;/p&gt;

&lt;p&gt;We also want to relate this method to the specifications plus proofs approach. A first way could be to have simulations which &lt;em&gt;by construction&lt;/em&gt; cover all the execution paths. An other way could be to express properties over simulations to enforce a specification over any simulations.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/checking-concurrent-programs-with-symbolic-simulations.html</link>
        <guid>https://rocq-blog.clarus.me/checking-concurrent-programs-with-symbolic-simulations.html</guid>
      </item>
    
      <item>
        <title>Pluto: a first concurrent web server in Gallina</title>
        <pubDate>Thu, 04 Dec 2014 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;&lt;a href=&quot;https://github.com/coq-concurrency/pluto&quot;&gt;Pluto&lt;/a&gt; is, to our knowledge, the first concurrent web server written in Gallina, the functional language of &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is a research project which aims to apply the pure and dependently typed &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; language to system programming, with inputs/outputs and fine grained concurrency in mind. This kind of programming is particularly error-prone and hard to test, due to non-determinism and interactions with the external world. Moreover, such programs (like servers and databases) often manipulate critical data. We try to develop new programming techniques with an extremist and purely functional approach, in the hope to lead to safer systems.&lt;/p&gt;

&lt;p&gt;For now &lt;a href=&quot;https://github.com/coq-concurrency/pluto&quot;&gt;Pluto&lt;/a&gt; can serve static websites, using event-based I/Os and lightweight threads to handle concurrent requests.&lt;/p&gt;

&lt;h2&gt;Use&lt;/h2&gt;

&lt;p&gt;The simplest way to install &lt;a href=&quot;https://github.com/coq-concurrency/pluto&quot;&gt;Pluto&lt;/a&gt; is to use &lt;a href=&quot;http://opam.ocamlpro.com/&quot;&gt;OPAM&lt;/a&gt; for Coq. See this &lt;a href=&quot;http://coq-blog.clarus.me/use-opam-for-coq.html&quot;&gt;tutorial&lt;/a&gt; for more informations. Add the stable and unstable repositories:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-released https://coq.inria.fr/opam/released
opam repo add coq-extra-dev https://coq.inria.fr/opam/extra-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Install Pluto:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam install --jobs=4 coq-concurrency-pluto
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Run it on some &lt;code&gt;html/&lt;/code&gt; folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pluto.native 8000 html/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your website is now available on &lt;a href=&quot;http://localhost:8000/&quot;&gt;localhost:8000&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Architecture&lt;/h2&gt;

&lt;p&gt;Coq is a pure language so it cannot directly express concurrency and I/Os. For that we use a Domain Specific Language (DSL) with new primitive constructs to describe impure computations. The architecture is implemented in the &lt;a href=&quot;https://github.com/coq-concurrency/system&quot;&gt;coq-concurrency/system&lt;/a&gt; project.&lt;/p&gt;

&lt;h3&gt;Operators&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Read&lt;/code&gt; and &lt;code&gt;Write&lt;/code&gt; commands read or update atomically global references (shared by all the threads). &lt;code&gt;Ret&lt;/code&gt; lifts a pure Coq expression, &lt;code&gt;Bind&lt;/code&gt; sequences two computations. The &lt;code&gt;Send&lt;/code&gt; constructor does an asynchronous call to the OS. It uses a handler with its own private memory (a lightweight thread), called each time an answer is sent to the request. The &lt;code&gt;Exit&lt;/code&gt; command halts the program and stops all pending handlers.&lt;/p&gt;

&lt;p&gt;We decided to use fully asynchronous I/Os with lightweight threads for two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it is generally considered more efficient than the synchronous system-calls plus system-threads model (see the evolution from the &lt;a href=&quot;http://www.apache.org/&quot;&gt;Apache&lt;/a&gt; 1 multi-threaded server to mono-threaded event-driven systems like &lt;a href=&quot;http://nodejs.org/&quot;&gt;Node.js&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;it corresponds more to what computers intrinsically are: the most primitive communication facilities on microprocessors are the &lt;a href=&quot;http://x86.renejeschke.de/html/file_module_x86_id_222.html&quot;&gt;OUT instruction&lt;/a&gt; and the &lt;a href=&quot;http://en.wikipedia.org/wiki/Interrupt&quot;&gt;interruption mechanism&lt;/a&gt;. The &lt;a href=&quot;http://en.wikipedia.org/wiki/Direct_memory_access&quot;&gt;Direct Memory Access&lt;/a&gt; is a fastest solution in practice, but also relies on these primitives. Finally, this corresponds to the &lt;a href=&quot;http://openmirage.org/wiki/xen-events&quot;&gt;Xen API&lt;/a&gt; design, in the hope that some day Coq could be ported as an unikernel like OCaml with &lt;a href=&quot;http://www.openmirage.org/&quot;&gt;MirageOS&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Implementation&lt;/h3&gt;

&lt;p&gt;The implementation of this DSL is two folds. In Coq, a &lt;code&gt;run&lt;/code&gt; function gives an executable semantics of the computations. The existence of an executable semantics is an improvement over other works which only give unrealized axioms for I/Os effects (for example in Haskell or in Idris, see &lt;a href=&quot;https://github.com/idris-lang/Idris-dev/blob/master/libs/prelude/IO.idr&quot;&gt;IO.idr&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;We also compile Coq programs to OCaml using a customized version of the &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/%7Eletouzey/download/letouzey_extr_cie08.pdf&quot;&gt;extraction mechanism&lt;/a&gt; of Coq. The impure operators are compiled to impure OCaml operators realizing the effects, like sending messages to the OS.&lt;/p&gt;

&lt;p&gt;The impure effects can be classified into three categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;memory&lt;/li&gt;
&lt;li&gt;exit&lt;/li&gt;
&lt;li&gt;asynchronous calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The memory and exit effects are implemented, both in Coq and OCaml, by the monadic transformations of the state and exception monads (see &lt;a href=&quot;http://gallium.inria.fr/%7Exleroy/mpri/progfunc/monads.2up.pdf&quot;&gt;monadic transformations&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In Coq, the asynchronous calls are represented by a monad reader and a monad writer. The system-calls are typed, and to each request type corresponds one answer type. The handlers are stored into a heap and must have a type compatible with their request. They have their own private memory (implemented as a state monad), and can be called an unbounded number of times (for example, with a web server, the socket listener will get as many inputs as connecting clients). A handler can also disconnect itself to be garbage collected.&lt;/p&gt;

&lt;p&gt;In OCaml, the handlers are stored in the heap extracted from the Coq implementation. Communication with the OS occurs through a unique bidirectional pipe. The &lt;code&gt;Send&lt;/code&gt; method writes a message on the pipe. A single loop listens synchronously to the pipe, and dispatches the events to the handlers. All messages are serialized into strings. At the other end of the pipe sits a &lt;a href=&quot;https://github.com/coq-concurrency/proxy&quot;&gt;proxy&lt;/a&gt;. The proxy parses the messages and translates them into real system-calls. It is implemented in OCaml using &lt;a href=&quot;http://ocsigen.org/lwt/&quot;&gt;Lwt&lt;/a&gt;. Note that Lwt is not used as a lightweight threads library, but as an asynchronous API to Unix. For a more technical discussion about asynchronous system-calls in Unix, you can read this &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/%7Ejch/research/cpc-2012.pdf&quot;&gt;paper&lt;/a&gt; of Kerneis and Chroboczek.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;static/images/pluto_runtime.svg&quot; alt=&quot;Schema&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Implementation architecture&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;h3&gt;Correction&lt;/h3&gt;

&lt;p&gt;There is no mechanism to prove properties about the effects yet (I/Os, shared memory, &amp;hellip;). However it is possible to certify the functional part of the server, or any interactive Coq application based on our runtime, using standard techniques in Coq (see for example the &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/%7Esozeau/research/publications/Program-ing_Finger_Trees_in_Coq.pdf&quot;&gt;Program extension&lt;/a&gt; of Matthieu Sozeau).&lt;/p&gt;

&lt;p&gt;We can also question the correctness of the compilation to OCaml, or whether the &lt;code&gt;run&lt;/code&gt; function in Coq is faithful to the extracted code. We have no formal proof, but we designed the system with correctness in mind. In particular, we designed our DSL with a minimal &amp;ldquo;attack surface&amp;rdquo;. The memory, the lightweight threads, the exit effect are compiled by monadic transformation in Coq and then using the standard extraction mechanism. For I/Os, there is no abstract &lt;code&gt;World&lt;/code&gt; type which could lead to duplication in Coq. We just use a monad reader and monad writer, and messages are sent or read into a pipe. On the long term, we could dream of a formally proven Coq compiler and adapt it to our customized compilation of read/write effects.&lt;/p&gt;

&lt;h2&gt;Code extracts&lt;/h2&gt;

&lt;p&gt;This is the main function of the server, in &lt;a href=&quot;https://github.com/coq-concurrency/pluto/blob/master/Pluto.v&quot;&gt;pluto/Pluto.v&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition program (argv : list LString.t) : C.t [] unit :=
  match argv with
  | [_; port; website_dir] =&amp;gt;
    match LString.to_N 10 port with
    | None =&amp;gt; print_usage tt
    | Some port_number =&amp;gt;
      Time.get (fun time =&amp;gt;
      let time := Moment.Print.rfc1123 @@ Moment.of_epoch @@ Z.of_N time in
      let welcome_message := LString.s &amp;quot;Pluto starting on &amp;quot; ++ website_dir ++
        LString.s &amp;quot;, port &amp;quot; ++ port ++
        LString.s &amp;quot;, on &amp;quot; ++ time ++ LString.s &amp;quot;.&amp;quot; in
      Log.write welcome_message (fun _ =&amp;gt;
      ServerSocket.bind port_number (fun client =&amp;gt;
        match client with
        | None =&amp;gt;
          Log.write (LString.s &amp;quot;Server socket failed.&amp;quot;) (fun _ =&amp;gt; C.Exit tt)
        | Some client =&amp;gt; handle_client website_dir client
        end)))
    end
  | _ =&amp;gt; print_usage tt
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The program is parametrized by the list of the command-line arguments. The return type is &lt;code&gt;C.t [] unit&lt;/code&gt;, the type of computations returning &lt;code&gt;unit&lt;/code&gt; with an empty shared memory (&lt;code&gt;[]&lt;/code&gt; means that the list of memory cells is empty).&lt;/p&gt;

&lt;p&gt;We first check that there are two arguments, the first one being an integer (the port number). Else we print a usage message and exit. Then we request the current time to log it. The definition of &lt;code&gt;Time.get&lt;/code&gt; is in &lt;a href=&quot;https://github.com/coq-concurrency/system/blob/master/StdLib.v&quot;&gt;system/StdLib.v&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(** Get the current time (the number of seconds since Unix epoch). *)
Definition get {sig : Signature.t} (handler : N -&amp;gt; C.t sig unit)
  : C.t sig unit :=
  C.Send Command.Time tt tt (fun _ time =&amp;gt;
    do! handler time in
    C.Ret None).
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The function &lt;code&gt;get&lt;/code&gt; is parametrized by a handler &lt;code&gt;N -&amp;gt; C.t sig unit&lt;/code&gt; which is a computation receiving the time in seconds. The &lt;code&gt;handler&lt;/code&gt; is called when the OS responds to the &lt;code&gt;C.Send&lt;/code&gt; operator, calling the callback &lt;code&gt;fun _ time =&amp;gt; do! handler time in C.Ret None&lt;/code&gt;. This callback has no private memory (actually a private memory of type &lt;code&gt;unit&lt;/code&gt; initialized to &lt;code&gt;tt&lt;/code&gt;). The callback always returns &lt;code&gt;None&lt;/code&gt; to disconnect itself after an answer (so that the handler is called at most once, even if the OS is bugged and responds several times to the &lt;code&gt;Command.Time&lt;/code&gt; request).&lt;/p&gt;

&lt;p&gt;We log the current time in RFC 1123 format with a welcome message and bind to the server socket. The handler is listening while there are new clients connecting, and runs the &lt;code&gt;handle_client&lt;/code&gt; method for each. This program may never terminate, while we are writing pure Coq without explicit non-termination monad. The non-termination effect is provided by the monad reader, which is compiled to OCaml as an infinite loop listening to the system pipe.&lt;/p&gt;

&lt;h2&gt;Future work&lt;/h2&gt;

&lt;p&gt;We learn many things writing a realistic example of a web server in Coq, especially in the way of implementing side effects. We also shown that it is possible to use Coq as a programming language to write interactive and concurrent softwares.&lt;/p&gt;

&lt;p&gt;Our main goal now is to extend our DSL of computations with a specification and a certification mechanism. We would like to write a specification of the non-purely functional part of our web server and prove its correctness, exploiting the unique ability of Coq to marry proofs and programs.&lt;/p&gt;

&lt;p&gt;Note: &lt;em&gt;Pluto is also the only planet discovered and undiscovered by the Americans. The &lt;a href=&quot;http://en.wikipedia.org/wiki/New_Horizons&quot;&gt;New Horizons&lt;/a&gt; space probe should give us more insights about this mysterious object.&lt;/em&gt;&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/pluto-a-first-concurrent-web-server-in-gallina.html</link>
        <guid>https://rocq-blog.clarus.me/pluto-a-first-concurrent-web-server-in-gallina.html</guid>
      </item>
    
      <item>
        <title>Why and how to write code compatible with many Coq versions</title>
        <pubDate>Wed, 03 Dec 2014 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;You should always write your code to make it compatible with at least two consecutive Coq versions, if you wish to have larger people adoption.&lt;/p&gt;

&lt;p&gt;The pace of Coq releases is surprisingly slow for a project with that many users, unfortunately due to a lack of organization resources. However, the releases happen and usually introduce a lot of incompatibilities. At this point, users depending on your libraries may be forced to migrate because of other dependencies, or forced not to migrate. Playing nice with them means allowing them to have the choice.&lt;/p&gt;

&lt;p&gt;More important, a lot of people prefer to use the development version of Coq because of the slow release cycle. So your code should at least support the latest stable release and the current development version.&lt;/p&gt;

&lt;h2&gt;Check your code&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://opam.ocamlpro.com/&quot;&gt;OPAM&lt;/a&gt; is the best way to test your code with different versions of Coq. You can read this &lt;a href=&quot;http://coq-blog.clarus.me/use-opam-for-coq.html&quot;&gt;introduction&lt;/a&gt; to learn more about how to use OPAM for Coq.&lt;/p&gt;

&lt;p&gt;Let us say we want to test our project &lt;code&gt;foo&lt;/code&gt; with the Coq versions &lt;code&gt;8.4.5&lt;/code&gt; and &lt;code&gt;dev&lt;/code&gt;. We create two installs of Coq in &lt;code&gt;foo/opam.8.4.5&lt;/code&gt; and &lt;code&gt;foo/opam.dev&lt;/code&gt;. For the stable version:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd foo/
mkdir opam.8.4.5 # we create the empty directory to prevent a bug of OPAM
opam init --root=opam.8.4.5
eval `opam config --root=opam.8.4.5 env`
opam install --jobs=4 coq.8.4.5
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In an other terminal, for the unstable Coq:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir opam.dev
opam init --root=opam.dev
eval `opam config --root=opam.dev env`
opam repo add coq-core-dev https://coq.inria.fr/opam/core-dev
opam install -j4 -v coq.dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You have now a different version of Coq in each terminal, and can test your code for two versions. If you have an old computer, and may be afraid of having many Coq installations for each project, remember you can always use cache mechanisms like the one provided by &lt;a href=&quot;https://www.docker.com/&quot;&gt;Docker&lt;/a&gt; to save disk space.&lt;/p&gt;

&lt;p&gt;We also provide a &lt;a href=&quot;http://coq-bench.github.io/&quot;&gt;coq-bench&lt;/a&gt; website, where OPAM packages are tested for different versions of Coq. This is another simple way to check your code compatibility if you have a package.&lt;/p&gt;

&lt;h2&gt;Be clean and robust&lt;/h2&gt;

&lt;p&gt;This is obvious but this must be emphasized: be clean and robust. Most Coq features outside the kernel can be considered as experimental, or do not have a clear semantics. This includes in particular the tactic language &lt;a href=&quot;https://coq.inria.fr/distrib/V8.4pl5/refman/Reference-Manual012.html&quot;&gt;LTac&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So try not to rely too much on advanced features, and make your proofs scripts robust (using &lt;a href=&quot;http://poleiro.info/posts/2013-11-17-automatic-naming-considered-harmful.html&quot;&gt;explicitly named variables&lt;/a&gt; or &lt;a href=&quot;http://poleiro.info/posts/2013-06-27-structuring-proofs-with-bullets.html&quot;&gt;bullets&lt;/a&gt; for example).&lt;/p&gt;

&lt;h2&gt;Preprocess&lt;/h2&gt;

&lt;p&gt;You can use preprocessing to solve breaking incompatibility changes. This helps to keep one code database, instead of splitting your developments with one branch per Coq version.&lt;/p&gt;

&lt;p&gt;Some people use the &lt;a href=&quot;http://en.wikipedia.org/wiki/C_preprocessor&quot;&gt;CPP preprocessor&lt;/a&gt;, but it has a heavy syntax and is quite limited. Instead, I recommend to go for simpler and more powerful tools like &lt;a href=&quot;http://en.wikipedia.org/wiki/ERuby&quot;&gt;ERB&lt;/a&gt;. Here is an example file &lt;code&gt;Test.v&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition proj (n : {n : nat &amp;amp; n &amp;gt;= 2}) : nat :=
  match n with
  | existT n _ =&amp;gt; n
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code will not work with &lt;code&gt;coq.dev&lt;/code&gt; as you need one more argument in the &lt;code&gt;match&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition proj (n : {n : nat &amp;amp; n &amp;gt;= 2}) : nat :=
  match n with
  | existT _ n _ =&amp;gt; n
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The solution is to make a &lt;code&gt;Test.v.erb&lt;/code&gt; file, which will be preprocessed into &lt;code&gt;Test.v&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition proj (n : {n : nat &amp;amp; n &amp;gt;= 2}) : nat :=
  match n with
  | existT &amp;lt;%= &amp;quot;_&amp;quot; unless version[0..2] == &amp;quot;8.4&amp;quot; %&amp;gt; n _ =&amp;gt; n
  end.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We add a &lt;code&gt;_&lt;/code&gt; in the &lt;code&gt;match&lt;/code&gt;, unless if the Coq version starts by &lt;code&gt;8.4&lt;/code&gt;. Other common constructs are:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%= &amp;quot;bla&amp;quot; if version[0..2] == &amp;quot;8.4&amp;quot; %&amp;gt;
&amp;lt;%= version[0..2] == &amp;quot;8.4&amp;quot; ? &amp;quot;bla&amp;quot; : &amp;quot;bli&amp;quot; %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To preprocess this &lt;code&gt;Test.v.erb&lt;/code&gt; file, create a &lt;code&gt;pp.rb&lt;/code&gt; file at the root of your directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Preprocessor.
require &amp;#39;erb&amp;#39;

# The version of Coq.
version = `coqc -v`.match(/version ([^(]*) \(/)[1]

Dir.glob(&amp;quot;*.v.erb&amp;quot;) do |file_name|
  renderer = ERB.new(File.read(file_name, encoding: &amp;quot;UTF-8&amp;quot;))
  output_name = file_name[0..-5]
  File.open(output_name, &amp;quot;w&amp;quot;) do |file|
    file &amp;lt;&amp;lt; renderer.result()
  end
  puts &amp;quot;#{file_name} -&amp;gt; #{output_name}&amp;quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The command &lt;code&gt;ruby pp.rb&lt;/code&gt; will then compile all the files matching &lt;code&gt;*.v.erb&lt;/code&gt; to &lt;code&gt;*.v&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit from the comments:&lt;/strong&gt;
To integrate preprocessing with an IDE, you can use the &lt;a href=&quot;https://gist.github.com/cpitclaudel/2c75c8dc88b0e1c9a6e7&quot;&gt;ProofGeneral&amp;rsquo;s plugin&lt;/a&gt; of &lt;a href=&quot;http://pit-claudel.fr/clement/&quot;&gt;Clément Pit-Claudel&lt;/a&gt;. For other IDEs, the only solution is to edit the &lt;code&gt;.v&lt;/code&gt; files in the IDE for one specific version of Coq, and do back-and-forths with the &lt;code&gt;.v.erb&lt;/code&gt; files to add compatibility.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/why-and-how-to-write-code-compatible-with-many-coq-versions.html</link>
        <guid>https://rocq-blog.clarus.me/why-and-how-to-write-code-compatible-with-many-coq-versions.html</guid>
      </item>
    
      <item>
        <title>Make a Coq package</title>
        <pubDate>Tue, 25 Nov 2014 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;We will show you a typical workflow to create and publish a Coq package with &lt;a href=&quot;http://opam.ocamlpro.com/&quot;&gt;OPAM&lt;/a&gt;. This will allow you to share your Coq developments in a simple way and gain visibility.&lt;/p&gt;

&lt;p&gt;We assume you already know how to use OPAM to install Coq packages. If not, you can read this &lt;a href=&quot;http://coq-blog.clarus.me/use-opam-for-coq.html&quot;&gt;tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Create a project&lt;/h2&gt;

&lt;p&gt;Go on &lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt; and make a new project, for example &lt;code&gt;that-super-proof&lt;/code&gt;. Add a &lt;code&gt;LICENSE&lt;/code&gt; file with your copyright if you want to make your package open-source (without a license a code is proprietary). I usually choose the &lt;a href=&quot;http://opensource.org/licenses/MIT&quot;&gt;MIT&lt;/a&gt; license, as one of the most permissive and popular:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;The MIT License (MIT)

Copyright (c) &amp;lt;year&amp;gt; &amp;lt;copyright holders&amp;gt;

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the &amp;quot;Software&amp;quot;), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Add a main file &lt;code&gt;All.v&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Theorem two : 1 + 1 = 2.
  now admit.
Qed.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can now commit your work.&lt;/p&gt;

&lt;h3&gt;The case of OCaml plugins&lt;/h3&gt;

&lt;p&gt;If your project is a Coq plugin (containing &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; files), you can get inspiration from the &lt;a href=&quot;https://github.com/mattam82/Constructors&quot;&gt;Constructors&lt;/a&gt; project of &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/%7Esozeau/&quot;&gt;Matthieu Sozeau&lt;/a&gt;. This is an example of a simple OCaml plugin, including the branches &lt;code&gt;v8.4&lt;/code&gt; and &lt;code&gt;v8.5&lt;/code&gt; for compatibility with the different versions of Coq.&lt;/p&gt;

&lt;h2&gt;Compile&lt;/h2&gt;

&lt;p&gt;We will use &lt;code&gt;coq_makefile&lt;/code&gt; to generate a Makefile. Create a file &lt;code&gt;Make&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;-R . ThatSuperProof

All.v
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and an executable file &lt;code&gt;configure.sh&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/sh

coq_makefile -f Make -o Makefile
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To compile your project, run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./configure.sh
make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;coq_makefile&lt;/code&gt; is clever and also generates an &lt;code&gt;install&lt;/code&gt; rule, among over things.&lt;/p&gt;

&lt;h2&gt;Publish&lt;/h2&gt;

&lt;p&gt;To publish a new version you need to make a release. In the GitHub page of your project, go to the &lt;em&gt;releases&lt;/em&gt; section and add a new release named &lt;code&gt;1.0.0&lt;/code&gt;. People tend to use the &lt;a href=&quot;http://semver.org/&quot;&gt;SemVer&lt;/a&gt; convention for the version names, as &lt;code&gt;MAJOR.MINOR.PATCH&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MAJOR&lt;/code&gt;: major changes&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MINOR&lt;/code&gt;: minor changes&lt;/li&gt;
&lt;li&gt;&lt;code&gt;PATCH&lt;/code&gt;: bug fixes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We do a pull-request to add our new package (see &lt;a href=&quot;https://help.github.com/articles/using-pull-requests/&quot;&gt;pull-requests on GitHub&lt;/a&gt; if you need help). Fork the &lt;a href=&quot;https://github.com/coq/opam-coq-archive&quot;&gt;OPAM Coq repository&lt;/a&gt; and add a folder &lt;code&gt;coq-that-super-proof/coq-that-super-proof.dev&lt;/code&gt; in &lt;code&gt;released/packages/&lt;/code&gt;. Package names must start with the &lt;code&gt;coq-&lt;/code&gt; prefix.&lt;/p&gt;

&lt;p&gt;A package is described by three files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;descr&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;An arithmetic library.
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;opam&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam-version: &amp;quot;1.2&amp;quot;
maintainer: &amp;quot;me@myself.ninja&amp;quot;
homepage: &amp;quot;https://github.com/myself/that-super-proof&amp;quot;
dev-repo: &amp;quot;https://github.com/myself/that-super-proof.git&amp;quot;
bug-reports: &amp;quot;https://github.com/myself/that-super-proof/issues&amp;quot;
authors: [&amp;quot;Me Myself&amp;quot;]
license: &amp;quot;MIT&amp;quot;
build: [
  [&amp;quot;./configure.sh&amp;quot;]
  [make &amp;quot;-j%{jobs}%&amp;quot;]
]
install: [
  [make &amp;quot;install&amp;quot;]
]
remove: [&amp;quot;rm&amp;quot; &amp;quot;-R&amp;quot; &amp;quot;%{lib}%/coq/user-contrib/ThatSuperProof&amp;quot;]
depends: [
  &amp;quot;coq&amp;quot; {&amp;gt;= &amp;quot;8.4.5&amp;quot; &amp;amp; &amp;lt; &amp;quot;8.5~&amp;quot;}
]
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt; &amp;quot;8.5~&amp;quot;&lt;/code&gt; is there to say that the Coq version must be lesser than &lt;code&gt;8.5.0&lt;/code&gt;, &lt;code&gt;8.5.1&lt;/code&gt;, &amp;hellip; as well as &lt;code&gt;8.5~beta1&lt;/code&gt;, &lt;code&gt;8.5~beta2&lt;/code&gt;, &amp;hellip;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;url&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http: &amp;quot;https://github.com/myself/that-super-proof/archive/1.0.0.tar.gz&amp;quot;
checksum: &amp;quot;da1da74c8f6c560b153ab8dc558cf29e&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The MD5 checksum is mandatory, and can be obtained with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;curl -L https://github.com/myself/that-super-proof/archive/1.0.0.tar.gz |md5sum
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can test your own fork of the OPAM Coq repository using &lt;code&gt;opam repo add&lt;/code&gt; on the folder &lt;code&gt;released&lt;/code&gt; of your fork. Then, issue a pull-request with your new package.&lt;/p&gt;

&lt;h2&gt;Use the bench&lt;/h2&gt;

&lt;p&gt;There is a bench system available on &lt;a href=&quot;http://coq-bench.github.io/&quot;&gt;coq-bench.github.io&lt;/a&gt;. We test all the packages for each version of Coq. We host this service to help you to check that your packages compile for each platform, even development ones. Compatibility across Coq versions is not necessary but allows you to reach more users. You can specify the Coq versions you depend upon in the &lt;code&gt;depends&lt;/code&gt; field of your &lt;code&gt;opam&lt;/code&gt; files.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/make-a-coq-package.html</link>
        <guid>https://rocq-blog.clarus.me/make-a-coq-package.html</guid>
      </item>
    
      <item>
        <title>Use OPAM for Coq</title>
        <pubDate>Mon, 24 Nov 2014 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;&lt;a href=&quot;http://opam.ocamlpro.com/&quot;&gt;OPAM&lt;/a&gt; is a package manager edited by &lt;a href=&quot;http://www.ocamlpro.com/&quot;&gt;OCamlPro&lt;/a&gt;. We will describe how to use it to handle &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; packages.&lt;/p&gt;

&lt;h2&gt;Install OPAM&lt;/h2&gt;

&lt;p&gt;Install OPAM by your preferred method. It is recommended to use the latest version (&lt;code&gt;1.2.2&lt;/code&gt; as of May 7, 2015), which can be installed from the sources:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;curl -L https://github.com/ocaml/opam/releases/download/1.2.2/opam-full-1.2.2.tar.gz |tar -xz
cd opam-full-1.2.2
./configure &amp;amp;&amp;amp; make lib-ext &amp;amp;&amp;amp; make
sudo make install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By default the OPAM packages are installed in &lt;code&gt;~/.opam&lt;/code&gt;. You can also have many installation folders if you want many versions of Coq or packages. A practice I recommend is to have one installation folder per project or configuration. To configure OPAM in a fresh folder &lt;code&gt;opam&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir opam
opam init --root=opam # answer no to the question
eval `opam config env --root=opam` # run this command for each shell session
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Remark:&lt;/strong&gt; Some people also use the &lt;code&gt;switch&lt;/code&gt; mechanism to handle many OPAM installations.&lt;/p&gt;

&lt;h2&gt;Add the repositories&lt;/h2&gt;

&lt;p&gt;To add the repository for the Coq packages:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-released https://coq.inria.fr/opam/released
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is also a repository for the development versions. Use it at your own risks:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-extra-dev https://coq.inria.fr/opam/extra-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To add the development versions of Coq:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam repo add coq-core-dev https://coq.inria.fr/opam/core-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Install a package&lt;/h2&gt;

&lt;p&gt;The Coq packages are in the namespace &lt;code&gt;coq-&lt;/code&gt;. To list all of them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam search coq-
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To install a package:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam install coq-io-hello-world
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To specify the version you want to install:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam install coq-io-hello-world.1.1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the package is slow to install (for instance, Coq itself), use the &lt;code&gt;-j&lt;/code&gt; option to speed it up and &lt;code&gt;-v&lt;/code&gt; to see the progress:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opam install -j4 -v coq
&lt;/code&gt;&lt;/pre&gt;
</description>
        <link>https://rocq-blog.clarus.me/use-opam-for-coq.html</link>
        <guid>https://rocq-blog.clarus.me/use-opam-for-coq.html</guid>
      </item>
    
      <item>
        <title>Cybele</title>
        <pubDate>Thu, 02 May 2013 00:00:00 +0000</pubDate>
        <description>&lt;p&gt;&lt;em&gt;This post is a re-post from the now defunct blog of &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/&quot;&gt;PPS&lt;/a&gt;. The date of this post is the same as the original post.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;proof by extraction&lt;/em&gt; is a new proof technique, combining advantages of the proof by reflection and the proof by untrusted OCaml oracles. The idea is to add effects to the &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt; language, so mutable variables, non-termination and exceptions are allowed. The execution is made efficient by extraction to &lt;a href=&quot;http://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; which can pre-compute complex values. The results are then post-checked in Coq thanks to our theory of &lt;em&gt;simulable monads&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We provide &lt;a href=&quot;http://cybele.gforge.inria.fr/&quot;&gt;Cybele&lt;/a&gt;, a Coq plugin to do proof by extraction. You can start looking at the &lt;a href=&quot;http://cybele.gforge.inria.fr/get_started.html&quot;&gt;Get started&lt;/a&gt; and &lt;a href=&quot;http://cybele.gforge.inria.fr/examples.html&quot;&gt;Examples&lt;/a&gt; sections. It is based on an original idea of &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/%7Eyrg/&quot;&gt;Yann Régis-Gianas&lt;/a&gt;, and we are developing it with the help of &lt;a href=&quot;http://www.pps.univ-paris-diderot.fr/%7Elgonzale/&quot;&gt;Lourdes del Carmen González Huesca&lt;/a&gt;, and &lt;a href=&quot;http://www.mpi-sws.org/%7Ebeta/&quot;&gt;Beta Ziliani&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;From proof by reflection to proof by extraction&lt;/h2&gt;

&lt;p&gt;We consider the example of the equivalence decision between two terms given a list of known equivalences. Let us take a type &lt;code&gt;T&lt;/code&gt; with &lt;code&gt;~&lt;/code&gt; an equivalence relation and a list of hypothesis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;H_1 : e_{i_1} ~ e_{j_1}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&amp;hellip;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;H_n : e_{i_n} ~ e_{j_n}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our goal is to prove that &lt;code&gt;e_i ~ e_j&lt;/code&gt; using the reflexivity, symmetry and transitivity properties. On small instances we can obviously solve it by hand, or write a tactic to do it for us. We just need to apply the equivalence properties as many times as necessary. But on a bigger sample:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;H_{1,2} : e_1 ~ e_2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&amp;hellip;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;H_{999,1000} : e_{999} ~ e_{1000}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a proof of &lt;code&gt;e_1 ~ e_{1000}&lt;/code&gt; would consist of 998 applications of the transitivity rule. This is just huge. The solution of the proof by reflection technique is to replace a proof term by a &lt;em&gt;computation&lt;/em&gt;. This computation has to be done by a proven correct decision procedure.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://en.wikipedia.org/wiki/Disjoint-set_data_structure&quot;&gt;union-find&lt;/a&gt; algorithm is an efficient way to solve the equivalence problem. Let us note &lt;code&gt;hs&lt;/code&gt; the list of hypothesis indexes &lt;code&gt;[(i_1, j_1), ..., (i_n, j_n)]&lt;/code&gt; and &lt;code&gt;(i, j)&lt;/code&gt; the indexes of our goal &lt;code&gt;e_i ~ e_j&lt;/code&gt;. Then a decision procedure in pseudo-code would look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;decide (hs, (i, j)) : bool :=
  let a = ref [] in
  map (fun (i, j) -&amp;gt; union a i j) hs;
  let i2 = find a i in
  let j2 = find a j in
  i2 = j2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We start with an empty array &lt;code&gt;a&lt;/code&gt; representing the fact that equivalence classes are &lt;em&gt;a priori&lt;/em&gt; singleton sets. We merge them calling the &lt;code&gt;union&lt;/code&gt; procedure on each hypothesis. If the representatives &lt;code&gt;(i2, j2)&lt;/code&gt; of &lt;code&gt;(i, j)&lt;/code&gt; are equal then the property &lt;code&gt;e_i ~ e_j&lt;/code&gt; holds.&lt;/p&gt;

&lt;p&gt;A nice way to show the soundness of &lt;code&gt;decide&lt;/code&gt; is to add an invariant to the array &lt;code&gt;a&lt;/code&gt; stating that if &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;j&lt;/code&gt; are linked then &lt;code&gt;e_i ~ e_j&lt;/code&gt;. This can be done elegantly in Coq, which natively supports mixed proofs and programs (see the &lt;a href=&quot;http://coq.inria.fr/refman/Reference-Manual028.html&quot;&gt;Program&lt;/a&gt; construct). At the end we just return a proof of &lt;code&gt;e_i ~ e_j&lt;/code&gt; if &lt;code&gt;i2&lt;/code&gt; equals &lt;code&gt;j2&lt;/code&gt;. An harder thing is to encode the union-find in a purely functional way since it critically relies on a mutable array, and show the termination of the &lt;code&gt;union&lt;/code&gt; and &lt;code&gt;find&lt;/code&gt; procedures.&lt;/p&gt;

&lt;p&gt;We decided not to do it, and instead introduce a &lt;a href=&quot;http://en.wikipedia.org/wiki/Monad_(functional_programming)&quot;&gt;monad&lt;/a&gt; &lt;code&gt;M&lt;/code&gt; representing side-effects, non-termination and exceptions. We obtain a solution close to the pseudo-code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Definition decide (known_eqs : equalities) (i j : T) : M (i ~ j).
  refine (
    let! a := tmp_ref s 0 nil in
    do! List.iter (unify a) known_eqs in
    let! Pi2 := find a i in
    let (i2, Hii2) := Pi2 in
    let! Pj2 := find a j in
    let (j2, Hjj2) := Pj2 in
    if eq_dec i2 j2 then
      ret _
    else
      error &amp;quot;decide: the terms are not equal&amp;quot;).
  ... (* some proof term *)
Defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On line 3, &lt;code&gt;let!&lt;/code&gt; is a notation for the bind operator. Line 9, we test the equality of &lt;code&gt;i2&lt;/code&gt; and &lt;code&gt;j2&lt;/code&gt;. Line 10, &lt;code&gt;ret _&lt;/code&gt; means we are returning a proof, namely the proof that &lt;code&gt;e_i ~ e_j&lt;/code&gt;. This proof is delayed until line 13, where it is made in proof mode. It uses the properties &lt;code&gt;Hii2&lt;/code&gt; and &lt;code&gt;Hjj2&lt;/code&gt; given by the invariant on &lt;code&gt;a&lt;/code&gt;, stating that &lt;code&gt;e_i ~ e_{i_2}&lt;/code&gt; and &lt;code&gt;e_j ~ e_{j_2}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is very much in the style of the IO monad of &lt;a href=&quot;http://www.haskell.org/&quot;&gt;Haskell&lt;/a&gt;. Ours provides the following primitives.&lt;/p&gt;

&lt;h4&gt;Memory&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;ref : &lt;code&gt;forall i, T_i -&amp;gt; M (Ref.t T_i)&lt;/code&gt; (memory allocation)&lt;/li&gt;
&lt;li&gt;read : &lt;code&gt;Ref.t T -&amp;gt; M T&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;write : &lt;code&gt;Ref.t T -&amp;gt; T -&amp;gt; M ()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Non-termination&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;fix : &lt;code&gt;((A -&amp;gt; B) -&amp;gt; A -&amp;gt; B) -&amp;gt; A -&amp;gt; B&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;dependent_fix : &lt;code&gt;((forall x : A, B) -&amp;gt; forall x : A, B) -&amp;gt; forall x : A, B&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Exceptions&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;raise : &lt;code&gt;string -&amp;gt; M T&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;try_with : &lt;code&gt;(() -&amp;gt; M T) -&amp;gt; (string -&amp;gt; M T) -&amp;gt; M T&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Printing&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;print : &lt;code&gt;T -&amp;gt; M ()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Obviously, this monad cannot be run natively in Coq. There is no run function of type &lt;code&gt;M A -&amp;gt; A&lt;/code&gt; or &lt;code&gt;M A -&amp;gt; option A&lt;/code&gt;. This is due to the general fixpoint operators which cannot be encoded in Coq since its typing rules enforce termination. Plus we want to run it as efficiently as possible.&lt;/p&gt;

&lt;h2&gt;An hybrid back-end&lt;/h2&gt;

&lt;p&gt;To run our monad we combine the Coq and OCaml back-ends:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;static/images/cybele_compilation.svg&quot; alt=&quot;Compilation&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Compilation chain.&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;We use the &lt;a href=&quot;http://coq.inria.fr/refman/Reference-Manual027.html&quot;&gt;extraction&lt;/a&gt; mechanism of Coq to get an equivalent program of our decision procedure in OCaml. All the monadic operators are replaced by native OCaml operators, since this language supports all the effects of our monad. We run it on a specific problem instance &lt;code&gt;x&lt;/code&gt;. It may not return a result in case of uncaught exception or infinite loop. If the execution is successful, we extract what we call a &lt;em&gt;prophecy&lt;/em&gt;. This value is a guide which helps Coq to evaluate the monad efficiently with an extended run function of type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;run : prophecy -&amp;gt; M A -&amp;gt; option A
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the non-termination it contains the numbers of steps needed to evaluate a monadic value. It can also contain some pre-computed values in OCaml to save computational time in Coq. We provide a general mechanism to pass results from OCaml to Coq in the prophecy with the system of &lt;em&gt;input memory&lt;/em&gt;. When you allocate a reference, you can mark it as an &amp;ldquo;input&amp;rdquo;. If you do so, its final value after the OCaml run will be given as its initial value to Coq. Any value can be passed thought the prophecy as long as you can serialize it. It does not work for functions or proofs, but does for all common data-structures such as integers or lists.&lt;/p&gt;

&lt;p&gt;Notice that if you do not save results in the prophecy, computations are made twice: once in OCaml, then post-checked in Coq with the run function. This is the main limitation of our system, but it could be solved with a proven correct extraction mechanism. We could directly trust the execution made in OCaml and import the result in Coq.&lt;/p&gt;

&lt;h2&gt;An hybrid programming model&lt;/h2&gt;

&lt;p&gt;We pretend that our programming model combines nicely safety and efficiency. Safety because of the strong type system of Coq and the ability to prove invariants on our programs. Efficiency because costly computations are delayed to OCaml. It supersedes what can be done with proof by reflection in pure Coq or with an OCaml plugin generating proof certificates. In a way, &lt;a href=&quot;http://cybele.gforge.inria.fr/&quot;&gt;Cybele&lt;/a&gt; unifies these two techniques, and allow to combine both approach depending on the problem.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;static/images/cybele_comparison.svg&quot; alt=&quot;Compilation&quot; class=&quot;img-responsive center-block&quot; /&gt;&lt;div class=&quot;text-center&quot;&gt;&lt;mark&gt;Trade-off efficiency vs correctness.&lt;/mark&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;As future work we plan to add more monadic operators, especially for concurrency. It would also be great to have a trusted extraction mechanism for Coq.&lt;/p&gt;
</description>
        <link>https://rocq-blog.clarus.me/cybele.html</link>
        <guid>https://rocq-blog.clarus.me/cybele.html</guid>
      </item>
    
  </channel>
</rss>
