prim-uniq-0.2: Opaque unique identifiers in primitive state monads

Safe HaskellTrustworthy
LanguageHaskell98

Data.Unique.Tag

Synopsis

Documentation

data Tag s a Source #

The Tag type is like an ad-hoc GADT allowing runtime creation of new constructors. Specifically, it is like a GADT "enumeration" with one phantom type.

A Tag constructor can be generated in any primitive monad (but only tags from the same one can be compared). Every tag is equal to itself and to no other. The GOrdering class allows rediscovery of a tag's phantom type, so that Tags and values of type DSum (Tag s) can be tested for equality even when their types are not known to be equal.

Tag uses a Uniq as a witness of type equality, which is sound as long as the Uniq is truly unique and only one Tag is ever constructed from any given Uniq. The type of newTag enforces these conditions. veryUnsafeMkTag provides a way for adventurous (or malicious!) users to assert that they know better than the type system.

Instances
GShow (Tag RealWorld) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

gshowsPrec :: Int -> Tag RealWorld a -> ShowS #

GEq (Tag s :: Type -> Type) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

geq :: Tag s a -> Tag s b -> Maybe (a :~: b) #

GCompare (Tag s :: Type -> Type) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

gcompare :: Tag s a -> Tag s b -> GOrdering a b #

Eq (Tag s a) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

(==) :: Tag s a -> Tag s a -> Bool #

(/=) :: Tag s a -> Tag s a -> Bool #

Ord (Tag s a) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

compare :: Tag s a -> Tag s a -> Ordering #

(<) :: Tag s a -> Tag s a -> Bool #

(<=) :: Tag s a -> Tag s a -> Bool #

(>) :: Tag s a -> Tag s a -> Bool #

(>=) :: Tag s a -> Tag s a -> Bool #

max :: Tag s a -> Tag s a -> Tag s a #

min :: Tag s a -> Tag s a -> Tag s a #

Show (Tag RealWorld a) Source # 
Instance details

Defined in Unsafe.Unique.Tag

newTag :: PrimMonad m => m (Tag (PrimState m) a) Source #

Create a new tag witnessing a type a. The GEq or GOrdering instance can be used to discover type equality of two occurrences of the same tag.

(I'm not sure whether the recovery is sound if a is instantiated as a polymorphic type, so I'd advise caution if you intend to try it. I suspect it is, but I have not thought through it very deeply and certainly have not proved it.)

data RealWorld :: Type #

RealWorld is deeply magical. It is primitive, but it is not unlifted (hence ptrArg). We never manipulate values of type RealWorld; it's only used in the type system, to parameterise State#.

Instances
Show (Uniq RealWorld) Source #

There is only one RealWorld, so this instance is sound (unlike the general unsafeShowsPrecUniq). Note that there is no particular relationship between Uniq values (or the strings show turns them into) created in different executions of a program. The value they render to should be considered completely arbitrary, and the Show instance only even exists for convenience when testing code that uses Uniqs.

Instance details

Defined in Unsafe.Unique.Prim

GShow (Tag RealWorld) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

gshowsPrec :: Int -> Tag RealWorld a -> ShowS #

Show (Tag RealWorld a) Source # 
Instance details

Defined in Unsafe.Unique.Tag

data (a :: k) :~: (b :: k) :: forall k. k -> k -> Type where infix 4 #

Propositional equality. If a :~: b is inhabited by some terminating value, then the type a is the same as the type b. To use this equality in practice, pattern-match on the a :~: b to get out the Refl constructor; in the body of the pattern-match, the compiler knows that a ~ b.

Since: base-4.7.0.0

Constructors

Refl :: forall k (a :: k) (b :: k). a :~: a 
Instances
TestEquality ((:~:) a :: k -> Type)

Since: base-4.7.0.0

Instance details

Defined in Data.Type.Equality

Methods

testEquality :: (a :~: a0) -> (a :~: b) -> Maybe (a0 :~: b) #

GShow ((:~:) a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gshowsPrec :: Int -> (a :~: a0) -> ShowS #

GRead ((:~:) a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

greadsPrec :: Int -> GReadS ((:~:) a) #

GEq ((:~:) a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

geq :: (a :~: a0) -> (a :~: b) -> Maybe (a0 :~: b) #

GCompare ((:~:) a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: (a :~: a0) -> (a :~: b) -> GOrdering a0 b #

a ~ b => Bounded (a :~: b)

Since: base-4.7.0.0

Instance details

Defined in Data.Type.Equality

Methods

minBound :: a :~: b #

maxBound :: a :~: b #

a ~ b => Enum (a :~: b)

Since: base-4.7.0.0

Instance details

Defined in Data.Type.Equality

Methods

succ :: (a :~: b) -> a :~: b #

pred :: (a :~: b) -> a :~: b #

toEnum :: Int -> a :~: b #

fromEnum :: (a :~: b) -> Int #

enumFrom :: (a :~: b) -> [a :~: b] #

enumFromThen :: (a :~: b) -> (a :~: b) -> [a :~: b] #

enumFromTo :: (a :~: b) -> (a :~: b) -> [a :~: b] #

enumFromThenTo :: (a :~: b) -> (a :~: b) -> (a :~: b) -> [a :~: b] #

Eq (a :~: b)

Since: base-4.7.0.0

Instance details

Defined in Data.Type.Equality

Methods

(==) :: (a :~: b) -> (a :~: b) -> Bool #

(/=) :: (a :~: b) -> (a :~: b) -> Bool #

Ord (a :~: b)

Since: base-4.7.0.0

Instance details

Defined in Data.Type.Equality

Methods

compare :: (a :~: b) -> (a :~: b) -> Ordering #

(<) :: (a :~: b) -> (a :~: b) -> Bool #

(<=) :: (a :~: b) -> (a :~: b) -> Bool #

(>) :: (a :~: b) -> (a :~: b) -> Bool #

(>=) :: (a :~: b) -> (a :~: b) -> Bool #

max :: (a :~: b) -> (a :~: b) -> a :~: b #

min :: (a :~: b) -> (a :~: b) -> a :~: b #

a ~ b => Read (a :~: b)

Since: base-4.7.0.0

Instance details

Defined in Data.Type.Equality

Methods

readsPrec :: Int -> ReadS (a :~: b) #

readList :: ReadS [a :~: b] #

readPrec :: ReadPrec (a :~: b) #

readListPrec :: ReadPrec [a :~: b] #

Show (a :~: b)

Since: base-4.7.0.0

Instance details

Defined in Data.Type.Equality

Methods

showsPrec :: Int -> (a :~: b) -> ShowS #

show :: (a :~: b) -> String #

showList :: [a :~: b] -> ShowS #

class GEq (f :: k -> Type) where #

A class for type-contexts which contain enough information to (at least in some cases) decide the equality of types occurring within them.

Methods

geq :: f a -> f b -> Maybe (a :~: b) #

Produce a witness of type-equality, if one exists.

A handy idiom for using this would be to pattern-bind in the Maybe monad, eg.:

extract :: GEq tag => tag a -> DSum tag -> Maybe a
extract t1 (t2 :=> x) = do
    Refl <- geq t1 t2
    return x

Or in a list comprehension:

extractMany :: GEq tag => tag a -> [DSum tag] -> [a]
extractMany t1 things = [ x | (t2 :=> x) <- things, Refl <- maybeToList (geq t1 t2)]

(Making use of the DSum type from Data.Dependent.Sum in both examples)

Instances
GEq (TypeRep :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

geq :: TypeRep a -> TypeRep b -> Maybe (a :~: b) #

GEq ((:~:) a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

geq :: (a :~: a0) -> (a :~: b) -> Maybe (a0 :~: b) #

(GEq a, GEq b) => GEq (Sum a b :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

geq :: Sum a b a0 -> Sum a b b0 -> Maybe (a0 :~: b0) #

(GEq a, GEq b) => GEq (Product a b :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

geq :: Product a b a0 -> Product a b b0 -> Maybe (a0 :~: b0) #

GEq (Tag s :: Type -> Type) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

geq :: Tag s a -> Tag s b -> Maybe (a :~: b) #

data GOrdering (a :: k) (b :: k) :: forall k. k -> k -> Type where #

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.

Constructors

GLT :: forall k (a :: k) (b :: k). GOrdering a b 
GEQ :: forall k (a :: k) (b :: k). GOrdering a a 
GGT :: forall k (a :: k) (b :: k). GOrdering a b 
Instances
GShow (GOrdering a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gshowsPrec :: Int -> GOrdering a a0 -> ShowS #

GRead (GOrdering a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

greadsPrec :: Int -> GReadS (GOrdering a) #

Eq (GOrdering a b) 
Instance details

Defined in Data.GADT.Internal

Methods

(==) :: GOrdering a b -> GOrdering a b -> Bool #

(/=) :: GOrdering a b -> GOrdering a b -> Bool #

Ord (GOrdering a b) 
Instance details

Defined in Data.GADT.Internal

Methods

compare :: GOrdering a b -> GOrdering a b -> Ordering #

(<) :: GOrdering a b -> GOrdering a b -> Bool #

(<=) :: GOrdering a b -> GOrdering a b -> Bool #

(>) :: GOrdering a b -> GOrdering a b -> Bool #

(>=) :: GOrdering a b -> GOrdering a b -> Bool #

max :: GOrdering a b -> GOrdering a b -> GOrdering a b #

min :: GOrdering a b -> GOrdering a b -> GOrdering a b #

Show (GOrdering a b) 
Instance details

Defined in Data.GADT.Internal

Methods

showsPrec :: Int -> GOrdering a b -> ShowS #

show :: GOrdering a b -> String #

showList :: [GOrdering a b] -> ShowS #

class GEq f => GCompare (f :: k -> Type) where #

Type class for comparable GADT-like structures. When 2 things are equal, must return a witness that their parameter types are equal as well (GEQ).

Methods

gcompare :: f a -> f b -> GOrdering a b #

Instances
GCompare (TypeRep :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: TypeRep a -> TypeRep b -> GOrdering a b #

GCompare ((:~:) a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: (a :~: a0) -> (a :~: b) -> GOrdering a0 b #

(GCompare a, GCompare b) => GCompare (Sum a b :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: Sum a b a0 -> Sum a b b0 -> GOrdering a0 b0 #

(GCompare a, GCompare b) => GCompare (Product a b :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: Product a b a0 -> Product a b b0 -> GOrdering a0 b0 #

GCompare (Tag s :: Type -> Type) Source # 
Instance details

Defined in Unsafe.Unique.Tag

Methods

gcompare :: Tag s a -> Tag s b -> GOrdering a b #