File: Types.hs

package info (click to toggle)
haskell-primitive 0.9.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: haskell: 4,436; ansic: 72; makefile: 2
file content (547 lines) | stat: -rw-r--r-- 22,635 bytes parent folder | download
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

#if __GLASGOW_HASKELL__ < 906
{-# LANGUAGE TypeInType #-}
#endif

#include "HsBaseConfig.h"

-- |
-- Module      : Data.Primitive.Types
-- Copyright   : (c) Roman Leshchinskiy 2009-2012
-- License     : BSD-style
--
-- Maintainer  : Roman Leshchinskiy <rl@cse.unsw.edu.au>
-- Portability : non-portable
--
-- Basic types and classes for primitive array operations.

module Data.Primitive.Types
  ( Prim(..)
  , sizeOf, sizeOfType, alignment, alignmentOfType, defaultSetByteArray#, defaultSetOffAddr#
  , PrimStorable(..)
  , Ptr(..)
  ) where

import Control.Monad.Primitive
import Data.Primitive.MachDeps
import Data.Primitive.Internal.Operations
import Foreign.Ptr (IntPtr, intPtrToPtr, ptrToIntPtr, WordPtr, wordPtrToPtr, ptrToWordPtr)
import Foreign.C.Types
import System.Posix.Types
import Data.Complex

import GHC.Word (Word8(..), Word16(..), Word32(..), Word64(..))
import GHC.Int (Int8(..), Int16(..), Int32(..), Int64(..))

import GHC.Stable (StablePtr(..))

import GHC.Exts hiding (setByteArray#)

import Foreign.Storable (Storable)


import qualified Foreign.Storable as FS

import GHC.IO (IO(..))
import qualified GHC.Exts

import Control.Applicative (Const(..))
import Data.Functor.Identity (Identity(..))
import qualified Data.Monoid as Monoid
import qualified Data.Semigroup as Semigroup
import Data.Proxy

#if !MIN_VERSION_base(4,13,0)
import Data.Ord (Down(..))
#endif

-- | Class of types supporting primitive array operations. This includes
-- interfacing with GC-managed memory (functions suffixed with @ByteArray#@)
-- and interfacing with unmanaged memory (functions suffixed with @Addr#@).
-- Endianness is platform-dependent.
class Prim a where
  -- We use `Proxy` instead of `Proxy#`, since the latter doesn't work with GND for GHC <= 8.8.

  -- | The size of values of type @a@ in bytes. This has to be used with TypeApplications: @sizeOfType \@a@.
  --
  -- @since 0.9.0.0
  sizeOfType# :: Proxy a -> Int#
  sizeOfType# _ = sizeOf# (dummy :: a)

  -- | The size of values of type @a@ in bytes. The argument is not used.
  --
  -- It is recommended to use 'sizeOfType#' instead.
  sizeOf# :: a -> Int#
  sizeOf# _ = sizeOfType# (Proxy :: Proxy a)

  -- | The alignment of values of type @a@ in bytes. This has to be used with TypeApplications: @alignmentOfType \@a@.
  --
  -- @since 0.9.0.0
  alignmentOfType# :: Proxy a -> Int#
  alignmentOfType# _ = alignment# (dummy :: a)

  -- | The alignment of values of type @a@ in bytes. The argument is not used.
  --
  -- It is recommended to use 'alignmentOfType#' instead.
  alignment# :: a -> Int#
  alignment# _ = alignmentOfType# (Proxy :: Proxy a)

  -- | Read a value from the array. The offset is in elements of type
  -- @a@ rather than in bytes.
  indexByteArray# :: ByteArray# -> Int# -> a

  -- | Read a value from the mutable array. The offset is in elements of type
  -- @a@ rather than in bytes.
  readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)

  -- | Write a value to the mutable array. The offset is in elements of type
  -- @a@ rather than in bytes.
  writeByteArray# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s

  -- | Fill a slice of the mutable array with a value. The offset and length
  -- of the chunk are in elements of type @a@ rather than in bytes.
  setByteArray#
    :: MutableByteArray# s
    -> Int# -- ^ offset
    -> Int# -- ^ length
    -> a
    -> State# s
    -> State# s
  setByteArray# = defaultSetByteArray#

  -- | Read a value from a memory position given by an address and an offset.
  -- The memory block the address refers to must be immutable. The offset is in
  -- elements of type @a@ rather than in bytes.
  indexOffAddr# :: Addr# -> Int# -> a

  -- | Read a value from a memory position given by an address and an offset.
  -- The offset is in elements of type @a@ rather than in bytes.
  readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, a #)

  -- | Write a value to a memory position given by an address and an offset.
  -- The offset is in elements of type @a@ rather than in bytes.
  writeOffAddr# :: Addr# -> Int# -> a -> State# s -> State# s

  -- | Fill a memory block given by an address, an offset and a length.
  -- The offset and length are in elements of type @a@ rather than in bytes.
  setOffAddr#
    :: Addr#
    -> Int# -- ^ offset
    -> Int# -- ^ length
    -> a
    -> State# s
    -> State# s
  setOffAddr# = defaultSetOffAddr#

  {-# MINIMAL (sizeOfType# | sizeOf#), (alignmentOfType# | alignment#), indexByteArray#, readByteArray#, writeByteArray#,
    indexOffAddr#, readOffAddr#, writeOffAddr# #-}

-- | A dummy value of type @a@.
dummy :: a
dummy = errorWithoutStackTrace "Data.Primitive.Types: implementation mistake in `Prim` instance"
{-# NOINLINE dummy #-}

-- | The size of values of type @a@ in bytes. This has to be used with TypeApplications: @sizeOfType \@a@.
--
-- >>> :set -XTypeApplications
-- >>> import Data.Int (Int32)
-- >>> sizeOfType @Int32
-- 4
--
-- @since 0.9.0.0
sizeOfType :: forall a. Prim a => Int
sizeOfType = I# (sizeOfType# (Proxy :: Proxy a))

-- | The size of values of type @a@ in bytes. The argument is not used.
--
-- It is recommended to use 'sizeOfType' instead.
--
-- This function has existed since 0.1, but was moved from 'Data.Primitive'
-- to 'Data.Primitive.Types' in version 0.6.3.0.
sizeOf :: Prim a => a -> Int
sizeOf x = I# (sizeOf# x)

-- | The alignment of values of type @a@ in bytes. This has to be used with TypeApplications: @alignmentOfType \@a@.
--
-- @since 0.9.0.0
alignmentOfType :: forall a. Prim a => Int
alignmentOfType = I# (alignmentOfType# (Proxy :: Proxy a))

-- | The alignment of values of type @a@ in bytes. The argument is not used.
--
-- It is recommended to use 'alignmentOfType' instead.
--
-- This function has existed since 0.1, but was moved from 'Data.Primitive'
-- to 'Data.Primitive.Types' in version 0.6.3.0.
alignment :: Prim a => a -> Int
alignment x = I# (alignment# x)

-- | @since 0.9.0.0
instance Prim a => Prim (Complex a) where
  sizeOf# _ = 2# *# sizeOf# (undefined :: a)
  alignment# _ = alignment# (undefined :: a)
  indexByteArray# arr# i# =
    let x = indexByteArray# arr# (2# *# i#)
        y = indexByteArray# arr# (2# *# i# +# 1#)
    in x :+ y
  readByteArray# arr# i# =
    \s0 -> case readByteArray# arr# (2# *# i#) s0 of
       (# s1#, x #) -> case readByteArray# arr# (2# *# i# +# 1#) s1# of
          (# s2#, y #) -> (# s2#, x :+ y #)
  writeByteArray# arr# i# (a :+ b) =
    \s0 -> case writeByteArray# arr# (2# *# i#) a s0 of
       s1 -> case writeByteArray# arr# (2# *# i# +# 1#) b s1 of
         s2 -> s2
  indexOffAddr# addr# i# =
    let x = indexOffAddr# addr# (2# *# i#)
        y = indexOffAddr# addr# (2# *# i# +# 1#)
    in x :+ y
  readOffAddr# addr# i# =
    \s0 -> case readOffAddr# addr# (2# *# i#) s0 of
       (# s1, x #) -> case readOffAddr# addr# (2# *# i# +# 1#) s1 of
         (# s2, y #) -> (# s2, x :+ y #)
  writeOffAddr# addr# i# (a :+ b) =
    \s0 -> case writeOffAddr# addr# (2# *# i#) a s0 of
       s1 -> case writeOffAddr# addr# (2# *# i# +# 1#) b s1 of
         s2 -> s2
  {-# INLINE sizeOf# #-}
  {-# INLINE alignment# #-}
  {-# INLINE indexByteArray# #-}
  {-# INLINE readByteArray# #-}
  {-# INLINE writeByteArray# #-}
  {-# INLINE indexOffAddr# #-}
  {-# INLINE readOffAddr# #-}
  {-# INLINE writeOffAddr# #-}

-- | An implementation of 'setByteArray#' that calls 'writeByteArray#'
-- to set each element. This is helpful when writing a 'Prim' instance
-- for a multi-word data type for which there is no CPU-accelerated way
-- to broadcast a value to contiguous memory. It is typically used
-- alongside 'defaultSetOffAddr#'. For example:
--
-- > data Trip = Trip Int Int Int
-- >
-- > instance Prim Trip
-- >   sizeOfType# _ = 3# *# sizeOfType# (proxy# :: Proxy# Int)
-- >   alignmentOfType# _ = alignmentOfType# (proxy# :: Proxy# Int)
-- >   indexByteArray# arr# i# = ...
-- >   readByteArray# arr# i# = ...
-- >   writeByteArray# arr# i# (Trip a b c) =
-- >     \s0 -> case writeByteArray# arr# (3# *# i#) a s0 of
-- >        s1 -> case writeByteArray# arr# ((3# *# i#) +# 1#) b s1 of
-- >          s2 -> case writeByteArray# arr# ((3# *# i#) +# 2# ) c s2 of
-- >            s3 -> s3
-- >   setByteArray# = defaultSetByteArray#
-- >   indexOffAddr# addr# i# = ...
-- >   readOffAddr# addr# i# = ...
-- >   writeOffAddr# addr# i# (Trip a b c) =
-- >     \s0 -> case writeOffAddr# addr# (3# *# i#) a s0 of
-- >        s1 -> case writeOffAddr# addr# ((3# *# i#) +# 1#) b s1 of
-- >          s2 -> case writeOffAddr# addr# ((3# *# i#) +# 2# ) c s2 of
-- >            s3 -> s3
-- >   setOffAddr# = defaultSetOffAddr#
defaultSetByteArray# :: Prim a => MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s
defaultSetByteArray# arr# i# len# ident = go 0#
  where
  go ix# s0 = if isTrue# (ix# <# len#)
    then case writeByteArray# arr# (i# +# ix#) ident s0 of
      s1 -> go (ix# +# 1#) s1
    else s0

-- | An implementation of 'setOffAddr#' that calls 'writeOffAddr#'
-- to set each element. The documentation of 'defaultSetByteArray#'
-- provides an example of how to use this.
defaultSetOffAddr# :: Prim a => Addr# -> Int# -> Int# -> a -> State# s -> State# s
defaultSetOffAddr# addr# i# len# ident = go 0#
  where
  go ix# s0 = if isTrue# (ix# <# len#)
    then case writeOffAddr# addr# (i# +# ix#) ident s0 of
      s1 -> go (ix# +# 1#) s1
    else s0

-- | Newtype that uses a 'Prim' instance to give rise to a 'Storable' instance.
-- This type is intended to be used with the @DerivingVia@ extension available
-- in GHC 8.6 and up. For example, consider a user-defined 'Prim' instance for
-- a multi-word data type.
--
-- > data Uuid = Uuid Word64 Word64
-- >   deriving Storable via (PrimStorable Uuid)
-- > instance Prim Uuid where ...
--
-- Writing the 'Prim' instance is tedious and unavoidable, but the 'Storable'
-- instance comes for free once the 'Prim' instance is written.
newtype PrimStorable a = PrimStorable { getPrimStorable :: a }

instance Prim a => Storable (PrimStorable a) where
  sizeOf _ = sizeOfType @a
  alignment _ = alignmentOfType @a
  peekElemOff (Ptr addr#) (I# i#) =
    primitive $ \s0# -> case readOffAddr# addr# i# s0# of
      (# s1, x #) -> (# s1, PrimStorable x #)
  pokeElemOff (Ptr addr#) (I# i#) (PrimStorable a) = primitive_ $ \s# ->
    writeOffAddr# addr# i# a s#

#define derivePrim(ty, ctr, sz, align, idx_arr, rd_arr, wr_arr, set_arr, idx_addr, rd_addr, wr_addr, set_addr) \
instance Prim (ty) where {                                        \
  sizeOfType# _ = unI# sz                                         \
; alignmentOfType# _ = unI# align                                 \
; indexByteArray# arr# i# = ctr (idx_arr arr# i#)                 \
; readByteArray#  arr# i# s# = case rd_arr arr# i# s# of          \
                        { (# s1#, x# #) -> (# s1#, ctr x# #) }    \
; writeByteArray# arr# i# (ctr x#) s# = wr_arr arr# i# x# s#      \
; setByteArray# arr# i# n# (ctr x#) s#                            \
    = let { i = fromIntegral (I# i#)                              \
          ; n = fromIntegral (I# n#)                              \
          } in                                                    \
      case unsafeCoerce# (internal (set_arr arr# i n x#)) s# of   \
        { (# s1#, _ #) -> s1# }                                   \
                                                                  \
; indexOffAddr# addr# i# = ctr (idx_addr addr# i#)                \
; readOffAddr#  addr# i# s# = case rd_addr addr# i# s# of         \
                        { (# s1#, x# #) -> (# s1#, ctr x# #) }    \
; writeOffAddr# addr# i# (ctr x#) s# = wr_addr addr# i# x# s#     \
; setOffAddr# addr# i# n# (ctr x#) s#                             \
    = let { i = fromIntegral (I# i#)                              \
          ; n = fromIntegral (I# n#)                              \
          } in                                                    \
      case unsafeCoerce# (internal (set_addr addr# i n x#)) s# of \
        { (# s1#, _ #) -> s1# }                                   \
; {-# INLINE sizeOfType# #-}                                      \
; {-# INLINE alignmentOfType# #-}                                 \
; {-# INLINE indexByteArray# #-}                                  \
; {-# INLINE readByteArray# #-}                                   \
; {-# INLINE writeByteArray# #-}                                  \
; {-# INLINE setByteArray# #-}                                    \
; {-# INLINE indexOffAddr# #-}                                    \
; {-# INLINE readOffAddr# #-}                                     \
; {-# INLINE writeOffAddr# #-}                                    \
; {-# INLINE setOffAddr# #-}                                      \
}

#if __GLASGOW_HASKELL__ >= 902
liberate# :: State# s -> State# r
liberate# = unsafeCoerce#
shimmedSetWord8Array# :: MutableByteArray# s -> Int -> Int -> Word8# -> IO ()
shimmedSetWord8Array# m (I# off) (I# len) w = IO (\s -> (# liberate# (GHC.Exts.setByteArray# m off len (GHC.Exts.word2Int# (GHC.Exts.word8ToWord# w)) (liberate# s)), () #))
shimmedSetInt8Array# :: MutableByteArray# s -> Int -> Int -> Int8# -> IO ()
shimmedSetInt8Array# m (I# off) (I# len) i = IO (\s -> (# liberate# (GHC.Exts.setByteArray# m off len (GHC.Exts.int8ToInt# i) (liberate# s)), () #))
#else
liberate# :: State# s -> State# r
liberate# = unsafeCoerce#
shimmedSetWord8Array# :: MutableByteArray# s -> Int -> Int -> Word# -> IO ()
shimmedSetWord8Array# m (I# off) (I# len) w = IO (\s -> (# liberate# (GHC.Exts.setByteArray# m off len (GHC.Exts.word2Int# w) (liberate# s)), () #))
shimmedSetInt8Array# :: MutableByteArray# s -> Int -> Int -> Int# -> IO ()
shimmedSetInt8Array# m (I# off) (I# len) i = IO (\s -> (# liberate# (GHC.Exts.setByteArray# m off len i (liberate# s)), () #))
#endif

unI# :: Int -> Int#
unI# (I# n#) = n#

derivePrim(Word, W#, sIZEOF_WORD, aLIGNMENT_WORD,
           indexWordArray#, readWordArray#, writeWordArray#, setWordArray#,
           indexWordOffAddr#, readWordOffAddr#, writeWordOffAddr#, setWordOffAddr#)
derivePrim(Word8, W8#, sIZEOF_WORD8, aLIGNMENT_WORD8,
           indexWord8Array#, readWord8Array#, writeWord8Array#, shimmedSetWord8Array#,
           indexWord8OffAddr#, readWord8OffAddr#, writeWord8OffAddr#, setWord8OffAddr#)
derivePrim(Word16, W16#, sIZEOF_WORD16, aLIGNMENT_WORD16,
           indexWord16Array#, readWord16Array#, writeWord16Array#, setWord16Array#,
           indexWord16OffAddr#, readWord16OffAddr#, writeWord16OffAddr#, setWord16OffAddr#)
derivePrim(Word32, W32#, sIZEOF_WORD32, aLIGNMENT_WORD32,
           indexWord32Array#, readWord32Array#, writeWord32Array#, setWord32Array#,
           indexWord32OffAddr#, readWord32OffAddr#, writeWord32OffAddr#, setWord32OffAddr#)
derivePrim(Word64, W64#, sIZEOF_WORD64, aLIGNMENT_WORD64,
           indexWord64Array#, readWord64Array#, writeWord64Array#, setWord64Array#,
           indexWord64OffAddr#, readWord64OffAddr#, writeWord64OffAddr#, setWord64OffAddr#)
derivePrim(Int, I#, sIZEOF_INT, aLIGNMENT_INT,
           indexIntArray#, readIntArray#, writeIntArray#, setIntArray#,
           indexIntOffAddr#, readIntOffAddr#, writeIntOffAddr#, setIntOffAddr#)
derivePrim(Int8, I8#, sIZEOF_INT8, aLIGNMENT_INT8,
           indexInt8Array#, readInt8Array#, writeInt8Array#, shimmedSetInt8Array#,
           indexInt8OffAddr#, readInt8OffAddr#, writeInt8OffAddr#, setInt8OffAddr#)
derivePrim(Int16, I16#, sIZEOF_INT16, aLIGNMENT_INT16,
           indexInt16Array#, readInt16Array#, writeInt16Array#, setInt16Array#,
           indexInt16OffAddr#, readInt16OffAddr#, writeInt16OffAddr#, setInt16OffAddr#)
derivePrim(Int32, I32#, sIZEOF_INT32, aLIGNMENT_INT32,
           indexInt32Array#, readInt32Array#, writeInt32Array#, setInt32Array#,
           indexInt32OffAddr#, readInt32OffAddr#, writeInt32OffAddr#, setInt32OffAddr#)
derivePrim(Int64, I64#, sIZEOF_INT64, aLIGNMENT_INT64,
           indexInt64Array#, readInt64Array#, writeInt64Array#, setInt64Array#,
           indexInt64OffAddr#, readInt64OffAddr#, writeInt64OffAddr#, setInt64OffAddr#)
derivePrim(Float, F#, sIZEOF_FLOAT, aLIGNMENT_FLOAT,
           indexFloatArray#, readFloatArray#, writeFloatArray#, setFloatArray#,
           indexFloatOffAddr#, readFloatOffAddr#, writeFloatOffAddr#, setFloatOffAddr#)
derivePrim(Double, D#, sIZEOF_DOUBLE, aLIGNMENT_DOUBLE,
           indexDoubleArray#, readDoubleArray#, writeDoubleArray#, setDoubleArray#,
           indexDoubleOffAddr#, readDoubleOffAddr#, writeDoubleOffAddr#, setDoubleOffAddr#)
derivePrim(Char, C#, sIZEOF_CHAR, aLIGNMENT_CHAR,
           indexWideCharArray#, readWideCharArray#, writeWideCharArray#, setWideCharArray#,
           indexWideCharOffAddr#, readWideCharOffAddr#, writeWideCharOffAddr#, setWideCharOffAddr#)
derivePrim(Ptr a, Ptr, sIZEOF_PTR, aLIGNMENT_PTR,
           indexAddrArray#, readAddrArray#, writeAddrArray#, setAddrArray#,
           indexAddrOffAddr#, readAddrOffAddr#, writeAddrOffAddr#, setAddrOffAddr#)
derivePrim(StablePtr a, StablePtr, sIZEOF_PTR, aLIGNMENT_PTR,
           indexStablePtrArray#, readStablePtrArray#, writeStablePtrArray#, setStablePtrArray#,
           indexStablePtrOffAddr#, readStablePtrOffAddr#, writeStablePtrOffAddr#, setStablePtrOffAddr#)
derivePrim(FunPtr a, FunPtr, sIZEOF_PTR, aLIGNMENT_PTR,
           indexAddrArray#, readAddrArray#, writeAddrArray#, setAddrArray#,
           indexAddrOffAddr#, readAddrOffAddr#, writeAddrOffAddr#, setAddrOffAddr#)

-- Prim instances for newtypes in Foreign.C.Types
deriving instance Prim CChar
deriving instance Prim CSChar
deriving instance Prim CUChar
deriving instance Prim CShort
deriving instance Prim CUShort
deriving instance Prim CInt
deriving instance Prim CUInt
deriving instance Prim CLong
deriving instance Prim CULong
deriving instance Prim CPtrdiff
deriving instance Prim CSize
deriving instance Prim CWchar
deriving instance Prim CSigAtomic
deriving instance Prim CLLong
deriving instance Prim CULLong
deriving instance Prim CBool
deriving instance Prim CIntPtr
deriving instance Prim CUIntPtr
deriving instance Prim CIntMax
deriving instance Prim CUIntMax
deriving instance Prim CClock
deriving instance Prim CTime
deriving instance Prim CUSeconds
deriving instance Prim CSUSeconds
deriving instance Prim CFloat
deriving instance Prim CDouble

-- Prim instances for newtypes in System.Posix.Types
#if defined(HTYPE_DEV_T)
deriving instance Prim CDev
#endif
#if defined(HTYPE_INO_T)
deriving instance Prim CIno
#endif
#if defined(HTYPE_MODE_T)
deriving instance Prim CMode
#endif
#if defined(HTYPE_OFF_T)
deriving instance Prim COff
#endif
#if defined(HTYPE_PID_T)
deriving instance Prim CPid
#endif
#if defined(HTYPE_SSIZE_T)
deriving instance Prim CSsize
#endif
#if defined(HTYPE_GID_T)
deriving instance Prim CGid
#endif
#if defined(HTYPE_NLINK_T)
deriving instance Prim CNlink
#endif
#if defined(HTYPE_UID_T)
deriving instance Prim CUid
#endif
#if defined(HTYPE_CC_T)
deriving instance Prim CCc
#endif
#if defined(HTYPE_SPEED_T)
deriving instance Prim CSpeed
#endif
#if defined(HTYPE_TCFLAG_T)
deriving instance Prim CTcflag
#endif
#if defined(HTYPE_RLIM_T)
deriving instance Prim CRLim
#endif
#if defined(HTYPE_BLKSIZE_T)
deriving instance Prim CBlkSize
#endif
#if defined(HTYPE_BLKCNT_T)
deriving instance Prim CBlkCnt
#endif
#if defined(HTYPE_CLOCKID_T)
deriving instance Prim CClockId
#endif
#if defined(HTYPE_FSBLKCNT_T)
deriving instance Prim CFsBlkCnt
#endif
#if defined(HTYPE_FSFILCNT_T)
deriving instance Prim CFsFilCnt
#endif
#if defined(HTYPE_ID_T)
deriving instance Prim CId
#endif
#if defined(HTYPE_KEY_T)
deriving instance Prim CKey
#endif
#if defined(HTYPE_TIMER_T)
deriving instance Prim CTimer
#endif
deriving instance Prim Fd

-- Andrew Martin: The instances for WordPtr and IntPtr are written out by
-- hand in a tedious way. We cannot use GND because the data constructors for
-- these types were not available before GHC 8.2. The CPP for generating code
-- for the Int and Word types does not work here. There is a way to clean this
-- up a little with CPP, and if anyone wants to do that, go for it. In the
-- meantime, I am going to ship this with the instances written out by hand.

-- | @since 0.7.1.0
instance Prim WordPtr where
  sizeOfType# _ = sizeOfType# (Proxy :: Proxy (Ptr ()))
  alignmentOfType# _ = alignmentOfType# (Proxy :: Proxy (Ptr ()))
  indexByteArray# a i = ptrToWordPtr (indexByteArray# a i)
  readByteArray# a i s0 = case readByteArray# a i s0 of
    (# s1, p #) -> (# s1, ptrToWordPtr p #)
  writeByteArray# a i wp = writeByteArray# a i (wordPtrToPtr wp)
  setByteArray# a i n wp = setByteArray# a i n (wordPtrToPtr wp)
  indexOffAddr# a i = ptrToWordPtr (indexOffAddr# a i)
  readOffAddr# a i s0 = case readOffAddr# a i s0 of
    (# s1, p #) -> (# s1, ptrToWordPtr p #)
  writeOffAddr# a i wp = writeOffAddr# a i (wordPtrToPtr wp)
  setOffAddr# a i n wp = setOffAddr# a i n (wordPtrToPtr wp)

-- | @since 0.7.1.0
instance Prim IntPtr where
  sizeOfType# _ = sizeOfType# (Proxy :: Proxy (Ptr ()))
  alignmentOfType# _ = alignmentOfType# (Proxy :: Proxy (Ptr ()))
  indexByteArray# a i = ptrToIntPtr (indexByteArray# a i)
  readByteArray# a i s0 = case readByteArray# a i s0 of
    (# s1, p #) -> (# s1, ptrToIntPtr p #)
  writeByteArray# a i wp = writeByteArray# a i (intPtrToPtr wp)
  setByteArray# a i n wp = setByteArray# a i n (intPtrToPtr wp)
  indexOffAddr# a i = ptrToIntPtr (indexOffAddr# a i)
  readOffAddr# a i s0 = case readOffAddr# a i s0 of
    (# s1, p #) -> (# s1, ptrToIntPtr p #)
  writeOffAddr# a i wp = writeOffAddr# a i (intPtrToPtr wp)
  setOffAddr# a i n wp = setOffAddr# a i n (intPtrToPtr wp)

-- | @since 0.6.5.0
deriving instance Prim a => Prim (Const a b)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Down a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Identity a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Monoid.Dual a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Monoid.Sum a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Monoid.Product a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Semigroup.First a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Semigroup.Last a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Semigroup.Min a)
-- | @since 0.6.5.0
deriving instance Prim a => Prim (Semigroup.Max a)