-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM.scm
90 lines (74 loc) · 3.24 KB
/
M.scm
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
;;; Copyright 2019 by Christian Jaeger <ch@christianjaeger.ch>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require easy)
(export (macro M)
(macro M*)
(macro MF))
;; Quick new idea for (pattern) matching, just... work on the
;; non-|show| representation. Will only work for readable
;; representations though. Isn't this just a Scheme inconsistency? Has
;; Clojure fixed this? But could easily change to take the head as the
;; constructor and derive the predicate name from it by simply
;; appending |?|, or perhaps look up some class property for that.
;; Extend with constant matching? Actually, the |show| way would also
;; work for distinguishing constants `'foo` from variables `foo`!
;; Wow. So don't give Clojure's way priority, right?
(defmacro (M expr . clauses)
(with-gensym
V
`(let ((,V ,expr))
(xcond ,@(map (lambda (clause)
(assert* pair? clause
(lambda (clause)
(let-pair
((test body) clause)
(let ((test* (source-code test)))
(mcase
test
((pair-of (possibly-source-of symbol?)
(possibly-source-of symbol?))
`((pair? ,V)
(let-pair ((,(car test*)
,(cdr test*))
,V)
,@body)))
((vector-of (possibly-source-of symbol?))
`((and (vector? ,V)
(= (vector-length ,V)
,(vector-length test*)))
(let-vector (,(vector->list test*)
,V)
,@body)))
(null?
`((null? ,V)
,@body))))))))
clauses)))))
(defmacro (M* . clauses)
(with-gensym V
`(lambda (,V)
(M ,V ,@clauses))))
(defmacro (MF expr . clauses)
`(M (force ,expr) ,@clauses))
(TEST
> (def t (M* ((some . other) (vector some other))
([some other such] (list other some such))
([] 'emptyvec)
;; ((some other such) (list other some such))
;; ((some other . such) (list other some such))
;; sgh how to do values? meh. rlly missing,consistency,sgh?.
(() 'none)))
> (%try (t 1))
(exception text: "no match\n")
> (t (list 3 4 5))
[3 (4 5)]
> (t (list))
none
> (t (vector))
emptyvec
> (t (vector 'a 'b 10))
(b a 10)
> (%try (t (vector 'a 'b)))
(exception text: "no match\n"))