Automatisches Ableiten von Instanzen (I)

data Tree a = Branch { key :: a 
                   , left :: Tree a 
                   , right :: Tree a
                   }
            | Leaf
instance Show a => Show (Tree a) where
    show t @ (Branch {}) = 
        "Branch{" ++ "key="  ++ show (key  t) ++ ","
                ++ "left=" ++ show (left t) ++ ","
                ++ "left=" ++ show (left t) ++ "}"
    show Leaf = "Leaf"
Beachte: generische Instanz mit Typconstraint

Das kann der Compiler selbst:

data Tree a = ... deriving Show



Johannes Waldmann 2010-01-25