Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > 50e6cd590109ca4ca0931fda4b384942 > files > 30

cduce-0.5.3-2mdv2010.0.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>CDuce: Error messages and Warnings</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/><link href="cduce.css" type="text/css" rel="stylesheet"/></head><body style="margin: 0; padding : 0;"><table width="100&#37;" border="0" cellspacing="10" cellpadding="0"><tr><td valign="top" style="width:20&#37;;" align="left"><div class="leftbar" id="leftbar"><div class="smallbox"><ul><li><a href="#p1">Key concepts</a></li><li><a href="#emptyty">Empty types</a></li><li><a href="#pr">Unused branches</a></li></ul><p>
You can cut and paste the code on this page and 
test it on the <a href="http://reglisse.ens.fr/cgi-bin/cduce">online interpreter</a>.
</p></div></div></td><td><h1>Error messages and Warnings</h1><div class="mainpanel"><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="tutorial.html">Tutorial</a>: Error messages and Warnings</p><p><a href="tutorial_patterns.html"><img class="icon" width="16" alt="Previous page:" src="img/left.gif" height="16"/> Patterns</a> <a href="tutorial_references.html"><img class="icon" width="16" alt="Next page:" src="img/right.gif" height="16"/> References</a></p></div><div><h2><a name="p1">Key concepts</a></h2><p>
CDuce, statically detects a large class of error and tries to help their
debugging by providing precise error messages and, in case of type errors, by
showing a description (we call it a &quot;sample&quot;) of specific values that would make
the computation fail.
</p><p>
CDuce signals the classic syntax errors as well as those for instance of unbound variables. It also checks that pattern matching is exhaustive
  <a name="bnote1"/><a href="#note1">[1]</a>.
For instance if we declare the type <b><tt>Person</tt></b> defined in Section &quot;<a href="tutorial_overloading.html">Overloading</a>&quot; and  try the following definition:
</p><div class="code"><pre>
fun name (Person -&gt; String) 
     | &lt;person gender = <strong class="highlight">&quot;F&quot;</strong>&gt;[ n ;_] -&gt; n 
</pre></div><p> then we obtain the following message error (frames of the same form as the following denote text taken verbatim from the on line demo, no color or formatting added):</p><div class="session"><pre>
Error at chars 228-298:
<strong class="highlight"><i>fun name (Person -&gt; String) 
     | &lt;person gender = &quot;F&quot;&gt;[ n ;_] -&gt; n</i></strong> 
This pattern matching is not exhaustive
Residual type:
&lt;person gender = [ 'M' ]&gt;[ Name Children ]
Sample:
&lt;person {| gender = [ 'M' ] |}&gt;[ &lt;name {| |}&gt;[  ] &lt;children {| |}&gt;[  ] ]
</pre></div><p>
This error message tells us three things: (1) that pattern matching is not
defined for all the possible input types (as we forgot the case when the
attribute is <b><tt>&quot;M&quot;</tt></b>); (2) it gives us the exact type of the values of
the type we have forgotten in our matching (in this case this is exactly
<b><tt>MPerson</tt></b>); (3) it shows us a &quot;sample&quot; of the residual type, that is
a simplified representation of a value that would make the expression fail (in
this case it shows us the value <b><tt>&lt;person gender=&quot;M&quot;&gt;[ &lt;name&gt;[ ]
&lt;children&gt;[ ] ]</tt></b>).
</p><div class="note"><b>Note:  </b>Samples are  simplified representations of values in the sense that they show 
only that part of the value that is relevant for the error and may omit other parts
that are needed to obtain an effective value.
</div><h3>Warnings</h3><p>
CDuce use warnings to signal possible subtler errors. So for instance it issues a warning whenever a capture variable of a pattern is not used in the subsequent expression. This is very useful for instance to detect misprinted types in patterns such as in:
</p><div class="code"><pre>
transform [ 1 &quot;c&quot; 4 &quot;duce&quot; 2 6 ] with
   x &amp; <strong class="highlight">Sting</strong> -&gt; [ x ]
</pre></div><p> The intended semantics of this expression was to extract the sequence of all
the strings occuring in the matched sequence. But because of the typo in
<b><tt>St(r)ing</tt></b> the transformation is instead the identity function:
<b><tt>Sting</tt></b> is considered as a fresh capture variable. CDuce however
detects that <b><tt>Sting</tt></b> is never used in the subsequent expression
and it pinpoints the possible presence of an error by issuing the
following warning:
</p><div class="session"><pre>
Warning at chars 42-60:
<strong class="highlight"><i>   x &amp; Sting -&gt; [ x ]</i></strong>
The capture variable Sting is declared in the pattern but not used in 
the body of this branch. It might be a misspelled or undeclared type 
or name (if it isn't, use _ instead).
<i>transform [ 1 &quot;c&quot; 4 &quot;duce&quot; 2 6 ] with
   x &amp; Sting -&gt; [ x ]</i>
- : [ 1 [ 'c' ] 4 [ 'duce' ] 2 6 ] =
    [ 1 &quot;c&quot; 4 &quot;duce&quot; 2 6 ]

Ok.
</pre></div></div><div><h2><a name="emptyty">Empty types</a></h2><p>
CDuce's type system can find very nasty errors. For instance look at this DTD declaration
</p><div class="xmlcode"><pre>
&lt;!ELEMENT person (name,children)&gt;
&lt;!ELEMENT children (person+)&gt;
&lt;!ELEMENT name (#PCDATA)&gt;
</pre></div><p>
Apparently this declaration does not pose any problem. But if you consider it more carefully you will see that there exists no document that can be valid for such a DTD,
as a person contains a sequence of children that contain a non empty
sequence of persons, etc generating an infinite tree.
</p><p>
Let us write the same type in CDuce and look at the result returned by the type-checker
</p><div class="session"><pre>
type Person = &lt;person&gt;[ Name Children ]
type Children = &lt;children&gt;[Person+]
type Name = &lt;name&gt;[PCDATA]

Warning at chars 57-76:
<i>type Children = </i><strong class="highlight"><i>&lt;children&gt;[Person+]</i></strong>
This definition yields an empty type for Children
Warning at chars 14-39:
<i>type Person = </i><strong class="highlight"><i>&lt;person&gt;[ Name Children ]</i></strong>
This definition yields an empty type for Person
</pre></div><p>
The type checker correctly issues a &quot;Warning&quot; to signal that the first 
two types  are empty. Note that instead the declarations</p><div class="code"><pre>
type Person = &lt;person&gt;[ Name Children ]
type Children = &lt;children&gt;[(<strong class="highlight">ref Person</strong>)+]
type Name = &lt;name&gt;[PCDATA]
</pre></div><p>
correctly do not yield any warning: in this case it is possible to build a value of type person (and thus of type children), for instance by using a recursive definition where a person is a child of itself.
</p><p>
We paid special care in localizing errors and suggesting solutions. 
You can  try it by
yourself by picking the examples available on the <a href="http://reglisse.ens.fr/cgi-bin/cduce">on line interpreter</a> and putting in
them random errors.
</p></div><div><h2><a name="pr">Unused branches</a></h2><p>
The emptiness test is used also to check for possible errors in the definition
of patterns. If the type checker statically determines that a pattern in a match
operation can never be matched then it is very likely that even if the match
expression is well-typed, the programmer had made an error.  This is determined by checking whether the intersection of set of all values that can be fed to the branch and the set of all values that  Consider for example
the following code:
</p><div class="code"><pre>
type Person = &lt;person&gt;[&lt;name&gt;String &lt;tel&gt;String  (&lt;email&gt;String)?]

fun main_contacts(x : [Person*]):[String*] =
  transform x with 
     | &lt;_&gt;[_ _ &lt;<strong class="highlight">emal</strong>&gt;s] -&gt; [s]
     | &lt;_&gt;[_  &lt;tel&gt;s ] -&gt; [s]
</pre></div><p>
This function was supposed to extract the list of contacts from a list of persons
elements giving priority to email addresses over telephone numbers.  Even if
there is a typo in the pattern of the first branch, the function is well
typed. However because of the typo the first branch will never be selected and
emails never printed. The CDuce type-checker however recognizes that this branch
has no chance to be selected since <b><tt> Person &amp; &lt;_&gt;[_ _
&lt;emal&gt;s]</tt></b>=<b><tt>Empty</tt></b> and it warns the programmer by issuing the following warning message:
</p><div class="session"><pre>
Warning at chars 144-167:
<i>       | </i><strong class="highlight"><i>&lt;_&gt;[_ _ &lt;emal&gt;;s] -&gt; [s]</i></strong>
This branch is not used
<i>fun main_contacts(x : [Person*]):[String*] =
    transform x with
       | &lt;_&gt;[_ _ &lt;emal&gt;s] -&gt; [s]
       | &lt;_&gt;[_  &lt;tel&gt;s ] -&gt; [s]</i>
- : [ Person* ] -&gt; [ String* ] = &lt;fun&gt;

Ok.
</pre></div></div><div class="meta"><p><a href="sitemap.html">Site map</a></p></div><div class="meta"><p><a name="note1"/><a href="#bnote1">[1]</a> 
    It checks it in functions, <b><tt>match</tt></b>, and <b><tt>map</tt></b> expressions, but
    not for <b><tt>transform</tt></b> and <b><tt>xtransform</tt></b> for which a default branch
    returning the empty sequence is always defined
  </p></div><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="tutorial.html">Tutorial</a>: Error messages and Warnings</p><p><a href="tutorial_patterns.html"><img class="icon" width="16" alt="Previous page:" src="img/left.gif" height="16"/> Patterns</a> <a href="tutorial_references.html"><img class="icon" width="16" alt="Next page:" src="img/right.gif" height="16"/> References</a></p></div></div></td></tr></table></body></html>