summaryrefslogtreecommitdiff
path: root/lec07/Sized.hs
diff options
context:
space:
mode:
authorEdoardo La Greca2025-09-09 20:38:00 +0200
committerEdoardo La Greca2025-09-09 20:38:00 +0200
commitf46dafc461e4ee3fa1637051284f89b443f00a51 (patch)
treebfc6b3aebbe12141d34b9727e11766bffdc2937e /lec07/Sized.hs
parent476ae2fe83ea46e3845b0c32afe7df57c8fb109b (diff)
add files for lecture 7's homework
Diffstat (limited to 'lec07/Sized.hs')
-rw-r--r--lec07/Sized.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/lec07/Sized.hs b/lec07/Sized.hs
new file mode 100644
index 0000000..9214b76
--- /dev/null
+++ b/lec07/Sized.hs
@@ -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 = (+)