-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pattern_matching.fold
285 lines (213 loc) · 5.33 KB
/
Pattern_matching.fold
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- Pattern-matching
fun hello name =
if name == "world" then
print "Hello WORLD"
else
print "Hello, $name"
fun hello "wolrd" =
print "Hello WORLD"
| hello name =
print "Hello, $name"
fun hello name =
case name do
| "world" ->
print "Hello, WOLRLD"
| other ->
print "Hello, $other"
end
-- * --
type Option a =
| None
| Some a
function hello
| "wolrd" -> print "Hello WORLD"
| name -> print "Hello, $name"
def max [x] = Some x
| max [x & xs] =
let m = max xs in
Some (if (x > m) then x else m)
| max [] = None
def length list =
case list
[] -> 0
[x & xs] -> 1 + length xs
end
max [x] =
Some x
max [x & xs] =
let m = max xs in
Some (if (x > m) then x else m)
max [] = None
def max [x] = Some x
| max [x & xs] =
let m = max xs in
Some (if (x > m) then x else m)
| max [] = None
def max =
| [x] -> Some x
| max [x & xs] ->
let m = max xs in
Some (if (x > m) then x else m)
| max [] -> None
-- https://github.com/ocaml/ocaml/pull/722#issuecomment-243293068
let consider_pattern tc1 tc2 pc fp' dc =
Log.debug_f "Considering directory entry %s" dc >>
match String.cuts ~sep:"%" dc with
| [] -> assert false
| [_] -> Lwt.return_none
| [dc1; dc2] ->
begin match cut_prefix tc1 dc1, cut_suffix tc2 dc2 with
| None, _ | _, None -> Lwt.return_none
| Some dc1', Some dc2' ->
Log.debug_f "Matching %s against %s%%%s" pc dc1' dc2' >>
begin match cut_circumfix dc1' dc2' pc with
| None -> Lwt.return_none
| Some pc' ->
consider_fixed (Filename.concat tp dc) fp' (Filename.concat rp pc')
end
end
| _ -> Lwt.return_none
fun consider_pattern tc1 tc2 pc fp' dc =
Lwt.do
Log.debug ("Considering directory entry %s" % dc);
case String.cuts sep: "%" dc do
| [] -> assert false
| [_] -> Lwt.return_none
| [dc1, dc2] ->
case (cut_prefix tc1 dc1, cut_suffix tc2 dc2) do
| (None, _) | (_, None) -> Lwt.return_none
| (Some dc1', Some dc2') ->
Log.debug ("Matching %s against %s%s%s" % (pc, dc1', dc2'))
case cut_circumfix dc1' dc2' pc do
| None -> Lwt.return_none
| Some pc' ->
consider_fixed (Filename.concat tp dc) fp' (Filename.concat rp pc')
end
end
| _ -> Lwt.return_none
end
end
--
fun eval expr env =
case macroexpand expr env do
-- get_meta (add_meta x attr) == Some attr
| Form [Atom (_, Symbol "get_meta"), Form [Atom (_, Symbol "add_meta"), x, attr]] ->
(Form [Expr.symbol "Some", attr], env)
-- define x (add_meta x attr) == (); get_meta x == Some attr
| Form [Atom (_, Symbol "define"), Atom (_, name),
Form [Atom (_, Symbol "add_meta"), x, attr]] ->
let (value, env') = eval x env
env'' = Env.define name (Form [Expr.symbol "add_meta", value, attr]) env' in
(Expr.symbol "()", env'')
-- get_meta x == (Option.map get_meta (Env.lookup x env))
| Form [Atom (_, Symbol "get_meta"), Atom (_, lit)] ->
match Env.lookup lit env
| Some (Form [Atom (_, Symbol "add_meta"), value, attr]) ->
(Form [Expr.symbol "Some", attr], env)
| _ ->
(Expr.symbol "None", env)
end
| Form [Atom (_, Symbol "macro"), Atom (_, name), body] ->
undefined
| Form [Atom (loc, Symbol "macro") & _] ->
fail ("invalid macro syntax at %s" % Location.to_string loc)
| expr -> eval_expr expr env
end
-- * --
-- TODO: Implement pattern matching with substrings using tries or FAs.
x = "foo"
xs = ["foo", "bar", "baz"]
case (Random.choice xs)
| ~x -> "It's foo!"
| _ -> "Try, again!"
-- // --
fold [1 .. 100] init: [] do acc i ->
case (find_number i)
| Ok x -> [x & acc]
| Error -> acc
end
end
-- • --
// Example take from reason: <https://github.com/facebook/reason/issues/376>
-- do-end blocks
let result =
match a
| (One, Two x) ->
print_string "matched thingy x"
zz = 10
zz
| (Two x, One) ->
print_string "matched other x"
match another
| Foo ->
print_string "foo"
x
| Bar ->
print_string "bar"
x
end
end
// braces
let result =
case a {
| (One, Two x) ->
print_string "matched thingy x"
zz = 10
zz
| (Two x, One) ->
print_string "matched other x"
case another {
| Foo ->
print_string "foo"
x
| Bar ->
print_string "bar"
x
}
}
// indentation
let result =
case a
| (One, Two x) ->
print_string "matched thingy x"
zz = 10
zz
| (Two x, One) ->
print_string "matched other x"
case another
Foo ->
print_string "foo"
x
Bar ->
print_string "bar"
x
-- * --
let result =
case a
| (One, Two x) =>
print "matched thingy x";
let zz = 10 in
zz + 1
| (Two x, One) =>
print "matched other x";
case another do
| Foo =>
print "foo";
x
| Bar =>
print "bar";
x
end
-- Views
-- https://ghc.haskell.org/trac/ghc/wiki/ViewPatterns#Examples
type conc_list 'a =
| Empty
| Single 'a
| Concat (conc_list 'a) (conc_list 'a)
val view : conc_list 'a -> list 'a
def view Empty = []
| view (Single a) = [a]
| view (Concat (view left_head left_tail) right) = left_head
match x with
| (x y) ->
| (view x) x y