[racket] image equality and evaluators

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Feb 9 22:12:03 EST 2012

20 minutes ago, Jordan Johnson wrote:
> But the REPL renders (ev '(rectangle 2 2 'solid 'blue))) as a
> picture of a tiny rectangle, as I would expect.
> 
> How is the above exposing the difference between image=?'s
> perception of an image, and the object implementing the image?

My guess is that the repl considers them as different types but both
know how to render themselves graphically.  So probably this:

  (define (f x y) (list x y (equal? x y)))
  (f (rectangle 2 2 'solid 'blue) (ev '(rectangle 2 2 'solid 'blue)))

returns a list with two identical images and #f.


> And, more importantly, is there a way to make (meaningful) image
> comparisons across the boundary imposed by the sandbox?

The easy way to do that is to keep the tests inside the sandbox.  For
example, replace

  (equal? (ev '(f1)) (f2))

with

  (equal? (ev '(f1)) (ev '(f2)))

or even better:

  (ev '(equal? (f1) (f2)))

The possible problem with this is that a student might define their
own functions (but I think that that doesn't apply to the student
languages).

The other way is what gets you what you want: you need to use
`sandbox-namespace-specs' to make the sandbox share an instance of the
necessary modules between your own code and the sandbox, so that
structs in the sandbox are the same type as your own.  Here's a quick
example:

  #lang racket
  (require racket/sandbox 2htdp/image)
  (define ev
    (parameterize ([sandbox-namespace-specs
                    (list make-base-namespace '2htdp/image)])
      (make-evaluator 'racket #:requires '(2htdp/image))))
  (image? (ev '(rectangle 2 2 'solid 'blue)))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


Posted on the users mailing list.