Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions some.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ library
Data.Some.GADT
Data.Some.Newtype

-- Proxy for base >= 4.7.0
if impl(ghc >= 7.8.2)
exposed-modules:
Data.GADT.Coerce

other-modules: Data.GADT.Internal
build-depends:
base >=4.3 && <4.18
Expand Down
107 changes: 107 additions & 0 deletions src/Data/GADT/Coerce.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
#if __GLASGOW_HASKELL__ >= 810
{-# LANGUAGE StandaloneKindSignatures #-}
#endif
module Data.GADT.Coerce (
GCoercible (..),
defaultGcoercible,
) where

import Data.Functor.Product (Product (..))
import Data.Functor.Sum (Sum (..))
import Data.IORef (IORef)
import Data.STRef (STRef)
import Data.Type.Equality ((:~:) (..))
import Data.Type.Coercion

import Unsafe.Coerce (unsafeCoerce)

import Data.GADT.Compare (GEq, geq)

#if MIN_VERSION_base(4,10,0)
import Data.Type.Equality ((:~~:) (..))
import qualified Type.Reflection as TR
#endif

#if __GLASGOW_HASKELL__ >= 810
import Data.Kind (Type, Constraint)
#endif

-- |A class for type-contexts which contain enough information
-- to (at least in some cases) decide the coercibility of types
-- occurring within them.
#if __GLASGOW_HASKELL__ >= 810
type GCoercible :: (k -> Type) -> Constraint
#endif
class GCoercible f where
gcoercible :: f a -> f b -> Maybe (Coercion a b)

-- |If 'f' has a 'GEq' instance, this function makes a suitable default
-- implementation of 'gcoercible'.
defaultGcoercible :: GEq f => f a -> f b -> Maybe (Coercion a b)
defaultGcoercible x y = fmap repr $ geq x y

instance GCoercible ((:~:) a) where
gcoercible = defaultGcoercible

#if MIN_VERSION_base(4,10,0)
instance GCoercible ((:~~:) a) where
gcoercible = defaultGcoercible

instance GCoercible TR.TypeRep where
gcoercible = defaultGcoercible
#endif

instance (GCoercible a, GCoercible b) => GCoercible (Sum a b) where
gcoercible (InL x) (InL y) = gcoercible x y
gcoercible (InR x) (InR y) = gcoercible x y
gcoercible _ _ = Nothing

instance (GCoercible a, GCoercible b) => GCoercible (Product a b) where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird (also in existing GEq instance. Either side would be enough, won't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? It is possible for one to fail and the other to succeed, is it not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For TestEquality yes only one is sufficient.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was thinking of TestEquality, and it doesn't seem to have instance for Product so all is fine.

(btw, that's another point not to have TestEquality as a super-class for GEq).

gcoercible (Pair x y) (Pair x' y') = do
Coercion <- gcoercible x x'
Coercion <- gcoercible y y'
return Coercion

instance GCoercible IORef where
gcoercible x y =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be more welcome to add this and STRef instance if the implementation for this function were in base, and I could use Safe (or at least Trustworthy) interface. (This package takes Safe Haskell seriously for time being).

Turns out there is for heterogenous equality for StableName (eqStableName), so figuring out coercion based on STRef equality kind of belongs to base as well.

As maintainer I'll be happy if burden of tracking GHC internals is in GHC-provided lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had the same thought. The unsafe function should be where the implementation is, as those must be kept in sync?

if x == unsafeCoerce y
then Just $ unsafeCoerce $ repr Refl
else Nothing

instance GCoercible (STRef s) where
gcoercible x y =
if x == unsafeCoerce y
then Just $ unsafeCoerce $ repr Refl
else Nothing

-- This instance seems nice, but it's simply not right:
--
-- > instance GCoercible StableName where
-- > gcoercible sn1 sn2
-- > | sn1 == unsafeCoerce sn2
-- > = Just (unsafeCoerce Refl)
-- > | otherwise = Nothing
--
-- Proof:
--
-- > x <- makeStableName id :: IO (StableName (Int -> Int))
-- > y <- makeStableName id :: IO (StableName ((Int -> Int) -> Int -> Int))
-- >
-- > let Just boom = gcoercible x y
-- >
-- > Data.Type.Coercion.coerceWith boom (const 0) id 0
-- > let "Illegal Instruction" = "QED."
--
-- The core of the problem is that 'makeStableName' only knows the closure it is
-- passed to, not any type information. Together with the fact that the same
-- closure has the same 'StableName' each time 'makeStableName' is called on it,
-- there is potential for abuse when a closure can be given many incompatible
-- types.
--
-- 'GCoericble' gets us closer than GEq, but the problem is Coercions state that
-- *all* values can be coerced, but due to polymophism it is quite easy to find
-- situations where some values of a type are safe to coerce and others are not.
-- We just need one such value to abuse 'GCoercible StableName'.
26 changes: 0 additions & 26 deletions src/Data/GADT/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -321,32 +321,6 @@ instance GEq TR.TypeRep where
-- GCompare
-------------------------------------------------------------------------------

-- This instance seems nice, but it's simply not right:
--
-- > instance GEq StableName where
-- > geq sn1 sn2
-- > | sn1 == unsafeCoerce sn2
-- > = Just (unsafeCoerce Refl)
-- > | otherwise = Nothing
--
-- Proof:
--
-- > x <- makeStableName id :: IO (StableName (Int -> Int))
-- > y <- makeStableName id :: IO (StableName ((Int -> Int) -> Int -> Int))
-- >
-- > let Just boom = geq x y
-- > let coerce :: (a :~: b) -> a -> b; coerce Refl = id
-- >
-- > coerce boom (const 0) id 0
-- > let "Illegal Instruction" = "QED."
--
-- The core of the problem is that 'makeStableName' only knows the closure
-- it is passed to, not any type information. Together with the fact that
-- the same closure has the same StableName each time 'makeStableName' is
-- called on it, there is serious potential for abuse when a closure can
-- be given many incompatible types.


-- |A type for the result of comparing GADT constructors; the type parameters
-- of the GADT values being compared are included so that in the case where
-- they are equal their parameter types can be unified.
Expand Down