Skip to content
Newer
Older
100644 41 lines (28 sloc) 1.04 KB
4760ff3 HLint police
Eyal Lotem authored Oct 20, 2011
1 {-# LANGUAGE TemplateHaskell, FlexibleInstances, MultiParamTypeClasses, KindSignatures #-}
c66178c Added haddock style top-level documentation, with CartesianList example
Michael Sloan authored Jun 21, 2011
2
3 import Control.Newtype
4 import Control.Newtype.TH
5 import Data.Monoid
6 import Data.Foldable (fold)
7
4760ff3 HLint police
Eyal Lotem authored Oct 20, 2011
8 newtype Yarn = Yarn String
c66178c Added haddock style top-level documentation, with CartesianList example
Michael Sloan authored Jun 21, 2011
9
10 newtype Occasionally a = Occasionally (Maybe a)
11
12 newtype (Num a, Show b) => Endocrine a b = Endocrine { pump :: a -> b }
13
14 newtype Kinda (a :: * -> *) (b :: *) = Kinda b
15
16 newtype CartesianList a = CartesianList [[a]] deriving Show
17
18 instance Monoid (CartesianList a) where
19 mempty = pack [[]]
20 a `mappend` b = pack [x ++ y | x <- unpack a, y <- unpack b]
21
22 $(mkNewTypes [''Yarn, ''Occasionally, ''Endocrine, ''Kinda, ''CartesianList])
23
24 pun :: (Newtype a b, Show b) => a -> IO ()
25 pun = print . unpack
26
27 klift :: Int -> Kinda Maybe Int
28 klift = Kinda
29
30 single x = [x]
31
32 main :: IO ()
33 main = do
34 -- Let's see if we can use them. Going to assume that pack works..
35 pun $ Yarn "ball"
36 pun . Occasionally $ Just "ice"
37 print $ unpack (Endocrine show) 42
38 pun . klift $ 5
39 print $ underF CartesianList (\xs -> [fold xs])
4760ff3 HLint police
Eyal Lotem authored Oct 20, 2011
40 [[[4],[5],[6]], [[1],[2]], [[0]]]
Something went wrong with that request. Please try again.