Skip to content

Commit b452169

Browse files
authored
Skip the context for deriving Generic (#18)
* Skip the context for deriving Generic * Mention `deriving Generic` behaviour in docs * Add example
1 parent f2f8ee1 commit b452169

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

examples/generic/Generic.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
import Extensible
3+
import GHC.Generics
4+
5+
extensible [d| data A a = A a deriving (Eq, Generic) |]
6+
7+
main :: IO ()
8+
main = pure ()

extensible-data.cabal

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ executable multifield
101101
main-is: MultiField.hs
102102
other-modules: MultiFieldBase
103103

104+
executable generic
105+
import: deps, example
106+
hs-source-dirs: examples/generic
107+
main-is: Generic.hs
108+
104109
executable lam
105110
import: deps, example
106111
hs-source-dirs: examples/lam

src/Extensible.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
-- variable and each extension. If this doesn't work (e.g. you want to
5959
-- derive 'Eq' but have a type variable of kind @'K.Type' -> 'K.Type'@),
6060
-- you must instead write your own declaration outside of the call to
61-
-- 'extensible'.
61+
-- 'extensible'. The only special case is that 'Generic' is not given
62+
-- a context.
6263
-- * Deriving for non-regular datatypes (datatypes with recursive
6364
-- occurrences applied to different types) doesn't work.
6465
--
@@ -278,6 +279,7 @@ where
278279
import Language.Haskell.TH as TH
279280
import Language.Haskell.TH.Syntax
280281
import Generics.SYB (Data, everywhere, mkT)
282+
import GHC.Generics (Generic)
281283
import Control.Monad
282284
import Data.Functor.Identity
283285
import Data.Void
@@ -630,10 +632,10 @@ makeInstances :: Config
630632
makeInstances conf name names ext tvs (SimpleDeriv strat prds) =
631633
pure $ map make1 prds
632634
where
633-
make1 prd = StandaloneDerivD strat'
634-
(map tvPred tvs ++ map allPred names)
635-
(prd `AppT` appExtTvs (ConT name) ext tvs)
636-
where
635+
make1 prd = StandaloneDerivD strat' ctx (prd `AppT` ty) where
636+
ty = appExtTvs (ConT name) ext tvs
637+
ctx | prd == ConT ''Generic = []
638+
| otherwise = (map tvPred tvs ++ map allPred names)
637639
tvPred = AppT prd . VarT . tyvarName
638640
allPred name' = appExtTvs (ConT bname `AppT` prd) ext tvs
639641
where bname = applyAffix (bundleName conf) name'

0 commit comments

Comments
 (0)