2018-12-05 18:54

Page 1
- It is a combination of λ and
`>>=`- λ as in lambda, anonymous functions,
**\**`x`**->**2`*``x` - The operator in the
`>>=`class.**Monad**

- λ as in lambda, anonymous functions,

number::String->Maybe(Integer,String)numbers=casespanisDigitsof([],_)->Nothing(ds,r)->Just(readds,r)addition::String->Maybe(Integer,String)additions=casenumbersofJust(n1,'+':r)->casenumberrofJust(n2,r')->Just(n1+n2,r')_->Nothing_->Nothingmultiplication::String->Maybe(Integer,String)multiplications=casenumbersofJust(n1,'*':r)->casenumberrofJust(n2,r')->Just(n1*n2,r')_->Nothing_->Nothingcalculation::String->Maybe(Integer,String)calculations=caseadditionsofNothing->multiplicationsresult->result

number=read<$>oneOrMore(satisDigit)addition=(+)<$>number<*char'+'<*>numbermultiplication=(*)<$>number<*char'*'<*>numbercalculation=addition<|>multiplication

- The parser:
`number`**=**`read``<$>``oneOrMore`(`sat``isDigit`)`addition`**=**(`+`)`<$>``number``<*``char`'+'`<*>``number``multiplication`**=**(`*`)`<$>``number``<*``char`'*'`<*>``number``calculation`**=**`addition``<|>``multiplication`

- The grammar:
number ::= digit {digit}. addition ::= number "+" number. multiplication ::= number "*" number. calculation ::= addition | multiplication.
- Haskell is suitable as a host language in which you can embedd other (domain-specific) languages.

Type of effect Function type Pure function: **Input****->****Output**Extra input: **Input****->****Extra****->****Output**Extra output: **Input****->**(**Extra**,**Output**)Changing the state: **Input****->****State****->**(**State**,**Output**)IO operations: **Input****->****IO****Output**Success or failure: **Input****->****Maybe****Output**Many results: **Input****->**[**Output**]- Can we factor out a common pattern?

We can factor them like this:

Type of effect The common pattern The difference Pure function: **Input****->****M****Output**=**M**`a``a`Extra input: **Input****->****M****Output**=**M**`a`**Extra****->**`a`Extra output: **Input****->****M****Output**=**M**`a`(

**Extra**,`a`)Changing the state: **Input****->****M****Output**=**M**`a`**State****->**(**State**,`a`)IO operations: **Input****->****M****Output**=**M**`a`**IO**`a`Success or failure: **Input****->****M****Output**=**M**`a`**Maybe**`a`Many results: **Input****->****M****Output**=**M**`a`[

`a`]- In all cases it can be seen as a modification of the return type!
- Sometimes we will need combinations of these effects.

**class****Functor**`f`**where**`fmap`**::**(`a`**->**`b`)**->**`f``a`**->**`f``b``f``<$>``x`**=**`fmap``f``x`**class****Functor**`f`**=>****Applicative**`f`**where**`pure`**::**`a`**->**`f``a`(`<*>`)**::**`f`(`a`**->**`b`)**->**`f``a`**->**`f``b`**class****Applicative**`m`**=>****Monad**`m`**where**`return`**::**`a`**->**`m``a`(`>>=`)**::**`m``a`**->**(`a`**->**`m``b`)**->**`m``b`(`=<<`)**::****Monad**`m`**=>**(`a`**->**`m``b`)**->**`m``a`**->**`m``b``f``=<<``x`**=**`x``>>=``f`

- The notation is translated into expressions using
**do**`>>=`- That's why it works for any monad!

- Example:
**do**`x`**<-**`m1``y`**<-**`m2``x``return`(`x``+``y`)⟹ `m1``>>=`**\**`x`**->****do**`y`**<-**`m2``x``return`(`x``+``y`))⟹ `m1``>>=`**\**`x`**->**`m2``x``>>=`**\**`y`**->**`return`(`x``+``y`)

- We have seen a few examples where using simplifies the code:
`<$>` `rNumeric`**::****Gen****Rank**`rNumeric`**=****do**`n`**<-**`choose`(2,10)`return`(**Numeric**`n`)`rNumeric`**=****Numeric**`<$>``choose`(2,10)`number`**::****Parser****Integer**`number`**=****do**`ds`**<-**`oneOrMore``digit``return`(`read``ds`)`number`**=**`read``<$>``oneOrMore``digit`

- Here are some examples where using simplifies the code:
`<*>` `rCard`**::****Gen****Card**`rCard`**=****do**`s`**<-**`rSuit``r`**<-**`rRank``return`(**Card**`s``r`)`rCard`**=****Card**`<$>``rSuit``<*>``rRand``oneOrMore`**::****Parser**`item`**->****Parser**[`item`]`oneOrMore``item`**=****do**`i`**<-**`item``is`**<-**`zeroOrMore``item``return`(`i`**:**`is`)`oneOrMore``item`**=**(**:**)`<$>``item``<*>``zeroOrMore``item`

- Functors, Applicatives, and Monads in Pictures
- See Week 4, exercise 2 for examples of useful generic monadic helper functions.
- The course Advanced Functional Programming (TDA342) covers monads in more depth.