diff options
| author | Mikolaj Konarski <mikolaj.konarski@funktory.com> | 2026-04-04 12:09:08 +0200 |
|---|---|---|
| committer | Mikolaj Konarski <mikolaj.konarski@funktory.com> | 2026-04-04 12:09:08 +0200 |
| commit | 53bc096c807fc63cfe5f1102131b475deb5e21ca (patch) | |
| tree | 63115ff12a4bb5cb656164718163b885f7b91d27 /src/Data | |
| parent | a9ac62f66e45e64f83043e0ebda04f0b4b80b913 (diff) | |
Remove almost all unsafeCoerce
Diffstat (limited to 'src/Data')
| -rw-r--r-- | src/Data/Array/Nested/Convert.hs | 8 | ||||
| -rw-r--r-- | src/Data/Array/Nested/Ranked/Shape.hs | 10 | ||||
| -rw-r--r-- | src/Data/Array/Nested/Shaped/Shape.hs | 11 |
3 files changed, 11 insertions, 18 deletions
diff --git a/src/Data/Array/Nested/Convert.hs b/src/Data/Array/Nested/Convert.hs index d6bdc6a..155a1a7 100644 --- a/src/Data/Array/Nested/Convert.hs +++ b/src/Data/Array/Nested/Convert.hs @@ -57,10 +57,7 @@ import Data.Array.Nested.Types -- * To ranked --- TODO: change all those unsafeCoerces into coerces by defining shaped --- and ranked index types as newtypes of the mixed index type --- and similarly for the sized lists or, preferably, by defining --- all as newtypes over [], exploiting fusion and getting free toList. +-- This unsafeCoerce, exceptionally, can't be eliminated using newtype coercions. ixrFromIxS :: IxS sh i -> IxR (Rank sh) i ixrFromIxS = unsafeCoerce @@ -83,8 +80,9 @@ shrFromShX = coerce -- * To shaped +-- This unsafeCoerce, exceptionally, can't be eliminated using newtype coercions. ixsFromIxR :: IxR (Rank sh) i -> IxS sh i -ixsFromIxR = unsafeCoerce -- TODO: switch to coerce once newtypes overhauled +ixsFromIxR = unsafeCoerce -- ixsFromIxX re-exported diff --git a/src/Data/Array/Nested/Ranked/Shape.hs b/src/Data/Array/Nested/Ranked/Shape.hs index b0fb30d..6d94c8d 100644 --- a/src/Data/Array/Nested/Ranked/Shape.hs +++ b/src/Data/Array/Nested/Ranked/Shape.hs @@ -38,7 +38,6 @@ import GHC.IsList (IsList) import GHC.IsList qualified as IsList import GHC.TypeLits import GHC.TypeNats qualified as TN -import Unsafe.Coerce (unsafeCoerce) import Data.Array.Nested.Lemmas import Data.Array.Nested.Mixed.Shape @@ -291,7 +290,7 @@ ixrToLinear :: Num i => IShR m -> IxR m i -> i ixrToLinear (ShR sh) ix = ixxToLinear sh (ixxFromIxR ix) ixxFromIxR :: IxR n i -> IxX (Replicate n Nothing) i -ixxFromIxR = unsafeCoerce -- TODO: switch to coerce once newtypes overhauled +ixxFromIxR = coerce {-# INLINEABLE ixrFromLinear #-} ixrFromLinear :: forall i m. Num i => IShR m -> Int -> IxR m i @@ -299,8 +298,8 @@ ixrFromLinear (ShR sh) i | Refl <- lemRankReplicate (Proxy @m) = ixrFromIxX $ ixxFromLinear sh i -ixrFromIxX :: IxX sh i -> IxR (Rank sh) i -ixrFromIxX = unsafeCoerce -- TODO: switch to coerce once newtypes overhauled +ixrFromIxX :: IxX (Replicate n Nothing) i -> IxR n i +ixrFromIxX = coerce shrEnum :: IShR n -> [IIxR n] shrEnum = shrEnum' @@ -309,8 +308,7 @@ shrEnum = shrEnum' shrEnum' :: forall i n. Num i => IShR n -> [IxR n i] shrEnum' (ShR sh) | Refl <- lemRankReplicate (Proxy @n) - = (unsafeCoerce :: [IxX (Replicate n Nothing) i] -> [IxR n i]) $ shxEnum' sh - -- TODO: switch to coerce once newtypes overhauled + = (coerce :: [IxX (Replicate n Nothing) i] -> [IxR n i]) $ shxEnum' sh -- * Ranked shapes diff --git a/src/Data/Array/Nested/Shaped/Shape.hs b/src/Data/Array/Nested/Shaped/Shape.hs index 97d1559..c39d482 100644 --- a/src/Data/Array/Nested/Shaped/Shape.hs +++ b/src/Data/Array/Nested/Shaped/Shape.hs @@ -36,7 +36,6 @@ import GHC.Exts (build, withDict) import GHC.IsList (IsList) import GHC.IsList qualified as IsList import GHC.TypeLits -import Unsafe.Coerce (unsafeCoerce) import Data.Array.Nested.Mixed.Shape import Data.Array.Nested.Permutation @@ -267,22 +266,21 @@ ixsToLinear :: Num i => ShS sh -> IxS sh i -> i ixsToLinear (ShS sh) ix = ixxToLinear sh (ixxFromIxS ix) ixxFromIxS :: IxS sh i -> IxX (MapJust sh) i -ixxFromIxS = unsafeCoerce -- TODO: switch to coerce once newtypes overhauled +ixxFromIxS = coerce {-# INLINEABLE ixsFromLinear #-} ixsFromLinear :: Num i => ShS sh -> Int -> IxS sh i ixsFromLinear (ShS sh) i = ixsFromIxX $ ixxFromLinear sh i ixsFromIxX :: IxX (MapJust sh) i -> IxS sh i -ixsFromIxX = unsafeCoerce -- TODO: switch to coerce once newtypes overhauled +ixsFromIxX = coerce shsEnum :: ShS sh -> [IIxS sh] shsEnum = shsEnum' {-# INLINABLE shsEnum' #-} -- ensure this can be specialised at use site shsEnum' :: Num i => ShS sh -> [IxS sh i] -shsEnum' (ShS sh) = (unsafeCoerce :: [IxX (MapJust sh) i] -> [IxS sh i]) $ shxEnum' sh - -- TODO: switch to coerce once newtypes overhauled +shsEnum' (ShS sh) = (coerce :: [IxX (MapJust sh) i] -> [IxS sh i]) $ shxEnum' sh -- * Shaped shapes @@ -346,8 +344,7 @@ shsLength (ShS shx) = shxLength shx shsRank :: forall sh. ShS sh -> SNat (Rank sh) shsRank (ShS shx) = - gcastWith (unsafeCoerceRefl - :: Rank (MapJust sh) :~: Rank sh) $ + gcastWith (unsafeCoerceRefl :: Rank (MapJust sh) :~: Rank sh) $ shxRank shx shsSize :: ShS sh -> Int |
