an abstract machine for concurrent haskell with futures
play

An Abstract Machine for Concurrent Haskell with Futures David Sabel - PowerPoint PPT Presentation

An Abstract Machine for Concurrent Haskell with Futures David Sabel Goethe-University, Frankfurt am Main, Germany ATPS12, Berlin, Germany 1 Introduction Calculus CHF Equivalence Abstract Machines Conclusion Motivation Concurrent


  1. An Abstract Machine for Concurrent Haskell with Futures David Sabel Goethe-University, Frankfurt am Main, Germany ATPS’12, Berlin, Germany 1

  2. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Motivation Concurrent Haskell (Peyton Jones, Gordon, Finne 1996) extends Haskell by concurrency The process calculus CHF (S.,Schmidt-Schauß 2011) models Concurrent Haskell with Futures operational semantics inspired by (Peyton Jones, 2001) Futures allow a declarative programming style for concurrency − Future = Variable whose value becomes available in the future − Our futures are concurrent, imperative, implicit − implicit synchronisation by data dependency do x1 ⇐ e1 x1 <- future e1 | x2 ⇐ e2 x2 <- future e2 main some actions ⇐ = = do some actions | print(x1+x2) print (x1+x2) David Sabel An Abstract Machine for Concurrent Haskell with Futures 2/18

  3. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Issues Operational semantics of CHF given in (S.,Schmidt-Schauß 2011) Appropriate for mathematical reasoning on contextual equivalence w.r.t. may- and should-convergence but its definition is complex not obvious how to implement In this work: Design an abstract machine for CHF based on the machines of (Sestoft 1997) for lazy evaluation which can be implemented easily (prototype exists) show correctness of the abstract machine w.r.t. may- and should-convergence David Sabel An Abstract Machine for Concurrent Haskell with Futures 3/18

  4. Introduction Calculus CHF Equivalence Abstract Machines Conclusion The Process Calculus CHF: Syntax Processes P, P i ∈ Proc ::= P 1 | P 2 | νx.P | x ⇐ e | x = e | x m e | x m − A process has a main thread: x main ⇐ = = e | P Expressions & Monadic Expressions e, e i ∈ Expr ::= me | x | λx.e | ( e 1 e 2 ) | seq e 1 e 2 | c e 1 . . . e ar( c ) | case T e of . . . ( c T,i x 1 . . . x ar( c T,i ) → e i ) . . . | letrec x 1 = e 1 . . . x n = e n in e me ∈ MExpr ::= return e | e 1 > >= e 2 | future e | takeMVar e | newMVar e | putMVar e 1 e 2 Types τ, τ i ∈ Typ ::= ( T τ 1 . . . τ n ) | τ 1 → τ 2 | IO τ | MVar τ Standard monomorphic type system David Sabel An Abstract Machine for Concurrent Haskell with Futures 4/18

  5. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Operational Semantics CHF Operational Semantics: Reduction P 1 − − → P 2 CHF Small-step reduction − − → Rules are closed w.r.t. structural congruence and process contexts Reduction rules for monadic computation and functional evaluation Some rules: CHF (fork) x ⇐ M [ future e ] − − → νy. ( x ⇐ M [ return y ] | y ⇐ e ) , y fresh CHF − − → νx. ( L [ e 1 ] | x = e 2 ) (lbeta) L [(( λx.e 1 ) e 2 )] L -contexts: L ::= x ⇐ M [ F ] | x ⇐ M [ F [ x n ]] | x n = E n [ x n − 1 ] | . . . | x 2 = E 2 [ x 1 ] | x 1 = E 1 evaluation contexts: E ::= [ · ] | ( E e ) | ( case E of alts ) | ( seq E e ) forcing contexts: F ::= E | ( takeMVar E ) | ( putMVar E e ) David Sabel An Abstract Machine for Concurrent Haskell with Futures 5/18

  6. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Program Equivalence Process P is successful if P well-formed ∧ P ≡ ν − → x i ( x main ⇐ = = return e | P ′ ) May-Convergence : (a successful process can be reached by reduction) Process P : P ↓ iff P is w.-f. and ∃ P ′ : P CHF , ∗ → P ′ ∧ P ′ successful − − − Expression e :: IO τ : e ↓ iff x main ⇐ = e ↓ = Should-Convergence : (every successor is may-convergent) Process P : P ⇓ iff P is w.-f. and ∀ P ′ : P CHF , ∗ → P ′ = − − − ⇒ P ′ ↓ Expression e :: IO τ : e ⇓ iff x main ⇐ = = e ⇓ Contextual Equivalence P 1 ∼ c P 2 iff ∀ D : ( D [ P 1 ] ↓ ⇐ ⇒ D [ P 2 ] ↓ ) ∧ ( D [ P 1 ] ⇓ ⇐ ⇒ D [ P 2 ] ⇓ ) David Sabel An Abstract Machine for Concurrent Haskell with Futures 6/18

  7. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Simplified Expressions and Processes Processes P, P i ∈ Proc P 1 | P 2 | νx.P | x ⇐ e | x = e | x m e | x m − ::= Expressions & Monadic Expressions e, e i ∈ Expr ::= me | x | λx.e | ( e 1 e 2 ) | seq e 1 e 2 | c e 1 . . . e ar( c ) | case T e of . . . ( c T,i x 1 . . . x ar( c T,i ) → e i ) . . . | letrec x 1 = e 1 . . . x n = e n in e me ∈ MExpr ::= return e | e 1 > >= e 2 | future e | takeMVar e | newMVar e | putMVar e 1 e 2 David Sabel An Abstract Machine for Concurrent Haskell with Futures 7/18

  8. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Simplified Expressions and Processes Simplified Processes P, P i ∈ Proc ::= P 1 | P 2 | νx.P | x ⇐ e | x = e | x m y | x m − Simplified Expressions & Monadic Expressions e, e i ∈ Expr ::= me | x | λx.e | ( e 1 x ) | seq e 1 x | c x 1 . . . x ar( c ) | case T e of . . . ( c T,i x 1 . . . x ar( c T,i ) → e i ) . . . | letrec x 1 = e 1 . . . x n = e n in e me ∈ MExpr ::= return x | x 1 > >= x 2 | future x | takeMVar x | newMVar x | putMVar x 1 x 2 David Sabel An Abstract Machine for Concurrent Haskell with Futures 7/18

  9. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Constructing the Abstract Machine Modular construction in three steps: M1 IOM1 CIOM1 evaluates pure expressions deterministic treats monadic operators like data constructors slight modification of Sestoft’s mark 1 David Sabel An Abstract Machine for Concurrent Haskell with Futures 8/18

  10. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Constructing the Abstract Machine Modular construction in three steps: M1 IOM1 CIOM1 adds storage (MVars) monadic operations are executed uses M1 for purely functional subevaluations single-threaded David Sabel An Abstract Machine for Concurrent Haskell with Futures 8/18

  11. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Constructing the Abstract Machine Modular construction in three steps: M1 IOM1 CIOM1 Concurrent threads (futures) nondeterministic Globally shared bindings and MVars uses IOM1 for thread-local evaluation David Sabel An Abstract Machine for Concurrent Haskell with Futures 8/18

  12. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Machine M1 State ( H , e, S ) H is a heap : a set of shared bindings x �→ e e is the currently evaluated expression S is a stack (holding the evaluation context) entries: # app ( x ) , # seq ( x ) , # case ( alts ) , # heap ( x ) Start state : For expression e : ( ∅ , e, []) Final state : ( H , v, []) where v = λz.e , v = ( c . . . ) , or v a monadic expression David Sabel An Abstract Machine for Concurrent Haskell with Futures 9/18

  13. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Machine M1 : Transitions Unwinding M1 (pushApp) ( H , ( e x ) , S ) − → ( H , e, # app ( x ) : S ) M1 (pushSeq) ( H , ( seq e x ) , S ) − → ( H , e, # seq ( x ) : S ) M1 (pushAlts) ( H , case T e of alts, S ) − → ( H , e, # case ( alts ) : S ) M1 ∪ � n (mkBinds) ( H , letrec { x i = e i } n → ( H · i =1 in e, S ) − i =1 { x i �→ e i } , e, S ) M1 ( H · ∪{ y �→ e } , y, S ) − → ( H , e, # heap ( y ) : S ) (enter) Evaluation M1 (takeApp) ( H , λx.e, # app ( y ) : S ) − → ( H , e [ y/x ] , S ) M1 (takeSeq) ( H , v, # seq ( y ) : S ) − → ( H , y, S ) , if v = λz.e or v = c . . . ( H , ( c − → x i ) , # case ( . . . ( c − → M1 → ( H , e [ x i /y i ] n y i → e ) . . . ) : S ) − i =1 , S ) (branch) M1 ( H , v, # heap ( y ) : S ) − → ( H · ∪{ y �→ v } , v, S ) (update) if v = λz.e , v = ( c . . . ) , v = x , or v a monadic operator David Sabel An Abstract Machine for Concurrent Haskell with Futures 10/18

  14. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Example ( ∅ , letrec x 1 = ( λy.y ) w, x 2 = takeMVar x 1 in (( λz.z ) x 2 ) , []) M1 , mkBinds − − − − − → ( { x 1 �→ ( λy.y ) w, x 2 �→ takeMVar x 1 } , ( λz.z ) x 2 , []) M1 , pushApp − − − − − → ( { x 1 �→ ( λy.y ) w, x 2 �→ takeMVar x 1 } , λz.z , [ # app ( x 2 )]) M1 , takeApp − − − − − → ( { x 1 �→ ( λy.y ) w, x 2 �→ takeMVar x 1 } , x 2 , []) M1 , enter − − − − → ( { x 1 �→ ( λy.y ) w } , takeMVar x 1 , [ # heap ( x 2 )]) M1 , update − − − − − → ( { x 1 �→ ( λy.y ) w, x 2 �→ takeMVar x 1 } , takeMVar x 1 , []) David Sabel An Abstract Machine for Concurrent Haskell with Futures 11/18

  15. Introduction Calculus CHF Equivalence Abstract Machines Conclusion Machine IOM1 M1 -state: ( H , e, S ) State ( H , M , e, S , I ) H is a heap e is the currently evaluated expression S is a stack (holding the evaluation context) M is a set of MVars : filled x m y , empty x m − I is an IO-stack (holding the monadic context) entries: # take , # put ( x ) , # > >= ( x ) Start state : For expression e :: IO τ : ( ∅ , ∅ , e, [] , []) Final state : ( H , M , return x, [] , []) David Sabel An Abstract Machine for Concurrent Haskell with Futures 12/18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend