-
Notifications
You must be signed in to change notification settings - Fork 15
/
match.smli
195 lines (171 loc) · 4.56 KB
/
match.smli
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
(*
* Licensed to Julian Hyde under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Julian Hyde licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*
* Pattern matching
*)
(*) Warning: match nonexhaustive
fun f 1 = 0;
> stdIn:23.5-23.12 Warning: match nonexhaustive
> raised at: stdIn:23.5-23.12
> val f = fn : int -> int
f 1;
> val it = 0 : int
f 2;
> uncaught exception Bind
> raised at: stdIn:23.5-23.12
(*) Warning: match nonexhaustive, twice
fun f x =
let
fun g 1 = 1
and h 2 = 2
in
(g x) + (h 2)
end;
> stdIn:5.9-5.16 Warning: match nonexhaustive
> raised at: stdIn:5.9-5.16
> stdIn:6.9-6.16 Warning: match nonexhaustive
> raised at: stdIn:6.9-6.16
> val f = fn : int -> int
f 1;
> val it = 3 : int
f 2;
> uncaught exception Bind
> raised at: stdIn:5.9-5.16
(*) Error: match redundant and nonexhaustive
fun f 1 = 0
| f 1 = 0;
> stdIn:4.5-4.12 Error: match redundant and nonexhaustive
> raised at: stdIn:4.5-4.12
(*) OK
fun f 1 = 0
| f _ = 1;
> val f = fn : int -> int
f 1;
> val it = 0 : int
f 2;
> val it = 1 : int
(*) Error: match redundant
fun f (1, _) = 1
| f (_, 2) = 2
| f (1, 2) = 3
| f (_, _) = 4;
> stdIn:5.5-5.17 Error: match redundant
> raised at: stdIn:5.5-5.17
(*) The Ackermann-Péter function
(*) See "Recursion Equations as a Programming Language", D A Turner 1982
fun ack 0 n = n + 1
| ack m 0 = ack (m - 1) 1
| ack m n = ack (m - 1) (ack m (n - 1));
> val ack = fn : int -> int -> int
ack 0 0;
> val it = 1 : int
ack 0 1;
> val it = 2 : int
ack 1 0;
> val it = 2 : int
ack 1 2;
> val it = 4 : int
ack 2 3;
> val it = 9 : int
ack 3 3;
> val it = 61 : int
(* Various types, exhaustive & non-exhaustive ------------------ *)
(*) From https://smlhelp.github.io/book/docs/start/syntax/
(*) unit, exhaustive
(fn () => ());
> val it = fn : unit -> unit
(*) bool, non-exhaustive
(fn true => 1);
> stdIn:3.2-3.14 Warning: match nonexhaustive
> raised at: stdIn:3.2-3.14
> val it = fn : bool -> int
(*) bool, exhaustive
(fn true => 1
| false => 0);
> val it = fn : bool -> int
(*) order, non-exhaustive
(fn LESS => ~1);
> stdIn:3.2-3.15 Warning: match nonexhaustive
> raised at: stdIn:3.2-3.15
> val it = fn : order -> int
(*) order, non-exhaustive
(fn LESS => ~1
| EQUAL => 0);
> stdIn:3.2-4.15 Warning: match nonexhaustive
> raised at: stdIn:3.2-4.15
> val it = fn : order -> int
(*) order, exhaustive
(fn LESS => ~1
| EQUAL => 0
| GREATER => 1);
> val it = fn : order -> int
(*) int, non-exhaustive
(fn 0 => true);
> stdIn:3.2-3.14 Warning: match nonexhaustive
> raised at: stdIn:3.2-3.14
> val it = fn : int -> bool
(*) order, exhaustive
(fn 0 => true
| _ => false);
> val it = fn : int -> bool
(*) int list, non-exhaustive
(fn x::_ => x + 1);
> stdIn:3.2-3.18 Warning: match nonexhaustive
> raised at: stdIn:3.2-3.18
> val it = fn : int list -> int
(*) int list, exhaustive
(fn [] => 0
| x::_ => x + 1);
> val it = fn : int list -> int
(*) int * bool, non-exhaustive
(fn (0,b) => true andalso b);
> stdIn:3.2-3.28 Warning: match nonexhaustive
> raised at: stdIn:3.2-3.28
> val it = fn : int * bool -> bool
(*) int * bool, exhaustive
(fn (0,b) => true andalso b
| (n,_) => false);
> val it = fn : int * bool -> bool
(* Exhaustive function of option list ------------------------- *)
(*) Non-exhaustive function
fun f [(SOME i)] = i
| f [] = 0;
> 0.0-0.0 Warning: match nonexhaustive
> raised at: 0.0-0.0
> val f = fn : int option list -> int
(*) Add NONE, still non-exhaustive
fun f [(SOME i)] = i
| f [NONE] = ~1
| f [] = 0;
> 0.0-0.0 Warning: match nonexhaustive
> raised at: 0.0-0.0
> val f = fn : int option list -> int
(*) Allow list longer than 1, still non-exhaustive
fun f [(SOME i)] = i
| f [] = 0
| f (one :: two :: rest) = ~2;
> 0.0-0.0 Warning: match nonexhaustive
> raised at: 0.0-0.0
> val f = fn : int option list -> int
(*) Exhaustive
fun f [(SOME i)] = i
| f [NONE] = ~1
| f [] = 0
| f (one :: two :: rest) = ~2;
> val f = fn : int option list -> int
(*) End match.smli