]> git.rkrishnan.org Git - yorgey.git/blob - hw7/Sized.hs
hw7: starting materials
[yorgey.git] / hw7 / Sized.hs
1 {-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
2 module Sized where
3
4 import Data.Monoid
5
6 newtype Size = Size Int
7   deriving (Eq, Ord, Show, Num)
8
9 getSize :: Size -> Int
10 getSize (Size i) = i
11
12 class Sized a where
13   size :: a -> Size
14
15 instance Sized Size where
16   size = id
17
18 -- This instance means that things like
19 --   (Foo, Size)
20 --   (Foo, (Bar, Size))
21 --   ...
22 -- are all instances of Sized.
23 instance Sized b => Sized (a,b) where
24   size = size . snd
25
26 instance Monoid Size where
27   mempty  = Size 0
28   mappend = (+)