Commit a29e1e2c authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2003-09-22 16:18:19 by cvscast] Empty log message

Original author: cvscast
Date: 2003-09-22 16:21:12+00:00
parent 8abfda62
...@@ -772,7 +772,7 @@ let rec expr glb loc = function ...@@ -772,7 +772,7 @@ let rec expr glb loc = function
let x = U.to_string (Id.value x) in let x = U.to_string (Id.value x) in
warning br_loc warning br_loc
("The capture variable " ^ x ^ ("The capture variable " ^ x ^
" is declared in the pattern but not used in the body of this branch. It might be a misspelled type or name.")); " is declared in the pattern but not used in the body of this branch. It might be a misspelled type or name (if not use _ instead)."));
let fv2 = Fv.diff fv2 (Patterns.fv p) in let fv2 = Fv.diff fv2 (Patterns.fv p) in
fv := Fv.cup !fv fv2; fv := Fv.cup !fv fv2;
accept := Types.cup !accept (Types.descr (Patterns.accept p)); accept := Types.cup !accept (Types.descr (Patterns.accept p));
......
...@@ -13,6 +13,70 @@ ...@@ -13,6 +13,70 @@
<b style="color:#FF0080">TO BE DONE</b> <b style="color:#FF0080">TO BE DONE</b>
</box> </box>
<box title="Handling optional attributes" link="pr">
<p>
The blend of type type constructors and boolean combinator can be used to
reduce verbosity in writing pattern matching. As an exmple we show how to handle
tags with several optional attibutes.
</p>
<p>
Consider the following fragment of code from site.cd from the CDuce
distribution that we have changed a bit so that it stands alone:
</p>
<sample><![CDATA[
type Sample = <sample highlight=?"true"|"false">String
let content (Sample -> String)
| <sample highlight="false">_ -> "non-higlighted code"
| <sample>_ -> "highlighted code"
]]></sample>
<p>
The idea here is to use the highlight attribute to specify that
certain pieces of <code>&lt;sample></code> should be emphasized. When the higlight
attribute is missing, the default value of "true" is presumed.
</p>
<p>
But what if we have two optional attributes? The naive solution would be
to write
<i>three</i> cases in the above function:
</p>
<sample><![CDATA[
type Sample = <sample lineno=?"true"|"false" highlight=?"true"|"false">String
let content (Sample -> String)
| <sample highlight="false" lineno="false">_ -> "no lineno, no highlight"
| <sample lineno="false">_ -> "no lineno, highlight"
| <sample highlight="false">_ -> "lineno, no highlight"
| <sample>_ -> "lineno, highlight"
]]></sample>
<p>
The intended use for the <code>lineno</code> attribute is to tell us whether line
numbers should be displayed alongside the sample code.)
<br/>
While this situation is still bearable it soon become unfeasible with more
than two optional attributes. A much better way of handling this situation
is to resort to intersection and default patterns as follows:
</p>
<sample><![CDATA[
let content (Sample -> String)
| <sample ( ({ highlight = h } | (h := "true"))
&({ lineno = l } | (l := "true")) )>_
-> (match l with "true" -> "lineno, " | _ -> " no lineno, ")@
(match h with "true" -> "highlight" | _ -> "no highlight")
]]></sample>
<p>
The intersection pattern <code>&amp;</code> makes both patterns to be matched
against the record of attributes: each pattern check the presence of an specific
attributes, if it present it captures the attribute value in a given variables
while if the attribute is absent the default subpattern to assign to the
variable a default value (disclaimer, for the moment defaut patterns accepts
only constants; this limitation will soon desappear).
</p>
</box>
<box title="Recursive patterns" link="pr"> <box title="Recursive patterns" link="pr">
......
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