Problem with Reasoned Schemer 2nd ed's conj2 / append-map-inf in Racket

102 views
Skip to first unread message

Jeff Ward

unread,
Jun 14, 2018, 5:45:22 PM6/14/18
to minikanren
Another person has posted about trouble using the Reasoned Schemer 2nd ed code in Racket, but I thought I would add some specifics:

My trouble starts in Chapter 1, Frame 50, where conj2 is used. conj2 is not defined in the installed files, so I typed in the definition of conj2 and everything that it depends on, using the definitions provided in Chapter 10. The error that I get says:
car: contract violation
expected: pair?
given: '()
pointing to the (g (car s-inf)) call in append-map-inf.


Hopefully, by the time I finish the book, I'll be able to debug this myself, but I am not there yet.  Here is my code:

#lang racket
(require Racket-miniKanren/miniKanren/mk)
(define succeed (== #t #t))
(define fail (== #t #f))
(define else succeed)

; Frame 10:55 in Reasoned Schemer, 2nd ed
(define (append-inf s-inf t-inf)
(cond
((null? s-inf) t-inf)
((pair? s-inf)
(cons (car s-inf)
(append-inf (cdr s-inf) t-inf)))
(else (lambda()
(append-inf t-inf (s-inf))))))

; Frame 10:84 in Reasoned Schemer, 2nd ed
(define (append-map-inf g s-inf)
(cond
((null? s-inf) '())
((pair? s-inf)
(append-inf (g (car s-inf))
(append-map-inf g (cdr s-inf))))
(else (lambda ()
(append-map-inf g (s-inf))))))

; Frame 10:81 in Reasoned Schemer, 2nd ed
(define (conj2 g1 g2)
(lambda (s)
(append-map-inf g2 (g1 s))))

; Frame 1:50 in Reasoned Schemer, 2nd ed
(run* (q)
(conj2 succeed succeed)) ; runtime error

; Frame 1:51
(run* (q)
(conj2 succeed (== 'corn q))) ; runtime error

; Frame 1:52
(run* (q)
(conj2 fail (== 'corn q))) ; runtime error

William Byrd

unread,
Jun 14, 2018, 5:47:50 PM6/14/18
to minikanren
Hi Jeff!

We just posted code for The Reasoned Schemer, Second Edition:

https://github.com/TheReasonedSchemer2ndEd/CodeFromTheReasonedSchemer2ndEd

Does this help?

Thanks!

--Will
> --
> You received this message because you are subscribed to the Google Groups
> "minikanren" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to minikanren+...@googlegroups.com.
> To post to this group, send email to minik...@googlegroups.com.
> Visit this group at https://groups.google.com/group/minikanren.
> For more options, visit https://groups.google.com/d/optout.

Jeff Ward

unread,
Jun 14, 2018, 5:59:04 PM6/14/18
to minikanren
Yes, works like a charm.  Thanks!

William Byrd

unread,
Jun 14, 2018, 5:59:51 PM6/14/18
to minikanren
w00t! :)

On Thu, Jun 14, 2018 at 4:59 PM, 'Jeff Ward' via minikanren
<minik...@googlegroups.com> wrote:
> Yes, works like a charm. Thanks!
>

Jeff Ward

unread,
Aug 6, 2018, 4:15:43 PM8/6/18
to minikanren
Now I am having trouble in Chapter 3.  

> (run* x (listo '(a b c . ,x)))
'(_0)    ; According to Section 3:12, this should have no value
> (run 1 x (listo '(a b c . ,x)))
'(_0)    ; Also looks wrong to me
> (run 1 x (listo '(a b c . d)))
'()        ; But this seems correct

In order to get the above to evaluate, I added the following to trs2-impl.scm:
#lang racket ; at the beginning
...
; ... then these lines from The Reasoned Schemer 2nd ed to the end of the file:
; 2:5
(defrel (caro p a)
  (fresh (d)
         (== (cons a d) p)))

; 2:13
(defrel (cdro p d)
  (fresh (a)
         (== (cons a d) p)))

; 2:33
(defrel (nullo x)
  (== '() x))

; 3:8
(defrel (listo l)
  (conde
   ((nullo l))
   ((fresh (d)
           (cdro l d)
           (listo d)))))


Thanks for any help that you can give!

Dakota Fisher

unread,
Aug 6, 2018, 4:20:47 PM8/6/18
to 'Jeff Ward' via minikanren

In your queries, are those quotes supposed to be quotes (') or are they supposed to be quasiquotes (`)?

Jason Hemann

unread,
Aug 6, 2018, 4:20:58 PM8/6/18
to minik...@googlegroups.com
Hi,

It could be a fonting issue on my end but to double check, are those backticks? I think you want a quasiquote there, not a quote.

Hope that's it.

JBH

Jeff Ward

unread,
Aug 6, 2018, 4:32:52 PM8/6/18
to minikanren
Thanks, Dakota and Jason!  That was the problem.
Reply all
Reply to author
Forward
0 new messages