# News aggregator

### PolyKind for Fixed and HasResolution from Data.Fixed

### GADTs and Exponentiated Functors

### Philip Wadler: The Ask

The Scottish Government have set a target of 10% of all trips by foot or bicycle, but less than 2% of the Scottish travel budget goes to '

*active travel*' (the buzzword for getting from one place to another minus a motor). We Walk, We Cycle, We Vote and Spokes suggest you ask your candidate to pledge the following:

To raise the share of the transport budget spent on walking and cycling to 10% over the course of the next parliament.See the pages linked above for more info, including hustings you can attend to put the question to your local candidates. A don't forget to Pedal on Parliament on 23 April 2016.

### Type-level list "elem" inference

### Gabriel Gonzalez: From mathematics to map-reduce

There's more mathematics to programming than meets the eye. This post will highlight one such connection that explains the link between map-reduce and category theory. I will then conclude with some wild speculation about what this might imply for future programming paradigms.

This post assumes that you already know Haskell and explains the mathematics behind the map-reduce using Haskell concepts and terminology. This means that this post will oversimplify some of the category theory concepts in order to embed them in Haskell, but the overall gist will still be correct.

Background (Isomorphism)In Haskell, we like to say that two types, s and t, are "isomorphic" if and only if there are two functions, fw and bw, of types

fw :: s -> tbw :: t -> s

... that are inverse of each other:

fw . bw = idbw . fw = id

We will use the symbol ≅ to denote that two types are isomorphic. So, for example, we would summarize all of the above by just writing:

s ≅ tThe fully general definition of isomorphism from category theory is actually much broader than this, but this definition will do for now.

Background (Adjoint functors)Given two functors, f and g, f is left-adjoint to g if and only if:

f a -> b ≅ a -> g bIn other words, for them to be adjoint there must be two functions, fw and bw of types:

fw :: (f a -> b) -> (a -> g b)bw :: (a -> g b) -> (f a -> b)

... such that:

fw . bw = idbw . fw = id

These "functors" are not necessarily the same as Haskell's Functor class. The category theory definition of "functor" is more general than Haskell's Functor class and we'll be taking advantage of that extra generality in the next section.

Free functorsImagine a functor named g that acted more like a type-level function that transforms one type into another type. In this case, g will be a function that erases a constraint named C. For example:

-- `g` is a *type-level* function, and `t` is a *type*g (C t => t) = t

In other words, g "forgets" the C constraint on type t. We call g a "forgetful functor".

If some other functor, f is left-adjoint to g then we say that f is the "free C" (where C is the constraint that g "forgets").

In other words, a "free C" is a functor that is left-adjoint to another functor that forgets the constraint C.

Free monoidThe list type constructor, [], is the "free Monoid"

The "free Monoid" is, by definition, a functor [] that is left-adjoint to some other functor g that deletes Monoid constraints.

When we say that g deletes Monoid constraints we mean that:

g (Monoid m => m) = m... and when we say that [] is left-adjoint to g that means that:

[] a -> b ≅ a -> g b... and the type [a] is syntactic sugar for [] a, so we can also write:

[a] -> b ≅ a -> g bNow substitute b with some type with a Monoid constraint, like this one:

b = Monoid m => mThat gives us:

[a] -> (Monoid m => m) ≅ a -> g (Monoid m => m)... and since g deletes Monoid constraints, that leaves us with:

[a] -> (Monoid m => m) ≅ a -> mThe above isomorphism in turn implies that there must be two functions, fw and bw, of types:

fw :: ([a] -> (Monoid m => m)) -> (a -> m)bw :: (a -> m) -> ([a] -> (Monoid m => m))

... and these two functions must be inverses of each other:

fw . bw = idbw . fw = id

We can pull out the Monoid constraints to the left to give us these more idiomatic types:

fw :: (Monoid m => [a] -> m)) -> (a -> m)bw :: Monoid m => ( a -> m) -> ([a] -> m)

Both of these types have "obvious" implementations:

fw :: (Monoid m => [a] -> m)) -> (a -> m)fw k x = k [x]

bw :: Monoid m => (a -> m) -> ([a] -> m)

bw k xs = mconcat (map k xs)

Now we need to prove that the fw and bw functions are inverse of each other. Here are the proofs:

-- Proof #1fw . bw

-- eta-expand

= \k -> fw (bw k)

-- eta-expand

= \k x -> fw (bw k) x

-- Definition of `fw`

= \k x -> bw k [x]

-- Definition of `bw`

= \k x -> mconcat (map k [x])

-- Definition of `map`

= \k x -> mconcat [k x]

-- Definition of `mconcat`

= \k x -> k x

-- eta-reduce

= \k -> k

-- Definition of `id`

= id

-- Proof #2

bw . fw

-- eta-expand

= \k -> bw (fw k)

-- eta-expand

= \k xs -> bw (fw k) xs

-- Definition of `bw`

= \k xs -> mconcat (map (fw k) xs)

-- eta-expand

= \k xs -> mconcat (map (\x -> fw k x) xs)

-- Definition of `fw`

= \k xs -> mconcat (map (\x -> k [x]) xs)

-- map (f . g) = map f . map g

= \k xs -> mconcat (map k (map (\x -> [x]) xs))

-- ... and then a miracle occurs ...

--

-- In all seriousness this step uses a "free theorem" which says

-- that:

--

-- forall (k :: Monoid m => [a] -> m) . mconcat . map k = k . mconcat

--

-- We haven't covered free theorems, but you can read more about them

-- here: http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf

= \k xs -> k (mconcat (map (\x -> [x]) xs)

-- This next step is a proof by induction, which I've omitted

= \k xs -> k xs

-- eta-reduce

= \k -> k

-- Definition of `id`

= idMap reduce

Let's revisit the type and implementation of our bw function:

bw :: Monoid m => (a -> m) -> ([a] -> m)bw k xs = mconcat (map k xs)

That bw function is significant because it is a simplified form of map-reduce:

- First you "map" a function named k over the list of xs
- Then you "reduce" the list using mconcat

In other words, bw is a pure "map-reduce" function and actually already exists in Haskell's standard library as the foldMap function.

The theory of free objects predict that all other functions of interest over a free object (like the free Monoid) can be reduced to the above fundamental function. In other words, the theory indicates that we can implement all other functions over lists in terms of this very general map-reduce function. We could have predicted the importance of "map-reduce purely from the theory of "free Monoids"!

However, there are other free objects besides free Monoids. For example, there are "free Monads" and "free Categorys" and "free Applicatives" and each of them is equipped with a similarly fundamental function that we can use to express all other functions of interest. I believe that each one of these fundamental functions is a programming paradigm waiting to be discovered just like the map-reduce paradigm.

### Magnus Therning: From JSON to sum type

For a while I’ve been planning to take full ownership of the JSON serialisation and parsing in cblrepo. The recent inclusion of instances of ToJSON and FromJSON for Version pushed me to take the first step by writing my own instances for all external types.

When doing this I noticed that all examples in the aeson docs use a product

data Person = Person { name :: Text , age :: Int }whereas I had to deal with quite a few sums, e.g. VersionRange. At first I struggled a little with how to write an instance of FromJSON. After quite a bit of thinking I came up with the following, which I think is fairly nice, but I’d really like to hear what others think about it. Maybe I’ve just missed a much simpler way of implementing parseJSON:

instance FromJSON V.VersionRange where parseJSON = withObject "VersionRange" go where go o = do lv <- (o .:? "LaterVersion") >>= return . fmap V.laterVersion tv <- (o .:? "ThisVersion") >>= return . fmap V.thisVersion ev <- (o .:? "EarlierVersion") >>= return . fmap V.earlierVersion av <- (o .:? "AnyVersion") >>= \ (_::Maybe [(Int,Int)]) -> return $ Just V.anyVersion wv <- (o .:? "WildcardVersion") >>= return . fmap V.WildcardVersion uvr <- (o .:? "UnionVersionRanges") >>= return . fmap toUvr ivr <- (o .:? "IntersectVersionRanges") >>= return . fmap toIvr vrp <- (o .:? "VersionRangeParens") >>= return . fmap V.VersionRangeParens maybe (typeMismatch "VersionRange" $ Object o) return (lv <|> tv <|> ev <|> uvr <|> ivr <|> wv <|> vrp <|> av) toUvr [v0, v1] = V.unionVersionRanges v0 v1 toIvr [v0, v1] = V.intersectVersionRanges v0 v1Any and all comments and suggestions are more than welcome!

### Reducing boilerplate

### Digraphs With Text: More flexible than graphs,eager to hear from you

### Accuracy of Data.Number.Fixed

### Call for Participation: PLACES 2016

### HasCallStack - runtime costs?

### Month in Haskell Mode February 2016

### New gtk2hs 0.12.4 release

Thanks to John Lato and Duncan Coutts for the latest bugfix release! The latest packages should be buildable on GHC 7.6, and the cairo package should behave a bit nicer in ghci on Windows. Thanks to all!

~d