Moby: the Moby Scheme Compiler
1 What is Moby?
Moby is a project from the PLT Scheme team. The Moby compiler consumes Advanced Student Language (ASL) programs that use World primitives, and produces applications for mobile platforms. The current prototype supports desktop browsers and smartphones. Our long-term goal is to make Scheme the premiere reactive scripting language for mobile phones.
Shriram Krishnamurthi presented the ideas behind Moby at ILC 2009 in his talk The Moby Scheme Compiler for Smartphones.
2 Running Moby from DrScheme
To use Moby from DrScheme, create a file in the Module language, and at the top of your program, include the following language line:
followed by the program. For example, running the program:
#lang planet dyoo/moby:2:12 |
(define initial-world 0) |
(js-big-bang initial-world (on-tick 1 add1)) |
will invoke a web browser, which should show the running program on a web page. Because on-tick is used, as every second passes, the runtime sends a tick stimulus to the program. The page should also provide links to download packages of the compiled program.
These programs run on the user’s web browser; they can also dynamically generate DOM trees and style them with CSS, as in the examples below.
The following example renders the world as a paragraph of text, styled with a font-size of 30. It uses draw-html and draw-css to draw the web page.
#lang planet dyoo/moby:2:12 |
(define initial-world 0) |
(define (draw-html w) |
(list (js-p '(("id" "myPara"))) |
(list (js-text "hello world")))) |
(define (draw-css w) |
'(("myPara" ("font-size" "30")))) |
(js-big-bang initial-world |
(on-draw draw-html draw-css)) |
The next example shows an image and an input text field. As with the previous example, it uses draw-html and draw-css to construct the web page, and every time the world changes, the runtime environment reacts by re-drawing the web page.
#lang planet dyoo/moby:2:12 |
(define (form-value w) |
(format "~a" w)) |
(define (update-form-value w v) |
(string->number v)) |
(define elt |
(js-input "text" update-form-value)) |
(define (draw-html w) |
(list (js-div) |
(list (js-img "http://plt-scheme.org/logo.png")) |
(list elt) |
(list (js-p '(("id" "aPara"))) |
(list (js-text (format "~a" w)))))) |
(define (draw-css w) |
'(("aPara" ("font-size" "50px")))) |
(js-big-bang 0 |
(on-draw draw-html draw-css)) |
3 The Moby World API
(js-big-bang a-world handlers ...) → void |
a-world : world |
handlers : handler? |
By default, the page that’s displayed contains a rendering of the world value. In the presence of an on-draw or on-redraw handler, js-big-bang will show a customized view.
The majority of the handlers register different stimuli that can trigger changes to the world. One instance is on-tick, which registers a function to update the world on a clock tick.
(on-draw to-dom to-css) → scene |
to-dom : (world -> (DOM-sexp)) |
to-css : (world -> (CSS-sexp)) |
(on-redraw hook) → handler? |
hook : (world -> scene) |
#lang planet dyoo/moby:2:12 |
(define WIDTH 320) |
(define HEIGHT 480) |
(define RADIUS 15) |
(define INITIAL-WORLD 0) |
(define (tick w) |
(+ w 5)) |
(define (hits-floor? w) |
(>= w HEIGHT)) |
(check-expect (hits-floor? 0) false) |
(check-expect (hits-floor? HEIGHT) true) |
(define (render w) |
(place-image (circle RADIUS "solid" "red") (/ WIDTH 2) w |
(empty-scene WIDTH HEIGHT))) |
(js-big-bang INITIAL-WORLD |
(on-tick 1/15 tick) |
(on-redraw render) |
(stop-when hits-floor?)) |
(stop-when stop?) → handler? |
stop? : (world -> boolean) |
#lang planet dyoo/moby:2:12 |
(define (at-ten x) |
(>= x 10)) |
(js-big-bang 0 |
(on-tick 1 add1) |
(stop-when at-ten)) |
3.1 Types
A dom-sexp describes the structure of a web page:
dom-sexp | = | (list dom-element dom-sexp ...) |
a css-sexp describes the structure of a page’s styling:
css-sexp | = |
|
attrib | = | (list string string) |
Each of the dom-elements can take in an optional attribute list to assign to the new dom element; the common useful attribute is a key-value binding for an "id", which can be used to identify an element in the css-drawing function.
(define a-dom-sexp (list (js-div '(("id" "main-div"))) |
(list (js-text "Hello world")))) |
(define a-css-sexp (list (list "main-div" |
(list "background" "white") |
(list "font-size" "40px")))) |
3.2 dom-element constructors
Here are the dom-element constructors.
(js-div [attribs]) → dom-element? |
attribs : (listof attrib?) = '() |
(js-p [attribs]) → dom-element? |
attribs : (listof attrib?) = '() |
(js-button world-update-f [attribs]) → dom-element |
world-update-f : (world -> world) |
attribs : (listof attrib) = '() |
(js-button! world-update-f effect-f [attribs]) → dom-element |
world-update-f : (world -> world) |
effect-f : (world -> effect) |
attribs : (listof attrib) = '() |
(js-text text) → dom-element |
text : string? |
(js-input type world-update-f [attribs]) → dom-element |
type : string |
world-update-f : (world string -> world) |
attribs : (listof attrib) = '() |
The example below has a single text input form element, which allows the user to enter some value. That value is read from the interface by the refresh function that’s associated to the button.
#lang planet dyoo/moby:2:12 |
(define input-node |
(js-input "text" '(("id" "myname")))) |
(define (refresh w) |
(get-input-value "myname")) |
(define (draw w) |
(list (js-div) |
(list (js-div) (list (js-text (format "I see: ~s~n" w)))) |
(list (js-div) (list input-node)) |
(list (js-div) (list (js-button refresh) |
(list (js-text "Update!")))))) |
(define (draw-css w) |
'()) |
(js-big-bang "" |
(on-draw draw draw-css)) |
(js-img url [attribs]) → dom-element |
url : string |
attribs : (listof attrib) = '() |
3.3 Stimulus Handlers
Stimulus handlers are provided as additional arguments to a js-big-bang.
Each stimulus has an effect-less and an effect-full version; the effect-full version allows you to provide an effect-generating function as well as a world-updater. When the given stimulus emits, the old world is used to compute both the new world and the optional effect. Afterwards, each effect in the effect group is applied.
effect | = | atomic-effect | ||
| | (listof effect) |
| ||||
|
| |||
|
| |||||||||||||||||
|
|
| |||
|
3.4 Effects
Effects allow world programs to apply side effects to the outside world. These are used in conjunction with the effect (!) version of the stimulus handlers described above.
(make-effect:none) → effect |
(make-effect:beep) → effect |
(make-effect:play-sound a-sound) → effect |
a-sound : sound |
(make-effect:stop-sound a-sound) → effect |
a-sound : sound |
(make-effect:pause-sound a-sound) → effect |
a-sound : sound |
sound | = | string | ||
| | playlist |
(make-effect:set-sound-volume volume) → effect |
volume : number |
(make-effect:raise-sound-volume) → effect |
(make-effect:lower-sound-volume) → effect |
(make-effect:play-dtmf-tone tone) → effect |
tone : number |
(make-effect:set-wake-lock flag) → effect |
flag : number |
(make-effect:release-wake-lock) → effect |
(make-effect:send-sms phone-number message) → effect |
phone-number : string |
message : string |
(make-effect:pick-playlist world-update-f) → effect |
world-update-f : (world playlist -> world) |
3.5 API Extensions
The following helper functions and forms are provided by Moby.
(include [a-path string?]) |
Note: this feature is experimental and will be replaced in the near future.
(get-url url) → string? |
url : string? |
(location-distance lat-1 long-1 lat-2 long-2) → number? |
lat-1 : number? |
long-1 : number? |
lat-2 : number? |
long-2 : number? |
(xml->s-exp xml-string) → s-expression? |
xml-string : string? |
(define-struct name (id ...)) |
make-foo: X Y -> foo
foo-a: foo -> X
foo-b: foo -> Y
foo?: any -> boolean
set-foo-a!: foo X -> void
set-foo-b!: foo Y -> void
(make-hasheq) → hash? |
(hash? x) → boolean? |
x : any/c |
(hash-set! a-hash key value) → void |
a-hash : hash? |
key : any/c |
value : any/c |
(hash-ref a-hash key value default-val) → any/c |
a-hash : hash? |
key : any/c |
value : any/c |
default-val : any/c |
If default-val is a thunk, calls it and returns its value.
Otherwise, returns default-val.
(hash-remove! a-hash key) → (void) |
a-hash : hash? |
key : any/c |
(hash-map a-hash f) → (listof any/c) |
a-hash : hash? |
f : (any/c any/c -> any/c) |
4 Developer details
The compiler takes a ASL program and translates it to Javascript code. Moby reimplements the ASL primitives in a Javascript runtime library that’s included with the compiled application. (See doc/moby-developer-api.txt for more details.)
To support smartphones, Moby uses a bridge library called Phonegap, which provides access to the native facilities of several cell phones. In this way, Moby should be able to support multiple platforms with a lot of code reuse. Moby handles the other libraries (tilt, location, sms, music), though with support only for the Android platforms for now.
4.1 Dependencies
Moby is mostly written in PLT Scheme, and the project sources are hosted by github.com. To develop with Moby, you will need the following:
4.2 Installing from Developer Sources
Download the Moby source, currently hosted on github and place them in your PLT Scheme collects directory.
For example,$ cd ~/.plt-scheme/4.2.1/collects
$ git clone git://github.com/dyoo/moby-scheme.git moby
downloads the developer sources into a PLT Scheme user collects directory.Also, do a setup-plt -l moby so that PLT Scheme compiles the Moby source code.
2. If you’re going to do Android development, make sure that ant and the android binary are in your path; Moby will use your PATH variable to find Apache Ant and the Android SDK.
You can verify that android and ant can be found with the following:$ which android
/usr/local/android/tools/android
$ which ant
/usr/bin/ant
The path values you get back may differ according to your system’s configuration.
4.3 Running Moby from the command line
js: compiles to a web page application, which can be deployed on any web server.
js+android-phonegap: compiles to an Android .apk application package; can also use features of the mobile platform.
By default, the command line utility will use the js backend.
$ cd moby/examples |
$ mred ../src/moby.ss falling-ball.ss |
$ cd FallingBall/ |
$ ls |
index.html main.js runtime test |
$ cd moby/examples |
$ mred ../src/moby.ss -t js+android-phonegap falling-ball.ss |
$ cd FallingBall |
$ ls |
AndroidManifest.xml build.properties gen res |
assets build.xml libs src |
bin default.properties local.properties tests |
|
$ ls bin |
classes classes.dex DroidGap.ap_ DroidGap-debug.apk |
$ ant install |
Buildfile: build.xml |
|
[some output cut] |
|
install: |
[echo] Installing bin/DroidGap-debug.apk onto default emulator... |
[exec] 1594 KB/s (120997 bytes in 0.074s) |
03:38 I/ddms: Created: [Debugger 8610-->1641 inactive] |
03:38 I/ddms: Good handshake from client, sending HELO to 1641 |
[exec] pkg: /data/local/tmp/DroidGap-debug.apk |
[exec] Success |
03:39 I/ddms: Closing [Client pid: 1641] |
|
BUILD SUCCESSFUL |
Total time: 6 seconds |
After this, you can look at the Android emulator, which should now have the "FallingBall" application installed.
4.4 Compiler
"src/compiler/beginner-to-javascript.ss": translates Scheme programs to javascript programs.
"src/compiler/env.ss": maintains the environment structures that map identifiers to bindings.
"src/compiler/permission.ss": defines a list of capabilities that a function can be tagged with.
"src/compiler/toplevel.ss": maps the primitive toplevel names available in the base language.
"src/compiler/modules.ss": adds a few extensions to the toplevel language, including the reactive world primitives.
"src/compiler/pinfo.ss": maintains program information used to analyze a program and figure out what functions are used and what capabilities are needed.
"src/compiler/desugar.ss": applies syntactic transformations on programs to a core form.
"src/compiler/helpers.ss": auxillary helper functions.
The compiler is intentionally written in a small superset of the language ("src/compiler/lang.ss"). As a consequence, it is self hosting, and we take advantage of this to produce a running compiler on the browser. ("support/js/test/test-repl.html")
"src/compiler/bootstrap-js-compiler.ss": compiles "beginner-to-javascript.ss" against itself to produce "support/js/compiler.js".
4.4.1 An example
Let’s see what beginner-to-javascript.ss gives us:
> (define p '((define (f x) |
(* x x)) |
|
(f 3))) |
|
> (define cp (program->compiled-program p)) |
program->compiled-program consumes a program – a list of s-expressions – and produces a compiled-program structure.
> cp |
#<compiled-program> |
The compiled program consists of a list of toplevel definitions and expressions.
> (compiled-program-defns cp) |
"\nfunction f(x) { return plt.Kernel._star_([x,x]); }" |
|
> (compiled-program-toplevel-exprs cp) |
> cp |
#<compiled-program> |
The compiled program consists of a list of toplevel definitions and expressions.
> (compiled-program-defns cp) |
"\nfunction f(x) { return plt.Kernel._star_([x,x]); }" |
|
> (compiled-program-toplevel-exprs cp) |
"(function (toplevel_dash_expression_dash_show0) { \n\ntoplevel_dash_expression_dash_show0((f((plt.types.Rational.makeInstance(3, 1))))); })" |
If we want to embed the evaluation of this program in a web page, we can use the two strings above to do so. For convenience, we provide a helper function compiled-program-main that ties both the definitions and expression evaluation together.
4.5 Runtime
The Javascript program that’s emitted depends on a runtime kernel that’s currently implemented in Javascript. See the files in "support/js/runtime".
5 Appendix
5.1 Bindings from ASL
The following toplevel bindings are available from Moby, and have the same meaning as in Advanced Student Language.
*
+
-
/
<
<=
=
=~
>
>=
abs
acos
add1
andmap
angle
append
asin
atan
boolean=?
boolean?
build-list
caaar
caadr
caar
cadar
cadddr
caddr
cadr
car
cdaar
cdadr
cdar
cddar
cdddr
cddr
cdr
ceiling
char->integer
char-alphabetic?
char-ci<=?
char-ci<?
char-ci=?
char-ci>=?
char-ci>?
char-downcase
char-lower-case?
char-numeric?
char-upcase
char-upper-case?
char-whitespace?
char<=?
char<?
char=?
char>=?
char>?
char?
check-expect
check-within
check-error
complex?
conjugate
cons
cons?
cos
cosh
current-seconds
denominator
e
eighth
empty
empty?
eof
eof-object?
eq?
equal?
equal~?
eqv?
error
even?
exact->inexact
exp
expt
false
false?
fifth
first
floor
foldl
format
fourth
gcd
identity
imag-part
inexact->exact
inexact?
integer->char
integer?
lcm
length
list
list?
list*
list->string
list-ref
log
magnitude
make-posn
make-string
map
max
member
memq
memv
min
modulo
negative?
not
null
null?
number->string
number?
numerator
odd?
pair?
pi
positive?
posn-x
posn-y
posn?
procedure?
quotient
random
rational?
real-part
real?
remainder
rest
reverse
round
second
seventh
sgn
sin
sinh
sixth
sqr
sqrt
string
string->list
string->number
string->symbol
string-append
string-ci<=?
string-ci<?
string-ci=?
string-ci>=?
string-ci>?
string-copy
string-length
string-ref
string<=?
string<?
string=?
string>=?
string>?
string?
struct?
sub1
substring
symbol->string
symbol=?
symbol?
tan
third
true
zero?
begin
set!
for-each
printf
let
let*
letrec
case
build-vector
make-vector
vector
vector-length
vector-ref
vector-set!
vector?
5.2 Unimplemented forms
begin0
delay
shared
recur
when
unless