]> git.rkrishnan.org Git - yorgey.git/blobdiff - hw7/Sized.hs
hw7: starting materials
[yorgey.git] / hw7 / Sized.hs
diff --git a/hw7/Sized.hs b/hw7/Sized.hs
new file mode 100644 (file)
index 0000000..9214b76
--- /dev/null
@@ -0,0 +1,28 @@
+{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
+module Sized where
+
+import Data.Monoid
+
+newtype Size = Size Int
+  deriving (Eq, Ord, Show, Num)
+
+getSize :: Size -> Int
+getSize (Size i) = i
+
+class Sized a where
+  size :: a -> Size
+
+instance Sized Size where
+  size = id
+
+-- This instance means that things like
+--   (Foo, Size)
+--   (Foo, (Bar, Size))
+--   ...
+-- are all instances of Sized.
+instance Sized b => Sized (a,b) where
+  size = size . snd
+
+instance Monoid Size where
+  mempty  = Size 0
+  mappend = (+)