showing a user defined type

I've been playing around with "The Little MLer" (pg. 95, 96) to try to improve my understanding of types. I can ask ML: - ints(0); val it = Link (1,fn) : chain - and Haskell: *Main> :t ints 0 ints 0 :: Chain *Main> ints 0 <interactive>:1:0: No instance for (Show Chain) arising from a use of `print' at <interactive>:1:0-5 Possible fix: add an instance declaration for (Show Chain) In a stmt of a 'do' expression: print it *Main> I think I need to write a show function for type Chain but not sure how to proceed. Michael =============== ;;ML datatype chain = Link of (int * (int -> chain)) fun ints(n) = Link(n + 1, ints) ;;Haskell data Chain = Link Int (Int -> Chain) ints :: Int -> Chain ints n = Link (n+1) ints

On May 18, 2009, at 21:19 , michael rice wrote:
*Main> :t ints 0 ints 0 :: Chain *Main> ints 0
<interactive>:1:0: No instance for (Show Chain)
In general, you want to append deriving Show to your types. You may also want to be able to input them in ghci, so instead say deriving (Show, Read) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Unfortunately, you can't derive Show on Chain as defined, because it contains a function:
data Chain = Link Int (Int -> Chain)
You can write this:
instance Show Chain where show (Link n _) = "Link " ++ show n ++ " <fn>"
Or you can make a dummy "Show" instance for functions:
instance Show (a -> b) where show _ = "<fn>" data Chain = Link Int (Int -> Chain) deriving Show
One question: Do you expect to ever call the function with a different
value? For example:
otherChain :: Chain
otherChain = case (ints 0) of Link _ f -> f 100
If not, you can replace Chain entirely by [Int], due to laziness,
something that's not possible in ML. (Although you can get the same
result in ML by using (int * (() -> chain)) instead.
-- ryan
On Mon, May 18, 2009 at 6:36 PM, Brandon S. Allbery KF8NH
On May 18, 2009, at 21:19 , michael rice wrote:
*Main> :t ints 0 ints 0 :: Chain *Main> ints 0
<interactive>:1:0: No instance for (Show Chain)
In general, you want to append deriving Show to your types. You may also want to be able to input them in ghci, so instead say deriving (Show, Read) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, May 18, 2009 at 10:02 PM, Ryan Ingram
Unfortunately, you can't derive Show on Chain as defined, because it contains a function:
Sure you can. I just tried the following, and it compiled without complaints.
import Text.Show.Functions
data Chain = Link Int (Int -> Chain) deriving (Show)
The usual warnings about orphan instances apply, but the purpose of
the Text.Show.Functions module is to provide a standard Show instance
for functions so that libraries (e.g., QuickCheck) don't declare
conflicting instances.
--
Dave Menendez
participants (4)
-
Brandon S. Allbery KF8NH
-
David Menendez
-
michael rice
-
Ryan Ingram