Presentation for effect systems meetup

https://hasgeek.com/fpindia/effect-systems-reading-group/
This commit is contained in:
Sanchayan Maity 2023-05-18 20:40:01 +05:30
commit 8cfedc2ce9
6 changed files with 124 additions and 0 deletions

View file

@ -0,0 +1,11 @@
all:
make slide slideshow
slide:
pandoc -t beamer --include-in-header=./style.tex effects-freer-monads.md -f markdown-implicit_figures -V colorlinks=true -V linkcolor=blue -V urlcolor=red -o effects-freer-monads.pdf
slideshow:
pandoc -t beamer --include-in-header=./style.tex effects-freer-monads.md -f markdown-implicit_figures -V colorlinks=true -V linkcolor=blue -V urlcolor=red -i -o effects-freer-monads-slideshow.pdf
view:
zathura --mode=presentation effects-freer-monads.pdf &

Binary file not shown.

View file

@ -0,0 +1,112 @@
---
title:
- Effect Systems in Haskell - Part I
author:
- Sanchayan Maity
institute:
- asymptotic.io
theme:
- default
classoption:
- aspectratio=169
---
# Agenda
- Cover two papers on Effect Systems by Oleg Kiselyov
* Extensible Effects - An Alternative to Monad Transformers
* Freer Monads, More Extensible Effects
- Another related paper `Reflection Without Remorse` as a prerequisite
- Some sections today's discussion isn't going to cover
* Efficiency/Performance of the library or effect system itself
* For the sake of time, focus more on Freer Monads paper
* Comparison of effect system libraries or how to choose one
# Why effect systems
- Monads to model effects but monads don't compose[^1]
- transformers/mtl has limitations
* Monad transformer stacks are rigid
* Doesn't allow handling something like `Reader Int (Reader String)` due to functional dependencies
```haskell
class Monad m => MonadReader r m | m -> r
```
* More than a few effects in stack become unwieldy
* n-square instances problem
[^1]: [Composing Monads by Mark Jones and Luc Duponcheel](https://web.cecs.pdx.edu/~mpj/pubs/RR-1004.pdf)
# Effect system libraries
- `freer-simple` based on Extensible Effects and Freer Monads, More Extensible Effects by Oleg Kiselyov
- `polysemy` based on Effect Handlers in Scope by Wu, Schrijvers et al
- `fused-effects` based on Fusion for Free: Efficient Algebraic Effect Handlers by Wu, Schrijvers et al
- `cleff` based on `ReaderT IO`
- `effectful` based on `ReaderT IO`
- others?
# Free monads
Given a `Functor f`, `Free f` is a `Free` monad.
```haskell
data Term f a = Pure a
| Impure (f (Term f a))
```
A Monad is something that "computes" when monadic context is collapsed by `join :: m (m a) -> m a` (recalling that `>>=` can be defined as `x >>= y = join (fmap y x))`. This is how Monads carry context through a sequential chain of computations: because at each point in the series, the context from the previous call is collapsed with the next.
A free monad satisfies all the Monad laws, but doesn't do any collapsing (that's the computation). It just builds up a nested series of contexts. The user who creates such a free monadic value is responsible for doing something with those nested contexts, so that the meaning of such a composition can be deferred until after the monadic value has been created.[^2]
[^2]: John Wiegley on [Stack Overflow](https://stackoverflow.com/a/13388966).
# Free monads performance
- Vanilla free monads don't have great performance.
- Solutions like Codensity monad transformer and Church encoded free monad exist.[^3][^4]
```haskell
newtype FT f m a =
FT { runFT :: forall r. (a -> m r) -> (forall x. (x -> m r) -> f x -> m r) -> m r }
```
[^3]: Asymptotic Improvement of Computations over Free Monads - Janis Voigtländer
[^4]: [The Free and The Furious: And by 'Furious' I mean Codensity. - raichoo](https://www.youtube.com/watch?v=EiIZlX_k89Y)
# Reflection without remorse
- A left associated expression is asymptotically slower than the equivalent right associated expression. $O(n^2)$ vs $O(n)$ respectively.
- What's meant by reflection? Build and observe.
- Efficient data structures give asymptotic improvement for problematic occurrences of build and observe pattern like monads and monadic reflection.
# Extensible effects
- Defines only one effect `Eff`
- Type level list of effects
- What does it mean to be extensible?
# Freer monads
- Improves on extensible effects
- How?
* Relaxes the `Functor` constraint, becoming `Freer`!
* No need for `Functor` and `Typeable` on `Union`
- `freer` and `freer-simple` are based on `Freer` monads
# Resources
- [Why Free monads matter](https://www.haskellforall.com/2012/06/you-could-have-invented-free-monads.html)
- [Free monad considered harmful](https://markkarpov.com/post/free-monad-considered-harmful.html)
- [Building real-world Haskell applications using Tagless-Final and ReaderT](https://fpunfold.com/2023/01/30/final-tagless-readert.html)
- [when to use CPS vs codensity vs reflection without remorse](https://stackoverflow.com/questions/45334985/when-to-use-cps-vs-codensity-vs-reflection-without-remorse-in-haskell)
- [ReaderT pattern is just extensible effects](https://xn--i2r.xn--rhqv96g/2022/02/03/readert-is-extensible-effects/)
- [My Effects Bibliography](https://www.dantb.dev/posts/effects-bibliography/)
- [Effects Bibliography](https://github.com/yallop/effects-bibliography)
# Questions?
- Reach out on
* Email: sanchayan@sanchayanmaity.net
* Mastodon: https://sanchayanmaity.com/@sanchayan
* Blog: sanchayanmaity.net
* Telegram: t.me/SanchayanMaity

Binary file not shown.

View file

@ -0,0 +1 @@
\logo{\includegraphics[height=0.5cm]{../images/Haskell.jpg}\vspace{220pt}\hspace{8pt}}

BIN
images/Haskell.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB