When we started EXE we didn't know the landscape of ways we could implement interoperability with Erlang runtime. From the first, it seems reasonable to break all the compatibility and build the new world. But what about thinking of building reasonable type checked code that produces OTP compatible gen_server?
To show you that way we understand gen_server first we need to reimplement poor man's OTP-compatible gen_server in Erlang, by the way showing the session types and typed handler of gen_server.
We at Synrc are not using much OTP. First, we found that OTP principles, in general, are OK, but for simple services all those handle_call, handle_info necessary declarations seems a bit extra. We have irreplaceable application_controller, complicated user_drv layer, and more legacy stuff that prevent us from building modern and flexible functional codebase. E.g. we have the use case in mad for loading the application from escript bundle and ETS filesystem and even made a custom static handler for that purposes.
The reason to rethink OTP ideas is not only NIH syndrome. We also want to see minimal primitives to specify gen_server more formally in its distilled form. Others may take into account that this is toy sample.
The basic idea of gen_server is to be run and to be reacted during life-time to a series of ordered events. You may think of gen_server as simple stream data type of a given state and also typed with an effectfull process.
The very first gen_server in ERTS and LING is application_controller, that starts from init module. It starts even without proc_lib and enters gen_server loop manually.
NOTE: init message is our public version of ack. We use init for reinitialization even without stopping the process. But instead of gen_server for N2O protocols we use n2o_async interfaced with proc/2 message and state parameters.
We can make it gen_server-compatible by placing two lines in init
Here is the hardest part for reasoning. We give computing control to Erlang VM which calls hibernate function in the gen_server module.
Hibernate, Infinity or Timeout:
Here is happened computational emptiness and all sources of effects.
You may think of receive as effectful suspended case.
Wake up from Hibernate state here:
Session Types in EXE
Note that init is added by me, because it always needed a place for reinit without stopping the process.
Effects could be stored as persisted trace of ok and/or error events in KVS. We could allow process to continue on errors, etc.
Loop is waiting for data in drain and pass it to Server which can handle only tuple/3 with following first elements: EXIT, timeout, system, $gen_call, $gen_cast, init. After receiving message the server will call appropriate function in a given sen_server module.
OTP reply is a tuple/2:
There is a trick in OTP to handle Any messages built into protocol which is not good for reasoning. We will convert them to cast messages (or better deny totally).
Here is new callback module, that eliminates gen_server behaviour callbacks, but allows raw protocol messages naming.
Callback Module in EXE
In general it is possible to compile such EXE record in OTP-compatible Erlang code.
Typed Free Structure can handle the stream.