-
Notifications
You must be signed in to change notification settings - Fork 30
/
example-5-quine.scm
45 lines (34 loc) · 1.22 KB
/
example-5-quine.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
;; Example 5: quine-ception!
;; Definitions
;; HACK: We visualize the answer to our query by synthesizing the dead code Q.
(define answer
(lambda ()
'(Q: ,Q)))
;; Tower of interpretation!
;;; Scheme
;;;; miniKanren
;;;;; Scheme interpreter written in miniKanren
;;;;;; Scheme interpreter (eval) relationally interpreted
;;;;;;; Scheme program (,Q) being interpreted by eval <-- YOU ARE HERE
(define eval
(lambda (term env)
(match term
;; Construct literal data.
(`(quote ,datum) datum)
;; Create a single-argument closure.
(`(lambda (,(? symbol? x)) ,body)
(lambda (a)
(eval body (lambda (y)
(if (equal? x y) a (env y))))))
;; Reference a variable.
((? symbol? x) (env x))
;; Build a pair from ,a and ,b.
(`(cons ,a ,b) (cons (eval a env) (eval b env)))
;; Apply ,rator to ,rand.
(`(,rator ,rand)
((eval rator env) (eval rand env))))))
;; Test Cases
(eval ',Q 'initial-env) => Q
;; This takes about a minute to synthesize due to evaluating through multiple
;; nested levels of interpretive overhead. Note: synthesizing a quine with a
;; comparable interpreter implemented directly in miniKanren is much faster.