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 "sample") of specific values that would make
the computation fail.
CDuce signals the classic syntax errors as well as those for instance of unbound variables. It also checks that pattern matching is exhaustive
[1].
For instance if we declare the type Person defined in Section "Overloading" and try the following definition:
fun name (Person -> String)
| <person gender = "F">[ n ;_] -> n
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):
Error at chars 228-298:
fun name (Person -> String)
| <person gender = "F">[ n ;_] -> n
This pattern matching is not exhaustive
Residual type:
<person gender = [ 'M' ]>[ Name Children ]
Sample:
<person {| gender = [ 'M' ] |}>[ <name {| |}>[ ] <children {| |}>[ ] ]
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 "M"); (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
MPerson); (3) it shows us a "sample" 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 <person gender="M">[ <name>[ ]
<children>[ ] ]).
Note: 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.
Warnings
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:
transform [ 1 "c" 4 "duce" 2 6 ] with
x & Sting -> [ x ]
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
St(r)ing the transformation is instead the identity function:
Sting is considered as a fresh capture variable. CDuce however
detects that Sting is never used in the subsequent expression
and it pinpoints the possible presence of an error by issuing the
following warning:
Warning at chars 42-60:
x & Sting -> [ x ]
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).
transform [ 1 "c" 4 "duce" 2 6 ] with
x & Sting -> [ x ]
- : [ 1 [ 'c' ] 4 [ 'duce' ] 2 6 ] =
[ 1 "c" 4 "duce" 2 6 ]
Ok.
|