Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Module imports form a cycle

Hello i was wondering how do you solve circular dependency in Haskell

I have :
Module for a type.

module Company where

import Tclass

data Worker=Worker{
    age::Int,
    name::String,
    title::Title,
    income::Int
}

data Title=Manager | Dev | Tester deriving (Show)


data Company=Company{
    cname::String,
    people::[Worker],
    yf::Int
}

Module for the instance of the typeclass

module Tclass where
import System.Environment
import Company

class Console a where
    wLine::a->IO()
    rLine::IO String->a

instance Show Worker where
    show w="to be defined"
    print =putStr . show 

If i just want to load the type in ghci and use it ; how do i import them without getting the error:

Module imports form a cycle:
         module `Tclass' (.\Tclass.hs)
        imports `Company' (Company.hs)
  which imports `Tclass' (.\Tclass.hs)

P.S I have tried creating a auxiliary module that imports the two modules and the two modules would import it too,but no luck with this one either :

module Au where
 import Tclass
 import Company

module Company
 import Au

module Tclass 
 import Au

Upvotes: 3

Views: 1643

Answers (1)

Olivier Sohn
Olivier Sohn

Reputation: 1322

Moving the Show instance to the other module, you can break the cycle:

module Tclass where
import System.Environment
import Company

class Console a where
    wLine::a->IO()
    rLine::IO String->a

and

module Company where

data Worker=Worker{
    age::Int,
    name::String,
    title::Title,
    income::Int
}
instance Show Worker where
    show w="to be defined"
    print =putStr . show 

data Title=Manager | Dev | Tester deriving (Show)

data Company=Company{
    cname::String,
    people::[Worker],
    yf::Int
}

Upvotes: 4

Related Questions