diff --git a/Blaze/ByteString/Builder.hs b/Blaze/ByteString/Builder.hs
--- a/Blaze/ByteString/Builder.hs
+++ b/Blaze/ByteString/Builder.hs
@@ -3,7 +3,7 @@
 -- Module      : Blaze.ByteString.Builder
 -- Copyright   : (c) 2010 Jasper Van der Jeugt & Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -12,7 +12,7 @@
 -- of the @blaze-builder@ library.
 --
 -- > import Blaze.ByteString.Builder
--- 
+--
 -- It provides you with a type 'Builder' that allows to efficiently construct
 -- lazy bytestrings with a large average chunk size.
 --
@@ -57,7 +57,7 @@
 -----------------------------------------------------------------------------
 
 module Blaze.ByteString.Builder
-    ( 
+    (
       -- * The 'Builder' type
       Builder
 
diff --git a/Blaze/ByteString/Builder/ByteString.hs b/Blaze/ByteString/Builder/ByteString.hs
--- a/Blaze/ByteString/Builder/ByteString.hs
+++ b/Blaze/ByteString/Builder/ByteString.hs
@@ -1,10 +1,16 @@
-{-# LANGUAGE CPP, BangPatterns, OverloadedStrings, MonoPatBinds #-}
+{-# LANGUAGE CPP, BangPatterns, OverloadedStrings #-}
 
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
+{-# OPTIONS_GHC -fno-warn-unused-imports #-}
+
 -- |
 -- Module      : Blaze.ByteString.Builder.ByteString
 -- Copyright   : (c) 2010 Jasper Van der Jeugt & Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -18,7 +24,7 @@
 -- > import qualified Data.ByteString.Lazy as L
 --
 module Blaze.ByteString.Builder.ByteString
-    ( 
+    (
     -- * Strict bytestrings
       writeByteString
     , fromByteString
@@ -37,7 +43,13 @@
 import           Blaze.ByteString.Builder.Internal hiding (insertByteString)
 import qualified Blaze.ByteString.Builder.Internal as I   (insertByteString)
 
-import Foreign
+#ifdef HAS_FOREIGN_UNSAFE_MODULE
+import Foreign                   (withForeignPtr, touchForeignPtr, copyBytes, plusPtr, minusPtr)
+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
+#else
+import Foreign                   (unsafeForeignPtrToPtr, withForeignPtr, touchForeignPtr, copyBytes, plusPtr, minusPtr)
+#endif
+
 import Data.Monoid
 
 import qualified Data.ByteString      as S
@@ -47,7 +59,7 @@
 import qualified Data.ByteString.Base as S
 import qualified Data.ByteString.Lazy.Base as L -- FIXME: check if this is the right module
 #else
-import qualified Data.ByteString.Internal as S
+import qualified Data.ByteString.Internal      as S
 import qualified Data.ByteString.Lazy.Internal as L
 #endif
 
@@ -76,7 +88,7 @@
 --
 -- If you statically know that copying or inserting the strict bytestring is
 -- always the best choice, then you can use the 'copyByteString' or
--- 'insertByteString' functions. 
+-- 'insertByteString' functions.
 --
 fromByteString :: S.ByteString -> Builder
 fromByteString = fromByteStringWith defaultMaximalCopySize
@@ -97,7 +109,7 @@
 fromByteStringWith :: Int          -- ^ Maximal number of bytes to copy.
                    -> S.ByteString -- ^ Strict 'S.ByteString' to serialize.
                    -> Builder      -- ^ Resulting 'Builder'.
-fromByteStringWith maxCopySize = 
+fromByteStringWith maxCopySize =
     \bs -> fromBuildStepCont $ step bs
   where
     step !bs !k br@(BufRange !op _)
@@ -106,7 +118,7 @@
 {-# INLINE fromByteStringWith #-}
 
 -- | @copyByteString bs@ serialize the strict bytestring @bs@ by copying it to
--- the output buffer. 
+-- the output buffer.
 --
 -- Use this function to serialize strict bytestrings that are statically known
 -- to be smallish (@<= 4kb@).
@@ -115,10 +127,10 @@
 copyByteString = \bs -> fromBuildStepCont $ copyByteStringStep bs
 {-# INLINE copyByteString #-}
 
-copyByteStringStep :: S.ByteString 
+copyByteStringStep :: S.ByteString
                    -> (BufRange -> IO (BuildSignal a))
                    -> (BufRange -> IO (BuildSignal a))
-copyByteStringStep (S.PS ifp ioff isize) !k = 
+copyByteStringStep (S.PS ifp ioff isize) !k =
     goBS (unsafeForeignPtrToPtr ifp `plusPtr` ioff)
   where
     !ipe = unsafeForeignPtrToPtr ifp `plusPtr` (ioff + isize)
@@ -138,7 +150,7 @@
 {-# INLINE copyByteStringStep #-}
 
 -- | @insertByteString bs@ serializes the strict bytestring @bs@ by inserting
--- it directly as a chunk of the output stream. 
+-- it directly as a chunk of the output stream.
 --
 -- Note that this implies flushing the output buffer; even if it contains just
 -- a single byte. Hence, you should use this operation only for large (@> 8kb@)
@@ -146,7 +158,7 @@
 -- to be processed efficiently.
 --
 insertByteString :: S.ByteString -> Builder
-insertByteString = 
+insertByteString =
     \bs -> fromBuildStepCont $ step bs
   where
     step !bs !k !(BufRange op _) = return $ I.insertByteString op bs k
@@ -167,7 +179,7 @@
 --
 -- If you statically know that copying or inserting /all/ chunks of the lazy
 -- bytestring is always the best choice, then you can use the
--- 'copyLazyByteString' or 'insertLazyByteString' functions. 
+-- 'copyLazyByteString' or 'insertLazyByteString' functions.
 --
 fromLazyByteString :: L.ByteString -> Builder
 fromLazyByteString = fromLazyByteStringWith defaultMaximalCopySize
@@ -188,7 +200,7 @@
 fromLazyByteStringWith :: Int          -- ^ Maximal number of bytes to copy.
                        -> L.ByteString -- ^ Lazy 'L.ByteString' to serialize.
                        -> Builder      -- ^ Resulting 'Builder'.
-fromLazyByteStringWith maxCopySize = 
+fromLazyByteStringWith maxCopySize =
   L.foldrChunks (\bs b -> fromByteStringWith maxCopySize bs `mappend` b) mempty
 {-# INLINE fromLazyByteStringWith #-}
 
@@ -199,7 +211,7 @@
 -- See 'copyByteString' for usage considerations.
 --
 copyLazyByteString :: L.ByteString -> Builder
-copyLazyByteString = 
+copyLazyByteString =
   L.foldrChunks (\bs b -> copyByteString bs `mappend` b) mempty
 {-# INLINE copyLazyByteString #-}
 
diff --git a/Blaze/ByteString/Builder/Char/Utf8.hs b/Blaze/ByteString/Builder/Char/Utf8.hs
--- a/Blaze/ByteString/Builder/Char/Utf8.hs
+++ b/Blaze/ByteString/Builder/Char/Utf8.hs
@@ -1,20 +1,20 @@
-{-# OPTIONS_GHC -fno-warn-unused-imports #-} 
+{-# OPTIONS_GHC -fno-warn-unused-imports #-}
 -- ignore warning from 'import Data.Text.Encoding'
 
 -- |
 -- Module      : Blaze.ByteString.Builder.Char.Utf8
 -- Copyright   : (c) 2010 Jasper Van der Jeugt & Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
 --
 -- 'Write's and 'Builder's for serializing Unicode characters using the UTF-8
--- encoding. 
+-- encoding.
 --
 module Blaze.ByteString.Builder.Char.Utf8
-    ( 
+    (
       -- * Writing UTF-8 encoded characters to a buffer
       writeChar
 
@@ -46,7 +46,7 @@
 
     f2 x1 x2       = pokeN 2 $ \op -> do pokeByteOff op 0 x1
                                          pokeByteOff op 1 x2
-                   
+
     f3 x1 x2 x3    = pokeN 3 $ \op -> do pokeByteOff op 0 x1
                                          pokeByteOff op 1 x2
                                          pokeByteOff op 2 x3
diff --git a/Blaze/ByteString/Builder/Char8.hs b/Blaze/ByteString/Builder/Char8.hs
--- a/Blaze/ByteString/Builder/Char8.hs
+++ b/Blaze/ByteString/Builder/Char8.hs
@@ -1,18 +1,23 @@
-{-# OPTIONS_GHC -fno-warn-unused-imports #-} 
-{-# LANGUAGE MonoPatBinds #-}
 -- ignore warning from 'import Data.Text.Encoding'
+{-# OPTIONS_GHC -fno-warn-unused-imports #-}
 
+{-# LANGUAGE CPP #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- |
 -- Module      : Blaze.ByteString.Builder.Char8
 -- Copyright   : (c) 2010 Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
 --
 -- //Note:// This package is intended for low-level use like implementing
--- protocols. If you need to //serialize// Unicode characters use one of the 
+-- protocols. If you need to //serialize// Unicode characters use one of the
 -- UTF encodings (e.g. 'Blaze.ByteString.Builder.Char.UTF-8').
 --
 -- 'Write's and 'Builder's for serializing the lower 8-bits of characters.
@@ -21,7 +26,7 @@
 -- 'Data.ByteString.Char8'.
 --
 module Blaze.ByteString.Builder.Char8
-    ( 
+    (
       -- * Writing Latin-1 (ISO 8859-1) encodable characters to a buffer
       writeChar
 
diff --git a/Blaze/ByteString/Builder/HTTP.hs b/Blaze/ByteString/Builder/HTTP.hs
--- a/Blaze/ByteString/Builder/HTTP.hs
+++ b/Blaze/ByteString/Builder/HTTP.hs
@@ -1,4 +1,9 @@
-{-# LANGUAGE BangPatterns, CPP, MagicHash, OverloadedStrings, MonoPatBinds #-}
+{-# LANGUAGE BangPatterns, CPP, MagicHash, OverloadedStrings #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- | Support for HTTP response encoding.
 --
 -- TODO: Improve documentation.
@@ -67,7 +72,7 @@
 -}
 
 pokeWord32HexN :: Int -> Word32 -> Ptr Word8 -> IO ()
-pokeWord32HexN n0 w0 op0 = 
+pokeWord32HexN n0 w0 op0 =
     go w0 (op0 `plusPtr` (n0 - 1))
   where
     go !w !op
@@ -94,7 +99,7 @@
 {-# INLINE word32HexLength #-}
 
 writeWord32Hex :: Word32 -> Write
-writeWord32Hex w = 
+writeWord32Hex w =
     boundedWrite (2 * sizeOf w) (pokeN len $ pokeWord32HexN len w)
   where
     len = word32HexLength w
@@ -108,7 +113,7 @@
                 map oneLine [0,1] ++
                 [B.insertByteString ""] ++
                 map oneLine [0..16]
-               
+
   where
     oneLine x = fromWriteSingleton writeWord32Hex x `mappend` Char8.fromChar ' '
 
@@ -133,22 +138,22 @@
       where
         go innerStep !(BufRange op ope)
           -- FIXME: Assert that outRemaining < maxBound :: Word32
-          | outRemaining < minimalBufferSize = 
+          | outRemaining < minimalBufferSize =
               return $ bufferFull minimalBufferSize op (go innerStep)
           | otherwise = do
-              let !brInner@(BufRange opInner _) = BufRange 
+              let !brInner@(BufRange opInner _) = BufRange
                      (op  `plusPtr` (chunkSizeLength + 2))     -- leave space for chunk header
                      (ope `plusPtr` (-maxAfterBufferOverhead)) -- leave space at end of data
 
                   -- wraps the chunk, if it is non-empty, and returns the
                   -- signal constructed with the correct end-of-data pointer
                   {-# INLINE wrapChunk #-}
-                  wrapChunk :: Ptr Word8 -> (Ptr Word8 -> IO (BuildSignal a)) 
+                  wrapChunk :: Ptr Word8 -> (Ptr Word8 -> IO (BuildSignal a))
                             -> IO (BuildSignal a)
-                  wrapChunk !opInner' mkSignal 
+                  wrapChunk !opInner' mkSignal
                     | opInner' == opInner = mkSignal op
                     | otherwise           = do
-                        pokeWord32HexN chunkSizeLength 
+                        pokeWord32HexN chunkSizeLength
                             (fromIntegral $ opInner' `minusPtr` opInner)
                             op
                         execWrite writeCRLF (opInner `plusPtr` (-2))
@@ -165,16 +170,16 @@
 
                 BufferFull minRequiredSize opInner' nextInnerStep ->
                     wrapChunk opInner' $ \op' ->
-                      return $! bufferFull 
-                        (minRequiredSize + maxEncodingOverhead) 
+                      return $! bufferFull
+                        (minRequiredSize + maxEncodingOverhead)
                         op'
-                        (go nextInnerStep)  
+                        (go nextInnerStep)
 
-                InsertByteString opInner' bs nextInnerStep 
+                InsertByteString opInner' bs nextInnerStep
                   | S.null bs ->                        -- flush
                       wrapChunk opInner' $ \op' ->
-                        return $! insertByteString 
-                          op' S.empty 
+                        return $! insertByteString
+                          op' S.empty
                           (go nextInnerStep)
 
                   | otherwise ->                        -- insert non-empty bytestring
@@ -182,21 +187,21 @@
                         -- add header for inserted bytestring
                         -- FIXME: assert(S.length bs < maxBound :: Word32)
                         !op'' <- (`runPoke` op') $ getPoke $
-                            writeWord32Hex (fromIntegral $ S.length bs) 
+                            writeWord32Hex (fromIntegral $ S.length bs)
                             `mappend` writeCRLF
                         -- insert bytestring and write CRLF in next buildstep
                         return $! InsertByteString
                           op'' bs
-                          (unBuilder (fromWrite writeCRLF) $ 
+                          (unBuilder (fromWrite writeCRLF) $
                             buildStep $ go nextInnerStep)
-                  
+
           where
             -- minimal size guaranteed for actual data no need to require more
             -- than 1 byte to guarantee progress the larger sizes will be
             -- hopefully provided by the driver or requested by the wrapped
             -- builders.
-            minimalChunkSize  = 1   
-                                    
+            minimalChunkSize  = 1
+
             -- overhead computation
             maxBeforeBufferOverhead = sizeOf (undefined :: Int) + 2 -- max chunk size and CRLF after header
             maxAfterBufferOverhead  = 2 +                           -- CRLF after data
@@ -210,7 +215,7 @@
             outRemaining :: Int
             outRemaining    = ope `minusPtr` op
             chunkSizeLength = word32HexLength $ fromIntegral outRemaining
-      
+
 
 -- | The zero-length chunk '0\r\n\r\n' signaling the termination of the data transfer.
 chunkedTransferTerminator :: Builder
diff --git a/Blaze/ByteString/Builder/Html/Utf8.hs b/Blaze/ByteString/Builder/Html/Utf8.hs
--- a/Blaze/ByteString/Builder/Html/Utf8.hs
+++ b/Blaze/ByteString/Builder/Html/Utf8.hs
@@ -3,7 +3,7 @@
 -- Module      : Blaze.ByteString.Builder.Html.Utf8
 -- Copyright   : (c) 2010 Jasper Van der Jeugt & Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -16,7 +16,7 @@
 -- 'Data.Binary.Builder' implementation, this module will most likely keep its
 -- place, as it provides a set of very specialized functions.
 module Blaze.ByteString.Builder.Html.Utf8
-    ( 
+    (
       module Blaze.ByteString.Builder.Char.Utf8
 
       -- * Writing HTML escaped and UTF-8 encoded characters to a buffer
@@ -42,7 +42,7 @@
 -- | Write a HTML escaped and UTF-8 encoded Unicode character to a bufffer.
 --
 writeHtmlEscapedChar :: Char -> Write
-writeHtmlEscapedChar c0 = 
+writeHtmlEscapedChar c0 =
     boundedWrite 6 (io c0)
     -- WARNING: Don't forget to change the bound if you change the bytestrings.
   where
diff --git a/Blaze/ByteString/Builder/Int.hs b/Blaze/ByteString/Builder/Int.hs
--- a/Blaze/ByteString/Builder/Int.hs
+++ b/Blaze/ByteString/Builder/Int.hs
@@ -1,10 +1,15 @@
+{-# LANGUAGE CPP #-}
+
+#ifdef USE_MONO_PAT_BINDS
 {-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- |
 -- Module      : Blaze.ByteString.Builder.Int
 -- Copyright   : (c) 2010 Simon Meier
 --
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -15,7 +20,7 @@
 -- integers at once.
 --
 module Blaze.ByteString.Builder.Int
-    ( 
+    (
     -- * Writing integers to a buffer
 
       writeInt8
@@ -37,7 +42,7 @@
     , writeInt64host         -- :: Int64 -> Write
 
     -- * Creating builders from integers
-    
+
     -- | We provide serialization functions both for singleton integers as well as
     -- for lists of integers. Using these list serialization functions is /much/ faster
     -- than using @mconcat . map fromInt/<n/>@, as the list serialization
@@ -85,7 +90,7 @@
 --
 -- we rely on 'fromIntegral' to do a loss-less conversion to the corresponding
 -- 'Word' type
--- 
+--
 ------------------------------------------------------------------------------
 
 
@@ -178,22 +183,22 @@
 
 -- | Serialize an 'Int16' in big endian format.
 fromInt16be :: Int16 -> Builder
-fromInt16be = fromWriteSingleton writeInt16be 
+fromInt16be = fromWriteSingleton writeInt16be
 {-# INLINE fromInt16be #-}
 
 -- | Serialize a list of 'Int16's in big endian format.
 fromInt16sbe :: [Int16] -> Builder
-fromInt16sbe = fromWriteList writeInt16be 
+fromInt16sbe = fromWriteList writeInt16be
 {-# INLINE fromInt16sbe #-}
 
 -- | Serialize an 'Int16' in little endian format.
 fromInt16le :: Int16 -> Builder
-fromInt16le = fromWriteSingleton writeInt16le 
+fromInt16le = fromWriteSingleton writeInt16le
 {-# INLINE fromInt16le #-}
 
 -- | Serialize a list of 'Int16's in little endian format.
 fromInt16sle :: [Int16] -> Builder
-fromInt16sle = fromWriteList writeInt16le 
+fromInt16sle = fromWriteList writeInt16le
 {-# INLINE fromInt16sle #-}
 
 
@@ -202,42 +207,42 @@
 
 -- | Serialize an 'Int32' in big endian format.
 fromInt32be :: Int32 -> Builder
-fromInt32be = fromWriteSingleton writeInt32be 
+fromInt32be = fromWriteSingleton writeInt32be
 {-# INLINE fromInt32be #-}
 
 -- | Serialize a list of 'Int32's in big endian format.
 fromInt32sbe :: [Int32] -> Builder
-fromInt32sbe = fromWriteList writeInt32be 
+fromInt32sbe = fromWriteList writeInt32be
 {-# INLINE fromInt32sbe #-}
 
 -- | Serialize an 'Int32' in little endian format.
 fromInt32le :: Int32 -> Builder
-fromInt32le = fromWriteSingleton writeInt32le 
+fromInt32le = fromWriteSingleton writeInt32le
 {-# INLINE fromInt32le #-}
 
 -- | Serialize a list of 'Int32's in little endian format.
 fromInt32sle :: [Int32] -> Builder
-fromInt32sle = fromWriteList writeInt32le 
+fromInt32sle = fromWriteList writeInt32le
 {-# INLINE fromInt32sle #-}
 
 -- | Serialize an 'Int64' in big endian format.
 fromInt64be :: Int64 -> Builder
-fromInt64be = fromWriteSingleton writeInt64be 
+fromInt64be = fromWriteSingleton writeInt64be
 {-# INLINE fromInt64be #-}
 
 -- | Serialize a list of 'Int64's in big endian format.
 fromInt64sbe :: [Int64] -> Builder
-fromInt64sbe = fromWriteList writeInt64be 
+fromInt64sbe = fromWriteList writeInt64be
 {-# INLINE fromInt64sbe #-}
 
 -- | Serialize an 'Int64' in little endian format.
 fromInt64le :: Int64 -> Builder
-fromInt64le = fromWriteSingleton writeInt64le 
+fromInt64le = fromWriteSingleton writeInt64le
 {-# INLINE fromInt64le #-}
 
 -- | Serialize a list of 'Int64's in little endian format.
 fromInt64sle :: [Int64] -> Builder
-fromInt64sle = fromWriteList writeInt64le 
+fromInt64sle = fromWriteList writeInt64le
 {-# INLINE fromInt64sle #-}
 
 
@@ -251,33 +256,33 @@
 -- conversion.
 --
 fromInthost :: Int -> Builder
-fromInthost = fromWriteSingleton writeInthost 
+fromInthost = fromWriteSingleton writeInthost
 {-# INLINE fromInthost #-}
 
 -- | Serialize a list of 'Int's.
 -- See 'fromInthost' for usage considerations.
 fromIntshost :: [Int] -> Builder
-fromIntshost = fromWriteList writeInthost 
+fromIntshost = fromWriteList writeInthost
 {-# INLINE fromIntshost #-}
 
 -- | Write an 'Int16' in native host order and host endianness.
 fromInt16host :: Int16 -> Builder
-fromInt16host = fromWriteSingleton writeInt16host 
+fromInt16host = fromWriteSingleton writeInt16host
 {-# INLINE fromInt16host #-}
 
 -- | Write a list of 'Int16's in native host order and host endianness.
 fromInt16shost :: [Int16] -> Builder
-fromInt16shost = fromWriteList writeInt16host 
+fromInt16shost = fromWriteList writeInt16host
 {-# INLINE fromInt16shost #-}
 
 -- | Write an 'Int32' in native host order and host endianness.
 fromInt32host :: Int32 -> Builder
-fromInt32host = fromWriteSingleton writeInt32host 
+fromInt32host = fromWriteSingleton writeInt32host
 {-# INLINE fromInt32host #-}
 
 -- | Write a list of 'Int32's in native host order and host endianness.
 fromInt32shost :: [Int32] -> Builder
-fromInt32shost = fromWriteList writeInt32host 
+fromInt32shost = fromWriteList writeInt32host
 {-# INLINE fromInt32shost #-}
 
 -- | Write an 'Int64' in native host order and host endianness.
diff --git a/Blaze/ByteString/Builder/Internal.hs b/Blaze/ByteString/Builder/Internal.hs
--- a/Blaze/ByteString/Builder/Internal.hs
+++ b/Blaze/ByteString/Builder/Internal.hs
@@ -1,9 +1,14 @@
-{-# LANGUAGE CPP, BangPatterns, Rank2Types, MonoPatBinds #-}
+{-# LANGUAGE CPP, BangPatterns, Rank2Types #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- |
 -- Module      : Blaze.ByteString.Builder.Internal
 -- Copyright   : (c) 2010 Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -46,10 +51,15 @@
   , defaultFirstBufferSize
   , defaultMinimalBufferSize
   , defaultBufferSize
-  , defaultMaximalCopySize 
+  , defaultMaximalCopySize
 ) where
 
-import Foreign
+#ifdef HAS_FOREIGN_UNSAFE_MODULE
+import Foreign                   (withForeignPtr, sizeOf, copyBytes, plusPtr, minusPtr)
+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
+#else
+import Foreign                   (unsafeForeignPtrToPtr, withForeignPtr, sizeOf, copyBytes, plusPtr, minusPtr)
+#endif
 
 import Control.Monad (unless)
 
@@ -73,7 +83,7 @@
     where overhead = 2 * sizeOf (undefined :: Int)
 
 -- | The minimal length (~4kb) a buffer must have before filling it and
--- outputting it as a chunk of the output stream. 
+-- outputting it as a chunk of the output stream.
 --
 -- This size determines when a buffer is spilled after a 'flush' or a direct
 -- bytestring insertion. It is also the size of the first chunk generated by
@@ -83,7 +93,7 @@
     where overhead = 2 * sizeOf (undefined :: Int)
 
 -- | The default length (64) for the first buffer to be allocated when
--- converting a 'Builder' to a lazy bytestring. 
+-- converting a 'Builder' to a lazy bytestring.
 --
 -- See 'toLazyByteStringWith' for further explanation.
 defaultFirstBufferSize :: Int
@@ -102,11 +112,11 @@
 ------------------------------------------------------------------------------
 -- Flushing and running a Builder
 ------------------------------------------------------------------------------
-                    
+
 -- | Prepend the chunk if it is non-empty.
 {-# INLINE nonEmptyChunk #-}
 nonEmptyChunk :: S.ByteString -> L.ByteString -> L.ByteString
-nonEmptyChunk bs lbs | S.null bs = lbs 
+nonEmptyChunk bs lbs | S.null bs = lbs
                      | otherwise = L.Chunk bs lbs
 
 
@@ -154,7 +164,7 @@
 -- @firstBufSize = bufSize@ means that all chunks will use an underlying buffer
 -- of size @bufSize@. This is recommended, if you know that you always output
 -- more than @minBufSize@ bytes.
-toLazyByteStringWith 
+toLazyByteStringWith
     :: Int           -- ^ Buffer size (upper-bounds the resulting chunk size).
     -> Int           -- ^ Minimal free buffer space for continuing filling
                      -- the same buffer after a 'flush' or a direct bytestring
@@ -166,7 +176,7 @@
     -> L.ByteString  -- ^ Lazy bytestring to output after the builder is
                      -- finished.
     -> L.ByteString  -- ^ Resulting lazy bytestring
-toLazyByteStringWith bufSize minBufSize firstBufSize (Builder b) k = 
+toLazyByteStringWith bufSize minBufSize firstBufSize (Builder b) k =
     S.inlinePerformIO $ fillFirstBuffer (b (buildStep finalStep))
   where
     finalStep (BufRange pf _) = return $ Done pf ()
@@ -195,14 +205,14 @@
                               copyBytes pfNew pf l
                               let !br' = BufRange (pfNew `plusPtr` l) peNew
                               runBuildStep nextStep br'
-                      
-                  InsertByteString pf' bs nextStep 
+
+                  InsertByteString pf' bs nextStep
                       | pf' == pf ->
                           return $ nonEmptyChunk bs (S.inlinePerformIO $ fillNewBuffer bufSize nextStep)
                       | otherwise ->
                           return $ L.Chunk (mkbs pf')
                               (nonEmptyChunk bs (S.inlinePerformIO $ fillNewBuffer bufSize nextStep))
-                    
+
     -- allocate and fill a new buffer
     fillNewBuffer !size !step0 = do
         fpbuf <- S.mallocByteString size
@@ -220,14 +230,14 @@
                       | pf' == pf -> return k
                       | otherwise -> return $ L.Chunk (mkbs pf') k
 
-                    BufferFull newSize pf' nextStep 
-                      | pf' == pf -> 
+                    BufferFull newSize pf' nextStep
+                      | pf' == pf ->
                           fillNewBuffer (max newSize bufSize) nextStep
-                      | otherwise -> 
+                      | otherwise ->
                           return $ L.Chunk (mkbs pf')
-                              (S.inlinePerformIO $ 
+                              (S.inlinePerformIO $
                                   fillNewBuffer (max newSize bufSize) nextStep)
-                        
+
                     InsertByteString  pf' bs nextStep
                       | pf' == pf                      ->
                           return $ nonEmptyChunk bs (S.inlinePerformIO $ fill pf' nextStep)
@@ -254,7 +264,7 @@
 -- execute.
 --
 toLazyByteString :: Builder -> L.ByteString
-toLazyByteString b = toLazyByteStringWith 
+toLazyByteString b = toLazyByteStringWith
     defaultBufferSize defaultMinimalBufferSize defaultFirstBufferSize b L.empty
 {-# INLINE toLazyByteString #-}
 
@@ -307,7 +317,7 @@
                                                -- 'S.ByteString'.
                    -> Builder                  -- ^ 'Builder' to run.
                    -> IO ()                    -- ^ Resulting 'IO' action.
-toByteStringIOWith bufSize io (Builder b) = 
+toByteStringIOWith bufSize io (Builder b) =
     fillBuffer bufSize (b (buildStep finalStep))
   where
     finalStep !(BufRange pf _) = return $ Done pf ()
@@ -327,7 +337,7 @@
                 BufferFull minSize pf' nextStep  -> do
                     io $ S.PS fpbuf 0 (pf' `minusPtr` pf)
                     fillBuffer (max bufSize minSize) nextStep
-                    
+
                 InsertByteString pf' bs nextStep  -> do
                     io $ S.PS fpbuf 0 (pf' `minusPtr` pf)
                     unless (S.null bs) (io bs)
@@ -354,7 +364,7 @@
 
 {- FIXME: Generalize this code such that it can replace the above clunky
  - implementations.
-              
+
 -- | A monad for lazily composing lazy bytestrings using continuations.
 newtype LBSM a = LBSM { unLBSM :: (a, L.ByteString -> L.ByteString) }
 
@@ -366,7 +376,7 @@
 -- | Execute a put and return the written buffers as the chunks of a lazy
 -- bytestring.
 toLazyByteString :: Put a -> (a, L.ByteString)
-toLazyByteString put = 
+toLazyByteString put =
     (fst result, k (bufToLBSCont (snd result) L.empty))
   where
 
@@ -392,14 +402,14 @@
 
 {-
 -- | A Builder that traces a message
-traceBuilder :: String -> Builder 
+traceBuilder :: String -> Builder
 traceBuilder msg = fromBuildStepCont $ \k br@(BufRange op ope) -> do
     putStrLn $ "traceBuilder " ++ show (op, ope) ++ ": " ++ msg
     k br
 
 test2 :: Word8 -> [S.ByteString]
 test2 x = L.toChunks $ toLazyByteString2 $ fromBuilder $ mconcat
-  [ traceBuilder "before flush" 
+  [ traceBuilder "before flush"
   , fromWord8 48
   , flushBuilder
   , flushBuilder
diff --git a/Blaze/ByteString/Builder/Internal/Buffer.hs b/Blaze/ByteString/Builder/Internal/Buffer.hs
--- a/Blaze/ByteString/Builder/Internal/Buffer.hs
+++ b/Blaze/ByteString/Builder/Internal/Buffer.hs
@@ -1,9 +1,14 @@
-{-# LANGUAGE CPP, BangPatterns, Rank2Types, MonoPatBinds #-}
+{-# LANGUAGE CPP, BangPatterns, Rank2Types #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- |
 -- Module      : Blaze.ByteString.Builder.Internal.Buffer
 -- Copyright   : (c) 2010 Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -16,32 +21,36 @@
     Buffer
 
   -- ** Status information
-  , freeSize 
-  , sliceSize 
-  , bufferSize 
+  , freeSize
+  , sliceSize
+  , bufferSize
 
   -- ** Creation and modification
-  , allocBuffer 
-  , reuseBuffer 
-  , nextSlice 
+  , allocBuffer
+  , reuseBuffer
+  , nextSlice
   , updateEndOfSlice
   , execBuildStep
 
   -- ** Conversion to bytestings
-  , unsafeFreezeBuffer 
-  , unsafeFreezeNonEmptyBuffer 
+  , unsafeFreezeBuffer
+  , unsafeFreezeNonEmptyBuffer
 
   -- * Buffer allocation strategies
   , BufferAllocStrategy
-  , allNewBuffersStrategy 
-  , reuseBufferStrategy 
+  , allNewBuffersStrategy
+  , reuseBufferStrategy
 
   -- * Executing puts respect to some monad
   , runPut
   ) where
 
-import Prelude
-import Foreign
+#ifdef HAS_FOREIGN_UNSAFE_MODULE
+import Foreign                   (Word8, ForeignPtr, Ptr, plusPtr, minusPtr)
+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
+#else
+import Foreign                   (unsafeForeignPtrToPtr, Word8, ForeignPtr, Ptr, plusPtr, minusPtr)
+#endif
 
 import qualified Data.ByteString      as S
 
@@ -74,7 +83,7 @@
 
 -- | The size of the whole byte array underlying the buffer.
 bufferSize :: Buffer -> Int
-bufferSize (Buffer fpbuf _ _ ope) = 
+bufferSize (Buffer fpbuf _ _ ope) =
     ope `minusPtr` unsafeForeignPtrToPtr fpbuf
 
 -- | @allocBuffer size@ allocates a new buffer of size @size@.
@@ -99,7 +108,7 @@
 -- filled again) referential transparency may be lost.
 {-# INLINE unsafeFreezeBuffer #-}
 unsafeFreezeBuffer :: Buffer -> S.ByteString
-unsafeFreezeBuffer (Buffer fpbuf p0 op _) = 
+unsafeFreezeBuffer (Buffer fpbuf p0 op _) =
     S.PS fpbuf (p0 `minusPtr` unsafeForeignPtrToPtr fpbuf) (op `minusPtr` p0)
 
 -- | Convert a buffer to a non-empty bytestring. See 'unsafeFreezeBuffer' for
@@ -113,14 +122,14 @@
 -- | Update the end of slice pointer.
 {-# INLINE updateEndOfSlice #-}
 updateEndOfSlice :: Buffer    -- Old buffer
-                 -> Ptr Word8 -- New end of slice  
+                 -> Ptr Word8 -- New end of slice
                  -> Buffer    -- Updated buffer
 updateEndOfSlice (Buffer fpbuf p0 _ ope) op' = Buffer fpbuf p0 op' ope
 
 -- | Execute a build step on the given buffer.
 {-# INLINE execBuildStep #-}
 execBuildStep :: BuildStep a
-              -> Buffer  
+              -> Buffer
               -> IO (BuildSignal a)
 execBuildStep step (Buffer _ _ op ope) = runBuildStep step (BufRange op ope)
 
@@ -142,10 +151,10 @@
 -- buffer to use and how to compute a new buffer @nextBuf minSize buf@ with at
 -- least size @minSize@ from a filled buffer @buf@. The double nesting of the
 -- @IO@ monad helps to ensure that the reference to the filled buffer @buf@ is
--- lost as soon as possible, but the new buffer doesn't have to be allocated 
+-- lost as soon as possible, but the new buffer doesn't have to be allocated
 -- too early.
 type BufferAllocStrategy = (IO Buffer, Int -> Buffer -> IO (IO Buffer))
-  
+
 -- | The simplest buffer allocation strategy: whenever a buffer is requested,
 -- allocate a new one that is big enough for the next build step to execute.
 --
@@ -155,13 +164,13 @@
 -- special circumstances.
 allNewBuffersStrategy :: Int                 -- Minimal buffer size.
                       -> BufferAllocStrategy
-allNewBuffersStrategy bufSize = 
+allNewBuffersStrategy bufSize =
     ( allocBuffer bufSize
     , \reqSize _ -> return (allocBuffer (max reqSize bufSize)) )
 
 -- | An unsafe, but possibly more efficient buffer allocation strategy:
 -- reuse the buffer, if it is big enough for the next build step to execute.
-reuseBufferStrategy :: IO Buffer          
+reuseBufferStrategy :: IO Buffer
                     -> BufferAllocStrategy
 reuseBufferStrategy buf0 =
     (buf0, tryReuseBuffer)
@@ -179,7 +188,7 @@
 --
 -- TODO: Generalize over buffer allocation strategy.
 {-# INLINE runPut #-}
-runPut :: Monad m 
+runPut :: Monad m
        => (IO (BuildSignal a) -> m (BuildSignal a)) -- lifting of buildsteps
        -> (Int -> Buffer -> m Buffer) -- output function for a guaranteedly non-empty buffer, the returned buffer will be filled next
        -> (S.ByteString -> m ())    -- output function for guaranteedly non-empty bytestrings, that are inserted directly into the stream
@@ -194,7 +203,7 @@
     runStep step buf@(Buffer fpbuf p0 op ope) = do
         let !br = BufRange op ope
         signal <- liftIO $ runBuildStep step br
-        case signal of 
+        case signal of
             Done op' x ->         -- put completed, buffer partially runSteped
                 return (x, Buffer fpbuf p0 op' ope)
 
diff --git a/Blaze/ByteString/Builder/Internal/Types.hs b/Blaze/ByteString/Builder/Internal/Types.hs
--- a/Blaze/ByteString/Builder/Internal/Types.hs
+++ b/Blaze/ByteString/Builder/Internal/Types.hs
@@ -1,9 +1,14 @@
-{-# LANGUAGE CPP, BangPatterns, Rank2Types, MonoPatBinds #-}
+{-# LANGUAGE CPP, BangPatterns, Rank2Types #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- |
 -- Module      : Blaze.ByteString.Builder.Internal.Types
 -- Copyright   : (c) 2010 Simon Meier
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -36,11 +41,11 @@
       {-# UNPACK #-} !(Ptr Word8)
                      !(BuildStep a)
   | InsertByteString
-      {-# UNPACK #-} !(Ptr Word8) 
+      {-# UNPACK #-} !(Ptr Word8)
                      !S.ByteString
                      !(BuildStep a)
 
-newtype BuildStep a =  
+newtype BuildStep a =
     BuildStep { runBuildStep :: BufRange -> IO (BuildSignal a) }
 
 -- Hiding the implementation of 'BuildStep's
@@ -61,8 +66,8 @@
 -- The 'Builder' Monoid and the 'Put' Monad
 ------------------------------------------------------------------------------
 
-newtype Builder = Builder { 
-    unBuilder :: forall r. BuildStep r -> BuildStep r 
+newtype Builder = Builder {
+    unBuilder :: forall r. BuildStep r -> BuildStep r
   }
 
 instance Monoid Builder where
@@ -105,7 +110,7 @@
 -- Creation from concrete 'BuildStep's
 ------------------------------------------------------------------------------
 
-putBuildStepCont :: (forall r. (a -> BufRange -> IO (BuildSignal r)) -> 
+putBuildStepCont :: (forall r. (a -> BufRange -> IO (BuildSignal r)) ->
                                (     BufRange -> IO (BuildSignal r))
                     ) -> Put a
 putBuildStepCont step = Put step'
@@ -113,7 +118,7 @@
     step' k = BuildStep $ step (\x -> runBuildStep (k x))
 
 
-fromBuildStepCont :: (forall r. (BufRange -> IO (BuildSignal r)) -> 
+fromBuildStepCont :: (forall r. (BufRange -> IO (BuildSignal r)) ->
                                 (BufRange -> IO (BuildSignal r))
                      ) -> Builder
 fromBuildStepCont step = Builder step'
diff --git a/Blaze/ByteString/Builder/Internal/UncheckedShifts.hs b/Blaze/ByteString/Builder/Internal/UncheckedShifts.hs
--- a/Blaze/ByteString/Builder/Internal/UncheckedShifts.hs
+++ b/Blaze/ByteString/Builder/Internal/UncheckedShifts.hs
@@ -1,4 +1,9 @@
-{-# LANGUAGE CPP, MagicHash, MonoPatBinds #-}
+{-# LANGUAGE CPP, MagicHash #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- |
 -- Module      : Blaze.ByteString.Builder.Internal.UncheckedShifts
 -- Copyright   : (c) 2010 Simon Meier
@@ -7,7 +12,7 @@
 --               (c) Lennart Kolmodin, Ross Patterson
 --
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
diff --git a/Blaze/ByteString/Builder/Internal/Write.hs b/Blaze/ByteString/Builder/Internal/Write.hs
--- a/Blaze/ByteString/Builder/Internal/Write.hs
+++ b/Blaze/ByteString/Builder/Internal/Write.hs
@@ -1,10 +1,14 @@
-{-# LANGUAGE CPP, BangPatterns, MonoPatBinds #-}
+{-# LANGUAGE CPP, BangPatterns #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
 -- |
 -- Module      : Blaze.ByteString.Builder.Internal.Poke
 -- Copyright   : (c) 2010 Simon Meier
 --               (c) 2010 Jasper van der Jeugt
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -73,15 +77,15 @@
 -- | Changing a sequence of bytes starting from the given pointer. 'Poke's are
 -- the most primitive buffer manipulation. In most cases, you don't use the
 -- explicitely but as part of a 'Write', which also tells how many bytes will
--- be changed at most. 
-newtype Poke = 
+-- be changed at most.
+newtype Poke =
     Poke { runPoke :: Ptr Word8 -> IO (Ptr Word8) }
 
 -- | A write of a bounded number of bytes.
 --
 -- When defining a function @write :: a -> Write@ for some @a@, then it is
 -- important to ensure that the bound on the number of bytes written is
--- data-independent. Formally, 
+-- data-independent. Formally,
 --
 --  @ forall x y. getBound (write x) = getBound (write y) @
 --
@@ -112,10 +116,10 @@
 -- case. Assumes that the bound of the write is data-independent.
 {-# INLINE getBound' #-}
 getBound' :: String             -- ^ Name of caller: for debugging purposes.
-          -> (a -> Write) 
+          -> (a -> Write)
           -> Int
 getBound' msg write =
-    getBound $ write $ error $ 
+    getBound $ write $ error $
     "getBound' called from " ++ msg ++ ": write bound is not data-independent."
 
 instance Monoid Poke where
@@ -135,7 +139,7 @@
   {-# INLINE mappend #-}
   (Write bound1 w1) `mappend` (Write bound2 w2) =
     Write (bound1 + bound2) (w1 `mappend` w2)
-  
+
   {-# INLINE mconcat #-}
   mconcat = foldr mappend mempty
 
@@ -144,7 +148,7 @@
 -- to a buffer using the IO action @io@. Note that @io@ MUST write EXACTLY @size@
 -- bytes to the buffer!
 {-# INLINE pokeN #-}
-pokeN :: Int 
+pokeN :: Int
        -> (Ptr Word8 -> IO ()) -> Poke
 pokeN size io = Poke $ \op -> io op >> return (op `plusPtr` size)
 
@@ -153,8 +157,8 @@
 -- a builder that writes exactly @size@ bytes. Note that @io@ MUST write
 -- EXACTLY @size@ bytes to the buffer!
 {-# INLINE exactWrite #-}
-exactWrite :: Int 
-           -> (Ptr Word8 -> IO ()) 
+exactWrite :: Int
+           -> (Ptr Word8 -> IO ())
            -> Write
 exactWrite size io = Write size (pokeN size io)
 
@@ -169,7 +173,7 @@
 {-# INLINE writeLiftIO #-}
 writeLiftIO :: (a -> Write) -> IO a -> Write
 writeLiftIO write io =
-    Write (getBound' "writeLiftIO" write) 
+    Write (getBound' "writeLiftIO" write)
           (Poke $ \pf -> do x <- io; runWrite (write x) pf)
 
 -- | @writeIf p wTrue wFalse x@ creates a 'Write' with a 'Poke' equal to @wTrue
@@ -179,8 +183,8 @@
 -- independent.
 {-# INLINE writeIf #-}
 writeIf :: (a -> Bool) -> (a -> Write) -> (a -> Write) -> (a -> Write)
-writeIf p wTrue wFalse x = 
-    boundedWrite (max (getBound $ wTrue x) (getBound $ wFalse x)) 
+writeIf p wTrue wFalse x =
+    boundedWrite (max (getBound $ wTrue x) (getBound $ wFalse x))
                  (if p x then getPoke $ wTrue x else getPoke $ wFalse x)
 
 -- | Compare the value to a test value and use the first write action for the
@@ -192,12 +196,12 @@
 -- | TODO: Test this. It might well be too difficult to use.
 --   FIXME: Better name required!
 {-# INLINE writeOrdering #-}
-writeOrdering :: (a -> Ordering) 
+writeOrdering :: (a -> Ordering)
               -> (a -> Write) -> (a -> Write) -> (a -> Write)
               -> (a -> Write)
-writeOrdering ord wLT wEQ wGT x = 
-    boundedWrite bound (case ord x of LT -> getPoke $ wLT x; 
-                                      EQ -> getPoke $ wEQ x; 
+writeOrdering ord wLT wEQ wGT x =
+    boundedWrite bound (case ord x of LT -> getPoke $ wLT x;
+                                      EQ -> getPoke $ wEQ x;
                                       GT -> getPoke $ wGT x)
   where
     bound = max (getBound $ wLT x) (max (getBound $ wEQ x) (getBound $ wGT x))
@@ -205,7 +209,7 @@
 -- | A write combinator useful to build decision trees for deciding what value
 -- to write with a constant bound on the maximal number of bytes written.
 {-# INLINE writeOrd #-}
-writeOrd :: Ord a 
+writeOrd :: Ord a
        => a
        -> (a -> Write) -> (a -> Write) -> (a -> Write)
        -> (a -> Write)
@@ -226,7 +230,7 @@
 
 {-# INLINE fromWriteSingleton #-}
 fromWriteSingleton :: (a -> Write) -> (a -> Builder)
-fromWriteSingleton write = 
+fromWriteSingleton write =
     mkBuilder
   where
     mkBuilder x = fromBuildStepCont step
@@ -242,7 +246,7 @@
 
 -- | Construct a 'Builder' writing a list of data one element at a time.
 fromWriteList :: (a -> Write) -> [a] -> Builder
-fromWriteList write = 
+fromWriteList write =
     makeBuilder
   where
     makeBuilder xs0 = fromBuildStepCont $ step xs0
@@ -271,7 +275,7 @@
 
 -- | Write a storable value.
 {-# INLINE writeStorable #-}
-writeStorable :: Storable a => a -> Write 
+writeStorable :: Storable a => a -> Write
 writeStorable x = exactWrite (sizeOf x) (\op -> poke (castPtr op) x)
 
 -- | A builder that serializes a storable value. No alignment is done.
diff --git a/Blaze/ByteString/Builder/Word.hs b/Blaze/ByteString/Builder/Word.hs
--- a/Blaze/ByteString/Builder/Word.hs
+++ b/Blaze/ByteString/Builder/Word.hs
@@ -1,4 +1,9 @@
-{-# LANGUAGE CPP, MonoPatBinds #-}
+{-# LANGUAGE CPP #-}
+
+#ifdef USE_MONO_PAT_BINDS
+{-# LANGUAGE MonoPatBinds #-}
+#endif
+
 -- |
 -- Module      : Blaze.ByteString.Builder.Word
 -- Copyright   : (c) 2010 Jasper Van der Jeugt & Simon Meier
@@ -7,7 +12,7 @@
 --               (c) Lennart Kolmodin, Ross Patterson
 --
 -- License     : BSD3-style (see LICENSE)
--- 
+--
 -- Maintainer  : Simon Meier <iridcode@gmail.com>
 -- Stability   : experimental
 -- Portability : tested on GHC only
@@ -15,7 +20,7 @@
 -- 'Write's and 'Builder's for serializing words.
 --
 -- Note that for serializing a three tuple @(x,y,z)@ of bytes (or other word
--- values) you should use the expression 
+-- values) you should use the expression
 --
 -- > fromWrite $ writeWord8 x `mappend` writeWord8 y `mappend` writeWord z
 --
@@ -32,7 +37,7 @@
 #include "MachDeps.h"
 #endif
 module Blaze.ByteString.Builder.Word
-    ( 
+    (
     -- * Writing words to a buffer
 
       writeWord8
@@ -54,7 +59,7 @@
     , writeWord64host         -- :: Word64 -> Write
 
     -- * Creating builders from words
-    
+
     -- | We provide serialization functions both for singleton words as well as
     -- for lists of words. Using these list serialization functions is /much/ faster
     -- than using @mconcat . map fromWord/<n/>@, as the list serialization
@@ -102,7 +107,7 @@
 --
 -- Based upon the 'putWordX' functions from "Data.Binary.Builder" from the
 -- 'binary' package.
--- 
+--
 ------------------------------------------------------------------------------
 
 
@@ -228,25 +233,25 @@
 -- conversion.
 --
 writeWordhost :: Word -> Write
-writeWordhost w = 
+writeWordhost w =
     exactWrite (sizeOf (undefined :: Word)) (\p -> poke (castPtr p) w)
 {-# INLINE writeWordhost #-}
 
 -- | Write a 'Word16' in native host order and host endianness.
 writeWord16host :: Word16 -> Write
-writeWord16host w16 = 
+writeWord16host w16 =
     exactWrite (sizeOf (undefined :: Word16)) (\p -> poke (castPtr p) w16)
 {-# INLINE writeWord16host #-}
 
 -- | Write a 'Word32' in native host order and host endianness.
 writeWord32host :: Word32 -> Write
-writeWord32host w32 = 
+writeWord32host w32 =
     exactWrite (sizeOf (undefined :: Word32)) (\p -> poke (castPtr p) w32)
 {-# INLINE writeWord32host #-}
 
 -- | Write a 'Word64' in native host order and host endianness.
 writeWord64host :: Word64 -> Write
-writeWord64host w = 
+writeWord64host w =
     exactWrite (sizeOf (undefined :: Word64)) (\p -> poke (castPtr p) w)
 {-# INLINE writeWord64host #-}
 
@@ -274,22 +279,22 @@
 
 -- | Serialize a 'Word16' in big endian format.
 fromWord16be :: Word16 -> Builder
-fromWord16be = fromWriteSingleton writeWord16be 
+fromWord16be = fromWriteSingleton writeWord16be
 {-# INLINE fromWord16be #-}
 
 -- | Serialize a list of 'Word16's in big endian format.
 fromWord16sbe :: [Word16] -> Builder
-fromWord16sbe = fromWriteList writeWord16be 
+fromWord16sbe = fromWriteList writeWord16be
 {-# INLINE fromWord16sbe #-}
 
 -- | Serialize a 'Word16' in little endian format.
 fromWord16le :: Word16 -> Builder
-fromWord16le = fromWriteSingleton writeWord16le 
+fromWord16le = fromWriteSingleton writeWord16le
 {-# INLINE fromWord16le #-}
 
 -- | Serialize a list of 'Word16's in little endian format.
 fromWord16sle :: [Word16] -> Builder
-fromWord16sle = fromWriteList writeWord16le 
+fromWord16sle = fromWriteList writeWord16le
 {-# INLINE fromWord16sle #-}
 
 
@@ -298,42 +303,42 @@
 
 -- | Serialize a 'Word32' in big endian format.
 fromWord32be :: Word32 -> Builder
-fromWord32be = fromWriteSingleton writeWord32be 
+fromWord32be = fromWriteSingleton writeWord32be
 {-# INLINE fromWord32be #-}
 
 -- | Serialize a list of 'Word32's in big endian format.
 fromWord32sbe :: [Word32] -> Builder
-fromWord32sbe = fromWriteList writeWord32be 
+fromWord32sbe = fromWriteList writeWord32be
 {-# INLINE fromWord32sbe #-}
 
 -- | Serialize a 'Word32' in little endian format.
 fromWord32le :: Word32 -> Builder
-fromWord32le = fromWriteSingleton writeWord32le 
+fromWord32le = fromWriteSingleton writeWord32le
 {-# INLINE fromWord32le #-}
 
 -- | Serialize a list of 'Word32's in little endian format.
 fromWord32sle :: [Word32] -> Builder
-fromWord32sle = fromWriteList writeWord32le 
+fromWord32sle = fromWriteList writeWord32le
 {-# INLINE fromWord32sle #-}
 
 -- | Serialize a 'Word64' in big endian format.
 fromWord64be :: Word64 -> Builder
-fromWord64be = fromWriteSingleton writeWord64be 
+fromWord64be = fromWriteSingleton writeWord64be
 {-# INLINE fromWord64be #-}
 
 -- | Serialize a list of 'Word64's in big endian format.
 fromWord64sbe :: [Word64] -> Builder
-fromWord64sbe = fromWriteList writeWord64be 
+fromWord64sbe = fromWriteList writeWord64be
 {-# INLINE fromWord64sbe #-}
 
 -- | Serialize a 'Word64' in little endian format.
 fromWord64le :: Word64 -> Builder
-fromWord64le = fromWriteSingleton writeWord64le 
+fromWord64le = fromWriteSingleton writeWord64le
 {-# INLINE fromWord64le #-}
 
 -- | Serialize a list of 'Word64's in little endian format.
 fromWord64sle :: [Word64] -> Builder
-fromWord64sle = fromWriteList writeWord64le 
+fromWord64sle = fromWriteList writeWord64le
 {-# INLINE fromWord64sle #-}
 
 
@@ -347,33 +352,33 @@
 -- conversion.
 --
 fromWordhost :: Word -> Builder
-fromWordhost = fromWriteSingleton writeWordhost 
+fromWordhost = fromWriteSingleton writeWordhost
 {-# INLINE fromWordhost #-}
 
 -- | Serialize a list of 'Word's.
 -- See 'fromWordhost' for usage considerations.
 fromWordshost :: [Word] -> Builder
-fromWordshost = fromWriteList writeWordhost 
+fromWordshost = fromWriteList writeWordhost
 {-# INLINE fromWordshost #-}
 
 -- | Write a 'Word16' in native host order and host endianness.
 fromWord16host :: Word16 -> Builder
-fromWord16host = fromWriteSingleton writeWord16host 
+fromWord16host = fromWriteSingleton writeWord16host
 {-# INLINE fromWord16host #-}
 
 -- | Write a list of 'Word16's in native host order and host endianness.
 fromWord16shost :: [Word16] -> Builder
-fromWord16shost = fromWriteList writeWord16host 
+fromWord16shost = fromWriteList writeWord16host
 {-# INLINE fromWord16shost #-}
 
 -- | Write a 'Word32' in native host order and host endianness.
 fromWord32host :: Word32 -> Builder
-fromWord32host = fromWriteSingleton writeWord32host 
+fromWord32host = fromWriteSingleton writeWord32host
 {-# INLINE fromWord32host #-}
 
 -- | Write a list of 'Word32's in native host order and host endianness.
 fromWord32shost :: [Word32] -> Builder
-fromWord32shost = fromWriteList writeWord32host 
+fromWord32shost = fromWriteList writeWord32host
 {-# INLINE fromWord32shost #-}
 
 -- | Write a 'Word64' in native host order and host endianness.
diff --git a/CHANGES b/CHANGES
--- a/CHANGES
+++ b/CHANGES
@@ -1,9 +1,13 @@
+* 0.3.1.1
+  - Changed imports of Foreign.Unsafe to make it GHC 7.8 compatible
+  - -Wall clean on GHC 7.0 - 7.6
+
 * 0.3.1.0
   - Widened dependencies on text and bytestring
 
 * 0.3.0.1
 
-  - Fix build warning in Blaze.ByteString.Builder.Word 
+  - Fix build warning in Blaze.ByteString.Builder.Word
     (contributed by Greg Weber)
 
 * 0.3.0.1
@@ -18,8 +22,8 @@
 
   - Renamings in internal modules: WriteIO -> Poke and associated functions.
 
-* 0.2.1.4 
-   
+* 0.2.1.4
+
   - Fixed bug: appending to 'chunkedTransferEncoding somebuilder' also encoded
     the appended builder, which is obviously wrong.
 
@@ -33,7 +37,7 @@
     issues caused by GHC bug http://hackage.haskell.org/trac/ghc/ticket/4498
 
 * 0.2.1.1
-  
+
   - Reexport 'Write' datatype and 'fromWriteList', 'fromWriteSingleton',
     'fromWrite' functions together with writes and builders for storables.
   - Add 'MonoPatBinds' language extension to (hopefully) solve the issues
@@ -51,7 +55,7 @@
       encoding this results currently in a slight slowdown due to GHC not
       recognizing the strictness of the returned value. This will be fixed in
       the future.
-    - BuildSteps support returning a result in `Done`, which enables to 
+    - BuildSteps support returning a result in `Done`, which enables to
       implement a `Put` monad using CPS.
     - chunked list writes were removed, as they result in worse performance
       when writing non-trivial lists. (cf. benchmarks)
@@ -60,16 +64,16 @@
       `blaze-builder-enumeratee` package, to execute puts and builders.
       It will be used later also by the execution functions of the
       `blaze-builder` package.
-  
+
   Implemented new functionality
-    - `Blaze.ByteString.Builder.HTTP` provides a builder transformer for 
+    - `Blaze.ByteString.Builder.HTTP` provides a builder transformer for
        doing in-buffer chunked HTTP encoding of an arbitary other builder.
     - `Blaze.ByteString.Builder.Char8` provides functions to serialize the
        lower 8-bits of characters similiar to what `Data.ByteString.Char8`
        provides for bytestrings.
 
 * 0.2.0.3
-  
+
   Loosen 'text' dependency to '>= 0.10 && < 0.12'
 
 * 0.2.0.2
@@ -77,7 +81,7 @@
   Fixed bug: use &#39; instead of &apos; for HTML escaping '
 
 * 0.2.0.1
-  
+
   Added a missing benchmark file.
 
 * blaze-builder-0.2.0.0
@@ -90,7 +94,7 @@
   Changed module structure:
     Blaze.ByteString.Builder.Core -> Blaze.ByteString.Builder
     Blaze.ByteString.Builder.Utf8 -> Blaze.ByteString.Builder.Char.Utf8
-    Blaze.ByteString.Builder.Html -> Blaze.ByteString.Builder.Html.Utf8 
+    Blaze.ByteString.Builder.Html -> Blaze.ByteString.Builder.Html.Utf8
 
   Changed function names:
     writeByte     -> writeWord8
@@ -106,7 +110,7 @@
     'empty'    : use 'mempty' instead
     'singleton': use 'fromWord8' instead
     'append'   : use 'mappend' instead
-  
+
 
 * blaze-builder-0.1
 
diff --git a/blaze-builder.cabal b/blaze-builder.cabal
--- a/blaze-builder.cabal
+++ b/blaze-builder.cabal
@@ -1,5 +1,5 @@
 Name:                blaze-builder
-Version:             0.3.1.0
+Version:             0.3.1.1
 Synopsis:            Efficient buffered output.
 
 Description:         
@@ -48,6 +48,14 @@
 
 Library
   ghc-options:       -Wall
+
+  -- Earlier versions of GHC did not split off unsafe functions in "Foreign"
+  if impl(ghc >= 7.4)
+      cpp-options:   -DHAS_FOREIGN_UNSAFE_MODULE
+
+  -- GHC 7.0.x and lower required MonoPatBinds feature
+  if impl(ghc < 7.2)
+      cpp-options:   -DUSE_MONO_PAT_BINDS
 
   exposed-modules:   Blaze.ByteString.Builder
                      Blaze.ByteString.Builder.Int
diff --git a/tests/LlvmSegfault.hs b/tests/LlvmSegfault.hs
--- a/tests/LlvmSegfault.hs
+++ b/tests/LlvmSegfault.hs
@@ -31,5 +31,5 @@
 word8s = map (fromWord8 . fromIntegral) $ [(1::Int)..1000]
 
 main :: IO ()
-main = 
+main =
     print $ toLazyByteStringWith 10 10 (mconcat word8s) L.empty
diff --git a/tests/Tests.hs b/tests/Tests.hs
--- a/tests/Tests.hs
+++ b/tests/Tests.hs
@@ -79,7 +79,7 @@
     show = show . toLazyByteString
 
 instance Eq Builder where
-    b1 == b2 = 
+    b1 == b2 =
         -- different and small buffer sizses for testing wrapping behaviour
         toLazyByteStringWith  1024     1024 256 b1 mempty ==
         toLazyByteStringWith  2001     511  256 b2 mempty
