-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathRuntime.hs
363 lines (293 loc) · 11.2 KB
/
Runtime.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
-- | Implementations of builtin Granule functions in Haskell
{-# LANGUAGE NamedFieldPuns, Strict, NoImplicitPrelude, TypeFamilies,
DataKinds, GADTs #-}
{-# OPTIONS_GHC -fno-full-laziness -fno-warn-unused-binds #-}
module Language.Granule.Runtime
(
-- Granule runtime-specific data structures
FloatArray(..), BenchList(..), RuntimeData(..)
-- Granule runtime-specific procedures
, pure
, mkIOBenchMain,fromStdin,toStdout,toStderr,timeDate,readInt,showChar,intToFloat
, showInt,showFloat, charToInt, charFromInt, stringAppend
, newFloatArray,newFloatArrayI,writeFloatArray,writeFloatArrayI
, readFloatArray,readFloatArrayI,lengthFloatArray,deleteFloatArray,copyFloatArray'
, uniqueReturn,uniqueBind,uniquePush,uniquePull,uniquifyFloatArray,borrowFloatArray
, newFloatArraySafe,newFloatArrayISafe,writeFloatArraySafe,writeFloatArrayISafe
, readFloatArraySafe,readFloatArrayISafe,deleteFloatArraySafe,copyFloatArraySafe
, uniquifyFloatArraySafe,borrowFloatArraySafe
, newRefSafe, freezeRefSafe, swapRefSafe, readRefSafe, copyRefSafe
, cap, Cap(..), Capability(..), CapabilityType
-- Re-exported from Prelude
, String, Int, IO, Float, Maybe(..), Show(..), Char, getLine
, putStr, read, (<$>) , fromIntegral, Monad(..)
, ($), error, (>), (++), id, Num(..), (.)
, pack, Text
) where
import Foreign.Marshal.Array ( callocArray )
import Foreign.Ptr ( Ptr )
import Foreign.Storable ( Storable(peekElemOff, pokeElemOff) )
import System.IO.Unsafe ( unsafePerformIO )
import Foreign.Marshal.Alloc ( free )
import System.IO ( hFlush, stderr, stdout )
import qualified Data.Array.IO as MA
import Criterion.Main ( defaultMain, bench, bgroup, nfAppIO )
import System.IO.Silently ( silence )
import Prelude
( Int, IO, Double, Maybe(..), Show(..), Char, read, fromEnum, toEnum
, (<$>), (<>), fromIntegral, ($), error, (>), (++), id, Num (..), (.) )
import Control.Monad
import GHC.Err (undefined)
import Data.Function (const)
import Data.Text
import Data.Text.IO
import Data.Time.Clock
import qualified Data.IORef as MR
-- ^ Eventually this can be expanded with other kinds of runtime-managed data
data RuntimeData a = FA FloatArray | PR (PolyRef a)
-- ^ Granule calls doubles floats
type Float = Double
-- ^ Granule calls Text String
type String = Text
pure :: Monad m => a -> m a
pure = return
--------------------------------------------------------------------------------
-- Benchmarking
--------------------------------------------------------------------------------
data BenchList =
BenchGroup String BenchList BenchList
| Bench Int String (Int -> IO ()) BenchList
| Done
mkIOBenchMain :: BenchList -> IO ()
mkIOBenchMain ns = defaultMain (go ns)
where go (Bench n s f next) = bench (unpack s) (nfAppIO (silence . f) n) : go next
go (BenchGroup str benchs next) = bgroup (unpack str) (go benchs) : go next
go Done = []
--------------------------------------------------------------------------------
-- I/O
--------------------------------------------------------------------------------
fromStdin :: IO String
fromStdin = do
putStr "> "
hFlush stdout
getLine
toStdout :: String -> IO ()
toStdout = putStr
toStderr :: String -> IO ()
toStderr = let red x = "\ESC[31;1m" <> x <> "\ESC[0m" in (hPutStr stderr) . red
timeDate :: () -> IO String
timeDate () = getCurrentTime >>= (return . pack . show)
--------------------------------------------------------------------------------
-- Conversions
--------------------------------------------------------------------------------
readInt :: String -> Int
readInt s =
if null s
then 0
else read $ unpack s
showChar :: Char -> String
showChar = singleton
charToInt :: Char -> Int
charToInt = fromEnum
charFromInt :: Int -> Char
charFromInt = toEnum
intToFloat :: Int -> Float
intToFloat = fromIntegral
showInt :: Int -> String
showInt = pack . show
showFloat :: Float -> String
showFloat = pack . show
--------------------------------------------------------------------------------
-- String operations
--------------------------------------------------------------------------------
stringAppend :: String -> String -> String
stringAppend = (<>)
--------------------------------------------------------------------------------
-- Mutable arrays
--------------------------------------------------------------------------------
data FloatArray =
HaskellArray {
-- | Length of Haskell array
grLength :: Int,
-- | An ordinary Haskell IOArray
grArr :: MA.IOArray Int Float } |
PointerArray {
-- | Length of the array in memory
grLength :: Int,
-- | Pointer to a block of memory
grPtr :: Ptr Float }
data PolyRef a =
HaskellRef {
haskRef :: MR.IORef a
}
{-# NOINLINE newFloatArray #-}
newFloatArray :: Int -> FloatArray
newFloatArray = unsafePerformIO . newFloatArraySafe
newFloatArraySafe :: Int -> IO FloatArray
newFloatArraySafe size = do
ptr <- callocArray size
return $ PointerArray size ptr
{-# NOINLINE newRef #-}
newRef :: a -> PolyRef a
newRef = unsafePerformIO . newRefSafe
newRefSafe :: a -> IO (PolyRef a)
newRefSafe v = do
r <- MR.newIORef v
return $ HaskellRef r
{-# NOINLINE newFloatArrayI #-}
newFloatArrayI :: Int -> FloatArray
newFloatArrayI = unsafePerformIO . newFloatArrayISafe
newFloatArrayISafe :: Int -> IO FloatArray
newFloatArrayISafe size = do
arr <- MA.newArray (0,size-1) 0.0
return $ HaskellArray size arr
{-# NOINLINE writeFloatArray #-}
writeFloatArray :: FloatArray -> Int -> Float -> FloatArray
writeFloatArray a i v = unsafePerformIO $ writeFloatArraySafe a i v
writeFloatArraySafe :: FloatArray -> Int -> Float -> IO FloatArray
writeFloatArraySafe a i v =
if i > grLength a
then error $ "array index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
else case a of
HaskellArray{} -> error "expected unique array"
PointerArray len ptr -> do
() <- pokeElemOff ptr i v
return $ PointerArray len ptr
{-# NOINLINE writeFloatArrayI #-}
writeFloatArrayI :: FloatArray -> Int -> Float -> FloatArray
writeFloatArrayI a i v = unsafePerformIO $ writeFloatArrayISafe a i v
writeFloatArrayISafe :: FloatArray -> Int -> Float -> IO FloatArray
writeFloatArrayISafe a i v =
if i > grLength a
then error $ "array index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
else case a of
PointerArray{} -> error "expected non-unique array"
HaskellArray len arr -> do
arr' <- MA.mapArray id arr
() <- MA.writeArray arr' i v
return $ HaskellArray len arr'
{-# NOINLINE swapRef #-}
swapRef :: PolyRef a -> a -> (a, PolyRef a)
swapRef r v = unsafePerformIO $ swapRefSafe r v
swapRefSafe :: PolyRef a -> a -> IO (a, PolyRef a)
swapRefSafe HaskellRef{haskRef} v = do
x <- MR.readIORef haskRef
MR.writeIORef haskRef v
return $ (x, HaskellRef haskRef)
{-# NOINLINE readRef #-}
readRef :: PolyRef a -> (a, PolyRef a)
readRef = unsafePerformIO . readRefSafe
readRefSafe :: PolyRef a -> IO (a, PolyRef a)
readRefSafe HaskellRef{haskRef} = do
x <- MR.readIORef haskRef
return $ (x, HaskellRef haskRef)
{-# NOINLINE readFloatArray #-}
readFloatArray :: FloatArray -> Int -> (Float, FloatArray)
readFloatArray a i = unsafePerformIO $ readFloatArraySafe a i
readFloatArraySafe :: FloatArray -> Int -> IO (Float, FloatArray)
readFloatArraySafe a i =
if i > grLength a
then error $ "readFloatArray index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
else case a of
HaskellArray{} -> error "expected unique array"
PointerArray len ptr -> do
v <- peekElemOff ptr i
return (v,a)
{-# NOINLINE readFloatArrayI #-}
readFloatArrayI :: FloatArray -> Int -> (Float, FloatArray)
readFloatArrayI a i = unsafePerformIO $ readFloatArrayISafe a i
readFloatArrayISafe :: FloatArray -> Int -> IO (Float, FloatArray)
readFloatArrayISafe a i =
if i > grLength a
then error $ "readFloatArrayI index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
else case a of
PointerArray{} -> error "expected non-unique array"
HaskellArray _ arr -> do
e <- MA.readArray arr i
return (e,a)
lengthFloatArray :: FloatArray -> (Int, FloatArray)
lengthFloatArray a = (grLength a, a)
{-# NOINLINE deleteFloatArray #-}
deleteFloatArray :: FloatArray -> ()
deleteFloatArray = unsafePerformIO . deleteFloatArraySafe
deleteFloatArraySafe :: FloatArray -> IO ()
deleteFloatArraySafe PointerArray{grPtr} =
free grPtr
deleteFloatArraySafe HaskellArray{grArr} =
void (MA.mapArray (const undefined) grArr)
{-# NOINLINE freezeRef #-}
freezeRef :: PolyRef a -> a
freezeRef = unsafePerformIO . freezeRefSafe
freezeRefSafe :: PolyRef a -> IO a
freezeRefSafe HaskellRef{haskRef} =
MR.readIORef haskRef
{-# NOINLINE copyFloatArray' #-}
copyFloatArray' :: FloatArray -> FloatArray
copyFloatArray' = unsafePerformIO . copyFloatArraySafe
copyFloatArraySafe :: FloatArray -> IO FloatArray
copyFloatArraySafe a =
case a of
PointerArray{} -> error "expected non-unique array"
HaskellArray len arr -> do
arr' <- MA.mapArray id arr
return $ uniquifyFloatArray $ HaskellArray len arr'
{-# NOINLINE copyRef #-}
copyRef :: PolyRef a -> PolyRef a
copyRef = unsafePerformIO . copyRefSafe
copyRefSafe :: PolyRef a -> IO (PolyRef a)
copyRefSafe HaskellRef{haskRef} = do
val <- MR.readIORef haskRef
haskRef' <- MR.newIORef val
return $ HaskellRef haskRef'
{-# NOINLINE uniquifyFloatArray #-}
uniquifyFloatArray :: FloatArray -> FloatArray
uniquifyFloatArray = unsafePerformIO . uniquifyFloatArraySafe
uniquifyFloatArraySafe :: FloatArray -> IO FloatArray
uniquifyFloatArraySafe a =
case a of
PointerArray{} -> error "expected non-unique array"
HaskellArray len arr -> do
arr' <- newFloatArraySafe len
forM_ [0..(len-1)] $ \i -> do
v <- MA.readArray arr i
pokeElemOff (grPtr arr') i v
return arr'
{-# NOINLINE borrowFloatArray #-}
borrowFloatArray :: FloatArray -> FloatArray
borrowFloatArray = unsafePerformIO . borrowFloatArraySafe
borrowFloatArraySafe :: FloatArray -> IO FloatArray
borrowFloatArraySafe a =
case a of
HaskellArray{} -> error "expected unique array"
PointerArray len ptr -> do
arr' <- newFloatArrayISafe len
forM_ [0..(len-1)] $ \i -> do
v <- peekElemOff ptr i
MA.writeArray (grArr arr') i v
return arr'
--------------------------------------------------------------------------------
-- Uniqueness monadic operations
--------------------------------------------------------------------------------
uniqueReturn :: a -> a
uniqueReturn = id
uniqueBind :: (a -> b) -> a -> b
uniqueBind f = f
uniquePush :: (a,b) -> (a,b)
uniquePush = id
uniquePull :: (a,b) -> (a,b)
uniquePull = id
--------------------------------------------------------------------------------
-- Capabilities
--------------------------------------------------------------------------------
-- Emulate dependent types
data Cap = ConsoleTag | TimeDateTag
data Capability (c :: Cap) where
Console :: Capability 'ConsoleTag
TimeDate :: Capability 'TimeDateTag
type family CapabilityType (c :: Cap)
type instance CapabilityType 'ConsoleTag = Text -> ()
type instance CapabilityType 'TimeDateTag = () -> Text
{-# NOINLINE cap #-}
cap :: Capability cap -> () -> CapabilityType cap
cap Console () = \x -> unsafePerformIO $ toStdout $ x
cap TimeDate () = \() -> unsafePerformIO $ timeDate ()