complete noob to Haskell here with probably an even noobier question. I’m trying to get ghci output working and am stuck on instance declarations. How could I declare an instance for “(Show (Stack -> Stack))” given:
data Cmd = LD Int
| ADD
| MULT
| DUP
deriving Show
type Prog = [Cmd]
type Stack = [Int]
type D = Stack -> Stack
I’ve been trying to create a declaration like:
instance Show D where show = Stack
but all my attempts have resulted in illegal instance declarations. Any help and/or references much appreciated!
First of all, by default, type synonyms (that is, stuff defined using
type) aren’t legal in instance declarations. There’s a GHC extensions to allow this, however.Beyond that, in this specific case,
showneeds to return aString; your instance is trying to return a… type synonym name, which doesn’t even make sense to begin with, and besides refers to a list ofInt, which is the wrong return type forshow.Finally,
Dis a function type–what is that supposed toshow, anyway? There’s really not much you can meaningfully do with aShowinstance on a function type, in most cases.If you just want it to say “this is type D”, you could write an instance like this:
I’m not sure how useful that is in practice, though.