This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
045check.wart
96 lines (80 loc) · 1.91 KB
/
045check.wart
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
let $if if
mac! (if ... args)
$if ~cdr.args
car.args
`(,$if ,car.args
,cadr.args
(if ,@cddr.args))
mac (or ... args)
if args
`(let $x ,car.args
(if $x
$x
(or ,@cdr.args)))
false
mac (backstopped_by default ... rest)
`(or ,@rest ,default)
mac (and ... args)
if no.args
1
if ~cdr.args
car.args
`(if ,car.args
(and ,@cdr.args)
false)
let ($=) (=) # ignore later refinements
def (isa 'Type x)
($= type.x Type)
def! (a = b)
(or (a $= b)
(and cons?.a cons?.b # default for objects
(car.a = car.b)
(cdr.a = cdr.b)))
# can't use '!=' because '!' is not an infix char
# '=' signals comparison; '<>' seems similar to '<-'
alias (~=) ~equal
mac (default var val|to)
`(or ,var
(,var <- ,val))
def (match? a b bindings)
default bindings :to (table) # used in later refinements
(or (a = b)
(a = '_) # _ matches anything
(and cons?.a cons?.b # default for objects
(match? car.a car.b bindings)
(match? cdr.a cdr.b bindings)))
def (only f)
(fn args
(if f (f @args)))
mac (check x test else)
`(let $x ,x
(if (,test $x)
$x
,else))
def (maybe f a b|to)
if a
(f a b)
b
mac (in x ... choices)
`(let $x ,x
(or ,@(map (fn(_) `(,_ = $x))
choices)))
def (predicate x)
if (isa function x)
x
(x = :else)
(fn() 1)
:else
(fn(_) (x = _))
mac (caselet var expr ... branches)
let expand (afn(branches)
(if ~cdr.branches
car.branches
`(if ((predicate ,car.branches) ,var)
,cadr.branches
,(self cddr.branches))))
`(let ,var ,expr ,expand.branches)
mac (case expr ... branches)
`(caselet $x ,expr ,@branches)
mac (acase expr ... branches)
`(caselet it ,expr ,@branches)