# Haskell

## Table of Contents

- [C] . haskell
- haskell Do notation expanded
- monads haskellmonad
- haskellmonad monad transformers
- related haskellfp
- TODO [D] Tweet from Dmitry Kovanikov (@ChShersh), at Jun 1, 04:37 haskell
- DONE [C] krispo/awesome-haskell: A collection of awesome Haskell links, frameworks, libraries and software. Inspired by awesome projects line. haskell
- [B] debug: what I learnt during setting app hakyll blog.. haskell
- [C] Resilient-Haskell-Software - Gwern.net haskell
- haskell Stackage Server https://www.stackage.org/
- haskell debugging ghci
- [B] debug: stack trace haskell
- haskellfun Henry de Valence on Twitter: "7 years ago i was very smart and wrote my website in haskell and now i can't update it because i forgot how to make a monad out of posts" / Twitter
- [C] applicative functors haskell
- [D] monad vs applicative haskell

## ¶[C] . haskell

ihaskell install

jupyter console –kernel haskell

## ¶ Do notation expanded haskell

do e → e do { e; stmts } → e >> do { stmts } do { v <- e; stmts } → e >>= \v -> do { stmts } do { let decls; stmts} → let decls in do { stmts }

This is not quite the whole story, since v might be a pattern instead of a variable. For example, one can write

do (x:xs) <- foo

bar x

but what happens if foo produces an empty list? Well, remember that ugly fail function in the Monad type class declaration? That’s what happens.

## ¶monads haskellmonad

### ¶writer monad, logger monad haskellmonad

-- l: log type, Monoid newtype Writer l a = Writer { runWriter :: (a, l) }

### ¶reader monad haskellmonad

newtype Reader r a = Reader { runReader :: r -> a }

### ¶state monad haskellmonad

newtype State s a = State { runState :: s -> (a, s) }

## ¶ monad transformers haskellmonad

Monad transformers exist only because monads do not compose in general.

MaybeT : provides the construction for (f of Maybe) for any Monad f.

data Compose f g x = Compose (f (g x)) instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose z) = Compose (fmap (fmap f) z) instance (Monad f, Monad g) => Monad (Compose f g) where bind :: Compose f g a -> (a -> Compose f g b) -> Compose f g b bind (f (g x)) ff = bind = ??? no way! f a -> (a -> f b) -> f b g c -> (c -> g d) -> g d

## ¶related haskellfp

## ¶TODO [D] Tweet from Dmitry Kovanikov (@ChShersh), at Jun 1, 04:37 haskell

Looks like @iokasimov can write good #haskell jokes 😏

## ¶DONE [C] krispo/awesome-haskell: A collection of awesome Haskell links, frameworks, libraries and software. Inspired by awesome projects line. haskell

- State "DONE" from

## ¶[B] debug: what I learnt during setting app hakyll blog.. haskell

- stack ghci
- Debug.Trace.traceShowM within do block
- pandoc –metadata

## ¶[C] Resilient-Haskell-Software - Gwern.net haskell

## ¶ Stackage Server https://www.stackage.org/ haskell

## ¶ debugging ghci haskell

stack ghci (module loaded automatically) :set args rebuild main :break 222

## ¶[B] debug: stack trace haskell

stack build --profile stack exec -- site rebuild +RTS -xc

## ¶ Henry de Valence on Twitter: "7 years ago i was very smart and wrote my website in haskell and now i can't update it because i forgot how to make a monad out of posts" / Twitter haskellfun

## ¶[C] applicative functors haskell

regular functor

class Functor f where fmap :: (a -> b) -> f a -> f b

Laws:

fmap id = id -- Identity fmap (p . q) = (fmap p) . (fmap q) -- Homomorphism

applicative

class (Functor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b

Laws:

pure id <*> v = v -- Identity pure (.) <*> u <*> v <*> w = u <*> (v <*> w) -- Composition pure f <*> pure x = pure (f x) -- Homomorphism u <*> pure y = pure ($ y) <*> u -- Interchange fmap f x = pure f <*> x -- Fmap

The mapping between Haskell functors is a family of functions parameterized by types. For instance, a mapping between the [] functor and the Maybe functor will map a list of a, [a] into Maybe a.

Here's an example of such a family of functions called safeHead:

safeHead :: [a] -> Maybe a safeHead [] = Nothing safeHead (x:xs) = Just x

## ¶[D] monad vs applicative haskell

https://wiki.haskell.org/Typeclassopedia#Intuition_3

Let’s look more closely at the type of (>>=). The basic intuition is that it combines two computations into one larger computation. The first argument, m a, is the first computation. However, it would be boring if the second argument were just an m b; then there would be no way for the computations to interact with one another (actually, this is exactly the situation with Applicative). So, the second argument to (>>=) has type a -> m b: a function of this type, given a result of the first computation, can produce a second computation to be run. In other words, x >>= k is a computation which runs x, and then uses the result(s) of x to decide what computation to run second, using the output of the second computation as the result of the entire computation.

Actually, because Haskell allows general recursion, one can recursively construct infinite grammars, and hence Applicative (together with Alternative) is enough to parse any context-sensitive language with a finite alphabet.

http://byorgey.wordpress.com/2012/01/05/parsing-context-sensitive-languages-with-applicative

Here’s the key insight: normally, grammars are defined as finite objects: a finite set of terminals, a finite set of nonterminals, and a finite set of productions. However, Haskell’s general recursion means that we can write down a "grammar" with an infinite set of production rules. This is what lets us get away with parsing context-sensitive languages with Applicative: we just make a different production rule for every possible input!