1 -----------------------------------------------------------------------------
3 -- Module : Data.Generics.List
4 -- Copyright : (c) The University of Glasgow, CWI 2001--2004
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable
11 -- \"Scrap your boilerplate\" --- Generic programming in Haskell
12 -- See <http://www.cs.vu.nl/boilerplate/>. The present module illustrates
13 -- one possible treatment of polymorphic datatypes for specialising
16 -----------------------------------------------------------------------------
18 module Data.Generics.List (
20 -- * Processing polymorphic lists
30 ------------------------------------------------------------------------------
36 import Data.Generics.Basics
38 -------------------------------------------------------------
40 -- Processing polymorphic lists
42 -------------------------------------------------------------
45 -- | Test for list datatype
46 isList :: Data a => a -> Bool
47 isList x = typerepTyCon (typeOf x) ==
48 typerepTyCon (typeOf (undefined::[()]))
52 isNil :: Data a => a -> Bool
53 isNil x = toConstr x == toConstr ([]::[()])
57 isCons :: Data a => a -> Bool
58 isCons x = toConstr x == toConstr (():[])
61 -- | gmapQ for polymorphic lists; Nothing for other than lists
62 lgmapQ :: forall a q. Data a => (forall a. Data a => a -> q) -> a -> Maybe [q]
66 else Just ( if isNil x
69 then ( gmapQi 0 f x : gmapQi 1 (fromJust . lgmapQ f) x )