typeclass woes...how to constain a typeclass to be "closed" under an operation....

This is reposted from the beginnners….I’ve not done Haskell for a while and was struggling to get anything to work, I’ve hopefully refound my feet.
Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
I have a suggestion that does work
class Foo m where
op :: m a -> m (S a)
That is closed, but now were working on types of kind • -> •
(S can be a type family or a data type…lets say it’s a type family….probably an associated type (if I know what that means))
Is this the idiom/pattern i should follow? Or can the closure contraint be expressed directly in a typeclass any other way?
From: Haskell-Cafe [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Brandon Allbery
Sent: 06 January 2015 1:59 PM
To: Rob Leslie
Cc: Haskell Cafe
Subject: Re: [Haskell-cafe] Haskeline and asynchronous messages
On Tue, Jan 6, 2015 at 3:27 AM, Rob Leslie

I'm not sure, but can't closure constraint be expressed as:
class Closed m where
op :: m -> m
Then class Closed is closed under operation op. Seems I'm missing something.
Regards,
Alexey
2015-01-06 19:43 GMT+02:00 Nicholls, Mark
This is reposted from the beginnners….I’ve not done Haskell for a while and was struggling to get anything to work, I’ve hopefully refound my feet.
Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
I have a suggestion that does work
class Foo m where
op :: m a -> m (S a)
That is closed, but now were working on types of kind • -> •
(S can be a type family or a data type…lets say it’s a type family….probably an associated type (if I know what that means))
Is this the idiom/pattern i should follow? Or can the closure contraint be expressed directly in a typeclass any other way?
From: Haskell-Cafe [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Brandon Allbery Sent: 06 January 2015 1:59 PM To: Rob Leslie Cc: Haskell Cafe Subject: Re: [Haskell-cafe] Haskeline and asynchronous messages
On Tue, Jan 6, 2015 at 3:27 AM, Rob Leslie
wrote: Does anyone have a suggestion on using Haskeline in an environment where another thread may be writing messages to the terminal -- is it possible to somehow print incoming messages above the input line without disturbing the input line?
Terminals don't really work that way; you need to be looking at something like curses or vty.
--
brandon s allbery kf8nh sine nomine associates
allbery.b@gmail.com ballbery@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, Jan 06, 2015 at 05:43:47PM +0000, Nicholls, Mark wrote:
Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
Do you really need a typeclass (or indeed any way) of doing this? I suspect it will not work. If you consider the multiplication for the monoid of concatenation of lists (++) :: [a] -> [a] -> [a] you see that its type already implies that it is "closed". Tom

I will post the question again properly tomorrow but your example indeed is almost a good example, but is trivially closed If the operation on monoid was [a]->[b]->[c] That is also closed; ie [c] is also a monoid.... And the typeclass declaration follows the idiom ive suggested This only works for types of kind *->* If i wanted to do this over a type of kind * (ignoring the trivial a->a) Can i express this in a typeclass? Please ignore until i resubmit this question Ps i hate phones Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 18:10, Tom Ellis
wrote: On Tue, Jan 06, 2015 at 05:43:47PM +0000, Nicholls, Mark wrote: Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
Do you really need a typeclass (or indeed any way) of doing this? I suspect it will not work. If you consider the multiplication for the monoid of concatenation of lists
(++) :: [a] -> [a] -> [a]
you see that its type already implies that it is "closed".
Tom _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.

Bind on monad is a better example, its specified to be closed by the typeclass, but agajn operares on types of kind *->* if i have a function that orbits through multiple types (via type family) of kind * then i have to aggregate them under some wrapper type of kind *->* to define the set closed operation on the wrapper Hmmmmm As you can see i dont do much haskell, and im trying to get my head around how to map simple mathematical concepts into it Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 18:53, Nicholls, Mark
wrote: I will post the question again properly tomorrow but your example indeed is almost a good example, but is trivially closed
If the operation on monoid was
[a]->[b]->[c]
That is also closed; ie [c] is also a monoid.... And the typeclass declaration follows the idiom ive suggested
This only works for types of kind *->*
If i wanted to do this over a type of kind * (ignoring the trivial a->a)
Can i express this in a typeclass?
Please ignore until i resubmit this question
Ps i hate phones
Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 18:10, Tom Ellis
wrote: On Tue, Jan 06, 2015 at 05:43:47PM +0000, Nicholls, Mark wrote: Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
Do you really need a typeclass (or indeed any way) of doing this? I suspect it will not work. If you consider the multiplication for the monoid of concatenation of lists
(++) :: [a] -> [a] -> [a]
you see that its type already implies that it is "closed".
Tom _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.

Hi,
You can constrain the result type to be in the same class by writing
something like:
{-# LANGUAGE UndecidableInstances, FlexibleInstances #-}
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
class Foo_ a -- just to prevent a cycle in superclass constraints
instance Foo a => Foo_ a
class Foo_ (S a) => Foo a where
type S a
op :: a -> (S a)
-- and an example where you get a compile error if "op x" has an instance,
-- but "op (op x)" does not have an instance.
instance Foo Int where
type S Int = Char
op = toEnum
instance Foo Char where
type S Char = (Char,Char)
op x = (x,x)
instance Foo (Char,Char) where
type S (Char,Char) = Int
op (x,y) = fromEnum x
On Tue, Jan 6, 2015 at 1:53 PM, Nicholls, Mark
I will post the question again properly tomorrow but your example indeed is almost a good example, but is trivially closed
If the operation on monoid was
[a]->[b]->[c]
That is also closed; ie [c] is also a monoid.... And the typeclass declaration follows the idiom ive suggested
This only works for types of kind *->*
If i wanted to do this over a type of kind * (ignoring the trivial a->a)
Can i express this in a typeclass?
Please ignore until i resubmit this question
Ps i hate phones
Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 18:10, Tom Ellis
wrote: On Tue, Jan 06, 2015 at 05:43:47PM +0000, Nicholls, Mark wrote: Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
Do you really need a typeclass (or indeed any way) of doing this? I suspect it will not work. If you consider the multiplication for the monoid of concatenation of lists
(++) :: [a] -> [a] -> [a]
you see that its type already implies that it is "closed".
Tom _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Oooo I'll have a go with this tomorrow Thanks Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 19:06, adam vogt
wrote: Hi,
You can constrain the result type to be in the same class by writing something like:
{-# LANGUAGE UndecidableInstances, FlexibleInstances #-} {-# LANGUAGE TypeFamilies, FlexibleContexts #-}
class Foo_ a -- just to prevent a cycle in superclass constraints instance Foo a => Foo_ a
class Foo_ (S a) => Foo a where type S a op :: a -> (S a)
-- and an example where you get a compile error if "op x" has an instance, -- but "op (op x)" does not have an instance. instance Foo Int where type S Int = Char op = toEnum
instance Foo Char where type S Char = (Char,Char) op x = (x,x)
instance Foo (Char,Char) where type S (Char,Char) = Int op (x,y) = fromEnum x
On Tue, Jan 6, 2015 at 1:53 PM, Nicholls, Mark
wrote: I will post the question again properly tomorrow but your example indeed is almost a good example, but is trivially closed If the operation on monoid was
[a]->[b]->[c]
That is also closed; ie [c] is also a monoid.... And the typeclass declaration follows the idiom ive suggested
This only works for types of kind *->*
If i wanted to do this over a type of kind * (ignoring the trivial a->a)
Can i express this in a typeclass?
Please ignore until i resubmit this question
Ps i hate phones
Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 18:10, Tom Ellis
wrote: On Tue, Jan 06, 2015 at 05:43:47PM +0000, Nicholls, Mark wrote: Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
Do you really need a typeclass (or indeed any way) of doing this? I suspect it will not work. If you consider the multiplication for the monoid of concatenation of lists
(++) :: [a] -> [a] -> [a]
you see that its type already implies that it is "closed".
Tom _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.

In fact i had tried somethjng very much like this but without the Foo_ and haskell wasnt happy with the cyclic definition So maybe this is the answer! I'll let you know Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 19:14, Nicholls, Mark
wrote: Oooo
I'll have a go with this tomorrow
Thanks
Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 19:06, adam vogt
wrote: Hi,
You can constrain the result type to be in the same class by writing something like:
{-# LANGUAGE UndecidableInstances, FlexibleInstances #-} {-# LANGUAGE TypeFamilies, FlexibleContexts #-}
class Foo_ a -- just to prevent a cycle in superclass constraints instance Foo a => Foo_ a
class Foo_ (S a) => Foo a where type S a op :: a -> (S a)
-- and an example where you get a compile error if "op x" has an instance, -- but "op (op x)" does not have an instance. instance Foo Int where type S Int = Char op = toEnum
instance Foo Char where type S Char = (Char,Char) op x = (x,x)
instance Foo (Char,Char) where type S (Char,Char) = Int op (x,y) = fromEnum x
On Tue, Jan 6, 2015 at 1:53 PM, Nicholls, Mark
wrote: I will post the question again properly tomorrow but your example indeed is almost a good example, but is trivially closed If the operation on monoid was
[a]->[b]->[c]
That is also closed; ie [c] is also a monoid.... And the typeclass declaration follows the idiom ive suggested
This only works for types of kind *->*
If i wanted to do this over a type of kind * (ignoring the trivial a->a)
Can i express this in a typeclass?
Please ignore until i resubmit this question
Ps i hate phones
Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage.
On 6 Jan 2015, at 18:10, Tom Ellis
wrote: On Tue, Jan 06, 2015 at 05:43:47PM +0000, Nicholls, Mark wrote: Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass
Do you really need a typeclass (or indeed any way) of doing this? I suspect it will not work. If you consider the multiplication for the monoid of concatenation of lists
(++) :: [a] -> [a] -> [a]
you see that its type already implies that it is "closed".
Tom _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe CONFIDENTIALITY NOTICE
This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
participants (4)
-
adam vogt
-
Alexey Shmalko
-
Nicholls, Mark
-
Tom Ellis