KEMBAR78
Docase notation for Haskell | PPTX
Monadic pattern matching with ‘docase’Tomáš Petříček(tomas.petricek@cl.cam.ac.uk)University of Cambridge, UKAdvisors: Alan Mycroft, Don Syme
IntroductionWarm Fuzzy Things (aka Monads)Sequencing of effectful computationsUsed for parallel & concurrent programming Extend monad interface with additional operationsspawn :: Par a -> Par (IVar a)	get   :: Ivar a -> Par aWhat are common additional operations?Is there nice notation for using them?
Multiplying future valuesFuture valuesCompute resultin backgroundPattern matching“a” waits for a valueSuspended computationRun bothmultiply f1 f2 =docase(f1, f2) of(a, b) -> return (a * b)
Multiplying future valuesFuture valuesCompute resultin backgroundPattern matching“a”waits for a value“?” does not needa value to match“0” waits for aspecific valueRun bothmultiply f1 f2 =docase(f1, f2) of  (0, ?) -> return 0 (?, 0) -> return 0(a, b) -> return (a * b)Choice
Multiplying future valuesFuture valuesCompute resultin backgroundPattern matching“a” waits for a value“?” does not needa value to match“0” waits for aspecific valueRun bothmultiply f1 f2 =docase(f1, f2) of  (0, ?) -> return 0 (?, 0) -> return 0(a, b) -> return (a * b)ChoiceFail
IntroductionMonad with three additional operationsParallel composition	m a -> m b -> m (a, b)Monadic choice	m a -> m a -> m aAliasing of computationsm a -> m (m a)Some of them supported by many monadsParallel programming (Par monad)Parsers for textual input (Parser monad)Reactive & concurrent (Orcmonad?,  Chpmonad?)
Parsing using JoinadsValidating Cambridge phone numbersContain only digitsConsists of 10 charactersStart with a prefix “1223”Represents intersection of languagesReturns results of all three parsersvalid = docase( many (satisfies isDigit),                 multiple 10 character,startsWith(string "1223") )of (str, _, _) -> return str
Printing buffer using joinsJoin calculusChannels store valuesJoins specifyreactionsPattern matchingUse clauses to encode joinslet buffer() =docase (get, putInt, putString) of(r, n, ?) ->       reply r (intToString n)(r, ?, s) ->        reply r sFirst clauseSecond clauseSecond clauseputIntputStringget
Joinads as an algebraic structureSet 𝓙 representing “joinadic” computationsConstant 0 and associative operators ⊗, ⊕:a ⊗ 0 = 0a ⊕ 0 = aa ⊗ b = b ⊗aa ⊗ (b ⊕ c) = (a ⊗ b) ⊕ (a ⊗ c)(𝓙, ⊕, ⊗, 0) is a commutative near-semiring
SummaryRelated to new monad comprehensionsAllows expressing parallel composition Reuse the MonadZiptype class Free for commutative monadsFuture plansImplementing GHC language extensionLooking for more Monad examplesFor more informationhttp://tomasp.net/blog/docase-haskell.aspxhttp://tomasp.net/blog/comprefun.aspx

Docase notation for Haskell

  • 1.
    Monadic pattern matchingwith ‘docase’Tomáš Petříček(tomas.petricek@cl.cam.ac.uk)University of Cambridge, UKAdvisors: Alan Mycroft, Don Syme
  • 2.
    IntroductionWarm Fuzzy Things(aka Monads)Sequencing of effectful computationsUsed for parallel & concurrent programming Extend monad interface with additional operationsspawn :: Par a -> Par (IVar a) get :: Ivar a -> Par aWhat are common additional operations?Is there nice notation for using them?
  • 3.
    Multiplying future valuesFuturevaluesCompute resultin backgroundPattern matching“a” waits for a valueSuspended computationRun bothmultiply f1 f2 =docase(f1, f2) of(a, b) -> return (a * b)
  • 4.
    Multiplying future valuesFuturevaluesCompute resultin backgroundPattern matching“a”waits for a value“?” does not needa value to match“0” waits for aspecific valueRun bothmultiply f1 f2 =docase(f1, f2) of (0, ?) -> return 0 (?, 0) -> return 0(a, b) -> return (a * b)Choice
  • 5.
    Multiplying future valuesFuturevaluesCompute resultin backgroundPattern matching“a” waits for a value“?” does not needa value to match“0” waits for aspecific valueRun bothmultiply f1 f2 =docase(f1, f2) of (0, ?) -> return 0 (?, 0) -> return 0(a, b) -> return (a * b)ChoiceFail
  • 6.
    IntroductionMonad with threeadditional operationsParallel composition m a -> m b -> m (a, b)Monadic choice m a -> m a -> m aAliasing of computationsm a -> m (m a)Some of them supported by many monadsParallel programming (Par monad)Parsers for textual input (Parser monad)Reactive & concurrent (Orcmonad?, Chpmonad?)
  • 7.
    Parsing using JoinadsValidatingCambridge phone numbersContain only digitsConsists of 10 charactersStart with a prefix “1223”Represents intersection of languagesReturns results of all three parsersvalid = docase( many (satisfies isDigit), multiple 10 character,startsWith(string "1223") )of (str, _, _) -> return str
  • 8.
    Printing buffer usingjoinsJoin calculusChannels store valuesJoins specifyreactionsPattern matchingUse clauses to encode joinslet buffer() =docase (get, putInt, putString) of(r, n, ?) -> reply r (intToString n)(r, ?, s) -> reply r sFirst clauseSecond clauseSecond clauseputIntputStringget
  • 9.
    Joinads as analgebraic structureSet 𝓙 representing “joinadic” computationsConstant 0 and associative operators ⊗, ⊕:a ⊗ 0 = 0a ⊕ 0 = aa ⊗ b = b ⊗aa ⊗ (b ⊕ c) = (a ⊗ b) ⊕ (a ⊗ c)(𝓙, ⊕, ⊗, 0) is a commutative near-semiring
  • 10.
    SummaryRelated to newmonad comprehensionsAllows expressing parallel composition Reuse the MonadZiptype class Free for commutative monadsFuture plansImplementing GHC language extensionLooking for more Monad examplesFor more informationhttp://tomasp.net/blog/docase-haskell.aspxhttp://tomasp.net/blog/comprefun.aspx