Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error: No Instance for (Fractional Int)

1,544 views
Skip to first unread message

Nils-Hero Lindemann

unread,
Dec 29, 2010, 5:46:47 AM12/29/10
to
Hello,

What is wrong with this code?

<code>

data Term = Constant Int | Divide Term Term

evaluate :: Term -> Int
evaluate (Constant a) = a
evaluate (Divide x y) = evaluate x / evaluate y

</code>

it gives this error

<error>

No Instance for (Fractional Int)
arising from a use of `/'
at (etc..)
In the expression evaluate x / evaluate y
In the definition of `evaluate':
evaluate (Divide x y) = evaluate x / evaluate y

</error>


Greetings from Nils

--
Nils-Hero Lindemann <ni...@freiewelt.org>

Mark T. B. Carroll

unread,
Dec 28, 2010, 6:14:29 PM12/28/10
to
Nils-Hero Lindemann <ni...@freiewelt.org> writes:

> evaluate :: Term -> Int
> evaluate (Constant a) = a
> evaluate (Divide x y) = evaluate x / evaluate y
>

> No Instance for (Fractional Int)
> arising from a use of `/'

Yup. You're dividing, but the function is declared to return an Int,
which can't be fractional, but division can give fractional results.
Perhaps you wanted to use `div` instead?

Mark

Nils-Hero Lindemann

unread,
Dec 29, 2010, 9:58:33 AM12/29/10
to
The code is from the tutorial 'Monads for functional programming' (by
Philip Wadler, University of Glasgow, Publish Date unknown). i
overlooked (or did not understand) this: 'We use <symbol> to
denote integer division'.


So all this works:

a) use div instead of (/)
--- snip ---


data Term = Constant Int | Divide Term Term

evaluate :: Term -> Int
evaluate (Constant a) = a

evaluate (Divide x y) = evaluate x `div` evaluate y
--- snip ---

b) Change the Constant's type into Float
--- snip ---
data Term = Constant Float | Divide Term Term
evaluate :: Term -> Float


evaluate (Constant a) = a
evaluate (Divide x y) = evaluate x / evaluate y

--- snip ---

c) convert Int to Float
--- snip ---


data Term = Constant Int | Divide Term Term

evaluate :: Term -> Float
evaluate (Constant a) = fromIntegral a


evaluate (Divide x y) = evaluate x / evaluate y

--- snip ---


Thank you for the hint, Marc


am Tue, 28 Dec 2010 18:14:29 -0500
schrieb "Mark T. B. Carroll" <mt...@bcs.org>:

> Yup. You're dividing, but the function is declared to return an Int,
> which can't be fractional, but division can give fractional results.
> Perhaps you wanted to use `div` instead?
>
> Mark


--
Nils-Hero Lindemann <ni...@freiewelt.org>

Aatu Koskensilta

unread,
Dec 28, 2010, 7:59:33 PM12/28/10
to
Nils-Hero Lindemann <ni...@freiewelt.org> writes:

> What is wrong with this code?
>
> <code>
>
> data Term = Constant Int | Divide Term Term
>
> evaluate :: Term -> Int
> evaluate (Constant a) = a
> evaluate (Divide x y) = evaluate x / evaluate y

The problem is that / only works on values of types in the Fractional
typeclass. In general dividing an integer by an integer does not result
in an integer so we can see the above code is not well-typed, even on an
informal understanding of "well-typed".

--
Aatu Koskensilta (aatu.kos...@uta.fi)

"Wovon man nicht sprechen kann, darüber muss man schweigen"
- Ludwig Wittgenstein, Tractatus Logico-Philosophicus

Nils-Hero Lindemann

unread,
Dec 29, 2010, 10:13:01 AM12/29/10
to
am Wed, 29 Dec 2010 02:59:33 +0200 schrieb Aatu Koskensilta
<aatu.kos...@uta.fi>:

> "Wovon man nicht sprechen kann, darüber muss man schweigen"
> - Ludwig Wittgenstein, Tractatus Logico-Philosophicus

Ludwig konnte eben nicht singen :-)

Danke auch an Dich

--
Nils-Hero Lindemann <ni...@freiewelt.org>

0 new messages