module Darcs.Patch.Witnesses.Sealed
( Sealed(..)
, seal
, unseal
, mapSeal
, unsafeUnseal
, unsafeUnsealFlipped
, unsafeUnseal2
, Sealed2(..)
, seal2
, unseal2
, mapSeal2
, FlippedSeal(..)
, flipSeal
, unsealFlipped
, mapFlipped
, unsealM
, liftSM
, Gap(..)
, FreeLeft
, unFreeLeft
, FreeRight
, unFreeRight
) where
import Prelude ()
import Darcs.Prelude
import Darcs.Patch.Witnesses.Eq ( MyEq, EqCheck(..) )
import Darcs.Patch.Witnesses.Show
import Darcs.Patch.Witnesses.Eq ( (=\/=) )
import Darcs.Patch.Witnesses.Unsafe ( unsafeCoerceP1, unsafeCoerceP )
data Sealed a where
Sealed :: a wX -> Sealed a
seal :: a wX -> Sealed a
seal = Sealed
instance MyEq a => Eq (Sealed (a wX)) where
Sealed x == Sealed y | IsEq <- x =\/= y = True
| otherwise = False
data Sealed2 a where
Sealed2 :: !(a wX wY) -> Sealed2 a
seal2 :: a wX wY -> Sealed2 a
seal2 = Sealed2
data FlippedSeal a wY where
FlippedSeal :: !(a wX wY) -> FlippedSeal a wY
flipSeal :: a wX wY -> FlippedSeal a wY
flipSeal = FlippedSeal
unsafeUnseal :: Sealed a -> a wX
unsafeUnseal (Sealed a) = unsafeCoerceP1 a
unsafeUnsealFlipped :: FlippedSeal a wY -> a wX wY
unsafeUnsealFlipped (FlippedSeal a) = unsafeCoerceP a
unsafeUnseal2 :: Sealed2 a -> a wX wY
unsafeUnseal2 (Sealed2 a) = unsafeCoerceP a
unseal :: (forall wX . a wX -> b) -> Sealed a -> b
unseal f x = f (unsafeUnseal x)
unsealM :: Monad m => m (Sealed a) -> (forall wX . a wX -> m b) -> m b
unsealM m1 m2 = do sx <- m1
unseal m2 sx
liftSM :: Monad m => (forall wX . a wX -> b) -> m (Sealed a) -> m b
liftSM f m = do sx <- m
return (unseal f sx)
mapSeal :: (forall wX . a wX -> b wX) -> Sealed a -> Sealed b
mapSeal f = unseal (seal . f)
mapFlipped :: (forall wX . a wX wY -> b wX wZ) -> FlippedSeal a wY -> FlippedSeal b wZ
mapFlipped f (FlippedSeal x) = FlippedSeal (f x)
unseal2 :: (forall wX wY . a wX wY -> b) -> Sealed2 a -> b
unseal2 f a = f (unsafeUnseal2 a)
mapSeal2 :: (forall wX wY . a wX wY -> b wX wY) -> Sealed2 a -> Sealed2 b
mapSeal2 f = unseal2 (seal2 . f)
unsealFlipped :: (forall wX wY . a wX wY -> b) -> FlippedSeal a wZ -> b
unsealFlipped f (FlippedSeal a) = f a
instance Show1 a => Show (Sealed a) where
showsPrec d (Sealed x) = showParen (d > appPrec) $ showString "Sealed " . showsPrec1 (appPrec + 1) x
instance Show2 a => Show (Sealed2 a) where
showsPrec d (Sealed2 x) = showParen (d > appPrec) $ showString "Sealed2 " . showsPrec2 (appPrec + 1) x
newtype Poly a = Poly { unPoly :: forall wX . a wX }
newtype Stepped (f :: (* -> *) -> *) a wX = Stepped { unStepped :: f (a wX) }
newtype FreeLeft p = FLInternal (Poly (Stepped Sealed p))
newtype FreeRight p = FRInternal (Poly (FlippedSeal p))
unFreeLeft :: FreeLeft p -> Sealed (p wX)
unFreeLeft (FLInternal x) = unStepped (unPoly x)
unFreeRight :: FreeRight p -> FlippedSeal p wX
unFreeRight (FRInternal x) = unPoly x
class Gap w where
emptyGap :: (forall wX . p wX wX) -> w p
freeGap :: (forall wX wY . p wX wY) -> w p
joinGap :: (forall wX wY wZ . p wX wY -> q wY wZ -> r wX wZ) -> w p -> w q -> w r
instance Gap FreeLeft where
emptyGap e = FLInternal (Poly (Stepped (Sealed e)))
freeGap e = FLInternal (Poly (Stepped (Sealed e)))
joinGap op (FLInternal p) (FLInternal q)
= FLInternal (Poly (case unPoly p of Stepped (Sealed p') -> case unPoly q of Stepped (Sealed q') -> Stepped (Sealed (p' `op` q'))))
instance Gap FreeRight where
emptyGap e = FRInternal (Poly (FlippedSeal e))
freeGap e = FRInternal (Poly (FlippedSeal e))
joinGap op (FRInternal p) (FRInternal q)
= FRInternal (Poly (case unPoly q of FlippedSeal q' -> case unPoly p of FlippedSeal p' -> FlippedSeal (p' `op` q')))