1 -- | This module provides the HsValueMap type, which can structurally map a
2 -- Haskell value to something else.
3 module HsValueMap where
7 import Control.Applicative
8 import Data.Traversable
11 -- | A datatype that maps each of the single values in a haskell structure to
12 -- a mapto. The map has the same structure as the haskell type mapped, ie
14 data HsValueMap mapto =
15 Tuple [HsValueMap mapto]
17 deriving (Show, Eq, Ord)
19 instance Functor HsValueMap where
20 fmap f (Single s) = Single (f s)
21 fmap f (Tuple maps) = Tuple (map (fmap f) maps)
23 instance Foldable HsValueMap where
24 foldMap f (Single s) = f s
25 -- The first foldMap folds a list of HsValueMaps, the second foldMap folds
26 -- each of the HsValueMaps in that list
27 foldMap f (Tuple maps) = foldMap (foldMap f) maps
29 instance Traversable HsValueMap where
30 traverse f (Single s) = Single <$> f s
31 traverse f (Tuple maps) = Tuple <$> (traverse (traverse f) maps)
33 data PassState s x = PassState (s -> (s, x))
35 instance Functor (PassState s) where
36 fmap f (PassState a) = PassState (\s -> let (s', a') = a s in (s', f a'))
38 instance Applicative (PassState s) where
39 pure x = PassState (\s -> (s, x))
40 PassState f <*> PassState x = PassState (\s -> let (s', f') = f s; (s'', x') = x s' in (s'', f' x'))
42 -- | Creates a HsValueMap with the same structure as the given type, using the
43 -- given function for mapping the single types.
45 Type.Type -- ^ The type to map to a HsValueMap
46 -> HsValueMap Type.Type -- ^ The resulting map and state
49 case Type.splitTyConApp_maybe ty of
51 if (TyCon.isTupleTyCon tycon)
53 Tuple (map mkHsValueMap args)