Re: [Haskell-cafe] difference between class context and deriving
On Tue, Aug 2, 2011 at 5:57 AM, Patrick Browne wrote: > data Eq a => Set1 a = NilSet1 | ConsSet1 a (Set1 a) > data Set2 a = NilSet2 | ConsSet2 a (Set2 a) deriving Eq > The former declaration is a language wart, IMO. All it does is attach a restriction to the constructors of Set1; try > :t NilSet1 NilSet1 :: Eq a => Set1 a > :t NilSet2 NilSet2 :: Set2 a But it doesn't give you that restriction back when you use it: > let f (ConsSet1 v _) = v == v > :t f f :: Eq a => Set1 a -> Bool You'd think that since you had to provide the evidence (Eq a) when you constructed ConsSet1, that it'd be available, but it's not. -- Seems to have same type > :t ConsSet2 1 NilSet2 > ConsSet2 1 NilSet2 :: forall t. (Num t) => Set2 t > :t ConsSet1 1 NilSet1 > ConsSet1 1 NilSet1 :: forall t. (Num t) => Set1 t > Remember that Eq is a superclass of Num. > let f1 x = ConsSet1 x NilSet1 > let f2 x = ConsSet2 x NilSet2 > :t f1 f1 :: Eq a => a -> Set1 a f2 :: a -> Set2 a 'deriving Eq' on the definition of Set2 makes the instance 'instance Eq a => Eq (Set2 a)'. So you can construct Set2 at any type, and when the underlying type 'a' has equality, then 'Set2 a' does as well. -- ryan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] difference between class context and deriving
On Tue, 2 Aug 2011, Patrick Browne wrote: What is the difference between using a class context and deriving in data type declaration? Are there certain situations in which one or the other is preferred? data Eq a => Set1 a = NilSet1 | ConsSet1 a (Set1 a) Note that these contexts might get removed from the language: http://hackage.haskell.org/trac/haskell-prime/wiki/NoDatatypeContexts ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] difference between class context and deriving
Hi Patrick, On 2 August 2011 13:57, Patrick Browne wrote: > What is the difference between using a class context and deriving in > data type declaration? A class context simply says something about the types involved in the construction. In your example, > data Eq a => Set1 a = NilSet1 | ConsSet1 a (Set1 a) the type `a` must have an instance of `Eq`. This does not imply that `Set1` itself has an instance of `Eq`. On the other hand, the `deriving` keyword tells the compiler that you'd like it to try and derive a default instance for a class. In your example, this results in an instance of `Eq Set2`. Hopefully that should explain why you had: > (NilSet1) == (NilSet1) -- no instance, error > (NilSet2) == (NilSet2) -- True All the best, Nick ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] difference between class context and deriving
What is the difference between using a class context and deriving in data type declaration? Are there certain situations in which one or the other is preferred? data Eq a => Set1 a = NilSet1 | ConsSet1 a (Set1 a) data Set2 a = NilSet2 | ConsSet2 a (Set2 a) deriving Eq (NilSet1) == (NilSet1) -- no instance, error (NilSet2) == (NilSet2) -- True -- seems OK type Type1 = Set1 Integer type Type2 = Set2 Integer -- seems OK data Data1 = Set1 | Set2 -- Seems to have same type :t ConsSet2 1 NilSet2 ConsSet2 1 NilSet2 :: forall t. (Num t) => Set2 t :t ConsSet1 1 NilSet1 ConsSet1 1 NilSet1 :: forall t. (Num t) => Set1 t -- Pat This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe