From 89f14f77489d1be05aa71991382b01fae6d29a1e Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 19 May 2024 16:59:46 +0200 Subject: Expose all IsList(toList) functions without constraints Some are Foldable.toList, some are a separate top-level function --- src/Data/Array/Nested/Internal.hs | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'src/Data/Array/Nested') diff --git a/src/Data/Array/Nested/Internal.hs b/src/Data/Array/Nested/Internal.hs index 19e6545..ab5f015 100644 --- a/src/Data/Array/Nested/Internal.hs +++ b/src/Data/Array/Nested/Internal.hs @@ -43,7 +43,7 @@ import Control.Monad.ST import qualified Data.Array.RankedS as S import Data.Bifunctor (first) import Data.Coerce (coerce, Coercible) -import Data.Foldable (toList) +import Data.Foldable as Foldable (toList) import Data.Functor.Const import Data.Kind import Data.List.NonEmpty (NonEmpty(..)) @@ -241,23 +241,19 @@ instance KnownNat n => IsList (ListR n i) where go SZ [] = ZR go (SS n) (i : is) = i ::: go n is go _ _ = error "IsList(ListR): Mismatched list length" - toList = go - where - go :: ListR n' i -> [i] - go ZR = [] - go (i ::: is) = i : go is + toList = Foldable.toList -- | Untyped: length is checked at runtime. instance KnownNat n => IsList (IxR n i) where type Item (IxR n i) = i fromList = IxR . IsList.fromList - toList (IxR idx) = IsList.toList idx + toList = Foldable.toList -- | Untyped: length is checked at runtime. instance KnownNat n => IsList (ShR n i) where type Item (ShR n i) = i fromList = ShR . IsList.fromList - toList (ShR idx) = IsList.toList idx + toList = Foldable.toList type role ListS nominal representational @@ -293,6 +289,10 @@ showListS f l = showString "[" . go "" l . showString "]" go _ ZS = id go prefix (x ::$ xs) = showString prefix . f x . go "," xs +listSToList :: ListS sh (Const i) -> [i] +listSToList ZS = [] +listSToList (Const i ::$ is) = i : listSToList is + -- | An index into a shape-typed array. -- @@ -356,6 +356,10 @@ instance Show (ShS sh) where lengthShS :: ShS sh -> Int lengthShS (ShS l) = getSum (foldListS (\_ -> Sum 1) l) +shSToList :: ShS sh -> [Int] +shSToList ZSS = [] +shSToList (sn :$$ sh) = fromSNat' sn : shSToList sh + -- | Untyped: length is checked at runtime. instance KnownShS sh => IsList (ListS sh (Const i)) where @@ -368,17 +372,13 @@ instance KnownShS sh => IsList (ListS sh (Const i)) where go _ _ = error $ "IsList(ListS): Mismatched list length (type says " ++ show (lengthShS (knownShS @sh)) ++ ", list has length " ++ show (length topl) ++ ")" - toList = go - where - go :: ListS sh' (Const i) -> [i] - go ZS = [] - go (Const i ::$ is) = i : go is + toList = listSToList -- | Very untyped: only length is checked (at runtime), index bounds are __not checked__. instance KnownShS sh => IsList (IxS sh i) where type Item (IxS sh i) = i fromList = IxS . IsList.fromList - toList (IxS idx) = IsList.toList idx + toList = Foldable.toList -- | Untyped: length and values are checked at runtime. instance KnownShS sh => IsList (ShS sh) where @@ -394,11 +394,7 @@ instance KnownShS sh => IsList (ShS sh) where go _ _ = error $ "IsList(ShS): Mismatched list length (type says " ++ show (lengthShS (knownShS @sh)) ++ ", list has length " ++ show (length topl) ++ ")" - toList = go - where - go :: ShS sh' -> [Int] - go ZSS = [] - go (sn :$$ sh) = fromSNat' sn : go sh + toList = shSToList -- | Wrapper type used as a tag to attach instances on. The instances on arrays -- cgit v1.2.3-70-g09d2