That Shivers/Might transducers paper that linked to is still sinking in. I kinda lean towards thinking that some of the tranducer combinators can be implemented in terms of the simpler source/sink combinators. Not sure.

```open SMLofNJ.Cont;
datatype ('a, 'b) chan = Chan of ('a * ('b, 'a) chan) cont;

fun sw x (Chan k) = callcc ( fn k' => throw k (x, Chan k') );

fun rx c = sw () c
fun tx x c = #2 (sw x c)

fun source d =
let
val _ = print "source starting\n"
val d = tx "hel" d
val d = tx "lo" d
val d = tx " " d
val d = tx "world" d
val d = tx " " d
in "---" end

fun xduce d u =
let
fun xd acc d u =
let val _ = print "xducer receiving...\n"
val (x, u) = rx u
val _ = print ("xducer got (" ^ x ^ ")\n")
in
if x = " "
then xd "" (tx acc d) u
else xd (acc ^ x) d u
end
val _ = print "xduce starting\n"
in
xd "" d u
end

fun takeTwo c = let val _ = print "takeTwo starting\n"
val (x, c') = rx c
val (y, c'') = rx c' in x ^ " " ^ y end

(* f : ('a, 'b) chan -> 'x
g : 'a * ('b, 'a) chan -> 'x
comp f g : 'x
(starts with computation in f)
*)
fun ccomp f g =
callcc (fn k => g (callcc (fn u => throw k (f (Chan u)))));
(* u : ('a * ('b, 'a) chan) cont *)
(* Chan u : ('a, 'b) chan *)
(* f (Chan u) : 'x *)
(* k : 'x cont *)
(* throw k (f (Chan u)) : _|_ *)
(* callcc (fn u => throw k (f (Chan u))) : 'a * ('b, 'a) chan *)

(* f : ('a, 'b) chan -> 'x
g : ('b, 'a) chan -> 'x
comp f g : 'x
(starts with computation in f)
*)
fun comp f g = ccomp f (fn (_,c) => g c);

val n = comp takeTwo (fn d => comp (xduce d) source);

fun alice c = let val _ = print "blah\n" val (x, c) = sw 1 c val (y, c) = sw 2 c in ("alice", x, y) end;
fun bob (p, c) = let val _ = print "blah2\n" val (u, c) = sw (Int.toString p) c val (v, c) = sw (Int.toString u) c in ("bob", Int.toString u, Int.toString v) end;
val z = ccomp alice bob;```
Tags:
• Post a new comment

#### Error

Anonymous comments are disabled in this journal

default userpic