Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Docase notation for Haskell

1,288 views

Published on

Slides from a brief presentation about the 'docase' notation that I did at Haskell Hackathon in Cambridge. The notation makes it easier to work with monads that have some additional operations (such as Par monad or Parsers).

Published in: Technology, Education
  • Be the first to comment

Docase notation for Haskell

  1. 1. Monadic pattern matching with ‘docase’<br />Tomáš Petříček(tomas.petricek@cl.cam.ac.uk)University of Cambridge, UK<br />Advisors: Alan Mycroft, Don Syme<br />
  2. 2. Introduction<br />Warm Fuzzy Things (aka Monads)<br />Sequencing of effectful computations<br />Used for parallel & concurrent programming <br />Extend monad interface with additional operations<br />spawn :: Par a -> Par (IVar a) get :: Ivar a -> Par a<br />What are common additional operations?<br />Is there nice notation for using them?<br />
  3. 3. Multiplying future values<br />Future values<br />Compute resultin background<br />Pattern matching<br />“a” waits for a value<br />Suspended computation<br />Run both<br />multiply f1 f2 =<br />docase(f1, f2) of<br />(a, b) -> return (a * b)<br />
  4. 4. Multiplying future values<br />Future values<br />Compute resultin background<br />Pattern matching<br />“a”waits for a value<br />“?” does not needa value to match<br />“0” waits for aspecific value<br />Run both<br />multiply f1 f2 =<br />docase(f1, f2) of<br /> (0, ?) -> return 0<br /> (?, 0) -> return 0<br />(a, b) -> return (a * b)<br />Choice<br />
  5. 5. Multiplying future values<br />Future values<br />Compute resultin background<br />Pattern matching<br />“a” waits for a value<br />“?” does not needa value to match<br />“0” waits for aspecific value<br />Run both<br />multiply f1 f2 =<br />docase(f1, f2) of<br /> (0, ?) -> return 0<br /> (?, 0) -> return 0<br />(a, b) -> return (a * b)<br />Choice<br />Fail<br />
  6. 6. Introduction<br />Monad with three additional operations<br />Parallel composition<br /> m a -> m b -> m (a, b)<br />Monadic choice<br /> m a -> m a -> m a<br />Aliasing of computations<br />m a -> m (m a)<br />Some of them supported by many monads<br />Parallel programming (Par monad)<br />Parsers for textual input (Parser monad)<br />Reactive & concurrent (Orcmonad?, Chpmonad?)<br />
  7. 7. Parsing using Joinads<br />Validating Cambridge phone numbers<br />Contain only digits<br />Consists of 10 characters<br />Start with a prefix “1223”<br />Represents intersection of languages<br />Returns results of all three parsers<br />valid = docase( many (satisfies isDigit),<br /> multiple 10 character,<br />startsWith(string "1223") )<br />of (str, _, _) -> return str<br />
  8. 8. Printing buffer using joins<br />Join calculus<br />Channels store values<br />Joins specifyreactions<br />Pattern matching<br />Use clauses to encode joins<br />let buffer() =<br />docase (get, putInt, putString) of<br />(r, n, ?) -> <br /> reply r (intToString n)<br />(r, ?, s) -> <br /> reply r s<br />First clause<br />Second clause<br />Second clause<br />putInt<br />putString<br />get<br />
  9. 9. Joinads as an algebraic structure<br />Set 𝓙 representing “joinadic” computations<br />Constant 0 and associative operators ⊗, ⊕:<br />a ⊗ 0 = 0<br />a ⊕ 0 = a<br />a ⊗ b = b ⊗a<br />a ⊗ (b ⊕ c) = (a ⊗ b) ⊕ (a ⊗ c)<br />(𝓙, ⊕, ⊗, 0) is a commutative near-semiring<br />
  10. 10. Summary<br />Related to new monad comprehensions<br />Allows expressing parallel composition <br />Reuse the MonadZiptype class <br />Free for commutative monads<br />Future plans<br />Implementing GHC language extension<br />Looking for more Monad examples<br />For more information<br />http://tomasp.net/blog/docase-haskell.aspx<br />http://tomasp.net/blog/comprefun.aspx<br />

×