Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Ambigous occurence Show

When i try to create an instance of the typeclass Show i get the places where i used show highlighted with the following message:

Ambiguous occurrence `show'
    It could refer to either `Prelude.show',
                             imported from `Prelude' at [..]\Company.hs   (and originally defined in `GHC.Show')
                          or `Company.show',
                             defined at [..]\Company.hs:29:5

Company.hs

module Company where
    import Data.List

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


    names=["age","name","title","income"]
    accesors=[show . age,show . name, show . title,show .income]

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

    data Company=Company{
        cname::String,
        people::[Man],
        yf::Int
    }deriving (Show)

    instance Show Man where 
    show w = intercalate "," (zipWith (\name acc->name++":"++acc w) names accesors)

P.S Added all the module which I just load in the Prelude.The error is with the show applied on all accesors.

Upvotes: 0

Views: 109

Answers (1)

sshine
sshine

Reputation: 16105

After your edit: Your problem is due to indentation. You need to add at least one space before show w = ..., otherwise it will become a top-level definition inside the Company module, causing there to be two separate show definitions of which one is not overloaded, rather than a part of the instance Show Man definition.

Before your edit: Your code doesn't execute because of missing parts, but adding those missing parts it also does not produce the warnings you're giving:

module Main where

import Data.List

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

data Man = Man { _age    :: Int
               , _name   :: String
               , _title  :: Title
               , _income :: Int
               }

instance Show Man where
  show m = intercalate "," (zipWith (\name acc -> name ++ ":" ++ acc m) names accessors)

data Company = Company { _cname  :: String
                       , _people :: [Man]
                       , _yf     :: Int
                       }

instance Show Company where
  show (Company cname people yf) =
    intercalate "\n  " $
      ("Company (" ++ cname ++ ", " ++ show yf ++ "):") : map show people

names :: [String]
names = ["age", "name", "title", "income"]

accessors :: [Man -> String]
accessors = [show . _age, show . _name, show . _title, show . _income]

main :: IO ()
main = do
  print alice
  print bob
  print (Company "Alice & Bob's" [alice, bob] 1)

alice, bob :: Man
alice = Man 41 "Alice" Manager 41000
bob = Man 42 "Bob" Manager 42000

Gives:

$ ./show
age:41,name:"Alice",title:Manager,income:41000
age:42,name:"Bob",title:Manager,income:42000
Company (Alice & Bob's, 1):
  age:41,name:"Alice",title:Manager,income:41000
  age:42,name:"Bob",title:Manager,income:42000

Although you would probably be quite happy with deriving (Show) for each of these.

Upvotes: 2

Related Questions