Getting Started
This page builds a counter actor, talks to it, and tests it. It takes a few minutes and needs only the Aleph compiler.
Installation
Install the Aleph compiler (aleph) from MLambda.Aleph’s installers: on Linux install.sh, on Windows
install.ps1. Then make a project:
aleph new counter
cd counter
Add MLambda.Actor as a dependency in the project’s aleph.toml, pointing at your checkout:
[package]
name = "counter"
version = "0.1.0"
src = "src"
main = "Main"
[dependencies]
mlambda-actor = { path = "../MLambda.Actors/MLambda.Actor" }
Creating your first actor
An actor is a class: a data type holds the actor’s state, and its instance of the Actor trait holds its
methods. Its messages are a data type of their own; a message that wants an answer carries a Reply.
Create src/Counter.ai:
import Prelude
import Data.Either
import Actor.Types
import Actor.Class
import Actor.Behavior
import Actor.Address
data Counter = Counter { count :: Int }
data CounterMsg = Increment | Current (Reply Int)
@route("counter")
instance Actor Counter where
type Message Counter = CounterMsg
receive ctx c msg = case msg of
Increment -> continue c { count = c.count + 1 }
Current r -> respond r c.count >> continue c
instance Initial Counter where
initial params = Counter 0
counterOf :: ActorSystem -> Address Counter
counterOf system = actorOf system (parameters [])
type Message Counter = CounterMsgnames the messages the class accepts.receiveis called once per message, with the actor’s context, its current state and the message. It returns a step:continuewith the next state, orbecome,stash,stop,failWith(see Lifecycle).respond r valueanswers an ask.>>runs the answer, then the step.instance Initial Countersays how a counter is made from parameters alone.actorOfneeds it.
Spawning actors
Everything starts with an actor system. actorOf makes an actor of a class under /user:
import Actor.System as Sys
let system = Sys.actorSystem () in
let counter = counterOf system in
...
The address is typed by its class (Address Counter), so the compiler only lets you send a CounterMsg.
Sending messages
There are two ways:
-- tell: the message is delivered; nothing comes back
tell counter Increment
-- ask: the message is made with the reply it is answered on; you wait for the answer
case ask counter (\r -> Current r) of
Right n -> putStrLn (showInt n)
Left (Fault kind detail) -> putStrLn kind
An ask’s answer is Either Fault r. It is Left when the actor refused (refuse r fault), failed on the message,
or was already stopped. Messages from one sender arrive in order, so a tell followed by an ask sees the tell’s
effect.
Full example
src/Main.ai:
import Prelude
import Data.Either
import Actor.Types
import Actor.Class
import Actor.System as Sys
import Counter
main :: Int
main = case countedTwice (Sys.actorSystem ()) of
text -> case putStrLn text of
_ -> 0
countedTwice :: ActorSystem -> String
countedTwice system =
let counter = counterOf system in
tell counter Increment >> tell counter Increment >> case ask counter (\r -> Current r) of
Right n -> strcat "count = " (showInt n)
Left (Fault kind detail) -> strcat "failed: " kind
aleph run
# count = 2
Testing it
Specs live under test/; test/Main.ai lists the suites and aleph test runs them:
-- test/CounterSpec.ai
import Prelude
import Data.Either
import Test
import Test.Assert
import Actor.Class
import Actor.System as Sys
import Counter
countAfter :: Int -> Address Counter -> Int
countAfter n counter = case n of
0 -> case ask counter (\r -> Current r) of
Right c -> c
Left f -> 0 - 1
_ -> tell counter Increment >> countAfter (n - 1) counter
counterSuite :: Suite
counterSuite = suite "counter"
[ Test "three increments count three" (\_ -> assertEqualInt 3 (countAfter 3 (counterOf (Sys.actorSystem ()))))
]
-- test/Main.ai
import Test
import CounterSpec
main = reportSummary (runSuites [counterSuite])
aleph test
# > counter
# ok three increments count three
# summary: passed=1 failed=0
Building an executable
aleph build -o counter # on Windows: -o counter.exe
The result is one native executable with the runtime inside it. For Linux containers, see Deployment.
Where next
- Actors: classes, attributes, refusals, actors made with their state.
- Addresses & Routes: route templates and parameters.
- Topology & Hosting: the same actor in a cluster.