on the BEAM
Developer @ Erlang Solutions
Started working on Caramel
around early September,
as an experiment.
Why is it so hard to
type-check Erlang?
I had the idea to start with a language that was already well-typed and cram it into Erlang.
The thesis?
The resulting subset of Erlang is also well-typed.
To give us its benefits, OCaml sets some constraints:
My first attempts were small programs.
Then came in some more complex state machines. In this case look at the phantom type of the state indicating its validity through the abstract types "valid" and "invalid".
So far these attempts had been typing code in a single process...
...could this scale to multiple processes?
Assuming that there is no code reloading, and that we know the exact type of messages that a process can receive, we can limit the "send" function to only allow messages of the type a process is known to receive.
It goes without saying, this is not a new discovery, and there exists plenty of research on the subject that I'm still getting familiar with!
type 'a t
(** The type of channels *)
(** ... *)
val send : 'a t -> 'a -> unit
(** [send c v] sends the values [v] over the channel [c]. If the channel buffer
* is full then the sending domain blocks until space becomes available. *)
(** ... *)How do we know what type of messages a process can receive?
By observing what are the types of messages the receive expression is expected to return, we can build up the type of messages that this process can handle.
f() ->
receive
{ok, Number} -> Number + 1
end.let f recv =
match recv ~timeout:Infinity with
| `Ok number -> number + 1Because number is being added to 1, and it came from the Ok tuple, then the receive expression must return values of type:
[ `Ok of int ] (** ok tuples with an integer *)recv is expected to return:
[ `Add of int
| `Hello of string
| `Reset
]3 process types:
"a", can receive messages of type [ `Call (int pid, int) ]
"b", can receive messages of type int (just numbers, not much)
"c", can receive messages of type bool
It looked like I could successfully compile a strict subset of OCaml right into somewhat idiomatic Erlang that seemed to work!
The next step was right on the nose.
If I can go from OCaml to Erlang,
can I come back from Erlang to OCaml while preserving this type information?
For a strict subset of Erlang,
it seems that we can!
Still a lot of research to be done in the area, but the results so far look promising.
I'm continuing to work with a lot of energies, thanks to