Re: [racket-users] difference in struct printing from default #:transparent versus make-constructor-style-printer

2020-09-03 Thread Jeremy Siek
Thanks Ryan!

On Thursday, September 3, 2020 at 5:47:50 AM UTC-4 rmculp...@gmail.com 
wrote:

> The Racket printer uses a separate property to determine whether a struct 
> is quotable. If you add 
>
>   #:property prop:custom-print-quotable 'never
>
> to the declarations of Int2 and Prim2, your program should produce the 
> output you expect. I'll add a note to the docs for 
> make-constructor-style-printer.
>
> Ryan
>
>
> On Wed, Sep 2, 2020 at 10:07 PM Jeremy Siek  wrote:
>
>> I'm seeing some bad behavior from make-constructor-style-printer when
>> there are lists mixed in with the structs. It seems to switch from print 
>> mode to
>> write mode when going under a list. Whereas the default printer for 
>> transparent structs gets this right. The following program demonstrates the 
>> difference/problem.
>> Any thoughts on how to get the make-constructor-style-printer to behave
>> properly?
>>
>> #lang racket
>> (require racket/struct)
>>
>> (struct Int1 (value) #:transparent)
>> (struct Prim1 (op arg*) #:transparent)
>>
>> (struct Int2 (value) #:transparent
>>   #:methods gen:custom-write
>>   [(define write-proc
>>  (make-constructor-style-printer
>>   (lambda (obj) 'Int2)
>>   (lambda (obj) (list (Int2-value obj)])
>> (struct Prim2 (op arg*) #:transparent
>>   #:methods gen:custom-write
>>   [(define write-proc
>>  (make-constructor-style-printer
>>   (lambda (obj) 'Prim2)
>>   (lambda (obj) (list (Prim2-op obj) (Prim2-arg* obj)])
>>
>> (define p1 (Prim1 '+ (list (Int1 1) (Int1 2
>> (print p1)(newline)
>>
>> (define p2 (Prim2 '+ (list (Int2 1) (Int2 2
>> (print p2)(newline)
>>
>> The output is:
>>
>> (Prim1 '+ (list (Int1 1) (Int1 2)))
>> (Prim2 '+ '(# #))
>>
>> -Jeremy
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/bdeb1052-ea25-4dc0-8292-a13d15bf7870n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/racket-users/bdeb1052-ea25-4dc0-8292-a13d15bf7870n%40googlegroups.com?utm_medium=email_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/f2e7dee9-af4b-4337-92c2-2b5790441b73n%40googlegroups.com.


[racket-users] difference in struct printing from default #:transparent versus make-constructor-style-printer

2020-09-02 Thread Jeremy Siek
I'm seeing some bad behavior from make-constructor-style-printer when
there are lists mixed in with the structs. It seems to switch from print 
mode to
write mode when going under a list. Whereas the default printer for 
transparent structs gets this right. The following program demonstrates the 
difference/problem.
Any thoughts on how to get the make-constructor-style-printer to behave
properly?

#lang racket
(require racket/struct)

(struct Int1 (value) #:transparent)
(struct Prim1 (op arg*) #:transparent)

(struct Int2 (value) #:transparent
  #:methods gen:custom-write
  [(define write-proc
 (make-constructor-style-printer
  (lambda (obj) 'Int2)
  (lambda (obj) (list (Int2-value obj)])
(struct Prim2 (op arg*) #:transparent
  #:methods gen:custom-write
  [(define write-proc
 (make-constructor-style-printer
  (lambda (obj) 'Prim2)
  (lambda (obj) (list (Prim2-op obj) (Prim2-arg* obj)])

(define p1 (Prim1 '+ (list (Int1 1) (Int1 2
(print p1)(newline)

(define p2 (Prim2 '+ (list (Int2 1) (Int2 2
(print p2)(newline)

The output is:

(Prim1 '+ (list (Int1 1) (Int1 2)))
(Prim2 '+ '(# #))

-Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/bdeb1052-ea25-4dc0-8292-a13d15bf7870n%40googlegroups.com.


[racket-users] Re: Pretty Printing for ASTs represented as structs

2020-08-17 Thread Jeremy Siek
Thanks for the tip!

I found the following to work pretty well :)

#lang racket
(require racket/match)
(require racket/struct)

(struct Var (name)
   #:methods gen:custom-write
  [(define (write-proc ast port mode)
 (match ast
   [(Var x)
(write x port)]))])

(struct Num (value) 
   #:methods gen:custom-write
  [(define (write-proc ast port mode)
 (match ast
   [(Num n)
(write n port)]))])

(struct Let (var rhs body)
  #:methods gen:custom-write
  [(define write-proc
 (make-constructor-style-printer
  (lambda (obj) 'let)
  (lambda (obj) (list (unquoted-printing-string "([")
  (Let-var obj)
  (Let-rhs obj)
  (unquoted-printing-string "])\n")
  (Let-body obj)])
  
(define x (Var 'x))
(define y (Var 'y))
(define n (Num 42))
(define l (Let x n (Let y n x)))
(pretty-print l)

Produced the output:

(let ([ x 42 ])
 (let ([ y 42 ])
 x))


On Sunday, August 16, 2020 at 5:10:20 PM UTC-4 jackh...@gmail.com wrote:

> I recommend using make-constructor-style-printer 
> ,
>  
> which automates a lot of the fiddly indentation-handling you'd have to do 
> otherwise.
> On Sunday, August 16, 2020 at 1:48:50 PM UTC-7 jerem...@gmail.com wrote:
>
>>
>> Hi All,
>>
>> I'm looking into using Racket structs to represent abstract syntax trees.
>> I've gotten as far as defining gen:custom-write properties, but have not
>> yet figured out how to control indenting. Also, I'm not sure I'm just 
>> calling
>> write-string, write, and newline in my gen:custom-write methods, which I
>> suspect is not exactly correct.
>>
>> Are there some good up-to-date examples of this that I could look at?
>>
>> Thanks,
>> Jeremy
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/0bf543ed-3587-4fed-a527-49ebad106eafn%40googlegroups.com.


[racket-users] Pretty Printing for ASTs represented as structs

2020-08-16 Thread Jeremy Siek

Hi All,

I'm looking into using Racket structs to represent abstract syntax trees.
I've gotten as far as defining gen:custom-write properties, but have not
yet figured out how to control indenting. Also, I'm not sure I'm just 
calling
write-string, write, and newline in my gen:custom-write methods, which I
suspect is not exactly correct.

Are there some good up-to-date examples of this that I could look at?

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/550ad1e1-90af-4f6d-8f31-57fccfe2d25an%40googlegroups.com.