Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cduce
cduce
Commits
0f11a5a0
Commit
0f11a5a0
authored
Jul 10, 2007
by
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
Changes
2
Hide whitespace changes
Inline
Side-by-side
typing/typer.ml
View file @
0f11a5a0
...
...
@@ -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
));
...
...
web/tutorial/patterns.xml
View file @
0f11a5a0
...
...
@@ -13,6 +13,70 @@
<b
style=
"color:#FF0080"
>
TO BE DONE
</b>
</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>
<
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>
&
</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"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment