Please read my problem below, after the bold text, before taking your time to scrutinize this code. I don't want to waste your time if you can't answer this.
Okay. I have created my own data type within Haskell. It is
data Dialogue= Choice String [(String, Dialogue)]
| Action String Event
-- deriving (Show)
Note the commented out 'deriving (Show)' which is important for my problem below.
I have a function named dialogue defined as
dialogue:: Game -> Dialogue -> IO Game
dialogue (Game n p ps) (Action s e) = do
putStrLn s
return (e (Game n p ps))
dialogue (Game n p ps) (Choice s xs) = do
putStrLn s
let ys = [ fst a | a <- xs ]
let i = [1..length ys]
putStrLn (enumerate 1 ys)
str <- getLine
if str `elem` exitWords
then do
return (Game n p ps)
else do
let c = read str::Int
if c `elem` i
then do
let ds = [ snd b | b <- xs ]
let d = ds !! c
putStrLn $ show d
return (Game n p ps)
else do
error "error"
My data type game is defined as
data Game = Game Node Party [Party] | Won
deriving (Eq,Show)
And an Event is a type, defined by myself as
type Event = Game -> Game
Now, this is where my problem occurs. When I go to load this file within cmd and I have not included deriving (Show) within my data type Dialogue, I get the following error:
* No instance for (Show Dialogue) arising from a use of `show'
* In the second argument of `($)', namely `(show d)'
In a stmt of a 'do' block: putStrLn $ (show d)
In the expression:
do let ds = ...
let d = ds !! c
putStrLn $ (show d)
return (Game n p ps)
|
120 | putStrLn $ (show d)
It would seem to me that I need to include the deriving (Show) in order to be able to print this data type to the console. However, when I do include deriving (Show), I get this error:
* No instance for (Show Event)
arising from the second field of `Action' (type `Event')
(maybe you haven't applied a function to enough arguments?)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
* When deriving the instance for (Show Dialogue)
|
85 | deriving Show
I have spent quite a long time trying to find out why this might be happening. But I cannot find anywhere online that seems to document this particular problem.
Any help would be perfect, or even just a link to an appropriate explanation.
**Edit: ** My Event is a type synonym and so I cannot add deriving Show to this
Thanks a lot
Event
has to have aShow
instance for things containing it to be able to automatically deriveShow
. That probably means addingderiving Show
to yourEvent
definition. – Ry-♦Event
type is an alias and can not carry a deriving clause. It is also a function so automatic deriving won't work. – Thomas M. DuBuissonEvent
? Functions can not be printed in general. Perhaps you could define your own custominstance Show Dialogue
without relying onShow Event
, e.g. by showing that as some generic string "<event>". – chi