<!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>Certi ed proofs in programs involving exceptions</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Jean-Guillaume Dumas</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Dominique Duval</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Burak Ekici</string-name>
          <email>Burak.Ekicig@imag.fr</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Jean-Claude Reynaud</string-name>
          <email>Jean-Claude.Reynaud@imag.fr</email>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Laboratoire J. Kuntzmann, Universite de Grenoble.</institution>
          <addr-line>51, rue des Mathematiques, umr CNRS 5224, bp 53X, F38041 Grenoble</addr-line>
          ,
          <country country="FR">France</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Reynaud Consulting</institution>
          ,
          <addr-line>RC</addr-line>
        </aff>
      </contrib-group>
      <abstract>
        <p>Exception handling is provided by most modern programming languages. It allows to deal with anomalous or exceptional events which require special processing. In computer algebra, exception handling is an e cient way to implement the dynamic evaluation paradigm: for instance, in linear algebra, dynamic evaluation can be used for applying programs which have been written for matrices with coe cients in a eld to matrices with coe cients in a ring. Thus, a proof system for computer algebra should include a treatement of exceptions, which must rely on a careful description of a semantics of exceptions. The categorical notion of monad can be used for formalizing the raising of exceptions: this has been proposed by Moggi and implemented in Haskell. In this paper, we provide a proof system for exceptions which involves both raising and handling, by extending Moggi's approach. Moreover, the core part of this proof system is dual to a proof system for side e ects in imperative languages, which relies on the categorical notion of comonad. Both proof systems are implemented in the Coq proof assistant.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>Using exceptions is an e cient way to simultaneously compute with dynamic
evaluation in exact linear algebra. For instance, for computing the rank of a
matrix, a program written for matrices with coe cients in a eld can easily be
modi ed by adding an exception treatment in order to be applied to a matrix
with coe cients in a ring: the exception mechanism provides an automatic case
distinction whenever it is required. The question that arises then is how to prove
correctness of the new algorithm. It would be nice to design proofs with two
levels, as we designed the algorithms: a rst level without exceptions, then a
second level taking the exceptions into account. In this paper, we propose a
proof system following this principle.</p>
      <p>Exceptions form a computational e ect, in the sense that a syntactic
expression f : X ! Y is not always interpreted as a function Jf K : JXK ! JY K (where,
as usual, the sets JXK and JY K denote the interpretations of the types X and
Y ). For instance a function which raises an exception can be interpreted as a
function Jf K : JXK ! JY K + Exc where Exc is the set of exceptions and \+"
denotes the disjoint union. In a programming language, exceptions usually di er
from errors in the sense that it is possible to recover from an exception while this
is impossible for an error; thus, exceptions have to be both raised and handled.</p>
      <p>The fundamental computational e ect is the evolution of states in an
imperative language, when seen from a functional point of view. There are several
frameworks for combining functional and imperative programming: the e ect
systems classify the expressions according to the way they interact with the
store [11], while the Kleisli category of the monad of states (X St )St (where
St is the set of states) also provides a classi cation of expressions [14]; indeed,
both approaches are related [20]. Lawvere theories were proposed for dealing
with the operations and equations related to computational e ects [16,12].
Another related approach, based on the fact that the state is observed, relies on the
classi cation of expressions provided by the coKleisli category of the comonad
of states X St and its associated Kleisli-on-coKleisli category [4].</p>
      <p>The treatment of exceptions is another fundamental computational e ect.
It can be studied from the point of view of the monad of exceptions X + Exc
(where Exc is the set of exceptions), or with a Lawvere theory, however in these
settings it is di cult to handle exceptions because this operation does not have
the required algebraicity property [17,18], in contrast with raising exceptions,
looking up and updating states. This makes exceptions much more di cult to
formalize than other e ects in the Lawvere theory framework. This issue has
been circumvented in [19] in order to get a Hoare logic for exceptions, in [13] by
using both algebras and coalgebras and in [18] by introducing handlers. The
formalization of exceptions can also be made from a coalgebraic point of view [10].
In this paper we extend Moggi's original approach: we use the classi cation of
expressions provided by the Kleisli category of the monad of exceptions and its
associated coKleisli-on-Kleisli category; moreover, we use the duality between
states and exceptions discovered in [3]. However, it is not necessary to know
about monads or comonads for reading this paper: the de nitions and results
are presented in an elementary way, in terms of equational theories.</p>
      <p>In Section 2 we give a motivating example for the use of exceptions as an
e cient way to compute with dynamic evaluation in exact linear algebra. Then
in Section 3 we de ne the syntax of a simple language for dealing with exceptions.
The intended denotational semantics is described in Section 4: we dissociate the
core operations for switching between exceptions and non-exceptional values, on
one side, from their encapsulation in the raising and handling operations, on the
other side. In Section 5 we add decorations to the syntax, in order to classify the
expressions of the language according to their interaction with the exceptions.
Decorations extend the syntax much like compiler quali ers or speci ers (e.g.,
like the throw annotation in C++ functions' signatures). In addition, we also
decorate the equations, which provides a proof system for dealing with this
decorated syntax. The main result of the paper is that this proof system is sound
with respect to the semantics of exceptions (Theorem 1). A major property of
this new proof system is that we can separate the veri cation of the proofs in
two steps: a rst step checks properties of the programs whenever there is no
e ect, while a second step takes the e ects into account via the decorations.
Several properties of exceptions have been proven using this proof system, and
these proofs have been veri ed in the Coq proof assistant.
2</p>
    </sec>
    <sec id="sec-2">
      <title>Rank computations modulo composite numbers</title>
      <p>Rank algorithms play a fundamental role in computer algebra. For instance,
computing homology groups of simplicial complexes reduces to computing ranks
and integer Smith normal forms of the associated boundary matrices [6]. One of
the most e cient method for computing the Smith normal form of such boundary
matrices also reduces to computing ranks but modulo the valence, a product
of the primes involved in the Smith form [7]. Now rank algorithms (mostly
Gaussian elimination and Krylov based methods) work well over elds (note
that Gaussian elimination can be adapted modulo powers of primes [7, §5.1]).
Modulo composite numbers, zero divisors arise. Gauss-Bareiss method could
be used but would require to know the determinant in advance, with is more
di cult than the valence. The strategy used in [7] is to factor the valence, but
only partially (factoring is still not polynomial). The large remaining composite
factors will have very few zero divisors and thus Gaussian elimination or Krylov
methods will have very few risks of falling upon them. Thus one can use dynamic
evaluation: try to launch the rank algorithm modulo this composite number with
large prime factors and \pretend" to be working over a eld [8]. In any case,
if a zero divisor is encoutered, then the valence has been further factored (in
polynomial time!) and the algorithm can split its computation independently
modulo both factors. An e ective algorithm design, here in C++, enabling this
dynamic evaluation with very little code modi cation, uses exceptions:
1. Add one exception at the arithmetic level, for signaling a tentative division
by a zero divisor, see Fig. 1.
inline Integer invmod(const Integer&amp; a, const Integer&amp; m) f</p>
      <sec id="sec-2-1">
        <title>Integer gcd,u,v; ExtendedEuclideanAlgorithm(gcd,u,v,a,m);</title>
        <p>if (gcd != 1) throw ZmzInvByZero(gcd);
return u&gt;0?u:u+=m;
g
2. Catch this exception inside the rank algorithm and throw a new exception
with the state of the rank iteration, see Fig. 2 (in our implementation the
class zmz wraps integers modulo m, the latter modulus being a static global
variable).
3. Then it is su cient to wrap the rank algorithm with a dynamic evaluation,
splitting the continuation modulo both factors, see Fig. 3.
try f</p>
        <p>
          invpiv = zmz(
          <xref ref-type="bibr" rid="ref1">1</xref>
          ) / A[k][k];
g catch (ZmzInvByZero e) f
        </p>
        <p>throw GaussNonInvPivot(e.getGcd(), k, currentrank);
void DynEvalRank(RankPairs&amp; vectranks, size t addedrank, ZmzMatrix&amp; A) f
try f
long rank = gaussrank(A); // in place modi cations of A
vectranks.push back(RankPair(rank+addedrank, zmz::getModulus()) );
g catch (GaussNonInvPivot e) f // Split
long mymodulus1 = zmz::getModulus()/e.getGcd();
long mymodulus2 = zmz::getModulus()/mymodulus1;
// Homomorphism on the (n-k) (m-k) remaining block
zmz::setModulus( mymodulus1 );</p>
      </sec>
      <sec id="sec-2-2">
        <title>ZmzMatrix M1(A,e.getIndex(),e.getIndex());</title>
        <p>DynEvalRank(vectranks, e.getCurrentRank(), M1);
// Homomorphism on the (n-k) (m-k) remaining block
zmz::setModulus( mymodulus2 );</p>
      </sec>
      <sec id="sec-2-3">
        <title>ZmzMatrix M2(A,e.getIndex(),e.getIndex());</title>
        <p>DynEvalRank(vectranks, e.getCurrentRank(), M2);
g g</p>
        <p>The advantage of using exceptions over other software design is twofold: rst,
throwing an exception at the arithmetic level and not only on the inverse call
in the rank algorithm allows to prevent that other unexpected divisions by zero
divisors go unnoticed; second, re-throwing a new exception in the rank algorithm
allows to keep its speci cations unchanged. It also enables to keep the modi
cations of rank algorithms to a minimum and to clearly separate normal behavior
with primes from the exceptional handling of splitting composite moduli.</p>
        <p>In the following, we propose a proof system with decorations, so that proofs
can easily be made in two steps: a rst step without exceptions, that is, just
preserving an initial proof of the rank algorithm; then a second level taking the
exceptions into account.</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>Syntax for exceptions</title>
      <p>The syntax for exceptions in computer languages depends on the language: the
keywords for raising exceptions may be either raise or throw, and for handling
exceptions they may be either handle, try-with, try-except or try-catch,
for instance. In this paper we rather use throw and try-catch. The syntax
for dealing with exceptions may be described in two parts: a basic part which
deals with the basic data types and an exceptional part for raising and handling
exceptions.</p>
      <p>The language we develop is quite close to the Ynot language [15]. Like Ynot,
our approach is "a generalization of the well-known ideas from type-and-e ect
systems and their monadic counterparts". However, we can use comonads as
well as monads, and we o er more modularity: in Ynot there is a single monad
encompassing all e ects, while we are able to compose e ects, thus enabling to
prove programs in several simpler stages.</p>
      <p>The basic part of our syntax is a signature Sigbase , made of types (or sorts)
and operations. The exceptional types form a subset T of the set of types of
Sigbase . For instance in C++ any type (basic type or class) is an exceptional
type, while in Java there is a base class for exceptional types, such that the
exceptional types are precisely the subtypes of this base class. Now, we assume
that some basic signature Sigbase and some set of exceptional types T have been
chosen.</p>
      <p>The signature Sigexc for exceptions is made of Sigbase together with the
operations for raising and handling exceptions, as follows.</p>
      <p>De nition 1. The signature for exceptions Sigexc is made of Sigbase with the
following operations: a raising operation for each exceptional type T and each
type Y :</p>
      <p>throwT;Y : T ! Y ;
and a handling operation for each Sigexc-term f : X ! Y , each non-empty list of
exceptional types (Ti)1 i n and each family of Sigexc-terms (gi : Ti ! Y )1 i n:
tryff g catch fTi ) gig1 i n : X ! Y :</p>
      <p>An important, and somewhat surprising, feature of a language with
exceptions is that all expressions in the language, including the try-catch expressions,
propagate exceptions. Indeed, if an exception is raised before some try-catch
expression is evaluated, this exception is propagated. In fact, the catch block in a
try-catch expression may recover from exceptions which are raised inside the try
block, but the catch block alone is not an expression of the language. This means
that the operations for catching exceptions are private operations: they are not
part of the signature for exceptions. More precisely, the operations for raising
and handling exceptions can be expressed in terms of a private empty type and
two families of private operations: the tagging operations for creating exceptions
and the untagging operations for catching them (inside the catch block of any
try-catch expression). The tagging and untagging operations are called the core
operations for exceptions. They are not part of Sig exc, but the interpretations
of the operations for raising and handling exceptions, which are part of Sig exc,
are de ned in terms of the interpretations of the core operations. The meaning
of the core operations is given in Section 4.</p>
      <p>De nition 2. Let Sig exc be the signature for exceptions. The core of Sig exc is
the signature Sig core made of Sig base with a type 0 called the empty type and
two operations for each exceptional type T :</p>
      <p>tagT : T ! 0 and untagT : 0 ! T
where tagT is called the exception constructor or the tagging operation and
untagT is called the exception recovery or the untagging operation.
4</p>
    </sec>
    <sec id="sec-4">
      <title>Denotational semantics for exceptions</title>
      <p>In this Section we de ne a denotational semantics for exceptions which relies
on the common semantics of exceptions in various languages, for instance in
C++ [1, Ch. 15], Java [9, Ch. 14] or ML.</p>
      <p>The basic part of the syntax is interpreted in the usual way: each type X is
interpreted as a set JXK and each operation f : X ! Y of Sig base as a function
Jf K : JXK ! JY K. But, when f : X ! Y in Sig exc is a raising or handling
operation, or when f : X ! Y in Sig core is a tagging or untagging operation, it
is not interpreted as a function Jf K : JXK ! JY K: this corresponds to the fact that
the exceptions form a computational e ect. The distinction between ordinary and
exceptional values is discussed in Subsection 4.1. Then, denotational semantics
of raising and handling exceptions are considered in Subsections 4.2 and 4.3,
respectively. We assume that some interpretation of Sig base has been chosen.
4.1</p>
      <p>Ordinary values and exceptional values
In order to express the denotational semantics of exceptions, a fundamental point
is to distinguish between two kinds of values: the ordinary (or non-exceptional)
values and the exceptions. It follows that the operations may be classi ed
according to the way they may, or may not, interchange these two kinds of values:
an ordinary value may be tagged for constructing an exception, and later on the
tag may be cleared in order to recover the value; then we say that the exception
gets untagged. Let Exc be a set, called the set of exceptions. For each set A,
the set A + Exc is the disjoint union of A and Exc and the canonical inclusions
are denoted normal A : A ! A + Exc and abrupt A : Exc ! A + Exc. For each
functions f : A ! B and g : Exc ! B, we denote by [f jg] : A + Exc ! B the
unique function such that [f jg] normal A = f and [f jg] abrupt A = g.</p>
      <sec id="sec-4-1">
        <title>De nition 3. An element of A + Exc is an ordinary value if it is in A and an</title>
        <p>exceptional value if it is in Exc. A function ' : A + Exc ! B + Exc:
{ raises an exception if there is some x 2 A such that '(x) 2 Exc.
{ recovers from an exception if there is some e 2 Exc such that '(e) 2 B.
{ propagates exceptions if '(e) = e for every e 2 Exc.</p>
        <p>Clearly, a function ' : A + Exc ! B + Exc which propagates exceptions may
raise an exception, but cannot recover from an exception. Such a function '
is characterized by its restriction 'jA : A ! B + Exc, since its restriction on
exceptions 'jExc : Exc ! B + Exc is the inclusion abrupt B of Exc in B + Exc.</p>
        <p>In the denotational semantics for exceptions, we will see that a term f : X !
Y of Sig exc or Sig core may be interpreted either as a function Jf K : JXK ! JY K or
aHsoawfeuvnecr,tiionnaJlfl Kc a:sJeXs,Kit!isJYpoKs+siEblxec toor caosnavfeurtncJtfioKntoJfaK f:uJnXctKi+onEfxrcom! JJXY KK++EExxcc.
to JY K + Exc, as follows.</p>
      </sec>
      <sec id="sec-4-2">
        <title>De nition 4. The upcasting conversions are the following transformations:</title>
        <p>{ every function ' : A ! B gives rise to ' = normal B ' : A ! B + Exc,
{ every function : A ! B + Exc gives rise to  = [ jabrupt B] : A + Exc !</p>
      </sec>
      <sec id="sec-4-3">
        <title>B + Exc, which is equal to on A and which propagates exceptions;</title>
        <p>{ it follows that every function ' : A ! B gives rise to ' = ( ') =
[normal B 'jabrupt B] = ' + id Exc : A + Exc ! B + Exc, which is equal to
' on A and which propagates exceptions.</p>
        <p>Since the upcasting conversions are safe (i.e., injective), when there is no
ambiguity the symbols ,  and may be omitted. In this way, for each f : X ! Y
and g : Y ! Z, whatever their e ects, we get Jf K : JXK + Exc ! JY K + Exc and
JagnKd: SJiYgKco+reEcxacn!beJZinKt+erEpxrect,ewdhbicyh crasnt bcoencvoemrtpinogsedth. eTihnutse,rpevreetrayttioenrmofofeaScighexocf
its components f : X ! Y to a function Jf K : JXK + Exc ! JY K + Exc. For
Sig exc , this coincides with the Kleisli composition associated to the exception
monad A + Exc [14].</p>
        <p>We will also use the following conversion.</p>
      </sec>
      <sec id="sec-4-4">
        <title>De nition 5. The downcasting conversion is the following transformation:</title>
        <p>{ every function : A + Exc ! B + Exc gives rise to  = normal A : A !</p>
      </sec>
      <sec id="sec-4-5">
        <title>B + Exc which is equal to on A and which propagates exceptions.</title>
        <p>This conversion is unsafe: di erent 's may give rise to the same  .
4.2</p>
        <p>Tagging and raising exceptions
Raising exceptions relies on the interpretation of the tagging operations. The
interpretation of the empty type 0 is the empty set ;; thus, for each type X the
interpretation of 0 + X can be identi ed with JXK.</p>
        <p>De nition 6. Let Exc be the disjoint union of the sets JT K for all the exceptional
types T . Then, for each exceptional type T , the interpretation of the tagging
operation tagT : T ! 0 is the coprojection function</p>
        <p>JtagT K : JT K ! Exc :
Thus, the tagging function JtagT K : JT K ! Exc maps a non-exceptional value
(or parameter ) a 2 JT K to an exception JtagT K(a) 2 Exc. We can now de ne the
raising of exceptions in a programming language.</p>
      </sec>
      <sec id="sec-4-6">
        <title>De nition 7. For each exceptional type T and each type Y , the interpretation</title>
        <p>of the raising operation throwT ;Y is the tagging function JtagT K followed by the
inclusion of Exc in JY K + Exc:</p>
        <p>JthrowT ;Y K = abrupt JY K JtagT K : JT K ! JY K + Exc :
4.3</p>
        <p>Untagging and handling exceptions
Handling exceptions relies on the interpretation of the untagging operations for
clearing the exception tags.</p>
      </sec>
      <sec id="sec-4-7">
        <title>De nition 8. For each exceptional type T , the interpretation of the untagging</title>
        <p>operation untagT : 0 ! T is the function
which satis es for each exceptional type R:</p>
        <p>JuntagT K JtagRK =</p>
        <p>JuntagT K : Exc ! J K</p>
        <p>T + Exc ;
(normal T</p>
        <p>J K
abrupt JT K JtagRK
when R = T
when R 6= T
Thus, the untagging function JuntagT K, when applied to any exception e, rst
tests whether e is in JT K; if this is the case, then it returns the parameter a 2 JT K
smuacihn tohfatJuen=tagJTtaKgTisK(Ea)x,c,otJhuenrtwaigsTe Kitispruonpiaqguaetleys
dtheteeremxcienpetdiobnye.itSsinrecsetrtihcetiodnosto all the exceptional types, and therefore by the equalities in De nition 8. For
handling exceptions of types T1; : : : Tn, raised by the interpretation of some term
f : X ! Y of Sig exc, one provides for each i in f1; : : : ; ng a term gi : Ti ! Y
of Sig exc (thus, the interpretation of gi may itself raise exceptions). Then the
handling process builds a function which rst executes f , and if f returns an
exception then maybe catches this exception. The catching part encapsulates some
untagging functions, but the resulting function always propagates exceptions.
De nition 9. For each term f : X ! Y of Sig exc, and each non-empty lists
(Ti)1 i n of exceptional types and (gi : Ti ! Y )1 i n of terms of Sig exc, let
(recover i)1 i n denote the family of functions de ned recursively by:
: R T + Exc:</p>
        <p>J K ! J K
recover i =
(
[ JgnK j abrupt Y ] untagTn
[ JgiK j recover i+1 ] untagTi
when i = n
when i &lt; n</p>
      </sec>
      <sec id="sec-4-8">
        <title>Then the interpretation of the handling operation is:</title>
        <p>: Exc ! Y + Exc
Jtry ff g catch fTi ) gig1 i nK = [ normal Y j recover 1 ]
f :</p>
        <p>J K
It should be noted that Jf K : JXK ! JY K + Exc and that similarly
Jtry ff g catch fTi ) gig1 i nK : JXK ! JY K + Exc. When n = 1 we get:
Jtry ff g catch fT ) ggK = [ normal Y j [ JgK j abrupt Y ] untagT ] Jf K :
This de nition matches that of Java exceptions [9, Ch. 14] or C++
exceptions [1, §15]. In particular, in the interpretation of try ff g catch fTi ) gig1 i n,
each function JgiK may itself raise exceptions; and the types T1; : : : ; Tn need not
be pairwise distinct, but if Ti = Tj for some i &lt; j then gj is never executed.
5</p>
      </sec>
    </sec>
    <sec id="sec-5">
      <title>A decorated equational proof system for exceptions</title>
      <p>In Sections 3 and 4 we have formalized the signature for exceptions Sig exc,
its associated core signature Sig core , and we have described their denotational
semantics. However the soundness property is not satis ed, in the sense that the
denotational semantics is not a model of the signature, in the usual sense: indeed,
a term f : X ! Y is not always interpreted as a function Jf K : JXK ! JY K;
it may be interpreted as Jf K : JXK ! JY K + Exc, or as Jf K : JXK + Exc !
JY K + Exc. In order to get soundness, in this Section we add decorations to the
signature for exceptions by classifying the operations and equations according
to the interaction of their interpretations with the mechanism of exceptions.</p>
      <p>Signature
(Section 3)
decoration
/ Decorated signature (sound)
(Section 5)
/ Semantics
(Section 4)
interpretation
5.1</p>
      <p>
        Decorations for exceptions
By looking at the interpretation (in Section 4) of the syntax for exceptions (from
Section 3), we get a classi cation of the operations and terms in three parts,
depending on their interaction with the exceptions mechanism. The terms are
decorated by (0), (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) and (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) used as superscripts, they are called respectively
pure terms, propagators and catchers, according to their interpretations:
(0) the interpretation of a pure term may neither raise exceptions nor recover
from exceptions,
(
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) the interpretation of a propagator may raise exceptions but is not allowed
to recover from exceptions,
(
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) the interpretation of a catcher may raise exceptions and recover from
exceptions.
      </p>
      <p>
        For instance, the decoration (0) corresponds to the decoration noexcept in C++
(replacement of the deprecated throw()) and the decoration (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) corresponds to
throw(...), still in C++. The decoration (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) is usually not encountered in the
language, since catching is the prerogative of the core untagging function, which
is private. Similarly, we introduce two kinds of equations between terms. This is
done by using two distinct relational symbols which correspond to two distinct
interpretations:
( ) a strong equation is an equality of functions both on ordinary values and
on exceptions
( ) a weak equation is an equality of functions only on ordinary values, but
maybe not on exceptions.
      </p>
      <p>The interpretation of these three kinds of terms and two kinds of equations is
summarized in Fig. 4. It has been shown in Section 4.1 that any propagator can
be seen as a catcher and that any pure term can be seen as a propagator and
thus also as a catcher. This allows to compose terms of di erent nature, so that
it is not a restriction to give the interpretation of the decorated equations only
when both members are catchers.</p>
      <p>X
X
X</p>
      <sec id="sec-5-1">
        <title>Syntax type</title>
        <p>X
term
f
f
f
term
term
/ Y
/ Y
/ Y</p>
      </sec>
      <sec id="sec-5-2">
        <title>Decorated syntax type</title>
        <p>X
X
X</p>
        <p>X
pure term</p>
        <p>f(0)
propagator</p>
        <p>
          f(
          <xref ref-type="bibr" rid="ref1">1</xref>
          )
catcher
f(
          <xref ref-type="bibr" rid="ref2">2</xref>
          )
/ Y
/ Y
/ Y
equation strong equation
f = g : X ! Y f (
          <xref ref-type="bibr" rid="ref2">2</xref>
          ) g(
          <xref ref-type="bibr" rid="ref2">2</xref>
          ) : X ! Y
equation weak equation
f = g : X ! Y f (
          <xref ref-type="bibr" rid="ref2">2</xref>
          ) g(
          <xref ref-type="bibr" rid="ref2">2</xref>
          ) : X ! Y
JXK
        </p>
        <p>JXK
X + Exc
J K</p>
      </sec>
      <sec id="sec-5-3">
        <title>Interpretation</title>
        <p>JXK</p>
        <p>JfK
JfK</p>
        <p>JfK
f = g
J K J K
/ Y</p>
        <p>J K
/ JY K + Exc</p>
        <p>/ JY K + Exc
Jf K
normal JXK = JgK
normal X</p>
        <p>J K</p>
        <p>Now we can add decorations to the signature for exceptions Sig exc and its
associated core signature Sig core , from De nitions 1 and 2.</p>
        <p>De nition 10. The decorated signature for exceptions Sig edxecco and its
associated decorated core signature Sig cdoerceo are made of Sig exc and Sig core ,
respectively, decorated as follows: the basic operations are pure, the tagging, raising and
handling operations are propagators, and the untagging operations are catchers.
5.2</p>
        <p>
          Decorated rules for exceptions
In this Section we de ne an equational proof system for exceptions. This proof
system is made of the rules in Fig. 5. It can be used for proving properties of
exceptions, for instance in the Coq proof assistant. In Fig. 5, the decoration
properties are often grouped with other properties: for instance, \f (
          <xref ref-type="bibr" rid="ref1">1</xref>
          ) g(
          <xref ref-type="bibr" rid="ref1">1</xref>
          )"
means \f (
          <xref ref-type="bibr" rid="ref1">1</xref>
          ) and g(
          <xref ref-type="bibr" rid="ref1">1</xref>
          ) and f g"; in addition, the decoration (
          <xref ref-type="bibr" rid="ref2">2</xref>
          ) is usually
dropped, since the rules assert that every term can be seen as a catcher; and
several rules with the same premisses may be grouped together: HC11::::::HCpn stands
for H1:::Hn ,. . . , H1:::Hn .
        </p>
        <p>C1 Cp</p>
        <p>Rules (a) are the usual rules for the monadic equational logic; they are valid
for all decorated terms and for strong equations. Rules (b) provide more
information on the decorated monadic equational logic for exceptions; in particular,
the substitution of f in a weak equation g1 g2 holds only when f is pure,
which is quite restrictive. Rules (c) ensure that the empty type is a kind of
initial type with respect to weak equations. Rules (d) are used for case distinctions
between exceptional arguments (on the \0" side of X + 0) and non-exceptional
arguments (on the \X" side of X + 0). The symbol  in rules (e) is interpreted
as the downcast conversion, see De nition 5. Rules in (f) mean that the tagging
operations are interpreted as the canonical inclusions of the exceptional types in
the set Exc, see De nition 6. The rules in (g) determine the untagging operations
up to strong equations: an untagging operation recovers the exception
parameter whenever the exception type is matched, and it propagates the exception
otherwise see De nition 8.</p>
        <p>Remark 1. It has been shown in [3] that the denotational semantics of the core
language for exceptions is dual to the denotational semantics for states: the
tagging and untagging operations are respectively dual to the lookup and update
operations. In fact, this duality is also valid for the decorated equational logics.</p>
        <p>This decorated proof system is used now (in De nition 12) for
constructing the raising and handling operations from the core tagging and untagging
operations. It has to be noted that the term catch fTi ) gig1 i n f may
catch exceptions, while the handling operation, which coincides with catch fTi )
gig1 i n f on non-exceptional values, must propagate exceptions; this is why
the downcast operator  is used.</p>
        <sec id="sec-5-3-1">
          <title>De nition 11. For each exceptional type T and each type Y , the raising oper</title>
          <p>
            ation throw T(1;)Y : T ! Y is the propagator de ned as:
throw T(1;)Y = [ ]Y
tagT :
De nition 12. For each propagator f (
            <xref ref-type="bibr" rid="ref1">1</xref>
            ) : X ! Y , each non-empty lists of types
(Ti)1 i n and propagators (gi(
            <xref ref-type="bibr" rid="ref1">1</xref>
            ) : Ti ! Y )1 i n, let catch fTi ) gig(
            <xref ref-type="bibr" rid="ref12">12</xref>
            )i n : Y !
Y denote the catcher de ned by:
catch fTi ) gig(
            <xref ref-type="bibr" rid="ref12">12</xref>
            )i n = [ id Y j recover 1 ]
(a) Monadic equational rules for exceptions ( rst part):
(b) Monadic equational rules for exceptions (second part):
where (recover i(
            <xref ref-type="bibr" rid="ref2">2</xref>
            ) : 0 ! Y )1 i n denotes the family of catchers de ned
recursively by:
recover i(
            <xref ref-type="bibr" rid="ref2">2</xref>
            ) =
(
gn
          </p>
          <p>
            untagTn
[gi j recover i+1 ] untagTi
when i = n;
when i &lt; n:
Then, the handling operation (tryff g catch fTi ) gig1 i n)(
            <xref ref-type="bibr" rid="ref1">1</xref>
            ) : X ! Y is the
propagator de ned by:
          </p>
          <p>tryff g catch fTi ) gig1 i n =  (catch fTi ) gig1 i n f )
When n = 1 we get:
tryff g catch fT ) gg =  (catch fT ) gg f ) =  ([ id Y j g untagT ] f ):
Now Theorem 1 derives easily, by induction, from Fig. 5 and 4 and from
De nitions 11 and 12.</p>
        </sec>
        <sec id="sec-5-3-2">
          <title>Theorem 1. The decorated rules for exceptions and the raising and handling constructions are sound with respect to the denotational semantics of exceptions.</title>
          <p>5.3</p>
          <p>
            A decorated proof: a propagator propagates
With these tools, it is now possible to prove properties of programs involving
exceptions and to check these proofs in Coq. For instance, let us prove that
given an exception, a propagator will do nothing apart from propagating it.
Recall that the interpretation of [ ]Z (or, more precisely, of [ ]Z ) is abrupt Z :
Exc ! JZK + Exc. J K
Lemma 1. For each propagator g(
            <xref ref-type="bibr" rid="ref1">1</xref>
            ) : X ! Y we have g [ ]X [ ]Y .
Proof. This lemma can be proved as follows; the labels refer to Fig. 5, and their
subscripts to the proof in Coq of Fig. 6.
          </p>
          <p>(c)1
(a)</p>
          <p>X
[ ]X : 0 ! X
(c)2
(b)4
g [ ]X : 0 ! Y
g [ ]X
The proof in Coq follows the same line as the mathematical proof above. It goes
as in Figure 6. The Coq library for exceptions, EXCEPTIONS-0.1.tar.gz, can
be found online: http://coqe ects.forge.imag.fr (in le Proofs.v) with proofs of
many other properties of programs involving exceptions.</p>
          <p>It should be recalled that any Coq proof is read from bottom up. The
application of the from empty is weakly unique rule certi es that the term
g o (@empty X), because its domain is the empty type, is weakly equal to
the term (@empty Y). Thus, we have the left side sub-proof: g o (@empty
X) (@empty Y). There, weak equality is converted into strong:
applying propagator weakeq is strongeq resolves the goal: g o (@empty X) ==
(@empty Y) and produces as sub-goals that there is no catcher involved in both
hand sides (middle and right side sub-proofs).</p>
        </sec>
      </sec>
      <sec id="sec-5-4">
        <title>Lemma propagator propagates 1:</title>
        <p>forall X Y (g: term Y X),
is propagator g -&gt; g o (@empty X) == (@empty Y).
Proof.</p>
        <p>intros.
(* (b)4 *) apply propagator weakeq is strongeq.
(* (b)2 *) apply is comp. assumption.
(* (b)1 *) apply is pure propagator. (* (c)3 *) apply is empty.
(* (b)3 *) apply is pure propagator. (* (c)4 *) apply is empty.
(* (c)2 *) apply from empty is weakly unique.</p>
        <p>Qed.</p>
        <p>JuntagT K JtagRK =
6</p>
      </sec>
    </sec>
    <sec id="sec-6">
      <title>Conclusion</title>
      <p>In object-oriented languages, exceptions are usually the objects of classes which
are related by a hierarchy of subclasses. Our framework can be extended in
this direction by introducing a hierarchy of exceptional types: the set T is
endowed with a partial order _ called the subtyping relation, and the
signature Sig base is extended with a cast operation castR;T : R ! T whenever
R _ T . The interpretation of castR;T is a pure function JcastR;T K : JRK ! JT K,
such that JcastT ;T K is the identity on JT K and when S _ R _ T then
tJhcaesftSu;nTcKtio=n JJucansttaRg;TTKK : EJxccas!tS;JRTK.K +DeExncitsioantis8 ehsafsortoeabche emxocedpi tieodnaalstyfopleloRws::
(normal JT K JcastR;T K
abrupt JT K JtagRK
when R _ T
otherwise
: R T +Exc:</p>
      <p>J K ! J K
Exceptions are part of most modern programming languages and they are useful
in computer algebra, typically for implementing dynamic evaluation. We have
presented a new framework for formalizing the treatment of exceptions. These
decorations form a bridge between the syntax and the denotational semantics
by turning the syntax sound with respect to the semantics, without adding any
explicit \type of exceptions" as a possible return type for the operations. The
salient features of our approach are:
{ We provide rules for equational proofs on programs involving exceptions
(Fig. 5) and an automatic process for interpreting these proofs (Fig. 4).
{ Decorating the equations allows to separate properties that are true only up
to e ects (weak equations) from properties that are true even when e ects
are considered (strong equations).
{ Moreover, the veri cation of the proofs can be done in two steps: in a rst
step, decorations are dropped and the proof is checked syntactically; in a
second step, the decorations are taken into account in order to prove properties
involving computational e ects.
{ The distinction between the language for exceptions and its associated
private core language (De nitions 1 and 2) allows to split the proofs in two
successive parts; in addition, the private part can be directly dualized from
the proofs on global states (relying on [3] and [4]).
{ A proof assistant can be used for checking the decorated proofs on
exceptions. Indeed the decorated proof system for states, as described in [4]
has been implemented in Coq [2] and dualized for exceptions (see http:
//coqeffects.forge.imag.fr).</p>
      <p>We have used the approach of decorated logic, which provides rules for
computational e ects by adding decorations to usual rules for \pure" programs.
This approach can be extended in order to deal with multivariate operations [5],
conditionals, loops, and high-order functions.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>Working</given-names>
            <surname>Draft</surname>
          </string-name>
          ,
          <article-title>Standard for Programming Language C++</article-title>
          . ISO/IEC JTC1/SC22/WG21 standard 14882:
          <year>2011</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Jean-Guillaume</surname>
            <given-names>Dumas</given-names>
          </string-name>
          , Dominique Duval, Burak Ekici,
          <string-name>
            <given-names>Damien</given-names>
            <surname>Pous</surname>
          </string-name>
          .
          <article-title>Formal veri cation in Coq of program properties involving the global state e ect</article-title>
          .
          <source>JFLA'13</source>
          ,
          <string-name>
            <surname>Frejus</surname>
          </string-name>
          , France,
          <year>2013</year>
          . arXiv:
          <volume>1310</volume>
          .
          <fpage>0794</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Jean-Guillaume</surname>
            <given-names>Dumas</given-names>
          </string-name>
          , Dominique Duval, Laurent Fousse,
          <string-name>
            <surname>Jean-Claude Reynaud</surname>
          </string-name>
          .
          <article-title>A duality between exceptions and states</article-title>
          .
          <source>Mathematical Structures in Computer Science</source>
          <volume>22</volume>
          , p.
          <fpage>719</fpage>
          -
          <lpage>722</lpage>
          (
          <year>2012</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Jean-Guillaume</surname>
            <given-names>Dumas</given-names>
          </string-name>
          , Dominique Duval, Laurent Fousse,
          <string-name>
            <surname>Jean-Claude Reynaud</surname>
          </string-name>
          .
          <article-title>Decorated proofs for computational e ects: States</article-title>
          .
          <source>ACCAT 2012. Electronic Proceedings in Theoretical Computer Science</source>
          <volume>93</volume>
          , p.
          <fpage>45</fpage>
          -
          <lpage>59</lpage>
          (
          <year>2012</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Jean-Guillaume</surname>
            <given-names>Dumas</given-names>
          </string-name>
          , Dominique Duval,
          <string-name>
            <surname>Jean-Claude Reynaud</surname>
          </string-name>
          .
          <article-title>Cartesian e ect categories are Freyd-categories</article-title>
          .
          <source>Journal of Symbolic Computation</source>
          <volume>46</volume>
          , p.
          <fpage>272</fpage>
          -
          <lpage>293</lpage>
          (
          <year>2011</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Jean-Guillaume</surname>
            <given-names>Dumas</given-names>
          </string-name>
          ,
          <string-name>
            <given-names>Frank</given-names>
            <surname>Heckenbach</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B. David</given-names>
            <surname>Saunders</surname>
          </string-name>
          ,
          <string-name>
            <given-names>and Volkmar</given-names>
            <surname>Welker</surname>
          </string-name>
          .
          <article-title>Computing simplicial homology based on e cient Smith normal form algorithms</article-title>
          .
          <source>In Michael Joswig and Nobuki Takayama</source>
          , editors,
          <source>Algebra, Geometry and Software Systems</source>
          , pages
          <fpage>177</fpage>
          {
          <fpage>206</fpage>
          . Springer, March
          <year>2003</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Jean-Guillaume Dumas</surname>
            ,
            <given-names>B. David</given-names>
          </string-name>
          <string-name>
            <surname>Saunders</surname>
            ,
            <given-names>and Gilles</given-names>
          </string-name>
          <string-name>
            <surname>Villard</surname>
          </string-name>
          .
          <article-title>On e cient sparse integer matrix Smith normal form computations</article-title>
          .
          <source>Journal of Symbolic Computation</source>
          ,
          <volume>32</volume>
          (
          <issue>1</issue>
          /2):
          <volume>71</volume>
          {
          <fpage>99</fpage>
          ,
          <string-name>
            <surname>July</surname>
            <given-names>{August</given-names>
          </string-name>
          <year>2001</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>Dominique</given-names>
            <surname>Duval</surname>
          </string-name>
          .
          <article-title>Simultaneous computations in elds of di erent characteristics</article-title>
          . In Erich Kaltofen and Stephen M. Watt, editors,
          <source>Computers and Mathematics</source>
          , pages
          <volume>321</volume>
          {
          <fpage>326</fpage>
          .
          <string-name>
            <surname>Springer</surname>
            <given-names>US</given-names>
          </string-name>
          ,
          <year>1989</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <given-names>James</given-names>
            <surname>Gosling</surname>
          </string-name>
          , Bill Joy, Guy Steele,
          <string-name>
            <given-names>Gilad</given-names>
            <surname>Bracha</surname>
          </string-name>
          .
          <article-title>The Java Language Speci cation, Third Edition</article-title>
          .
          <string-name>
            <surname>Addison-Wesley Longman</surname>
          </string-name>
          (
          <year>2005</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <given-names>Bart</given-names>
            <surname>Jacobs</surname>
          </string-name>
          .
          <article-title>A Formalisation of Java's Exception Mechanism</article-title>
          .
          <source>ESOP 2001. Springer Lecture Notes in Computer Science</source>
          <year>2028</year>
          . p.
          <fpage>284</fpage>
          -
          <lpage>301</lpage>
          (
          <year>2001</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <surname>John M. Lucassen</surname>
          </string-name>
          , David K.
          <article-title>Gi ord. Polymorphic e ect systems</article-title>
          .
          <source>POPL</source>
          <year>1988</year>
          . ACM Press, p.
          <fpage>47</fpage>
          -
          <lpage>57</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          12.
          <string-name>
            <surname>Martin</surname>
            <given-names>Hyland</given-names>
          </string-name>
          ,
          <source>John Power. The Category Theoretic Understanding of Universal Algebra: Lawvere Theories and Monads. Electronic Notes in Theoretical Computer Science</source>
          <volume>172</volume>
          , p.
          <fpage>437</fpage>
          -
          <lpage>458</lpage>
          (
          <year>2007</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          13. Paul Blain Levy.
          <article-title>Monads and adjunctions for global exceptions</article-title>
          .
          <source>MFPS 2006. Electronic Notes in Theoretical Computer Science</source>
          <volume>158</volume>
          , p.
          <fpage>261</fpage>
          -
          <lpage>287</lpage>
          (
          <year>2006</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          14.
          <string-name>
            <given-names>Eugenio</given-names>
            <surname>Moggi</surname>
          </string-name>
          .
          <source>Notions of Computation and Monads. Information and Computation</source>
          <volume>93</volume>
          (
          <issue>1</issue>
          ), p.
          <fpage>55</fpage>
          -
          <lpage>92</lpage>
          (
          <year>1991</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          15.
          <string-name>
            <surname>Aleksandar</surname>
            <given-names>Nanevski</given-names>
          </string-name>
          , Greg Morrisett, Avraham Shinnar, Paul Govereau, Lars Birkedal.
          <article-title>Ynot: Dependent Types for Imperative Programs</article-title>
          .
          <source>Proceedings of the 13th ACM SIGPLAN International Conference on Functional Programming (ICFP '08)</source>
          , p.
          <fpage>229</fpage>
          -
          <lpage>240</lpage>
          (
          <year>2008</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          16.
          <string-name>
            <surname>Gordon D. Plotkin</surname>
          </string-name>
          , John Power. Notions of Computation Determine Monads.
          <source>FoSSaCS 2002. Springer-Verlag Lecture Notes in Computer Science</source>
          <volume>2303</volume>
          , p.
          <fpage>342</fpage>
          -
          <lpage>356</lpage>
          (
          <year>2002</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          17.
          <string-name>
            <surname>Gordon D. Plotkin</surname>
          </string-name>
          , John Power. Algebraic Operations and Generic E ects.
          <source>Applied Categorical Structures</source>
          <volume>11</volume>
          (
          <issue>1</issue>
          ), p.
          <fpage>69</fpage>
          -
          <lpage>94</lpage>
          (
          <year>2003</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref18">
        <mixed-citation>
          18.
          <string-name>
            <surname>Gordon D. Plotkin</surname>
            ,
            <given-names>Matija</given-names>
          </string-name>
          <string-name>
            <surname>Pretnar</surname>
          </string-name>
          .
          <article-title>Handlers of Algebraic E ects</article-title>
          .
          <source>ESOP 2009. Springer-Verlag Lecture Notes in Computer Science</source>
          <volume>5502</volume>
          , p.
          <fpage>80</fpage>
          -
          <lpage>94</lpage>
          (
          <year>2009</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref19">
        <mixed-citation>
          19. Lutz Schroder,
          <string-name>
            <given-names>Till</given-names>
            <surname>Mossakowski</surname>
          </string-name>
          .
          <article-title>Generic Exception Handling and the Java Monad</article-title>
          .
          <source>AMAST 2004. Springer-Verlag Lecture Notes in Computer Science</source>
          <volume>3116</volume>
          , p.
          <fpage>443</fpage>
          -
          <lpage>459</lpage>
          (
          <year>2004</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref20">
        <mixed-citation>
          20.
          <string-name>
            <given-names>Philip</given-names>
            <surname>Wadler</surname>
          </string-name>
          .
          <article-title>The Marriage of E ects and</article-title>
          <string-name>
            <surname>Monads. ICFP</surname>
          </string-name>
          <year>1998</year>
          . ACM Press, p.
          <fpage>63</fpage>
          -
          <lpage>74</lpage>
          (
          <year>1998</year>
          ).
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>