Re: [Haskell-cafe] Declaring a tuple of instances of Enums as an instance of the Enum class

2010-05-25 Thread Henning Thielemann
Daniel Fischer schrieb:
 On Sunday 23 May 2010 15:33:58, Ivan Lazar Miljenovic wrote:
 R J rj248...@hotmail.com writes:
 Say I've got a type Month declared as an instance of the Enum
 class, and a type MonthPair declared as a pair of months:
 data Month = January | February | March | April | May | June | July |
 August | September | October | November | December deriving (Eq, Enum,
 Ord, Show)
 type MonthPair = (Month, Month) deriving (Enum)
 The deriving on MonthPair gives me the error parse error on input
 deriving'.
 You can't derive instances for type aliases (as its mainly there for
 documentation reasons, etc.).  However, pairs don't have Enum instances
 by default so you still can't use its instance.

 If you define data MonthPair = MonthPair Month Month then you should
 be able to derive Enum.
 
 No, per the report (http://haskell.org/onlinereport/derived.html)
 
 Derived instance declarations for the class Enum are only possible for 
 enumerations (data types with only nullary constructors).

You might define an instance more generally:

data EnumPair a b = EnumPair a b

instance (Enum a, Bounded b, Enum b) = Enum (EnumPair a b) where
   ...

Then define something like
  fromEnum (EnumPair a b) = (maxBound - minBound)*a + b

(needs still some asTypeOf's and fromEnum's)

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


Re: [Haskell-cafe] Declaring a tuple of instances of Enums as an instance of the Enum class

2010-05-25 Thread Ryan Ingram
Also, this way lies a bit of madness, since fromEnum/toEnum work on
Int, not Integer.

This means
   EnumPair (EnumPair Month Month) (EnumPair Month Month) overflows

  -- ryan

On Tue, May 25, 2010 at 11:30 AM, Henning Thielemann
schlepp...@henning-thielemann.de wrote:
 Daniel Fischer schrieb:
 On Sunday 23 May 2010 15:33:58, Ivan Lazar Miljenovic wrote:
 R J rj248...@hotmail.com writes:
 Say I've got a type Month declared as an instance of the Enum
 class, and a type MonthPair declared as a pair of months:
 data Month = January | February | March | April | May | June | July |
 August | September | October | November | December deriving (Eq, Enum,
 Ord, Show)
 type MonthPair = (Month, Month) deriving (Enum)
 The deriving on MonthPair gives me the error parse error on input
 deriving'.
 You can't derive instances for type aliases (as its mainly there for
 documentation reasons, etc.).  However, pairs don't have Enum instances
 by default so you still can't use its instance.

 If you define data MonthPair = MonthPair Month Month then you should
 be able to derive Enum.

 No, per the report (http://haskell.org/onlinereport/derived.html)

 Derived instance declarations for the class Enum are only possible for
 enumerations (data types with only nullary constructors).

 You might define an instance more generally:

 data EnumPair a b = EnumPair a b

 instance (Enum a, Bounded b, Enum b) = Enum (EnumPair a b) where
   ...

 Then define something like
  fromEnum (EnumPair a b) = (maxBound - minBound)*a + b

 (needs still some asTypeOf's and fromEnum's)

 ___
 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


[Haskell-cafe] Declaring a tuple of instances of Enums as an instance of the Enum class

2010-05-23 Thread R J

Say I've got a type Month declared as an instance of the Enum class, and a 
type MonthPair declared as a pair of months:
data Month =  January   |  February 
  |  March   |  April   
|  May   |  June   
|  July   |  August   |  
September   |  October   |  
November   |  December   
deriving (Eq, Enum, Ord, Show)
type MonthPair =  (Month, Month)   deriving 
(Enum)
The deriving on MonthPair gives me the error parse error on input 
'deriving'.
Why is this error generated?  Is there a syntax error, or is there a conceptual 
problem with enumerating a Cartesian product, such as Month x Month?  The 
cardinality of the Cartesian product is finite (including the bottom values, 
cardinality = 1 + (12 + 1)*(12 + 1) = 170), and so the product is amenable at 
least to some arbitrary enumeration (such as Cantor's diagonal method).
Thanks. 

  
_
The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with 
Hotmail. 
http://www.windowslive.com/campaign/thenewbusy?tile=multicalendarocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_5___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Declaring a tuple of instances of Enums as an instance of the Enum class

2010-05-23 Thread Ivan Lazar Miljenovic
R J rj248...@hotmail.com writes:

 Say I've got a type Month declared as an instance of the Enum
 class, and a type MonthPair declared as a pair of months:
 data Month = January | February | March | April | May | June | July |
 August | September | October | November | December deriving (Eq, Enum,
 Ord, Show)
 type MonthPair = (Month, Month) deriving (Enum)
 The deriving on MonthPair gives me the error parse error on input
 deriving'.

You can't derive instances for type aliases (as its mainly there for
documentation reasons, etc.).  However, pairs don't have Enum instances
by default so you still can't use its instance.

If you define data MonthPair = MonthPair Month Month then you should
be able to derive Enum.

-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Declaring a tuple of instances of Enums as an instance of the Enum class

2010-05-23 Thread Daniel Fischer
On Sunday 23 May 2010 15:33:58, Ivan Lazar Miljenovic wrote:
 R J rj248...@hotmail.com writes:
  Say I've got a type Month declared as an instance of the Enum
  class, and a type MonthPair declared as a pair of months:
  data Month = January | February | March | April | May | June | July |
  August | September | October | November | December deriving (Eq, Enum,
  Ord, Show)
  type MonthPair = (Month, Month) deriving (Enum)
  The deriving on MonthPair gives me the error parse error on input
  deriving'.

 You can't derive instances for type aliases (as its mainly there for
 documentation reasons, etc.).  However, pairs don't have Enum instances
 by default so you still can't use its instance.

 If you define data MonthPair = MonthPair Month Month then you should
 be able to derive Enum.

No, per the report (http://haskell.org/onlinereport/derived.html)

Derived instance declarations for the class Enum are only possible for 
enumerations (data types with only nullary constructors).

You can derive Enum instances for newtype wrappers around Enums with GHC 
(possibly others), but for types such as MonthPair you have to give the 
instances yourself (maybe you can let them be generated by tools like 
Derive).
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Declaring a tuple of instances of Enums as an instance of the Enum class

2010-05-23 Thread Ivan Lazar Miljenovic
Daniel Fischer daniel.is.fisc...@web.de writes:

 On Sunday 23 May 2010 15:33:58, Ivan Lazar Miljenovic wrote:
 R J rj248...@hotmail.com writes:
  Say I've got a type Month declared as an instance of the Enum
  class, and a type MonthPair declared as a pair of months:
  data Month = January | February | March | April | May | June | July |
  August | September | October | November | December deriving (Eq, Enum,
  Ord, Show)
  type MonthPair = (Month, Month) deriving (Enum)
  The deriving on MonthPair gives me the error parse error on input
  deriving'.

 You can't derive instances for type aliases (as its mainly there for
 documentation reasons, etc.).  However, pairs don't have Enum instances
 by default so you still can't use its instance.

 If you define data MonthPair = MonthPair Month Month then you should
 be able to derive Enum.

 No, per the report (http://haskell.org/onlinereport/derived.html)

 Derived instance declarations for the class Enum are only possible for 
 enumerations (data types with only nullary constructors).

Whoops, you're right.

-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe