
I've created a simple type declaration: data MathExpression = Float | Add MathExpression MathExpression | Subtract MathExpression MathExpression | Multiply MathExpression MathExpression | Divide MathExpression MathExpression deriving (Show) Now how do I create an instance of MathExpression which is just a Float? This doesn't work: *Main> let pi = 3.14 :: MathExpression <interactive>:1:10: No instance for (Fractional MathExpression) arising from the literal `3.14' Possible fix: add an instance declaration for (Fractional MathExpression) In the expression: 3.14 :: MathExpression In an equation for `pi': pi = 3.14 :: MathExpression

This is not a valid data declaration. You can't have a "Float" field
without any constructor name and have it still of type
"MathExpression". I suggest you do something like:
data MathExpr = MathFloat Float
....
So you may declare pi:
let mathPi = MathFloat pi -- note "pi" is defined in the prelude alread
Cheers,
Thomas
On Fri, Aug 19, 2011 at 1:40 PM, Paul Reiners
I've created a simple type declaration:
data MathExpression = Float | Add MathExpression MathExpression | Subtract MathExpression MathExpression | Multiply MathExpression MathExpression | Divide MathExpression MathExpression deriving (Show)
Now how do I create an instance of MathExpression which is just a Float?
This doesn't work:
*Main> let pi = 3.14 :: MathExpression
<interactive>:1:10: No instance for (Fractional MathExpression) arising from the literal `3.14' Possible fix: add an instance declaration for (Fractional MathExpression) In the expression: 3.14 :: MathExpression In an equation for `pi': pi = 3.14 :: MathExpression
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, Aug 19, 2011 at 1:45 PM, Thomas DuBuisson
This is not a valid data declaration. You can't have a "Float" field without any constructor name and have it still of type
And the reason why it accepts 'data MathExpr = Float', is because data constructors and types live in separate namespaces. There is already a type called Float in the Prelude, but there is no 'Float' data constructor. (Just pointing this out in case it's not obvious!) Jason

Your MathExpression data type has nothing to do with numbers of any kind. Your "Float" data constructor doesn't mean that float numbers are a part of your type; instead it means that you have a SINGLE value of type MathExpression, and this value is named "Float".
You should modify your data declaration as "data MathExpression = Float Float | …", and after that you can write something like "a = Float 4.2", which would automatically make "a" a value of type MathExpession".
Отправлено с iPad
20.08.2011, в 0:40, Paul Reiners
I've created a simple type declaration:
data MathExpression = Float | Add MathExpression MathExpression | Subtract MathExpression MathExpression | Multiply MathExpression MathExpression | Divide MathExpression MathExpression deriving (Show)
Now how do I create an instance of MathExpression which is just a Float?
This doesn't work:
*Main> let pi = 3.14 :: MathExpression
<interactive>:1:10: No instance for (Fractional MathExpression) arising from the literal `3.14' Possible fix: add an instance declaration for (Fractional MathExpression) In the expression: 3.14 :: MathExpression In an equation for `pi': pi = 3.14 :: MathExpression
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Jason Dagit
-
MigMit
-
Paul Reiners
-
Stephen Tetley
-
Thomas DuBuisson