n00b circular dep question

Hi all, This is my first post, and I'm a n00b, so go easy if I breach protocol. :-) I have a graph where the nodes contain a number of fields of various types. The values for one of those fields -- call it Mass -- need to reside in a separate file from the main graph definition (this is for workflow reasons and is given to me as a constraint). In order to set the Mass for a particular node, I need access to the other fields of that node. The other constraint is that the graph needs to be available in an interpreter (eg GHCi). So, I have a circular dependency in my modules that I don't know how to resolve in Haskell. I went looking for the equivalent of #include which is how I would have solved this in C++. I'm sure there is a simple answer to this and I'm hoping this group can point me in the right direction. Many thanks, ogina

On Apr 25, 2008, at 11:54 AM, Jennifer Miller wrote:
I have a circular dependency in my modules that I don't know how to resolve in Haskell.
I'm pretty sure that GHC will sort out those dependencies for you as long as you are exporting the correct things from each module. - Jake

On Apr 25, 2008, at 9:54 AM, Jennifer Miller wrote:
So, I have a circular dependency in my modules that I don't know how to resolve in Haskell. I went looking for the equivalent of #include which is how I would have solved this in C++. I'm sure there is a simple answer to this and I'm hoping this group can point me in the right direction.
GHC supports CPP, so you could take the same approach in Haskell. # cat Main.hs data Foo = Foo { x :: Int, y :: Int } deriving Show foo = Foo { x = 2, #include "y.hs" } # cat y.hs y = (x foo) + 2 # ghci -cpp Main.hs GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( Main.hs, interpreted ) Ok, modules loaded: Main. *Main> foo Foo {x = 2, y = 4} *Main> Aaron

Hello Jennifer, Friday, April 25, 2008, 8:54:42 PM, you wrote:
So, I have a circular dependency in my modules that I don't know how to resolve in Haskell.
1. haskell standard allows circular deps between modules 2. ghc supports this part of standard in a rather awkward way - you need to generate .hs-boot files using some switch (look into docs). which is like .h files generated automatic from .cpp. once these files aregenerated, your circular deps will be ok -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On Sat, Apr 26, 2008 at 4:07 AM, Bulat Ziganshin
2. ghc supports this part of standard in a rather awkward way - you need to generate .hs-boot files using some switch (look into docs). which is like .h files generated automatic from .cpp. once these files aregenerated, your circular deps will be ok
Are you sure? I would be very interested in a switch that automatically generates hs-boot files, but I've been unable to find any mention of it. Stuart

Hello Stuart, Saturday, April 26, 2008, 1:57:47 PM, you wrote:
Are you sure? I would be very interested in a switch that automatically generates hs-boot files, but I've been unable to find any mention of it.
no, i never used this function, so you should be informed better -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On Fri, 25 Apr 2008, Jennifer Miller wrote:
I have a graph where the nodes contain a number of fields of various types. The values for one of those fields -- call it Mass -- need to reside in a separate file from the main graph definition (this is for workflow reasons and is given to me as a constraint). In order to set the Mass for a particular node, I need access to the other fields of that node. The other constraint is that the graph needs to be available in an interpreter (eg GHCi).
So, I have a circular dependency in my modules that I don't know how to resolve in Haskell. I went looking for the equivalent of #include which is how I would have solved this in C++. I'm sure there is a simple answer to this and I'm hoping this group can point me in the right direction.
Sometimes circular module dependencies can be solved by using a type parameter. mutually recursive: module A where data LabeledTree label = LabeledTree label (Tree label) module B where data Tree label = Branch (LabeledTree label) (LabeledTree label) | Leaf no mutual recursion: module A where data LabeledTreeGen tree label = LabeledTree label (tree label) module B where type LabeledTree label = LabeledTreeGen Tree label data Tree label = Branch (LabeledTree label) (LabeledTree label) | Leaf

Thanks for the suggestions. The #include worked but I will look for ways to remove the circular dependency altogether.
Jennifer
On Friday, April 25, 2008, at 02:48PM, "Henning Thielemann"
On Fri, 25 Apr 2008, Jennifer Miller wrote:
I have a graph where the nodes contain a number of fields of various types. The values for one of those fields -- call it Mass -- need to reside in a separate file from the main graph definition (this is for workflow reasons and is given to me as a constraint). In order to set the Mass for a particular node, I need access to the other fields of that node. The other constraint is that the graph needs to be available in an interpreter (eg GHCi).
So, I have a circular dependency in my modules that I don't know how to resolve in Haskell. I went looking for the equivalent of #include which is how I would have solved this in C++. I'm sure there is a simple answer to this and I'm hoping this group can point me in the right direction.
Sometimes circular module dependencies can be solved by using a type parameter.
mutually recursive:
module A where
data LabeledTree label = LabeledTree label (Tree label)
module B where
data Tree label = Branch (LabeledTree label) (LabeledTree label) | Leaf
no mutual recursion:
module A where
data LabeledTreeGen tree label = LabeledTree label (tree label)
module B where
type LabeledTree label = LabeledTreeGen Tree label
data Tree label = Branch (LabeledTree label) (LabeledTree label) | Leaf
participants (6)
-
Aaron Tomb
-
Bulat Ziganshin
-
Henning Thielemann
-
Jake Mcarthur
-
Jennifer Miller
-
Stuart Cook