Skip to content

Commit 8376343

Browse files
jasagredoamesgen
authored andcommitted
Define some Index functions in terms of SOP combinators
1 parent fa023d3 commit 8376343

9 files changed

Lines changed: 46 additions & 47 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Non-Breaking
2+
3+
- Define some functions in terms of SOP combinators leveraging the recent
4+
simplification of `Index` in `sop-extras` package.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Abstract/SingleEraBlock.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ eraIndexEmpty (EraIndex ns) = case ns of {}
159159
eraIndexFromNS :: SListI xs => NS f xs -> EraIndex xs
160160
eraIndexFromNS = EraIndex . hmap (const (K ()))
161161

162-
eraIndexFromIndex :: Index xs blk -> EraIndex xs
162+
eraIndexFromIndex :: SListI xs => Index xs blk -> EraIndex xs
163163
eraIndexFromIndex index = EraIndex $ injectNS index (K ())
164164

165165
eraIndexZero :: EraIndex (x ': xs)

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Block.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ distribAnnTip AnnTip{..} =
205205
distrib (WrapTipInfo info) =
206206
AnnTip annTipSlotNo annTipBlockNo info
207207

208-
undistribAnnTip :: SListI xs => NS AnnTip xs -> AnnTip (HardForkBlock xs)
208+
undistribAnnTip :: forall xs. SListI xs => NS AnnTip xs -> AnnTip (HardForkBlock xs)
209209
undistribAnnTip = hcollapse . himap undistrib
210210
where
211211
undistrib :: Index xs blk

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Embed/Nary.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ module Ouroboros.Consensus.HardFork.Combinator.Embed.Nary (
2525
import Data.Bifunctor (first)
2626
import Data.Coerce (Coercible, coerce)
2727
import Data.SOP.BasicFunctors
28+
import Data.SOP.Constraint
2829
import Data.SOP.Counting (Exactly (..))
2930
import Data.SOP.Dict (Dict (..))
3031
import Data.SOP.Index
3132
import qualified Data.SOP.InPairs as InPairs
3233
import Data.SOP.Strict
34+
import qualified Data.SOP.Telescope as Telescope
3335
import Ouroboros.Consensus.Block
3436
import Ouroboros.Consensus.Config
3537
import Ouroboros.Consensus.HardFork.Combinator
@@ -75,11 +77,9 @@ newtype InjectionIndex xs x =
7577

7678
-- | Many instances of 'Inject' do not need the 'History.Bound's, eg those that
7779
-- do not construct 'HardForkState's
78-
forgetInjectionIndex :: InjectionIndex xs x -> Index xs x
79-
forgetInjectionIndex (InjectionIndex tele) = case tele of
80-
TZ (State.Current _k Refl) -> IZ
81-
TS _k tele' ->
82-
IS (forgetInjectionIndex (InjectionIndex tele'))
80+
forgetInjectionIndex :: SListI xs => InjectionIndex xs x -> Index xs x
81+
forgetInjectionIndex (InjectionIndex tele) =
82+
Index $ hmap State.currentState $ Telescope.tip tele
8383

8484
-- | Build an 'InjectionIndex' from oracular 'History.Bound's and an 'Index'
8585
--
@@ -89,18 +89,17 @@ forgetInjectionIndex (InjectionIndex tele) = case tele of
8989
-- INVARIANT: the result is completely independent of the 'history.Bound's for
9090
-- eras /after/ the given 'Index'.
9191
oracularInjectionIndex ::
92-
Exactly xs History.Bound
92+
SListI xs
93+
=> Exactly xs History.Bound
9394
-> Index xs x
9495
-> InjectionIndex xs x
95-
oracularInjectionIndex (Exactly np) idx = case (idx, np) of
96-
(IZ , K start :* _ ) ->
97-
InjectionIndex
98-
$ TZ State.Current { currentStart = start, currentState = Refl }
99-
(IS idx', kstart :* kstarts) ->
100-
let InjectionIndex iidx =
101-
oracularInjectionIndex (Exactly kstarts) idx'
102-
in
103-
InjectionIndex (TS kstart iidx)
96+
oracularInjectionIndex (Exactly np) (Index idx) =
97+
InjectionIndex
98+
$ Telescope.bihzipWith
99+
(\x (K ()) -> x)
100+
(\(K start) Refl -> State.Current { currentStart = start, currentState = Refl })
101+
np
102+
$ Telescope.fromTip idx
104103

105104
-- | NOT EXPORTED
106105
--

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Ledger.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ instance CanHardFork xs => IsLedger (LedgerState (HardForkBlock xs)) where
157157
extended :: HardForkState LedgerState xs
158158
extended = State.extendToSlot cfg slot st
159159

160-
tickOne :: SingleEraBlock blk
160+
tickOne :: (SListI xs, SingleEraBlock blk)
161161
=> EpochInfo (Except PastHorizonException)
162162
-> SlotNo
163163
-> ComputeLedgerEvents
@@ -207,7 +207,7 @@ instance CanHardFork xs
207207
error "reapplyBlockLedgerResult: can't be from other era"
208208
)
209209

210-
apply :: SingleEraBlock blk
210+
apply :: (SListI xs, SingleEraBlock blk)
211211
=> STS.ValidationPolicy
212212
-> ComputeLedgerEvents
213213
-> Index xs blk
@@ -728,14 +728,14 @@ ledgerViewInfo :: forall blk f. SingleEraBlock blk
728728
=> f blk -> LedgerEraInfo blk
729729
ledgerViewInfo _ = LedgerEraInfo $ singleEraInfo (Proxy @blk)
730730

731-
injectLedgerError :: Index xs blk -> LedgerError blk -> HardForkLedgerError xs
731+
injectLedgerError :: SListI xs => Index xs blk -> LedgerError blk -> HardForkLedgerError xs
732732
injectLedgerError index =
733733
HardForkLedgerErrorFromEra
734734
. OneEraLedgerError
735735
. injectNS index
736736
. WrapLedgerErr
737737

738-
injectLedgerEvent :: Index xs blk -> AuxLedgerEvent (LedgerState blk) -> OneEraLedgerEvent xs
738+
injectLedgerEvent :: SListI xs => Index xs blk -> AuxLedgerEvent (LedgerState blk) -> OneEraLedgerEvent xs
739739
injectLedgerEvent index =
740740
OneEraLedgerEvent
741741
. injectNS index

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Mempool.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,14 @@ ledgerInfo :: forall blk. SingleEraBlock blk
354354
=> State.Current (Ticked :.: LedgerState) blk -> LedgerEraInfo blk
355355
ledgerInfo _ = LedgerEraInfo $ singleEraInfo (Proxy @blk)
356356

357-
injectApplyTxErr :: Index xs blk -> ApplyTxErr blk -> HardForkApplyTxErr xs
357+
injectApplyTxErr :: SListI xs => Index xs blk -> ApplyTxErr blk -> HardForkApplyTxErr xs
358358
injectApplyTxErr index =
359359
HardForkApplyTxErrFromEra
360360
. OneEraApplyTxErr
361361
. injectNS index
362362
. WrapApplyTxErr
363363

364-
injectValidatedGenTx :: Index xs blk -> Validated (GenTx blk) -> Validated (GenTx (HardForkBlock xs))
364+
injectValidatedGenTx :: SListI xs => Index xs blk -> Validated (GenTx blk) -> Validated (GenTx (HardForkBlock xs))
365365
injectValidatedGenTx index =
366366
HardForkValidatedGenTx
367367
. OneEraValidatedGenTx

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Protocol.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Ouroboros.Consensus.HardFork.Combinator.Protocol (
3131
import Control.Monad.Except
3232
import Data.Functor.Product
3333
import Data.SOP.BasicFunctors
34+
import Data.SOP.Constraint
3435
import Data.SOP.Index
3536
import Data.SOP.InPairs (InPairs (..))
3637
import qualified Data.SOP.InPairs as InPairs
@@ -304,7 +305,7 @@ update HardForkConsensusConfig{..}
304305
where
305306
cfgs = getPerEraConsensusConfig hardForkConsensusConfigPerEra
306307

307-
updateEra :: forall xs blk. SingleEraBlock blk
308+
updateEra :: forall xs blk. (SListI xs, SingleEraBlock blk)
308309
=> EpochInfo (Except PastHorizonException)
309310
-> SlotNo
310311
-> Index xs blk
@@ -378,7 +379,8 @@ translateConsensus ei HardForkConsensusConfig{..} =
378379
pcfgs = getPerEraConsensusConfig hardForkConsensusConfigPerEra
379380
cfgs = hcmap proxySingle (completeConsensusConfig'' ei) pcfgs
380381

381-
injectValidationErr :: Index xs blk
382+
injectValidationErr :: SListI xs
383+
=> Index xs blk
382384
-> ValidationErr (BlockProtocol blk)
383385
-> HardForkValidationErr xs
384386
injectValidationErr index =

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Serialisation/Common.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ encodeNS es ns = mconcat [
446446
, hcollapse $ hzipWith apFn es ns
447447
]
448448

449-
decodeNS :: SListI xs => NP (Decoder s :.: f) xs -> Decoder s (NS f xs)
449+
decodeNS :: forall xs f s. SListI xs => NP (Decoder s :.: f) xs -> Decoder s (NS f xs)
450450
decodeNS ds = do
451451
enforceSize "decodeNS" 2
452452
i <- Dec.decodeWord8
@@ -460,7 +460,7 @@ decodeNS ds = do
460460
-> K (Decoder s (NS f xs)) blk
461461
aux index (Comp dec) (K ()) = K $ injectNS index <$> dec
462462

463-
decodeAnnNS :: SListI xs
463+
decodeAnnNS :: forall xs f. SListI xs
464464
=> NP (AnnDecoder f) xs
465465
-> forall s. Decoder s (Lazy.ByteString -> NS f xs)
466466
decodeAnnNS ds = do

sop-extras/src/Data/SOP/Index.hs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414

1515
module Data.SOP.Index (
1616
-- * Indexing SOP types
17-
Index (..)
17+
Index (.., IS, IZ)
1818
, dictIndexAll
1919
, indices
2020
, injectNS
2121
, injectNS'
22-
, pattern IS
23-
, pattern IZ
2422
, projectNP
2523
-- * Zipping with indices
2624
, hcimap
@@ -69,26 +67,22 @@ pattern IS z <- Index (S (Index -> z)) where
6967
indices :: forall xs. SListI xs => NP (Index xs) xs
7068
indices = case sList @xs of
7169
SNil -> Nil
72-
SCons -> IZ :* hmap (Index . S . getIndex) indices
70+
SCons -> IZ :* hmap IS indices
7371

74-
dictIndexAll :: All c xs => Proxy c -> Index xs x -> Dict c x
75-
dictIndexAll p = \case
76-
IZ -> Dict
77-
IS idx -> dictIndexAll p idx
72+
dictIndexAll :: forall c xs x. All c xs => Proxy c -> Index xs x -> Dict c x
73+
dictIndexAll p (Index idx) = hcollapse $ hcmap p (\Refl -> K Dict) idx
7874

79-
injectNS :: forall f x xs. Index xs x -> f x -> NS f xs
80-
injectNS idx x = case idx of
81-
IZ -> Z x
82-
IS idx' -> S (injectNS idx' x)
75+
injectNS :: forall f x xs. All Top xs => Index xs x -> f x -> NS f xs
76+
injectNS (Index idx) x = hmap (\Refl -> x) idx
8377

8478
injectNS' ::
85-
forall f a b x xs. (Coercible a (f x), Coercible b (NS f xs))
79+
forall f a b x xs. All Top xs => (Coercible a (f x), Coercible b (NS f xs))
8680
=> Proxy f -> Index xs x -> a -> b
8781
injectNS' _ idx = coerce . injectNS @f idx . coerce
8882

89-
projectNP :: Index xs x -> NP f xs -> f x
90-
projectNP IZ (x :* _ ) = x
91-
projectNP (IS idx) (_ :* xs) = projectNP idx xs
83+
projectNP :: All Top xs => Index xs x -> NP f xs -> f x
84+
projectNP (Index idx) np =
85+
hcollapse $ hliftA2 (\f Refl -> K f) np idx
9286

9387
{-------------------------------------------------------------------------------
9488
Zipping with indices
@@ -214,9 +208,9 @@ nsFromIndex n = go 0 sList
214208
go !_ SNil = Nothing
215209

216210
toWord8 :: Index xs x -> Word8
217-
toWord8 = go 0
211+
toWord8 = go 0 . getIndex
218212
where
219-
go :: Word8 -> Index ys y -> Word8
213+
go :: Word8 -> NS ((:~:) y) ys -> Word8
220214
go !n = \case
221-
IZ -> n
222-
IS idx -> go (n + 1) idx
215+
Z Refl -> n
216+
S idx -> go (n + 1) idx

0 commit comments

Comments
 (0)