<commit_before>%
% (c) The AQUA Project, Glasgow University, 1994-1996
%

\section[PrelUnsafe]{Module @PrelUnsafe@}

These functions have their own module because we definitely don't want
them to be inlined.

\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}

module PrelUnsafe
        ( unsafePerformIO, 
	  unsafeInterleaveIO, 
	  trace,
        ) where
\end{code}

\begin{code}
import PrelBase
import PrelIOBase
import PrelAddr
import {-# SOURCE #-} PrelErr ( error )
\end{code}

%*********************************************************
%*							*
\subsection{Unsafe @IO@ operations}
%*							*
%*********************************************************

\begin{code}
unsafePerformIO	:: IO a -> a
unsafePerformIO (IO m)
  = case m realWorld# of
      IOok _ r   -> r
      IOfail _ e -> error ("unsafePerformIO: I/O error: " ++ show e ++ "\n")

unsafeInterleaveIO :: IO a -> IO a
unsafeInterleaveIO (IO m) = IO ( \ s ->
	let
	    IOok _ r = m s
	in
	IOok s r)

{-# GENERATE_SPECS _trace a #-}
trace :: String -> a -> a
trace string expr
  = unsafePerformIO (
	((_ccall_ PreTraceHook sTDERR{-msg-}):: IO ())  >>
	fputs sTDERR string				>>
	((_ccall_ PostTraceHook sTDERR{-msg-}):: IO ()) >>
	return expr )
  where
    sTDERR = (``stderr'' :: Addr)
\end{code}
<commit_msg>[project @ 1998-05-16 20:01:18 by sof] unsafeInterleaveIO: handle I/O failure a little bit more gracefully<commit_after>%
% (c) The AQUA Project, Glasgow University, 1994-1996
%

\section[PrelUnsafe]{Module @PrelUnsafe@}

These functions have their own module because we definitely don't want
them to be inlined. The reason is that we may end up turning an action
into a constant when it is not:

  new :: IORef Int
  new = 
   let
    foo = unsafePerformIO getNextValue
   in
   newIORef foo 

If unsafePerformIO is inlined here, the application of getNextValue to the realWorld# 
token might be floated out, leaving us with

  foo' = getNextValue realWorld#

  new :: IORef Int
  new = newIORef foo'

which is not what we want.

\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}

module PrelUnsafe
        ( unsafePerformIO, 
	  unsafeInterleaveIO, 
	  trace,
        ) where
\end{code}

\begin{code}
import PrelBase
import PrelIOBase
import PrelAddr
import {-# SOURCE #-} PrelErr ( error )
\end{code}

%*********************************************************
%*							*
\subsection{Unsafe @IO@ operations}
%*							*
%*********************************************************

\begin{code}
unsafePerformIO	:: IO a -> a
unsafePerformIO (IO m)
  = case m realWorld# of
      IOok _ r   -> r
      IOfail _ e -> error ("unsafePerformIO: I/O error: " ++ show e ++ "\n")

unsafeInterleaveIO :: IO a -> IO a
unsafeInterleaveIO (IO m) = IO ( \ s ->
	let
         res =
	   case m s of
	     IOok _ r   -> r
	     IOfail _ e -> error ("unsafeInterleaveIO: I/O error: " ++ show e ++ "\n")
	in
	IOok s res
    )


trace :: String -> a -> a
trace string expr
  = unsafePerformIO (
	((_ccall_ PreTraceHook sTDERR{-msg-}):: IO ())  >>
	fputs sTDERR string				>>
	((_ccall_ PostTraceHook sTDERR{-msg-}):: IO ()) >>
	return expr )
  where
    sTDERR = (``stderr'' :: Addr)
\end{code}
{'fraction_non_alphanumeric': 0.1895238095238095, 'fraction_numerical': 0.009523809523809525, 'mean_word_length': 3.252361673414305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7628590', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1082, 'n_words': 331}