File: patterns.html

package info (click to toggle)
ocaml-doc 3.10-1
  • links: PTS, VCS
  • area: non-free
  • in suites: lenny
  • size: 7,476 kB
  • ctags: 2,644
  • sloc: ml: 325; sh: 64; makefile: 34; ansic: 15
file content (117 lines) | stat: -rw-r--r-- 16,844 bytes parent folder | download
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
<!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">
<META name="GENERATOR" content="hevea 1.09">
<LINK rel="stylesheet" type="text/css" href="manual.css">
<TITLE>Patterns</TITLE>
</HEAD>
<BODY >
<A HREF="manual013.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual008.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
<A HREF="expr.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
<HR>
<H2 CLASS="section"><A NAME="htoc65">6.6</A>Patterns</H2><P>

<A NAME="@manual.kwd5"></A>
</P><TABLE CLASS="display dcenter"><TR VALIGN="middle"><TD CLASS="dcell"><TABLE CELLSPACING=6 CELLPADDING=0><TR><TD ALIGN=right NOWRAP>
<I><A NAME="pattern"><FONT COLOR=maroon>pattern</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
<I><A HREF="manual011.html#value-name"><FONT COLOR=maroon>value-name</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>_</TT></FONT>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><I><A HREF="manual013.html#constant"><FONT COLOR=maroon>constant</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><FONT COLOR=blue><TT>as</TT></FONT><I><A HREF="manual011.html#value-name"><FONT COLOR=maroon>value-name</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>(</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><FONT COLOR=blue><TT>)</TT></FONT>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>(</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><FONT COLOR=blue><TT>:</TT></FONT><I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I><FONT COLOR=blue><TT>)</TT></FONT>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><FONT COLOR=blue><TT>|</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><I><A HREF="manual011.html#constr"><FONT COLOR=maroon>constr</FONT></A></I><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>`</TT></FONT><I><A HREF="manual011.html#tag-name"><FONT COLOR=maroon>tag-name</FONT></A></I><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>#</TT></FONT><I><A HREF="manual011.html#typeconstr-name"><FONT COLOR=maroon>typeconstr-name</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>{<FONT COLOR=blue><TT>,</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>}
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>{</TT></FONT><I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I><FONT COLOR=blue><TT>=</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>{<FONT COLOR=blue><TT>;</TT></FONT><I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I><FONT COLOR=blue><TT>=</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>}<FONT COLOR=blue><TT>}</TT></FONT>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>[</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>{<FONT COLOR=blue><TT>;</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>}<FONT COLOR=blue><TT>]</TT></FONT>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><FONT COLOR=blue><TT>::</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>
</TD></TR>
<TR><TD ALIGN=right NOWRAP>&nbsp;</TD><TD ALIGN=center NOWRAP>&#X2223;</TD><TD ALIGN=left NOWRAP><FONT COLOR=blue><TT>[|</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>{<FONT COLOR=blue><TT>;</TT></FONT><I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I>}<FONT COLOR=blue><TT>|]</TT></FONT></TD></TR>
</TABLE></TD></TR>
</TABLE><P>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>
</P><DIV CLASS="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>&#X2013;</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>&#X2013;</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>&#X2013;</TD></TR>
</TABLE></DIV><P>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 &#X201C;this value does not match this pattern&#X201D;, or
&#X201C;this value matches this pattern, resulting in the following bindings
of names to values&#X201D;.</P><H4 CLASS="subsubsection">Variable patterns</H4><P>A pattern that consists in a value name matches any value,
binding the name to the value. The pattern <FONT COLOR=blue><TT>_</TT></FONT> also matches
any value, but does not bind any name.</P><P>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
<FONT COLOR=blue><TT>when</TT></FONT> guards can be used for this purpose).</P><H4 CLASS="subsubsection">Constant patterns</H4><P>A pattern consisting in a constant matches the values that
are equal to this constant.</P><H4 CLASS="subsubsection">Alias patterns</H4><P>
<A NAME="@manual.kwd7"></A></P><P>The pattern <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>as</TT></FONT> <I><A HREF="manual011.html#value-name"><FONT COLOR=maroon>value-name</FONT></A></I> matches the same values as
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB>. If the matching against <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> is successful,
the name <FONT COLOR=maroon><I>name</I></FONT> is bound to the matched value, in addition to the
bindings performed by the matching against <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB>.</P><H4 CLASS="subsubsection">Parenthesized patterns</H4><P>The pattern <FONT COLOR=blue><TT>(</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>)</TT></FONT> matches the same values as
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB>. A type constraint can appear in a
parenthesized pattern, as in <FONT COLOR=blue><TT>(</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>:</TT></FONT> <I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I> <FONT COLOR=blue><TT>)</TT></FONT>. This
constraint forces the type of <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> to be compatible with
<I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I>.</P><H4 CLASS="subsubsection">&#X201C;Or&#X201D; patterns</H4><P>The pattern <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>|</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB> represents the logical &#X201C;or&#X201D; of
the two patterns <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> and <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB>. A value matches
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>|</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB> either if it matches <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> or if it
matches <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB>. The two sub-patterns <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> and <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB>
must bind exactly the same identifiers to values having the same types.
Matching is performed from left to right.
More precisely,
in case some value<I>v</I> matches <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>|</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB>, the bindings 
performed are those of <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> when <I>v</I> matches <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB>.
Otherwise, value<I>v</I> matches <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB> whose bindings are performed.</P><H4 CLASS="subsubsection">Variant patterns</H4><P>The pattern <I><A HREF="manual011.html#constr"><FONT COLOR=maroon>constr</FONT></A></I> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> matches all variants whose
constructor is equal to <I><A HREF="manual011.html#constr"><FONT COLOR=maroon>constr</FONT></A></I>, and whose argument matches
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB>.</P><P>The pattern <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>::</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB> matches non-empty lists whose
heads match <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB>, and whose tails match <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>2</SUB>.</P><P>The pattern <FONT COLOR=blue><TT>[</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>;</TT></FONT> &#X2026; <FONT COLOR=blue><TT>;</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>n</SUB></I> <FONT COLOR=blue><TT>]</TT></FONT> matches lists
of length <I>n</I> whose elements match <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> &#X2026;<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>n</SUB></I>,
respectively. This pattern behaves like
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>::</TT></FONT> &#X2026; <FONT COLOR=blue><TT>::</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>n</SUB></I> <FONT COLOR=blue><TT>::</TT> <TT>[]</TT></FONT>.</P><H4 CLASS="subsubsection">Polymorphic variant patterns</H4><P>The pattern <FONT COLOR=blue><TT>`tag-name</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> matches all polymorphic variants
whose tag is equal to <I><A HREF="manual011.html#tag-name"><FONT COLOR=maroon>tag-name</FONT></A></I>, and whose argument matches
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB>.</P><H4 CLASS="subsubsection">Variant abbreviation patterns</H4><P>If the type [<FONT COLOR=blue><TT>('a,'b,...)</TT></FONT>] <I><A HREF="manual011.html#typeconstr"><FONT COLOR=maroon>typeconstr</FONT></A></I> = <FONT COLOR=blue><TT>[`tag</TT></FONT><SUB>1</SUB> <I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>|</TT></FONT>
&#X2026; <FONT COLOR=blue><TT>|</TT> <TT>`tag</TT></FONT><I><SUB>n</SUB></I> <I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A><SUB>n</SUB></I><FONT COLOR=blue><TT>]</TT></FONT> is defined, then the pattern #<I><A HREF="manual011.html#typeconstr"><FONT COLOR=maroon>typeconstr</FONT></A></I>
is a shorthand for the or-pattern <FONT COLOR=blue><TT>(`tag</TT></FONT><SUB>1</SUB><FONT COLOR=blue><TT>(_</TT> <TT>:</TT></FONT> <I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>)</TT> <TT>|</TT></FONT>
&#X2026; <FONT COLOR=blue><TT>|</TT> <TT>`tag</TT></FONT><I><SUB>n</SUB></I><FONT COLOR=blue><TT>(_</TT> <TT>:</TT></FONT> <I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A><SUB>n</SUB></I><FONT COLOR=blue><TT>))</TT></FONT>. It matches all values of type
#<I><A HREF="manual011.html#typeconstr"><FONT COLOR=maroon>typeconstr</FONT></A></I>.</P><H4 CLASS="subsubsection">Tuple patterns</H4><P>The pattern <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>,</TT></FONT> &#X2026; <FONT COLOR=blue><TT>,</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>n</SUB></I> matches <I>n</I>-tuples
whose components match the patterns <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> through <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>n</SUB></I>. That
is, the pattern matches the tuple values (<I>v</I><SUB>1</SUB>, &#X2026;, <I>v<SUB>n</SUB></I>) such that
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>i</SUB></I> matches <I>v<SUB>i</SUB></I> for <I>i</I> = 1,&#X2026; , <I>n</I>.</P><H4 CLASS="subsubsection">Record patterns</H4><P>The pattern <FONT COLOR=blue><TT>{</TT></FONT> <I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>;</TT></FONT> &#X2026; <FONT COLOR=blue><TT>;</TT></FONT> <I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A><SUB>n</SUB></I> <FONT COLOR=blue><TT>=</TT></FONT>
<I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>n</SUB></I> <FONT COLOR=blue><TT>}</TT></FONT> matches records that define at least the fields
<I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I><SUB>1</SUB> through <I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A><SUB>n</SUB></I>, and such that the value associated to
<I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A><SUB>i</SUB></I> matches the pattern <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>i</SUB></I>, for <I>i</I> = 1,&#X2026; , <I>n</I>.
The record value can define more fields than <I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I><SUB>1</SUB> &#X2026;<I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A><SUB>n</SUB></I>; the values associated to these extra fields are not taken
into account for matching.</P><H4 CLASS="subsubsection">Array patterns</H4><P>The pattern <FONT COLOR=blue><TT>[|</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>;</TT></FONT> &#X2026; <FONT COLOR=blue><TT>;</TT></FONT> <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>n</SUB></I> <FONT COLOR=blue><TT>|]</TT></FONT>
matches arrays of length <I>n</I> such that the <I>i</I>-th array element
matches the pattern <I><A HREF="#pattern"><FONT COLOR=maroon>pattern</FONT></A><SUB>i</SUB></I>, for <I>i</I> = 1,&#X2026; , <I>n</I>.</P><HR>
<A HREF="manual013.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual008.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
<A HREF="expr.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>