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