# News aggregator

### Doctest, quickcheck and typeclass

### Proposal: add -XPolyKinds to Data.Monoid

### How to walk under binders "for free" in 'bound'?

### ANN: HacBerlin - Haskell Hackathon in Berlin,26-28 Sep 2014

### ANN: HacBerlin - Haskell Hackathon in Berlin,26-28 Sep 2014

### Call for participation: FOSDEM 2015

### Call for Talk Proposals: Parsing< at >SLE

### Roman Cheplyaka: Type-based lift

In mtl, the ask method of the MonadReader class will automatically «lift» itself to the topmost ReaderT layer in the stack, which is very convenient, but only works as long as the topmost layer is the one you need. If you have multiple ReaderTs in the stack, you often have to insert manual lifts.

Previously I described why a smarter automatic lifting mechanism is needed to build truly reusable pieces of monadic code without too much boilerplate.

In this article I show two ways to achieve a type-based lift (that is, a lift which takes into account the r of ReaderT r), one relying on IncoherentInstances, the other — on closed type families.

Class-based approach and IncoherentInstancesIn Two failed attempts at extensible effects, I wrote that simply removing the fundep from mtl wouldn’t work. This claim was recently disproved by Ben Foppa and his extensible-transformers library.

Why did I think that such an approach wouldn’t work?

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, OverlappingInstances #-} import Control.Monad.Trans.Reader hiding (ask) import qualified Control.Monad.Trans.Reader as Trans import Control.Monad.Trans.Class class MonadReader r m where ask :: m r instance Monad m => MonadReader r (ReaderT r m) where ask = Trans.ask instance (Monad m, MonadReader r m, MonadTrans t) => MonadReader r (t m) where ask = lift askGHC, when asked to compile something that uses the above instances, will ask you in return to enable the IncoherentInstances extension. My experience with GHC told me that such a request is just a polite way for GHC to say «You’re doing something wrong!», so I immediately dismissed that approach. I had never seen a case where IncoherentInstances would be an acceptable solution to the problem. Well, this one seems to be exactly such a case!

Switching IncoherentInstances on here not only makes the type checker happy, but also makes the code work as expected, at least in the few tests that I tried.

Closed type classesIntuitively, the reason why GHC needs so many ugly extensions to make the above code work is that we’re trying to simulate a closed type class with an open one.

Our type class is essentially a type-level *if* operator comparing two types, and its two instances correspond to the two branches of the *if* operator.

If only we had closed type classes, we could write

import Control.Monad.Trans.Reader hiding (ask) import qualified Control.Monad.Trans.Reader as Trans import Control.Monad.Trans.Class class MonadReader r m where ask :: m r instance Monad m => MonadReader r (ReaderT r m) where ask = Trans.ask instance (Monad m, MonadReader r m, MonadTrans t) => MonadReader r (t m) where ask = lift ask(where I put instance declarations inside the class declaration to show that the class is closed).

Alas, GHC 7.8 does not have closed type classes, and I have not even heard of them being developed. All we have is closed type families. Closed type families would let us compute, say, a type-level number showing how far we have to lift a monadic action to reach the right level. They, however, do not allow us to compute a value-level witness — the very lifting function!

Closed type familiesStill, it is possible to achieve automatic lifting using closed type families alone. We developed this approach together with Andres Löh at ZuriHac’14.

The main idea is to split the problem into two.

First, we compute the amount of lifting required using a closed type family

-- Peano naturals, promoted to types by DataKinds data Nat = Zero | Suc Nat type family Find (t :: (* -> *) -> (* -> *)) (m :: * -> *) :: Nat where Find t (t m) = Zero Find t (p m) = Suc (Find t m)Second, assuming we know how far to lift, we can compute the lifting function using an ordinary (open) MPTC:

class Monad m => MonadReaderN (n :: Nat) r m where askN :: Proxy n -> m r instance Monad m => MonadReaderN Zero r (ReaderT r m) where askN _ = Trans.ask instance (MonadTrans t, Monad (t m), MonadReaderN n r m, Monad m) => MonadReaderN (Suc n) r (t m) where askN _ = lift $ askN (Proxy :: Proxy n)It is important to note that our instances of MonadReaderN are non-overlapping. The instance is uniquely determined by the n :: Nat type parameter.

Finally, we glue the two components together to get a nice ask function:

-- Nice constraint alias type MonadReader r m = MonadReaderN (Find (ReaderT r) m) r m ask :: forall m r . MonadReader r m => m r ask = askN (Proxy :: Proxy (Find (ReaderT r) m)) Problem solved?Not quite. Both solutions described here do abstract from the position of a monad transformer in the stack, but they do not abstract from the transformer itself. The MonadReader r constraint can only be satisfied with ReaderT r but not, say StateT r. Moreover, a MonadState constraint, defined as

type MonadState s m = MonadStateN (Find (Control.Monad.State.Lazy.StateT s) m) s mcan only be satisfied by the lazy, but not strict, StateT.

I’ll address this issue in a subsequent article.

### Lens Tutorial - Introduction (part 1)

### Intro to Machines & Arrows Part 2: 'Auto' as Category, Applicative & Arrow, ft. locally stateful compositions; Further down the road to an Arrowized FRP implementation.

### Installing ghc-7.8.3 OS X bindist fails on Xcode 4 CLI-only machine

### Cabal, Semantic Versioning and Endless Experimental

EDIT - Much confusion on my part as the Cabal versioning policy's (PVP) tracks the major version as A.B while Semantic Versioning's major version is only X. The second point confusion was the Semantic Versioning having a distinguished major version zero (0.y.z) which by convention implies instability.

So I was reading the latest about Cabal Hell and thinking about how it just doesn't happen to me anymore as I pretty much follow much of the points outlined in the latest how to avoid Cabal Hell discussion.

And then this morning ... fire and brimstone

The conflict happened as a result of two sets of narrow cabal bounds for attoparsec in two different libriaries.

For the remainder of my thoughts, I'm assuming the principles of Semantic Versioning apply.

To cut to the chase either there is a true conflict or there is not. If not, the bounds can be adjusted in one or both libraries creating an overlapping range or they safely cannot.

At the moment I don't as of yet know which situation applies.

However, all too often it's the latter, a false positive conflict from the overally conservative narrow version bounds. However, I'd do the same myself, i.e. be very conservative in using a narrow attoparsec version bounds as Attoparsec is specific in stating that it is a) Experimental and b) unstable (Semantic Versioning 0.x.y.z versions)

So for the sake of discussion point, and only so, I'll specifically use attoparsec here a bit. Attoparsec was first published 6 years ago with 40+ version releases and is one of the most widely used libs out there as a dependency. Why is the only use option for attoparsec one of experimental / unstable with only 0.x.y.z versions? In other words, why couldn't as least one V1.x have been tied off?

Consider the following situations for a Haskell library developer thinking of using attoparsec this morning:

1) attoparsec as-is is experimental and a 0.x.y.z versioned library subject to at will API modification. => If I use it, I'm going to put a really narrow cabal bounds band on this thing.

2) attoaparsec could-be has a V1.x, V2.x, ... set of releases. Each major release series stable in the sense of a fixed set of functionality and corresponding api + semantics. I can safely set my cabal range on a specific Major.Minor version. IF some user sees a conflict in Major.Minor version down the road, the probability is pretty high that yep, we have a real conflict here and not a case of a pessimistic narrow bounds false positive conflict.

Of course every library has a Experimental phase before solidifying into a minimal viable V1.x library and evolving in a backwards incompatable manner via V2.x ... . However attoparsec has been out there for 6 years, over 40 versions and is wildly successful. Hackage life would be far easier if attoparsec had foundone or more points of library stability along the way.

Once more I'm simply using attoparsec as an example here and not picking on it per se. The main idea here is that if essential, widely used libraries declare themselves experimental and unstable for perpetuity people are going to react by applying very narrow bounds constraints resulting in too frequent false positive conflicts.

submitted by precium[link] [28 comments]

### mightybyte: Haskell Best Practices for Avoiding "Cabal Hell"

With that preface, here are my best practices that seem to make Cabal work pretty well for me in my development.

1. I make sure that I have no more than the absolute minimum number of packages installed as --global. This means that I don't use the Haskell Platform or any OS haskell packages. I install GHC directly. Some might think this casts too much of a negative light on the Haskell Platform. But everyone will agree that having multiple versions of a package installed at the same time is a significant cause of build problems. And that is exactly what the Haskell Platform does for you--it installs specific versions of packages. If you use Haskell heavily enough, you will invariably encounter a situation where you want to use a different version of a package than the one the Haskell Platform gives you.

2. Make sure ~/.cabal/bin is at the front of your path. Hopefully you already knew this, but I see this problem a lot, so it's worth mentioning for completeness.

3. Install happy and alex manually. These two packages generate binary executables that you need to have in ~/.cabal/bin. They don't get picked up automatically because they are executables and not package dependencies.

4. Make sure you have the most recent version of cabal-install. There is a lot of work going on to improve these tools. The latest version is significantly better than it used to be, so you should definitely be using it.

5. Become friends with "rm -fr ~/.ghc". This command cleans out your --user repository, which is where you should install packages if you're not using a sandbox. It sounds bad, but right now this is simply a fact of life. The Haskell ecosystem is moving so fast that packages you install today will be out of date in a few months if not weeks or days. We don't have purely functional nix-style package management yet, so removing the old ones is the pragmatic approach. Note that sandboxes accomplish effectively the same thing for you. Creating a new sandbox is the same as "rm -fr ~/.ghc" and then installing to --user, but has the benefit of not deleting everything else you had in --user.

6. If you're not working on a single project with one harmonious dependency tree, then use sandboxes for separate projects or one-off package compiles.

7. Learn to use --allow-newer. Again, things move fast in Haskell land. If a package gives you dependency errors, then try --allow-newer and see if the package will just work with newer versions of dependencies.

8. Don't be afraid to dive into other people's packages. "cabal unpack" makes it trivial to download the code for any package. From there it's often trivial to make manual changes to version bounds or even small code changes. If you make local changes to a package, then you can either install it to --user so other packages use it, or you can do "cabal sandbox add-source /path/to/project" to ensure that your other projects use the locally modified version. If you've made code changes, then help out the community by sending a pull request to the package maintainer. Edit: bergmark mentions that unpack is now "cabal get" and "cabal get -s" lets you clone the project's source repository.

9. If you can't make any progress from the build messages cabal gives you, then try building with -v3. I have encountered situations where cabal's normal dependency errors are not helpful. Using -v3 usually gives me a much better picture of what's going on and I can usually figure out the root of the problem pretty quickly.