[project @ 2001-08-22 11:45:06 by sewardj]
[ghc-hetmet.git] / ghc / tests / deriving / should_run / drvrun006.hs
1 -- !!! Show/Read deriving example given in the Haskell Report.
2 module Main(main) where
3
4 infix 4 :^:
5 data Tree a 
6   =  Leaf a  | (Tree a) :^: (Tree a)
7      deriving (Show, Read)
8
9 val1 :: Tree Int
10 val1 = Leaf 2
11
12 val2 :: Tree Int
13 val2 = Leaf 2 :^: Leaf (-1)
14
15 main = do
16   print val1
17   print val2
18
19   print ((read (show val1))::Tree Int)
20   print ((read (show val2))::Tree Int)
21   print ((read (show val1))::Tree Integer)
22   print ((read (show val2))::Tree Integer)
23
24 {- What you'll want
25 instance (Show a) => Show (Tree a) where
26
27         showsPrec d (Leaf m) = showParen (d >= 10) showStr
28           where
29              showStr = showString "Leaf " . showsPrec 10 m
30
31         showsPrec d (u :^: v) = showParen (d > 4) showStr
32           where
33              showStr = showsPrec 5 u . 
34                        showString " :^: " .
35                        showsPrec 5 v
36
37 instance (Read a) => Read (Tree a) where
38
39         readsPrec d r =  readParen (d > 4)
40                          (\r -> [(u:^:v,w) |
41                                  (u,s) <- readsPrec 5 r,
42                                  (":^:",t) <- lex s,
43                                  (v,w) <- readsPrec 5 t]) r
44
45                       ++ readParen (d > 9)
46                          (\r -> [(Leaf m,t) |
47                                  ("Leaf",s) <- lex r,
48                                  (m,t) <- readsPrec 10 s]) r
49 -}