Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Erlang meets Dependent Types

Erlang meets Dependent Types

Talk given at SPLS St Andrews, June 2015.

Dissertation information: http://lenary.co.uk/publications/dissertation/

Sam Elliott

June 15, 2015
Tweet

More Decks by Sam Elliott

Other Decks in Research

Transcript

  1. Erlang & Concurrency Dependent Types Example Compiler Conclusion Erlang •

    Actor-based Concurrent Programming Language • !,receive, and spawn • Mailbox per Process • Isolated Processes • Dynamically Typed • dialyzer • OTP: Behaviours Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  2. Erlang & Concurrency Dependent Types Example Compiler Conclusion Idris •

    Dependently-typed Programming Language • “Pacman complete” — Brady • Types are Values and Values are Types • IR-Happy! Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  3. Erlang & Concurrency Dependent Types Example Compiler Conclusion A Problematic

    Program 1 -module(lock_srv ). -behaviour(gen_server ). handle_call(lock , _From , unlocked) → {reply , grant , locked}; 6 handle_call(lock , _From , locked) → {reply , wait , locked}; handle_call(unlock , _From , _) → {reply , ok , unlocked}. 11 %% Other gen_server callbacks elided Listing 1: Simple Concurrent Lock Service in Erlang using gen_server. Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  4. Erlang & Concurrency Dependent Types Example Compiler Conclusion In Idris?

    1 module Locks data LockReq = Lock | Unlock 4 data LockLockResp = Grant | Wait total LockResp : LockReq → Type LockResp Lock = LockLockResp 9 LockResp Unlock = Unit data LockSt = Locked | Unlocked total 14 lock : LockSt → (m : LockReq) → (LockResp m, LockSt) lock Locked Lock = (Wait , Locked) lock Unlocked Lock = (Grant , Locked) lock _ Unlock = ((), Unlocked) Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  5. Erlang & Concurrency Dependent Types Example Compiler Conclusion RPC Framework

    1 data RPCLang : (r : Type) 2 → (r → Type) → Type where MkRPCLang : {req : Type} → {resp : (req → Type )} → RPCLang req resp 7 data RPCRef : RPCLang _ _ → Type where MkRPCRef : Ptr → RPCRef l rpc : {l : RPCLang req resp} 12 → RPCRef l → (m : req) → EIO (resp m) Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  6. Erlang & Concurrency Dependent Types Example Compiler Conclusion Generic Processes

    1 data Process : Type → Type∗ → Type data ProcRef : Type → Type 6 spawn : Process l’ a → Process l (ProcRef l’) receive : Process l l 11 send : ProcRef l’ → l’ → Process l () 16 become : Process l a → (a → Process l’ b) → Process l’ b Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  7. Erlang & Concurrency Dependent Types Example Compiler Conclusion And Back

    Up To OTP 1 data GSL : (cl : Type) → (cl → Type) → Type → Type 3 GSP : Type → Type data GSRef : (GSL _ _ _) → Type 8 call : {l : GSL cl cr ct} → GSRef l → (m : cl) → GSP (cr m) 13 cast : {l : GSL _ _ ct} → GSRef l → ct → GSP Unit Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  8. Erlang & Concurrency Dependent Types Example Compiler Conclusion And Becoming

    Too Dependent 1 data GFL : (st : Type) → (se : Type) → (se → st → Type) → Type → Type where MkGFL : (st : Type) → (se : Type) → (ser : (se → st → Type )) 6 → (e : Type) → GFL st se ser e GFP : Type → Type data GFRef : (GFL _ _ _ _) 11 → Type where MkGFRef : {l : GFL _ _ _ _} → ErlPid → GFRef l 16 sync_send_event : {l : GFL st se ser e} → GFRef l → (ev : se) → GFP ??? Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  9. Erlang & Concurrency Dependent Types Example Compiler Conclusion Idris IRs

    Idris has several IRs, but only some for Code Generation • Lambda Terms • Defunctionalised Terms • Applicative Normal Form • Bytecode Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  10. Erlang & Concurrency Dependent Types Example Compiler Conclusion Defunctionalised IR

    Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types
  11. Erlang & Concurrency Dependent Types Example Compiler Conclusion Conclusion •

    We can produce verified concurrent programs in Idris • We can compile these programs to work with Erlang • We can run verified programs in Erlang Archibald Samuel Elliott, Edwin Brady Erlang meets Dependent Types