Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. What's the type of function "abs"? (Zhang Hengruo)
2. Re: What's the type of function "abs"? (Frerich Raabe)
3. Re: What's the type of function "abs"? (Daniel P. Wright)
4. Re: What's the type of function "abs"? (Zhang Hengruo)
5. Mathematical functions with multiple arguments
(Sumit Sahrawat, Maths & Computing, IIT (BHU))
6. Re: [Haskell-cafe] Mathematical functions with multiple
arguments (Sumit Sahrawat, Maths & Computing, IIT (BHU))
----------------------------------------------------------------------
Message: 1
Date: Wed, 11 Mar 2015 21:21:23 +0800
From: Zhang Hengruo <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] What's the type of function "abs"?
Message-ID:
<CAMeL8ffwki=xg1n48jmebtuujhpacw6p0fqfmiqkz3m5yks...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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-0001.html>
------------------------------
Message: 2
Date: Wed, 11 Mar 2015 14:28:05 +0100
From: Frerich Raabe <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] What's the type of function "abs"?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed
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 - [email protected]
www.froglogic.com - Multi-Platform GUI Testing
------------------------------
Message: 3
Date: Wed, 11 Mar 2015 22:30:45 +0900
From: "Daniel P. Wright" <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] What's the type of function "abs"?
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-2022-jp"
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 <[email protected]> ??????:
> 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
> [email protected]
> 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-0001.html>
------------------------------
Message: 4
Date: Wed, 11 Mar 2015 22:08:18 +0800
From: Zhang Hengruo <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] What's the type of function "abs"?
Message-ID:
<camel8fdhdkqo1_+mn4gblokq7h7ynyt2r7-uaxs8ran7hmh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]>:
> 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 <[email protected]> ??????:
>
> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> 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-0001.html>
------------------------------
Message: 5
Date: Thu, 12 Mar 2015 03:15:03 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>,
Haskell-cafe Cafe <[email protected]>
Subject: [Haskell-beginners] Mathematical functions with multiple
arguments
Message-ID:
<CAJbEW8OZg8a6824U1j7gsHj2SXzrRCnd=vztuex9zleono3...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi everybody,
I have a function of type
plot :: ([Double] -> Double) -- A function to plot
-> [(Double, Double)] -- Range for all arguments
-> IO ()
I want to enforce the fact that ranges for all arguments should be provided.
Is there a way to make the type system enforce it?
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150312/8adb8c76/attachment-0001.html>
------------------------------
Message: 6
Date: Thu, 12 Mar 2015 03:28:51 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: Adam Bergmark <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>,
Haskell-cafe Cafe <[email protected]>
Subject: Re: [Haskell-beginners] [Haskell-cafe] Mathematical functions
with multiple arguments
Message-ID:
<CAJbEW8NpJ0e9j5ujd9cixMinh9dexXWiVOxLn=5p_olz3tb...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I was wondering whether it was possible using fixed-vector, ut dependent
types seem to be a new thing I can experiment with.
Thanks for the quick reply.
On 12 March 2015 at 03:26, Adam Bergmark <[email protected]> wrote:
> If I understand you correctly you seem to want dependent types, this
> article uses the same example you need, promoting the length of a
> vector/list to the type level:
> https://www.fpcomplete.com/user/konn/prove-your-haskell-for-great-safety/dependent-types-in-haskell
>
> You'd end up with `plot :: (Vector n Double -> Double) -> Vector n
> (Double, Double) -> IO ()' where `n' is the length of the vector.
>
> HTH,
> Adam
>
>
> On Wed, Mar 11, 2015 at 10:45 PM, Sumit Sahrawat, Maths & Computing, IIT
> (BHU) <[email protected]> wrote:
>
>> Hi everybody,
>>
>> I have a function of type
>>
>> plot :: ([Double] -> Double) -- A function to plot
>> -> [(Double, Double)] -- Range for all arguments
>> -> IO ()
>>
>> I want to enforce the fact that ranges for all arguments should be
>> provided.
>> Is there a way to make the type system enforce it?
>>
>> --
>> Regards
>>
>> Sumit Sahrawat
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>
>>
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150312/324237e7/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 81, Issue 37
*****************************************