[plt-scheme] How to make unit functors?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue May 27 11:24:16 EDT 2003

At Sun, 25 May 2003 01:44:49 +0200, Jens_Axel_Søgaard wrote:
> ; Here is my attempt to define the functor, but as you probably have
> ; guessed it doesn't work:
> 
> ;(define (simple-set->set simple@)
> ;  (compound-unit/sig
> ;    (import (list : list^))
> ;    (link   (LIST   : list^        (list))
> ;            (SIMPLE : simple-set^  (simple@ list)))
> ;
> ;    (define empty   simple:empty)
> ;    (define member? simple:member?)
> ;    (define insert1 simple:insert1)
> ;    (define (insert s . xs)
> ;      (foldl simple:insert1 s xs))))

I think the only problem here is that `compound-unit/sig' doesn't both
link and define unit bodies. It just links. To have new definitions,
you need a new unit:

(define inserter@
 (unit/sig set^
   (import (simple : simple-set^))
   (define empty   simple:empty)
   (define member? simple:member?)
   (define insert1 simple:insert1)
   (define (insert s . xs)
     (foldl simple:insert1 s xs))))

(define (simple-set->set simple@)
  (compound-unit/sig
    (import (list : list^))
    (link   (SIMPLE : simple-set^  (simple@ LIST))
            (INSERTER : set^       (inserter@ SIMPLE)))
    (export (open INSERTER))))

Of course, the unit need not have a separate definition:

(define (simple-set->set simple@)
  (compound-unit/sig
    (import (list : list^))
    (link   (SIMPLE : simple-set^  (simple@ LIST))
            (INSERTER : set^       ((unit/sig set^
                                      (import (simple : simple-set^))
                                      (define empty   simple:empty)
                                      (define member? simple:member?)
                                      (define insert1 simple:insert1)
                                      (define (insert s . xs)
                                        (foldl simple:insert1 s xs)))
                                    SIMPLE)))
    (export (open INSERTER))))

Does that accomplish your goal?

Matthew




Posted on the users mailing list.