git @ Cat's Eye Technologies Lanthorn / 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
Lanthorn
========

Version 1.0 | _Entry_ @ [catseye.tc](https://catseye.tc/node/Lanthorn)
| _See also:_ [Lariat](https://catseye.tc/node/Lariat)
* [Iphigeneia](https://catseye.tc/node/Iphigeneia)

When I first came across a explanation of how `letrec` works, it was
in terms of updating references: each of the names is bound to a cell,
and when the thing that name refers to is eventually defined, that cell
is updated with that thing.

My reaction to this was _ugh_.  I mean, sure, it works, but in the
context of functional programming, such an imperative description is
really unsatisfying.

So, I present here a tiny, eager, purely functional language, christened
**Lanthorn**, whose sole purpose is to host a demonstration of how `letrec`
can be written as syntactic sugar over `let` in a purely functional way.

The transformation is unobtrusive in that it doesn't make any changes in
the body of the `letrec`.  The resulting code is not, however, intended
to be efficient.

Since the language is simple enough and conventional enough that you
can probably guess what the programs mean, let's leave the description
of the language until [Appendix A](#appendix-a), and go straight into
describing the transformation.

Desugaring
----------

    -> Tests for functionality "Desugar Lanthorn Program"

Basically, what we want to do, is take this...

    letrec
        odd  = fun(x) -> if eq(x, 0) then false else even(sub(x, 1))
        even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1))
    in
        even(6)

...and turn it into this.

    let
        odd0  = fun(x, odd1, even1) ->
                     let
                         odd = fun(x) -> odd1(x, odd1, even1)
                         even = fun(x) -> even1(x, odd1, even1)
                     in
                         if eq(x, 0) then false else even(sub(x, 1)))
        even0 = fun(x, odd1, even1) ->
                     let
                         odd = fun(x) -> odd1(x, odd1, even1)
                         even = fun(x) -> even1(x, odd1, even1)
                     in
                         if eq(x, 0) then true else odd(sub(x, 1)))
        odd   = fun(x) -> odd0(x, odd0, even0)
        even  = fun(x) -> even0(x, odd0, even0)
    in
        even(6)

Our evaluator implements this transformation in the
[Language.Lanthorn.LetRec](src/Language/Lanthorn/LetRec.hs) module.
Here is what it produces.  Note there is a bit of name-mangling
added, compared to the hand-written expansion above.

    letrec
        odd  = fun(x) -> if eq(x, 0) then false else even(sub(x, 1))
        even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1))
    in
        even(6)
    => let
    =>   odd$0 = fun(x, odd$1, even$1) -> let
    =>       odd = fun(x$1) -> odd$1(x$1, odd$1, even$1)
    =>       even = fun(x$1) -> even$1(x$1, odd$1, even$1)
    =>     in
    =>       if eq(x, 0) then false else even(sub(x, 1))
    =>   even$0 = fun(x, odd$1, even$1) -> let
    =>       odd = fun(x$1) -> odd$1(x$1, odd$1, even$1)
    =>       even = fun(x$1) -> even$1(x$1, odd$1, even$1)
    =>     in
    =>       if eq(x, 0) then true else odd(sub(x, 1))
    =>   odd = fun(x) -> odd$0(x, odd$0, even$0)
    =>   even = fun(x) -> even$0(x, odd$0, even$0)
    => in
    =>   even(6)

In English, it adds a number of extra parameters to each function in
the set of bindings.  Specifically, it adds one parameter for each
of the bindings.  It then sets up some bindings _inside_ each function
so that the function uses these parameters for the recursive calls
it makes.  It also sets up some bindings outside of these functions
to that the body of the `letrec` sees functions with the original
parameters they had, hiding all these extra parameters.

Related Work
------------

Xavier Pinho has written up an alternative way of transforming `letrec`
into `let`, using surjective pairing and the Y combinator, in
[an issue on the Lanthorn project on GitHub](https://github.com/catseye/Lanthorn/issues/1).

Appendix A
----------

### Basic Syntax of Lanthorn

    -> Tests for functionality "Pretty-print Lanthorn Program"

Function application, numeric literals, string literals.

    add(1, 2)
    => add(1, 2)

Name binding (`let`) and name reference.

    let a = 1
        b = 1
        in zed(a, b)
    => let
    =>   a = 1
    =>   b = 1
    => in
    =>   zed(a, b)

The character `$` may not appear in user-supplied names.

    let
      a$b = 1
    in
      zed(a$b)
    ?> unexpected "$"

Conditional by boolean (`if`).

    if gt(a, b) then a else b
    => if gt(a, b) then a else b

Function values.

    let up = fun(x) -> add(x, 1) in up(5)
    => let
    =>   up = fun(x) -> add(x, 1)
    => in
    =>   up(5)

### Basic Semantics of Lanthorn

    -> Tests for functionality "Evaluate Lanthorn Program"

    1
    ===> 1

    if true then 5 else 6
    ===> 5

    let a = 2 in a
    ===> 2

Basic functions.

    let r = fun(x) -> 77 in r(1)
    ===> 77

    let r = fun(x) -> x in r(66)
    ===> 66

`let` is like Scheme's `let*` or Standard ML's `let`:
later bindings can see earlier bindings.

    let
      p = 99
      r = fun(x) -> p
    in
      r(66)
    ===> 99

Note that depicting a function is implementation-dependent.

    fun(x) -> x
    ===> <<function>>

Can't shadow a binding in `let`.

    let a = 1 in let a = 2 in a
    ???> Already defined: a

Can't shadow a binding in the formals of a `fun`.

    let r = fun(x, x) -> x in r(10, 10)
    ???> Already defined: x

    let r = fun(x) -> let x = 3 in x in r(10)
    ???> Already defined: x

#### `letrec`

Basic usage of `letrec`.

    letrec
        oddp  = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1))
        evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1))
    in
        evenp(6)
    ===> true

    letrec
        oddp  = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1))
        evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1))
    in
        evenp(5)
    ===> false

`letrec` nested inside an `if` inside a function definition in an arm of
another `letrec`.

    letrec
        facto = fun(n) -> if eq(n, 1) then 1 else
            letrec
                oddp  = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1))
                evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1))
            in
                if oddp(n) then
                    mul(n, facto(sub(n, 1)))
                else
                    facto(sub(n, 1))
    in
        facto(8)
    ===> 105

`letrec` nested in the body of another `letrec`.

    letrec
        oddp  = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1))
        evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1))
    in
        letrec facto = fun(n) ->
            if eq(n, 1) then
                1
            else if oddp(n) then
                mul(n, facto(sub(n, 1)))
            else
                facto(sub(n, 1))
    in
        facto(8)
    ===> 105

Nested `letrec`, nested right in the arm of another `letrec`.  Currently,
this is an error, because the inner scope cannot "see" the outer `letrec`.
Though I'm not yet convinced of what the most reasonable behaviour is here.

    letrec
        facto =
            letrec
                oddp  = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1))
                evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1))
            in
                fun(n) -> if eq(n, 1) then 1 else
                    if oddp(n) then
                        mul(n, facto(sub(n, 1)))
                    else
                        facto(sub(n, 1))
    in
        facto(8)
    ???> Not in scope: facto

`letrec` nested inside a function definition inside an arm of a plain `let`.

    let
        factoo = fun(f, n) ->
            letrec
                oddp  = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1))
                evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1))
            in
                if eq(n, 1) then 1 else
                    if oddp(n) then
                        mul(n, f(f, sub(n, 1)))
                    else
                        f(f, sub(n, 1))
    in
        factoo(factoo, 7)
    ===> 105

`letrec` nested inside body of a plain `let`.

    let
        factopen = fun(f, n) -> if eq(n, 1) then 1 else mul(n, f(f, sub(n, 1)))
        target = 7
    in
        letrec
            oddp  = fun(x) -> if eq(x, 0) then false else evenp(sub(x, 1))
            evenp = fun(x) -> if eq(x, 0) then true else oddp(sub(x, 1))
        in
            if oddp(target) then factopen(factopen, target) else 0
    ===> 5040

`letrec` works on functions that have more than one argument.

    letrec
        oddsump  = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then false else evensump(sub(x, 1), y, z)
        evensump = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then true else oddsump(sub(x, 1), y, z)
    in
        evensump(5,3,1)
    ===> false

    letrec
        oddsump  = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then false else evensump(sub(x, 1), y, z)
        evensump = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then true else oddsump(sub(x, 1), y, z)
    in
        evensump(6,3,1)
    ===> true

`letrec` works on functions which use different argument names.

    letrec
        oddsump  = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then false else evensump(sub(x, 1), y, z)
        evensump = fun(p,q,r) -> if eq(add(p, add(q, r)), add(q, r)) then true else oddsump(sub(p, 1), q, r)
    in
        evensump(5,3,1)
    ===> false

    letrec
        oddsump  = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then false else evensump(sub(x, 1), y, z)
        evensump = fun(p,q,r) -> if eq(add(p, add(q, r)), add(q, r)) then true else oddsump(sub(p, 1), q, r)
    in
        evensump(6,3,1)
    ===> true

`letrec` works on functions that have differing numbers of arguments.

    letrec
        oddsump  = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then false else evensump(sub(x, 1), add(y, z))
        evensump = fun(p,q)   -> if eq(add(p, q), q) then true else oddsump(sub(p, 1), 1, sub(q, 1))
    in
        oddsump(5,3,1)
    ===> true

### Properties of the `letrec` transformation

    -> Tests for functionality "Desugar Lanthorn Program"

When a `letrec` is desugared, the generated functions have argument
names that are based on the original argument names.  Also, the
innermost `let`s bind the plain names to functions with the same arity
as the original functions.

    letrec
        oddsump  = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then false else evensump(sub(x, 1), y, z)
        evensump = fun(x,y,z) -> if eq(add(x, add(y, z)), add(y, z)) then true else oddsump(sub(x, 1), y, z)
    in
        evensump(5,3,1)
    => let
    =>   oddsump$0 = fun(x, y, z, oddsump$1, evensump$1) -> let
    =>       oddsump = fun(x$1, y$1, z$1) -> oddsump$1(x$1, y$1, z$1, oddsump$1, evensump$1)
    =>       evensump = fun(x$1, y$1, z$1) -> evensump$1(x$1, y$1, z$1, oddsump$1, evensump$1)
    =>     in
    =>       if eq(add(x, add(y, z)), add(y, z)) then false else evensump(sub(x, 1), y, z)
    =>   evensump$0 = fun(x, y, z, oddsump$1, evensump$1) -> let
    =>       oddsump = fun(x$1, y$1, z$1) -> oddsump$1(x$1, y$1, z$1, oddsump$1, evensump$1)
    =>       evensump = fun(x$1, y$1, z$1) -> evensump$1(x$1, y$1, z$1, oddsump$1, evensump$1)
    =>     in
    =>       if eq(add(x, add(y, z)), add(y, z)) then true else oddsump(sub(x, 1), y, z)
    =>   oddsump = fun(x, y, z) -> oddsump$0(x, y, z, oddsump$0, evensump$0)
    =>   evensump = fun(x, y, z) -> evensump$0(x, y, z, oddsump$0, evensump$0)
    => in
    =>   evensump(5, 3, 1)

The transformation mangles names that it generates so that they never
shadow names that appear in the user's program.  (Names containing `$` may
not appear in a user-supplied program.)

    let
        odd0 = fun(a, b, c) -> a
    in
        letrec
            odd  = fun(x) -> if eq(x, 0) then false else even(sub(x, 1))
            even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1))
        in
            even(6)
    => let
    =>   odd0 = fun(a, b, c) -> a
    => in
    =>   let
    =>     odd$0 = fun(x, odd$1, even$1) -> let
    =>         odd = fun(x$1) -> odd$1(x$1, odd$1, even$1)
    =>         even = fun(x$1) -> even$1(x$1, odd$1, even$1)
    =>       in
    =>         if eq(x, 0) then false else even(sub(x, 1))
    =>     even$0 = fun(x, odd$1, even$1) -> let
    =>         odd = fun(x$1) -> odd$1(x$1, odd$1, even$1)
    =>         even = fun(x$1) -> even$1(x$1, odd$1, even$1)
    =>       in
    =>         if eq(x, 0) then true else odd(sub(x, 1))
    =>     odd = fun(x) -> odd$0(x, odd$0, even$0)
    =>     even = fun(x) -> even$0(x, odd$0, even$0)
    =>   in
    =>     even(6)

    -> Tests for functionality "Evaluate Lanthorn Program"

    letrec
        odd  = fun(x) -> if eq(x, 0) then false else even(sub(x, 1))
        odd0 = fun(a, b, c) -> a
        even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1))
    in
        even(6)
    ===> true

You might think that instead of mangling names, we could just allow shadowing
in the language.  But that by itself doesn't solve our problem, since you
could still say something like the following.  The `letrec` desugaring would
have to be more aware of how it constructs names, at any rate, in order to
avoid the conflict here.  And mangling is the simplest way to do that.

    -> Tests for functionality "Desugar Lanthorn Program"

    letrec
        odd  = fun(x) -> if eq(x, 0) then false else even(sub(x, 1))
        odd0 = fun(a, b, c) -> a
        even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1))
    in
        even(6)
    => let
    =>   odd$0 = fun(x, odd$1, odd0$1, even$1) -> let
    =>       odd = fun(x$1) -> odd$1(x$1, odd$1, odd0$1, even$1)
    =>       odd0 = fun(a$1, b$1, c$1) -> odd0$1(a$1, b$1, c$1, odd$1, odd0$1, even$1)
    =>       even = fun(x$1) -> even$1(x$1, odd$1, odd0$1, even$1)
    =>     in
    =>       if eq(x, 0) then false else even(sub(x, 1))
    =>   odd0$0 = fun(a, b, c, odd$1, odd0$1, even$1) -> let
    =>       odd = fun(x$1) -> odd$1(x$1, odd$1, odd0$1, even$1)
    =>       odd0 = fun(a$1, b$1, c$1) -> odd0$1(a$1, b$1, c$1, odd$1, odd0$1, even$1)
    =>       even = fun(x$1) -> even$1(x$1, odd$1, odd0$1, even$1)
    =>     in
    =>       a
    =>   even$0 = fun(x, odd$1, odd0$1, even$1) -> let
    =>       odd = fun(x$1) -> odd$1(x$1, odd$1, odd0$1, even$1)
    =>       odd0 = fun(a$1, b$1, c$1) -> odd0$1(a$1, b$1, c$1, odd$1, odd0$1, even$1)
    =>       even = fun(x$1) -> even$1(x$1, odd$1, odd0$1, even$1)
    =>     in
    =>       if eq(x, 0) then true else odd(sub(x, 1))
    =>   odd = fun(x) -> odd$0(x, odd$0, odd0$0, even$0)
    =>   odd0 = fun(a, b, c) -> odd0$0(a, b, c, odd$0, odd0$0, even$0)
    =>   even = fun(x) -> even$0(x, odd$0, odd0$0, even$0)
    => in
    =>   even(6)

    -> Tests for functionality "Evaluate Lanthorn Program"

    let
        odd0 = fun(a, b, c) -> a
    in
        letrec
            odd  = fun(x) -> if eq(x, 0) then false else even(sub(x, 1))
            even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1))
        in
            even(6)
    ===> true

Note that there is probably a case where a `letrec` nested another `letrec`, and
which shadows variables of the enclosing `letrec`, produces a less readable
error message about shadowing, because it mentions the mangled names; but
I can live with that for now.