smalltalk-tng
view etng-r2/monadic-book.tng @ 321:c4a0718c2d3c
Sketch of dependencies
| author | Tony Garnock-Jones <tonygarnockjones@gmail.com> |
|---|---|
| date | Sat Oct 08 15:36:03 2011 -0400 (7 months ago) |
| parents | |
| children |
line source
1 namespace m = "http://github.com/leithaus/XTrace/tree/monadic/src/main/book/content/#";
2 namespace c = "http://eighty-twenty.org/etng/r1/ns/collection#";
4 define m:mention reference -> { .m:expression k -> k .m:mention reference };
5 define m:abstraction formals body -> { .m:expression k -> k .m:abstraction formals body };
6 define m:application operation actuals -> { .m:expression k -> k .m:application operation actuals };
8 -- Let's imagine we chose a representation for names, separately from
9 -- the representation of expressions. How, in eTNG, would we denote
10 -- the contract (type) for the constructors and destructors? Separately?
11 -- Together with the definitions? Compare with how Newmoon does it
12 -- these days, perhaps.
14 -- Ugh, the expression problem.
16 define m:freeVariables x ->
17 x.m:expression {
18 .m:mention r -> c:set.singleton r;
19 .m:abstraction f b -> (c:set.fromStream f) ++ (m:freeVariables b);
20 .m:application o a -> a | s:map m:freeVariables | s:foldr (m:freeVariables o) (binop ++);
21 };
23 define m:closure fn -> { .m:value k -> k .m:closure fn };
24 define m:quantity q -> { .m:value k -> k .m:quantity q };
26 -- Ideally want to define some kind of interface for objects to satisfy.
27 --
28 -- type Dereferencer = {def apply( m : Mention ) : Value }
29 -- type Expansionist =
30 -- {def extend( fmls : List[Mention], actls : List[Value] ) : Dereferencer}
31 -- type Environment <: (Dereferencer with Expansionist)
32 -- type Applicator = Expression => List[Value] => Value
34 define m:initialApplicator expression actuals ->
35 expression.m:expression {
36 .m:integerExpression i -> m:quantity i;
37 _ -> throw exception("why are we here?");
38 };
40 define m:reduce (applicator, environment) ->
41 rec reduceExpression { expression ->
42 expression.m:expression {
43 .m:integerExpression i -> m:quantity i;
44 .m:mention v -> environment.m:lookup v;
45 .m:abstraction formals body ->
46 m:closure { actuals ->
47 let keys = formals | s:map m:mention;
48 m:reduce (applicator, environment.m:extend(keys, actuals)) body };
49 .m:application operator actuals ->
50 (reduceExpression operator).m:value {
51 .m:closure fn -> fn (actuals | s:map reduceExpression);
52 _ -> throw exception("attempt to apply non function");
53 };
54 }
55 };
