如何嵌套未装箱的载体?

haz*_*haz 4 arrays haskell vector

这可能是一个非常基本的问题,但我已经四处寻找,似乎无法找到答案.

我想使用未装箱的矢量来表示2D列表.使用法线向量很容易做到这一点:

> import qualified Data.Vector as V
> V.fromList [V.fromList [1..5]]
[[1,2,3,4,5]]
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用未装箱的矢量:

> import qualified Data.Vector.Unboxed as U
> U.fromList [U.fromList [1..5]]
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

• Non type-variable argument
    in the constraint: U.Unbox (U.Vector a)
  (Use FlexibleContexts to permit this)
• When checking the inferred type
    it :: forall a.
          (U.Unbox (U.Vector a), U.Unbox a, Num a, Enum a) =>
          U.Vector (U.Vector a)
Run Code Online (Sandbox Code Playgroud)

我怀疑它与此有关:

> V.fromList [1..5]
[1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)

> U.fromList [1..5]
[1.0,2.0,3.0,4.0,5.0]
Run Code Online (Sandbox Code Playgroud)

但我似乎无法理解如何避免这种情况.

提前致谢!

chi*_*chi 5

好吧,我们可以开始遵循编译器给出的建议:

> :set -XFlexibleContexts
Run Code Online (Sandbox Code Playgroud)

然而:

> U.fromList [U.fromList [1..5]]

<interactive>:10:1: error:
    • No instance for (U.Unbox (U.Vector a0))
        arising from a use of ‘print’
Run Code Online (Sandbox Code Playgroud)

这里的问题是您无法取消装箱矢量(即使它是未装箱的数据).要取消装箱数据类型,您需要精确描述字节布局,包括其大小.但是矢量没有大小.考虑例如

U.fromList [U.fromList [1..5], U.fromList [1..7]]
Run Code Online (Sandbox Code Playgroud)

即使在C中,这也需要一个"锯齿状"矩阵,通常用指针(=拳击)表示.