[plt-scheme] Playing with signed units --- better docs?

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Sat Mar 5 02:24:42 EST 2005

Hi everyone,

I've been trying to go through the documentation in:

    http://download.plt-scheme.org/doc/209/html/mzlib/mzlib-Z-H-41.html

One part that is bothering me is that the documentation doesn't contain a
single complete toy example of how one actually uses a signed unit.  The
example that's given, a physics simulation, is so clean and abstract that
the whole thing mostly consists of '...' ellipses, and not one example of
a real call to invoke-unit/sig.

This is in contrast to the chapter right before it, on unsigned units,
which, thankfully, does contain some working examples to make the usage
and syntax concrete.  It just seems weird that the documentation on
unitsig.ss leaves a lot to the imagination, just at the point when the
system that it's describing is quite verbose and abstract.

I'm wondering if the signed unit documentation can be improved and fleshed
out with more concrete examples, since it seems that a lot of the
subsystems in PLT Scheme do use units extensively.  Some of the functions
really don't seem obvious without usage, especially because the unit
syntax does feel more heavier than the other subsystems in PLT Scheme.

I guess I'm asking for a nice signed unit tutorial that's separate from
the reference documentation, and one that doesn't already assume that the
reader knows how to use signed units already.  *grin*


I couldn't find one yet from Google, so I've been trying to work out some
examples myself.  One is a simple 'pair' example, so maybe this might help
other people who are trying to use units:

;;;;;;
(require (lib "unitsig.ss"))

(define-signature pair^
  (cons car cdr))

(define mypair1@
  (unit/sig pair^ (import)
    (rename
     (mycons cons)
     (mycar car)
     (mycdr cdr))

    (define (mycons x y)
      (display "mypair1@")
      (newline)
      (cons x y))

    (define mycar car)
    (define mycdr cdr)))


(define mypair2@
  (unit/sig pair^ (import)
    (define (cons x y)
      (lambda (b)
        (display "mypair2@")
        (newline)
        (if b x y)))
    (define (car pair) (pair #t))
    (define (cdr pair) (pair #f))))
;;;;;;



I had some initial difficulties trying to actually test this
implementation out, but finally hammered out two ways of invoking the
unit, one by yanking out the definitions out of a unit with
define-values/invoke-unit/sig, and another by compounding units together:

;;;;;;
(define (test-pair-implementation1 pair-impl@)
  (define-values/invoke-unit/sig pair^ pair-impl@)
  (display (car (cons 1 2)))
  (newline))


(define (test-pair-implementation2 impl@)
  (define test@
    (unit/sig ()
        (import pair^)
      (display (car (cons 1 2)))
      (newline)))

  (invoke-unit/sig
   (compound-unit/sig
     (import)
     (link (P : pair^ (impl@))
           (T : () (test@ P)))
     (export))))
;;;;;;



I hope this helps!



Posted on the users mailing list.