{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Ouroboros.Consensus.Block.NestedContent (
HasNestedContent (..)
, NestedCtxt_
, curriedNest
, NestedCtxt (..)
, castNestedCtxt
, mapNestedCtxt
, castSomeNestedCtxt
, mapSomeNestedCtxt
, module Ouroboros.Consensus.Util.DepPair
, SomeSecond (..)
) where
import Data.Kind (Type)
import Data.Maybe (isJust)
import Data.Proxy
import Data.Type.Equality
import Data.Typeable (Typeable)
import NoThunks.Class (InspectHeap (..), NoThunks)
import Ouroboros.Consensus.Util (SomeSecond (..))
import Ouroboros.Consensus.Util.DepPair
class ( forall a. Show (NestedCtxt_ blk f a)
, SameDepIndex (NestedCtxt_ blk f)
) => HasNestedContent f blk where
unnest :: f blk -> DepPair (NestedCtxt f blk)
nest :: DepPair (NestedCtxt f blk) -> f blk
default unnest :: ( TrivialDependency (NestedCtxt f blk)
, TrivialIndex (NestedCtxt f blk) ~ f blk
)
=> f blk -> DepPair (NestedCtxt f blk)
unnest = NestedCtxt f blk (f blk) -> f blk -> DepPair (NestedCtxt f blk)
forall (f :: * -> *) a. f a -> a -> DepPair f
DepPair NestedCtxt f blk (f blk)
NestedCtxt f blk (TrivialIndex (NestedCtxt f blk))
forall (f :: * -> *). TrivialDependency f => f (TrivialIndex f)
indexIsTrivial
default nest :: ( TrivialDependency (NestedCtxt f blk)
, TrivialIndex (NestedCtxt f blk) ~ f blk
)
=> DepPair (NestedCtxt f blk) -> f blk
nest (DepPair NestedCtxt f blk a
x a
y) = NestedCtxt f blk a -> a -> TrivialIndex (NestedCtxt f blk)
forall (f :: * -> *) a.
TrivialDependency f =>
f a -> a -> TrivialIndex f
fromTrivialDependency NestedCtxt f blk a
x a
y
curriedNest :: HasNestedContent f blk => NestedCtxt f blk a -> a -> f blk
curriedNest :: forall (f :: * -> *) blk a.
HasNestedContent f blk =>
NestedCtxt f blk a -> a -> f blk
curriedNest NestedCtxt f blk a
ctxt a
a = DepPair (NestedCtxt f blk) -> f blk
forall (f :: * -> *) blk.
HasNestedContent f blk =>
DepPair (NestedCtxt f blk) -> f blk
nest (NestedCtxt f blk a -> a -> DepPair (NestedCtxt f blk)
forall (f :: * -> *) a. f a -> a -> DepPair f
DepPair NestedCtxt f blk a
ctxt a
a)
data family NestedCtxt_ blk :: (Type -> Type) -> (Type -> Type)
newtype NestedCtxt f blk a = NestedCtxt {
forall (f :: * -> *) blk a.
NestedCtxt f blk a -> NestedCtxt_ blk f a
flipNestedCtxt :: NestedCtxt_ blk f a
}
deriving instance Show (NestedCtxt_ blk f a)
=> Show (NestedCtxt f blk a)
instance SameDepIndex (NestedCtxt_ blk f)
=> SameDepIndex (NestedCtxt f blk) where
sameDepIndex :: forall a b.
NestedCtxt f blk a -> NestedCtxt f blk b -> Maybe (a :~: b)
sameDepIndex (NestedCtxt NestedCtxt_ blk f a
ctxt) (NestedCtxt NestedCtxt_ blk f b
ctxt') =
NestedCtxt_ blk f a -> NestedCtxt_ blk f b -> Maybe (a :~: b)
forall a b.
NestedCtxt_ blk f a -> NestedCtxt_ blk f b -> Maybe (a :~: b)
forall (f :: * -> *) a b.
SameDepIndex f =>
f a -> f b -> Maybe (a :~: b)
sameDepIndex NestedCtxt_ blk f a
ctxt NestedCtxt_ blk f b
ctxt'
instance TrivialDependency (NestedCtxt_ blk f)
=> TrivialDependency (NestedCtxt f blk) where
type TrivialIndex (NestedCtxt f blk) = TrivialIndex (NestedCtxt_ blk f)
hasSingleIndex :: forall a b. NestedCtxt f blk a -> NestedCtxt f blk b -> a :~: b
hasSingleIndex (NestedCtxt NestedCtxt_ blk f a
ctxt) (NestedCtxt NestedCtxt_ blk f b
ctxt') =
NestedCtxt_ blk f a -> NestedCtxt_ blk f b -> a :~: b
forall a b. NestedCtxt_ blk f a -> NestedCtxt_ blk f b -> a :~: b
forall (f :: * -> *) a b.
TrivialDependency f =>
f a -> f b -> a :~: b
hasSingleIndex NestedCtxt_ blk f a
ctxt NestedCtxt_ blk f b
ctxt'
indexIsTrivial :: NestedCtxt f blk (TrivialIndex (NestedCtxt f blk))
indexIsTrivial =
NestedCtxt_ blk f (TrivialIndex (NestedCtxt_ blk f))
-> NestedCtxt f blk (TrivialIndex (NestedCtxt_ blk f))
forall (f :: * -> *) blk a.
NestedCtxt_ blk f a -> NestedCtxt f blk a
NestedCtxt NestedCtxt_ blk f (TrivialIndex (NestedCtxt_ blk f))
forall (f :: * -> *). TrivialDependency f => f (TrivialIndex f)
indexIsTrivial
castNestedCtxt :: (NestedCtxt_ blk f a -> NestedCtxt_ blk' f a)
-> NestedCtxt f blk a
-> NestedCtxt f blk' a
castNestedCtxt :: forall blk (f :: * -> *) a blk'.
(NestedCtxt_ blk f a -> NestedCtxt_ blk' f a)
-> NestedCtxt f blk a -> NestedCtxt f blk' a
castNestedCtxt NestedCtxt_ blk f a -> NestedCtxt_ blk' f a
coerce (NestedCtxt NestedCtxt_ blk f a
ctxt) = NestedCtxt_ blk' f a -> NestedCtxt f blk' a
forall (f :: * -> *) blk a.
NestedCtxt_ blk f a -> NestedCtxt f blk a
NestedCtxt (NestedCtxt_ blk f a -> NestedCtxt_ blk' f a
coerce NestedCtxt_ blk f a
ctxt)
mapNestedCtxt :: (NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a')
-> NestedCtxt f blk a
-> NestedCtxt f' blk' a'
mapNestedCtxt :: forall blk (f :: * -> *) a blk' (f' :: * -> *) a'.
(NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a')
-> NestedCtxt f blk a -> NestedCtxt f' blk' a'
mapNestedCtxt NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a'
f (NestedCtxt NestedCtxt_ blk f a
ctxt) = NestedCtxt_ blk' f' a' -> NestedCtxt f' blk' a'
forall (f :: * -> *) blk a.
NestedCtxt_ blk f a -> NestedCtxt f blk a
NestedCtxt (NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a'
f NestedCtxt_ blk f a
ctxt)
deriving instance (HasNestedContent f blk, forall a. Show (g a))
=> Show (GenDepPair g (NestedCtxt f blk))
deriving instance HasNestedContent f blk => Show (SomeSecond (NestedCtxt f) blk)
deriving via InspectHeap (SomeSecond (NestedCtxt f) blk)
instance (Typeable f, Typeable blk) => NoThunks (SomeSecond (NestedCtxt f) blk)
instance SameDepIndex (NestedCtxt_ blk f)
=> Eq (SomeSecond (NestedCtxt f) blk) where
SomeSecond NestedCtxt f blk b
ctxt == :: SomeSecond (NestedCtxt f) blk
-> SomeSecond (NestedCtxt f) blk -> Bool
== SomeSecond NestedCtxt f blk b
ctxt' = Maybe (b :~: b) -> Bool
forall a. Maybe a -> Bool
isJust (NestedCtxt f blk b -> NestedCtxt f blk b -> Maybe (b :~: b)
forall a b.
NestedCtxt f blk a -> NestedCtxt f blk b -> Maybe (a :~: b)
forall (f :: * -> *) a b.
SameDepIndex f =>
f a -> f b -> Maybe (a :~: b)
sameDepIndex NestedCtxt f blk b
ctxt NestedCtxt f blk b
ctxt')
castSomeNestedCtxt :: (forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f a)
-> SomeSecond (NestedCtxt f) blk
-> SomeSecond (NestedCtxt f) blk'
castSomeNestedCtxt :: forall blk (f :: * -> *) blk'.
(forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f a)
-> SomeSecond (NestedCtxt f) blk -> SomeSecond (NestedCtxt f) blk'
castSomeNestedCtxt forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f a
coerce (SomeSecond NestedCtxt f blk b
ctxt) = NestedCtxt f blk' b -> SomeSecond (NestedCtxt f) blk'
forall {k1} {k2} (f :: k1 -> k2 -> *) (a :: k1) (b :: k2).
f a b -> SomeSecond f a
SomeSecond ((NestedCtxt_ blk f b -> NestedCtxt_ blk' f b)
-> NestedCtxt f blk b -> NestedCtxt f blk' b
forall blk (f :: * -> *) a blk'.
(NestedCtxt_ blk f a -> NestedCtxt_ blk' f a)
-> NestedCtxt f blk a -> NestedCtxt f blk' a
castNestedCtxt NestedCtxt_ blk f b -> NestedCtxt_ blk' f b
forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f a
coerce NestedCtxt f blk b
ctxt)
mapSomeNestedCtxt :: (forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a)
-> SomeSecond (NestedCtxt f) blk
-> SomeSecond (NestedCtxt f') blk'
mapSomeNestedCtxt :: forall blk (f :: * -> *) blk' (f' :: * -> *).
(forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a)
-> SomeSecond (NestedCtxt f) blk -> SomeSecond (NestedCtxt f') blk'
mapSomeNestedCtxt forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a
f (SomeSecond NestedCtxt f blk b
ctxt) = NestedCtxt f' blk' b -> SomeSecond (NestedCtxt f') blk'
forall {k1} {k2} (f :: k1 -> k2 -> *) (a :: k1) (b :: k2).
f a b -> SomeSecond f a
SomeSecond ((NestedCtxt_ blk f b -> NestedCtxt_ blk' f' b)
-> NestedCtxt f blk b -> NestedCtxt f' blk' b
forall blk (f :: * -> *) a blk' (f' :: * -> *) a'.
(NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a')
-> NestedCtxt f blk a -> NestedCtxt f' blk' a'
mapNestedCtxt NestedCtxt_ blk f b -> NestedCtxt_ blk' f' b
forall a. NestedCtxt_ blk f a -> NestedCtxt_ blk' f' a
f NestedCtxt f blk b
ctxt)