2 % (c) The AQUA Project, Glasgow University, 1994-2000
6 {-# OPTIONS -fno-implicit-prelude #-}
13 hugsprimPmFromInteger,
16 hugsprimRunIO_toplevel,
20 hugsprimCreateAdjThunk,
32 import PrelReal(Integral)
33 import Prelude(fromIntegral)
34 import IO(putStr,hFlush,stdout,stderr)
35 import PrelException(catch,catchException)
36 import PrelIOBase(IO(..),unsafePerformIO)
37 import PrelShow(show,shows,showString,showChar,Show,ShowS)
38 import PrelRead(Read,ReadS,lex,reads)
39 import PrelFloat(Double)
40 import PrelReal(Fractional,fromRational,toRational)
43 import PrelPack(unpackCString)
45 -- Stuff needed by Hugs for desugaring. Do not mess with these!
46 -- They need to correspond exactly to versions written in
47 -- the Hugs standalone Prelude.
49 -- hugs doesn't know about RealWorld and so throws this
50 -- away if the original type signature is used
51 -- hugsprimMkIO :: (RealWorld -> (a,RealWorld)) -> IO a
53 -- The first arg is an IO value created by Hugs, without the
54 -- newtype ST wrapper. What we do here place a wrapper around
55 -- it, so that it can be called from GHC-land, which uses a
56 -- different IO representation.
58 -- This is all very delicate and relies crucially on the non-inlined
59 -- connectWorlds fn to create an artificial dependency of the hugs_ioaction
60 -- on the grealworld. That's needed to stop the simplifier floating
61 -- the case outside of the \ grealworld.
62 hugsprimMkIO :: (rw -> (a,rw)) -> IO a
63 hugsprimMkIO hugs_ioaction
64 = IO ( \ grealworld -> case hugs_ioaction
65 (connectWorlds grealworld) of
66 (res, hrealworld') -> (# grealworld, res #)
69 {-# NOINLINE connectWorlds #-}
70 connectWorlds :: State# RealWorld -> a -- really, -> Hugs' RealWorld
71 connectWorlds hrealworld
72 = error "connectWorlds: hugs entered the RealWorld"
77 hugsprimCreateAdjThunk :: (a -> b) -> String -> Char -> IO Addr
78 hugsprimCreateAdjThunk fun typestr callconv
79 = error "hugsprimCreateAdjThunk in combined mode: unimplemented"
81 fromDouble :: Fractional a => Double -> a
82 fromDouble n = fromRational (toRational n)
84 hugsprimEqChar :: Char -> Char -> Bool
85 hugsprimEqChar c1 c2 = c1 == c2
87 hugsprimPmInt :: Num a => Int -> a -> Bool
88 hugsprimPmInt n x = fromInt n == x
90 hugsprimPmInteger :: Num a => Integer -> a -> Bool
91 hugsprimPmInteger n x = fromInteger n == x
93 hugsprimPmDouble :: Fractional a => Double -> a -> Bool
94 hugsprimPmDouble n x = fromDouble n == x
96 -- The following primitives are only needed if (n+k) patterns are enabled:
97 hugsprimPmSub :: Integral a => Int -> a -> a
98 hugsprimPmSub n x = x - fromInt n
100 hugsprimPmFromInteger :: Integral a => Integer -> a
101 hugsprimPmFromInteger = fromIntegral
103 hugsprimPmSubtract :: Integral a => a -> a -> a
104 hugsprimPmSubtract x y = x - y
106 hugsprimPmLe :: Integral a => a -> a -> Bool
107 hugsprimPmLe x y = x <= y
109 hugsprimUnpackString :: Addr -> String
110 hugsprimUnpackString a = unpackCString a
112 -- ToDo: make the message more informative.
114 hugsprimPmFail = error "Pattern Match Failure"
116 hugsprimCompAux :: Ord a => a -> a -> Ordering -> Ordering
117 hugsprimCompAux x y o = case compare x y of EQ -> o; LT -> LT; GT -> GT
119 hugsprimError :: String -> a
120 hugsprimError s = error s
122 hugsprimShowField :: Show a => String -> a -> ShowS
123 hugsprimShowField m v = showString m . showChar '=' . shows v
125 hugsprimReadField :: Read a => String -> ReadS a
126 hugsprimReadField m s0 = [ r | (t, s1) <- lex s0, t == m,
131 -- used when Hugs invokes top level function
133 hugsprimRunIO_toplevel :: IO a -> ()
134 hugsprimRunIO_toplevel m
135 = protect 5 (fst (unST composite_action realWorld))
138 = do writeIORef prelCleanupAfterRunAction Nothing
140 cleanup_handles <- readIORef prelCleanupAfterRunAction
141 case cleanup_handles of
145 realWorld = error "primRunIO: entered the RealWorld"
146 protect :: Int -> () -> ()
150 = primCatch (protect (n-1) comp)
151 (\e -> fst (unST (putStr (show e ++ "\n")) realWorld))
154 hugsprimRunIO_toplevel :: IO a -> ()
155 hugsprimRunIO_toplevel m
157 catchException (m >> hFlush stderr >> hFlush stdout)
158 (\e -> putStr ("error: " ++ show e ++ "\n"))