Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Monadic pattern matching with ‘docase’<br />Tomáš Petříček(tomas.petricek@cl.cam.ac.uk)University of Cambridge, UK<br />Ad...
Introduction<br />Warm Fuzzy Things (aka Monads)<br />Sequencing of effectful computations<br />Used for parallel & concur...
Multiplying future values<br />Future values<br />Compute resultin background<br />Pattern matching<br />“a” waits for a v...
Multiplying future values<br />Future values<br />Compute resultin background<br />Pattern matching<br />“a”waits for a va...
Multiplying future values<br />Future values<br />Compute resultin background<br />Pattern matching<br />“a” waits for a v...
Introduction<br />Monad with three additional operations<br />Parallel composition<br />	m a -> m b -> m (a, b)<br />Monad...
Parsing using Joinads<br />Validating Cambridge phone numbers<br />Contain only digits<br />Consists of 10 characters<br /...
Printing buffer using joins<br />Join calculus<br />Channels store values<br />Joins specifyreactions<br />Pattern matchin...
Joinads as an algebraic structure<br />Set 𝓙 representing “joinadic” computations<br />Constant 0 and associative operator...
Summary<br />Related to new monad comprehensions<br />Allows expressing parallel composition <br />Reuse the MonadZiptype ...
Nächste SlideShare
Wird geladen in …5
×

Docase notation for Haskell

1.324 Aufrufe

Veröffentlicht am

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).

Veröffentlicht in: Technologie, Bildung
  • Als Erste(r) kommentieren

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 />

×