<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Specifying Functional Programs with Intuitionistic First Order Logic</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Marcin Benke</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Institute of Informatics, University of Warsaw</institution>
        </aff>
      </contrib-group>
      <fpage>57</fpage>
      <lpage>63</lpage>
      <abstract>
        <p>We propose a method of specifying functional programs (in a subset of Haskell) using intuitionistic first order logic, that works well for inductive datatypes, higher-order functions and parametric polymorphism.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>Will we ever know the answer to all questions? Can one enter the same river twice?
These and similar questions have been asked again and again since classical times and
even long before that. Yet, ironically, the classical logic views the world as fixed and
cognizable. A model of classical logic is a static structure with complete information
about the relations between its elements. Such is the outlook that justifies the infamous
tertium non datur: p ∨ ¬p.</p>
      <p>Software development is one of the disciplines, where one is recurringly, and often
painfully reminded that such outlook is not only idealized, but often naïve: there is no
one static world, but a multiverse of branching and merging worlds.; not only are we far
from having all answers, they are rarely final even when we have them. Indeed there is
a lot of bitter truth to the adage that the only constant is change.</p>
      <p>Intuitionistic logic offers an attractive option for modelling a world of constant
change and incomplete information. A Kripke model consists of a multitude of worlds,
connected by an ordering relation. This relation may be time (earlier and later state
of the world), but not necessarily so; nor need this order be linear; commit graph in a
version control system gives a decent approximation, though we can and sometimes do
consider models with infinite number of worlds as well.</p>
      <p>
        One of the selling points of functional programming is its potential for easier and
better specification and verification. While this potential is indisputable, the tools and
methods to realise it are still lacking (see e.g. [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]).
      </p>
      <p>
        Most approaches use either first order classical logic (e.g. [
        <xref ref-type="bibr" rid="ref2 ref6">2, 6</xref>
        ]) or higher-order
logic (e.g. [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ]). In this paper we propose a method of specifying Haskell programs
using intuitionistic first order logic, that works well for inductive datatypes,
higherorder functions and parametric polymorphism.
⋆ This work has been supported by Polish NCN grant NCN 2012/07/B/ST6/01532.
      </p>
    </sec>
    <sec id="sec-2">
      <title>The Logic</title>
      <sec id="sec-2-1">
        <title>Core Logic</title>
        <p>
          Our logic is essentially a variant of λP from [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ] extended with constructs for existential
quantifiers, alternative, conjunction and falsity. There are no separate constructs for
implication or negation, as these can be easily encoded.
        </p>
        <p>Γ ::= {} | Γ, (x : φ) | Γ, (α : κ)
κ ::= ∗ | (Πx : φ)κ
φ ::= α | (∀x : φ)φ | φM | (∃x : φ)φ | φ ∧ φ | φ ∨ φ | ⊥
M ::= x | (λx : φ.M ) | (M1M2) | [M1, M2]∃x:φ.φ |
abstract hx : φ1, y : φ2i = M1 in M2 | hM1, M2iφ1∧φ2 |
π1M | π2M | in1,φ1∨φ2 M | in2,φ1∨φ2 M |
case M1 in (left x : φ1.M2)(right y : φ2.M3)
εφ(M )
2.2</p>
      </sec>
      <sec id="sec-2-2">
        <title>Notation and Extensions</title>
        <sec id="sec-2-2-1">
          <title>Additional connectives</title>
          <p>a → b ≡ ∀:a.b
a ↔ b ≡ a → b ∧ b → a</p>
          <p>¬a ≡ a → ⊥
A Universe of values V : ⋆ is assumed. Quantifiers usually range over this universe,
hence</p>
          <p>∀x.φ ≡ ∀x : V.φ</p>
        </sec>
        <sec id="sec-2-2-2">
          <title>Axiom schemas of the form</title>
          <p>schema name(P:kind) : formula
are to be understood as a finite set of formulas: one for every predicate symbol of the
appropriate kind in the signature.</p>
          <p>
            Proof sketches are rendered in a Mizar-like notation (cf. e.g. [
            <xref ref-type="bibr" rid="ref3 ref7">3, 8, 7</xref>
            ])
Haskell code is written using so called “Bird-tracks”, e.g.
&gt; id :: a -&gt; a
&gt; id x = x
Note The approach described in this document is “untyped” in the sense that we don’t
use Haskell type declarations, but derive our own types. Hence we might call our
approach “owntyped” (there is also a strong connection with refinement types). On the
other hand, we still use data type declaration as a source of useful information.
          </p>
        </sec>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>3 Datatypes</title>
      <p>In this section we illustrate our method on some example Haskell datatypes and
functions, starting with the simplest ones and progrsssing towards more complex ones.
3.1 Bool
&gt; data Bool = False | True
We can characterize Bool by the following axiom
axiom defBool : ∀ x. Bool(x) ↔ x=False ∨ x=True
or by an axiom schema
schema elimBool(P):
(P(False) ∧ P(True)) → ∀ x. Bool(x) → P(x)</p>
      <p>Now consider the following definition
bnot False = True
bnot True = False
This definition can be characterized as follows
axiom defBnot : bnot False = True ∧ bnot True = False
Now let’s prove that not takes Bool to Bool (in Mizar-like notation):
theorem typeBnot : ∀ x.Bool(x) → Bool(bnot x)
proof
consider x st Bool(x)
then x = False ∨ x = True by defBool
thus thesis by cases
suppose x = False
then bnot x = True
then thesis
suppose x = True
then bnot x = False
thus thesis
end</p>
      <p>An alternative proof of typeBnot, using elimBool
theorem typeBnot : ∀ x.Bool(x) → Bool(bnot x)
proof
consider x st Bool(x)
Bool(True) ∧ Bool(False) by defBool
then Bool(bnot False) ∧ Bool(bnot True) by defBnot
let P(x) = Bool(bnot x)
thus thesis by elimBool(P)
end</p>
      <p>This seems like an overkill and can probably be proved automatically. However,
note that our statement is substantially stronger than a simple type assertion: it also
states that bnot terminates for all inputs. Now, what about a function that doesn’t?
Consider
bad True = True
bad False = bad False
In Haskell, bad :: Bool -&gt; Bool, but a theorem like</p>
      <p>∀ x.Bool(x) → Bool(bad x)
is not provable. On the other hand, we can prove
theorem notSoBad : ∀ x.(Bool(x) ∧ x /= False)</p>
      <p>→ Bool(bad x)
3.2 Nat
&gt; data Nat where { Z :: Nat; S :: Nat → Nat }
env Z, S : V
axiom introNat : Nat(Z) ∧ ∀ n. Nat n → Nat (S n)
schema elimNat (P:V→*) =
( P Z
&amp; ∀ n. Nat n → P n → P(S n)
) → ∀ m. P m</p>
      <p>Alternative (and equivalent?) elimination
schema elimNat (P:V→*) =
( P Z
&amp; (∀ n. P n → P (S n))
) → ∀ m. P m</p>
      <p>Now we can define some functions
&gt; plus Z x = x
&gt; plus (S n) x = S(plus n x)
axiom plusDef : ∀ x.plus Z x = x</p>
      <p>∧ ∀ n x.plus (S n) x = S(plus n x)
Some properties
theorem plusType : ∀ x y. Nat(x) → Nat(y) → Nat(plus x
y)
proof
∀ y. plus Z y = y by plusDef
then ∀ y.Nat(y) → Nat(plus Z y)
∀ n x. Nat(plus n x) → Nat(S (plus n x)) by introNat
then ∀ n x. Nat(plus n x) → Nat(plus (S n) x) by
plusDef
thus thesis by elimNat(P) where</p>
      <p>P n = ∀ y.Nat(plus n y)
end
predicate PlusZ(n : V) = plus x Z = x
theorem plusZR : ∀ n. Nat(n) → plusZ(n)
proof
plus Z Z = Z by plusDef
∀ n.plus n Z = n → S(plus n Z) = S n by equality
∀ n.plus (S n) Z = S(plus n Z) by plusDef
then ∀ n.plus n Z = n → plus (S n) Z = S n
thus thesis by elimNat(plusZ)
end</p>
      <sec id="sec-3-1">
        <title>3.3 Lists</title>
        <p>To avoid confusion, we write the list type as List a and the corresponding predicate
asListratherthanusetheusual[a].Inpracticethisisjustamatterofsyntacticsugar.
&gt; data List a = Nil | Cons a (List a)</p>
        <p>Lists can be axiomatised as follows:
env List : V→* → *, Nil : V, Cons : V → V
schema introList(T:V→*)
= List(T)([])
&amp; (T(x)&amp;List(xs) → List(Cons x xs))
schema elimList(T,P:V→*)
= P(Nil)
&amp; (∀ x xs. T(x) &amp; P (xs) → P(Cons x xs))
→ ∀ xs. List(T)(xs) → P(xs)</p>
        <p>Sample theorem for map
&gt; id x = x
&gt; map f Nil = Nil
&gt; map f (Cons x xs) = Cons (f x) (map f xs)
axiom mapDef : map f Nil = Nil &amp; ∀ f x xs...
theorem mapType(T,U: V→*) : (∀ x. T(x) → U(f x))
→ (∀ xs. List(T)(xs) → List(U)</p>
        <p>(map f xs))
theorem mapId(T:V→*) : ∀ xs.map id xs = xs
proof
let P(xs:V) = map id xs = xs
have P(Nil) &amp; ∀ x xs.P(xs) → P(Cons x xs) by mapDef
thus thesis by elimList(P)</p>
        <p>Consider a (slightly convoluted) example of a function summing a list:
sum :: List Nat -&gt; Nat
sum Nil = Z
sum (Cons n ns) = case n of</p>
        <p>Z -&gt; sum (Cons n ns)
(S m) -&gt; S(sum (Cons m ns))
this can be characterized as follows:
axiom sumNil : sum Nil = Z
axiom sumCons
: ∀ n ns. (n = Z → sum (Cons n ns) = sum ns)
∧ (∀ m. n = S m</p>
        <p>→ sum (Cons n ns) = S(sum (Cons n ns))</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>4 Polymorphic Functions</title>
      <p>If types translate to predicates, then one might think quantification over types might
requiring quantifying over predicates. But we may avoid this reading “for all types a
and values x of type a” as simply “for all x (regardless of type)”.
const :: a -&gt; b -&gt; a
const x y = x
--# axiom forall x y. const x y = x</p>
    </sec>
    <sec id="sec-5">
      <title>5 Conclusions and Future Work</title>
      <p>WehaveproposedamethodofspecifyingHaskellprogramsusingintuitionisticfirstorderlogic,thatworkswellforinductivedatatypes,higher-orderfunctionsandparametric
polymorphism.Ontheotherhand,onebigremainingchallengeishandlingalsoad-hoc
polymorphism, i.e. type classes. One idea we’ve toyed with went along the following
lines (in a notation slightly different to what we have used so far):
class Functor f where</p>
      <p>fmap :: forall a b.(a-&gt;b) -&gt; f a -&gt; f b
-- fmap_id :: forall a.f a -&gt; Prop
-- # require fmap_id = forall x. fmap id x === x
instance Functor Maybe where
fmap f Nothing = Nothing
fmap f (Just x) = Just f x
-- # axiom Functor_im(Nothing)
-- # axiom forall x.Functor_im(Just x)
-- # conjecture forall i. Functor_im(i) -&gt; fmap id i = i
This is not yet completely satisactory and needs more work.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Baranowski</surname>
          </string-name>
          , W.:
          <article-title>Automated verification tools for Haskell programs</article-title>
          .
          <source>Master's thesis</source>
          , University of Warsaw (
          <year>2014</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Claessen</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Johansson</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Rosén</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Smallbone</surname>
          </string-name>
          , N.:
          <article-title>Hipspec: Automating inductive proofs of program properties</article-title>
          .
          <source>In: In Workshop on Automated Theory eXploration: ATX</source>
          <year>2012</year>
          (
          <year>2012</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Grabowski</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kornilowicz</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Naumowicz</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          :
          <article-title>Mizar in a nutshell</article-title>
          .
          <source>Journal of Formalized Reasoning</source>
          <volume>3</volume>
          (
          <issue>2</issue>
          ) (
          <year>2010</year>
          ), http://jfr.unibo.it/article/view/1980
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Sonnex</surname>
            ,
            <given-names>W.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Drossopoulou</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Eisenbach</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          : Zeno:
          <article-title>An automated prover for properties of recursive data structures</article-title>
          . In: Flanagan,
          <string-name>
            <given-names>C.</given-names>
            ,
            <surname>König</surname>
          </string-name>
          ,
          <string-name>
            <surname>B</surname>
          </string-name>
          . (eds.)
          <article-title>Tools and Algorithms for the Construction</article-title>
          and
          <source>Analysis of Systems, Lecture Notes in Computer Science</source>
          , vol.
          <volume>7214</volume>
          , pp.
          <fpage>407</fpage>
          -
          <lpage>421</lpage>
          . Springer Berlin Heidelberg (
          <year>2012</year>
          ), http://dx.doi.org/10.1007/978- 3-
          <fpage>642</fpage>
          -28756-5_
          <fpage>28</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Sørensen</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Urzyczyn</surname>
            ,
            <given-names>P.:</given-names>
          </string-name>
          <article-title>Lectures on the Curry-Howard Isomorphism, Studies in Logic and the Foundations of Mathematics</article-title>
          , vol.
          <volume>149</volume>
          .
          <string-name>
            <surname>Elsevier</surname>
          </string-name>
          (
          <year>2006</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Vytiniotis</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Jones</surname>
            ,
            <given-names>S.P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Rosén</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Claessen</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          :
          <article-title>Halo: Haskell to logic through denotational semantics</article-title>
          .
          <source>Acm Sigplan Notices</source>
          <volume>48</volume>
          :1, s.
          <fpage>431</fpage>
          -
          <lpage>442</lpage>
          (
          <year>2013</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7. Wenzel, M.,
          <string-name>
            <surname>Wiedijk</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          :
          <article-title>A comparison of Mizar and Isar</article-title>
          .
          <source>J. Autom. Reasoning</source>
          <volume>29</volume>
          (
          <issue>3-4</issue>
          ),
          <fpage>389</fpage>
          -
          <lpage>411</lpage>
          (
          <year>2002</year>
          ), http://dx.doi.org/10.1023/A:
          <fpage>1021935419355</fpage>
          <lpage>8</lpage>
          .
          <string-name>
            <surname>Wiedijk</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          :
          <article-title>Formal proof sketches</article-title>
          . In: Berardi,
          <string-name>
            <given-names>S.</given-names>
            ,
            <surname>Coppo</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            ,
            <surname>Damiani</surname>
          </string-name>
          ,
          <string-name>
            <surname>F</surname>
          </string-name>
          . (eds.)
          <article-title>Types for Proofs and Programs</article-title>
          , International Workshop, TYPES 2003, Torino, Italy, April 30 - May 4,
          <year>2003</year>
          ,
          <source>Revised Selected Papers. Lecture Notes in Computer Science</source>
          , vol.
          <volume>3085</volume>
          , pp.
          <fpage>378</fpage>
          -
          <lpage>393</lpage>
          . Springer (
          <year>2003</year>
          ), http://dx.doi.org/10.1007/978-3-
          <fpage>540</fpage>
          -24849-1_
          <fpage>24</fpage>
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>