gpt4 book ai didi

haskell - 寻找 «instance (Enum a, Bounded a) => IArray UArray a»

转载 作者:行者123 更新时间:2023-12-01 11:53:55 24 4
gpt4 key购买 nike

我正在寻找一种方法来拥有 Enum a => UArray a(这对我来说很有意义,因为我们可以简单地将枚举映射到 Int 并通过 toEnumfromEnum)

到目前为止,我试图从 Data.Array.Base 窃取 UArray Int 的代码并在各处走私一些 toEnumfromEnum:

{-# LANGUAGE MagicHash, UnboxedTuples #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}

module UArrays where

import Data.Array.Base
import Data.Array.ST
import Data.Array.Unboxed

import GHC.Base -- (Int(I#), Int#(..))
import GHC.Prim -- (indexIntArray#, readIntArray#, writeIntArray#)
import GHC.ST (ST(..), runST)

import Unsafe.Coerce

instance (Enum a, Bounded a) => IArray UArray a where
{-# INLINE bounds #-}
bounds (UArray l u _ _) = (l, u)
{-# INLINE numElements #-}
numElements (UArray _ _ n _) = n
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies minBound)
{-# INLINE unsafeAt #-}
unsafeAt (UArray _ _ _ arr#) (I# i#) =
I# $ fromEnum (indexIntArray# arr# i#)
{-# INLINE unsafeReplace #-}
unsafeReplace arr ies = runST (unsafeReplaceUArray arr ies)
{-# INLINE unsafeAccum #-}
unsafeAccum f arr ies = runST (unsafeAccumUArray f arr ies)
{-# INLINE unsafeAccumArray #-}
unsafeAccumArray f initialValue lu ies =
runST (unsafeAccumArrayUArray f initialValue lu ies)

-- data STUArray s i e = STUArray !i !i !Int (GHC.Prim.MutableByteArray# s)
instance (Enum a, Bounded a) => MArray (STUArray s) a (ST s) where
{-# INLINE getBounds #-}
getBounds (STUArray l u _ _) = return (l, u)
{-# INLINE getNumElements #-}
getNumElements (STUArray _ _ n _) = return n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_ (l, u) = unsafeNewArraySTUArray_ (l, u) wORD_SCALE
{-# INLINE newArray_ #-}
newArray_ arrBounds = newArray arrBounds minBound
{-# INLINE unsafeRead #-}
-- unsafeRead :: GHC.Arr.Ix i => a i e -> Int -> m e
unsafeRead (STUArray _ _ _ marr#) (I# i#) =
ST $ \ s1# ->
case readIntArray# marr# i# s1# of
(# s2#, e# #) -> (# s2#, I# (toEnum e#) #)
{-# INLINE unsafeWrite #-}
-- unsafeWrite :: GHC.Arr.Ix i => a i e -> Int -> e -> m ()
unsafeWrite (STUArray _ _ _ marr#) (I# i#) (I# e#) =
ST $ \ s1# ->
case writeIntArray# marr# (unsafeCoerce i#) (I# $ fromEnum e#) s1# of
s2# -> (# s2#, () #)

当然它不能编译:

[2 of 4] Compiling UArrays          ( UArrays.hs, interpreted )

UArrays.hs:27:14:
Couldn't match expected type `Int#' with actual type `Int'
In the return type of a call of `fromEnum'
In the second argument of `($)', namely
`fromEnum (indexIntArray# arr# i#)'
In the expression: I# $ fromEnum (indexIntArray# arr# i#)

UArrays.hs:52:45:
Couldn't match expected type `Int' with actual type `Int#'
In the first argument of `toEnum', namely `e#'
In the first argument of `I#', namely `(toEnum e#)'
In the expression: I# (toEnum e#)

UArrays.hs:57:57:
Couldn't match expected type `Int#' with actual type `Int'
In the return type of a call of `fromEnum'
In the second argument of `($)', namely `fromEnum e#'
In the third argument of `writeIntArray#', namely
`(I# $ fromEnum e#)'
Failed, modules loaded: Utils.

GHC.* 中也没有神奇的unboxInt::Int -> Int#I# 上的模式匹配也没有't yield Int 而是一个 Int#,但不知何故 UArray Int 存在并在 Int# 上工作。

我还找到了一篇关于 making an UArray for newtypes 的帖子,但它似乎并不适用,因为它依赖于 unsafeCoerce。我试过了,但它产生了一个有趣的 listArray (0, 54) $ cycle [Red, Yellow, Green],其中所有构造函数都是 Blue

我走错路了吗?

更新:

现在可以使用了,这是源代码:

最佳答案

您需要意识到 Int 是一个装箱整数,它是通过构造函数 I# 从未装箱的整数 Int# 构造的。所以:

-- These functions aren't practical; they just demonstrate the types.
unboxInt :: Int -> Int#
unboxInt (I# unboxed) = unboxed

boxInt :: Int# -> Int
boxInt = I#

因为 fromEnum 已经返回了一个装箱的整数,你不必通过再次应用 I# 来重新装箱它,所以在这个代码片段中,例如:

{-I# $-} fromEnum (indexIntArray# arr# i#)

...您可以简单地省略 I# 构造函数。同样,当使用 toEnum 时,您应该应用 I# 构造函数从未装箱的整数中获取装箱的整数。

正如@leftaroundabout 提到的,这种装箱和拆箱加上 fromEnumtoEnum 可能具有的复杂性(特别是对于元组等)可能会导致性能下降到使用简单的盒装 Array

关于haskell - 寻找 «instance (Enum a, Bounded a) => IArray UArray a»,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8941386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com