-
Notifications
You must be signed in to change notification settings - Fork 2
/
quines.rkt
90 lines (80 loc) · 2.78 KB
/
quines.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
#lang racket
;;; One of the fundamental techniques involved in Ken Thompson's Reflections on
;;; Trusting Trust is quining: writing a program which can access its own source
;;; code. Here are a few example quines in Racket.
;;; QUINE #1
;;; with care, we could move replace-magic inside of the quine,
;;; but I think it's clearer this way.
(define (replace-magic r e)
(match e
[`(quote ,_) e]
['MAGIC r]
[(cons a b) (cons (replace-magic r a) (replace-magic r b))]
[_ e]))
(define (quine)
(define source
'(define (quine)
(define source MAGIC)
(displayln "Hello I am a quine.")
(replace-magic (list 'quote source) source)))
(displayln "Hello I am a quine.")
(replace-magic (list 'quote source) source))
;;; QUINE #2
;;; takes advantage of quasiquotation, but is a little tricky because of that
(define (quine2)
(define (source x)
`(define (quine2)
(define (source x) ,x)
(displayln "Hello I am a quine.")
(source
(list 'quasiquote (source (list (string->symbol "unquote") 'x))))))
(displayln "Hello I am a quine.")
(source
(list 'quasiquote (source (list (string->symbol "unquote") 'x)))))
;;; A more advanced technique is writing a quine *generator*. You give a quine
;;; generator a program that *wants* to access its own source, and it produces a
;;; program that *does*.
;;; QUINE GENERATOR #1
;;; (make-quine self '(list 'hello self))
;;; returns a program that runs (list 'hello self),
;;; with 'self bound to its own source code
(define (make-quine name src)
(define magic-src
`(let ([,name (replace-magic 'MAGIC 'MAGIC)])
,src))
(replace-magic magic-src magic-src))
;;; making this self-sufficient
(define (make-quine-better name src)
(define magic-src
`(let ()
(define (replace-magic r e)
(match e
[`(quote ,_) e]
['MAGIC r]
[(cons a b) (cons (replace-magic r a) (replace-magic r b))]
[_ e]))
(define ,name (replace-magic (list 'quote MAGIC) MAGIC))
,src))
(replace-magic (list 'quote magic-src) magic-src))
;;; QUINE GENERATOR #2
;;; I barely understand this one myself.
;;;
;;; (make-quine2 (lambda (x) `(list 'hello ,x)))
;;; returns a program that runs (list 'hello SELF)
;;; where SELF is its own (quoted) source code
(define (make-quine2 func)
(define gen (gensym 'gen))
(define self (gensym 'self))
(define arg (gensym 'x))
(define (source x)
`(let ()
(define (,gen ,arg) ,x)
(define ,self
(,gen
(list 'quasiquote (,gen (list (string->symbol "unquote") ',arg)))))
,(func self)))
(source (list 'quasiquote (source (list 'unquote arg)))))
;; cheating implementation of quine2:
(define (make-quine2-cheating func)
(let ([name (gensym)])
(make-quine name (func name))))