Hi!
I want to ask three questions:
1. What about to add the dimensions of an array to its type?
Now I write some complex code(using REPA) and sometimes runtime dimensional errors make me crazy due to difficulty finding causes.
I decided to write a wrapper:
newtype DimMatrix r (y :: Nat) (x :: Nat) a
= DimMatrix { getInternal :: Matrix r a}
Now I can use it, even if I don't know its dimensions in runtime:
1) create DimMatrix in CPS style:
withMat :: Array D DIM2 Double -> (forall x y. (KnownNat x, KnownNat y) => DimMatrix D x y Double -> k) -> k
2) Write a complex function with its prerequisites, written as Constraint:
someComplexFunction
:: forall y1 x1 y2 x2.
( HasCallStack
, AllConstrained KnownNat [x1, x2, y1, y2]
, x2 ~ d
, y1 ~ y2
)
=> DimMatrix D y1 x1 Double
-> DimMatrix D y2 x2 Double
-> .....
3) Check the prerequisites. Unfortunately, it should be done at runtime and compiler can't check the quality of checking. But in the case of simple properties, it is ok.
And use the Evidence from the dimensions
library:
data Evidence :: Constraint -> Type where
E :: a => Evidence a
withEvidence :: Evidence a -> (a => r) -> r
withEvidence d r = case d of E -> r
And now I have nice compile-time error messages in case of my mistakes and even more - now compiler can remind me about forgotten prerequisites. Of course, it is just a fast ad-hoc solution, but I think that it can be generalized and made more convenient.
What if I'll write a wrapper (or even native) array with dimensions in its type? Would it be useful?
2. Mutable arrays. Now (if I understand it correctly) Accelerate doesn't support it because it can't check that result's size is equal to the input array's size.
But with the type-safe arrays, it can! I did not look deep into the Accelerate's internal code and want to ask - Are there any other reasons that prevent us from a mutable array implementation?
Is it a time-consuming task? Would it be useful?
I am asking that because I need to plan my time and don't want to spend it for something useless, but I would glad to make Accelerate library better! :-)
Thanks!