[plt-scheme] parser-tools

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Mon May 14 01:04:13 EDT 2007

Hi Sean,

By the way, I did a quick browse through the project you're working on. 
There are a few libraries in PLT Scheme that you can take a lot of 
advantage of.

For example, I see in:

http://www.bisonofborg.com:8080/svn/proof-src/trunk/scheme-src/object/object.scm

the beginnings of a class system.  PLT Scheme has a native class library 
that you can reuse.

For example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   (define (make-prf-type type-name)
     (if       (not (symbol? type-name))
                 (error "Argument must be a symbol"))
     (let      ((name type-name))
       (lambda (flag)
         (cond ((eq? flag 'name) name)
                (else (error "Flag not supported"))))))


; Int
   (define (make-prf-int val)
     (if       (not (integer? val))
                 (error "Argument must be an integer"))
     (let      ((type (make-prf-type 'Int))
                (value val))
       (lambda (flag)
         (cond ((eq? flag 'value) value)
               ((eq? flag 'type) type)
               (else "Flag not supported")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


can be written with (lib "class.ss") as:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module test-class mzscheme
   (require (lib "class.ss"))

   (define prf-object%
     (class object%
       (init type-name)
       (unless (symbol? type-name)
         (error 'prf-object% "type must be a symbol"))

       (define -type type-name)
       (define/public (type) -type)
       (super-new)))

   (define prf-int%
     (class prf-object%
       (init val)
       (unless (integer? val)
         (error 'prf-int% "val must be an integer"))

       (define -val val)
       (define/public (value) -val)
       (super-new (type-name 'Int)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Here's some example usage of these definitions:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (define my-int (new prf-int% [val 5]))
> (send my-int type)
Int
> (send my-int value)
5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


You can find out more about the class system here:

     http://people.cs.uchicago.edu/~robby/pubs/papers/aplas2006-fff.pdf
     http://schemecookbook.org/Cookbook/IntroductionToMzlibClasses


Posted on the users mailing list.