Skip to content

Commit ca4db4e

Browse files
committed
[feat] allow deriving via Generically
1 parent f5dd349 commit ca4db4e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

generics-sop/generics-sop.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ library
7070
template-haskell >= 2.8 && < 2.21,
7171
th-abstraction >= 0.4 && < 0.6,
7272
ghc-prim >= 0.3 && < 0.11
73+
if impl(ghc < 9.4.1)
74+
build-depends: generically
75+
7376
hs-source-dirs: src
7477
default-language: Haskell2010
7578
ghc-options: -Wall

generics-sop/src/Generics/SOP/Universe.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE UndecidableInstances #-}
22
{-# LANGUAGE UndecidableSuperClasses #-}
3+
{-# LANGUAGE CPP #-}
34
-- | Codes and interpretations
45
module Generics.SOP.Universe where
56

@@ -8,13 +9,20 @@ import Data.Coerce (Coercible, coerce)
89
import Data.Proxy
910
import qualified GHC.Generics as GHC
1011

12+
#if MIN_VERSION_base(4,17,0)
13+
import GHC.Generics (Generically(Generically))
14+
#else
15+
import GHC.Generics.Generically(Generically(Generically))
16+
#endif
17+
1118
import Generics.SOP.BasicFunctors
1219
import Generics.SOP.Constraint
1320
import Generics.SOP.NP
1421
import Generics.SOP.NS
1522
import Generics.SOP.GGP
1623
import Generics.SOP.Metadata
1724
import qualified Generics.SOP.Type.Metadata as T
25+
import Language.Haskell.TH (Extension(DeriveLift))
1826

1927
-- | The (generic) representation of a datatype.
2028
--
@@ -270,3 +278,29 @@ newtypeFrom = coerce
270278
newtypeTo :: IsNewtype a x => x -> a
271279
newtypeTo = coerce
272280
{-# INLINE newtypeTo #-}
281+
282+
-- | Derive 'Generic' via 'Generically'
283+
--
284+
#if MIN_VERSION_GLASGOW_HASKELL(8,6,1,0)
285+
-- >>> :set -XDerivingStrategies -XDerivingVia -XDeriveGeneric -XUndecidableInstances
286+
-- >>> data A = B Int | C Bool deriving stock GHC.Generic deriving Generic via Generically A
287+
-- >>> :kind! Code A
288+
-- Code A :: [[*]]
289+
-- = '[ '[Int], '[Bool]]
290+
-- >>> from (B 4)
291+
-- SOP (Z (I 4 :* Nil))
292+
-- >>> from (C False)
293+
-- SOP (S (Z (I False :* Nil)))
294+
#endif
295+
--
296+
-- @since 0.5.2.0
297+
instance
298+
(GHC.Generic a
299+
, GFrom a
300+
, GTo a
301+
, Rep a ~ SOP I (GCode a)
302+
, All SListI (Code a)
303+
) => Generic (Generically a) where
304+
type Code (Generically a) = GCode a
305+
from (Generically a) = gfrom a
306+
to rep = Generically (gto rep)

0 commit comments

Comments
 (0)