Docase notation for Haskell

10

Click here to load reader

description

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

Transcript of Docase notation for Haskell

Page 1: Docase notation for Haskell

Tomáš Petříček ([email protected])University of Cambridge, UK

Advisors: Alan Mycroft, Don Syme

Monadic pattern matching with ‘docase’

Page 2: Docase notation for Haskell

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?

Page 3: Docase notation for Haskell

Multiplying future values

Future values Compute result

in background

Pattern matching “a” waits for a value

multiply f1 f2 = docase (f1, f2) of (a, b) -> return (a * b)

Suspended computation

Run both

Page 4: Docase notation for Haskell

Multiplying future values

Future values Compute result

in background

Pattern matching “a” waits for a value “?” does not need

a value to match “0” waits for a

specific value

multiply f1 f2 = docase (f1, f2) of (0, ?) -> return 0 (?, 0) -> return 0 (a, b) -> return (a * b)

Run both

Choice

Page 5: Docase notation for Haskell

Multiplying future values

Future values Compute result

in background

Pattern matching “a” waits for a value “?” does not need

a value to match “0” waits for a

specific value

multiply f1 f2 = docase (f1, f2) of (0, ?) -> return 0 (?, 0) -> return 0 (a, b) -> return (a * b)

Run both

Fail

Choice

Page 6: Docase notation for Haskell

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 (Orc monad?, Chp monad?)

Page 7: Docase notation for Haskell

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

Page 8: Docase notation for Haskell

Printing buffer using joins

Join calculus Channels

store values Joins specify

reactions

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

putInt

putString

get

First clause

Second clause Second clause

Page 9: Docase notation for Haskell

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

Page 10: Docase notation for Haskell

Summary

Related to new monad comprehensions Allows expressing parallel composition Reuse the MonadZip type 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