What's the type of function "abs"?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

What's the type of function "abs"?

Zhang Hengruo
I'm a newbie, knowing a little about Haskell, and hope my question isn't
very silly...
=====================================================================
My absolute value function looks like this:

abs2 :: Num a => a -> a
abs2 n = if n >= 0 then n else 0 - n

GHCi tells me that I should add Ord type class to its definition. Well,
it's true. It has used relational operators and Num isn't a subclass of Ord.
However, when I input ":t abs" in GHCi, the interpreter shows "abs :: Num a
=> a -> a". I read GHC/Num.lhs and find that abs is defined as "abs :: a ->
a" and has no concrete content. So I think the abs function is written in C
as a module to implement directly and the type of abs just follows its
class GHC.Num. Is it right? Or there are any other reasons?

Thanks,

Hengruo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150311/26f7586d/attachment.html>

Reply | Threaded
Open this post in threaded view
|

What's the type of function "abs"?

Frerich Raabe
Hi,

On 2015-03-11 14:21, Zhang Hengruo wrote:

> My absolute value function looks like this:
>
> abs2 :: Num a => a -> a
> abs2 n = if n >= 0 then n else 0 - n
>
> GHCi tells me that I should add Ord type class to its definition. Well, it's
> true. It has used relational operators and Num isn't a subclass
> of Ord.
> However, when I input ":t abs" in GHCi, the interpreter shows "abs :: Num a
> => a -> a". I read GHC/Num.lhs and find that abs is defined as
> "abs :: a -> a" and has no concrete content. So I think the abs function is
> written in C as a module to implement directly and the type of abs
> just follows its class GHC.Num. Is it right? Or there are any other reasons?

'abs' is a function defined on the 'Num' class, i.e. any instance of 'Num'
(such as 'Num Int') may define 'abs' as it pleases. See

   http://hackage.haskell.org/package/base-4.7.0.2/docs/src/GHC-Num.html#abs

for a few instantiations of the 'Num' class. In particular, concrete
instances of the class know the actual type of 'a'. For instance, the
definition of 'Num Int' could actually use '>=' because there's indeed an Ord
instance for Int. In other cases, e.g. 'Num Word', 'abs' is just the identity
function, i.e. 'abs x = x' since there are no non-positive Word values.

--
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing

Reply | Threaded
Open this post in threaded view
|

What's the type of function "abs"?

Daniel P. Wright
In reply to this post by Zhang Hengruo
One nice thing about haskell is that the source to most things is available on hackage. If you look at abs, here:

https://hackage.haskell.org/package/base-4.7.0.2/docs/src/GHC-Num.html#abs

You will see two things:

1) abs is actually part of the Num typeclass, so can be defined differently for the different numerical types
2) the definition for Int is similar to yours:
    abs n = if n `geInt` 0 then n else negate n

Notice `geInt` is just an int-specific (>=) operator. Because we're defining an instance we know the concrete type we're dealing with (the type is Int -> Int by this point, rather than a -> a), so we don't need to make use of Ord in this case.



11 Mar 2015 22:21?Zhang Hengruo <hengruo.z at gmail.com> ??????:

> I'm a newbie, knowing a little about Haskell, and hope my question isn't very silly...
> =====================================================================
> My absolute value function looks like this:
>
> abs2 :: Num a => a -> a
> abs2 n = if n >= 0 then n else 0 - n
>
> GHCi tells me that I should add Ord type class to its definition. Well, it's true. It has used relational operators and Num isn't a subclass of Ord.
> However, when I input ":t abs" in GHCi, the interpreter shows "abs :: Num a => a -> a". I read GHC/Num.lhs and find that abs is defined as "abs :: a -> a" and has no concrete content. So I think the abs function is written in C as a module to implement directly and the type of abs just follows its class GHC.Num. Is it right? Or there are any other reasons?
>
> Thanks,
>
> Hengruo
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150311/d90a483f/attachment.html>

Reply | Threaded
Open this post in threaded view
|

What's the type of function "abs"?

Zhang Hengruo
Oh! Thank you very much! I took notice of instances of Num just now... You
two are so kind!

2015-03-11 21:30 GMT+08:00 Daniel P. Wright <dani at dpwright.com>:

> One nice thing about haskell is that the source to most things is
> available on hackage. If you look at abs, here:
>
> https://hackage.haskell.org/package/base-4.7.0.2/docs/src/GHC-Num.html#abs
>
> You will see two things:
>
> 1) abs is actually part of the Num typeclass, so can be defined
> differently for the different numerical types
> 2) the definition for Int is similar to yours:
>
>     abs n  = if n `geInt` 0 then n else negate n
>
>
> Notice `geInt` is just an int-specific (>=) operator. Because we're
> defining an instance we know the concrete type we're dealing with (the type
> is Int -> Int by this point, rather than a -> a), so we don't need to make
> use of Ord in this case.
>
>
>
> 11 Mar 2015 22:21?Zhang Hengruo <hengruo.z at gmail.com> ??????:
>
> I'm a newbie, knowing a little about Haskell, and hope my question isn't
> very silly...
> =====================================================================
> My absolute value function looks like this:
>
> abs2 :: Num a => a -> a
> abs2 n = if n >= 0 then n else 0 - n
>
> GHCi tells me that I should add Ord type class to its definition. Well,
> it's true. It has used relational operators and Num isn't a subclass of Ord.
> However, when I input ":t abs" in GHCi, the interpreter shows "abs :: Num
> a => a -> a". I read GHC/Num.lhs and find that abs is defined as "abs :: a
> -> a" and has no concrete content. So I think the abs function is written
> in C as a module to implement directly and the type of abs just follows its
> class GHC.Num. Is it right? Or there are any other reasons?
>
> Thanks,
>
> Hengruo
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150311/f54d341e/attachment.html>