On Thu, 2009-12-31 at 04:59 -0500, John Van Enk wrote:
> Hi List,
> 
> I recently needed a ring structure (circular list with bi-directional
> access) and didn't see anything obvious on Hackage. I threw something
> together fairly quickly and would like some feedback before tossing it
> on Hackage.
> 
> I'd really appreciate if some one would:
>      1. make sure the code looks goodish (127 lines with full docs)
>      2. make sure my tests look saneish 
> If I hear nothing, I'll assume wild support and push to Hackage.
> 
> Code: http://github.com/sw17ch/data-ring/blob/master/src/Data/Ring.hs
> Tests:
> http://github.com/sw17ch/data-ring/blob/master/tests/quickcheck.hs
> Package Root: http://github.com/sw17ch/data-ring
> 
> Thanks ahead of time,
> John Van Enk

Monad, MonadPlus, Applicative, Alternative, Foldable and Traversable.

About comonad - not exactly as every comonad is copointed and the only
possible way is extract Empty = _|_

Regards
From 5f8bb8eeaabe6d2b82cd68ba99272603b82d9c67 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:00:33 +0100
Subject: [PATCH 1/6] Added Monad instance

---
 src/Data/CircularList.hs |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index 24e5bb9..17b7bba 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -230,3 +230,7 @@ instance Arbitrary a => Arbitrary (CList a) where
 instance Functor CList where
     fmap _ Empty = Empty
     fmap fn (CList l f r) = (CList (fmap fn l) (fn f) (fmap fn r))
+
+instance Monad CList where
+    return  = singleton
+    m >>= f = fromList $ concat $ toList $ fmap (toList . f) m
-- 
1.6.6

From a0841065478225c7d7beff39fc206ed6b4be35e8 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:01:33 +0100
Subject: [PATCH 2/6] Added MonadPlus instance

---
 src/Data/CircularList.hs |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index 17b7bba..cfd39b6 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -66,6 +66,7 @@ module Data.CircularList (
     isEmpty, size,
 ) where
 
+import Control.Monad
 import Test.QuickCheck.Arbitrary
 import Test.QuickCheck.Gen
 
@@ -234,3 +235,8 @@ instance Functor CList where
 instance Monad CList where
     return  = singleton
     m >>= f = fromList $ concat $ toList $ fmap (toList . f) m
+
+instance MonadPlus CList where
+    mzero = Empty
+    (CList l v r) `mplus` c = (CList l v (r++toList c))
+    Empty `mplus` c = c
\ No newline at end of file
-- 
1.6.6

From 7ad6834537d63ba511ff37b2a490d89bde8a4a02 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:02:34 +0100
Subject: [PATCH 3/6] Added Applicative instance

---
 src/Data/CircularList.hs |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index cfd39b6..09defe3 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -66,6 +66,7 @@ module Data.CircularList (
     isEmpty, size,
 ) where
 
+import Control.Applicative hiding (empty)
 import Control.Monad
 import Test.QuickCheck.Arbitrary
 import Test.QuickCheck.Gen
@@ -232,6 +233,10 @@ instance Functor CList where
     fmap _ Empty = Empty
     fmap fn (CList l f r) = (CList (fmap fn l) (fn f) (fmap fn r))
 
+instance Applicative CList where
+    pure = return
+    (<*>) = ap
+
 instance Monad CList where
     return  = singleton
     m >>= f = fromList $ concat $ toList $ fmap (toList . f) m
-- 
1.6.6

From 8a49a4658ad5fc6da96cb688e4a8761415ac2678 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:03:38 +0100
Subject: [PATCH 4/6] Added Alternative instance

---
 src/Data/CircularList.hs |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index 09defe3..ffbc735 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -67,6 +67,7 @@ module Data.CircularList (
 ) where
 
 import Control.Applicative hiding (empty)
+import qualified Control.Applicative
 import Control.Monad
 import Test.QuickCheck.Arbitrary
 import Test.QuickCheck.Gen
@@ -237,6 +238,10 @@ instance Applicative CList where
     pure = return
     (<*>) = ap
 
+instance Alternative CList where
+    empty = mzero
+    (<|>) = mplus
+
 instance Monad CList where
     return  = singleton
     m >>= f = fromList $ concat $ toList $ fmap (toList . f) m
-- 
1.6.6

From c930f1f86a676620df317ef0e3a90d52b527ca59 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:19:01 +0100
Subject: [PATCH 5/6] Added Foldable instance

---
 src/Data/CircularList.hs |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index ffbc735..d976a40 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -69,6 +69,8 @@ module Data.CircularList (
 import Control.Applicative hiding (empty)
 import qualified Control.Applicative
 import Control.Monad
+import Data.Foldable (Foldable)
+import qualified Data.Foldable
 import Test.QuickCheck.Arbitrary
 import Test.QuickCheck.Gen
 
@@ -249,4 +251,8 @@ instance Monad CList where
 instance MonadPlus CList where
     mzero = Empty
     (CList l v r) `mplus` c = (CList l v (r++toList c))
-    Empty `mplus` c = c
\ No newline at end of file
+    Empty `mplus` c = c
+
+instance Foldable CList where
+    foldr f i = foldr f i . toList
+    foldl f i = foldl f i . toList
-- 
1.6.6

From 7d92a2a4210fb1b221dcab599c567ae5c3511457 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:26:38 +0100
Subject: [PATCH 6/6] Added Traversable instance

---
 src/Data/CircularList.hs |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index d976a40..6e8065e 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -71,6 +71,8 @@ import qualified Control.Applicative
 import Control.Monad
 import Data.Foldable (Foldable)
 import qualified Data.Foldable
+import Data.Traversable hiding (mapM)
+import qualified Data.Traversable
 import Test.QuickCheck.Arbitrary
 import Test.QuickCheck.Gen
 
@@ -256,3 +258,7 @@ instance MonadPlus CList where
 instance Foldable CList where
     foldr f i = foldr f i . toList
     foldl f i = foldl f i . toList
+
+instance Traversable CList where
+    traverse f = fmap fromList . traverse f . toList
+    mapM f = liftM fromList . mapM f . toList
-- 
1.6.6

From 80f98d06861b878caf306699f1651597b4adaa60 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:35:00 +0100
Subject: [PATCH 7/9] Added Pointed instance

---
 src/Data/CircularList.hs |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index 6e8065e..690fd6e 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -68,6 +68,7 @@ module Data.CircularList (
 
 import Control.Applicative hiding (empty)
 import qualified Control.Applicative
+import Control.Comonad
 import Control.Monad
 import Data.Foldable (Foldable)
 import qualified Data.Foldable
@@ -238,6 +239,9 @@ instance Functor CList where
     fmap _ Empty = Empty
     fmap fn (CList l f r) = (CList (fmap fn l) (fn f) (fmap fn r))
 
+instance Pointed CList where
+    point = return
+
 instance Applicative CList where
     pure = return
     (<*>) = ap
@@ -262,3 +266,4 @@ instance Foldable CList where
 instance Traversable CList where
     traverse f = fmap fromList . traverse f . toList
     mapM f = liftM fromList . mapM f . toList
+
-- 
1.6.6

From dfaa055d5d6c8b1b54fd09288fdd4815179b7852 Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:36:15 +0100
Subject: [PATCH 8/9] Added Copointed instance

---
 src/Data/CircularList.hs |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index 690fd6e..74dc83c 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -259,6 +259,10 @@ instance MonadPlus CList where
     (CList l v r) `mplus` c = (CList l v (r++toList c))
     Empty `mplus` c = c
 
+instance Copointed CList where
+    extract Empty = error "Extracting from empty ring"
+    extract (CList _ v _) = v
+
 instance Foldable CList where
     foldr f i = foldr f i . toList
     foldl f i = foldl f i . toList
-- 
1.6.6

From 907304d1cd5bf14e1dfe26fc302955a4c238f93a Mon Sep 17 00:00:00 2001
From: Maciej Piechotka <uzytkown...@gmail.com>
Date: Mon, 4 Jan 2010 14:47:37 +0100
Subject: [PATCH 9/9] Added Comonad instance

---
 src/Data/CircularList.hs |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/src/Data/CircularList.hs b/src/Data/CircularList.hs
index 74dc83c..ad0ae1a 100644
--- a/src/Data/CircularList.hs
+++ b/src/Data/CircularList.hs
@@ -263,6 +263,17 @@ instance Copointed CList where
     extract Empty = error "Extracting from empty ring"
     extract (CList _ v _) = v
 
+instance Comonad CList where
+    duplicate c = CList (dupLeft c) c (dupRight c)
+      where dupRight Empty = []
+            dupRight (CList _ _ []) = []
+            dupRight (CList l v (r:rs)) = let next = CList (v:l) r rs
+                                          in next:dupRight next
+            dupLeft Empty = []
+            dupLeft (CList [] _ _) = []
+            dupLeft (CList (l:ls) v r) = let prev = CList (v:r) l ls
+                                         in prev:dupLeft prev
+
 instance Foldable CList where
     foldr f i = foldr f i . toList
     foldl f i = foldl f i . toList
-- 
1.6.6

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to