2018-12-02 22:19

Page 1
instanceMonadIO-- predefinedinstanceMonadGen-- from QuickCheckinstanceMonadParser-- from the Parsing module

**instance****Monad****Maybe****where**`return``x`**=****Just**`x`**Just**`x``>>=``f`**=**`f``x`**Nothing**`>>=`**_****=****Nothing**- In this instance:
`return`**::**`a`**->****Maybe**`a`(`>>=`)**::****Maybe**`a`**->**(`a`**->****Maybe**`b`)**->****Maybe**`b`

**instance****Monad**[]**where**`return``x`**=**[`x`]`xs``>>=``f`**=**[`y`**|**`x`**<-**`xs`,`y`**<-**`f``x`]- In this instance:
`return`**::**`a`**->**[`a`] (`>>=`)**::**[`a`]**->**(`a`**->**[`b`])**->**[`b`]

- The similarity is not a coincidence
[

`y`**|**`x`**<-**`xs`,`y`**<-**`f``x`]⟺

**do**`x`**<-**`xs`;`y`**<-**`f``x`;`return``y`

- The list monad is the same as list comprehensions

**instance****Functor****IO****instance****Functor****Gen**-- from QuickCheck**instance****Functor****Parser**- So we have
`fmap`**::**(`a`**->**`b`)**->****IO**`a`**->****IO**`b``fmap`**::**(`a`**->**`b`)**->****Gen**`a`**->****Gen**`b``fmap`**::**(`a`**->**`b`)**->****Parser**`a`**->****Parser**`b`

**instance****Functor****Maybe****where**`fmap``f`**Nothing****=****Nothing**`fmap``f`(**Just**`x`)**=****Just**(`f``x`)- In this instance
`fmap`**::**(`a`**->**`b`)**->****Maybe**`a`**->****Maybe**`b`

**instance****Functor**[]**where**`fmap`**=**`map`- In this instance
`fmap`**::**(`a`**->**`b`)**->**[`a`]**->**[`b`]

**instance****Applicative****IO****instance****Applicative****Gen**-- from QuickCheck**instance****Applicative****Parser**- So we have
(

`<*>`)**::****IO**(`a`**->**`b`)**->****IO**`a`**->****IO**`b`(`<*>`)**::****Gen**(`a`**->**`b`)**->****Gen**`a`**->****Gen**`b`(`<*>`)**::****Parser**(`a`**->**`b`)**->****Parser**`a`**->****Parser**`b`

**instance****Applicative**[]**where**`pure``x`**=**[`x`]`fs``<*>``xs`**=**[`f``x`**|**`f`**<-**`fs`,`x`**<-**`xs`]- In this instance
`pure`**::**`a`**->**[`a`] (`<*>`)**::**[`a`**->**`b`]**->**[`a`]**->**[`b`]

**instance****Applicative****Maybe****where**`pure``x`**=****Just**`x`**Just**`f``<*>`**Just**`x`**=****Just**(`f``x`)**_**`<*>`**_****=****Nothing**- In this instance
`pure`**::**`a`**->****Maybe**`a`(`<*>`)**::****Maybe**(`a`**->**`b`)**->****Maybe**`a`**->****Maybe**`b`

- Starting with GHC 7.10, is a superclass of
**Applicative**,**Monad****class****Applicative**`m`**=>****Monad**`m`**where**-- ...

- so to make a instance you also have to make an
**Monad**instance and a**Applicative**instance.**Functor** - This is easy using standard library functions:
- =
`fmap``liftM` - =
`pure``return` - =
(

`<*>`)`ap`