From 6f41fd676865a14572ef4570919632fbfb4a04f1 Mon Sep 17 00:00:00 2001 From: Mikolaj Konarski Date: Sun, 11 May 2025 20:14:10 +0200 Subject: Add zip and zipWith for sized lists --- src/Data/Array/Mixed/Shape.hs | 19 +++++++++++++++++ src/Data/Array/Nested/Internal/Shape.hs | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) (limited to 'src/Data/Array') diff --git a/src/Data/Array/Mixed/Shape.hs b/src/Data/Array/Mixed/Shape.hs index b49e005..4dd0aa6 100644 --- a/src/Data/Array/Mixed/Shape.hs +++ b/src/Data/Array/Mixed/Shape.hs @@ -152,6 +152,19 @@ listxLast :: forall f n sh. ListX (n : sh) f -> f (Last (n : sh)) listxLast (_ ::% sh@(_ ::% _)) = listxLast sh listxLast (x ::% ZX) = x +listxZip :: ListX sh (Const i) -> ListX sh (Const j) -> ListX sh (Const (i, j)) +listxZip ZX ZX = ZX +listxZip (Const i ::% irest) (Const j ::% jrest) = + Const (i, j) ::% listxZip irest jrest +--listxZip _ _ = error "listxZip: impossible pattern needlessly required" + +listxZipWith :: (i -> j -> k) -> ListX sh (Const i) -> ListX sh (Const j) + -> ListX sh (Const k) +listxZipWith _ ZX ZX = ZX +listxZipWith f (Const i ::% irest) (Const j ::% jrest) = + Const (f i j) ::% listxZipWith f irest jrest +--listxZipWith _ _ _ = error "listxZipWith: impossible pattern needlessly required" + -- * Mixed indices @@ -222,6 +235,12 @@ ixxInit = coerce (listxInit @(Const i)) ixxLast :: forall n sh i. IxX (n : sh) i -> i ixxLast = coerce (listxLast @(Const i)) +ixxZip :: IxX n i -> IxX n j -> IxX n (i, j) +ixxZip (IxX l1) (IxX l2) = IxX $ listxZip l1 l2 + +ixxZipWith :: (i -> j -> k) -> IxX n i -> IxX n j -> IxX n k +ixxZipWith f (IxX l1) (IxX l2) = IxX $ listxZipWith f l1 l2 + ixxFromLinear :: IShX sh -> Int -> IIxX sh ixxFromLinear = \sh i -> case go sh i of (idx, 0) -> idx diff --git a/src/Data/Array/Nested/Internal/Shape.hs b/src/Data/Array/Nested/Internal/Shape.hs index 5d5f8e3..102d9d8 100644 --- a/src/Data/Array/Nested/Internal/Shape.hs +++ b/src/Data/Array/Nested/Internal/Shape.hs @@ -135,6 +135,18 @@ listrIndex SZ (x ::: _) = x listrIndex (SS i) (_ ::: xs) | Refl <- lemLeqSuccSucc (Proxy @k) (Proxy @n) = listrIndex i xs listrIndex _ ZR = error "k + 1 <= 0" +listrZip :: ListR n i -> ListR n j -> ListR n (i, j) +listrZip ZR ZR = ZR +listrZip (i ::: irest) (j ::: jrest) = (i, j) ::: listrZip irest jrest +listrZip _ _ = error "listrZip: impossible pattern needlessly required" + +listrZipWith :: (i -> j -> k) -> ListR n i -> ListR n j -> ListR n k +listrZipWith _ ZR ZR = ZR +listrZipWith f (i ::: irest) (j ::: jrest) = + f i j ::: listrZipWith f irest jrest +listrZipWith _ _ _ = + error "listrZipWith: impossible pattern needlessly required" + listrPermutePrefix :: forall i n. [Int] -> ListR n i -> ListR n i listrPermutePrefix = \perm sh -> listrFromList perm $ \sperm -> @@ -222,6 +234,12 @@ ixrLast (IxR list) = listrLast list ixrAppend :: forall n m i. IxR n i -> IxR m i -> IxR (n + m) i ixrAppend = coerce (listrAppend @_ @i) +ixrZip :: IxR n i -> IxR n j -> IxR n (i, j) +ixrZip (IxR l1) (IxR l2) = IxR $ listrZip l1 l2 + +ixrZipWith :: (i -> j -> k) -> IxR n i -> IxR n j -> IxR n k +ixrZipWith f (IxR l1) (IxR l2) = IxR $ listrZipWith f l1 l2 + ixrPermutePrefix :: forall n i. [Int] -> IxR n i -> IxR n i ixrPermutePrefix = coerce (listrPermutePrefix @i) @@ -434,6 +452,19 @@ listsAppend :: ListS sh f -> ListS sh' f -> ListS (sh ++ sh') f listsAppend ZS idx' = idx' listsAppend (i ::$ idx) idx' = i ::$ listsAppend idx idx' +listsZip :: ListS sh (Const i) -> ListS sh (Const j) -> ListS sh (Const (i, j)) +listsZip ZS ZS = ZS +listsZip (Const i ::$ irest) (Const j ::$ jrest) = + Const (i, j) ::$ listsZip irest jrest +--listsZip _ _ = error "listsZip: impossible pattern needlessly required" + +listsZipWith :: (i -> j -> k) -> ListS sh (Const i) -> ListS sh (Const j) + -> ListS sh (Const k) +listsZipWith _ ZS ZS = ZS +listsZipWith f (Const i ::$ irest) (Const j ::$ jrest) = + Const (f i j) ::$ listsZipWith f irest jrest +--listsZipWith _ _ _ = error "listsZipWith: impossible pattern needlessly required" + listsTakeLenPerm :: forall f is sh. Perm is -> ListS sh f -> ListS (TakeLen is sh) f listsTakeLenPerm PNil _ = ZS listsTakeLenPerm (_ `PCons` is) (n ::$ sh) = n ::$ listsTakeLenPerm is sh @@ -530,6 +561,12 @@ ixsLast (IxS list) = getConst (listsLast list) ixsAppend :: forall sh sh' i. IxS sh i -> IxS sh' i -> IxS (sh ++ sh') i ixsAppend = coerce (listsAppend @_ @(Const i)) +ixsZip :: IxS n i -> IxS n j -> IxS n (i, j) +ixsZip (IxS l1) (IxS l2) = IxS $ listsZip l1 l2 + +ixsZipWith :: (i -> j -> k) -> IxS n i -> IxS n j -> IxS n k +ixsZipWith f (IxS l1) (IxS l2) = IxS $ listsZipWith f l1 l2 + ixsPermutePrefix :: forall i is sh. Perm is -> IxS sh i -> IxS (PermutePrefix is sh) i ixsPermutePrefix = coerce (listsPermutePrefix @(Const i)) -- cgit v1.2.3-70-g09d2