Docase notation for Haskell

  • 786 views
Uploaded 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 …

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

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
786
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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