Docase notation for Haskell
Click here to load reader
-
Upload
tomas-petricek -
Category
Technology
-
view
822 -
download
5
description
Transcript of Docase notation for Haskell
![Page 1: Docase notation for Haskell](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/1.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/2.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/3.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/4.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/5.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/6.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/7.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/8.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/9.jpg)
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](https://reader038.fdocuments.us/reader038/viewer/2022100601/557cf5a4d8b42a89158b48b7/html5/thumbnails/10.jpg)
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