Skip to content

Commit 55821c5

Browse files
committed
HFC.Embed.Unary: use DerivingVia
Now works fine, potentially because we dropped GHC 8.10
1 parent 90b38a1 commit 55821c5

File tree

1 file changed

+54
-98
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Embed

1 file changed

+54
-98
lines changed

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

Lines changed: 54 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
{-# LANGUAGE FlexibleInstances #-}
66
{-# LANGUAGE GADTs #-}
77
{-# LANGUAGE InstanceSigs #-}
8-
{-# LANGUAGE KindSignatures #-}
8+
{-# LANGUAGE PolyKinds #-}
99
{-# LANGUAGE QuantifiedConstraints #-}
1010
{-# LANGUAGE RankNTypes #-}
1111
{-# LANGUAGE RecordWildCards #-}
1212
{-# LANGUAGE ScopedTypeVariables #-}
13+
{-# LANGUAGE StandaloneDeriving #-}
1314
{-# LANGUAGE StandaloneKindSignatures #-}
1415
{-# LANGUAGE TypeApplications #-}
1516
{-# LANGUAGE TypeOperators #-}
@@ -36,7 +37,7 @@ module Ouroboros.Consensus.HardFork.Combinator.Embed.Unary (
3637
import Cardano.Slotting.EpochInfo
3738
import Data.Bifunctor (first)
3839
import Data.Coerce
39-
import Data.Kind (Type)
40+
import Data.Kind (Constraint, Type)
4041
import Data.Proxy
4142
import Data.SOP.BasicFunctors
4243
import Data.SOP.Functors
@@ -106,49 +107,43 @@ inject' _ =
106107

107108
{-------------------------------------------------------------------------------
108109
Defaults (to ease implementation)
109-
110-
It'd be nicer to use deriving-via here, but we cannot due to a GHC bug
111-
(resulting in @No family instance for ‘GenTx’@ errors).
112-
See <https://gitlab.haskell.org/ghc/ghc/issues/13154#note_224287> .
113110
-------------------------------------------------------------------------------}
114111

115-
defaultProjectNS :: forall f blk.
116-
Coercible (f (HardForkBlock '[blk])) (NS f '[blk])
117-
=> f (HardForkBlock '[blk]) -> f blk
118-
defaultProjectNS = unZ . (coerce :: f (HardForkBlock '[blk]) -> NS f '[blk])
119-
120-
defaultInjectNS :: forall f blk.
121-
Coercible (f (HardForkBlock '[blk])) (NS f '[blk])
122-
=> f blk -> f (HardForkBlock '[blk])
123-
defaultInjectNS = (coerce :: NS f '[blk] -> f (HardForkBlock '[blk])) . Z
124-
125-
defaultProjectNP :: forall f blk.
126-
Coercible (f (HardForkBlock '[blk])) (NP f '[blk])
127-
=> f (HardForkBlock '[blk]) -> f blk
128-
defaultProjectNP = hd . (coerce :: f (HardForkBlock '[blk]) -> NP f '[blk])
129-
130-
defaultInjectNP :: forall f blk.
131-
Coercible (f (HardForkBlock '[blk])) (NP f '[blk])
132-
=> f blk -> f (HardForkBlock '[blk])
133-
defaultInjectNP = (coerce :: NP f '[blk] -> f (HardForkBlock '[blk])) . (:* Nil)
134-
135-
defaultProjectSt :: forall f blk.
136-
Coercible (f (HardForkBlock '[blk])) (HardForkState f '[blk])
137-
=> f (HardForkBlock '[blk]) -> f blk
138-
defaultProjectSt =
139-
State.currentState
140-
. Telescope.fromTZ
141-
. getHardForkState
142-
. (coerce :: f (HardForkBlock '[blk]) -> HardForkState f '[blk])
143-
144-
defaultInjectSt :: forall f blk.
145-
Coercible (f (HardForkBlock '[blk])) (HardForkState f '[blk])
146-
=> f blk -> f (HardForkBlock '[blk])
147-
defaultInjectSt =
148-
(coerce :: HardForkState f '[blk] -> f (HardForkBlock '[blk]))
149-
. HardForkState
150-
. Telescope.TZ
151-
. State.Current History.initBound
112+
type IsomorphicUnary :: ((k -> Type) -> [k] -> Type) -> (k -> Type) -> k -> Type
113+
newtype IsomorphicUnary h f a = IsomorphicUnary (f a)
114+
115+
instance
116+
( IsSOPLike h
117+
, forall blk. Coercible (f (HardForkBlock '[blk])) (h f '[blk])
118+
) => Isomorphic (IsomorphicUnary h f) where
119+
project ::
120+
forall blk.
121+
IsomorphicUnary h f (HardForkBlock '[blk])
122+
-> IsomorphicUnary h f blk
123+
project = coerce (fromSOPLike :: h f '[blk] -> f blk)
124+
125+
inject ::
126+
forall blk.
127+
IsomorphicUnary h f blk
128+
-> IsomorphicUnary h f (HardForkBlock '[blk])
129+
inject = coerce (toSOPLike :: f blk -> h f '[blk])
130+
131+
type IsSOPLike :: ((k -> Type) -> [k] -> Type) -> Constraint
132+
class IsSOPLike h where
133+
fromSOPLike :: h f '[a] -> f a
134+
toSOPLike :: f a -> h f '[a]
135+
136+
instance IsSOPLike NS where
137+
fromSOPLike = unZ
138+
toSOPLike = Z
139+
140+
instance IsSOPLike NP where
141+
fromSOPLike = hd
142+
toSOPLike = (:* Nil)
143+
144+
instance IsSOPLike HardForkState where
145+
fromSOPLike = State.fromTZ
146+
toSOPLike = HardForkState . Telescope.TZ . State.Current History.initBound
152147

153148
{-------------------------------------------------------------------------------
154149
Forwarding instances
@@ -162,61 +157,22 @@ instance Isomorphic ((->) a) where
162157
Simple instances
163158
-------------------------------------------------------------------------------}
164159

165-
instance Isomorphic WrapIsLeader where
166-
project = defaultProjectNS
167-
inject = defaultInjectNS
168-
169-
instance Isomorphic WrapGenTxId where
170-
project = defaultProjectNS
171-
inject = defaultInjectNS
172-
173-
instance Isomorphic WrapValidatedGenTx where
174-
project = defaultProjectNS
175-
inject = defaultInjectNS
176-
177-
instance Isomorphic I where
178-
project = defaultProjectNS
179-
inject = defaultInjectNS
180-
181-
instance Isomorphic GenTx where
182-
project = defaultProjectNS
183-
inject = defaultInjectNS
184-
185-
instance Isomorphic Header where
186-
project = defaultProjectNS
187-
inject = defaultInjectNS
188-
189-
instance Isomorphic BlockConfig where
190-
project = defaultProjectNP
191-
inject = defaultInjectNP
192-
193-
instance Isomorphic CodecConfig where
194-
project = defaultProjectNP
195-
inject = defaultInjectNP
196-
197-
instance Isomorphic StorageConfig where
198-
project = defaultProjectNP
199-
inject = defaultInjectNP
200-
201-
instance Isomorphic (Flip LedgerState mk) where
202-
project = defaultProjectSt
203-
inject = defaultInjectSt
204-
205-
instance Isomorphic WrapCannotForge where
206-
project = defaultProjectNS
207-
inject = defaultInjectNS
208-
209-
instance Isomorphic WrapChainDepState where
210-
project = defaultProjectSt
211-
inject = defaultInjectSt
212-
213-
instance Isomorphic WrapForgeStateUpdateError where
214-
project = defaultProjectNS
215-
inject = defaultInjectNS
216-
217-
instance Isomorphic WrapTipInfo where
218-
project = defaultProjectNS
219-
inject = defaultInjectNS
160+
deriving via IsomorphicUnary NP BlockConfig instance Isomorphic BlockConfig
161+
deriving via IsomorphicUnary NP CodecConfig instance Isomorphic CodecConfig
162+
deriving via IsomorphicUnary NP StorageConfig instance Isomorphic StorageConfig
163+
164+
deriving via IsomorphicUnary NS GenTx instance Isomorphic GenTx
165+
deriving via IsomorphicUnary NS Header instance Isomorphic Header
166+
deriving via IsomorphicUnary NS I instance Isomorphic I
167+
deriving via IsomorphicUnary NS WrapCannotForge instance Isomorphic WrapCannotForge
168+
deriving via IsomorphicUnary NS WrapForgeStateUpdateError instance Isomorphic WrapForgeStateUpdateError
169+
deriving via IsomorphicUnary NS WrapGenTxId instance Isomorphic WrapGenTxId
170+
deriving via IsomorphicUnary NS WrapIsLeader instance Isomorphic WrapIsLeader
171+
deriving via IsomorphicUnary NS WrapTipInfo instance Isomorphic WrapTipInfo
172+
deriving via IsomorphicUnary NS WrapValidatedGenTx instance Isomorphic WrapValidatedGenTx
173+
174+
deriving via IsomorphicUnary HardForkState (Flip LedgerState mk) instance Isomorphic (Flip LedgerState mk)
175+
deriving via IsomorphicUnary HardForkState WrapChainDepState instance Isomorphic WrapChainDepState
220176

221177
{-------------------------------------------------------------------------------
222178
Hash

0 commit comments

Comments
 (0)