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

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>A Series on Strongly-Specified Functions in Coq</title>
    <link>https://soap.coffee/~lthms/series/StronglySpecifiedFunctions.html</link>
    <description>Articles in the series "A Series on Strongly-Specified Functions in Coq"</description>
    <atom:link href="https://soap.coffee/~lthms/series/StronglySpecifiedFunctions.xml" rel="self"
               type="application/rss+xml" />
    
    
    <item>
      <title>Implementing Strongly-Specified Functions with the Program Framework</title>
      <link>https://soap.coffee/~lthms/posts/StronglySpecifiedFunctionsProgram.html</link>
      <guid>https://soap.coffee/~lthms/posts/StronglySpecifiedFunctionsProgram.html</guid>
      <pubDate>January 1, 2017</pubDate>
      <description>
        
        &lt;h1&gt;Implementing Strongly-Specified Functions with the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; Framework&lt;/h1&gt;&lt;div id=&quot;tags-list&quot;&gt;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/coq.html&quot; class=&quot;tag hover-lemon&quot; marked=&quot;&quot;&gt;coq&lt;/a&gt; &lt;/div&gt;
&lt;h2&gt;The Theory&lt;/h2&gt;
&lt;p&gt;If I had to explain &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt;, I would say &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; is the heir of
the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; tactic. It gives you a convenient way to embed proofs within
functional programs that are supposed to fade away during code extraction.  But
what do I mean when I say &quot;embed proofs&quot; within functional programs? I found
two ways to do it.&lt;/p&gt;
&lt;h3&gt;Invariants&lt;/h3&gt;
&lt;p&gt;First, we can define a record with one or more fields of type
&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Prop&lt;/span&gt;&lt;/code&gt;. By doing so, we can constrain the values of other fields. Put
another way, we can specify invariant for our type. For instance, in
&lt;a href=&quot;https://github.com/lthms/SpecCert&quot; class=&quot;hover-lavender&quot; marked=&quot;&quot;&gt;SpecCert&amp;nbsp;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#github&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/a&gt;, I have defined the memory
controller&apos;s SMRAMC register as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Record&lt;/span&gt; SmramcRegister := {
  d_open: bool;
  d_lock: bool;
  lock_is_close: d_lock = true -&amp;gt; d_open = false;
}.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So &lt;code class=&quot;hljs language-coq&quot;&gt;lock_is_closed&lt;/code&gt; is an invariant I know each instance of
&lt;code class=&quot;hljs&quot;&gt;SmramcRegister&lt;/code&gt; will have to comply with, because every time I
will construct a new instance, I will have to prove
&lt;code class=&quot;hljs language-coq&quot;&gt;lock_is_closed&lt;/code&gt; holds true. For instance:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; lock (reg: SmramcRegister)
  : SmramcRegister.
  &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; ({| &lt;span class=&quot;hljs-type&quot;&gt;d_open&lt;/span&gt; := false; d_lock := true |&lt;span class=&quot;hljs-type&quot;&gt;}).
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Coq leaves us this goal to prove.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;reg : SmramcRegister
============================
true = true -&amp;gt; false = false
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This sound reasonable enough.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Proof&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;trivial&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have seen in my previous article about strongly specified
functions that mixing proofs and regular terms may lead to
cumbersome code.&lt;/p&gt;
&lt;p&gt;From that perspective, &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; helps. Indeed, the &lt;code class=&quot;hljs language-coq&quot;&gt;lock&lt;/code&gt; function
can also be defined as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; Coq &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;.

#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; lock&apos; (reg: SmramcRegister)
  : SmramcRegister :=
  {| &lt;span class=&quot;hljs-type&quot;&gt;d_open&lt;/span&gt; := false
   ; d_lock := true
   |&lt;span class=&quot;hljs-type&quot;&gt;}.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Pre and Post Conditions&lt;/h3&gt;
&lt;p&gt;Another way to &quot;embed proofs in a program&quot; is by specifying pre-
and post-conditions for its component. In Coq, this is done using
sigma types.&lt;/p&gt;
&lt;p&gt;On the one hand, a precondition is a proposition a function input has to
satisfy in order for the function to be applied.  For instance, a precondition
for &lt;code class=&quot;hljs language-coq&quot;&gt;head : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; {a}, list a -&amp;gt; a&lt;/code&gt; the function that returns the first
element of a list &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; requires &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; to contain at least one element.
We can write that using a sigma-type. The type of &lt;code class=&quot;hljs language-coq&quot;&gt;head&lt;/code&gt; then becomes
&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; {a} (l: list a | &lt;span class=&quot;hljs-type&quot;&gt;l&lt;/span&gt; &amp;lt;&amp;gt; []) : a&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;On the other hand, a post condition is a proposition a function
output has to satisfy in order for the function to be correctly
implemented. In this way, &lt;code class=&quot;hljs&quot;&gt;head&lt;/code&gt; should in fact return the first
element of &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; and not something else.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; makes writing this specification straightforward.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; head {a} (l : list a | &lt;span class=&quot;hljs-type&quot;&gt;l&lt;/span&gt; &amp;lt;&amp;gt; [])
  : { x : a | &lt;span class=&quot;hljs-type&quot;&gt;exists&lt;/span&gt; l&apos;, x :: l&apos; = l }.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We recall that because &lt;code class=&quot;hljs language-coq&quot;&gt;{ l: list a | &lt;span class=&quot;hljs-type&quot;&gt;l&lt;/span&gt; &amp;lt;&amp;gt; [] }&lt;/code&gt; is not the same as &lt;code class=&quot;hljs language-coq&quot;&gt;list a&lt;/code&gt;, in theory we cannot just compare &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; with &lt;code class=&quot;hljs language-coq&quot;&gt;x :: l&apos;&lt;/code&gt; (we need to
use &lt;code class=&quot;hljs language-coq&quot;&gt;proj1_sig&lt;/code&gt;). One advantage of &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; is to deal with it using
an implicit coercion.&lt;/p&gt;
&lt;p&gt;Note that for the type inference to work as expected, the
unwrapped value (here, &lt;code class=&quot;hljs language-coq&quot;&gt;x :: l&apos;&lt;/code&gt;) needs to be the left operand of
&lt;code class=&quot;hljs language-coq&quot;&gt;=&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now that &lt;code class=&quot;hljs language-coq&quot;&gt;head&lt;/code&gt; have been specified, we have to implement it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; head {a} (l: list a | &lt;span class=&quot;hljs-type&quot;&gt;l&lt;/span&gt; &amp;lt;&amp;gt; [])
  : { x : a | &lt;span class=&quot;hljs-type&quot;&gt;exists&lt;/span&gt; l&apos;, cons x l&apos; = l } :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;x&lt;/span&gt; :: l&apos; =&amp;gt; x
  | &lt;span class=&quot;hljs-type&quot;&gt;[] =&amp;gt; !
  end&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;exists&lt;/span&gt; l&apos;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;reflexivity&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Qed&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I want to highlight several things here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We return &lt;code class=&quot;hljs language-coq&quot;&gt;x&lt;/code&gt; (of type &lt;code class=&quot;hljs language-coq&quot;&gt;a&lt;/code&gt;) rather than a sigma-type, then
&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; is smart enough to wrap it. To do so, it tries to prove the post
condition and because it fails, we have to do it ourselves (this is the
Obligation we solve after the function definition.)&lt;/li&gt;
&lt;li&gt;The &lt;code class=&quot;hljs language-coq&quot;&gt;[]&lt;/code&gt; case is absurd regarding the precondition, we tell Coq that
using the bang (&lt;code class=&quot;hljs language-coq&quot;&gt;!&lt;/code&gt;) symbol.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can have a look at the extracted code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;(** val head : &apos;a1 list -&amp;gt; &apos;a1 **)&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; head = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Nil&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;hljs-comment&quot;&gt;(* absurd case *)&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Cons&lt;/span&gt; (a, _) -&amp;gt; a
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The implementation is pretty straightforward, but the pre- and
post conditions have faded away. Also, the absurd case is
discarded using an assertion. This means one thing: [head] should
not be used directly from the Ocaml world. &quot;Interface&quot; functions
have to be total. *)&lt;/p&gt;
&lt;h2&gt;The Practice&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; Coq &lt;span class=&quot;hljs-keyword&quot;&gt;Require&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Import&lt;/span&gt; Lia.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I have challenged myself to build a strongly specified library. My goal was to
define a type &lt;code class=&quot;hljs language-coq&quot;&gt;vector : nat -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;&lt;/code&gt; such as &lt;code class=&quot;hljs language-coq&quot;&gt;vector a n&lt;/code&gt;
is a list of &lt;code class=&quot;hljs language-coq&quot;&gt;n&lt;/code&gt; instance of &lt;code class=&quot;hljs language-coq&quot;&gt;a&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; vector (a : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) : nat -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
| &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; {n} : a -&amp;gt; vector a n -&amp;gt; vector a (S n)
| &lt;span class=&quot;hljs-type&quot;&gt;vnil&lt;/span&gt; : vector a O.

&lt;span class=&quot;hljs-keyword&quot;&gt;Arguments&lt;/span&gt; vcons [a n] &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Arguments&lt;/span&gt; vnil {a}.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I had three functions in mind: &lt;code class=&quot;hljs language-coq&quot;&gt;take&lt;/code&gt;, &lt;code class=&quot;hljs language-coq&quot;&gt;drop&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;extract&lt;/code&gt;.
I learned a few lessons. My main takeaway remains: do not use sigma types,
&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; and dependent types together. From my point of view, Coq is not
yet ready for this. Maybe it is possible to make those three work together, but
I have to admit I did not find out how. As a consequence, my preconditions are
defined as extra arguments.&lt;/p&gt;
&lt;p&gt;To be able to specify the post conditions of my three functions and
some others, I first defined &lt;code class=&quot;hljs language-coq&quot;&gt;nth&lt;/code&gt; to get the &lt;em&gt;nth&lt;/em&gt; element of a
vector.&lt;/p&gt;
&lt;p&gt;My first attempt to write &lt;code class=&quot;hljs language-coq&quot;&gt;nth&lt;/code&gt; was a failure.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; nth {a n}
    (v : vector a n) (i : nat) {struct v}
  : option a :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v, i &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;, O =&amp;gt; Some x
  | &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; x r, S i =&amp;gt; nth r i
  | &lt;span class=&quot;hljs-type&quot;&gt;vnil&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; =&amp;gt; None
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;raised an anomaly.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; nth {a n}
    (v : vector a n) (i : nat) {struct v}
  : option a :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; x r =&amp;gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; i &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
    | &lt;span class=&quot;hljs-type&quot;&gt;O&lt;/span&gt; =&amp;gt; Some x
    | &lt;span class=&quot;hljs-type&quot;&gt;S&lt;/span&gt; i =&amp;gt; nth r i
    &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;vnil&lt;/span&gt; =&amp;gt; None
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;code class=&quot;hljs language-coq&quot;&gt;nth&lt;/code&gt;, it is possible to give a very precise definition of
&lt;code class=&quot;hljs language-coq&quot;&gt;take&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; take {a n}
    (v : vector a n) (e : nat | &lt;span class=&quot;hljs-type&quot;&gt;e&lt;/span&gt; &amp;lt;= n)
  : { u : vector a e | &lt;span class=&quot;hljs-type&quot;&gt;forall&lt;/span&gt; i : nat,
        i &amp;lt; e -&amp;gt; nth u i = nth v i } :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; e &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;S&lt;/span&gt; e&apos; =&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
            | &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; x r =&amp;gt; vcons x (take r e&apos;)
            | &lt;span class=&quot;hljs-type&quot;&gt;vnil&lt;/span&gt; =&amp;gt; !
            &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;O&lt;/span&gt; =&amp;gt; vnil
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; le_S_n.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;induction&lt;/span&gt; i.
  + &lt;span class=&quot;hljs-built_in&quot;&gt;reflexivity&lt;/span&gt;.
  + &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; e0.
    now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; Lt.lt_S_n.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; PeanoNat.Nat.nle_succ_0 &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; H.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; PeanoNat.Nat.nlt_0_r &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; H.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a side note, I wanted to define the post condition as follows:
&lt;code class=&quot;hljs language-coq&quot;&gt;{ v&apos;: vector A e | &lt;span class=&quot;hljs-type&quot;&gt;forall&lt;/span&gt; (i : nat | &lt;span class=&quot;hljs-type&quot;&gt;i&lt;/span&gt; &amp;lt; e), nth v&apos; i = nth v i }&lt;/code&gt;. However, this made the goals and hypotheses become very hard
to read and to use. Sigma types in sigma types: not a good
idea.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;(** val take : &apos;a1 vector -&amp;gt; nat -&amp;gt; &apos;a1 vector **)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;rec&lt;/span&gt; take v = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;O&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;Vnil&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;S&lt;/span&gt; e&apos; -&amp;gt;
  (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;
   | &lt;span class=&quot;hljs-type&quot;&gt;Vcons&lt;/span&gt; (_, x, r) -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;Vcons&lt;/span&gt; (e&apos;, x, (take r e&apos;))
   | &lt;span class=&quot;hljs-type&quot;&gt;Vnil&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;hljs-comment&quot;&gt;(* absurd case *)&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I could tackle &lt;code class=&quot;hljs&quot;&gt;drop&lt;/code&gt; in a very similar manner:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; drop {a n}
    (v : vector a n) (b : nat | &lt;span class=&quot;hljs-type&quot;&gt;b&lt;/span&gt; &amp;lt;= n)
  : { v&apos;: vector a (n - b) | &lt;span class=&quot;hljs-type&quot;&gt;forall&lt;/span&gt; i,
        i &amp;lt; n - b -&amp;gt; nth v&apos; i = nth v (b + i) } :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; b &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;0&lt;/span&gt; =&amp;gt; v
  | &lt;span class=&quot;hljs-type&quot;&gt;S&lt;/span&gt; n =&amp;gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
           | &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; r =&amp;gt; (drop r n)
           | &lt;span class=&quot;hljs-type&quot;&gt;vnil&lt;/span&gt; =&amp;gt; !
           &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;)
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; &amp;lt;- Minus.minus_n_O.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;induction&lt;/span&gt; n;
    &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; &amp;lt;- eq_rect_eq;
    &lt;span class=&quot;hljs-built_in&quot;&gt;reflexivity&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; le_S_n.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; PeanoNat.Nat.nle_succ_0 &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; H.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The proofs are easy to write, and the extracted code is exactly what one might
want it to be:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;(** val drop : &apos;a1 vector -&amp;gt; nat -&amp;gt; &apos;a1 vector **)&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;rec&lt;/span&gt; drop v = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;O&lt;/span&gt; -&amp;gt; v
| &lt;span class=&quot;hljs-type&quot;&gt;S&lt;/span&gt; n -&amp;gt;
  (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;
   | &lt;span class=&quot;hljs-type&quot;&gt;Vcons&lt;/span&gt; (_, _, r) -&amp;gt; drop r n
   | &lt;span class=&quot;hljs-type&quot;&gt;Vnil&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;hljs-comment&quot;&gt;(* absurd case *)&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; really shone when it comes to implementing extract. I just
had to combine &lt;code class=&quot;hljs language-coq&quot;&gt;take&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;drop&lt;/code&gt;. *)&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; extract {a n} (v : vector a n)
    (e : nat | &lt;span class=&quot;hljs-type&quot;&gt;e&lt;/span&gt; &amp;lt;= n) (b : nat | &lt;span class=&quot;hljs-type&quot;&gt;b&lt;/span&gt; &amp;lt;= e)
  : { v&apos;: vector a (e - b) | &lt;span class=&quot;hljs-type&quot;&gt;forall&lt;/span&gt; i,
        i &amp;lt; (e - b) -&amp;gt; nth v&apos; i = nth v (b + i) } :=
  take (drop v b) (e - b).


&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;transitivity&lt;/span&gt; e; &lt;span class=&quot;hljs-built_in&quot;&gt;auto&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; PeanoNat.Nat.sub_le_mono_r.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;destruct&lt;/span&gt; drop; &lt;span class=&quot;hljs-built_in&quot;&gt;cbn&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; *.
  &lt;span class=&quot;hljs-built_in&quot;&gt;destruct&lt;/span&gt; take; &lt;span class=&quot;hljs-built_in&quot;&gt;cbn&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; *.
  &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; e1; &lt;span class=&quot;hljs-built_in&quot;&gt;auto&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; &amp;lt;- e0; &lt;span class=&quot;hljs-built_in&quot;&gt;auto&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;lia&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The proofs are straightforward because the specifications of &lt;code class=&quot;hljs language-coq&quot;&gt;drop&lt;/code&gt; and
&lt;code class=&quot;hljs language-coq&quot;&gt;take&lt;/code&gt; are precise enough, and we do not need to have a look at their
implementations. The extracted version of &lt;code class=&quot;hljs language-coq&quot;&gt;extract&lt;/code&gt; is as clean as we can
anticipate.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;(** val extract : &apos;a1 vector -&amp;gt; nat -&amp;gt; nat -&amp;gt; &apos;a1 vector **)&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; extract v e b =
  take (drop v b) (sub e b)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I was pretty happy, so I tried some more. Each time, using &lt;code class=&quot;hljs language-coq&quot;&gt;nth&lt;/code&gt;, I managed
to write a precise post condition and to prove it holds true. For instance,
given &lt;code class=&quot;hljs language-coq&quot;&gt;map&lt;/code&gt; to apply a function &lt;code class=&quot;hljs language-coq&quot;&gt;f&lt;/code&gt; to each element of a vector &lt;code class=&quot;hljs language-coq&quot;&gt;v&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; map {a b n} (v : vector a n) (f : a -&amp;gt; b)
  : { v&apos;: vector b n | &lt;span class=&quot;hljs-type&quot;&gt;forall&lt;/span&gt; i,
        nth v&apos; i = option_map f (nth v i) } :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;vnil&lt;/span&gt; =&amp;gt; vnil
  | &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; a v =&amp;gt; vcons (f a) (map v f)
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;induction&lt;/span&gt; i.
  + &lt;span class=&quot;hljs-built_in&quot;&gt;reflexivity&lt;/span&gt;.
  + &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; e.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I also managed to specify and write &lt;code class=&quot;hljs language-coq&quot;&gt;append&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;#[program]
Fixpoint append {a n m}
    (v : vector a n) (u : vector a m)
  : { w : vector a (n + m) | forall i,
        (i &amp;lt; n -&amp;gt; nth w i = nth v i) /\
        (n &amp;lt;= i -&amp;gt; nth w i = nth u (i - n))
    } :=
  match v with
  | vnil =&amp;gt; u
  | vcons a v =&amp;gt; vcons a (append v u)
  end.

Next Obligation.
  split.
  + now intro.
  + intros _.
    now rewrite PeanoNat.Nat.sub_0_r.
Defined.

Next Obligation.
  rename wildcard&apos; into n.
  destruct (Compare_dec.lt_dec i (S n)); split.
  + intros _.
    destruct i.
    ++ reflexivity.
    ++ cbn.
       specialize (a1 i).
       destruct a1 as [a1 _].
       apply a1.
       auto with arith.
  + intros false.
    lia.
  + now intros.
  + intros ord.
    destruct i.
    ++ lia.
    ++ cbn.
       specialize (a1 i).
       destruct a1 as [_ a1].
       apply a1.
       auto with arith.
Defined.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, I tried to implement &lt;code class=&quot;hljs language-coq&quot;&gt;map2&lt;/code&gt; that takes a vector of &lt;code class=&quot;hljs language-coq&quot;&gt;a&lt;/code&gt;, a vector of
&lt;code class=&quot;hljs language-coq&quot;&gt;b&lt;/code&gt; (both of the same size) and a function &lt;code class=&quot;hljs language-coq&quot;&gt;f : a -&amp;gt; b -&amp;gt; c&lt;/code&gt; and returns a
vector of &lt;code class=&quot;hljs language-coq&quot;&gt;c&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First, we need to provide a precise specification for &lt;code class=&quot;hljs language-coq&quot;&gt;map2&lt;/code&gt;. To do that, we
introduce &lt;code class=&quot;hljs language-coq&quot;&gt;option_app&lt;/code&gt;, a function that Haskellers know all to well as being
part of the &lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;Applicative&lt;/span&gt;&lt;/code&gt; type class.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; option_app {a b}
    (opf: option (a -&amp;gt; b))
    (opx: option a)
  : option b :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; opf, opx &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;Some&lt;/span&gt; f, Some x =&amp;gt; Some (f x)
  | &lt;span class=&quot;hljs-type&quot;&gt;_&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; =&amp;gt; None
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We thereafter use &lt;code class=&quot;hljs language-coq&quot;&gt;&amp;lt;$&amp;gt;&lt;/code&gt; as an infix operator for &lt;code class=&quot;hljs language-coq&quot;&gt;option_map&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;&amp;lt;*&amp;gt;&lt;/code&gt; as
an infix operator for &lt;code class=&quot;hljs language-coq&quot;&gt;option_app&lt;/code&gt;. *)&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Infix&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;&amp;lt;$&amp;gt;&quot;&lt;/span&gt; := option_map (&lt;span class=&quot;hljs-built_in&quot;&gt;at&lt;/span&gt; level &lt;span class=&quot;hljs-number&quot;&gt;50&lt;/span&gt;).
&lt;span class=&quot;hljs-keyword&quot;&gt;Infix&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;&amp;lt;*&amp;gt;&quot;&lt;/span&gt; := option_app (&lt;span class=&quot;hljs-built_in&quot;&gt;at&lt;/span&gt; level &lt;span class=&quot;hljs-number&quot;&gt;55&lt;/span&gt;).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Given two vectors &lt;code class=&quot;hljs language-coq&quot;&gt;v&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;u&lt;/code&gt; of the same size and a function &lt;code class=&quot;hljs language-coq&quot;&gt;f&lt;/code&gt;, and given
&lt;code class=&quot;hljs language-coq&quot;&gt;w&lt;/code&gt; the result computed by &lt;code class=&quot;hljs language-coq&quot;&gt;map2&lt;/code&gt;, then we can propose the following
specification for &lt;code class=&quot;hljs language-coq&quot;&gt;map2&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (i : nat), nth w i = f &amp;lt;$&amp;gt; nth v i &amp;lt;*&amp;gt; nth u i&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This reads as follows: the &lt;code class=&quot;hljs language-coq&quot;&gt;i&lt;/code&gt;th element of &lt;code class=&quot;hljs language-coq&quot;&gt;w&lt;/code&gt; is the result of applying
the &lt;code class=&quot;hljs language-coq&quot;&gt;i&lt;/code&gt;th elements of &lt;code class=&quot;hljs language-coq&quot;&gt;v&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;u&lt;/code&gt; to &lt;code class=&quot;hljs language-coq&quot;&gt;f&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It turns out implementing &lt;code class=&quot;hljs language-coq&quot;&gt;map2&lt;/code&gt; with the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; framework has
proven to be harder than I originally expected. My initial attempt was the
following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; map2 {a b c n}
    (v : vector a n) (u : vector b n)
    (f : a -&amp;gt; b -&amp;gt; c) {struct v}
  : { w: vector c n | &lt;span class=&quot;hljs-type&quot;&gt;forall&lt;/span&gt; i,
        nth w i = f &amp;lt;$&amp;gt; nth v i &amp;lt;*&amp;gt; nth u i
    } :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; v, u &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;vcons&lt;/span&gt; x rst, vcons x&apos; rst&apos; =&amp;gt;
      vcons (f x x&apos;) (map2 rst rst&apos; f)
  | &lt;span class=&quot;hljs-type&quot;&gt;vnil&lt;/span&gt;, vnil =&amp;gt; vnil
  | &lt;span class=&quot;hljs-type&quot;&gt;_&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; =&amp;gt; !
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;Illegal application:
The term &quot;@eq&quot; of type &quot;forall A : Type, A -&amp;gt; A -&amp;gt; Prop&quot;
cannot be applied to the terms
 &quot;nat&quot; : &quot;Set&quot;
 &quot;S wildcard&apos;&quot; : &quot;nat&quot;
 &quot;b&quot; : &quot;Type&quot;
The 3rd term has type &quot;Type&quot; which should be coercible
to &quot;nat&quot;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So I had to fallback to defining the function in pure Ltac.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; map2 {a b c n}
    (v : vector a n) (u : vector b n)
    (f : a -&amp;gt; b -&amp;gt; c) {struct v}
  : { w: vector c n | &lt;span class=&quot;hljs-type&quot;&gt;forall&lt;/span&gt; i,
        nth w i = f &amp;lt;$&amp;gt; nth v i &amp;lt;*&amp;gt; nth u i
    } := &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;.

&lt;span class=&quot;hljs-keyword&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Obligation&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;dependent&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;induction&lt;/span&gt; v; &lt;span class=&quot;hljs-built_in&quot;&gt;dependent&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;induction&lt;/span&gt; u.
  + &lt;span class=&quot;hljs-built_in&quot;&gt;remember&lt;/span&gt; (IHv u f) &lt;span class=&quot;hljs-built_in&quot;&gt;as&lt;/span&gt; u&apos;.
    &lt;span class=&quot;hljs-built_in&quot;&gt;inversion&lt;/span&gt; u&apos;.
    &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; (exist &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; (vcons (f a0 a1) x) &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;).
    &lt;span class=&quot;hljs-built_in&quot;&gt;intros&lt;/span&gt; i.
    &lt;span class=&quot;hljs-built_in&quot;&gt;induction&lt;/span&gt; i.
    * &lt;span class=&quot;hljs-built_in&quot;&gt;reflexivity&lt;/span&gt;.
    * &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; (H i).
  + &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; (exist &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; vnil &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;).
    &lt;span class=&quot;hljs-built_in&quot;&gt;reflexivity&lt;/span&gt;.
&lt;span class=&quot;hljs-keyword&quot;&gt;Qed&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Is It Usable?&lt;/h2&gt;
&lt;p&gt;This post mostly gives the &quot;happy ends&quot; for each function. I think I tried
too hard for what I got in return and therefore I am convinced &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt;
is not ready (at least for a dependent type, I cannot tell for the rest). For
instance, I found at least one bug in Program logic (I still have to report
it). Have a look at the following code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;#[program]
&lt;span class=&quot;hljs-keyword&quot;&gt;Fixpoint&lt;/span&gt; map2 {a b c n}
     (u : vector a n) (v : vector b n)
     (f : a -&amp;gt; b -&amp;gt; c) {struct v}
  : vector c n :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; u &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;_&lt;/span&gt; =&amp;gt; vnil
  &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It gives the following error:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;Error: Illegal application:
The term &quot;@eq&quot; of type &quot;forall A : Type, A -&amp;gt; A -&amp;gt; Prop&quot;
cannot be applied to the terms
 &quot;nat&quot; : &quot;Set&quot;
 &quot;0&quot; : &quot;nat&quot;
 &quot;wildcard&apos;&quot; : &quot;vector A n&apos;&quot;
The 3rd term has type &quot;vector A n&apos;&quot; which should be
coercible to &quot;nat&quot;.
&lt;/code&gt;&lt;/pre&gt;
        
      </description>
    </item>
    
    
    
    <item>
      <title>Implementing Strongly-Specified Functions with the refine Tactic</title>
      <link>https://soap.coffee/~lthms/posts/StronglySpecifiedFunctionsRefine.html</link>
      <guid>https://soap.coffee/~lthms/posts/StronglySpecifiedFunctionsRefine.html</guid>
      <pubDate>January 11, 2015</pubDate>
      <description>
        
        &lt;h1&gt;Implementing Strongly-Specified Functions with the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; Tactic&lt;/h1&gt;&lt;div id=&quot;tags-list&quot;&gt;&lt;span class=&quot;icon&quot;&gt;&lt;svg&gt;&lt;use href=&quot;/~lthms/img/icons.svg#tag&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;/~lthms/tags/coq.html&quot; class=&quot;tag hover-lavender&quot; marked=&quot;&quot;&gt;coq&lt;/a&gt; &lt;/div&gt;
&lt;p&gt;I started to play with Coq, the interactive theorem prover
developed by Inria, a few weeks ago. It is a very powerful tool,
yet hard to master. Fortunately, there are some very good readings
if you want to learn (I recommend the Coq&apos;Art). This article is
not one of them.&lt;/p&gt;
&lt;p&gt;In this article, we will see how to implement strongly specified
list manipulation functions in Coq. Strong specifications are used
to ensure some properties on functions&apos; arguments and return
value. It makes Coq type system very expressive. Thus, it is
possible to specify in the type of the function &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt; that the return
value is the list passed as an argument in which the first element has been
removed, for example.&lt;/p&gt;
&lt;h2&gt;Is This List Empty?&lt;/h2&gt;
&lt;p&gt;It&apos;s the first question to deal with when manipulating
lists. There are some functions that require their arguments not
to be empty. It&apos;s the case for the &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt; function, for instance
it is not possible to remove the first element of a list that does
not have any elements in the first place.&lt;/p&gt;
&lt;p&gt;When one wants to answer such a question as “Is this list empty?”,
he has to keep in mind that there are two ways to do it: by a
predicate or by a boolean function. Indeed, &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Prop&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;bool&lt;/code&gt; are
two different worlds that do not mix easily. One solution is to
write two definitions and to prove their equivalence.  That is
&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; args, predicate args &amp;lt;-&amp;gt; bool_function args = true&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Another solution is to use the &lt;code class=&quot;hljs language-coq&quot;&gt;sumbool&lt;/code&gt; type as middlemen. The
scheme is the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Defining &lt;code class=&quot;hljs language-coq&quot;&gt;predicate : args → &lt;span class=&quot;hljs-keyword&quot;&gt;Prop&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Defining &lt;code class=&quot;hljs language-coq&quot;&gt;predicate_dec : args -&amp;gt; { predicate args } + { ~predicate args }&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Defining &lt;code class=&quot;hljs language-coq&quot;&gt;predicate_b&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; predicate_b (args) :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; predicate_dec args &lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt; true &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; false.
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Defining the &lt;code class=&quot;hljs language-coq&quot;&gt;empty&lt;/code&gt; Predicate&lt;/h3&gt;
&lt;p&gt;A list is empty if it is &lt;code class=&quot;hljs language-coq&quot;&gt;[]&lt;/code&gt; (&lt;code class=&quot;hljs language-coq&quot;&gt;nil&lt;/code&gt;). It&apos;s as simple as that!&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; empty {a} (l : list a) : &lt;span class=&quot;hljs-keyword&quot;&gt;Prop&lt;/span&gt; := l = [].
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Defining a decidable version of &lt;code class=&quot;hljs language-coq&quot;&gt;empty&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;A decidable version of &lt;code class=&quot;hljs language-coq&quot;&gt;empty&lt;/code&gt; is a function which takes a list
&lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; as its argument and returns either a proof that &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; is empty,
or a proof that &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; is not empty. This is encoded in the Coq
standard library with the &lt;code class=&quot;hljs language-coq&quot;&gt;sumbool&lt;/code&gt; type, and is written as
follows: &lt;code class=&quot;hljs language-coq&quot;&gt;{ empty l } + { ~ empty l }&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; empty_dec {a} (l : list a)
  : { empty l } + { ~ empty l }.
&lt;span class=&quot;hljs-keyword&quot;&gt;Proof&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;[] =&amp;gt; left&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;_&lt;/span&gt; =&amp;gt; &lt;span class=&quot;hljs-built_in&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;);
    &lt;span class=&quot;hljs-built_in&quot;&gt;unfold&lt;/span&gt; empty; &lt;span class=&quot;hljs-built_in&quot;&gt;trivial&lt;/span&gt;.
  &lt;span class=&quot;hljs-built_in&quot;&gt;unfold&lt;/span&gt; not; &lt;span class=&quot;hljs-built_in&quot;&gt;intro&lt;/span&gt; H; &lt;span class=&quot;hljs-built_in&quot;&gt;discriminate&lt;/span&gt; H.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, I decided to use the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; tactic which is
convenient when we manipulate the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Set&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Prop&lt;/span&gt;&lt;/code&gt; sorts at the
same time.&lt;/p&gt;
&lt;h3&gt;Defining &lt;code class=&quot;hljs language-coq&quot;&gt;empty_b&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;With &lt;code class=&quot;hljs language-coq&quot;&gt;empty_dec&lt;/code&gt;, we can define &lt;code class=&quot;hljs language-coq&quot;&gt;empty_b&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; empty_b {a} (l : list a) : bool :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; empty_dec l &lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt; true &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; false.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&apos;s try to extract &lt;code class=&quot;hljs language-coq&quot;&gt;empty_b&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;bool&lt;/span&gt; =
| &lt;span class=&quot;hljs-type&quot;&gt;True&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;False&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; sumbool =
| &lt;span class=&quot;hljs-type&quot;&gt;Left&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Right&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt; =
| &lt;span class=&quot;hljs-type&quot;&gt;Nil&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Cons&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; * &lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot;&gt;(** val empty_dec : &apos;a1 list -&amp;gt; sumbool **)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; empty_dec = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Nil&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;Left&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Cons&lt;/span&gt; (a, l0) -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;Right&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot;&gt;(** val empty_b : &apos;a1 list -&amp;gt; bool **)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; empty_b l =
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; empty_dec l &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;Left&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;True&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;Right&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-type&quot;&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-symbol&quot;&gt;&apos;a&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;&lt;/code&gt;, Coq has created the &lt;code class=&quot;hljs language-ocaml&quot;&gt;sumbool&lt;/code&gt; and
&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;bool&lt;/span&gt;&lt;/code&gt; types and &lt;code class=&quot;hljs language-ocaml&quot;&gt;empty_b&lt;/code&gt; is basically a translation from the
former to the latter. We could have stopped with &lt;code class=&quot;hljs language-ocmal&quot;&gt;empty_dec&lt;/code&gt;, but
&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;Left&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;Right&lt;/span&gt;&lt;/code&gt; are less readable that &lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;True&lt;/span&gt;&lt;/code&gt; and
&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;False&lt;/span&gt;&lt;/code&gt;. Note that it is possible to configure the Extraction mechanism
to use primitive OCaml types instead, but this is out of the scope of this
article.&lt;/p&gt;
&lt;h2&gt;Defining Some Utility Functions&lt;/h2&gt;
&lt;h3&gt;Defining &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;There are several ways to write a function that removes the first
element of a list. One is to return &lt;code class=&quot;hljs&quot;&gt;nil&lt;/code&gt; if the given list was
already empty:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; pop {a} ( l :list a) :=
  &lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
  | &lt;span class=&quot;hljs-type&quot;&gt;_&lt;/span&gt; :: l =&amp;gt; l
  | &lt;span class=&quot;hljs-type&quot;&gt;[] =&amp;gt; []
  end&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But it&apos;s not really satisfying. A &lt;code class=&quot;hljs&quot;&gt;pop&lt;/code&gt; call over an empty list should not be
possible. It can be done by adding an argument to &lt;code class=&quot;hljs&quot;&gt;pop&lt;/code&gt;: the proof that the
list is not empty.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; pop {a} (l : list a) (h : ~ empty l)
  : list a.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are, as usual when it comes to lists, two cases to
consider.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;l = x :: rst&lt;/code&gt;, and therefore &lt;code class=&quot;hljs language-coq&quot;&gt;pop (x :: rst) h&lt;/code&gt; is &lt;code class=&quot;hljs language-coq&quot;&gt;rst&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;l = []&lt;/code&gt;, which is not possible since we know &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; is not empty.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The challenge is to convince Coq that our reasoning is
correct. There are, again, several approaches to achieve that.  We
can, for instance, use the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; tactic again, but this time we
need to know a small trick to succeed as using a “regular” &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt;&lt;/code&gt;
will not work.&lt;/p&gt;
&lt;p&gt;From the following goal:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;  a : Type
  l : list a
  h : ~ empty l
  ============================
  list a
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; tactic naively, for instance, this way:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;_&lt;/span&gt; :: rst =&amp;gt; rst
          | &lt;span class=&quot;hljs-type&quot;&gt;[] =&amp;gt; _&lt;/span&gt;
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;leaves us the following goal to prove:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;  a : Type
  l : list a
  h : ~ empty l
  ============================
  list a
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nothing has changed! Well, not exactly. See, &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; has taken
our incomplete Gallina term, found a hole, done some
type-checking, found that the type of the missing piece of our
implementation is &lt;code class=&quot;hljs language-coq&quot;&gt;list a&lt;/code&gt; and therefore has generated a new
goal of this type.  What &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; has not done, however, is
remembering that we are in the case where &lt;code class=&quot;hljs language-coq&quot;&gt;l = []&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We need to generate a goal from a hole wherein this information is
available. It is possible to use a long form of &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt;&lt;/code&gt;. The
general approach is this: rather than returning a value of type
&lt;code class=&quot;hljs language-coq&quot;&gt;list a&lt;/code&gt;, our match will return a function of type &lt;code class=&quot;hljs language-coq&quot;&gt;l = ?l&apos; -&amp;gt; list a&lt;/code&gt;, where &lt;code class=&quot;hljs language-coq&quot;&gt;?l&lt;/code&gt; is a value of &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; for a given case (that is,
either &lt;code class=&quot;hljs language-coq&quot;&gt;x :: rst&lt;/code&gt; or &lt;code class=&quot;hljs language-coq&quot;&gt;[]&lt;/code&gt;). Of course and as a consequence, the type
of the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt;&lt;/code&gt; in now a function which awaits a proof to return
the expected result. Fortunately, this proof is trivial: it is
&lt;code class=&quot;hljs language-coq&quot;&gt;eq_refl&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;as&lt;/span&gt; l&apos;
                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; l = l&apos; -&amp;gt; list a
          &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;_&lt;/span&gt; :: rst =&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; =&amp;gt; rst
          | &lt;span class=&quot;hljs-type&quot;&gt;[] =&amp;gt; fun&lt;/span&gt; equ =&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt; eq_refl).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For us to conclude the proof, this is way better.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;  a : Type
  l : list a
  h : ~ empty l
  equ : l = []
  ============================
  list a
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We conclude the proof, and therefore the definition of &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; equ &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; h.
  &lt;span class=&quot;hljs-built_in&quot;&gt;exfalso&lt;/span&gt;.
  now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; h.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&apos;s better and yet it can still be improved. Indeed, according to its type,
&lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt; returns “some list.” As a matter of fact, &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt; returns “the
same list without its first argument.” It is possible to write
such precise definition thanks to sigma types, defined as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Inductive&lt;/span&gt; sig (A : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt;) (P : A -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;Prop&lt;/span&gt;) : &lt;span class=&quot;hljs-keyword&quot;&gt;Type&lt;/span&gt; :=
  exist : &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; (x : A), P x -&amp;gt; sig P.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rather than &lt;code class=&quot;hljs language-coq&quot;&gt;sig A p&lt;/code&gt;, sigma-types can be written using the
notation &lt;code class=&quot;hljs language-coq&quot;&gt;{ a | &lt;span class=&quot;hljs-type&quot;&gt;P&lt;/span&gt; }&lt;/code&gt;. They express subsets, and can be used to constraint
arguments and results of functions.&lt;/p&gt;
&lt;p&gt;We finally propose a strongly specified definition of &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; pop {a} (l : list a | &lt;span class=&quot;hljs-type&quot;&gt;~ empty&lt;/span&gt; l)
  : { l&apos; | &lt;span class=&quot;hljs-type&quot;&gt;exists&lt;/span&gt; a, proj1_sig l = cons a l&apos; }.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you think the previous use of &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt;&lt;/code&gt; term was ugly, brace yourselves.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; proj1_sig l &lt;span class=&quot;hljs-built_in&quot;&gt;as&lt;/span&gt; l&apos;
                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; proj1_sig l = l&apos;
                       -&amp;gt; { l&apos; | &lt;span class=&quot;hljs-type&quot;&gt;exists&lt;/span&gt; a, proj1_sig l = cons a l&apos; }
          &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;[] =&amp;gt; fun&lt;/span&gt; equ =&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;(_&lt;/span&gt; :: rst) =&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;fun&lt;/span&gt; equ =&amp;gt; exist &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; rst &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt; eq_refl).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This leaves us two goals to tackle.&lt;/p&gt;
&lt;p&gt;First, we need to discard the case where &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt; is the empty list.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;  a : Type
  l : {l : list a | ~ empty l}
  equ : proj1_sig l = []
  ============================
  {l&apos; : list a | exists a0 : a, proj1_sig l = a0 :: l&apos;}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  + &lt;span class=&quot;hljs-built_in&quot;&gt;destruct&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;as&lt;/span&gt; [l nempty]; &lt;span class=&quot;hljs-built_in&quot;&gt;cbn&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; *.
    &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; equ &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; nempty.
    &lt;span class=&quot;hljs-built_in&quot;&gt;exfalso&lt;/span&gt;.
    now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; nempty.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, we need to prove that the result we provide (&lt;code class=&quot;hljs language-coq&quot;&gt;rst&lt;/code&gt;) when the
list is not empty is correct with respect to the specification of
&lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs&quot;&gt;  a : Type
  l : {l : list a | ~ empty l}
  a0 : a
  rst : list a
  equ : proj1_sig l = a0 :: rst
  ============================
  exists a1 : a, proj1_sig l = a1 :: rst
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  + &lt;span class=&quot;hljs-built_in&quot;&gt;destruct&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;as&lt;/span&gt; [l nempty]; &lt;span class=&quot;hljs-built_in&quot;&gt;cbn&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; *.
    &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; equ.
    now &lt;span class=&quot;hljs-built_in&quot;&gt;exists&lt;/span&gt; a0.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&apos;s have a look at the extracted code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;(** val pop : &apos;a1 list -&amp;gt; &apos;a1 list **)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; pop = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Nil&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;hljs-comment&quot;&gt;(* absurd case *)&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Cons&lt;/span&gt; (a, l0) -&amp;gt; l0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If one tries to call &lt;code class=&quot;hljs language-coq&quot;&gt;pop nil&lt;/code&gt;, the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;assert&lt;/span&gt;&lt;/code&gt; ensures the call fails. Extra
information given by the sigma type has been stripped away. It can be
confusing, and in practice it means that, we you rely on the extraction
mechanism to provide a certified OCaml module, you &lt;em&gt;cannot expose
strongly specified functions in its public interface&lt;/em&gt; because nothing in the
OCaml type system will prevent a misuse which will in practice leads to an
&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;false&lt;/span&gt;&lt;/code&gt;. *)&lt;/p&gt;
&lt;h2&gt;Defining &lt;code class=&quot;hljs language-coq&quot;&gt;push&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;It is possible to specify &lt;code class=&quot;hljs language-coq&quot;&gt;push&lt;/code&gt; the same way &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt; has been. The only
difference is &lt;code class=&quot;hljs language-coq&quot;&gt;push&lt;/code&gt; accepts lists with no restriction at all. Thus, its
definition is a simpler, and we can write it without &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; push {a} (l : list a) (x : a)
  : { l&apos; | &lt;span class=&quot;hljs-type&quot;&gt;l&lt;/span&gt;&apos; = x :: l } :=
  exist &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; (x :: l) eq_refl.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the extracted code is just as straightforward.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; push l a =
  &lt;span class=&quot;hljs-type&quot;&gt;Cons&lt;/span&gt; (a, l)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Defining &lt;code class=&quot;hljs language-coq&quot;&gt;head&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Same as &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt; and &lt;code class=&quot;hljs language-coq&quot;&gt;push&lt;/code&gt;, it is possible to add extra information in the
type of &lt;code class=&quot;hljs language-coq&quot;&gt;head&lt;/code&gt;, namely the returned value of &lt;code class=&quot;hljs language-coq&quot;&gt;head&lt;/code&gt; is indeed the first value
of &lt;code class=&quot;hljs language-coq&quot;&gt;l&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Definition&lt;/span&gt; head {a} (l : list a | &lt;span class=&quot;hljs-type&quot;&gt;~ empty&lt;/span&gt; l)
  : { x | &lt;span class=&quot;hljs-type&quot;&gt;exists&lt;/span&gt; r, proj1_sig l = x :: r }.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&apos;s not a surprise its definition is very close to &lt;code class=&quot;hljs language-coq&quot;&gt;pop&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  &lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;match&lt;/span&gt; proj1_sig l &lt;span class=&quot;hljs-built_in&quot;&gt;as&lt;/span&gt; l&apos;
                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; proj1_sig l = l&apos; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          &lt;span class=&quot;hljs-built_in&quot;&gt;with&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;[] =&amp;gt; fun&lt;/span&gt; equ =&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          | &lt;span class=&quot;hljs-type&quot;&gt;x&lt;/span&gt; :: &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; =&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;fun&lt;/span&gt; equ =&amp;gt; exist &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;_&lt;/span&gt;
          &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt; eq_refl).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The proof is also very similar, and are left to read as an exercise for
passionate readers.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-coq&quot;&gt;  + &lt;span class=&quot;hljs-built_in&quot;&gt;destruct&lt;/span&gt; l &lt;span class=&quot;hljs-built_in&quot;&gt;as&lt;/span&gt; [l falso]; &lt;span class=&quot;hljs-built_in&quot;&gt;cbn&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; *.
    &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; equ &lt;span class=&quot;hljs-built_in&quot;&gt;in&lt;/span&gt; falso.
    &lt;span class=&quot;hljs-built_in&quot;&gt;exfalso&lt;/span&gt;.
    now &lt;span class=&quot;hljs-built_in&quot;&gt;apply&lt;/span&gt; falso.
  + &lt;span class=&quot;hljs-built_in&quot;&gt;exists&lt;/span&gt; l0.
    now &lt;span class=&quot;hljs-built_in&quot;&gt;rewrite&lt;/span&gt; equ.
&lt;span class=&quot;hljs-keyword&quot;&gt;Defined&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, the extracted code is as straightforward as it can get.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;hljs language-ocaml&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; head = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Nil&lt;/span&gt; -&amp;gt; &lt;span class=&quot;hljs-keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;hljs-comment&quot;&gt;(* absurd case *)&lt;/span&gt;
| &lt;span class=&quot;hljs-type&quot;&gt;Cons&lt;/span&gt; (a, l0) -&amp;gt; a
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Writing strongly specified functions allows for reasoning about the result
correctness while computing it. This can help in practice. However, writing
these functions with the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;refine&lt;/span&gt;&lt;/code&gt; tactic does not enable a very idiomatic
Coq code.&lt;/p&gt;
&lt;p&gt;To improve the situation, the &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt; framework distributed with the
Coq standard library helps, but it is better to understand what &lt;code class=&quot;hljs language-coq&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Program&lt;/span&gt;&lt;/code&gt;
achieves under its hood, which is basically what we have done in this article.&lt;/p&gt;
        
      </description>
    </item>
    
    
  </channel>
</rss>
