[Haskell-beginners] Categories in Haskell

Robert Goss goss.robert at gmail.com
Wed May 29 19:01:13 CEST 2013


I am interested in trying to use category to perform some mathematical
computations. It seems natural to want to talk about categories of
mathematical objects e.g. Groups,Rings,Algebras ... etc. But I have found
some difficulty doing this with Haskell's standard Category libraries.

class Category m where
  id :: m a a
  comp :: m a b -> m b c -> m a c

The main issue is that the objects of the category (represented by the id
morphism) are in bijection to some set of haskell types.

The problem (I think) this leads too is that

1. All the objects that may appear in a program must be known to the
compiler. Which leads to problems (for me at least) as the application I
want to study I don't know all the (Groups,Rings,... etc) that I will need.
2. This requires representing (for example) all groups as types. In general
I have found while possible it leads to pretty horrendous types.

Now this all makes sense Haskell's categories are for reasoning about
programs while I want to use it more for pure maths. Has anyone else had a
similar problem with categories in haskell? Or am I missing a way of
implementing such structures within the standard Category framework for
haskell.

For a (toy) example take trying to model the category of CyclicGroups

--Cyclic n is the object representing the cyclic group Z_n
newtype Cyclic = Cyclic Int
--CyclicMor n m p is represents a group morphism from Z_n to Z_m taking 1
-> p
newtype CyclicMor = CyclicMor Int Int Int

This now gives the definition of

id :: Cyclic -> CyclicMor
id (Cyclic n) = CyclicMor n n 1

comp :: CyclicMor -> CyclicMor -> Maybe CyclicMor
comp (CyclicMor n m p) (CyclicMor n' m' p')
  | m==n' = Just (CyclicMor n m' (p+p'))
  | otherwise = Nothing

The main loss in this approach is compiler can no longer determine if
morphisms are compatible. In my project I use a slightly more generic
variant :

class OrdinaryCategory m where
  id :: a -> m a a
  comp :: m a b -> m b c -> Maybe (m a c)

I haven't seen a similar construction in haskell libraries does anyone know
if this is because
1. It is already there and I just haven't come across it.
2. It solves a problems that no-one else has had.
3. My idea is fundamentally broken / useless in some key way.

Thanks for any comments anyone has.

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130529/d268eeca/attachment.htm>


More information about the Beginners mailing list