toolshed-0.11.1.0: src/ToolShed/Unsafe.hs
{-
Copyright (C) 2010 Dr. Alistair Ward
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-}
{- |
[@AUTHOR@] Dr. Alistair Ward
[@DESCRIPTION@]
* Provides impure functions to print & return the specified value.
* The pure interfaces permit calls from outside the /IO-monad/.
[@TODO@] Review function-names.
-}
module ToolShed.Unsafe(
-- * Functions
print',
printShow
) where
import qualified System.IO
import qualified System.IO.Unsafe
{-# NOINLINE print' #-}
-- | A transparent print-function, for use in debugging.
print' :: Show s
=> s -- ^ Arbitrary polymorphic input-data.
-> s -- ^ Output (identical to input).
print' = System.IO.Unsafe.unsafePerformIO . f where
f :: Show s => s -> IO s
f x = System.IO.hPrint System.IO.stderr x >> return {-to IO-monad-} x
{-# NOINLINE printShow #-}
-- | A transparent print-function, which prepends the specified label, for use in debugging.
printShow :: Show s
=> String -- ^ The label to be printed before the value of the input data.
-> s -- ^ Arbitrary polymorphic input-data.
-> s -- ^ Output (identical to input).
printShow label = System.IO.Unsafe.unsafePerformIO . f label where
f :: Show s => String -> s -> IO s
f s x = System.IO.hPutStrLn System.IO.stderr (s ++ show x) >> return {-to IO-monad-} x