Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Type of a Group. (Robert Goss)
2. Re: Type of a Group. (Rustom Mody)
3. Re: Type of a Group. (Robert Goss)
----------------------------------------------------------------------
Message: 1
Date: Sat, 09 Feb 2013 13:05:12 +0000
From: Robert Goss <[email protected]>
Subject: [Haskell-beginners] Type of a Group.
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Dear all,
Looking to learn a little haskell I tried to program up a little group
theory but feel I am stuck on what the types should be. At first it
seemed obvious (and the algebra package does this) that the type of a
group should be given by:
class Group g where
mul :: g -> g -> g
inv :: g -> g
unit :: g
My problem is this seems to assume that the type of group you are in is
encoded by the type system.
I first ran into problems with this when I wanted to define a cyclic
group. The only way I could define the type was either to define each
cyclic group separately so have C2, C3, C4, ... or parametrise it over
the class Nat. So a cyclic group would have the type Cyclic (Succ(Succ(
... Succ(Zero)) ... )) which would consistently define all cyclic groups
but is hardly any better. For example a computation mod a large prime p
is not going to be pleasant.
I came up with a partial solution by realising that a group is defined
as a set X and some operations on it to get
class Group g x where
mul :: g -> x -> x -> x
inv :: g-> x -> x
unit :: g -> x
Making it easy to define cyclic groups and all my old groups carry over.
But now to evaluate an expression I need to hang onto the group i am in
and pass it around. As i am newish to haskell I wanted to know if I have
missed a simpler more obvious way of doing things?
All the best,
Robert Goss
------------------------------
Message: 2
Date: Sat, 9 Feb 2013 22:29:37 +0530
From: Rustom Mody <[email protected]>
Subject: Re: [Haskell-beginners] Type of a Group.
To: [email protected]
Message-ID:
<caj+teodftzewu3r+ctprar0_ciba1d3rdwsmhbxsi9fvqhe...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
On Sat, Feb 9, 2013 at 6:35 PM, Robert Goss <[email protected]> wrote:
> Dear all,
>
> Looking to learn a little haskell I tried to program up a little group
> theory but feel I am stuck on what the types should be. At first it seemed
> obvious (and the algebra package does this) that the type of a group should
> be given by:
>
> class Group g where
> mul :: g -> g -> g
> inv :: g -> g
> unit :: g
>
> My problem is this seems to assume that the type of group you are in is
> encoded by the type system.
>
> I first ran into problems with this when I wanted to define a cyclic
> group. The only way I could define the type was either to define each
> cyclic group separately so have C2, C3, C4, ... or parametrise it over the
> class Nat. So a cyclic group would have the type Cyclic (Succ(Succ( ...
> Succ(Zero)) ... )) which would consistently define all cyclic groups but is
> hardly any better. For example a computation mod a large prime p is not
> going to be pleasant.
>
> I came up with a partial solution by realising that a group is defined as
> a set X and some operations on it to get
>
> class Group g x where
> mul :: g -> x -> x -> x
> inv :: g-> x -> x
> unit :: g -> x
>
> Making it easy to define cyclic groups and all my old groups carry over.
> But now to evaluate an expression I need to hang onto the group i am in and
> pass it around. As i am newish to haskell I wanted to know if I have missed
> a simpler more obvious way of doing things?
>
> All the best,
>
> Robert Goss
>
>
Taking your first approach I could do this much:
---------------------------------------------------
class Group g where
mul :: g -> g -> g
inv :: g -> g
unit :: g
class Group g => CycGroup g where
gen :: g
class CycGroup g => FiniteCycGroup g where
baseSet :: [g]
-- I would have preferred to have order :: Int, to baseSet
-- but ghc does not like that for some reason
data G2 = A|B
instance Group G2 where
unit = A
inv A = A
inv B = B
mul A A = A
mul A B = B
mul B A = B
mul B B = A
instance CycGroup G2 where
gen = B
instance FiniteCycGroup G2 where
baseSet = [A, B]
------------------------------------------------------
--
http://www.the-magus.in
http://blog.languager.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130209/4771dcf0/attachment-0001.htm>
------------------------------
Message: 3
Date: Sat, 09 Feb 2013 21:16:57 +0000
From: Robert Goss <[email protected]>
Subject: Re: [Haskell-beginners] Type of a Group.
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
First thank you for your reply.
Yes that is roughly got with my first attempt. The issue I have with it
is that i need a new data type for each cyclic group still unless I have
missed something.
The implementation that I wrote for the second type I mentioned is the
following:
{-# LANGUAGE MultiParamTypeClasses #-}
class Group g x where
mul :: g -> x -> x -> x
inv :: g -> x -> x
unit :: g -> x
class (Group g x) => FGGroup g x where
gens :: g -> [x]
newtype Cyclic = Cyclic Int deriving (Eq)
cyclic :: Int -> Cyclic
cyclic n = Cyclic n
instance Group Cyclic Int where
unit _ = 0
mul (Cyclic n) x y = (x+y) `mod` n
inv (Cyclic n) x = n - x
instance FGGroup Cyclic Int where
gens _ = [1]
All the best,
Robert
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 56, Issue 18
*****************************************