-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunlambda.rkt
91 lines (80 loc) · 2.05 KB
/
unlambda.rkt
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
#lang racket/base
(require (for-syntax racket/base
syntax/strip-context)
racket/contract)
(define current-character (make-parameter #f))
(define one-arg-lambda
(recursive-contract (-> one-arg-lambda one-arg-lambda)))
; d is special and receives its first argument as a thunk
(define ((d x) y)
(unlambda-app (x) y))
(define (i x)
x)
(define (c x)
(call/cc
(lambda (continuation)
(unlambda-app x (procedure-reduce-arity continuation 1)))))
(define (e x)
(exit))
(define (v x)
v)
(define (((s x) y) z)
(unlambda-app (unlambda-app x z) (unlambda-app y z)))
(define ((k x) y)
x)
(define (%dot c)
(lambda (x)
(write-char c)
x))
(define r (%dot #\newline))
(define (@ x)
(let ([c (read-char)])
(current-character c)
(unlambda-app
x
(if (eof-object? c)
v
i))))
(define (%question c)
(lambda (x)
(unlambda-app
x
(if (equal? c (current-character))
i
v))))
(define (\| x)
(unlambda-app
x
(if (char? (current-character))
(%dot (current-character))
v)))
(define (applyer function thunk)
(if (equal? function d)
(d thunk)
(function (thunk))))
(define-syntax-rule (unlambda-app x y)
(#%plain-app applyer x (lambda () y)))
(define-syntax (unlambda-module-begin stx)
(syntax-case stx ()
[(_ )
#`(#%module-begin
(module configure-runtime racket/base
(#%require unlambda/parser)
(current-read-interaction read-term))
(void))]
[(_ body ...)
(let ([a-d-o (replace-context (car (syntax-e #'(body ...)))
#'(all-defined-out))])
#`(#%module-begin
(module configure-runtime racket/base
(#%require unlambda/parser)
(current-read-interaction read-term))
body ...
(provide #,a-d-o)))]))
(define-syntax-rule (unlambda-define name value)
(define/contract name one-arg-lambda (procedure-rename value 'name)))
(provide s k i d e v c r @ \| %dot %question #%datum all-defined-out
#%top-interaction
(rename-out [unlambda-app #%app]
[unlambda-module-begin #%module-begin]
[unlambda-define %define]))