From 85593969debadbf11ad3c159de71e7b480ca367c Mon Sep 17 00:00:00 2001
From: Tom Smeding <tom@tomsmeding.com>
Date: Sat, 25 May 2024 21:37:38 +0200
Subject: Choose behaviour for rerank of empty array

This works around an undocumented runtime error in orthotope.
---
 src/Data/Array/Nested/Internal.hs | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

(limited to 'src/Data/Array/Nested')

diff --git a/src/Data/Array/Nested/Internal.hs b/src/Data/Array/Nested/Internal.hs
index a440ccc..fb2ba0b 100644
--- a/src/Data/Array/Nested/Internal.hs
+++ b/src/Data/Array/Nested/Internal.hs
@@ -961,6 +961,7 @@ mrerankP ssh sh2 f (M_Primitive sh arr) =
                            (\a -> let M_Primitive _ r = f (M_Primitive sh1 a) in r)
                            arr)
 
+-- | See the caveats at @X.rerank@.
 mrerank :: forall sh1 sh2 sh a b. (PrimElt a, PrimElt b)
         => StaticShX sh -> IShX sh2
         -> (Mixed sh1 a -> Mixed sh2 b)
@@ -1582,6 +1583,29 @@ rrerankP sn sh2 f (Ranked arr)
                      (\a -> let Ranked r = f (Ranked a) in r)
                      arr)
 
+-- | If there is a zero-sized dimension in the @n@-prefix of the shape of the
+-- input array, then there is no way to deduce the full shape of the output
+-- array (more precisely, the @n2@ part): that could only come from calling
+-- @f@, and there are no subarrays to call @f@ on. @orthotope@ errors out in
+-- this case; we choose to fill the @n2@ part of the output shape with zeros.
+--
+-- For example, if:
+--
+-- @
+-- arr :: Ranked 5 Int   -- of shape [3, 0, 4, 2, 21]
+-- f :: Ranked 2 Int -> Ranked 3 Float
+-- @
+--
+-- then:
+--
+-- @
+-- rrerank _ _ _ f arr :: Ranked 5 Float
+-- @
+--
+-- and this result will have shape @[3, 0, 4, 0, 0, 0]@. Note that the
+-- "reranked" part (the last 3 entries) are zero; we don't know if @f@ intended
+-- to return an array with shape all-0 here (it probably didn't), but there is
+-- no better number to put here absent a subarray of the input to pass to @f@.
 rrerank :: forall n1 n2 n a b. (PrimElt a, PrimElt b)
          => SNat n -> IShR n2
          -> (Ranked n1 a -> Ranked n2 b)
-- 
cgit v1.2.3-70-g09d2