---
title: Std.Actor
---

Concurrency primitives for the BEAM actor model.
Provides process spawning, message passing, monitoring, linking, and timers.

## Types

### Pid

```saga
type Pid msg =
  | Pid Int
  deriving (Eq, Debug)
```

Process identifier, parameterized by the message type the process accepts.
At runtime this is an Erlang pid. The constructor is exposed so handler
authors can build alternative implementations (e.g. sync handlers for testing).

### ExitReason

```saga
type ExitReason =
  | Normal
  | Shutdown
  | Killed
  | Noproc
  | Error String
  | Other String
```

Exit reasons for monitored processes

### SystemMsg

```saga
type SystemMsg =
  | Down (Pid a) ExitReason
  | Exit (Pid a) ExitReason
```

System messages delivered to the mailbox by the runtime (e.g. monitor notifications).
These can be matched in receive blocks alongside regular messages.

### MonitorRef

```saga
opaque type MonitorRef
```

### TimerRef

```saga
opaque type TimerRef
```

Timer effect: delays and scheduled messages

## Effects

### Process

```saga
effect Process {
  fun spawn : (f: Unit -> Unit needs {Actor msg, ..e}) -> Pid msg needs {Actor msg, ..e}
  fun send : (pid: Pid msg) -> (msg: msg) -> Unit
  fun exit : (pid: Pid msg) -> ExitReason -> Unit
}
```

Type variable `msg` is free per call site, inferred from the pid / spawned function.

### Actor

```saga
effect Actor msg {
  fun self : Unit -> Pid msg
}
```

Actor effect: parameterized by the current process's message type.
Only self and receive use this (scoped to this process's mailbox).

### Monitor

```saga
effect Monitor {
  fun monitor : (pid: Pid msg) -> MonitorRef
  fun demonitor : (ref: MonitorRef) -> Unit
}
```

Monitor processes and get notified when they die

### Link

```saga
effect Link {
  fun link : (pid: Pid msg) -> Unit
  fun unlink : (pid: Pid msg) -> Unit
}
```

Bidirectional crash propagation between processes

### Timer

```saga
effect Timer {
  fun sleep : (ms: Int) -> Unit
  fun send_after : (pid: Pid msg) -> (ms: Int) -> (msg: msg) -> TimerRef
  fun cancel_timer : (ref: TimerRef) -> Unit
}
```

## Handlers

### beam_actor

```saga
handler beam_actor for Process, Actor msg, Monitor, Link, Timer
```

Native handler for BEAM effects

