Re: unsafePerformIO to give warnings

2001-12-21 Thread Ian Lynagh

On Fri, Dec 21, 2001 at 10:21:06AM -, Simon Marlow wrote:
 
  If I want to give warnings when doing something and I don't care too
  much about the order they appear in, can I use this?
  
  foo x = if success x then Just x
   else warn Working out x went wrong Nothing
  
  warn :: String - a - a
  warn s x = unsafePerformIO (hPutStrLn stderr s) `seq` x
 
 This is what IOExts.trace is for, BTW.

Doh - I associate that with debugging and it never even occurred to me!

  warnings interspersed?
 
 It is possible to get warnings interspersed if you write something like
 
   warn (abc ++ warn foo def)

Ah, yes, OK, thanks.

  hugs and ghci only seem to print the first warning, but ISTR similar
  problems happen with threads and got the impression that they were in
  the wrong.
 
 Are you sure that laziness and/or optimisation aren't to blame for only
 getting a single warning?  Given that you say Hugs also produces the
 same result, it sounds like a laziness issue.

Sorry, I meant hugs and ghci only seem to print the warning the first
time I run the program, e.g. with

module Main where

import IOExts

main :: IO()
main = trace foo (putStrLn bar)

in hugs:

[...]
Type :? for help
Main main
foobar

Main main
bar

Main main
bar

Main main
bar

and in ghci:

Skipping  Main ( q.lhs, ./q.o )
Main main
foo
bar
Main main
bar
Main main
bar
Main main
bar
Main 


Thanks again
Ian


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



RE: unsafePerformIO to give warnings

2001-12-21 Thread Simon Marlow


Ian Lynagh writes:
 Sorry, I meant hugs and ghci only seem to print the warning the first
 time I run the program, e.g. with
 
 module Main where
 
 import IOExts
 
 main :: IO()
 main = trace foo (putStrLn bar)
 
 in hugs:
 
 [...]
 Type :? for help
 Main main
 foobar
 
 Main main
 bar
[snip]
 and in ghci:
 
 Skipping  Main ( q.lhs, ./q.o )
 Main main
 foo
 bar
 Main main
 bar
[snip]

This is because 'main' is a CAF: the act of reducing it to normal form
the first time forces the warning to be printed, but thereafter the
evaluated version is returned immediately.

To subvert this behaviour, in GHCi you can say ':set +r' which causes
all CAFs to be reverted between evaluations.

Cheers,
Simon

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



unsafePerformIO to give warnings

2001-12-20 Thread Ian Lynagh


Hi all

If I want to give warnings when doing something and I don't care too
much about the order they appear in, can I use this?

foo x = if success x then Just x
 else warn Working out x went wrong Nothing

warn :: String - a - a
warn s x = unsafePerformIO (hPutStrLn stderr s) `seq` x

The hslibs docs say

If the I/O computation wrapped in unsafePerformIO performs side
effects, then the relative order in which those side effects take
place (relative to the main I/O trunk, or other calls to
unsafePerformIO) is indeterminate.

but it's not entirely clear on whether or not I could end up with 2
warnings interspersed?

And is it guaranteed that the warnings will be printed at some point?
hugs and ghci only seem to print the first warning, but ISTR similar
problems happen with threads and got the impression that they were in
the wrong.


Thanks
Ian


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell