[racket] %<= doesn't like logic variables bound to numbers?

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Wed May 21 10:49:24 EDT 2014

Hi Tom,

This error is not coming from %<=. Your code includes this expression:

(- up vp)

and the error reports a contract violation of "-", because you've
given it up and vp, which are logic variables and not numbers.

The relational version doesn't behave the same because of this
behavior of %is from the documentation: "The goal (%is E1 E2) unifies
with E1 the result of evaluating E2 as a Racket expression. E2 may
contain logic variables, which are dereferenced automatically. Fails
if E2 contains unbound logic variables."

In other words, in the first version (- up vp) is equivalentish to (-
(logic-var-deref up) (logic-var-deref vp)).

Here is the more idiomatic way to do what you want, if you don't want
to use (%is #t ...):

(define %within-span-procedural
  (%rel (u up v vp n s diff)
        [(u v n s)
         (%is up (list-index s u))
         (%is vp (list-index s v))
         (%is diff (abs (- up vp)))
         (%<= n diff)]))

I might prefer something like

(define-syntax-rule (%must e)
 (%is #t e))

Or to be more Rackety:

(define-syntax-rule (%must e)
 (%is #f (not e)))

Hope this helps,

Jay

On Thu, May 15, 2014 at 10:16 PM, Tom Dean <tld at google.com> wrote:
> (require racklog)
>
> (define (list-index l x)
>   (for/or ([y l] [i (in-naturals)] #:when (equal? x y)) i))
>
> (define ALPHAQ '(A B C D E F G H I J K L M N O P Q))
>
> ;; Are U and V within N words of each other in S
> (define %within-span-relational
>   (%rel (u up v vp n s)
> [(u v n s)
> (%is up (list-index s u))
> (%is vp (list-index s v))
> (%is #t (<= n (abs (- up vp))))]  ))
>
> ;; > (%which () (%within-span-relational 'C 'G 3 ALPHAQ))
> ;; '()
>
> ;; Same as above but using the relation %<= instead of
> (define %within-span-procedural
>   (%rel (u up v vp n s)
> [(u v n s)
> (%is up (list-index s u))
> (%is vp (list-index s v))
> (%<= n (abs (- up vp)))]  ))
>
> ;; > (%which () (%within-span-procedural 'C 'G 3 ALPHAQ))
> ;; > -: contract violation
> ;;   expected: number?
> ;;   given: #<logic-var>
> ;;   argument position: 1st
> ;;   other arguments...:
> ;;    #<logic-var>
> ;;   context...:
> ;;   /Applications/Racket/collects/racket/private/misc.rkt:87:7
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

Posted on the users mailing list.