Embedding a Scheme Interpreter in the Tk Toolkit

Erick Gallesio

Universitede Nice SophiaAntipolis

Lab oratoire IS CNRS URA Bat

avenue Alb ert Einstein

Sophia Antipolis

Valbonne FRANCE

Abstract an exciting challenge To keep intact the TkTcl

pair spirit a little applicative language was nec

STk is a graphical package which rely on Tk and

essary Scheme was a go o d candidate to re

the Scheme Concretely

place Tcl b ecause it is small clean and well de

it can be seen as the Tk package where the Tcl

ned since it is an IEEE standard

language as been replaced by a Scheme inter

Programming with STk can b e done at two

preter Programming with STk can be done at

distinct levels First level is quite identical than

two distinct levels First level is quite identical

programming Tk with Tcl excepting several mi

than programming Tk with Tcl Second level of

nor syntactic dierences Second level of pro

programming uses a ful l object oriented system

gramming uses a full ob ject oriented system

Those two programming levels and current imple

with multiinheritance generic functions and a

mentation are described here

true meta ob ject proto col Those two levels of

programming are briey describ ed in the two rst

sections Section is devoted to implementation

Introduction

and section exp oses some encountered problems

when mixing Tk and Scheme

Todays available graphical to olkits for applica

tive languages are not satisfactory Most of the

time they ask to the user to b e an X exp ert which

STk First level

must cop e with complicated arcane details such

as server connections or queue events This is a The rst level of STk uses the standard Scheme

true problem since p eople which use this kind constructs To work at this level a user must

of languages are generally not inclined in system know a little set of rewriting rules from the orig

programming and little of them get over the gap

inal TkTcl With these rules the Tk

b etween the language and the to olkit abstraction

manual pages and a little knowledge of Scheme

levels

heshe can easily build a STk program

Creation of a new widget button lab el can

Tk is a p owerful X graphical to olkit dened

vas is done with sp ecial STk primitives pro

at the University of Berkeley by JOusterhout

cedures For instance creating a new button can

This to olkit gives to the user high level wid

gets such as buttons or menu and is easily pro b e done with

grammable In particular a little knowledge of

button b

X fundamentals are needed to build an applica

tion with it Tk package rely on an interpretative

Note that the name of the widget must b e

language named Tcl However dep endencies

stringied due to the Scheme evaluation mech

b etween those two packages are not to o intricate

anism The call of a widget creation primitive

and replacing Tcl by an applicative language was denes a new Scheme ob ject This ob ject which

To illustrate STk rst level of programming is considered as a new basic type by the Scheme

interpreter is automatically stored in a variable

Figure shows the simple le browser describ ed

whose name is equal to the string passed to the

in written in STk

creation function So the preceding button cre

ation would dene an ob ject stored in the b vari

able This ob ject is a sp ecial kind of pro cedure

STk Second level

which is generally used as in pure Tk to cus

tomize its asso ciated widget For instance the

Programming with material shown b efore is a lit

expression

tle bit tedious and more complicated than co d

b configure

ing with Tcl since one have to add parenthesis

text Hello world

pairs and quote options values Its only interest

border

is to bring the p ower and exibility of Tk to the

Scheme world

p ermits to set the text and background options

of the b button As we can see on this exam

The second level of STk is far more interest

ple parameters must b e well quoted in regard

ing since it uses a full ob ject oriented extension

of the Scheme evaluation rules Since this nota

of the Scheme language Dening an ob ject ori

tion is barely crude the keyword

ented layer on Scheme is a current activity in

mechanism has b een introduced in the Scheme

the Scheme community and several packages are

1

interpreter Consequently the preceding ex

available The ob ject layer of STk is derived

pression could have b een written as

from a package called Tiny Clos This exten

b configure sion provides ob jects ala CLOS Common Lisp

text Hello world

Ob ject System In fact the prop osed extension

border

is much closer from the ob jects one can nd in

Dylan since this language is already a tentative

which in turn is b oth close from Common Lisp

to merge CLOS notions in a Scheme like language

and pure Tk Of course as in Tk parameters can

b e passed at widget creation time and our button

STk ob ject extension gives to the user a full

creation and initialization could have b een done

in a single expression

ob ject oriented system with multiinheritance

and generic functions Furthermore all the im

button b

plementation rely on a true meta ob ject proto col

text Hello world

in the spirit of This mo del has b een used to

border

embo dy all the predened Tk widgets in a hier

The Tk binding mechanism which serves to

archy of Stk classes

create widget event handlers follow the same kind

of rules The b o dy of a Tk handler must b e writ

ten of course in Scheme Following example

Class hierarchy

shows such a script in this example the lab el

With the STk ob ject system every Tk graphical

indicates how many times mouse button has

b een depressed Button press counter increment

ob ject used in a program such as a menu a la

is achieved with the simple script given in the

b el or a button is represented as an ob ject in the

bind call

Scheme core All the dened STk classes build a

hierarchy which is briey describ ed here Firstly

define count

all the classes shared a unique ancestor the

pack append

Tkobject class Instances of this class con

label l

tain informations which are necessary to estab

textvariable count

lish a communication b etween the Scheme and

fill

bind l

Tk worlds Ob jects of this class have two main

set count count

slots named Id and parent The Id slot con

1

tains a string normally generated by the system

A keyword is a symbol b eginning with a colon It can

b een seen as a symbolic constant ie its value is itself which corresp ond to a unique variable name in

usrlocalbinstk f

scrollbar scroll command list yview

listbox list yscroll scroll set relief raised geometry x

pack append scroll right filly list left expand fill

define browse dir file

if not string dir set file stringappend dir file

if directory file

system format f browsestk A file

if file file

let ed getenv EDITOR

if ed

system stringappend ed file

system stringappend xedit file

error Bad directory or file file

define dir if argc car argv

system format f ls a A tmpbrowse dir

withinputfromfile tmpbrowse lambda

do f readline readline

eofobject f

list insert end f

bind list Controlc destroy

bind list DoubleButton browse dir selection get

Figure A simple le browser

STk hierarchy is shown in Figure the Tk world The parent slot contains a ref

erence to the ob ject which graphically includes

the current ob ject Normally end users will not

Basic notions

have to use direct instances of the Tkobject

2

This section describ es basic concepts of our ob

class

ject extension on a small example Denition of

The next level in our class hierarchy denes

a new ob ject class is done with the defclass form

a fork with two branches the Tkwidget

For instance

class and Tkcanvasitem class Instances of

defclass person

the former class are classical widgets such as

name initarg name

buttons or menus since instances of the later

accessor name

are ob jects contained in a canvas such as lines

age initarg age

or rectangles Tk widgets are also divided in

denes a p erson characteristics Two slots are

two categories Tksimplewidgets and Tk

declared name and age Characteristics of a slot

compositewidgets Simple widgets are directly

are expressed with its denition Here for in

implemented as Tk ob jects and comp osite ones

stance it is said that the slot name can b e inited

are built up on simple widgets eg le browser

with the keyword name up on instance creation

alert messages and so on A partial view of the

and that an accessor function should b e gener

2

ated for it Creation of a new instance is done

All classes whose name b egins with the Tk prex

with the make constructor are not intended for the nal user Tk-object

Tk-widget Tk-canvas-item

Tk-simple-widget

Frame Label Message Rectangle Line

Button

Tk-complex-button

Check-button Radio-button

Figure A partial view of STk hierarchy

defclass Button Label define p make person

command accessor command name Smith

initarg command age

allocation pseudo

This call p ermits to build a new p erson and to

type any

initialize the slots which comp ose himher

metaclass Tk

Reading the value of a slot can b e done with

the function slotvalue For instance

This denition says that a Button is a in

herits from Label with an extra slot called

slotvalue p age

command This slots allo cation scheme is said to

3

p ermits to get the value of slot age of the p ob

b e pseudo Pseudoslots are sp ecial purp ose

ject Setting this slot can b e done by using the

slots they can b e used as normal slots but they

generalized set dened in STk

are not allo cated in the Scheme world ie their

value is stored in one of the structures manipu

set slotvalue p age

lated by the Tk library instead of in a Scheme ob

Since an accessor as also b een dened on the name

ject Of course accessors will take into account

slot it can b e read with the following expression

this fact and functions for reading or writing such

slots are unchanged For example

name p

As b efore slot setting can b e done with the

set command b display OK

generalized set as in

p ermits to set the script asso ciated to the b but

set name p

ton

Preceding defclass states that the command

Tk classes

slot can contain a value of any type Type of a

Now that basic concepts have b een exp osed let

slot p ermits to the system to apply the adequate

come back to how using Tk with the ob ject layer

co ercion function when a slot is read Since Tk

In our mo del each Tk option is dened as a slot

3

For instance a simplied denition of a Tk but

Pseudoslots are dened in the metaclass

hence the metaclass option in denition ton could b e

several metho ds asso ciated with it These meth always computes results as strings this conver

o ds describ e the generic function b ehaviour ac sion can b e done automatically when we know

cording to the type of its parameters A metho d the type of the slot eg a b order width is always

for a generic function is dened with the dene stored as an integer in Tk structures No con

method form version is done when a slot is written this work

is done by Tk since it will reject bad values

Following example shows three metho ds of the

Note that using the ob ject extension of STk

generic function valueof

p ermits the user to forget some Tk idiosyncrasies

In particular it p ermits to avoid the knowledge

definemethod valueof obj Scale

of pure Tk naming conventions The only thing

stringnumber Id obj get

the user has to know when creating a new ob ject

is its parent An example of widgets creation is

definemethod valueof obj Entry

shown b elow

Id obj get

define f make Frame

definemethod valueof obj

define b make Button

error Bad call obj

text B

parent f

When calling the valueof generic function sys

define b make Button

tem will choose the more adequate metho d ac

text B

cording to its parameter type if parameter is a parent f

scale or an entry rst or second metho d is called

Created buttons here sp ecify that their parent

otherwise the third metho d is called since it do es

is the frame f Since this frame do es not sp ecify

n discriminates in favour of a particular param

a particular parent it is supp osed to b e a direct

eter type

descendant of the ro ot window This parents

Setter metho d are a sp ecial kind of metho ds

notion is also used for canvas items a canvas

which are used with the generalized set Here

item is considered as a son of the canvas which

are the corresp onding setter metho ds to previous

contains it For instance

valueof

define c make Canvas

define r make Rectangle

definemethod setter valueof

parent c

obj Scale value

coords

Id obj set value

denes a rectangle called r in the c canvas User

definemethod setter valueof

can now forget that r is included in c since this

obj Entry value

information is embedded in the Scheme ob ject

Id obj delete end

To move this rectangle one can use for example

Id obj insert value

the following expression

definemethod setter valueof obj

move r

error Bad call obj

which is more natural than the things we have to

One of these metho ds will b e called when evalu

do at STk rst level

ating following form dep ending of type of x

Generic functions

set valueof x

With the STk ob ject layer execution of a

As we can see here generic functions yield things metho d do esnt use the classical message sending

more homogeneous than what we can have at mechanism as in numerous ob ject languages but

STk rst level Indeed getting and setting the generic functions The mechanism implemented

value of an entry or a scale can now b e done in a in STk is a subset of the generic functions of

similar fashion with those metho ds CLOS As in CLOS a generic function can have

Implementation Perfor the package will b e more stable

mances

Op en problems

The STk interpreter is written in C and for some

parts in Scheme The ob ject oriented layer pre

Using Scheme with Tk causes some diculties

sented in section is totally written in Scheme

which cannot satisfactorily b e resolved Some

The Scheme interpreter is as far as p ossible con

problems are due to the very nature of Scheme

form with RRS It is imp ortant to note that

others are due to Tk Following is a list of ma jor

the Tk library is used unmo died in this inter

of them

preter All the Tcl functions call issued by Tk

 RRS requires that symbol must b e case in

primitives are simulated by STk This p ermits

sensitive Tk imp oses in a certain extent to

to the STk interpreter to b e as far as p ossible

the underlying interpretative language to b e

indep endent of the Tk co de In particular em

case sensitive since command in event han

b edding a new release of Tk will only require to

dler scripts take into account the case of the

link the new library to the actual STk inter

letter following the symbol

preter Furthermore external contributions we

can nd on net can b e easily included in the in

 Another problem arises with lists and

terpreter since Tcl intrinsics replacements are

strings Tcl do esnt distinguish those two

present in the STk core

types In particular one can set an option as

Actual implementation do esnt put accent on

a string and asking to Tk this option value

p erformances It must b e seen as a prototype

will yield a list This is not a problem in Tcl

which must b e stretched further However mea

since a list is only another vision of a string

suring the p erformances of the STk package is

Unfortunately there is no such direct equiv

a dicult task and has not b een really done yet

alence in Scheme Improvement of this p oint

What we can say for now is that there is a little

would probably require a mo dication of the

overhead when calling a Tk primitive written in

Tk library

C since the STk package must translate all the

parameters in C strings This translation must

 One of the ma jor assets of the Tk library is

b e done b ecause the Tk library thinks that it

the send command With this command a

works on Tcl which uses strings for passing pa

Tk application can ask to another running

rameters In counterpart pro cedure written in

Tk interpreter to evaluate an expression

Scheme are far more ecient than Tcl scripts

Since Tk library is not mo died STk in

since the Scheme interpreter uses an appropriate

terpreters cannot b e distinguished from Tcl

format which is cheaper than strings In par

ones Most of the time this is confusing and

ticular there is no data conversion when other

error prone b ecause requests to an applica

Scheme pro cedures are called Using the ob ject

tion must take into account the language its

extension of STk gives this to ol more p ower but

underlying interpreter understands Keep

is as we can exp ect more time consuming For

ing the communication p ossibility b etween

now p enalty when using the STk ob ject oriented

STk an Tcl applications seems to b e suit

layer is mainly due to ob ject creation creating

able but it would b e ne to establish a stan

a widget with a make is nearly times slower

dard way to determine what kind of inter

than basic creation achieved by using only rst

preter is running in a particular application

level primitives However getting or reading a

Tk option using a slot access is only times

slower than direct Tk conguration We can ex

References

p ect that rewriting some parts of the STk ob ject

layer in C will decrease those ratios Comparing J Ousterhout An X to olkit based on the

the Tcl and Scheme approaches needs much more Tcl Language In USENIX Winter Confer

working and a complete study will b e done when ence pages January

J Ousterhout Tcl an embeddable command

language In USENIX Winter Conference

pages January

G J Sussman H Ab elson and J Suss

man Structure and Interpretation of Com

puter Programs McGrawHill New York

IEEE Standard IEEE Standard

for the Scheme Programming Language In

stitute of Electrical and Electronical Engi

neers Inc New York

G L Steele Jr Common Lisp the Language

nd Edition Digital Press Bedford MA

G Kickzales Tinyclos unpublished work

Source available on arisiaxeroxcom in direc

tory pubmops December

Apple Dylan an object oriented dynamic

language Apple Computer

D G Brob ow G Kickzales J de Rivieres

The Art of Meta Object Protocol MIT Press

4

W Clinger and J Rees editors Rev ised

Rep ort on the Algorithmic Language Scheme

ACM Lisp Pointers