[racket] snips in redex traces

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Tue May 7 10:22:58 EDT 2013

I've added a sentence to the docs pointing out the use of write-special and
a silly example at the end too.

I tried to make a silly example using 'plot', something like this, but it
isn't that great. If others have suggestions, I think it would be nice to
show plot here ...

#lang racket
(require redex 2htdp/image
         (only-in plot plot
                  function
                  parametric))

(define/contract (unpair z)
  (-> exact-nonnegative-integer?
      (list/c exact-nonnegative-integer? exact-nonnegative-integer?))
  (define i (integer-sqrt z))
  (define i2 (* i i))
  (cond
    [(< (- z i2) i)
     (list (- z i2) i)]
    [else
     (list i (- z i2 i))]))

(define/contract (pair x y)
  (-> exact-nonnegative-integer? exact-nonnegative-integer?
      exact-nonnegative-integer?)
  (if (= x (max x y))
      (+ (* x x) x y)
      (+ (* y y) x)))

(define-language L (n ::= natural))
(define red
  (reduction-relation
   L
   (--> (n_1 n_2)
        ,(unpair (+ 1 (pair (term n_1)
                            (term n_2)))))))
(traces red (term (0 0)))

(define (plot-as-poly term)
  (plot
   (function (λ (x) (+ (expt x (list-ref term 0))
                       (expt x (list-ref term 1)))))
   #:x-min -10
   #:x-max 10
   #:y-min -100
   #:y-max 100))

(define (plot-parametric-thingy term)
  (plot (parametric (λ (t) (vector (expt (cos t) (+ (list-ref term 0) 1))
                                   (expt (sin t) (+ (list-ref term 0) 1))))
                    0 (* 2 pi))
        #:x-min -2 #:x-max 2
        #:y-min -2 #:y-max 2))


(traces red
        (term (0 0))
        #:pp
        (λ (term port w txt)
          (write-special
           (plot-as-poly term)
           port)))

(traces red
        (term (0 0))
        #:pp
        (λ (term port w txt)
          (write-special
           (plot-parametric-thingy term)
           port)))


Robby

On Tuesday, May 7, 2013, Robby Findler wrote:

> Yes, sure:
>
> #lang racket
> (require redex 2htdp/image)
> (define-language L
>   (t ::= RED  YELLOW GREEN))
>
> (define RED    (circle 14 "solid" "red"))
> (define YELLOW (circle 14 "solid" "yellow"))
> (define GREEN  (circle 14 "solid" "green"))
>
> (define R
>   (reduction-relation
>    L
>    (--> RED GREEN)
>    (--> YELLOW RED)
>    (--> GREEN YELLOW)))
>
> (traces R (term RED)
>         #:pp
>         (λ (term port w txt)
>           (write-special
>            (case term
>              [(RED) RED]
>              [(GREEN) GREEN]
>              [(YELLOW) YELLOW])
>            port)))
>
>
>
> On Tue, May 7, 2013 at 12:25 AM, David Van Horn <dvanhorn at ccs.neu.edu<javascript:_e({}, 'cvml', 'dvanhorn at ccs.neu.edu');>
> > wrote:
>
>> Is it possible to render snips in redex traces?
>>
>> For example, here's something I'd like to write and have it result in a
>> pretty traces window:
>>
>> #lang racket
>> ;; Traffic lights as redex model
>> (require redex 2htdp/image)
>> (define-language L)
>>
>> (define RED    (circle 14 "solid" "red"))
>> (define YELLOW (circle 14 "solid" "yellow"))
>> (define GREEN  (circle 14 "solid" "green"))
>>
>> (define R
>>   (reduction-relation
>>    L
>>    (--> (side-condition any_l (equal? (term any_l) RED))    ,GREEN)
>>    (--> (side-condition any_l (equal? (term any_l) YELLOW)) ,RED)
>>    (--> (side-condition any_l (equal? (term any_l) GREEN))  ,YELLOW)))
>>
>> (traces R (term ,RED))
>>
>> I'd also be happy if I could write a Term -> Snip function to supply as a
>> pretty printer.
>>
>> David
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/**users<http://lists.racket-lang.org/users>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130507/47f52e74/attachment-0001.html>

Posted on the users mailing list.