Module imports form a cycle - haskell

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

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
}

Related

How can I specify the package when instantiating a class?

I have many classes with the same name in different packages. Take the following directory structure as an example:
From within the constructor of the templates/Person class, how can I declare an instance of rules/Person?
The code I expected to work (but has an error on line 10) is as follows:
from Template import Template
import rules
class Person(Template):
def __init__(self):
super(Person, self).__init__('Person')
self.rules = [
rules.Person() #this is an error
]
PEP328 has the answer, I think.
from .myfolder import Template as Template
from .myfolder1 import Template as Template1
from .myfolder2 import Template as Template2
from pip_installed_library1 import Template as Template3
from pip_installed_library2 import Template as Template4
Using this pattern they never share names.

In Haskell, is it possible to qualify part of an imported module?

I'd like to be able to do something like: import qualified Data.Massiv.Array (qualified map).
This gives error: parse error on input `map'.
Or better yet, import qualified Data.Massiv.Array (qualified map) as AM, so I also can access foo as either foo or AM.foo, unless foo == map, then I have to use AM.map. This is to avoid conflict with Prelude.map.
Write two imports and you can use map as AM.map and use other functions without AM..
import qualified Data.Massiv.Array as AM
import Data.Massiv.Array hiding (map)

Haskell error Not in scope: data constructor

I wrote some simple module in Haskell and then import it in other file. Then I'm trying to use functions with data constructors from my module — there is an error Not in scope: data constructor: <value>. How can I fix it?
Note: when I'm using it in interpreter after importing — all is good without errors.
My module Test.hs:
module Test (test_f) where
data Test_Data = T|U|F deriving (Show, Eq)
test_f x
| x == T = T
| otherwise = F
And my file file.hs:
import Test
some_func = test_f
No error if I'm writing in interpreter:
> :l Test
> test_f T
T
In interpreter I'm trying to execute some_func T, but there is an error. And how can I use class Test_Data in my file to describe annotations?
You aren't exporting it from your module:
module Test (test_f, Test_Data(..)) where
The (..) part says "export all constructors for TestData".
You have an explicit export list in your module Test:
module Test (test_f) where
The export list (test_f) states that you want to export the function test_f and nothing else. In particular, the datatype Test_Data and its constructors are hidden.
To fix this, either remove the export list like this:
module Test where
Now all things will be exported.
Or add the datatype and its constructors to the export list like this:
module Test (test_f, Test_Data(..)) where
The notation Test_Data(..) exports a datatype with all corresponding constructors.

Successfully imported Data Constructor not in scope?

What is going on here? I'm importing a data constructor, apparently successfully because I don't get an error, but when I try to use the constructor, I get an error saying its not in scope!
In Test.hs:
import Database.Persist (Key)
main = Key
Result:
$ ghc test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
test.hs:3:8: Not in scope: data constructor `Key'
import Database.Persist (Key)
The above imports a type named Key but none of its constructors. To import the constructor Key of the type Key you need to do
import Database.Persist (Key(Key))
or just
import Database.Persist (Key(..))
to import all constructors of the given type.
In order to import a constructor you must use the following syntax
import Database.Persist (Key (..))
Generally, when importing a type or typeclass by name only the type gets imported. The constructors and member functions must be imported using the Name (..) or Name (Constructor) syntax. This is fairly convenient as it's often the case that you need to write a type signature using an imported type even if you don't ever need to construct or examine values of that type.

Cabal rebuild on embedded file change

I'm using the file-embed package thusly:
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B (w2c)
import qualified Data.FileEmbed as E
initWindow = do
b <- Gtk.builderNew
let glade = map B.w2c $ B.unpack $ $(E.embedFile "window.glade") in
Gtk.builderAddFromString b glade
...
Is it possible to make cabal rebuild this file even when only the glade file changes?
Support for this will be/has been added in GHC 7.4/7.6. The problem is that the compiler doesn't allow TemplateHaskell splices to add file dependencies yet. See this ticket for more information.
When this change lands, you can use the following code to create a new embedFile' function:
import Data.FileEmbed
import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Lib
embedFile' :: FilePath -> Q Exp
embedFile' path = do
qAddDependentFile path
embedFile path
This has to be in a separate module from where you use the TH function.

Resources