diff options
| author | Tom Smeding <tom@tomsmeding.com> | 2024-05-19 16:59:46 +0200 | 
|---|---|---|
| committer | Tom Smeding <tom@tomsmeding.com> | 2024-05-19 16:59:46 +0200 | 
| commit | 89f14f77489d1be05aa71991382b01fae6d29a1e (patch) | |
| tree | 09b5e8179edc61e7b27bd7bf72c5c5e1e497e357 /src/Data/Array/Nested | |
| parent | 2bfd35243211d2acbc35629d448d27a51a9112bc (diff) | |
Expose all IsList(toList) functions without constraints
Some are Foldable.toList, some are a separate top-level function
Diffstat (limited to 'src/Data/Array/Nested')
| -rw-r--r-- | src/Data/Array/Nested/Internal.hs | 34 | 
1 files changed, 15 insertions, 19 deletions
| 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 | 
