01 Dec 2009

PLT Scheme v4.2.3

posted by Eli Barzilay

PLT Scheme version 4.2.3 is now available from http://plt-scheme.org/

  • The unit test framework for the teaching languages provides check-member-of and check-range for checking “random functions”, i.e., “functions” that may produce several different results for one and the same argument.

  • Added a new image library, 2htdp/image. Significant changes from htdp/image:

  • copying and pasting does not introduce jaggies

  • equal? comparisons are more efficient

  • added rotation & scaling

  • got rid of pinholes (new overlay, beside, above functions based on bounding boxes)

  • The scheme/vector library provides common vector operations (also reprovided by scheme).

  • The scheme/promise library provides several new kinds of promises with alternatives execution strategies.

  • New port-reading utilities: in-port, port->list, file->list.

  • A new require-macro, path-up, for requiring a file that is higher in the directory tree.

more →

04 Oct 2009

PLT Scheme v4.2.2

posted by Eli Barzilay

PLT Scheme version 4.2.2 is now available from http://plt-scheme.org/

  • DrScheme now, by default, compiles all of the files that are loaded when it runs a program and saves the compiled files in the filesystem. This should lead to faster load times (not faster runtimes) since it avoids re-compiling files whose dependencies have not changed.

  • New Scribble libraries and documentation make it easier to get started with Scribble, especially for uses other than PLT documentation. DrScheme now has better indentation and syntax coloring support for Scribble languages (and generally all @-exp based languages).

  • The new syntax/keyword library provides support for macros with keyword options. A new quick start guide has been added to the documentation for the syntax/parse library.

  • Added support for abstract contracts via the #:exists keywords. This is an experiment to add support for data hiding to the contract system.

  • Added in-producer: a sequence expression makes it easy to iterate over producer functions (e.g., read). A new scheme/generator library creates generators that can use a (parameterized) yield function.

  • HtDP langs: several primitives now consume 0 and 1 arguments in ISL (and up), including append, + and *. In addition, make-list was added to the primitives.

  • The API to Universe has a number of new constructs. All Universe programs should run unchanged. The most important change is the addition of animate as an alternative name for run-simulation. In addition, adding the clause (state true) to a world description now pretty-prints the state of the world into a separate canvas.

  • A number of changes were made to the DeinProgramm / DMdA language levels: The check-property and contract forms were added, define-record-procedures-parametric has changed. See the documentation for details.

  • The test engine in the HtDP languages no longer warns programmers when the Definitions window has no tests.

  • ProfessorJ (and related code) is no longer included in the PLT distributions. It may re-appear in the future as a PLaneT package.

more →

21 Sep 2009

set! vs. set-box! and unbox

posted by Robby Findler

A few weeks ago I was chatting with some PLT folks and was surprised to hear them say that they avoided set! because using set-box! and unbox was easier to see what was going on.

This struck me as wrong since one might pass boxes around and then you can’t be sure which box you’re mutating, but you cannot pass variable references around and thus which variable you’re using is always lexically apparent. (Of course, when you add lambda into the mix that isn’t really true, since you can capture a variable in a closure and pass that around.)

Their point seemed to be that you had to write something special at each use of the box, unlike with set! where you simply write a variable reference and it might be getting a changing quantity and it might not be. This made me realize I could do something to help, at least, and so I changed Check Syntax so that it colored set!’d variables in red, like this:

more →

30 Jul 2009

PLT Scheme v4.2.1

posted by Eli Barzilay

PLT Scheme version 4.2.1 is now available from http://plt-scheme.org/

  • This is the last release that includes ProfessorJ. As of the next release, Kathy Gray who created and maintained the Professor will move the code to planet and maintain only at a minimal level.
  • Typed Scheme 2.0 extends the type system significantly, making it more expressive. For example, predicates applied to selectors, such as (number? (car x)), are meaningful to the type system.
  • Faster installation of Planet packages that trigger install of other Planet packages, because the documentation index is updated only once after a group of packages is installed.
  • The syntax/parse library provides macro writers with an enhanced syntax pattern matcher that reports errors based on the patterns’ declared classes of syntax.
  • Identifier mappings following the v4 dictionary interface and naming conventions are available from the syntax/id-table library.
  • Redex: added define-relation and generalized patterns that appear in “where” clauses to use the full Redex pattern matcher. (This is a backwards incompatible change, but one often requested; see the Redex release notes for details.)
  • The Web Server’s serializable closures are now available for other purposes through the web-server/lang/serial-lambda library.
  • Teachpacks: small changes to universe portion of the “universe.ss” API, plus the addition of a form for launching many (communicating) worlds simultaneously. Bug fixes concerning conversion to strings.
  • It is now possible to create custom scribble readers with a command characters different than @, see make-at-reader/inside and make-at-reader
  • Note: this is likely to be the last release that includes a solaris distribution. If you need these builds, or if you have access to a (Sparc) Solaris machine than can be used in PLT builds, then please let me know.
more →

23 Jun 2009

Serializable Closures in PLT Scheme

posted by Jay McCarthy

PLT Scheme supports an extensible serialization system for structures. A structure is serializable if it has a prop:serializable property. There are many properties in PLT Scheme for other extensions, such as applicable structures and custom equality predicates.

The PLT Web application development framework uses these features to provide serializable continuations through a number of source transformations and a serializable closure structure.

Warning: This remainder post refers to features only available in the latest SVN revision of PLT Scheme.

I’ve recently made these closures more accessible to non-Web programs through web-server/lang/serial-lambda. Here’s a demo:

#lang scheme
(require web-server/lang/serial-lambda

(define f
  (let ([z 5])
     (x y)
     (+ x y z))))

(define (test-it)
  (printf "~S~n" (f 1 2))
  (let ([fs (serialize f)])
    (printf "~S~n" fs)
    (let ([df (deserialize fs)])
      (printf "~S~n" df)
      (printf "~S~n" (df 1 2)))))

> (test-it)
((2) 1 ((#"/Users/jay/Dev/svn/plt/collects/web-server/exp/test-serial.ss" . "lifted.6")) 0 () () (0 5))
#(struct:7a410aca70b31e88b4c2f0fe77fa7ffe:0 #)

Now, let’s see how it is implemented. web-server/lang/serial-lambda is thin wrapper around web-server/lang/closure, which has two syntax transformer functions: define-closure! which defines the closure structure and make-closure which instantiates the closure. (The two tasks are separated to easily provide a user top-level definition syntax for named closures with different free identifires, rather than simply anonymous lambdas with fixed free identifiers.)

make-closure does the following:

  1. Expands the procedure syntax using local-expand, so it can use free-vars to compute the free identifires.

  2. Uses define-closure! to define the structure and get the name for the constructor.

  3. Instantiates the closure with the current values of the free identifiers.

The more interesting work is done by define-closure!. At a high-level, it needs to do the following:

  1. Create a deserialization function.

  2. Create a serialization function that references the deserializer.

  3. Define the closure structure type that references the serializer.

  4. Provide the deserializer from the current module so that arbitrary code can deserialize instances of this closure type.

These tasks are complicated in a few ways:

  • The deserializer needs the closure structure type definition to create instances and the serializer needs the closure structure type to access their fields.

  • The serializer needs the syntactic identifier of the deserializer so that scheme/serialize can dynamic-require it during deserialization.

  • The deserializer must be defined at the top-level, so it may be provided.

  • All this may occur in a syntactic expression context.

Thankfully, the PLT Scheme macro system is powerful to support all this.

The only complicated piece is allowing the deserializer and serializer to refer to the closure structure constructor and accessors. This is easily accomplished by first defining lifting boxes that will hold these values and initializing them when the structure type is defined. This is safe because all accesses to the boxes are under lambdas that are guaranteed not to be run before the structure type is defined.

An aside on the closure representation. The closure is represented as a structure with one field: the environment. The environment is represented as a thunk that returns n values, one for each of the free identifiers. This ensures that references that were under lambdas in the original syntax, remain under lambdas in the closure construction, so the serializable closures work correctly inside letrec. This thunk is applied by the serializer and the free values are stored in a vector. The closure also uses the prop:procedure structure property to provide an application function that simply invokes the environment thunk and binds its names, then applys the original procedure syntax to the arguments.

An aside on the serializer. The deserializer is bound to lifted identifier which is represented in PLT Scheme as an unreadable symbol. Version added support for (de)serializing these.

more →

01 Jun 2009

PLT Scheme v4.2

posted by Eli Barzilay

PLT Scheme version 4.2 is now available from http://plt-scheme.org/ Internally, this version includes a conversion from C++ to Scheme for part of the GUI toolbox — specifically, 25k lines of code that implement the general text and pasteboard editor. This conversion is a start on a larger reimplementation of the GUI toolbox. Although we believe that this change will help make PLT Scheme better in the long run, we must expect bugs in the short term due to porting errors. Users should therefore be aware of the change, even though the new implementation is meant to behave the same as previous versions.

  • A new statistical profiler is now available; see the “profiler” manual for more information. Currently, the profiler supports only textual output, but future plans include a GUI interface and DrScheme integration.

  • The world teachpack is now deprecated. Its replacement universe has a new interface that uses strings instead of symbols and characters.

  • Web-server: Native continuations in the stateless servlet language support capturing continuations from untransformed contexts; soft state library for stateless servlets.

  • DrScheme’s Stepper can now jump to a selected program expression.

  • New in scheme/base: hash-has-key?, hash-ref!, in-sequences, in-cycle. New in scheme: count, make-list (from scheme/list), const (from scheme/function).

  • Some performance improvements, including faster start-up for small programs. The latter is a result of delaying module invocations at higher phases (compile time, meta-compile time, etc.) until compilation is demanded at the next lower phase; this on-demand instantiation is per-phase, as opposed to per-module within a phase.

[Note that mirror sites can take a while to catch up with the new downloads.]

Feedback Welcome.

more →

25 May 2009

Typed Scheme 2.0

posted by Sam Tobin-Hochstadt

Typed Scheme version 2.0 is now available from SVN.

One persistent limitation of Typed Scheme has been that while this expression works as expected:

(if (number? x) (add1 x) 7)

The simple transformation of making x a part of a structure breaks Typed Scheme’s ability to reason about the code. So this expression doesn’t typecheck:

(if (number? (car x)) (add1 (car x)) 7)

With the newest version of Typed Scheme, now available in SVN, both of these will now work. In general, Typed Scheme can now follow paths into arbitrary immutable structures, including pairs.

This is part of a more general reworking of underlying mechanisms of the Typed Scheme typechecker, which makes it both simpler and more flexible. I hope that it will be possible, sing this new foundation to add additional features that make more programs easy to express in Typed Scheme.

Of course, these changes mean that Typed Scheme may be more unstable, so if you notice any new bugs, please let us know.

Unfortunately, this won’t be available in the upcoming 4.2 release, but it will be in the release after that.

If you have any questions or comments or feature requests for Typed Scheme, please let us know.

more →

24 May 2009

Explicit Renaming Macros; Implicitly

posted by Eli Barzilay

It’s been one too many times that I hear respectable Schemers talk about how they like explicit renaming macros — not because they’re more powerful, but because using them is close to using simple defmacros. In this post I’ll show how you can easily write ER-like macros in PLT, just so I won’t need to explain the same thing once again.


  • If you’re not interested in ER-macros, then you shouldn’t read this.

  • I’m not claiming that ER macros are not respectable, I’m just surprised at the knee jerk reaction to syntax-case.

  • This is not an attempt at providing some portable library or even a PLT library. The intention is to show that syntax-case-style macros are “as convenient” as ER macros, if you really want to get down to that level.

  • This is also not an attempt at any kind of formal claim of equivalence in any direction, only a demonstration that you can get the same kind of convenience.

  • The bottom line here should be just the convenience point, addressed at people who like ER macros for that, and who think that syntax-case macros are somehow magical or that you lose the ability to play with S-expressions.

The important fact here is that while PLT’s syntax-case macro system does not give you raw S-expressions, what you get is a simple wrapper holding them. A macro is a syntax transformer: a function that consumes a syntax value and returns one. For example:

(define-syntax (foo stx)

is a macro that always expands to 123 (with #'123 being the usual shorthand for (syntax 123)).

A syntax object in PLT Scheme (the input to macro functions) is an S-expression, with some lexical information added — this includes the lexical context (in an opaque form), source location, and a few more things. To be more precise, a syntax value is a nested structure of wrappers holding lists and pairs, holding more wrappers, with identifiers at the leaves, where an identifier is a wrapper holding a symbol. It’s easy to strip off all wrappers using syntax->datum if you like to work with S-expressions, but you don’t want to strip it off of identifiers, since that will lose the important gravy. (In fact, the defmacro library works by stripping off all information, even from identifiers, then reconstructing it by trying to match names in the output form with the original input.)

Instead of throwing away all information, what we want to do is preserve identifiers. We can use syntax->list for this: this is a function that takes a syntax value that contains a list, and strips off the top-level extra information leaving you with a list of syntaxes for the sub-expressions (returning #f if the input syntax does not hold a list). Once we have such a list, we can do the usual kind of processing with it, and when we’re done turn the result back into a syntax using datum->syntax (which “borrows” the original input expression’s information). For example,

(define-syntax (add1 stx)
    (let ([+ #'+])
      (datum->syntax stx `(,+ 1 ,@(cdr (syntax->list stx))) stx)))

That’s a very simple example though; if you try something a little more complicated, you quickly find out that all this unwrapping is inconvenient:

(define-syntax (mylet stx)
    (let ([*lambda #'lambda])
       `((,*lambda ,(map (lambda (x) (car (syntax->list x)))
                         (syntax->list (cadr (syntax->list stx))))
                   ,@(cddr (syntax->list stx)))
         ,@(map (lambda (x) (cadr (syntax->list x)))
                (syntax->list (cadr (syntax->list stx)))))

(Note also the *lambda that is used to avoid the lambda expressions used in the meta-code.)

What can help here is some helper function that receive a syntax value as its input, and turn all wrapped lists into real lists recursively, but will leave identifiers intact:

    (define (strip stx)
      (let ([maybe-list (syntax->list stx)])
        ;; syntax->list returns #f if the syntax is not a list
        (if maybe-list (map strip maybe-list) stx))))

But as long as we’re writing a syntax utility, we can make it do a litte more work: feed the resulting tree to your transformer, grab its result, and do the necessary datum->syntax voodoo on it:

    (define (er-like-transformer transformer)
      (define (strip stx)
        (let ([maybe-list (syntax->list stx)])
          ;; syntax->list returns #f if the syntax is not a list
          (if maybe-list (map strip maybe-list) stx)))
      (lambda (stx)
        (datum->syntax stx (transformer (strip stx)) stx))))

With this utility defined, the above macro becomes much easier to deal with:

(define-syntax mylet
     (lambda (exp)
       (let ((vars  (map car (cadr exp)))
             (inits (map cadr (cadr exp)))
             (body  (cddr exp)))
         `((,(syntax lambda) ,vars ,@body)

 ...and this is almost identical to the explicit renaming version of the macro; for example, compare it with the sample code in the [MIT-Scheme manual](http://groups.csail.mit.edu/mac/projects/scheme/documentation/scheme_3.html#SEC49).  The only change is that `(rename 'lambda)` is replaced with `(syntax lambda)`.

Obviously, this is very close, but doesn't show intentional captures.  So I just grabbed the `loop` example from the same page, and did the same change  only this time I used `#'foo` instead of `(syntax foo)` (and I also changed the one-sided `if` to a `when`).  The resulting macro works fine:  

(define-syntax loop
     (lambda (x)
       (let ((body (cdr x)))
           (,#'lambda (exit)
            (,#'let ,#'f () ,@body (,#'f))))))))

  (define-syntax while
    (syntax-rules ()
      ((while test body ...)
       (loop (when (not test) (exit #f))
             body ...))))

  (let ((x 10))
    (while (> x 0)
      (printf "~s\n" x)
      (set! x (- x 1))))

This is pretty close to a library, and indeed, as I was writing this text I found a post by Andre van Tonder on the Larceny mailing list that uses a very similar approach and does make a library out of it. This is done by adding two arguments to the expected ER-transformation function — one is a rename function (since the above method uses syntax it is limited to immediate identifiers), and the other is always passed as free-identifier=?. Such a compatibility library is, however, not the purpose of this post.

Finally, there is still a minor issue with this — PLT has an implicit #%app which is used wherever there are parentheses that stand for a function application — and in this code they are used unhygienically. This is usually not a noticeable problem, and if it is, you can add explicit #%apps. It might also be possible to find a more proper solution (e.g., use a hash table to keep track of lists that were disassembled by the client transformer), but at this point it might be better to just use the more natural syntax-case anyway.

more →

18 May 2009

The Two State Solution: Native and Serializable Continuations in the PLT Web Server

posted by Jay McCarthy

One of the annoyance of the stateless Web application language that comes with the PLT Web Server is that you can’t call third-party higher-order library procedures with arguments that try to capture serializable continuations. (I know, you try to do that all the time.) For example:

 (lambda (i)
    (lambda (k) (serialize k)))))

The problem is that the stateless language performs a transformation on your program to extract the continuations into a serializable representation. If you really need to do this, we’ve developed a compromise called “The Two State Solution”: one state on the client and the other on the server. Only the third-party parts of the continuation (in this case, the code inside build-list) are stored on the server; everything else is shipped to the client. You just need to annotate your code slightly to indicate where the transition is:

  (lambda (i)
      (lambda (k) (serialize k)))))))

serial->native signals the transition to the third-party and native->serial signals the transition back.

It is still a little annoying to find when you’ve called these third-party higher-order library procedures with arguments that try to capture serializable continuations, so there’s a simple macro that provides a transitioning wrapper for you:

(define-native (build-list/native _ ho) build-list)

expands to:

(define (build-list/native fst snd)
    (lambda args
       (apply snd args))))))

This new feature is documented in the online manual, of course.

more →

18 May 2009

Soft State in the PLT Web Server

posted by Jay McCarthy

Many Web applications and network protocols have values in the continuation that are necessary to complete the computation, but that may be recomputed if they are not available. This is “soft state”.

For example, a Web application may cache a user’s preferences from a database and deliver it to a Web browser as a hidden value; when the value is returned to the application in subsequent steps, it is used to customize the display. However, if the preferences were not available (or were corrupted in some way), the application could retrieve them from the database.

When using the PLT Web Server’s native continuations, this roughly corresponds to the use of a weak box: a box that the GC is allowed to erase the contents of. When using the PLT Web Server’s serializable continuations it roughly corresponds to a weak box and a weak hash table (that holds its keys weakly) to give the box a serializable value as an identifier.

This programming pattern is a bit difficult to get right. So, a library that implements it is now provided with PLT Scheme: web-server/lang/soft.

Here’s a trivial example:

#lang web-server

(provide interface-version start)
(define interface-version 'stateless)

(define softie
   (printf "Doing a long computation...~n")
   (sleep 1)))

(define (start req)
  (soft-state-ref softie)
  (printf "Done~n")
    (lambda (k-url)
      `(html (body (a ([href ,k-url]) "Done")))))))
more →

Made with Frog, a static-blog generator written in Racket.
Source code for this blog.