Commit 37bc0a89 authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2003-09-08 11:15:46 by cvscast] Beppe: added tutorial page on error messages and warnings

Original author: cvscast
Date: 2003-09-08 11:15:47+00:00
parent 880d2b00
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="hacking">
<page name="hacking" new="">
<title>Hacking CDuce</title>
......
......@@ -18,11 +18,11 @@
<include file="download.xml"/>
<include file="bench.xml"/>
<include file="papers.xml"/>
<include file="examples.xml"/>
<include file="team.xml"/>
<include file="manual.xml"/>
<include file="examples.xml"/>
<include file="hacking.xml"/>
<include file="mailing.xml"/>
<include file="team.xml"/>
<include file="sitemap.xml"/>
......
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="mailing" new="">
<page name="mailing">
<title>Mailing Lists</title>
......
......@@ -8,7 +8,7 @@
<include file="tutorial/first_functions.xml"/>
<include file="tutorial/overloading.xml"/>
<include file="tutorial/patterns.xml"/>
<include file="tutorial/errors.xml"/>
<left>
<p>
This page is a step-by-step introduction to CDuce.
......
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="tutorial_errors">
<title>Error messages</title>
<banner>Error messages</banner>
<title>Error messages and Warnings</title>
<banner>Error messages and Warnings</banner>
<left>
<boxes-toc/>
......@@ -17,7 +17,7 @@
<box title="Empty types" link="pr">
<p>
type system can find very nasty errors. For instance look at this declaration
CDuce's type system can find very nasty errors. For instance look at this DTD declaration
</p>
<p>
<tt>
......@@ -27,35 +27,90 @@ type system can find very nasty errors. For instance look at this declaration
</tt>
</p>
<p>
You see the problem? There is no document (without refs) that can be valid,
as a person contains a sequence of childrens that contain a non empty
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 (taken verbatim
from the on line demo, no color or formatting added)
Let us write the same type in CDuce and look at the result returned by the type-checker (taken verbatim from the on line demo, no color or formatting added)
</p>
<p>
<tt>
type Person = &lt;person>[ Name Children ]
type Children = &lt;children>[Person+]
type Person = &lt;person>[ Name Children ]<br/>
type Children = &lt;children>[Person+]<br/>
type Name = &lt;name>[PCDATA]
Warning at chars 57-76:
<i>type Children = <font color="red"><b>&lt;children&gt;[Person+]</b></font></i>
This definition yields an empty type for Children
Warning at chars 14-39:
<i>type Person = <font color="red"><b>&lt;person&gt;[ Name Children ]</b></font></i>
This definition yields an empty type for Person
</code>
<br/>
<br/>
Warning at chars 57-76:<br/>
<i>type Children = <b style="color:red">&lt;children&gt;[Person+]</b></i><br/>
This definition yields an empty type for Children <br/>
Warning at chars 14-39:<br/>
<i>type Person = <b style="color:red">&lt;person&gt;[ Name Children ]</b></i><br/>
This definition yields an empty type for Person<br/>
</tt>
</p>
<p>
The type checker correctly issues a "Warning" to signal that the first
two types are empty. Note that instead the declarations</p>
<p>
We paid special care in localizing errors and suggesting solutions. I can
give you more significative examples if you want, but you can ust try it by
<tt>
type Person = &lt;person>[ Name Children ]<br/>
type Children = &lt;children>[(<i style="color:red">ref Person</i>)+]<br/>
type Name = &lt;name>[PCDATA]<br/>
</tt></p>
<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://www.cduce.org/cgi-bin/cduce">on line demo</a> and putting in
href="../cgi-bin/cduce">on line demo</a> and putting in
them random errors.
</p>
</box>
<box title="Unused branches" link="pr">
<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>
<sample><![CDATA[
type Person = <person>[<name>String <tel>String (<email>String)?]
fun main_contacts(x : [Person*]):[String*] =
transform x with
| <_>[_ _ <{{emal}}>s] -> [s]
| <_>[_ <tel>s ] -> [s]
]]></sample>
<p>
This function was supposed 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
will never be selected as <code> Person &amp; &lt;_>[_ _
&lt;emal>s]</code>=<code>Empty</code> and it prevents the programmer by issuing the following warning message:
</p>
<p>
<tt>
Warning at chars 144-167:<br/>
<i> | <b style="color:red">&lt;_&gt;[_ _ &lt;emal&gt;s] -&gt; [s]</b></i><br/>
This branch is not used<br/>
<i>fun main_contacts(x : [Person*]):[String*] =<br/>
transform x with <br/>
| &lt;_&gt;[_ _ &lt;emal&gt;s] -&gt; [s]<br/>
| &lt;_&gt;[_ &lt;tel&gt;s ] -&gt; [s]</i>
<br/>
- : [ Person* ] -&gt; [ String* ] = &lt;fun&gt;<br/>
<br/>
Ok.
</tt>
</p>
</box>
</page>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment