global, modifiable variable for debugging

Hi, I have a program with a debug flag in it (Strangely I've yet to be able to write bug-free code). I'd like to change the state of the debug flag based on command line args. I looked at IOVar but that would cause all the pure procedures to get swallowed by the IO Monad. Is a better way to get this behavior ? Thanks, Brian

I think something like this would work:
myFlag = unsafePerformIO $ do
[x] <- getArgs
return $ x == "debug"
In general, unsafePerformIO should be avoided, but this is one of
those situations where we can give you a pass ;).
Michael
On Sun, Dec 26, 2010 at 9:53 PM,
Hi,
I have a program with a debug flag in it (Strangely I've yet to be able to write bug-free code). I'd like to change the state of the debug flag based on command line args.
I looked at IOVar but that would cause all the pure procedures to get swallowed by the IO Monad.
Is a better way to get this behavior ?
Thanks,
Brian
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Seems like you'd want x <- getArgs and x == ["debug"] rather than the
irrefutable pattern match.
On Sun, Dec 26, 2010 at 3:04 PM, Michael Snoyman
I think something like this would work:
myFlag = unsafePerformIO $ do [x] <- getArgs return $ x == "debug"
In general, unsafePerformIO should be avoided, but this is one of those situations where we can give you a pass ;).
Michael
On Sun, Dec 26, 2010 at 9:53 PM,
wrote: Hi,
I have a program with a debug flag in it (Strangely I've yet to be able to write bug-free code). I'd like to change the state of the debug flag based on command line args.
I looked at IOVar but that would cause all the pure procedures to get swallowed by the IO Monad.
Is a better way to get this behavior ?
Thanks,
Brian
_______________________________________________ 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

If you don't want go the unsafePerformIO route you might use implicit parameters [1]. You can add an hidden parameter to a function like: {-# LANGUAGE ImplicitParams #-} func1 :: (?dbg :: Bool) => String -> String func1 s = if ?dbg then (func2 ("func1 : " ++ s)) else s func2 :: (?dbg :: Bool) => String -> String func2 s = if ?dbg then (let ?dbg = not ?dbg in func3 ("func2 : " ++ s)) else s func3 :: (?dbg :: Bool) => String -> String func3 s = if ?dbg then ("func3 : " ++ s) else s test bool = do let ?dbg = bool putStrLn $ func1 "hello world" putStrLn $ func3 "goodbye world"
test True func2 : func1 : hello world func3 : goodbye world
test False hello world goodbye world
Notice that the value of ?dbg is propagated even though it is never
explicitly passed into func1. Similarly func1 never explicitly passes
?dbg to func2. Also as in func2 you can change ?dbg for other
functions downstream, but not the original ?dbg as shown by the second
"putStrLn ... ".
It seems a more flexible approach to a global variable but it does
litter your function signatures with "(?dbg :: Bool)".
-deech
[1] http://www.haskell.org/ghc/docs/7.0-latest/html/users_guide/other-type-exten...
On Sun, Dec 26, 2010 at 1:53 PM,
Hi,
I have a program with a debug flag in it (Strangely I've yet to be able to write bug-free code). I'd like to change the state of the debug flag based on command line args.
I looked at IOVar but that would cause all the pure procedures to get swallowed by the IO Monad.
Is a better way to get this behavior ?
Thanks,
Brian
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

We have some code to do exactly this:
https://github.com/snapframework/snap-core/blob/master/src/Snap/Internal/Deb...
You set a DEBUG environment variable to turn debugging output on. We
should probably split this code out into its own package.
G
On Sun, Dec 26, 2010 at 8:53 PM,
Hi,
I have a program with a debug flag in it (Strangely I've yet to be able to write bug-free code). I'd like to change the state of the debug flag based on command line args.
I looked at IOVar but that would cause all the pure procedures to get swallowed by the IO Monad.
Is a better way to get this behavior ?
Thanks,
Brian
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
--
Gregory Collins
participants (5)
-
aditya siram
-
briand@aracnet.com
-
Daniel Peebles
-
Gregory Collins
-
Michael Snoyman