views:

217

answers:

1

Hello, 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!

+5  A: 

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, show needs to return a String; 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 of Int, which is the wrong return type for show.

Finally, D is a function type--what is that supposed to show, anyway? There's really not much you can meaningfully do with a Show instance 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:

{-# LANGUAGE TypeSynonymInstances #-}
instance Show D where show _ = "Stack -> Stack"

I'm not sure how useful that is in practice, though.

camccann
Maybe he wants to show the mutation from one [ Int ] to another [ Int ]?Not certain how you would go intercepting this, displaying the parameter then the resultant.
Dan