
Hello, I've looked through the two tutorials and the Report, but couldn't find help on this topic. My question is whether you can place constraints on new data types. For instance, I want to make a new type that is a 4 element tuple where each element is greater than or equal to the previous entry. Is this possible? i.e. data Category = Membership a a a a I'd like to be able to prevent invalid Category data from being created. Any information would be appreciated. -- Rich AIM : rnezzy ICQ : 174908475

Rich Neswold
For instance, I want to make a new type that is a 4 element tuple where each element is greater than or equal to the previous entry.
data Category = Membership a a a a
You can make a 'smart' constructor function, and hide the real data constructor so that it cannot be used: module Category (Category(),membership) where data Category = Membership a a a a membership :: Ord a => a -> a -> a -> a -> Category a membership a b c d | d>=c && c>=b && b>=a = Membership a b c d | otherwise = error "bad values" However, you probably don't really want your program to die with "error" when you get badly-constrained data, so it might be better to look into something like the Maybe type for your smart constructor: membership :: Ord a => a -> a -> a -> a -> Maybe (Category a) so you can catch the failing cases and do something sensible with them. Regards, Malcolm

"Rich" == Rich Neswold
writes: Hello, I've looked through the two tutorials and the Report, but couldn't find help on this topic. My question is whether you can place constraints on new data types. For instance, I want to make a new type that is a 4 element tuple where each element is greater than or equal to the previous entry. Is this possible?
i.e.
data Category = Membership a a a a
I'd like to be able to prevent invalid Category data from being created. Any information would be appreciated.
Don't export constructors and define functions which will imply your constraints. module Foo (..., Category,... makeCategory1, makeCategory2, ...) -- WBR, Max Vasin.
participants (3)
-
Malcolm Wallace
-
Max Vasin
-
Rich Neswold