Skip to content

Processes

A receiving function declares its complete mailbox with receives. Typed Pids are invariant in their message type. Native receive handles selected messages; unmatched messages remain queued. process.self needs the declared mailbox.

Gale uses BEAM processes with typed messages. A receiving function declares its mailbox type, and Pid<M> identifies a process that accepts messages of type M. The compiler checks Gale sends against that type.

The mailbox type is part of the function’s signature. This server accepts Ping(reply_to) or Stop, and nothing else compiles against it:

mod processes
alias gale_std.process
pub type Message = Ping(process.Pid<:pong>) | Stop
fn loop() receives Message -> :ok {
receive {
Ping(reply_to) -> {
process.send(reply_to, :pong)
loop()
}
Stop -> :ok
}
}
pub fn start() -> process.Pid<Message> {
process.spawn(fn() receives Message -> loop())
}
pub fn round_trip(server: process.Pid<Message>) receives :pong -> :pong {
process.send(server, Ping(process.self()))
receive { :pong -> :pong }
}

process.Pid<M> is invariant in its message type: a pid for Integer messages is not a pid for Term messages, in either direction.

A typed pid can erase to AnyPid when you only need identity, but erasure forgets the mailbox, the service and any liveness claim, and no runtime check recovers them.

receive matches like case, with two differences that matter. It need not be exhaustive; unmatched messages stay queued for later. A pinned pattern is the idiomatic reply filter:

mod replies
alias gale_std.process
pub fn await(tag: process.Ref) receives {process.Ref, Integer} -> Option<Integer> {
receive {
{^tag, value} -> Some(value)
after 5_000 -> None
}
}

A message with a different tag does not match and remains queued; add an explicit _ -> ... arm only when you mean to consume stragglers. after 0 polls; native receive accepts nonnegative milliseconds up to 4,294,967,295 or :infinity. The Timeout alias alone doesn’t enforce that bound: invalid literals are errors, and dynamic values need validation or a proved domain.

Monitor and exit notifications are ordinary tuples, and the stdlib’s Down and ExitEvent are aliases for them. Match the tuples directly, pinning the parts you care about:

mod monitor_messages
alias gale_std.process
pub fn await_down(worker: AnyPid, monitor: process.Monitor)
receives process.Down -> Boolean {
receive {
{:DOWN, ^monitor, :process, ^worker, _} -> true
after 1_000 -> false
}
}
pub fn await_exit(worker: AnyPid) receives process.ExitEvent -> Boolean {
receive {
{:EXIT, ^worker, _} -> true
after 1_000 -> false
}
}

The lifecycle API wraps native operations with typed results:

  • process.flag(:trap_exit, true) returns the previous setting; spawn_monitor creates child and monitor atomically, returning {Pid<M>, Monitor}; demonitor keeps native default/:flush/:info options through a checked error boundary.
  • A matching {:DOWN, ...} tuple can be forged by any sender. Monitors are bookkeeping, not authentication, and never proof of survival.
  • send_after returns checked Timer results with native limits (TimerError is :invalid_argument for a native-invalid delay or a nonlocal pid; a dead local target is not an error). cancel_timer returns Option<Integer>, including Some(0). A None result proves nothing about receipt. Timers carry no message-type parameter and no exactly-once delivery theorem.
  • process.sleep(timeout) takes :infinity or a nonnegative Integer (chunked internally, so above the receive bound is fine); its precondition must be proved for dynamic arguments. Neither verification nor :infinity promises termination.

The mailbox declaration is an assumption about senders: all senders, including foreign Elixir code, must respect it. Gale checks Gale callers against it. An ordinary loop may run forever; proofs cover normally completed local transitions, payload typing and pattern facts. Delivery, fairness, caller survival, monitor authenticity and future schedules are outside the guarantee. The verification reference states the exact scope.