user type declarations in Haskell

I am trying to define the following types data MyStringType a = String deriving (Eq, Ord, Show) data QADouble a = Double deriving (Eq, Ord, Show) data HType a = QADouble a| DDTraceType a deriving (Eq, Ord, Show) So HType can represent strings or doubles. later I want to do something like the following: let a1 =QADouble 1 let a2 =QADouble 2 let a3 = a1 + a2 First, it is not working because Haskell complains about a3. it does not know how to calculate it. Is it a way to give him a hint? QADouble is Double...am I doing something absolutely wrong and silly? many thanks, vladimir _________________________________________________________________ Are you using the latest version of MSN Messenger? Download MSN Messenger 7.5 today! http://join.msn.com/messenger/overview

2006/6/22, Vladimir Portnykh
I am trying to define the following types
data MyStringType a = String deriving (Eq, Ord, Show) data QADouble a = Double deriving (Eq, Ord, Show) data HType a = QADouble a| DDTraceType a deriving (Eq, Ord, Show)
So HType can represent strings or doubles. later I want to do something like the following: let a1 =QADouble 1 let a2 =QADouble 2 let a3 = a1 + a2
a1 and a2 have type HType (not QADouble, nor Double). GHC doesn't know how to use (+) on HType because (+) is meaningful only for Num instances : try to type ':t (+)' in ghci : it will give you the type of (+). try to type also ':t a1'. hope it helps, vo minh thu

Vladimir Portnykh wrote:
I am trying to define the following types
data MyStringType a = String deriving (Eq, Ord, Show) data QADouble a = Double deriving (Eq, Ord, Show)
These are not what you think they are. MyStringType has a phantom type parameter and only one value, which is the constant String (but not of type String). What you actually meant can only be guessed, and I'm not even trying.
So HType can represent strings or doubles. later I want to do something like the following: let a1 =QADouble 1 let a2 =QADouble 2 let a3 = a1 + a2
data HType = QADouble Double | QAString Double
First, it is not working because Haskell complains about a3. it does not know how to calculate it.
What did you expect? What's the sum of a Double and a String if not an error? You have to define (+), which is already defined. Use some other name and give a definition: QADouble x `plus` QADouble y = ... QADouble x `plus` QAString y = ... QAString x `plus` QAString y = ... QAString x `plus` QADouble y = ... Udo. -- Lieber vom Fels zertrümmert als bei einer Frau verkümmert. -- aus einem Gipfelbuch
participants (3)
-
minh thu
-
Udo Stenzel
-
Vladimir Portnykh