Skip to content

Transient and the Actor Model (Erlang OTP, Cloud Haskell, Scala Akka)

Alberto edited this page Nov 8, 2016 · 6 revisions

Asked here: https://news.ycombinator.com/item?id=11486401

Good question. The actor model communicate via mailboxes, which execute callbacks sort to say. Essentially this produces the callback hell. Taking away the initializations, this is the ping pong example in Erlang: https://gist.github.com/torgeir/3978785

    ping(Receiver) ->
      { pong, Receiver } ! { ping, self() },
      receive
        pong -> io:format("Got pong!~n")
    end.

    pong() ->
      receive

        finished ->
          io:format("Pong finished~n");

        { ping, Sender } ->
          io:format("Got ping!~n"),
          Sender ! pong,
          pong()
      end.

This is the same in Transient: A message is sent to the receiver print "ping" in his console, then send a message to the sender, that print "pong" in his console

    pingPong receiver=  
      wormhole receiver pingPong'

    pingPong'= do
          teleport
          localIO $ print "ping"
          teleport
          localIO $ print "pong"
          pingPong'

You see that the ping and the pong are composed "monadically" in a single piece of code. The flow is quite understandable. Just start the program in both nodes (initialization code is not shown) This is a distributed program but I can compose this program with any other. This other program stream integers from a remote node from 1 in increasing order:

    streamFrom node= 
       wormhole  node $ do
                  teleport
                  r <- threads 1 $ choose[1..]
                  threadDelay 1000000
                  teleport
                  return r

I can compose both distributed programs:

    composed node =  do
         r <-  (pingPong node >> return 0) <|>  streamFrom node
         lliftIO $ case r of
              0 -> putStrLn "pong was received"
              n -> print n

composed print the numbers received and the "pong" messages followed by "pong was received" in the console of the caller.

It is possible, however, to communicate in Transient using the actor model and still maintain composability. Mailboxes exist in transient and can be read by any thread in the node. Mailboxes permits a thread to communicate with other threads across the node and across different nodes. To write in the mailbox of other node:

putRemoteMailBox mbox node dat= wormhole node . atRemote $ putMailBox mbox dat 

atRemote is defined as a "sandwitch" of teleports with the remote action in the middle.

This example is a chat that uses mailboxes.

Clone this wiki locally