forked from dalnefre/kernel_abe
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibrary.knl
165 lines (158 loc) · 4.33 KB
/
library.knl
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
;;;
;;; Kernel standard library
;;;
; Section 5
;($define! car
; ($lambda ((x . #ignore)) x))
;($define! cdr
; ($lambda ((#ignore . x)) x))
;...
($define! caar
($lambda (((x . #ignore) . #ignore)) x))
($define! cdar
($lambda (((#ignore . x) . #ignore)) x))
($define! cadr
($lambda ((#ignore x . #ignore)) x))
($define! cddr
($lambda ((#ignore . (#ignore . x))) x))
;...
($define! caaar
($lambda ((((x . #ignore) . #ignore) . #ignore)) x))
($define! cdaar
($lambda ((((#ignore . x) . #ignore) . #ignore)) x))
($define! cadar
($lambda (((#ignore . (x . #ignore)) . #ignore)) x))
($define! cddar
($lambda (((#ignore . (#ignore . x)) . #ignore)) x))
($define! caadr
($lambda ((#ignore . ((x . #ignore) . #ignore))) x))
($define! cdadr
($lambda ((#ignore . ((#ignore . x) . #ignore))) x))
($define! caddr
($lambda ((#ignore . (#ignore . (x . #ignore)))) x))
($define! cdddr
($lambda ((#ignore . (#ignore . (#ignore . x)))) x))
;...
($define! caaaar
($lambda (((((x . #ignore) . #ignore) . #ignore) . #ignore)) x))
($define! cdaaar
($lambda (((((#ignore . x) . #ignore) . #ignore) . #ignore)) x))
($define! cadaar
($lambda ((((#ignore . (x . #ignore)) . #ignore) . #ignore)) x))
($define! cddaar
($lambda ((((#ignore . (#ignore . x)) . #ignore) . #ignore)) x))
($define! caadar
($lambda (((#ignore . ((x . #ignore) . #ignore)) . #ignore)) x))
($define! cdadar
($lambda (((#ignore . ((#ignore . x) . #ignore)) . #ignore)) x))
($define! caddar
($lambda (((#ignore . (#ignore . (x . #ignore))) . #ignore)) x))
($define! cdddar
($lambda (((#ignore . (#ignore . (#ignore . x))) . #ignore)) x))
;...
($define! caaadr
($lambda ((#ignore . (((x . #ignore) . #ignore) . #ignore))) x))
($define! cdaadr
($lambda ((#ignore . (((#ignore . x) . #ignore) . #ignore))) x))
($define! cadadr
($lambda ((#ignore . ((#ignore . (x . #ignore)) . #ignore))) x))
($define! cddadr
($lambda ((#ignore . ((#ignore . (#ignore . x)) . #ignore))) x))
($define! caaddr
($lambda ((#ignore . (#ignore . ((x . #ignore) . #ignore)))) x))
($define! cdaddr
($lambda ((#ignore . (#ignore . ((#ignore . x) . #ignore)))) x))
($define! cadddr
($lambda ((#ignore . (#ignore . (#ignore . (x . #ignore))))) x))
($define! cddddr
($lambda ((#ignore . (#ignore . (#ignore . (#ignore . x))))) x))
;...
($define! list*
($lambda (head . tail)
($if (null? tail)
head
(cons head (apply list* tail)))))
($define! $let
($vau (bindings . body) env
(eval (cons (list* $lambda (map car bindings) body)
(map cadr bindings))
env)))
($define! $cond
($vau clauses env
($if (null? clauses)
#inert
($let ((((test . body) . clauses) clauses))
($if (eval test env)
(apply (wrap $sequence) body env)
(apply (wrap $cond) clauses env))))))
; Section 6
($define! not? ($lambda (x) ($if x #f #t)))
($define! $and?
($vau x e
($cond ((null? x) #t)
((null? (cdr x)) (eval (car x) e)) ; tail context
((eval (car x) e) (apply (wrap $and?) (cdr x) e))
(#t #f))))
($define! $or?
($vau x e
($cond ((null? x) #f)
((null? (cdr x)) (eval (car x) e)) ; tail context
((eval (car x) e) #t)
(#t (apply (wrap $or?) (cdr x) e)))))
($define! reduce ; acyclic
($lambda (ls bin id)
($cond ((null? ls) id)
((null? (cdr ls)) (car ls))
(#t
(bin (car ls) (reduce (cdr ls) bin id))))))
($define! get-current-environment (wrap ($vau () e e)))
($define! $remote-eval
($vau (o e) d
(eval o (eval e d))))
($define! $set!
($vau (exp1 formals exp2) env
(eval (list $define! formals
(list (unwrap eval) exp2 env))
(eval exp1 env))))
($define! $provide!
($vau (symbols . body) env
(eval (list $define! symbols
(list $let ()
(list* $sequence body)
(list* list symbols)))
env)))
; Section 12
($define! integer? number?) ; integers are currently the only number type implemented
($define! -
($vau (first . rest) env
(eval (list + first
(list * -1
(cons + rest)))
env)))
($define! zero?
($vau args env
(eval (list* =? 0 args) env)))
;;;
;;; Concurrency
;;;
($define! $parallel list) ; only valid because 'list' evals in parallel
;;;
;;; Bonus definitions
;;;
($define! Y ; applicative Y-combinator (recursive fixed-point)
($lambda (f)
(($lambda (g)
(g g))
($lambda (h)
($lambda (x)
((f (h h)) x))))))
;;;
;;; Factorial example
;;;
($sequence
($define! factorial
($lambda (n)
($if (<=? n 1)
1
(* n (factorial (+ n -1))))))
(factorial 5))