
I have two modules Structure and Component. Module Structure defines data Structure = Component ... Module Component defines data Component = ... I would like Structure to import Component. But Component includes functions that take a Structure as an argument. So I have (or would like) something like this organization. Module Structure where import Component data Structure = Component ... Module Component where data Component = ... f :: Structure -> Component ->Structure The functions in Component are really very Component related and should not be moved to Structure. So how can I set up this circular relationship? I thought that one approach would be to do something like the following. But when I try I get all fouled up. Help would be appreciated. Module Structure where class Comp data Structure = Comp ... Module Component where import Structure data Component = ... instance Comp Component If I do this I get a complaint that class Comp needs a type parameter. If I give it an artificial one, that leads to all sorts of other problems. Is there a better way? Thanks. * -- Russ *

On Sat, Dec 11, 2010 at 7:37 PM, Russ Abbott
I have two modules Structure and Component. Module Structure defines data Structure = Component ... Module Component defines data Component = ... I would like Structure to import Component. But Component includes functions that take a Structure as an argument. So I have (or would like) something like this organization.
[snip]
The functions in Component are really very Component related and should not be moved to Structure. So how can I set up this circular relationship?
GHC allows you to compile mutually recursive modules, see http://www.haskell.org/ghc/docs/latest/html/users_guide/separate-compilation... for how to do it. -- Jedaï

On Sat, Dec 11, 2010 at 08:56:23PM +0100, Chaddaï Fouché wrote:
On Sat, Dec 11, 2010 at 7:37 PM, Russ Abbott
wrote: I have two modules Structure and Component. Module Structure defines data Structure = Component ... Module Component defines data Component = ... I would like Structure to import Component. But Component includes functions that take a Structure as an argument. So I have (or would like) something like this organization.
[snip]
The functions in Component are really very Component related and should not be moved to Structure. So how can I set up this circular relationship?
GHC allows you to compile mutually recursive modules, see http://www.haskell.org/ghc/docs/latest/html/users_guide/separate-compilation... for how to do it.
Yes, this is possible, but it has always seemed sort of fragile and ugly to me. Another suggestion is to put the definitions of the Structure and Component data types into a separate module called Types, and then import Types into both the Structure module (which defines functions over Structures) and the Component module (which defines functions over Components). Then no circularity is needed. -Brent
participants (3)
-
Brent Yorgey
-
Chaddaï Fouché
-
Russ Abbott