Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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. Re: netwire accum delayed by one (Nathan H?sken)
2. Re: Excellent illustration of using the Haskell types --
revising Richard Bird's floor function so that it works properly
(Bob Hutchison)
3. Re: netwire accum delayed by one (Ertugrul S?ylemez)
4. Re: Excellent illustration of using the Haskell types --
revising Richard Bird's floor function so that it works properly
(Costello, Roger L.)
5. Re: Excellent illustration of using the Haskell types --
revising Richard Bird's floor function so that it works properly
(Patrick Mylund Nielsen)
6. netwire, <<loop>> with accum1 (Nathan H?sken)
7. Re: Excellent illustration of using the Haskell types --
revising Richard Bird's floor function so that it works properly
(Kim-Ee Yeoh)
----------------------------------------------------------------------
Message: 1
Date: Sun, 04 Nov 2012 14:14:13 +0100
From: Nathan H?sken <[email protected]>
Subject: Re: [Haskell-beginners] netwire accum delayed by one
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thanks!
I think there is still a bug in there. accumT1 "turns into" accumT after
the first invocation.
Regards,
Nathan
On 11/04/2012 06:22 AM, Ertugrul S?ylemez wrote:
> Nathan H?sken <[email protected]> wrote:
>
>>> Accum is documented to behave like a left scan, which also starts
>>> with the initial value. The reason is that for most applications
>>> you want the data dependency on the previous instant instead of on
>>> the current. Many useful FRP constructs can be (and are) expressed
>>> in terms of accum and accumT.
>>
>> While I understand the intention, I often have several accums (or
>> integral which is expressed in terms of accum) chained. For example:
>>
>> speed :: WireP CollisionData Vector
>> speed = accum collide initSpeed
>> where
>> collide = ...
>>
>> position :: WireP CollisionData Vector
>> position = integral_ initPos . speed
>>
>> which delays the output by 2.
>> Maybe there should also be a "non delaying" version of accum and
>> integral? I must admit, I would prefer it to inset delay manually, but
>> that probably just a matter of taste.
>
> That's how earlier versions of Netwire worked, where you always needed
> explicit delays, but that turned out to be very noisy in code. However,
> I see why you may want to have the non-delaying versions, so I have
> added and released them as version 4.0.2. Along with the change I also
> worked around an apparent Haddock bug, which prevented the documentation
> from being created for GHC 7.6, so now you get Haddocks, too.
>
>
> Greets,
> Ertugrul
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Sun, 4 Nov 2012 08:20:27 -0500
From: Bob Hutchison <[email protected]>
Subject: Re: [Haskell-beginners] Excellent illustration of using the
Haskell types -- revising Richard Bird's floor function so that it
works properly
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
Hi Roger,
I'm curious now? You've been posting these kinds of articles for years now,
mostly from what I've seen on XML and REST, but a few now on Haskell. It's an
interesting approach, and you've managed (at least) two things: 1) provided a
bunch of useful synopses of narrow(ish) topics within a larger subject; and, 2)
provoked really interesting responses from experts. And I've got to think the
body of work constitutes a very significant chunk of knowledge.
Why are you doing this?
Where are you publishing it? (just http://www.xfront.com/ ?)
What subjects are you doing this for?
What's the best way to follow along?
Cheers,
Bob
------------------------------
Message: 3
Date: Sun, 4 Nov 2012 14:34:36 +0100
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] netwire accum delayed by one
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Nathan H?sken <[email protected]> wrote:
> I think there is still a bug in there. accumT1 "turns into" accumT
> after the first invocation.
Damn copy/paste coding. Apparently I paid more attention to the
documentation than to the actual code. =)
Fixed.
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121104/bd30a35e/attachment-0001.pgp>
------------------------------
Message: 4
Date: Sun, 4 Nov 2012 13:44:51 +0000
From: "Costello, Roger L." <[email protected]>
Subject: Re: [Haskell-beginners] Excellent illustration of using the
Haskell types -- revising Richard Bird's floor function so that it
works properly
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
Hello Bob,
Thank you for the very kind words.
> Why are you doing this?
I learn something and want to share what I've learned. I am hopeful that it
benefits others.
> Where are you publishing it? (just http://www.xfront.com/ ?)
Yes, mostly I publish there.
> What subjects are you doing this for?
The subjects of most interest to me these days are: Haskell, XML, XSLT, and XML
Schema.
> What's the best way to follow along?
For Haskell I post what I learn to this list; sometimes I also create a summary
and put it on my web site (xfront.com). For the XML technologies I post to the
various XML lists and almost always put a summary on my web site.
You've made my day Bob!
/Roger
-----Original Message-----
From: Bob Hutchison [mailto:[email protected]]
Sent: Sunday, November 04, 2012 8:20 AM
To: Costello, Roger L.
Cc: [email protected]
Subject: Re: [Haskell-beginners] Excellent illustration of using the Haskell
types -- revising Richard Bird's floor function so that it works properly
Hi Roger,
I'm curious now... You've been posting these kinds of articles for years now,
mostly from what I've seen on XML and REST, but a few now on Haskell. It's an
interesting approach, and you've managed (at least) two things: 1) provided a
bunch of useful synopses of narrow(ish) topics within a larger subject; and, 2)
provoked really interesting responses from experts. And I've got to think the
body of work constitutes a very significant chunk of knowledge.
Why are you doing this?
Where are you publishing it? (just http://www.xfront.com/ ?)
What subjects are you doing this for?
What's the best way to follow along?
Cheers,
Bob
------------------------------
Message: 5
Date: Sun, 4 Nov 2012 15:08:29 +0100
From: Patrick Mylund Nielsen <[email protected]>
Subject: Re: [Haskell-beginners] Excellent illustration of using the
Haskell types -- revising Richard Bird's floor function so that it
works properly
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<CAEw2jfw5XcfGFnPGAtv3n+rWs=_12kbosyg6f6nfyydqgn-...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Ditto, enjoy the posts a lot!
On Sun, Nov 4, 2012 at 2:44 PM, Costello, Roger L. <[email protected]>wrote:
> Hello Bob,
>
> Thank you for the very kind words.
>
> > Why are you doing this?
>
> I learn something and want to share what I've learned. I am hopeful that
> it benefits others.
>
> > Where are you publishing it? (just http://www.xfront.com/ ?)
>
> Yes, mostly I publish there.
>
> > What subjects are you doing this for?
>
> The subjects of most interest to me these days are: Haskell, XML, XSLT,
> and XML Schema.
>
> > What's the best way to follow along?
>
> For Haskell I post what I learn to this list; sometimes I also create a
> summary and put it on my web site (xfront.com). For the XML technologies
> I post to the various XML lists and almost always put a summary on my web
> site.
>
> You've made my day Bob!
>
> /Roger
>
> -----Original Message-----
> From: Bob Hutchison [mailto:[email protected]]
> Sent: Sunday, November 04, 2012 8:20 AM
> To: Costello, Roger L.
> Cc: [email protected]
> Subject: Re: [Haskell-beginners] Excellent illustration of using the
> Haskell types -- revising Richard Bird's floor function so that it works
> properly
>
>
> Hi Roger,
>
> I'm curious now... You've been posting these kinds of articles for years
> now, mostly from what I've seen on XML and REST, but a few now on Haskell.
> It's an interesting approach, and you've managed (at least) two things: 1)
> provided a bunch of useful synopses of narrow(ish) topics within a larger
> subject; and, 2) provoked really interesting responses from experts. And
> I've got to think the body of work constitutes a very significant chunk of
> knowledge.
>
> Why are you doing this?
>
> Where are you publishing it? (just http://www.xfront.com/ ?)
>
> What subjects are you doing this for?
>
> What's the best way to follow along?
>
> Cheers,
> Bob
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121104/fea489da/attachment-0001.htm>
------------------------------
Message: 6
Date: Sun, 04 Nov 2012 15:21:48 +0100
From: Nathan H?sken <[email protected]>
Subject: [Haskell-beginners] netwire, <<loop>> with accum1
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hey,
I have another <<loop>> with netwire.
This is the minimal example I got it down to:
{-# LANGUAGE Arrows #-}
import Control.Wire
mainWire :: WireP () Double
mainWire = proc _ -> do
rec
out <- accum1 (+) 0 -< oldOut
oldOut <- delay 0 -< out
returnA -< out
main = wireLoop mainWire
wireLoop :: WireP () Double -> IO ()
wireLoop w' = do
let (mx, w) = stepWireP w' 1.0 ()
putStrLn $ show mx
wireLoop w
Output:
<<loop>>
I am aware, in this context I could just use accum. I triggered the
error where I changed integral_ and accum1.
By the way, is it apropriate, that I always write these "netwire bug
reports" over this list?
Regards,
Nathan
------------------------------
Message: 7
Date: Sun, 4 Nov 2012 22:50:05 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] Excellent illustration of using the
Haskell types -- revising Richard Bird's floor function so that it
works properly
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<capy+zdrncebebzobpdbpbvn9a3hknk8dy+jbefrscixf3mc...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Roger,
I have only the first edition at hand so I don't have the big picture of
what's going on. My guess is that based on the year of publication (1998)
of the 2nd edition, Bird was writing at a time when the Num typeclass
hierarchy was still in flux and simply wasn't what it is today.
Some comments:
> The type of 0 is ambiguous, it could be an Integer or it could be a
Double. This ambiguity can be seen by checking its type using WinGHCi:
> :type 0
> 0 :: Num a => a"
Rather than ambiguous, it's better to say that 0 is polymorphic. Or that
it's a Num-constrained expression. The definitive meaning of ambiguous is
whatever the type checker reports as such. ;)
> The datatype Double falls within the class Fractional.
Better to say: there is an instance of Double for the class Fractional. The
idea of "falling" suggests it's all forced upon the language by the Powers
That Be. It's not. There's a declaration for the instance in the prelude.
And the prelude can be suppressed.
> Fundamental rule of Haskell: you cannot compare an Integer against a
Double, you can only compare a Double against a Double.
You didn't quite ask it this way, but let me treat it as a question: Is it
possible to create a typeclass with a method compare that takes 2
independently varying types? You're correct: No, it can't be done if you
restrict yourself to Haskell98. However, multi-parameter typeclasses are
extremely well-supported and widely used both depth- (historically) and
breadth-wise (non-GHC).
> Here is the signature for floor:
> floor :: (Fractional a, Ord a, Real c) => a -> c
> Read as: Invoke function floor with a Fractional (Double) value and it
will return a Real (Integer) value.
Better to read as "floor takes a Fractional and Ord-constrained input and
produces a Real-constrained output." Why does this help Haskell learners?
Because writing the following is plenty common:
floor :: Fractional a -> Real c
or
sequence :: [Monad a] -> Monad [a]
Must. Keep. Types. And. Type classes. Distinct.
That said, awareness of the importance of speaking out code is a strong
sign of success in learning the language. In due course those
combinator-laden expressions won't seem such a dense fog of arcane symbols.
Really impressed at your making notes and publishing them. I look forward
to more of your tutorials!
-- Kim-Ee
On Sun, Nov 4, 2012 at 4:40 PM, Costello, Roger L. <[email protected]>wrote:
> Hi Folks,
>
> On page 82 of the book "Introduction to Functional Programming using
> Haskell" the author Richard Bird provides this sample implementation of the
> floor function:
>
> floor x = searchFrom 0
> where searchFrom = decrease . upper . lower
> lower = until (<=x)
> decrease
> upper = until (>x)
> increase
> decrease n = n - 1
> increase n = n + 1
>
> The problem with that implementation is that it does not return an Integer
> value; rather, it returns a decimal (Double) value. Here's an example:
>
> floor (-3.4) -- returns (-4.0)
>
> That is wrong. The specification for floor says that it "maps a value of
> type Float to a value of type Integer." Clearly it is not producing an
> Integer result.
>
> I will now explain how to revise floor so that it returns an Integer
> value. In the process we will see an excellent illustration of the Haskell
> types.
>
> The heart of the problem is with the until function.
>
> Here is how Richard Bird implements it:
>
> until :: (a -> Bool) -> (a -> a) -> a -> a
> until p f y = if p y then y else until p f (f y)
>
> It takes three arguments, p, f, and y. Look at the signature of that
> function. The type of the third argument, y, dictates the type of the other
> arguments and the type of the result--whatever type y is, the other
> arguments must be the same type and the result must be the same type.
>
> Function until is first invoked by function lower:
>
> lower = until (<=x) decrease 0
>
> Here are the three arguments provided to function until:
>
> (1) p is the partial function (<=x), where x is the input to the floor
> function. Suppose x is this value: x = (-3.4)
>
> (2) f is the function decrease
>
> (3) y is 0
>
> Now you may argue, "Hey, the third argument, y, is 0 and that's an
> Integer, so clearly the result of function until will be an Integer."
> However, that is not correct. The type of 0 is ambiguous, it could be an
> Integer or it could be a Double. This ambiguity can be seen by checking its
> type using WinGHCi:
>
> :type 0
> 0 :: Num a => a
>
> The class Num is high up in Haskell's type hierarchy and it represents any
> number (Integer, Double, etc.). Thus, we cannot determine the type of
> function until's result just by examining its third argument. The other
> arguments will determine whether the 0 is an Integer or a Double. Let's
> examine the first argument:
>
> p is the partial function (<=x), where x = (-3.4)
>
> p compares "some value" against (-3.4). Let's check the type of (-3.4)
> using WinGHCi:
>
> :type (-3.4)
> (-3.4) :: Fractional a => a
>
> The datatype Double falls within the class Fractional. So p compares "some
> value" against a Double.
>
> Fundamental rule of Haskell: you cannot compare an
> Integer against a Double, you can only compare a
> Double against a Double.
>
> Recall that p compares "some value" against (-3.4). What is that "some
> value"? If we examine the body of function until we see that it is the
> third argument, y, and we know that y = 0. Ah, now we know how Haskell
> will treat 0: since the 0 is being compared against a Double value Haskell
> will treat the 0 as a Double. Okay, now that we know the type of y we can
> plug it into the type signature for function until:
>
> until :: (a -> Bool) -> (a -> a) -> Double -> a
>
> All a's must be of the same type, so the other a's must also be Double:
>
> until :: (Double -> Bool) -> (Double -> Double) -> Double -> Double
>
> Therefore function until will return a Double value. For example:
>
> until (<=x) decrease 0 -- returns (0.0)
>
> The output of function until is assigned to function lower:
>
> lower = until (<=x) decrease
>
> So the result of function lower is a Double value.
>
> The output of lower is then input to upper and the Double datatype
> propagates through the entire chain of composed functions:
>
> decrease . upper . lower
>
> The result of function floor is therefore a Double value.
>
> Okay, so how do we get floor to return an Integer value? The key is to
> prevent p in function until from casting the type of y to a Double. Recall
> function lower:
>
> lower = until (<=x) decrease 0
>
> Notice that p is this partial function:
>
> (<=x)
>
> We must modify p to express, "Hey, compare x against an Integer value that
> has been cast to a Double value." That is implemented using a Lambda
> (anonymous) function:
>
> (\a -> (realToFrac a) <= x)
>
> Read that as: For whatever value, a, is provided convert it to a
> Fractional (Double) value and then compare it to x.
>
> Wow!
>
> So we are telling Haskell that the 0 is not to be treated as a Fractional
> (Double) value, it is to be treated as a Real (Integer) value.
>
> At last, we can implement function floor and it will return the desired
> Integer result:
>
> floor x = searchFrom 0
> where searchFrom = decrease . upper . lower
> lower = until (\a -> (realToFrac
> a) <= x) decrease
> upper = until (\a -> (realToFrac
> a) > x) increase
> decrease n = n - 1
> increase n = n + 1
>
> Notice that wherever function until is called (in lower and upper), the
> first argument, p, is a Lambda (anonymous) function that takes its
> argument, a, and casts it from a Real (Integer) value to a Fractional
> (Double) value. Here are a couple examples of using this revised floor
> function:
>
> floor (-3.4) -- returns (-4)
> floor 3.4 -- returns 3
>
> Notice that floor now returns an Integer value, which is what we want.
>
> Here is the signature for floor:
>
> floor :: (Fractional a, Ord a, Real c) => a -> c
>
> Read as: Invoke function floor with a Fractional (Double) value and it
> will return a Real (Integer) value.
>
> On page 83 Richard Bird shows a second version of function floor that uses
> a binary search:
>
> floor x = searchFrom (-1, 1)
> where searchFrom = fst . middle . cross(lower, upper)
> lower = until (<= x) double
> upper = until (> x) double
> middle = until done improve
> done (m, n) = (m + 1 == n)
> improve (m, n) = if p <= x then (p, n) else (m,
> p)
> where p = (m + n) div 2
>
> That has multiple problems. First, it is syntactically not a well-formed
> Haskell program because the div operator (on the last line) must have back
> ticks ( ` ) surrounding it:
>
> where p = (m + n) `div` 2
>
> Second, the functions lower and upper invoke function until. The first
> argument to until must be a Lambda function as described above:
>
> lower = until (\m -> (realToFrac m) <= x) double
> upper = until (\n -> (realToFrac n) > x) double
>
> Third, the function improve compares p (an Integer) against x (a Double),
> so p must be cast to a Fractional (Double) value:
>
> improve (m, n) = if (realToFrac p) <= x then (p,
> n) else (m, p)
> where p = (m + n) div 2
>
> With those three changes the function works as desired:
>
> floor x = searchFrom (-1, 1)
> where searchFrom = fst . middle . cross(lower, upper)
> lower = until (\m -> (realToFrac m) <= x) double
> upper = until (\n -> (realToFrac n) > x) double
> middle = until done improve
> done (m, n) = (m + 1 == n)
> improve (m, n) = if (realToFrac p) <= x then (p,
> n) else (m, p)
> where p = (m + n) `div`
> 2
>
> Here are a couple examples of using the revised floor function:
>
> floor (-3.4) -- returns (-4)
> floor 3.4 -- returns 3
>
> Notice that floor now returns an Integer value, which is what we want.
>
> Here is the signature for floor:
>
> floor :: (Fractional a, Ord a, Real c) => a -> c
>
> Read as: Invoke function floor with a Fractional (Double) value and it
> will return a Real (Integer) value.
>
> /Roger
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121104/8f231b0c/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 53, Issue 5
****************************************