21
votes

I am playing with app architecture and free monads in haskell. I've got it down, except for how to lift my "instruction" into the correct slot of my coproduct without explicitly giving the full Left/Right path.

Here's the haskell example I've been working from: https://gist.github.com/aaronlevin/87465696ba6c554bc72b#file-reasonable-hs

Here, to inject types into the coproduct, we explicitly mention the path.

For instance:

Program :: Coproduct Interaction (Coproduct Auth Logging) a
logC :: (Functor f) => (forall a. Logging a -> f a) -> String -> Free f ()

logger :: String -> Free Program ()
logger = logC (Program . Coproduct . Right . Coproduct . Right)

Here, logger has to be put in the right slot in the coproduct manually with Coproduct . Right . Coproduct . Right

Runar's talk in scala uses implicit type conversions and an Inject typeclass to achieve this result: https://gist.github.com/runarorama/a8fab38e473fafa0921d#file-gistfile1-scala-L119

In short, I'm wondering if there's a way to do this in haskell.

1
Reminds me offhand of Swiestera's Data Types a la Carte which uses a type class prolog trick to slot effects into the right place in a stack.J. Abrahamson
This was the correct answer. This is what helped me: okmij.org/ftp/Haskell/extensible/ALaCarte.hs Thanks so much for leading me to the answerBrian
@Brian If you have solved your problem could you provide an answer to the question and accept it?Bakuriu
What do you mean by Program :: Coproduct Interaction (Coproduct Auth Logging) a? That's not a valid kind signature, nor the type of a data constructor. Is it supposed to be some newtype declaration?dfeuer

1 Answers

1
votes

Filling out the answer section from the comments to the original question...

The original Haskell paper can be found here: Data Types à la Carte

A Haskell implementation can be found here: ALaCarte.hs