Context reducion Stack overflow

Hello, I'm trying to compile the following code:
import Data.IORef
class F f where m :: (a -> a) -> f a -> f a
class M m a where mm :: (a -> a) -> m -> IO ()
instance (F f, M m (f a)) => M m a where mm f v = mm (m f) v
And I'm getting: $ ghci teste.lhs -XMultiParamTypeClasses -XFlexibleInstances -XFlexibleContexts -XUndecidableInstances -XIncoherentInstances GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Failed, modules loaded: none. Prelude> ^DLeaving GHCi. GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( teste.lhs, interpreted ) teste.lhs:1:0: Context reduction stack overflow; size = 20 Use -fcontext-stack=N to increase stack size to N `$dM{atp} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a))))))))))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{atm} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a)))))))))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{atj} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a))))))))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{atg} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a)))))))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{atd} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a))))))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{ata} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a)))))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{at7} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f (f a))))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{at4} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f (f a)))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{at1} :: {M m (f (f (f (f (f (f (f (f (f (f (f (f a))))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asY} :: {M m (f (f (f (f (f (f (f (f (f (f (f a)))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asV} :: {M m (f (f (f (f (f (f (f (f (f (f a))))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asS} :: {M m (f (f (f (f (f (f (f (f (f a)))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asP} :: {M m (f (f (f (f (f (f (f (f a))))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asM} :: {M m (f (f (f (f (f (f (f a)))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asJ} :: {M m (f (f (f (f (f (f a))))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asG} :: {M m (f (f (f (f (f a)))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asD} :: {M m (f (f (f (f a))))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asA} :: {M m (f (f (f a)))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asx} :: {M m (f (f a))}' arising from a use of `mm' at teste.lhs:10:13-22 `$dM{asj} :: {M m (f a)}' arising from a use of `mm' at teste.lhs:10:13-22 Failed, modules loaded: none. An example of instances of these classes:
instance F [] where m = map
instance M (IORef a) a where mm = flip modifyIORef
I don't get what's the problem with my definition. Can someone clarify me on this? Greetings. -- marcot http://marcot.iaaeee.org/

On Mon, May 04, 2009 at 11:59:54AM -0300, Marco Túlio Gontijo e Silva wrote:
instance (F f, M m (f a)) => M m a where mm f v = mm (m f) v
Perhaps you mean instance (F f, M m a) => M m (f a) where ... ? What you have written means that to have an instance M m a, you must have an instance M m (f a), for which you must have an instance M m (f (f a)), for which you must have an instance M m (f (f (f a)))... What I have written above expresses what I presume you meant: we can automatically derive an instance of (M m) for some structured type (f a) as long as we have a base instance M m a and can lift functions (a -> a) to (f a -> f a). -Brent

Em Seg, 2009-05-04 às 15:54 -0400, Brent Yorgey escreveu:
On Mon, May 04, 2009 at 11:59:54AM -0300, Marco Túlio Gontijo e Silva wrote:
instance (F f, M m (f a)) => M m a where mm f v = mm (m f) v
Perhaps you mean
instance (F f, M m a) => M m (f a) where ...
?
No, I really meant what I wrote. An example: If I the instances I wrote:
instance F [] where m = map
instance M (IORef a) a where mm = flip modifyIORef
I want to define:
instance M (IORef [a]) a where mm f v = mm (m f) v
This could of course be written as: mm = mm . m I'd like to get this last instance automaticly from that definition, having: f = [] m = IORef [a]
What you have written means that to have an instance M m a, you must have an instance M m (f a), for which you must have an instance M m (f (f a)), for which you must have an instance M m (f (f (f a)))...
After this explanation I think I located the problem. My problem seems to be with how IncoherentInstances work, by picking always the most generic instance. Thanks for that. Do you know if there a way to get around this, maybe using another GHC extension? Greetings. -- marcot http://marcot.iaaeee.org/

On Tue, May 05, 2009 at 10:28:10AM -0300, Marco Túlio Gontijo e Silva wrote:
Em Seg, 2009-05-04 às 15:54 -0400, Brent Yorgey escreveu:
On Mon, May 04, 2009 at 11:59:54AM -0300, Marco Túlio Gontijo e Silva wrote:
instance (F f, M m (f a)) => M m a where mm f v = mm (m f) v
Perhaps you mean
instance (F f, M m a) => M m (f a) where ...
?
No, I really meant what I wrote. An example: If I the instances I wrote:
instance F [] where m = map
instance M (IORef a) a where mm = flip modifyIORef
I want to define:
instance M (IORef [a]) a where mm f v = mm (m f) v
This could of course be written as:
mm = mm . m
I'd like to get this last instance automaticly from that definition, having:
f = [] m = IORef [a]
I don't follow. In order to get instance M (IORef [a]) a from instance (F f, M m (f a)) => M m a would require instance M (IORef [a]) (f a) for some f, which you don't have. I might try rewriting M as class M f a where mm :: (a -> a) -> f a -> IO () and then your automatic lifting instance would be something like instance (F f, M f2 a) => M (f :.: f2) a where :.: denotes functor composition. -Brent

Em Ter, 2009-05-05 às 11:33 -0400, Brent Yorgey escreveu:
On Tue, May 05, 2009 at 10:28:10AM -0300, Marco Túlio Gontijo e Silva wrote:
Em Seg, 2009-05-04 às 15:54 -0400, Brent Yorgey escreveu:
On Mon, May 04, 2009 at 11:59:54AM -0300, Marco Túlio Gontijo e Silva wrote:
instance (F f, M m (f a)) => M m a where mm f v = mm (m f) v
Perhaps you mean
instance (F f, M m a) => M m (f a) where ...
?
No, I really meant what I wrote. An example: If I the instances I wrote:
instance F [] where m = map
instance M (IORef a) a where mm = flip modifyIORef
I want to define:
instance M (IORef [a]) a where mm f v = mm (m f) v
This could of course be written as:
mm = mm . m
I'd like to get this last instance automaticly from that definition, having:
f = [] m = IORef [a]
I don't follow. In order to get
instance M (IORef [a]) a
from
instance (F f, M m (f a)) => M m a
would require
instance M (IORef [a]) (f a)
for some f, which you don't have.
Yes, I have this instance defined for f = []: instance M (IORef [a]) [a] which is a instance of: instance M (IORef a) a
I might try rewriting M as
class M f a where mm :: (a -> a) -> f a -> IO ()
and then your automatic lifting instance would be something like
instance (F f, M f2 a) => M (f :.: f2) a
where :.: denotes functor composition.
Ok, I think this is another possibility. But how could I define :.:? Greetings. -- marcot http://marcot.iaaeee.org/

On Tue, May 05, 2009 at 03:07:16PM -0300, Marco Túlio Gontijo e Silva wrote:
and then your automatic lifting instance would be something like
instance (F f, M f2 a) => M (f :.: f2) a
where :.: denotes functor composition.
Ok, I think this is another possibility. But how could I define :.:?
newtype (g :.: f) a = O { unO :: g (f a) } Something like this is already defined in both the TypeCompose and category-extras libraries. -Brent
participants (2)
-
Brent Yorgey
-
Marco Túlio Gontijo e Silva