[Haskell-cafe] ANNOUNCE: Cardinality-0.1

Andrey Sisoyev Andrejs.Sisojevs at nextmail.ru
Wed Jan 20 06:06:07 EST 2010


Hi everybody!

I developed a new package that you may find handy, when transforming
containers.
With this package we now have unified ways to make safe transforms "from a
-> Maybe (to a)" and "from -> Maybe to" between simple containers like [],
Maybe, Identity, Map.

Test set that returns all True:

((sContTransT []) :: Maybe (Maybe Int)) == Just Nothing
((sContTrans  ()) :: Maybe (Maybe Int)) == Just Nothing
((sContTransT [1]) :: (Maybe (Maybe Int))) == Just (Just 1)
((sContTransT [1, 2]) :: (Maybe (Maybe Int))) == Nothing
((sContTransT $ Just "Hello") :: (Maybe (Identity String))) == Just
(Identity "Hello")
((sContTransT ["Hello"]) :: Maybe (Identity String)) == Just (Identity
"Hello")
((sContTransT (EmptySet :: EmptySet String)) :: (Maybe [String])) == Just []
((sContTransT "Hello") :: Maybe (EmptySet Char)) == Nothing
((sContTransT ("key", "elem")) :: Maybe (Map String String)) == Just
(singleton "key" "elem")
((sContTrans [("key1", "elem1"), ("key2", "elem2")]) :: Maybe (Map String
String)) == Just (fromList [("key1", "elem1"), ("key2", "elem2")])
((sContTrans (EmptySet :: EmptySet (String, String))) :: Maybe (Map String
String)) == Just empty
((sContTrans []) :: Maybe ()) == Just ()
((sContTrans (NEL 'H' "i!")) :: Maybe String) == Just "Hi!" --
Data.NeverEmptyList
((sContTrans ()) :: Maybe String) == Just ""
((sContTrans "") :: Maybe (Identity Char)) == Nothing
((sContTrans "Hi!") :: Maybe ()) == Nothing

Package is to be found here: http://hackage.haskell.org/package/Cardinality
It dances around cardinality - a measure of of the number of elements of the
set (container); and cardinality constraint of the container. Cardinality
constraints:
** (): 0 elements
** Identity a: 1 element
** (Maybe a): 0..1 elements
** [a]: 0..Infinity elements
** Map k e: 0..Infinity elements
** (a,a,a): 3 elements

I also introduced 2 containers (which I found strange not to be in the
Prelude):
** data EmptySet a = EmptySet -- (0 elements)
** data NeverEmptyList a = NEL a [a] -- (1..Infinity elements)

The package provides all sorts of transformation instances between this
types (except for tuples, 3ples, 4ples... I didn't make all transforms fo
them - only "list -> (N)ple" and opposites)

I tried also to deal with transforms like "container a -> a", "a ->
container a" using OverlappingInstances. But failed to approve them -
probably due to the lack of experience with this extension. I found them
introducing too much unwanted complexity into the reasoning about types. It
was the last straw to me, when GHC started to proving me, that unity is not
less general then any type variable "a", and demanding tribute of
IncoherentInstances.
-----------------------------------

Any feedback is appreciated.

Best regards, Andrey.
-- 
View this message in context: http://old.nabble.com/ANNOUNCE%3A-Cardinality-0.1-tp27239964p27239964.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list