Caramel: An OCaml to Erlang Compiler
caramel.abstractmachines.dev
caramel.abstractmachines.dev
Where does OCaml end and Erlang begin?
1. If you have a function that calls into other modules like `other_module:some_func(X)`, then the typing of this call can change at runtime. You could first have
some_func(N) when is_number(N) -> N + 1.
and later load a new version with some_func(N) when is_integer(N) -> N + 1.
The older version accepts all numbers, i.e. floats and integers, while the second only accept integers.2. Erlang message passing can not be typed statically in a meaningful (i.e. non-trivial) way, as the typing may be state-dependent:
Pid = spawn_link(fun () ->
receive
first -> ok;
_ -> error(bad_message)
end,
receive
second -> ok;
_ -> error(bad_message)
end
end)
Now, the static typing of sending to `Pid` depends on what you've sent before. This could maybe be handled by "enumerating" the receives, but I think that invites the halting problem into your type system.Writing down the PEG message grammar for the process becomes quite easy. Pattern matching is well-defined. The number of states may be large, but can often be factored nicely by data types and ranges (e.g. tagged tuples). However, PEG semantics make it difficult to analyze composition of receive statements in the general case.
It's easy to solve the Halting Problem in Erlang - just add a timeout to your receive and accept some false positives :)
If it fails to prove a type for a value, it falls back to "this could be any type" which brings this part of the code back to pure dynamic typing.
Really stoked to give this a whirl!
[1] I know, and in-case others don't, almost no one (literally) uses hot-code reloading. Still... Instead of killing it, as in the case of Lumen (I think), I'd like to see it become easier/better...
I would wager that nearly everyone who has run Erlang code in production has upgraded code (edit for precision: a module) live.
To engineer the entire application to support an upgrade, and to do all the testing required to make sure it will work correctly, is very resource-intensive, and (lacking real numbers, just from observing the industry working for Basho) I’d wager very few people do so.
There is a common misconception that 'no one on HN does something' means that 'no one does something'. HN is heavily biased towards startups and Web companies and large segments of the industry are very underrepresented. Pretty sure telecoms is one.
Why not? If you're doing hot code reloading, the types all of the functions and receive expressions expect still have to be compatible, otherwise the code will fail at runtime. Static type checking just lets you know about those problems ahead of time.
I could be wrong but I thought phoenix uses this when you're in dev.
https://gist.github.com/macintux/6349828#alternative-languag...
For Rust there is:
https://docs.rs/actix/0.10.0/actix/
and Lunatic (which compiles to WebAssembly): https://news.ycombinator.com/item?id=25160474
(edit: added Actix and Lunatic, formatting)