Re: [Haskell-cafe] Debugging

2007-05-04 Thread Monang Setyawan

Sorry, replying myself.

On 5/5/07, Monang Setyawan <[EMAIL PROTECTED]> wrote:


BTW, how about adding assertion in Haskell? Can it be done?
(I've searched in my GHC 6.4.2 library documentation, and can't find 'assert')


This can be done using Assertions. Lines below are taken from the docs.

Assertions

assert :: Bool -> a -> a
If the first argument evaluates to True, then the result is the second
argument. Otherwise an AssertionFailed exception is raised, containing
a String with the source file and line number of the call to assert.

Assertions can normally be turned on or off with a compiler flag (for
GHC, assertions are normally on unless optimisation is turned on with
-O or the -fignore-asserts option is given). When assertions are
turned off, the first argument to assert is ignored, and the second
argument is returned as the result.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Debugging

2007-05-04 Thread Donald Bruce Stewart
monang:
> On 5/5/07, Stefan O'Rear <[EMAIL PROTECTED]> wrote:
> >On Fri, May 04, 2007 at 10:44:15PM -0700, Ryan Dickie wrote:
> >> I've only written trivial applications and functions in haskell. But the
> >> title of this thread got me thinking.
> >>
> >> In an imperative language you have clear steps, states, variables to 
> >watch,
> >> etc.
> >> What techniques/strategies might one use for a functional language?
> >
> >I personally most often use a divide-and-conquer approach.  I pick a
> >point about halfway down the call stack, and add trace calls.  If the
> >subproblems are handled correctly, narrow scope to higher levels;
> >otherwise narrow to lower levels.  Repeat until you have a single
> >misbehaving function.
> 
> Isn't that called binary search, instead of divide-and-conquer?
> 
> BTW, how about adding assertion in Haskell? Can it be done?
> (I've searched in my GHC 6.4.2 library documentation, and can't find 
> 'assert')

'assert' is in Control.Exception. there's also a couple of 3rd party
packages for assert-like behaviour:

http://hackage.haskell.org/cgi-bin/hackage-scripts/package/loch-0.2
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/Safe-0.1

> 
> Is there any way to automatically clean all the mess I've done in
> debugging/asserting (like removing all trace/assert expressions) when
> I compile Haskell source code? Or should I create a simple program to
> remove them?

Using a logging/writer monad might be a nice approach. 'assert's are
compiled out under -O, in G, too.

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Debugging

2007-05-04 Thread Monang Setyawan

On 5/5/07, Stefan O'Rear <[EMAIL PROTECTED]> wrote:

On Fri, May 04, 2007 at 10:44:15PM -0700, Ryan Dickie wrote:
> I've only written trivial applications and functions in haskell. But the
> title of this thread got me thinking.
>
> In an imperative language you have clear steps, states, variables to watch,
> etc.
> What techniques/strategies might one use for a functional language?

I personally most often use a divide-and-conquer approach.  I pick a
point about halfway down the call stack, and add trace calls.  If the
subproblems are handled correctly, narrow scope to higher levels;
otherwise narrow to lower levels.  Repeat until you have a single
misbehaving function.


Isn't that called binary search, instead of divide-and-conquer?

BTW, how about adding assertion in Haskell? Can it be done?
(I've searched in my GHC 6.4.2 library documentation, and can't find 'assert')

Is there any way to automatically clean all the mess I've done in
debugging/asserting (like removing all trace/assert expressions) when
I compile Haskell source code? Or should I create a simple program to
remove them?



Stefan




--
Demi masa..
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] -> IO [Int]

2007-05-04 Thread Thomas Hartman

Hoogle is also helpful

http://haskell.org/hoogle/?q=%5Bm+a%5D+-%3E+m+%5Ba%5D

2007/5/4, Phlex <[EMAIL PROTECTED]>:

Hello all,

I'm trying to learn haskell, so here's is my first newbie question.
I hope this list is appropriate for such help requests.

I'm trying to write a function with the signature [IO Int] -> IO [Int]

Here is my first attempt :

conv :: [IO Int] -> IO [Int]
conv l = do val <- (head l)
return (val : (conv (tail l)))

This does not work as I'm consing an Int to an (IO [Int]).
So I tried this :

conv2 :: [IO Int] -> IO [Int]
conv2 l = do val <- (head l)
 rest <- (conv2 (tail l))
 return (val : rest)

That works, but it won't work for infinite lists.
How could I achieve the desired result ?

Thanks in advance,
Sacha
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Debugging

2007-05-04 Thread Stefan O'Rear
On Fri, May 04, 2007 at 10:44:15PM -0700, Ryan Dickie wrote:
> I've only written trivial applications and functions in haskell. But the
> title of this thread got me thinking.
> 
> In an imperative language you have clear steps, states, variables to watch,
> etc.
> What techniques/strategies might one use for a functional language?

I personally most often use a divide-and-conquer approach.  I pick a
point about halfway down the call stack, and add trace calls.  If the
subproblems are handled correctly, narrow scope to higher levels;
otherwise narrow to lower levels.  Repeat until you have a single
misbehaving function.

Stefan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Debugging

2007-05-04 Thread Ryan Dickie

On 5/4/07, Monang Setyawan <[EMAIL PROTECTED]> wrote:


On 5/5/07, Stefan O'Rear <[EMAIL PROTECTED]> wrote:
> On Sat, May 05, 2007 at 11:36:16AM +0700, Monang Setyawan wrote:
> > Hi, I'm a beginner Haskell user.
> >
> > Is there any way to trace/debug the function application in GHC?
>
> Absolutely!
>
> [EMAIL PROTECTED]:/tmp$ ghci X.hs
>___ ___ _
>   / _ \ /\  /\/ __(_)
>  / /_\// /_/ / /  | |GHC Interactive, version 6.7.20070502, for
Haskell 98.
> / /_\\/ __  / /___| |http://www.haskell.org/ghc/
> \/\/ /_/\/|_|Type :? for help.
>
> Loading package base ... linking ... done.
> [1 of 1] Compiling Main ( X.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> :break fac

Great!! Thanks, it really helps.
I should update my GHC to the newest version (I use the old  6.4.2
with no break command)

Is there any editor/IDE supporting this break command? It should be
cooler if we can debug functions just by placing mark in the line.

>
> Stefan
>


--
Demi masa..


--
Demi masa..
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




I've only written trivial applications and functions in haskell. But the
title of this thread got me thinking.

In an imperative language you have clear steps, states, variables to watch,
etc.
What techniques/strategies might one use for a functional language?

--ryan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Detect Either Windows or Linux environment

2007-05-04 Thread SevenThunders



Brandon S. Allbery KF8NH wrote:
> 
> 
> On May 5, 2007, at 0:21 , SevenThunders wrote:
>> Is there a simple way to detect what operating system a given  
>> Haskell program
>> is running under?
> 
> mress:5002 Z$ ghci
> ___ ___ _
>/ _ \ /\  /\/ __(_)
> / /_\// /_/ / /  | |  GHC Interactive, version 6.7.20070322, for  
> Haskell 98.
> / /_\\/ __  / /___| |  http://www.haskell.org/ghc/
> \/\/ /_/\/|_|  Type :? for help.
> 
> Loading package base ... linking ... done.
> Prelude> :m +System.Info
> Prelude System.Info> [os, arch]
> ["darwin","powerpc"]
> Prelude System.Info>
> 
> -- 
> brandon s. allbery  [solaris,freebsd,perl,pugs,haskell]   
> [EMAIL PROTECTED]
> system administrator  [openafs,heimdal,too many hats]   
> [EMAIL PROTECTED]
> electrical and computer engineering, carnegie mellon university   
> KF8NH
> 
> 
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

Thank you for your help.  I somehow missed that when I was browsing through
the library documentation.
On windows, interestingly the os function returns "mingw"
-- 
View this message in context: 
http://www.nabble.com/Detect-Either-Windows-or-Linux-environment-tf3695277.html#a10333801
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Debuggingy

2007-05-04 Thread Monang Setyawan

On 5/5/07, Stefan O'Rear <[EMAIL PROTECTED]> wrote:

On Sat, May 05, 2007 at 11:36:16AM +0700, Monang Setyawan wrote:
> Hi, I'm a beginner Haskell user.
>
> Is there any way to trace/debug the function application in GHC?

Absolutely!

[EMAIL PROTECTED]:/tmp$ ghci X.hs
   ___ ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |GHC Interactive, version 6.7.20070502, for Haskell 98.
/ /_\\/ __  / /___| |http://www.haskell.org/ghc/
\/\/ /_/\/|_|Type :? for help.

Loading package base ... linking ... done.
[1 of 1] Compiling Main ( X.hs, interpreted )
Ok, modules loaded: Main.
*Main> :break fac


Great!! Thanks, it really helps.
I should update my GHC to the newest version (I use the old  6.4.2
with no break command)

Is there any editor/IDE supporting this break command? It should be
cooler if we can debug functions just by placing mark in the line.



Stefan




--
Demi masa..


--
Demi masa..
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Debuggingy

2007-05-04 Thread Stefan O'Rear
On Sat, May 05, 2007 at 11:36:16AM +0700, Monang Setyawan wrote:
> Hi, I'm a beginner Haskell user.
> 
> Is there any way to trace/debug the function application in GHC?

Absolutely!

[EMAIL PROTECTED]:/tmp$ ghci X.hs
   ___ ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |GHC Interactive, version 6.7.20070502, for Haskell 98.
/ /_\\/ __  / /___| |http://www.haskell.org/ghc/
\/\/ /_/\/|_|Type :? for help.

Loading package base ... linking ... done.
[1 of 1] Compiling Main ( X.hs, interpreted )
Ok, modules loaded: Main.
*Main> :break fac
Breakpoint 0 activated at X.hs:(2,0)-(5,34)
*Main> fac 3
Stopped at X.hs:(2,0)-(5,34)
_result :: Int
[X.hs:(2,0)-(5,34)] *Main> :step
Stopped at X.hs:2:8-15
_result :: Int
x :: Int
fac' :: Int -> Int -> Int
[X.hs:2:8-15] *Main> x
3
[X.hs:2:8-15] *Main> :step
Stopped at X.hs:(4,8)-(5,34)
_result :: Int
[X.hs:(4,8)-(5,34)] *Main> :list
3  where
4  fac' 0 a = a
5  fac' k a = fac' (k-1) (a*k)
6  
[X.hs:(4,8)-(5,34)] *Main> :step
Stopped at X.hs:5:19-34
_result :: Int
a :: Int
ds :: Int
[X.hs:5:19-34] *Main> a
1
[X.hs:5:19-34] *Main> ds
3
[X.hs:5:19-34] *Main> :list
4  fac' 0 a = a
5  fac' k a = fac' (k-1) (a*k)
6  
[X.hs:5:19-34] *Main> Leaving GHCi.
[EMAIL PROTECTED]:/tmp$ cat X.hs
fac :: Int -> Int
fac x = fac' x 1
where
fac' 0 a = a
fac' k a = fac' (k-1) (a*k)
[EMAIL PROTECTED]:/tmp$ 

Stefan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Debugging

2007-05-04 Thread Monang Setyawan

Hi, I'm a beginner Haskell user.

Is there any way to trace/debug the function application in GHC?

--
Demi masa..
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Detect Either Windows or Linux environment

2007-05-04 Thread Brandon S. Allbery KF8NH


On May 5, 2007, at 0:21 , SevenThunders wrote:
Is there a simple way to detect what operating system a given  
Haskell program

is running under?


mress:5002 Z$ ghci
   ___ ___ _
  / _ \ /\  /\/ __(_)
/ /_\// /_/ / /  | |  GHC Interactive, version 6.7.20070322, for  
Haskell 98.

/ /_\\/ __  / /___| |  http://www.haskell.org/ghc/
\/\/ /_/\/|_|  Type :? for help.

Loading package base ... linking ... done.
Prelude> :m +System.Info
Prelude System.Info> [os, arch]
["darwin","powerpc"]
Prelude System.Info>

--
brandon s. allbery  [solaris,freebsd,perl,pugs,haskell]   
[EMAIL PROTECTED]
system administrator  [openafs,heimdal,too many hats]   
[EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon university   
KF8NH



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Detect Either Windows or Linux environment

2007-05-04 Thread SevenThunders

Is there a simple way to detect what operating system a given Haskell program
is running under?
It would help to make some programs that might have to interact with the
operating system more portable.

I haven't been able to quite figure out a simple way to do this.


-- 
View this message in context: 
http://www.nabble.com/Detect-Either-Windows-or-Linux-environment-tf3695277.html#a10333531
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Cost of Overloading vs. HOFs

2007-05-04 Thread Conal Elliott

[switching to haskell-cafe]

thanks for the explanation, John.  doesn't the list cases mentioned in the
definition of (+) below assume whole program, in order to know all Num
instances?

On 5/4/07, John Meacham <[EMAIL PROTECTED]> wrote:


On Fri, May 04, 2007 at 05:06:10PM -0700, Conal Elliott wrote:
> Cool.  You know which types to consider because jhc is a whole-program
> compiler?

Yes. though, i have since redone the back end so there is no technical
reason for it to be whole program any more, you can just benefit from
more optimizations that way. The old backend required global knowledge
to compile at all.


> Given the whole program, why not monomorphize, and inline away all of
the
> dictionaries?

Well that is the thing, there are no dictionaries at all, the only extra
arguments passed in are the types so,

f :: forall a . (Foo a, Baz a, Bar a Int, Bred a) => a -> a

still is only passed the single hidden argument of 'a' since a case on
it will determine what method to use.

(+) a x y = case a of
Int -> plusInt x y
Float -> plusFloat x y

and so forth. a here is a type, of kind '*'.


since the method lookup is done explicitly via the case statement, it
can be optimized via standard transformations in nice ways.

John






>
> - Conal
>
> On 5/4/07, John Meacham <[EMAIL PROTECTED]> wrote:
> >
> >On Fri, May 04, 2007 at 03:07:41PM -0700, Conal Elliott wrote:
> >> Does anyone know what became of Dictionary-free Overloading by
Partial
> >> Evaluation ?  Is it
> >> impractical for some reason?
> >
> >jhc also uses a dictionary free approach, doing a case directly on the
> >type parameter.
> >
> >The nice thing about this is that _all_ methods can be determined by a
> >single case evaluation, because finding out the right instance for any
> >method will determine the right instance for all other methods too for
a
> >given type. The standard case-of-known-value optimization takes care of
> >later scrutinizations (method lookups) on the same type.
> >
> >John
> >
> >
> >--
> >John Meacham - ⑆repetae.net⑆john⑈
> >___
> >Glasgow-haskell-users mailing list
> >[EMAIL PROTECTED]
> >http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
> >

> --
> Glasgow-haskell-users mailing list
> [EMAIL PROTECTED]
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


--
John Meacham - ⑆repetae.net⑆john⑈
___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Dan Piponi

On 5/4/07, Derek Elkins <[EMAIL PROTECTED]> wrote:

Dan Piponi wrote:
> I don't know anything about Continuation-based IO. Is there a
> reference online I could read about it?



Note, however, that I was being sardonic in my previous email.


Nonetheless, I'd never heard of it, it sounds intriguing, and I
couldn't find anything with google that succinctly explained what it
was. So thanks for the link.
--
Dan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Derek Elkins

Dan Piponi wrote:

I don't know anything about Continuation-based IO. Is there a
reference online I could read about it?


Many of the hits you get from putting 'Haskell "continuation-based IO" into 
Google give some information.  But here is one reasonable paper on it

http://citeseer.ist.psu.edu/hudak89expressiveness.html
"On the Expressiveness of Purely-Functional I/O Systems" Hudak 1989. (Also, one 
may want to look at "The Essence of Functional Programming".)


Note, however, that I was being sardonic in my previous email.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] -> IO [Int]

2007-05-04 Thread Phlex

Daniel Fischer wrote:

On 5/4/07, Phlex <[EMAIL PROTECTED]> wrote:
  

I'm trying to write a function with the signature [IO Int] -> IO [Int]


Control.Monad has a function (called "sequence") that does this for you. In fact, 
"sequence" is a more generic solution since its signature is (
Monad m => [m a] -> m [a]).



Unfortunately, this won't work for infinite lists either, for those you'd need 
an 'unsafe' action, namely 'unsafeInterleaveIO', like


import System.IO.Unsafe

conv :: [IO a] -> IO [a]
conv [] = return []
conv (x:xs) = do a <- x
 as <- unsafeInterleaveIO (conv xs)
 return (a:as)


*Test> :set -fno-print-bind-result
*Test> xs <- conv $ map return [1 .. ]
*Test> take 20 xs
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
  
HTH,

Daniel
  

Thank you all for the very good answers.

Sacha
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Dan Piponi

I don't know anything about Continuation-based IO. Is there a
reference online I could read about it?
--
Dan

On 5/4/07, Derek Elkins <[EMAIL PROTECTED]> wrote:

Bulat Ziganshin wrote:
> Hello Adrian,
>
> Friday, May 4, 2007, 11:43:35 AM, you wrote:
>
>> don't understand what this monad thingy is all about.
>
> the whole monadic business was introduced with the sole goal to let
> haskellers believe that they are smarter than other programmers :)

Indeed.  Continuation-based IO would have been so much clearer.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Poor first impression

2007-05-04 Thread John Meacham
ghc is in fedora extras, all you needed to do to install it is

; yum -y install ghc

just like you would install most everything on a fedora system.

John

-- 
John Meacham - ⑆repetae.net⑆john⑈
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] -> IO [Int]

2007-05-04 Thread Daniel Fischer

> -Ursprüngliche Nachricht-
> Von: "Rich Neswold" <[EMAIL PROTECTED]>
> Gesendet: 04.05.07 19:31:53
> An: Phlex <[EMAIL PROTECTED]>
> CC: haskell-cafe@haskell.org
> Betreff: Re: [Haskell-cafe] [IO Int] -> IO [Int]

On 5/4/07, Phlex <[EMAIL PROTECTED]> wrote:
> Hello all,
> 
> I'm trying to learn haskell, so here's is my first newbie question.
> I hope this list is appropriate for such help requests.
> 
> I'm trying to write a function with the signature [IO Int] -> IO [Int]
> 
> 
> Control.Monad has a function (called "sequence") that does this for you. In 
> fact, "sequence" is a more generic solution since its signature is (
> Monad m => [m a] -> m [a]).

Unfortunately, this won't work for infinite lists either, for those you'd need 
an 'unsafe' action, namely 'unsafeInterleaveIO', like


import System.IO.Unsafe

conv :: [IO a] -> IO [a]
conv [] = return []
conv (x:xs) = do a <- x
 as <- unsafeInterleaveIO (conv xs)
 return (a:as)


*Test> :set -fno-print-bind-result
*Test> xs <- conv $ map return [1 .. ]
*Test> take 20 xs
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

> 
> As a newbie, I found it educational to peruse the various modules in 
> "http://haskell.org/ghc/docs/latest/html/libraries/
> ". Just start looking at modules that sound interesting and see what has 
> already been defined. Some modules at first may be too advanced, but if you 
> go back to them in a few days (weeks?), they'll start making more sense, too.
> 

Very good advice, I think, and it's also very instructive to read the source 
code, you can learn a lot from that.

> 
> -- 
> Rich

HTH,
Daniel


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] -> IO [Int]

2007-05-04 Thread Jeff Polakow
Hello,

> I'm trying to learn haskell, so here's is my first newbie question.
> I hope this list is appropriate for such help requests.
> 
Yes.

> I'm trying to write a function with the signature [IO Int] -> IO [Int]
> 
As other people have mentioned, the library function sequence has this 
type (actually a type which generalizes this type).

> conv2 :: [IO Int] -> IO [Int]
> conv2 l = do val <- (head l)
>  rest <- (conv2 (tail l))
>  return (val : rest)
> 
> That works, 
>
This doesn't quite work since you don't cover the case for empty lists. 
You need to add another clause:

conv2 [] = return []

> but it won't work for infinite lists.
> How could I achieve the desired result ?
> 
I don't think a function of this type makes sense for infinite lists. Even 
the library function sequence will diverge on an infinite list. The issue 
is that you must evaluate all of the input [IO Int] to get the pure [Int] 
output. In other words, the type IO [Int] means an IO action which results 
in a (pure) list of ints. Thus, you cannot start returning the result 
[Int] while there are still IO actions to perform to compute the rest of 
the result.

-Jeff



---

This e-mail may contain confidential and/or privileged information. If you 
are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and destroy this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] outputing .hs file without comments

2007-05-04 Thread Duncan Coutts
On Fri, 2007-05-04 at 15:01 +0100, Frederik Eaton wrote:
> Is it possible to use Language.Haskell to print a program with
> comments preserved? That might be useful for refactoring.
> 
> Not that Language.Haskell isn't already cool enough, if the answer is
> "no", of course.

The answer is no, but take a look at HaRe the Haskell Refactoring tool:

http://www.cs.kent.ac.uk/projects/refactor-fp/hare.html


Duncan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] -> IO [Int]

2007-05-04 Thread Alfonso Acosta

the 'sequence' prelude function does exactly what you want.


It's implemented as

sequence ms = foldr k (return []) ms
where
  k m m' = do { x <- m; xs <- m'; return (x:xs) }

On 5/4/07, Phlex <[EMAIL PROTECTED]> wrote:

Hello all,

I'm trying to learn haskell, so here's is my first newbie question.
I hope this list is appropriate for such help requests.

I'm trying to write a function with the signature [IO Int] -> IO [Int]

Here is my first attempt :

conv :: [IO Int] -> IO [Int]
conv l = do val <- (head l)
return (val : (conv (tail l)))

This does not work as I'm consing an Int to an (IO [Int]).
So I tried this :

conv2 :: [IO Int] -> IO [Int]
conv2 l = do val <- (head l)
 rest <- (conv2 (tail l))
 return (val : rest)

That works, but it won't work for infinite lists.
How could I achieve the desired result ?

Thanks in advance,
Sacha
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] -> IO [Int]

2007-05-04 Thread Rich Neswold

On 5/4/07, Phlex <[EMAIL PROTECTED]> wrote:


Hello all,

I'm trying to learn haskell, so here's is my first newbie question.
I hope this list is appropriate for such help requests.

I'm trying to write a function with the signature [IO Int] -> IO [Int]



Control.Monad has a function (called "sequence") that does this for you. In
fact, "sequence" is a more generic solution since its signature is
(Monadm
=> [m a] -> m [a]).

As a newbie, I found it educational to peruse the various modules in "
http://haskell.org/ghc/docs/latest/html/libraries/";. Just start looking at
modules that sound interesting and see what has already been defined. Some
modules at first may be too advanced, but if you go back to them in a few
days (weeks?), they'll start making more sense, too.

--
Rich

AIM : rnezzy
ICQ : 174908475
Jabber: [EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] [IO Int] -> IO [Int]

2007-05-04 Thread Phlex

Hello all,

I'm trying to learn haskell, so here's is my first newbie question.
I hope this list is appropriate for such help requests.

I'm trying to write a function with the signature [IO Int] -> IO [Int]

Here is my first attempt :

conv :: [IO Int] -> IO [Int]
conv l = do val <- (head l)
   return (val : (conv (tail l)))

This does not work as I'm consing an Int to an (IO [Int]).
So I tried this :

conv2 :: [IO Int] -> IO [Int]
conv2 l = do val <- (head l)
rest <- (conv2 (tail l))
return (val : rest)

That works, but it won't work for infinite lists.
How could I achieve the desired result ?

Thanks in advance,
Sacha
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Derek Elkins

Bulat Ziganshin wrote:

Hello Adrian,

Friday, May 4, 2007, 11:43:35 AM, you wrote:


don't understand what this monad thingy is all about.


the whole monadic business was introduced with the sole goal to let
haskellers believe that they are smarter than other programmers :)


Indeed.  Continuation-based IO would have been so much clearer.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Bulat Ziganshin
Hello Adrian,

Friday, May 4, 2007, 11:43:35 AM, you wrote:

> don't understand what this monad thingy is all about.

the whole monadic business was introduced with the sole goal to let
haskellers believe that they are smarter than other programmers :)


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Dan Piponi

Hi Adrian,


They take a function f and something and return what f does to that. I
don't see why they should return a function.


Consider a function, f, that takes two arguments, one of type A, and
one of type B, and returns something of type C. In conventional
mathematical notation this would be written as f : A x B -> C.

Think about what this function does from a practical point of view. It
waits to be handed an object of type A. It then waits for an object of
type B. And now it can return an object of type C.

But suppose we hand f an object of type A, but not another of type B.
What do we get? Well we basically have something that's still waiting
for an object of type B, and when it gets that it'll return an object
of type C. Something that's waiting for a B in order to give you a C
is of type B->C. So after you've given f an A, you get back a B->C. So
f can be thought of as being of type A->(B->C).

So there are two ways of thinking of f. (1) A function that takes an A
and a B and gives you a C. Or (2) a function that takes an A and gives
you back a function mapping a B to a C. Haskell blurs the distinction
between these things and the transition from view (1) to view (2) is
called 'currying'.

Internally, the Haskell parser automatically converts A->B->C to
A->(B->C) so you can view the former as simply being shorthand for the
latter. (In CS-speak '->' is considered to be right associative but
that's just another way of saying the same thing.)
--
Dan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Hugs dropped Text.Regex?!

2007-05-04 Thread Chris Kuklewicz
John Goerzen wrote:
> I learned today, as I tried to run my ftphs unit tests under Hugs, that
> Hugs dropped Text.Regex between 200503.08 and 200609.21.
> 
> The Hugs download page claims that they did this because the new GHC
> implementation of Text.Regex is not compatible with Hugs.
> 
> But why can't Hugs keep the old implementation?
> 
> This is going to render Hugs just about useless for me.  I like regexps.
> 
> -- John

I will explain how to setup Text.Regex.* for Hugs (I hope)...

I have not been using Hugs, but I have created the current Text.Regex code in
use by GHC.

The stable regex-base package that comes with GHC 6.6 and 6.6.1 has instances
(of RegexContext) that depend on GHC's late resolution of overlapping instances.
 Hugs can only use that with some instanes removed (via the preprocessor).

The unstable regex-base package you can get with darcs is version 0.91 from
http://darcs.haskell.org/packages/regex-unstable/regex-base/ has been modified
to use a few newtypes to allow both GHC and Huge to use all the instances.

>From there you want a backend.  The old backend use the local regex.h api from
the c library and is in the regex-posix version 0.92 package at
http://darcs.haskell.org/packages/regex-unstable/regex-posix/

To get the old haskell API to this backend (Text.Regex itself) you need
regex-compat version 0.90 at
http://darcs.haskell.org/packages/regex-unstable/regex-compat/

To get the new pure haskell backend (works better in almost all cases) you need
regex-tda at
http://darcs.haskell.org/packages/regex-unstable/regex-tdfa/

Note that this regex-tdfa is the latest version, 0.93, which now has the patch
that Ross Paterson sent to me and makes it Hugs compatible.

So it is broken up into several smaller packages.  I think all but
regex-tdfa-0.93 are also available from
http://hackage.haskell.org/packages/archive/pkg-list.html#cat:Text

Cheers,
  Chris
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Hugs dropped Text.Regex?!

2007-05-04 Thread John Goerzen
I learned today, as I tried to run my ftphs unit tests under Hugs, that
Hugs dropped Text.Regex between 200503.08 and 200609.21.

The Hugs download page claims that they did this because the new GHC
implementation of Text.Regex is not compatible with Hugs.

But why can't Hugs keep the old implementation?

This is going to render Hugs just about useless for me.  I like regexps.

-- John

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] outputing .hs file without comments

2007-05-04 Thread Frederik Eaton
Is it possible to use Language.Haskell to print a program with
comments preserved? That might be useful for refactoring.

Not that Language.Haskell isn't already cool enough, if the answer is
"no", of course.

Frederik

On Fri, May 04, 2007 at 10:33:07AM +1000, Donald Bruce Stewart wrote:
> jmvilaca:
> > 
> >Hi all,
> > 
> > 
> >Is there a simple tool or command to remove all comments
> >from a Haskell file, i.e. something that outputs the input
> >file but without any comments on it?
> 
> Using Language.Haskell, such a program is almost trivial:
>
> --
> -- strip comments from haskell source
> --
> 
> {- the main module -}
> 
> import Language.Haskell.Parser
> import Language.Haskell.Pretty
> 
> main = interact $ \s -> case parseModule s of
> ParseFailed loc str -> (show loc)  ++ "\n"
> ParseOk m   -> (prettyPrint m) ++ "\n" 
> 
> And running this program on itself:
> 
> $ runhaskell A.hs < A.hs
> module Main (main) where
> import Language.Haskell.Parser
> import Language.Haskell.Pretty
> main
>   = interact $
>   \ s ->
> case parseModule s of
> ParseFailed loc str -> (show loc) ++ "\n"
> ParseOk m -> (prettyPrint m) ++ "\n"
> 
> 
> Hehe, it also pretty prints :-)
> 
> -- Don
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

-- 
http://ofb.net/~frederik/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Monad definition question

2007-05-04 Thread Robin Green
On Fri, 04 May 2007 14:42:53 +0300
Ilya Tsindlekht <[EMAIL PROTECTED]> wrote:

> Does the definition of monad silently assume that if f and f' are
> equal in the sense that they return the same value for any argument o
> correct type then m >>= f = m >>= f'

How could it be otherwise? How are you going to distinguish between f
and f' if they are indistinguishable functions, in Haskell?

> More specifically, the definition says x >>= return = x. How does one
> justify from this that x >>= (return . id) = x?
> 
> Are values of type a -> b in general assumed to be maps from the set
> of values of type a into the set ov values of type b?

Yes - if _|_ is considered to be a value.

> (What bothers
> me is that the problem whether two lambda-expressions define the same
> map is clearly undecidable.)

Yes. But this is a fundamental mathematical issue which isn't at all
specific to Haskell, of course. It suggests using some sort of
intensional type theory, so that you have to explicitly prove lambda
expressions to be equal.

> More generally, is some kind of logic without equality more
> appropriate for formalisation of Haskell then the usual kind(s) of
> logic with equality?

I suggest you look into Observational Type Theory.
-- 
Robin
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Monad definition question

2007-05-04 Thread Ilya Tsindlekht
Does the definition of monad silently assume that if f and f' are equal
in the sense that they return the same value for any argument o correct
type then m >>= f = m >>= f'

More specifically, the definition says x >>= return = x. How does one
justify from this that x >>= (return . id) = x?

Are values of type a -> b in general assumed to be maps from the set of
values of type a into the set ov values of type b? (What bothers me is
that the problem whether two lambda-expressions define the same map is
clearly undecidable.)

More generally, is some kind of logic without equality more appropriate
for formalisation of Haskell then the usual kind(s) of logic with
equality?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Michael Vanier

The -> in type signatures associates to the right, so the type signatures

> fmap :: (a -> b) -> (W a -> W b)
> bind :: (a -> W b) -> (W a -> W b)

are the same as:

> fmap :: (a -> b) -> W a -> W b
> bind :: (a -> W b) -> W a -> W b

Sometimes people put in the extra parentheses because they want to 
emphasize a particular way to use the function.


I'm assuming you understand that a function that takes two arguments and 
returns a (possibly non-function) value is equivalent to a function that 
takes one argument that returns a function that takes the other argument 
and returns a value.


HTH,

Mike



Adrian Neumann wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA512

I've read this blogpost about the "trivial monad"
http://sigfpe.blogspot.com/2007/04/trivial-monad.html, because I still
don't understand what this monad thingy is all about.

The author defines three functions:

data W a = W a deriving Show

return :: a -> W a
return x = W x

fmap :: (a -> b) -> (W a -> W b)
fmap f (W x) = W (f x)

bind :: (a -> W b) -> (W a -> W b)
bind f (W x) = f x

and asks the reader to prove the tree monad laws for them. However I
don't understand the type signatures for bind and fmap. I'd say (and
ghci's type inference agrees) that bind and fmap have the type

bind:: (a->W b) -> W a -> W b
fmap:: (a->b) -> W a -> W b

They take a function f and something and return what f does to that. I
don't see why they should return a function.

This of course makes it hard for me to prove the monad laws. The first
however works nonetheless:

1) bind f (return a)= f a

=> bind f (return a)= bind f (W a) = f a

Can someone explain bind and fmap (and possible law 2 and 3)?

Thanks,

Adrian
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGOuQS11V8mqIQMRsRCmngAJ9NwQMwXeS/PSM1NUsVA8gxPuA0KACfSLiA
ItqRZW5a4XyQ099bhMtSWmU=
=/8i/
-END PGP SIGNATURE-
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The Trivial Monad

2007-05-04 Thread Thomas Davie


On 4 May 2007, at 08:43, Adrian Neumann wrote:


However I
don't understand the type signatures for bind and fmap. I'd say (and
ghci's type inference agrees) that bind and fmap have the type

bind:: (a->W b) -> W a -> W b
fmap:: (a->b) -> W a -> W b

They take a function f and something and return what f does to that. I
don't see why they should return a function.


I suggest you look up currying.  In the mean time, I shall attempt an  
explanation.


In Haskell (as in many other functional languages), function types  
appear as if the function were curried.  This means that a function  
accepts one single argument, and returns one single result.  The  
important thing to realise though is that the result (or less  
importantly the argument) may be a function.


Let's study a (slightly over constrained) variant on the (+)  
function, with type (+) :: Int -> Int -> Int.  This type signature  
should be read to mean:


(+) takes an Int, and returns a new function of type (Int -> Int).   
We can see an example of this (+ 5) -- (+) is given an argument (5),  
and returns a function (that adds 5 to integers).


Another way to think about it is that the (->) type constructor is  
right associative, so any type written as a -> b -> c -> d -> e, can  
also be written as a -> (b -> (c -> (d -> e))).


I hope that helped a bit, and if it didn't I suggest going and  
looking up currying in as many places as you can.


Bob___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] The Trivial Monad

2007-05-04 Thread Adrian Neumann
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA512

I've read this blogpost about the "trivial monad"
http://sigfpe.blogspot.com/2007/04/trivial-monad.html, because I still
don't understand what this monad thingy is all about.

The author defines three functions:

data W a = W a deriving Show

return :: a -> W a
return x = W x

fmap :: (a -> b) -> (W a -> W b)
fmap f (W x) = W (f x)

bind :: (a -> W b) -> (W a -> W b)
bind f (W x) = f x

and asks the reader to prove the tree monad laws for them. However I
don't understand the type signatures for bind and fmap. I'd say (and
ghci's type inference agrees) that bind and fmap have the type

bind:: (a->W b) -> W a -> W b
fmap:: (a->b) -> W a -> W b

They take a function f and something and return what f does to that. I
don't see why they should return a function.

This of course makes it hard for me to prove the monad laws. The first
however works nonetheless:

1) bind f (return a)= f a

=> bind f (return a)= bind f (W a) = f a

Can someone explain bind and fmap (and possible law 2 and 3)?

Thanks,

Adrian
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGOuQS11V8mqIQMRsRCmngAJ9NwQMwXeS/PSM1NUsVA8gxPuA0KACfSLiA
ItqRZW5a4XyQ099bhMtSWmU=
=/8i/
-END PGP SIGNATURE-
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe