[Haskell-cafe] Stuck on design problem

Adam Gundry adam.gundry at strath.ac.uk
Mon May 20 14:37:26 CEST 2013


Hi Nicolas,

Your design doesn't look too unreasonable, though I haven't looked at in
detail. I do have a quick observation regarding the implementation that
I hope might help. Your gist defines

> class MonadLog m a where
>   getEntry :: Index -> m (Entry a)
>
> instance MonadLog (MemLog a) a

and then you hit the error

> No instance for (MonadLog (MemLog a0) ())

which is a tell-tale ambiguity problem. This is a common issue with
multi-parameter type classes. GHC cannot determine the existential
variable a0, because it will not commit to the instance for MemLog in
case a more specific instance turns up.

One option is to turn on GADTs or TypeFamilies and write

> instance a ~ a' => MonadLog (MemLog a) a'

which will allow the instance to match and generate the easily solved
constraint a0 ~ (). You may need to do the same for other instances.

Alternatively, you could add a functional dependency

> class MonadLog m a | m -> a

or use a type family:

> class MonadLog' m where
>   type Element m
>   getEntry' :: Index -> m (Entry (Element m))

> instance MonadLog' (MemLog a) where
>   type Element (MemLog a) = a
>   getEntry' i = flip (IntMap.!) i `fmap` ask


Hope this helps,

Adam


On 20/05/13 12:25, Nicolas Trangez wrote:
> All,
> 
> Since I'm stuck on a coding problem and can't figure out how to proceed,
> I decided to call for help.
> 
> I'm unable to get some code to typecheck, so maybe I'm missing something
> obvious on the implementation side, or more likely the design is
> completely wrong.
> 
> Here's the idea: I have some code which uses some log of "Entry a"
> values (for some polymorphic type 'a'), and provides actions which
> output "Command a" values which should be interpreted by some wrapper
> code, e.g. adding some new entries to the log. These actions are
> implemented in a custom WriterT transformer (in this demo code, the
> original is more complex) to output Commands, while the inner monad
> should give access to the log entries (this could be in-memory like in
> the example, or stored on disk so using IO).
> 
> You can find a trimmed-down version at
> https://gist.github.com/NicolasT/4230251f4f87f110d197
> 
> This doesn't type-check, and I'm not sure how to proceed. Am I taking a
> wrong approach? Do I need a different design?
> 
> Thanks,
> 
> Nicolas
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 


-- 
The University of Strathclyde is a charitable body, registered in
Scotland, with registration number SC015263.



More information about the Haskell-Cafe mailing list