john hughes simon peyton jones philip wadler
play

John Hughes, Simon Peyton Jones, Philip Wadler December 2012 A - PowerPoint PPT Presentation

John Hughes, Simon Peyton Jones, Philip Wadler December 2012 A functional language Purely functional Lazy Statically typed Designed 1988-1990 For research, teaching, and practical use By a committee of


  1. John Hughes, Simon Peyton Jones, Philip Wadler December 2012

  2. ¡ A functional language ¡ Purely functional ¡ Lazy ¡ Statically typed ¡ Designed 1988-1990 ¡ For research, teaching, and practical use ¡ By a committee of academics

  3. WG2.8 1992

  4. WG2.8 1992

  5. Perl, Python Practitioners 1,000,000 10,000 100 C# Geeks Java, PHP Rails 1 Apr 1990 1995 2000 2005 2010 Sarah Michael Meg Haskell (the cat)

  6. Haskell the cat (b. 2002)

  7. http://redmonk.com Sept 2012

  8. StackOverflow, # of tags GitHub, # of projects

  9. Keep faith with a few deep, simple principles, and see where they lead § Purity § Domain specific languages § Types “People will gladly adapt to the limitations of a great design.” Don Box

  10. Purity

  11. Pure Spectrum (no effects) C, C++, Java, C#, VB Excel, Haskell X := In1 X := X*X X := X + In2*In2 Commands, control flow Expressions, data flow § Do this, then do that § No notion of sequence § “X” is the name of a cell § “A2” is the name of a that has different values (single) value at different times

  12. Pure Spectrum (no effects) C, C++, Java, C#, VB Excel, Haskell e d i S X := In1 e r a s t c e f X := X*X f e w X := X + In2*In2 o h n o i t a t u p m o c e n o d s i Commands, control flow Expressions, data flow § Do this, then do that § No notion of sequence § “X” is the name of a cell § “A2” is the name of a that has different values (single) value at different times

  13. ¡ I/O is a side effect. So side effects are part of the specification of what we want. Result Prolonged embarrassment

  14. ¡ Every call-by-value language has given into the siren call of side effects ¡ But in Haskell (print “yes”) + (print “no”) just does not make sense. Even worse is [print “yes”, print “no”] ¡ So effects (I/O, references, exceptions) are just not an option. ¡ Result: prolonged embarrassment . Stream-based I/O, continuation I/O... but NO DEALS WIH THE DEVIL

  15. No side effects reverse :: [Char] -> [Char] toUpper :: Char -> Char useless :: () -> () I/O effects getChar :: FileHandle -> IO Char launchMissiles :: IO () International Pure by default side effects Side effects where necessary

  16. Arbitrary effects Useful C No effects Haskell Useless Dangerous Safe

  17. Cost Parallelism Testing Maintenance Scale and complexity Time

  18. Plan A (everyone else) Arbitrary effects Nirvana Useful Plan B (Haskell) No effects Useless Dangerous Safe

  19. Domain specific languages Goal The program expresses as directly as possible what is the mind of the domain expert

  20. b1 = addDur dqn [b 3, fs 4, g 4, fs 4] b2 = addDur dqn [b 3, es 4, fs 4, es 4] b3 = addSur dqn [as 3, fs 4, g 4, fs 4] bassLine = timesM 3 b1 :+: timesM 2 b2 :+: timesM 4 b3 :+: timesM 5 b1

  21. ¡ An EMBEDDED domain-specific langauge is just a library, whose API embodies the domain knowledge ¡ 80% of the benefit for 20% of the effort ¡ Haskell is particularly good at this, because of types, laziness, syntax.

  22. Orchestration (Orc) Hardware description language Reactive animations (Lava) (Fran) Diagrams (disgrams- Workflow cairo) Financial contracts Data-parallel (Repa) Hard real-time applications (Atom) URLs, routes, MongoDB schema, database queries, Parsers (Parsec) HTML (Yesod) XML (HaXml) GPUs (Nicola, Test-case generation Accelerate) (Quickcheck)

  23. Types

  24. Static typing is by far the most widely-used program verification technology in use today: particularly good cost/benefit ratio ¡ Lightweight (so programmers use them) ¡ Machine checked (fully automated, every compilation) ¡ Ubiquitous (so programmers can’t avoid them)

  25. ¡ [Old hat] Types guarantee the absence of certain classes of errors: “well typed programs don’t go wrong” ¡ True + ‘c’ ¡ Seg-faults ¡ Types are a design language ; types are the UML of Haskell ¡ The BIGGEST MERIT (though seldom mentioned) of types is their support for software maintenance

  26. All programs Programs that are well typed Programs that work Region of Abysmal Pain

  27. All programs Programs that are well typed Programs that work Smaller Region of Abysmal Pain

  28. Type families, kind Haskell polymorphism etc GADTs Scala Haskell Type classes ML ML polymorphism + ...and Java, C# algebraic data types generics Simple types 1980 1990 2000 2010 1970

  29. Type families, kind polymorphism etc GADTs Haskell Type classes ML ML polymorphism + algebraic data types Simple types 1980 1990 2000 2010 1970

  30. Transactions in Haskell

  31. ¡ A web server ¡ Lots of independent, I/O-performing threads ¡ With shared state ¡ GHC’s runtime natively supports super- lightweight threads ¡ But: how to control access to shared state? ¡ Usual answer: locks and condition variables

  32. A 10-second review: § Races : due to forgotten locks § Deadlock : locks acquired in “wrong” order. § Lost wakeups: forgotten notify to condition variable § Diabolical error recovery : need to restore invariants and release locks in exception handlers § These are serious problems. But even worse...

  33. Scalable double-ended queue: one lock per cell No interference if ends “far enough” apart But watch out when the queue is 0, 1, or 2 elements long!

  34. Difficulty of concurrent Coding style queue Sequential code Undergraduate

  35. Difficulty of concurrent Coding style queue Sequential code Undergraduate Locks and Publishable result at condition international conference variables

  36. Difficulty of concurrent Coding style queue Sequential code Undergraduate Locks and Publishable result at condition international conference variables Atomic blocks Undergraduate

  37. atomically { ... sequential get code ... } § To a first approximation, just write the sequential code, and wrap atomically around it § All-or-nothing semantics: Atomic commit § Atomic block executes in Isolation A C I D § Cannot deadlock (there are no locks!) § Atomicity makes error recovery easy (e.g. exception thrown inside the get code)

  38. do { atomically (…increment Fred’s account …decrement Bill’s account…) ; print receipt ; launch missiles } Outside atomically Inside atomically Yes NO Input/output Deposit or withdraw NO Yes atomically :: STM a -> IO a TM effects only Arbitrary I/O effects

  39. ¡ Efficient : side effects are the exception, not the rule => efficient ¡ Secure ¡ type system (without modification) keeps STM effects separate from I/O effects ¡ no possibility of modifying transactional variables outside transactions ¡ Compositional : a little DSL for describing transactions atomically :: STM a -> IO a retry :: STM a orElse :: STM a -> STM a -> STM a throw :: Exception -> STM a

  40. ¡ Purity, supported by types, allows us to build a domain specific language for describing composable transactions. Haskell The world’s finest imperative programming language

  41. newRef :: a -> IO (Ref a) readRef :: Ref a -> IO a writeRef :: Ref a -> a -> IO () print :: Int -> IO () Reads and main = do { r <- newRef 0 ; incR r writes are 100% ; s <- readRef r explicit! ; print s } You can’t say incR :: Ref Int -> IO () (r + 6), because incR r = do { v <- readRef r r :: Ref Int ; writeRef r (v+1) }

  42. forkIO :: IO () -> IO ThreadId § forkIO spawns a thread § It takes an action as its argument webServer :: RequestPort -> IO () webServer p = do { conn <- acceptRequest p ; forkIO (serviceRequest conn) ; webServer p } serviceRequest :: Connection -> IO () serviceRequest c = do { … interact with client … } No event-loop spaghetti!

  43. § How do threads coordinate with each other? main = do { r <- newRef 0 ; forkIO (incR r) ; incR r ; ... } Aargh! A race incR :: Ref Int -> IO () incR r = do { v <- readRef r ; writeRef r (v+1) }

  44. atomically :: STM a -> IO a newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM () incT :: TVar Int -> STM () incT r = do { v <- readTVar r; writeTVar r (v+1) } main = do { r <- atomically (newTVar 0) ; forkIO (atomically (incT r)) ; atomic (incT r) ; ... }

  45. Purity ¡and ¡Tes.ng ¡ Just ¡does ¡what ¡ Does ¡ NOT ¡read ¡ it ¡says ¡on ¡the ¡.n any ¡global ¡ —repeatably ¡ variables ¡ reverse [1,2,3] == [3,2,1] Does ¡ NOT ¡ Does ¡ NOT ¡ modify ¡any ¡ modify ¡its ¡ global ¡state ¡ argument ¡

  46. Purity ¡and ¡Proper.es ¡ Pure ¡func.ons ¡ They ¡ma=er! ¡ have ¡nice ¡ Jus.fy ¡ proper.es ¡ op.miza.ons ¡ reverse (reverse xs) == xs (xs ++ ys) ++ zs == xs ++ (ys ++ zs) Library ¡func.ons: ¡ What ¡about ¡ new ¡ proper.es ¡are ¡ func.ons? ¡ well-­‑known ¡

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