I have a problem with the OS Shen, for which problem I would like to
ask for some help from the group.
I have the below file in Shen, copied from two textbooks:
------------------------------------------------------------
\\ Lazy lists and more, in Shen
\\
\\ Dr Antti Ylikoski 2017-01-15
\\
\\ First load the maths-lib.shen
(defmacro @c-macro
[@c X Y] -> [cons [freeze X] [freeze Y]])
(defmacro @l-macro
[@l ] -> (freeze [])
[@l X | Y] -> [@c X [@l | Y]])
(define @hd
X -> (thaw (hd X)))
(define @tl
X -> (thaw (tl X)))
\\ ------------------------------------------------------------
\\ Abelson-Sussman-Sussman p. 50
(define smallest-divisor
N -> (find-divisor N 2))
(define find-divisor
N Test-Divisor ->
(cases (> (square Test-Divisor) N) N
(divides? Test-Divisor N) Test-Divisor
true (find-divisor N (+ Test-Divisor 1))))
(define divides?
A B -> (divisible-by? B A))
(define prime?
N -> (= N (smallest-divisor N)) where (> N 0)
N -> (error "Neg arg to prime?"))
(define next-prime
X -> X where (prime? X)
X -> (next-prime (+ 1 X)))
(define primes-from
N -> (@c N (primes-from (next-prime (+ N 1)))))
(define primes \\ Returns a lazy stream
-> (primes-from 2))
------------------------------------------------------------
When attempting to create a lazy stream of all primes by calling
(set p (primes))
the system will remain in an infinite loop.
I have (track ...)ed and (step +)ed the program. The following will
happen.
When the Shen expression (primes) is called, the expression
(primes-from 2) will be Shen evaluated.
This, in turn, will cause the expression
(@c 2 (primes-from (next-prime 3))) to be evaluated.
This call of the (@c ...) macro should return a cons cell of two
(freeze ...)ed Shen expressions, namely (freeze 2) and
(freeze (primes-from (next-prime 3))).
This will not happen. The Shen expression
(primes-from (next-prime 3)) will get called, which will run an
infinite loop. This will happen instead of the said Shen form being
(freeze...)d and the freeze'd form returned.
Any smart help would be very much appreciated.
yours, AJY
Helsinki, Finland, the EU