git @ Cat's Eye Technologies Xoomonk / master README.md
master

Tree @master (Download .tar.gz)

README.md @masterview rendered · raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
The Xoomonk Programming Language
================================

Language version 1.0

Abstract
--------

_Xoomonk_ is a programming language in which _malingering updatable stores_
are first-class objects.  Malingering updatable stores unify several language
constructs, including procedure activations, named parameters, and object-like
data structures.

    -> Tests for functionality "Interpret Xoomonk program"

Description
-----------

Overall, Xoomonk looks like a "typical" imperative language.  The result
of evaluating an expression can be assigned to a variable, and the contents
of a variable can be used in a subsequent expression.

    | a := 1
    | b := a
    | print b
    = 1

Like most typical imperative languages, a block (delimited by curly braces)
may contain a sequence of imperative statements.  However, such blocks may
appear as terms in expressions; here they evaluate to entire updatable stores.

    | a := {
    |   c := 5
    |   d := c
    | }
    | print a
    = [c=5,d=5]

Once created, a store can be updated and accessed.

    | a := {
    |   c := 5
    |   d := c
    | }
    | print a
    | a.d := 7
    | print a
    | print a.c
    = [c=5,d=5]
    = [c=5,d=7]
    = 5

A store can also be assigned to a variable after creation.  Stores are
accessed by reference, so this creates two aliases to the same store.

    | a := {
    |   c := 5
    |   d := c
    | }
    | b := a
    | b.c := 17
    | print a
    | print b
    = [c=17,d=5]
    = [c=17,d=5]

To create an independent copy of the store, the postfix `*` operator is
used.

    | a := {
    |   c := 5
    |   d := c
    | }
    | b := a*
    | b.c := 17
    | print a
    | print b
    = [c=5,d=5]
    = [c=17,d=5]

Empty blocks are permissible.

    | a := {}
    | print a
    = []

Once a store has been created, only those variables used in the store can be
updated and accessed — new variables cannot be added.

    | a := { b := 6 }
    | print a.c
    ? Attempt to access undefined variable c

    | a := { b := 6 }
    | a.c := 12
    ? Attempt to assign undefined variable c

In the outermost level, as well, a variable cannot be used before it
has been assigned.

    | print r
    | r := 5
    ? Attempt to access undefined variable r

Stores and integers are the only two data types in Xoomonk.  However, there
are some special forms of the print statement, demonstrated here, which
allow for textual output.

    | a := 65
    | print char a
    | print string "Hello, world!"
    | print string "The value of a is ";
    | print a;
    | print string "!"
    = A
    = Hello, world!
    = The value of a is 65!

Xoomonk enforces strict block scope.  Variables can be shadowed in an
inner block.

    | a := 14
    | b := {
    |   a := 12
    |   print a
    | }
    | print a
    = 12
    = 14

We now present today's main feature.

It's important to understand that a block need not define all the variables
used in it.  That is, a block may contain variables which are used, but never
assigned a value, in the block.  Such blocks do not immediately evaluate to a
store.  Instead, they evaluate to an object called an _unsaturated store_.

Or, to put it another way:

If, in a block, you refer to a variable which has not yet been given
a value in that updatable store, the computations within the block are not
performed until that variable is given a value.  Such a store is called an
_unsaturated store_.

    | a := {
    |   d := c
    | }
    | print a
    = [c=?,d=0]

An unsaturated store behaves similarly to a saturated store in certain
respects.  In particular, unsaturated stores can be updated.  If doing
so means that all of the undefined variables in the store are now defined,
the block associated with that store is evaluated, and the store becomes
saturated.  In this sense, an unsaturated store is like a promise, and
this bears some resemblance to lazy evaluation (thus the term _malingering_).

    | a := {
    |   print string "executing block"
    |   d := c
    | }
    | print a
    | a.c := 7
    | print a
    = [c=?,d=0]
    = executing block
    = [c=7,d=7]

Once a store has become saturated, the block associated with it is not
executed again.

    | a := {
    |   d := c
    | }
    | a.c := 7
    | print a
    | a.c := 4
    | print a
    = [c=7,d=7]
    = [c=4,d=7]

Saturating one copy of an unsaturated store will not saturate the other
copy.

    | a := {
    |   print string "saturated"
    |   d := c
    | }
    | b := a*
    | print a
    | print b
    | a.c := 7
    | print a
    | print b
    | b.c := 5
    | print b
    = [c=?,d=0]
    = [c=?,d=0]
    = saturated
    = [c=7,d=7]
    = [c=?,d=0]
    = saturated
    = [c=5,d=5]

Unassigned variables cannot be accessed from an unsaturated store.

    | a := {
    |   d := c
    | }
    | x := a.c
    ? Attempt to access unassigned variable c

Assigned variables can be accessed from an unsaturated store, but they have
the value 0 until the store is saturated.

    | a := {
    |   d := c
    | }
    | print a.d
    = 0

This is true, even if the variable is assigned a constant expression
inside the block.

    | a := {
    |   b := 7
    |   d := c
    | }
    | print a.b
    = 0

If, however, the unsaturated store contains some unassigned variables that have
been updated since the store was created, those variable may be accessed, even
if the store is still unsaturated.

    | a := {
    |   print string "executing block"
    |   p := q
    |   d := c
    | }
    | a.q := 7
    | print a.q
    = 7

It is possible to assign a variable in an unsaturated store which is
assigned somewhere in the block.  When the store becomes saturated, however,
that variable will be overwritten.

    | a := {
    |   b := 7
    |   d := c
    | }
    | a.b := 4
    | print a
    = [b=4,c=?,d=0]

    | a := {
    |   b := 7
    |   d := c
    | }
    | a.b := 4
    | a.c := 4
    | print a
    = [b=7,c=4,d=4]

It's important to note that a block is considered saturated when all the
variables *used* in the block are *assigned* somewhere in the block.  If, in a
block, a variable is used before it is assigned, the block will *still* be
executed, if saturated.

    | a := {
    |   c := b
    | }
    | a.b := 5
    | print a
    = [b=5,c=5]

    | a := {
    |   b := b
    | }
    ? Attempt to access undefined variable b

A way to work around this is to add an extra unassigned variable, to keep
the store unsaturated.

    | a := {
    |   print string "executing block"
    |   l := b
    |   b := 3
    |   l := 3
    | }
    | print string "saturating store"
    | a.b := 5
    | print a
    ? Attempt to access undefined variable b

    | a := {
    |   print string "executing block"
    |   l := b
    |   b := 3
    |   l := c
    |   l := 3
    | }
    | print string "saturating store"
    | a.b := 5
    | a.c := 9
    | print a
    = saturating store
    = executing block
    = [b=3,c=9,l=3]

We now describe how this language is (we reasonably assume) Turing-complete.

Firstly, there is a built-in special store called `$`, which is a Special
Name which is magically available in every scope and which cannot be assigned
to, but which can be modified.

    | $ := 4
    ? Cannot assign to $

    | $.foo := 4
    | print string "ok"
    = ok

Since `$` is available in every scope, it can be thought of as being the
"global scope", and can be conveniently used to communicate values across
scopes.

    | $.r := 4
    | q := {
    |   print string "hello"
    |   c := $.r
    |   j := d
    | }
    | q.d := 5
    | print q.c
    = hello
    = 4

Operations are accomplished with certain built-in unsaturated stores.  For
example, there is a store called `add`, which can be used for addition.  These
built-in stores are all located in the `$` store.

    | print $.add
    = [result=0,x=?,y=?]

    | a := {
    |   print $.add
    | }
    = [result=0,x=?,y=?]

    | $.add.x := 3
    | $.add.y := 5
    | print $.add.result
    | print $.add
    = 8
    = [result=8,x=3,y=5]

Because using a built-in operation store in this way saturates it, it cannot
be used again.  Typically, you will want to make a copy of the store first, and
use that copy, leaving the built-in store unmodified.

    | o1 := $.add*
    | o1.x := 4
    | o1.y := 7
    | o2 := $.add*
    | o2.x := o1.result
    | o2.y := 9
    | print o2.result
    = 20

Since Xoomonk is not a strictly minimalist language, there is a selection
of built-in stores which provide useful operations: `$.add`, `$.sub`, `$.mul`,
`$.div`, `$.gt`, and `$.not`.

    | o1 := $.sub*
    | o1.x := 7
    | o1.y := 4
    | print o1.result
    = 3

    | o1 := $.mul*
    | o1.x := 7
    | o1.y := 4
    | print o1.result
    = 28

    | o1 := $.div*
    | o1.x := 29
    | o1.y := 4
    | print o1.result
    = 7

    | o1 := $.gt*
    | o1.x := 29
    | o1.y := 4
    | print o1.result
    = 1

    | o1 := $.gt*
    | o1.x := 4
    | o1.y := 4
    | print o1.result
    = 0

    | o1 := $.not*
    | o1.x := 29
    | print o1.result
    = 0

    | o1 := $.not*
    | o1.x := 0
    | print o1.result
    = 1

Decision-making is also accomplished with a built-in store, `$.if`.  This store
contains variables caled `cond`, `then`, and `else`.  `cond` should
be an integer, and `then` and `else` should be unsaturated stores where `x` is
unassigned.  When the first three are assigned values, if `cond` is nonzero,
it is assigned to `x` in the `then` store; otherwise, if it is zero, it is
assigned to `x` in the `else` store.

    | o1 := $.if*
    | o1.then := {
    |   y := x
    |   print string "condition is true"
    | }
    | o1.else := {
    |   y := x
    |   print string "condition is false"
    | }
    | o1.cond := 0
    = condition is false

    | o1 := $.if*
    | o1.then := {
    |   y := x
    |   print string "condition is true"
    | }
    | o1.else := {
    |   y := x
    |   print string "condition is false"
    | }
    | o1.cond := 1
    = condition is true

Repetition is also accomplished with a built-in store, `$.loop`.  This store
contains an unassigned variable called `do`.  When it is assigned a value,
assumed to be an unsaturated store, a copy of that unsaturated store is made.
The variable `x` inside that copy is assigned the value 0.  This is supposed
to saturate the store.  The variable `continue` is then accessed from the
store.  If it is nonzero, the process repeats, with another copy of the `do`
store getting 0 assigned to its `x`, and so forth.

    | l := $.loop*
    | $.counter := 5
    | l.do := {
    |   y := x
    |   print $.counter
    |   o := $.sub*
    |   o.x := $.counter
    |   o.y := 1
    |   $.counter := o.result
    |   continue := o.result
    | }
    | print string "done!"
    = 5
    = 4
    = 3
    = 2
    = 1
    = done!

Because the `$.loop` construct will always execute the `do` store at least once
(even assuming its only unassigned variable is `x`), it acts like a so-called
`repeat` loop.  It can be used in conjunction with `$.if` to simulate a
so-called `while` loop.  With this loop, the built-in operations provided,
and variables which may contain unbounded integer values, Xoomonk should
be uncontroversially Turing-complete.  (Although admittedly, using an unbounded
integer or two to simulate a Turing machine's tape, especially with Xoomonk's
arithmetic operations, would be fairly cumbersome.)

Finally, there is no provision for defining functions or procedures, because
malingering stores can act as these constructs.

    | perimeter := {
    |   o1 := $.mul*
    |   o1.x := x
    |   o1.y := 2
    |   o2 := $.mul*
    |   o2.x := y
    |   o2.y := 2
    |   o3 := $.add*
    |   o3.x := o1.result
    |   o3.y := o2.result
    |   result := o3.result
    | }
    | p1 := perimeter*
    | p1.x := 13
    | p1.y := 6
    | print p1.result
    | p2 := perimeter*
    | p2.x := 4
    | p2.y := 1
    | print p2.result
    = 38
    = 10

Grammar
-------

    Xoomonk ::= { Stmt }.
    Stmt    ::= Assign | Print.
    Assign  ::= Ref ":=" Expr.
    Print   ::= "print" ("string" <string> | "char" Expr | Expr) [";"].
    Expr    ::= (Block | Ref | Const) ["*"].
    Block   ::= "{" { Stmt } "}".
    Ref     ::= Name {"." Name}.
    Name    ::= "$" | <alphanumeric>.
    Const   ::= <integer-literal>.

Discussion
----------

There is some similarity with Wouter van Oortmerssen's language Bla, in that
function environments are very close cousins of updatable stores.  But
Xoomonk, quite unlike Bla, is an imperative language; once created, a store
may be updated at any point.  And, of course, this property is exploited in
the introduction of malingering stores.

The original idea for Xoomonk had an infix operator `&`, which took two stores
as its arguments (at least one of which must be saturated) and evaluated to a
third store which was the result of merging the two argument stores.  This
result store may be saturated even if only one of the argument stores was
saturated, if the saturated store gave all the values that the unsaturated
store needed.  This operator was dropped because it is mostly just syntactic
sugar for assigning each of the desired variables from one store to the other.
However, it does admittedly provide a very natural syntax, which orthogonally
includes "named arguments", when using unsaturated stores as procedures:

    perimeter = {
      # see example above
    }
    g := perimeter* & { x := 13 y := 6 }
    print g.result

Xoomonk 0.1 had slightly more sophisticated semantics for unsaturated stores
than Xoomonk 1.0 has.  Specifically, there was a (fuzzy) idea that a variable
could be "unresolved", that is, assigned a value derived from variables which
were unassigned.  Xoomonk 1.0 simplified this to assigned variables having
the value 0 until the store is saturated, and a store being saturated when
all of its unassigned variables have been given a value, even if some
assigned variables are used in the block before they have ever been assigned.
Xoomonk 1.0 also dropped the idea that the main program was a block.

In addition, in Xoomonk 0.1, `$` was an prefix operator rather than a
Special Name, and the Special Name `^` allowed access to the lexically
enclosing store of a block.  While there is a certain elegance to this, it
was deemed overkill, and in Xoomonk 1.0, `$` was changed to a Special Name
which referred to a global store (which can be used to communicate values
between scopes without messing about with lexically enclosing "upvalues".)

Xoomonk, as a project, was also an early experiment in _test-driven language
design_.  When Xoomonk 0.1 was released, the language was described only with
the set of examples (above) written in Falderal format, each of which both
illustrates some aspect of the language, and is used as a test case.  When a
reference interpreter was implemented for Xoomonk 1.0, this made implementation
much easier.  (Since Xoomonk 0.1 was released, I've used this technique
successfully for several other languages as well.)

Happy malingering!

-Chris Pressey  
Cat's Eye Technologies  
August 7, 2011  
Evanston, IL