-
Notifications
You must be signed in to change notification settings - Fork 54
/
epsilon.cljc
775 lines (639 loc) · 21.3 KB
/
epsilon.cljc
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
(ns meander.epsilon
(:refer-clojure :exclude [and find keyword let not or some symbol])
#?(:clj
(:require [clojure.core :as clj]
[clojure.core.specs.alpha :as core.specs]
[clojure.spec.alpha :as s]
[meander.match.epsilon :as r.match]
[meander.match.syntax.epsilon :as r.match.syntax]
[meander.strategy.epsilon :as r]
[meander.syntax.epsilon :as r.syntax]
[meander.substitute.epsilon :as r.subst]
[meander.substitute.syntax.epsilon :as r.subst.syntax])
:cljs
(:require [cljs.core :as clj]
[cljs.core.specs.alpha :as core.specs]
[cljs.spec.alpha :as s :include-macros true]
[meander.match.epsilon :as r.match :include-macros true]
[meander.match.syntax.epsilon :as r.match.syntax :include-macros true]
[meander.strategy.epsilon :as r :include-macros true]
[meander.syntax.epsilon :as r.syntax]
[meander.substitute.epsilon :as r.subst :include-macros true]
[meander.substitute.syntax.epsilon :as r.subst.syntax :include-macros true]))
#?(:clj (:import (clojure.lang ExceptionInfo)))
#?(:cljs (:require-macros [meander.epsilon :refer [defsyntax find match search]])))
;; ---------------------------------------------------------------------
;; Match, Find, Search
(defmacro match
"Traditional pattern matching operator.
Syntax
(match x
pattern_1 expr_1
,,,
pattern_n expr_n)
Attempts to pattern match `x` against one of patterns `pattern_1`
through `pattern_n`. If some pattern `pattern_i` matches
successfully, `expr_i` will be executed. If none of the patterns
match successfully an error will be thrown indicating the pattern
match failed.
This operator restricts patterns which may have several possible
solutions. For example, the pattern
#{?x ?y}
matches any set with at least two elements. However, with
consideration to the property that Clojure sets are unordered, there
are many possible ways we could bind values for `?x` and
`?y`. Because there is no obvious way to know which solution to
pick, patterns which have this property are illegal in the context
of this operator.
For operators which relax this restriction, see `find` and `search`."
{:arglists '([x & clauses])
:style/indent :defn}
[& args] `(meander.match.epsilon/match ~@args))
(defmacro find
"Like `search` but returns only the first successful match."
{:arglists '([x & clauses])
:style/indent :defn}
[& args] `(meander.match.epsilon/find ~@args))
(defmacro search
"Like `match` but allows for patterns which may match `x` in more
than one way. Returns a lazy sequence of expression values in
depth-first order.
Example
(search [1 2 3]
[!xs ... !ys ...]
{'!xs !xs, '!ys !ys})
;; =>
({!xs [], !ys [1 2 3]}
{!xs [1], !ys [2 3]}
{!xs [1 2], !ys [3]}
{!xs [1 2 3], !ys []})
Note, if only the first value is needed, use `find` instead. The
expression
(first (search x ,,,))
can be significantly slower than
(find x ,,,)"
{:arglists '([x & clauses])
:style/indent :defn}
[& args] `(meander.match.epsilon/search ~@args))
(defmacro breadth-first-search
"Like `search` but traverses the search space in breadth first
order."
{:arglists '([x & clauses])
:style/indent :defn}
[& args]
(with-meta `(meander.match.epsilon/search ~@args)
(assoc (meta &form) :search-order :breadth-first)))
;; ---------------------------------------------------------------------
;; Substitute
(defmacro subst
;; TODO: This is not enough documentation.
"Substitution operator, the inverse of pattern matching. Evaluates
`pattern` in the Clojure environment."
{:style/indent :defn}
[pattern] `(r.subst/substitute ~pattern))
;; ---------------------------------------------------------------------
;; Rewrite
(def ^{:arglists '([x])
:private true}
compile-rewrite
(r/rewrite
[?x (!match !substitution ...)]
(`find ?x . !match (`subst !substitution) ...)
_
[:error "rewrite expects and odd number of arguments"]))
(defmacro rewrite
"Syntactic sugar for
(find x
p_1 (subst p_2)
,,,
p_n-1 (subst p_n))"
{:style/indent :defn}
[x & clauses]
(meander.match.epsilon/match (compile-rewrite [x clauses])
[:error ?error-message]
(throw (ex-info ?error-message {}))
?form
?form))
(s/fdef rewrite
:args (s/cat :x any?
:clauses (s/* (s/cat :match any?
:substitution any?)))
:ret any?)
(def ^{:arglists '([x])
:private true}
compile-rewrites
(r/rewrite
[?x (!match !substitution ...)]
(`meander.match.epsilon/search ?x . !match (`r.subst/substitute !substitution) ...)
_
[:error "rewrite expects and odd number of arguments"]))
(defmacro rewrites
{:style/indent :defn}
[x & clauses]
(meander.match.epsilon/match (compile-rewrites [x clauses])
[:error ?error-message]
(throw (ex-info ?error-message {}))
?form
?form))
(s/fdef rewrites
:args (s/cat :x any?
:clauses (s/* (s/cat :match any?
:substitution any?)))
:ret any?)
;; ---------------------------------------------------------------------
;; Syntax extensions
(defn match-syntax?
[env]
(match env
{::r.syntax/phase :meander/match}
true
_
false))
(defn subst-syntax?
[env]
(match env
{::r.syntax/phase :meander/substitute}
true
_
false))
(defmacro defsyntax
{:arglists '([name doc-string? attr-map? [params*] prepost-map? body]
[name doc-string? attr-map? ([params*] prepost-map? body) + attr-map?])
:style/indent :defn}
[& args]
`(r.syntax/defsyntax ~@args))
#?(:clj
(s/fdef defsyntax
:args ::core.specs/defn-args))
(defsyntax and
"Pattern matching operator which matches when `pattern` and,
optionally, all of `patterns` match."
[pattern & patterns]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/and ~pattern ~@patterns)
;; else
&form))
(defsyntax or
"Pattern matching operator which matches when either `pattern` or,
opitionally, one of `patterns` match."
[pattern & patterns]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/or ~pattern ~@patterns)
;; else
&form))
(defsyntax not
"Pattern matching operator which matches when `pattern` does not
match."
[pattern]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/not ~pattern)
;; else
&form))
(defsyntax let
{:style/indent 1}
([binding-patterns]
(case (::r.syntax/phase &env)
:meander/match
(meander.match.epsilon/match binding-patterns
[_ _ ...]
(reduce
(fn [?inner [?pattern ?expression]]
(r.subst/substitute (`r.match.syntax/let ?pattern ?expression ?inner)))
'_
(reverse (partition 2 binding-patterns)))
_
(throw (ex-info "The second argument to let must be a vector with an number or elements"
{:form &form
:meta (meta &form)})))
;; else
&form))
([binding-patterns target-pattern]
(case (::r.syntax/phase &env)
:meander/match
(meander.match.epsilon/match binding-patterns
[_ _ ...]
(reduce
(fn [?inner [?pattern ?expression]]
(r.subst/substitute (`r.match.syntax/let ?pattern ?expression ?inner)))
target-pattern
(reverse (partition 2 binding-patterns)))
_
(throw (ex-info "The second argument to let must be a vector with an number or elements"
{:form &form
:meta (meta &form)})))
;; else
&form)))
(s/fdef let
:args (s/cat :binding-patterns (s/and vector? (s/cat :pattern any? :expression any?))
:target-pattern (s/? any?))
:ret seq?)
(defsyntax pred
"Pattern matching operator which successfully matches whenever the
target of pattern matching applied to `expr` returns a truthy
value.
(match 1
(pred odd?)
:okay)
;; => :okay
Optionally, additional patterns `patterns` may be passed in which
case they will be treated as an `and` pattern against the target of
pattern matching.
(match 1
(pred odd? ?x)
:okay)
is the same as
(match 1
(and (pred odd?) ?x)
:okay)"
{:arglists '([expr] [expr & patterns])}
([p]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/pred ~p)
;; else
&form))
([p pattern]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/pred ~p ~pattern)
;; else
&form))
([p pattern & patterns]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/pred ~p (r.match.syntax/and ~pattern ~@patterns))
;; else
&form)))
(defsyntax seqable
"Pattern matching operator which matches the `seq` of anything that
is `seqable?` against
(p1 ,,, pn)
where the sequence `p1` through `pn` is equal to `patterns`."
[& patterns]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/pred seqable? (r.match.syntax/apply seq ~patterns))
;; else
&form))
(defsyntax scan
"Pattern matching operator which matches the `seq` of `seqable?`
forms of the shape
(_ ... p1 ,,, pn . _ ...)
or `vectors?` of the form
[_ ... p1 ,,, pn . _ ...]
where the sequence `p1` through `pn` is equal to `patterns`."
[& patterns]
(case (::r.syntax/phase &env)
:meander/match
(clj/let [patternc (count patterns)
[as as-pattern] (drop (- patternc 2) patterns)
inner (if (= :as as)
`(~@'(_ ...) ~@patterns ~@'(. _ ...) :as ~as-pattern)
`(~@'(_ ...) ~@patterns ~@'(. _ ...)))]
`(r.match.syntax/or
[~@inner]
;; Prevent producing the same search results twice when
;; the target is a vector.
(r.match.syntax/and (r.match.syntax/not (r.match.syntax/pred vector?))
(seqable ~@inner))))
;; else
&form))
(defsyntax separated
"Pattern matching operator which matches the `seq` of `seqable?`
forms of the shape
(_ ... p1 ,,, . _ ... pn . _ ...)
or `vectors?` of the form
[_ ... p1 ,,, . _ ... pn . _ ...]
where the sequence `p1` through `pn` is equal to `patterns`."
[& patterns]
(case (::r.syntax/phase &env)
:meander/match
(clj/let [inner `(~@'(_ ...) ~@(mapcat cons patterns (repeat '(. _ ...))))]
`(r.match.syntax/or
[~@inner]
;; Prevent producing the same search results twice when
;; the target is a vector.
(r.match.syntax/and (r.match.syntax/not (r.match.syntax/pred vector?))
(seqable ~@inner))))
;; else
&form))
(defsyntax app
"Pattern matching operator which applies pattern matching the result
applying `f` to the current value being matched."
([f pattern]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/apply ~f ~pattern)
:meander/substitute
`(r.subst.syntax/apply ~f ~pattern)
;;
&form))
([f pattern & patterns]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/apply ~f (r.match.syntax/and ~pattern ~@patterns))
:meander/substitute
`(r.subst.syntax/apply (partial apply ~f) [~pattern ~@patterns])
;; else
&form)))
(defsyntax guard
"Pattern matching operator which succeeds whenever `pred-expr`
returns a truthy result. `pred-expr` is evaluated by Clojure."
([pred-expr]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/guard ~pred-expr)
;; else
&form)))
(defsyntax re
"Pattern matching operator which matches strings which match the
regular expression `regex-pattern` with `re-matches`. Optionally, a
second argument `capture-pattern` can be passed which will be
matched against the result of the underlying `re-matches` call.
(match \"foo\"
(re #\"...\")
true)
;; =>
true
(match \"foo\"
(re #\"(.)(.)(.)\" [?0 ?1 ?2 ?3])
[?0 ?1 ?2 ?3])
;; =>
[\"foo\" \"f\" \"o\" \"o\"]"
([regex-pattern]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/re ~regex-pattern)
;; else
&form))
([regex-pattern capture-pattern]
(case (::r.syntax/phase &env)
:meander/match
`(r.match.syntax/re ~regex-pattern ~capture-pattern)
;; else
&form)))
(defsyntax $
"Pattern matching and substitution operator.
When used as a pattern matching operator will attempt match
`pattern` against some value that is a subtree of the target of
pattern matching. Optionally, `context-pattern` (should be a
variable pattern) can be passed and will be applied against a
function. This function accepts one argument and when invoked with
some value `x` will return the original, root target updated with
the `x` installed at the place where `pattern` matched successfully.
(match [:A 2]
($ (pred number? ?x))
?x)
;; => 2
(match [:A 2]
($ ?context (pred number? ?x))
(?context 3))
;; => [:A 3]"
([pattern]
(case (::r.syntax/phase &env)
(:meander/match :meander/substitute)
`(r.syntax/$ ~pattern)
;; else
&form))
([context-pattern pattern]
(case (::r.syntax/phase &env)
(:meander/match :meander/substitute)
`(r.syntax/$ ~context-pattern ~pattern)
;; else
&form)))
(defsyntax $*
([context pattern]
(case (::r.syntax/phase &env)
:meander/match
`(and (~'$ ?context# (and ~pattern ?pattern#))
(let [~context (fn
([]
(?context# ?pattern#))
([f#]
(?context# (f# ?pattern#)))
([f# & args#]
(?context# (apply f# ?pattern# args#))))]))
:meander/substitute
`(app ~context ~pattern)
;; else
&form))
([context pattern & patterns]
(case (::r.syntax/phase &env)
:meander/match
`(and (~'$ ?context# (and ~pattern ~@patterns ?pattern#))
(let [~context (fn
([]
(?context# ?pattern#))
([f#]
(?context# (f# ?pattern#)))
([f# & args#]
(?context# (apply f# ?pattern# args#))))]))
:meander/substitute
`(app ~context ~pattern ~@patterns)
;; else
&form)))
(defsyntax symbol
"Pattern matching and substitution operator.
When used as a pattern matching operator it will match a symbol
with `name` and, optionally, `namespace`.
(match 'foo/bar
(symbol ?name)
?name)
;; => \"bar\"
(match :foo/bar
(symbol ?namespace ?name)
[?namespace ?name])
;; => [\"foo\" \"bar\"]
When used as a substutition operator it will create a symbol with
`name` and, optionally, `namespace` e.g. it behaves the same as
`clojure.core/symbol` in the context of normal substitution
behavior.
(subst (symbol \"foo\" \"bar\"))
;; => 'foo/bar
;; clojure.core/let
(let [!namespaces [\"foo\" \"foo\"]
!names [\"bar\" \"baz\"]]
(subst [(symbol !namespaces !names) ...]))
;; => ['foo/bar 'foo/baz]"
([name]
(case (::r.syntax/phase &env)
:meander/match
`(and (pred symbol?) (app name ~name))
:meander/substitute
`(app clj/symbol ~name)
;; else
&form))
([namespace name]
(case (::r.syntax/phase &env)
:meander/match
`(and (pred symbol?)
(app namespace ~namespace)
(app name ~name))
:meander/substitute
`(app clj/symbol ~namespace ~name)
;; else
&form)))
(defsyntax keyword
"Pattern matching and substitution operator.
When used as a pattern matching operator it will match a keyword
with `name` and, optionally, `namespace`.
(match :foo/bar
(keyword ?name)
?name)
;; => \"bar\"
(match :foo/bar
(keyword ?namespace ?name)
[?namespace ?name])
;; => [\"foo\" \"bar\"]
When used as a substutition operator it will create a keyword with
`name` and, optionally, `namespace` e.g. it behaves the same as
`clojure.core/keyword` in the context of normal substitution
behavior.
(subst (keyword \"foo\" \"bar\"))
;; => :foo/bar
;; clojure.core/let
(let [!namespaces [\"foo\" \"foo\"]
!names [\"bar\" \"baz\"]]
(subst [(keyword !namespaces !names) ...]))
;; => [:foo/bar :foo/baz]"
([name]
(case (::r.syntax/phase &env)
:meander/match
`(and (pred keyword?) (app name ~name))
:meander/substitute
`(app clj/keyword ~name)
;; else
&form))
([namespace name]
(case (::r.syntax/phase &env)
:meander/match
`(and (pred keyword?)
(app namespace ~namespace)
(app name ~name))
:meander/substitute
`(app clj/keyword ~namespace ~name)
;; else
&form)))
(defsyntax with
"Pattern matching and substitution operator.
Syntax
(with [%pattern-name pattern ...]
target-pattern)
Allows for patterns to be referenced by %pattern-name in
target-pattern.
Example
(match [[1 2] [1 2]]
(with [%x-y [?x ?y]]
[%x-y %x-y])
{:x ?x, :y ?y})
;; =>
{:x 1, :y 2}"
{:style/indent 1}
([pattern-bindings body]
(case (::r.syntax/phase &env)
(:meander/match :meander/substitute)
(meander.epsilon/find pattern-bindings
;; Even number of syntactically valid bindings.
[(meander.epsilon/symbol nil (meander.epsilon/re #"%.+")) _ ...]
`(r.syntax/with ~pattern-bindings ~body)
;; Even number of bindings; one is invalid.
(r.syntax/with [%invalid-name (not (symbol nil (re #"%.+")))
%invalid-namespace (symbol (not nil) _)
%invalid-binding (or %invalid-name %invalid-namespace)]
[_ _ ... (and %invalid-binding ?x) _ . _ _ ...])
(throw (ex-info "with binding form must be a simple symbol the name of which begins with \"%\""
{:invalid-binding ?x
:form &form}))
;; Invalid binding form.
(not [_ _ ...])
(throw (ex-info "first argument to with must be a vector of the form [%<name> <pattern> ...]"
{:invalid-bindings pattern-bindings
:form &form})))
;; else
&form)))
(defsyntax gather
([pattern]
(case (::r.syntax/phase &env)
:meander/match
`(seqable (or ~pattern _#) ...)
;; else
&form))
([pattern count-pattern]
(case (::r.syntax/phase &env)
:meander/match
(match (r.syntax/parse count-pattern)
;; Logic variables, memory variables, `..?v`, and `..!v`.
(or {:tag :dtl, :lvr {:symbol ?var}}
{:tag :lvr :symbol ?var}
{:tag :dtm, :mvr {:symbol ?var}}
{:tag :mvr :symbol ?var})
`(let [?counter# (volatile! 0)]
(with [%gather# (or (and ~pattern
(let [_gather# (vswap! ?counter# inc)]))
_gather_#)]
(and (seqable %gather# ...)
(let [~?var (deref ?counter#)]))))
;; Natural numbers and `..n`
(or {:tag :lit, :value (pred nat-int? ?n)}
{:tag :dt+, :n ?n})
(clj/let [ellipsis (clj/symbol (str ".." ?n))]
`(let [?counter# (volatile! 0)]
;; This with should not be needed but is here temporarily
;; due to a compiler bug which can revealed by inlining
;; the %gather# reference.
(with [%gather# (or (and ~pattern
(let [_gather# (vswap! ?counter# inc)]))
_gather#)]
(and (seqable %gather# ~ellipsis)
(guard (<= ~?n (deref ?counter#)))))))
;; `_` and `...`
(or {:tag :any}
{:tag :dt*})
`(seqable (or ~pattern _#) ...)
_
(throw (ex-info "second argument to gather must be logic variable, memory variable, _ pattern, or an ellipsis"
{:form &form})))
;; else
&form)))
(defsyntax number
"Pattern matching operator which matches a `number?`. Optionally
`pattern` may be passed to further pattern match on the value."
([] `(number _#))
([pattern]
(if (match-syntax? &env)
`(pred number? ~pattern)
&form)))
(defsyntax some
"Pattern matching operator which matches a non `nil`
value. Optionally, `pattern` may be passed to further match on the
value."
([]
(if (match-syntax? &env)
`(not nil)
&form))
([pattern]
(if (match-syntax? &env)
`(and (not nil) ~pattern)
&form)))
(defsyntax cata
"Pattern matching operator which causes pattern matching to recurse
on the target of pattern matching. The result is then pattern
matched with `pattern`.
(match [1 2 3]
[(cata ?x) (cata ?y) (cata ?z)]
[?x ?y ?z]
3
:three
2
:two
1
:one)
;; =>
[:one :two :three]"
([pattern]
(if (match-syntax? &env)
`(r.match.syntax/cata ~pattern)
&form)))