1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
Patterns
</TITLE>
</HEAD>
<BODY >
<A HREF="manual011.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual013.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual006.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H2>5.6 Patterns</H2>
<A NAME="@manual.kwd5"></A><DIV ALIGN=center>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<TT><I><FONT COLOR=maroon>value-name</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>_</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>constant</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> <TT><FONT COLOR=blue>as</FONT></TT> <TT><I><FONT COLOR=maroon>value-name</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> <TT><FONT COLOR=blue>|</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>ncconstr</FONT></I></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> { <TT><FONT COLOR=blue>,</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> }</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>{</FONT></TT> <TT><I><FONT COLOR=maroon>label</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> { <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>label</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> } <TT><FONT COLOR=blue>}</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>[</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> { <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> } <TT><FONT COLOR=blue>]</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT> <TT><FONT COLOR=blue>::</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT></TD>
</TR></TABLE></DIV><BR>
The table below shows the relative precedences and associativity of
operators and non-closed pattern constructions. The constructions with
higher precedences come first.
<A NAME="@manual.kwd6"></A>
<BR>
<BR>
<DIV ALIGN=center>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%">
<TR><TD ALIGN=center NOWRAP><B>Operator</B></TD>
<TD ALIGN=center NOWRAP><B>Associativity</B></TD>
</TR>
<TR><TD ALIGN=left NOWRAP>
Constructor application</TD>
<TD ALIGN=left NOWRAP>--</TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>::</TT></TD>
<TD ALIGN=left NOWRAP>right</TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>,</TT></TD>
<TD ALIGN=left NOWRAP>--</TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>|</TT></TD>
<TD ALIGN=left NOWRAP>left</TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>as</TT></TD>
<TD ALIGN=left NOWRAP>--</TD>
</TR></TABLE></DIV><BR>
Patterns are templates that allow selecting data structures of a
given shape, and binding identifiers to components of the data
structure. This selection operation is called pattern matching; its
outcome is either ``this value does not match this pattern'', or
``this value matches this pattern, resulting in the following bindings
of names to values''.<BR>
<BR>
<H4>Variable patterns</H4>A pattern that consists in a value name matches any value,
binding the name to the value. The pattern <TT><FONT COLOR=blue>_</FONT></TT> also matches
any value, but does not bind any name.<BR>
<BR>
Patterns are <EM>linear</EM>: a variable cannot appear several times in
a given pattern. In particular, there is no way to test for equality
between two parts of a data structure using only a pattern (but
<TT><FONT COLOR=blue>when</FONT></TT> guards can be used for this purpose).<BR>
<BR>
<H4>Constant patterns</H4>A pattern consisting in a constant matches the values that
are equal to this constant.<BR>
<BR>
<H4>Alias patterns</H4>
<A NAME="@manual.kwd7"></A>The pattern <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>as</FONT></TT> <TT><I><FONT COLOR=maroon>value-name</FONT></I></TT> matches the same values as
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>. If the matching against <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> is successful,
the name <TT><I><FONT COLOR=maroon>name</FONT></I></TT> is bound to the matched value, in addition to the
bindings performed by the matching against <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>.<BR>
<BR>
<H4>Parenthesized patterns</H4>The pattern <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>)</FONT></TT> matches the same values as
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>. A type constraint can appear in a
parenthesized pattern, as in <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT>. This
constraint forces the type of <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> to be compatible with
<TT><I><FONT COLOR=maroon>type</FONT></I></TT>.<BR>
<BR>
<H4>``Or'' patterns</H4>The pattern <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>|</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> represents the logical ``or'' of
the two patterns <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> and <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB>. A value matches
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>|</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> either if it matches <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> or if it
matches <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB>. The two sub-patterns <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> and <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB>
must contain no identifiers. Hence no bindings are returned by
matching against an ``or'' pattern.<BR>
<BR>
<H4>Variant patterns</H4>The pattern <TT><I><FONT COLOR=maroon>ncconstr</FONT></I></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> matches all variants whose
constructor is equal to <TT><I><FONT COLOR=maroon>ncconstr</FONT></I></TT>, and whose argument matches
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>.<BR>
<BR>
The pattern <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>::</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> matches non-empty lists whose
heads match <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>, and whose tails match <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB>. This
pattern behaves like <TT><FONT COLOR=blue>(</FONT></TT> <TT><FONT COLOR=blue>::</FONT></TT> <TT><FONT COLOR=blue>)</FONT></TT> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>,</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> <TT><FONT COLOR=blue>)</FONT></TT>.<BR>
<BR>
The pattern <TT><FONT COLOR=blue>[</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>;</FONT></TT> ... <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>]</FONT></TT> matches lists
of length <I>n</I> whose elements match <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> ...<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>,
respectively. This pattern behaves like
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>::</FONT></TT> ... <TT><FONT COLOR=blue>::</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>::</FONT></TT> <TT><FONT COLOR=blue>[]</FONT></TT>.<BR>
<BR>
<H4>Tuple patterns</H4>The pattern <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>,</FONT></TT> ... <TT><FONT COLOR=blue>,</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> matches <I>n</I>-tuples
whose components match the patterns <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> through <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>. That
is, the pattern matches the tuple values (<TT><I><FONT COLOR=maroon>v</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>, ..., <TT><I><FONT COLOR=maroon>v</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>) such that
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>i</I></FONT></SUB> matches <TT><I><FONT COLOR=maroon>v</FONT></I></TT><SUB><FONT SIZE=2><I>i</I></FONT></SUB> for <I>i</I> = 1,... , <I>n</I>.<BR>
<BR>
<H4>Record patterns</H4>The pattern <TT><FONT COLOR=blue>{</FONT></TT> <TT><I><FONT COLOR=maroon>label</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>;</FONT></TT> ... <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>label</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>=</FONT></TT>
<TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>}</FONT></TT> matches records that define at least the labels
<TT><I><FONT COLOR=maroon>label</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> through <TT><I><FONT COLOR=maroon>label</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>, and such that the value associated to
<TT><I><FONT COLOR=maroon>label</FONT></I></TT><SUB><FONT SIZE=2><I>i</I></FONT></SUB> match the pattern <TT><I><FONT COLOR=maroon>pattern</FONT></I></TT><SUB><FONT SIZE=2><I>i</I></FONT></SUB>, for <I>i</I> = 1,... , <I>n</I>.
The record value can define more labels than <TT><I><FONT COLOR=maroon>label</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> ...<TT><I><FONT COLOR=maroon>label</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>; the values associated to these extra labels are not taken
into account for matching.<BR>
<BR>
<BR>
<BR>
<HR>
<A HREF="manual011.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual013.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual006.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|