1111{-# LANGUAGE ScopedTypeVariables #-}
1212{-# LANGUAGE TupleSections #-}
1313{-# LANGUAGE TypeApplications #-}
14+ {-# LANGUAGE TypeData #-}
1415{-# LANGUAGE TypeFamilies #-}
1516
1617module Cardano.Ledger.Binary.Decoding.Coders (
@@ -117,20 +118,20 @@ import Data.Void (Void)
117118-- fields). We use indexes to types to try and mark (and enforce) these distinctions.
118119
119120-- | Index for record density. Distinguishing (all the fields) from (some of the fields).
120- data Density = Dense | Sparse
121+ type data Density = Dense | Sparse
121122
122- -- | Index for a wrapped Coder. Wrapping is necessary for 'Summands' and ' SparseKeyed'.
123- data Wrapped where
123+ -- | Index for a wrapped Coder. Wrapping is necessary for 'Summands' and SparseKeyed'.
124+ type data Wrapped where
124125 Open :: Wrapped -- Needs some type-wide wrapping
125126 Closed :: Density -> Wrapped -- Does not need type-wide wrapping,
126- -- But may need field-wide wrapping, when Density is ' Sparse
127+ -- But may need field-wide wrapping, when Density is Sparse
127128
128129-- | A Field pairs an update function and a decoder for one field of a Sparse record.
129130data Field t where
130131 Field :: (x -> t -> t ) -> (forall s . Decoder s x ) -> Field t
131132
132133{-# INLINE field #-}
133- field :: Typeable x => (x -> t -> t ) -> Decode (' Closed d ) x -> Field t
134+ field :: Typeable x => (x -> t -> t ) -> Decode (Closed d ) x -> Field t
134135field update dec = Field update (decode dec)
135136
136137{-# INLINE fieldGuarded #-}
@@ -141,7 +142,7 @@ fieldGuarded ::
141142 -- | The condition to guard against
142143 (x -> Bool ) ->
143144 (x -> t -> t ) ->
144- Decode (' Closed d ) x ->
145+ Decode (Closed d ) x ->
145146 Field t
146147fieldGuarded failMsg check update dec =
147148 Field
@@ -151,7 +152,7 @@ fieldGuarded failMsg check update dec =
151152 )
152153
153154{-# INLINE ofield #-}
154- ofield :: Typeable x => (StrictMaybe x -> t -> t ) -> Decode (' Closed d ) x -> Field t
155+ ofield :: Typeable x => (StrictMaybe x -> t -> t ) -> Decode (Closed d ) x -> Field t
155156ofield update dec = Field update (SJust <$> decode dec)
156157
157158{-# INLINE invalidField #-}
@@ -160,15 +161,15 @@ invalidField n = field (flip $ const @t @Void) (Invalid n)
160161
161162-- | Sparse decode something with a (DecCBOR (Annotator t)) instance
162163-- A special case of 'field'
163- fieldA :: (Typeable x , Applicative ann ) => (x -> t -> t ) -> Decode (' Closed d ) x -> Field (ann t )
164+ fieldA :: (Typeable x , Applicative ann ) => (x -> t -> t ) -> Decode (Closed d ) x -> Field (ann t )
164165fieldA update dec = Field (liftA2 update) (pure <$> decode dec)
165166{-# INLINE fieldA #-}
166167
167168-- | Sparse decode something with a (DecCBOR (Annotator t)) instance
168169fieldAA ::
169170 (Typeable x , Typeable ann , Applicative ann ) =>
170171 (x -> t -> t ) ->
171- Decode (' Closed d ) (ann x ) ->
172+ Decode (Closed d ) (ann x ) ->
172173 Field (ann t )
173174fieldAA update dec = Field (liftA2 update) (decode dec)
174175{-# INLINE fieldAA #-}
@@ -197,10 +198,10 @@ fieldAA update dec = Field (liftA2 update) (decode dec)
197198--
198199-- data A = ACon Int B C
199200--
200- -- encodeA :: A -> Encode (' Closed ' Dense) A
201+ -- encodeA :: A -> Encode (Closed Dense) A
201202-- encodeA (ACon i b c) = Rec ACon !> To i !> E (encCBOR . unB) b !> To c
202203--
203- -- decodeA :: Decode (' Closed ' Dense) A
204+ -- decodeA :: Decode (Closed Dense) A
204205-- decodeA = RecD ACon <! From <! D (B <$> decCBOR) <! From
205206--
206207-- instance EncCBOR A where
@@ -214,13 +215,13 @@ fieldAA update dec = Field (liftA2 update) (decode dec)
214215-- @
215216-- data N = N1 Int | N2 B Bool | N3 A
216217--
217- -- encodeN :: N -> Encode ' Open N
218+ -- encodeN :: N -> Encode Open N
218219-- encodeN (N1 i) = Sum N1 0 !> To i
219220-- encodeN (N2 b tf) = Sum N2 1 !> E (encCBOR . unB) b !> To tf
220221-- encodeN (N3 a) = Sum N3 2 !> To a
221222--
222- -- decodeN :: Decode (' Closed ' Dense) N -- Note each clause has an ' Open decoder,
223- -- decodeN = Summands "N" decodeNx -- But Summands returns a (' Closed ' Dense) decoder
223+ -- decodeN :: Decode (Closed Dense) N -- Note each clause has an Open decoder,
224+ -- decodeN = Summands "N" decodeNx -- But Summands returns a (Closed Dense) decoder
224225-- where decodeNx 0 = SumD N1 <! From
225226-- decodeNx 1 = SumD N2 <! D (B <$> decCBOR) <! From
226227-- decodeNx 3 = SumD N3 <! From
@@ -253,13 +254,13 @@ fieldAA update dec = Field (liftA2 update) (decode dec)
253254-- encoding some of the values. Note the use of 'Sum' with virtual constructor tags 0,1,2,3
254255--
255256-- @
256- -- encM :: M -> Encode ' Open M
257+ -- encM :: M -> Encode Open M
257258-- encM (M 0 [] t) = Sum M 0 !> OmitC 0 !> OmitC [] !> To t
258259-- encM (M 0 bs t) = Sum M 1 !> OmitC 0 !> To bs !> To t
259260-- encM (M n [] t) = Sum M 2 !> To n !> OmitC [] !> To t
260261-- encM (M n bs t) = Sum M 3 !> To n !> To bs !> To t
261262--
262- -- decM :: Word -> Decode ' Open M
263+ -- decM :: Word -> Decode Open M
263264-- decM 0 = SumD M <! Emit 0 <! Emit [] <! From -- The virtual constructors tell which fields have been Omited
264265-- decM 1 = SumD M <! Emit 0 <! From <! From -- So those fields are reconstructed using 'Emit'.
265266-- decM 2 = SumD M <! From <! Emit [] <! From
@@ -287,7 +288,7 @@ fieldAA update dec = Field (liftA2 update) (decode dec)
287288-- The user must ensure that there is NOT an Omit on a required field. 'encM2' is an example.
288289--
289290-- @
290- -- encM2:: M -> Encode (' Closed ' Sparse) M
291+ -- encM2:: M -> Encode (Closed Sparse) M
291292-- encM2 (M n xs t) =
292293-- Keyed M
293294-- !> Omit (== 0) (Key 0 (To n)) -- Omit if n is zero
@@ -337,11 +338,11 @@ fieldAA update dec = Field (liftA2 update) (decode dec)
337338-- @
338339data Decode (w :: Wrapped ) t where
339340 -- | Label the constructor of a Record-like datatype (one with exactly 1 constructor) as a Decode.
340- RecD :: t -> Decode (' Closed ' Dense) t
341+ RecD :: t -> Decode (Closed Dense ) t
341342 -- | Label the constructor of a Record-like datatype (one with multiple constructors) as an Decode.
342- SumD :: t -> Decode ' Open t
343+ SumD :: t -> Decode Open t
343344 -- | Lift a Word to Decode function into a DeCode for a type with multiple constructors.
344- Summands :: Text. Text -> (Word -> Decode ' Open t ) -> Decode (' Closed ' Dense) t
345+ Summands :: Text. Text -> (Word -> Decode Open t ) -> Decode (Closed Dense ) t
345346 -- | Lift a Word to Field function into a DeCode for a type with 1 constructor stored sparsely
346347 SparseKeyed ::
347348 Typeable t =>
@@ -353,28 +354,28 @@ data Decode (w :: Wrapped) t where
353354 (Word -> Field t ) ->
354355 -- | Pairs of keys and Strings which must be there (default values not allowed)
355356 [(Word , String )] ->
356- Decode (' Closed ' Dense) t
357+ Decode (Closed Dense ) t
357358 -- | Label a (component, field, argument) as sparsely stored, which will be populated
358359 -- with the default value.
359- KeyedD :: t -> Decode (' Closed ' Sparse) t
360+ KeyedD :: t -> Decode (Closed Sparse ) t
360361 -- | Label a (component, field, argument). It will be decoded using the existing
361362 -- DecCBOR instance at @t@
362363 From :: DecCBOR t => Decode w t
363364 -- | Label components, fields, arguments. It will be decoded using the existing
364365 -- DecCBORGroup instance at @t@
365366 FromGroup :: (EncCBORGroup t , DecCBORGroup t ) => Decode w t
366367 -- | Label a (component, field, argument). It will be decoded using the given decoder.
367- D :: (forall s . Decoder s t ) -> Decode (' Closed ' Dense) t
368+ D :: (forall s . Decoder s t ) -> Decode (Closed Dense ) t
368369 -- | Apply a functional decoding (arising from 'RecD' or 'SumD') to get (type wise)
369370 -- smaller decoding.
370- ApplyD :: Typeable a => Decode w1 (a -> t ) -> Decode (' Closed d ) a -> Decode w1 t
371+ ApplyD :: Typeable a => Decode w1 (a -> t ) -> Decode (Closed d ) a -> Decode w1 t
371372 -- | Mark a Word as a Decoding which is not a valid Decoding. Used when decoding sums
372373 -- that are tagged out of range.
373374 Invalid :: Word -> Decode w t
374375 -- | Used to make (Decode w) an instance of Functor.
375376 Map :: Typeable a => (a -> b ) -> Decode w a -> Decode w b
376377 -- | Assert that the next thing decoded must be tagged with the given word.
377- TagD :: Word -> Decode (' Closed x ) t -> Decode (' Closed x ) t
378+ TagD :: Word -> Decode (Closed x ) t -> Decode (Closed x ) t
378379 -- | Decode the next thing, not by inspecting the bytes, but pulled out of thin air,
379380 -- returning @t@. Used in sparse decoding.
380381 Emit :: t -> Decode w t
@@ -391,10 +392,10 @@ data Decode (w :: Wrapped) t where
391392 -- | A functional Decode
392393 Decode w1 (Annotator (a -> t )) ->
393394 -- | An Decoder for an Annotator
394- Decode (' Closed d ) (Annotator a ) ->
395+ Decode (Closed d ) (Annotator a ) ->
395396 Decode w1 (Annotator t )
396397 -- | the function to Either can raise an error when applied by returning (Left errorMessage)
397- ApplyErr :: Typeable a => Decode w1 (a -> Either String t ) -> Decode (' Closed d ) a -> Decode w1 t
398+ ApplyErr :: Typeable a => Decode w1 (a -> Either String t ) -> Decode (Closed d ) a -> Decode w1 t
398399
399400infixl 4 <!
400401
@@ -403,19 +404,19 @@ infixl 4 <*!
403404infixl 4 <?
404405
405406-- | Infix form of @ApplyD@ with the same infixity and precedence as @($)@.
406- (<!) :: Typeable a => Decode w1 (a -> t ) -> Decode (' Closed w ) a -> Decode w1 t
407+ (<!) :: Typeable a => Decode w1 (a -> t ) -> Decode (Closed w ) a -> Decode w1 t
407408x <! y = ApplyD x y
408409{-# INLINE (<!) #-}
409410
410411-- | Infix form of @ApplyAnn@ with the same infixity and precedence as @($)@.
411412(<*!) ::
412413 (Typeable a , Typeable t ) =>
413- Decode w1 (Annotator (a -> t )) -> Decode (' Closed d ) (Annotator a ) -> Decode w1 (Annotator t )
414+ Decode w1 (Annotator (a -> t )) -> Decode (Closed d ) (Annotator a ) -> Decode w1 (Annotator t )
414415x <*! y = ApplyAnn x y
415416{-# INLINE (<*!) #-}
416417
417418-- | Infix form of @ApplyErr@ with the same infixity and precedence as @($)@.
418- (<?) :: Typeable a => Decode w1 (a -> Either String t ) -> Decode (' Closed d ) a -> Decode w1 t
419+ (<?) :: Typeable a => Decode w1 (a -> Either String t ) -> Decode (Closed d ) a -> Decode w1 t
419420f <? y = ApplyErr f y
420421{-# INLINE (<?) #-}
421422
@@ -481,7 +482,7 @@ decodeCount (ApplyErr cn g) n = do
481482
482483-- The type of DecodeClosed precludes pattern match against (SumD c) as the types are different.
483484
484- decodeClosed :: Typeable t => Decode (' Closed d ) t -> Decoder s t
485+ decodeClosed :: Typeable t => Decode (Closed d ) t -> Decoder s t
485486decodeClosed (Summands nm f) = decodeRecordSum nm (decodE . f)
486487decodeClosed (KeyedD cn) = pure cn
487488decodeClosed (RecD cn) = pure cn
@@ -569,7 +570,7 @@ mapCoder f x = Map f x
569570
570571-- | Use `Cardano.Ledger.Binary.Coders.encodeDual` and `decodeDual`, when you want to
571572-- guarantee that a type has both `EncCBOR` and `FromCBR` instances.
572- decodeDual :: forall t . (EncCBOR t , DecCBOR t ) => Decode (' Closed ' Dense) t
573+ decodeDual :: forall t . (EncCBOR t , DecCBOR t ) => Decode (Closed Dense ) t
573574decodeDual = D decCBOR
574575 where
575576 -- Enforce EncCBOR constraint on t
@@ -579,22 +580,22 @@ decodeDual = D decCBOR
579580-- =============================================================================
580581
581582listDecodeA ::
582- Typeable x => Decode (' Closed ' Dense) (Annotator x ) -> Decode (' Closed ' Dense) (Annotator [x ])
583+ Typeable x => Decode (Closed Dense ) (Annotator x ) -> Decode (Closed Dense ) (Annotator [x ])
583584listDecodeA dx = D (sequence <$> decodeList (decode dx))
584585{-# INLINE listDecodeA #-}
585586
586587setDecodeA ::
587588 (Ord x , Typeable x ) =>
588- Decode (' Closed ' Dense) (Annotator x ) ->
589- Decode (' Closed ' Dense) (Annotator (Set x ))
589+ Decode (Closed Dense ) (Annotator x ) ->
590+ Decode (Closed Dense ) (Annotator (Set x ))
590591setDecodeA dx = D (decodeAnnSet (decode dx))
591592{-# INLINE setDecodeA #-}
592593
593594mapDecodeA ::
594595 (Ord k , Typeable k , Typeable v ) =>
595- Decode (' Closed ' Dense) (Annotator k ) ->
596- Decode (' Closed ' Dense) (Annotator v ) ->
597- Decode (' Closed ' Dense) (Annotator (Map. Map k v ))
596+ Decode (Closed Dense ) (Annotator k ) ->
597+ Decode (Closed Dense ) (Annotator v ) ->
598+ Decode (Closed Dense ) (Annotator (Map. Map k v ))
598599mapDecodeA k v = D (decodeMapTraverse (decode k) (decode v))
599600{-# INLINE mapDecodeA #-}
600601
@@ -625,6 +626,6 @@ unusedRequiredKeys used required name =
625626{-# NOINLINE unusedRequiredKeys #-}
626627
627628-- | Prevent decoding until the 'Version' is at least the provided version.
628- guardUntilAtLeast :: DecCBOR a => String -> Version -> Decode (' Closed ' Dense) a
629+ guardUntilAtLeast :: DecCBOR a => String -> Version -> Decode (Closed Dense ) a
629630guardUntilAtLeast errMessage v = D (unlessDecoderVersionAtLeast v (fail errMessage) >> decCBOR)
630631{-# INLINE guardUntilAtLeast #-}
0 commit comments