Commit 0f11a5a0 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 a42c0679
......@@ -772,7 +772,7 @@ let rec expr glb loc = function
let x = U.to_string (Id.value x) in
warning br_loc
("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
fv := Fv.cup !fv fv2;
accept := Types.cup !accept (Types.descr (Patterns.accept p));
......@@ -13,6 +13,70 @@
<b style="color:#FF0080">TO BE DONE</b>
<box title="Handling optional attributes" link="pr">
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.
Consider the following fragment of code from from the CDuce
distribution that we have changed a bit so that it stands alone:
type Sample = <sample highlight=?"true"|"false">String
let content (Sample -> String)
| <sample highlight="false">_ -> "non-higlighted code"
| <sample>_ -> "highlighted code"
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.
But what if we have two optional attributes? The naive solution would be
to write
<i>three</i> cases in the above function:
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"
The intended use for the <code>lineno</code> attribute is to tell us whether line
numbers should be displayed alongside the sample code.)
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:
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")
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).
<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