Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building with template-haskell-2.21.*, th-abstraction-0.6.* #41

Merged
merged 2 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### next [????.??.??]
* Support building with `template-haskell-2.21.*` (GHC 9.8).
* The Template Haskell machinery now uses `TemplateHaskellQuotes` when building
with GHC 8.0+ instead of manually constructing each Template Haskell `Name`.
A consequence of this is that `deriving-compat` will now build with GHC 9.8,
Expand Down
7 changes: 4 additions & 3 deletions deriving-compat.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,17 @@ library
Text.Show.Deriving.Internal
build-depends: containers >= 0.1 && < 0.7
, ghc-prim
, th-abstraction >= 0.4 && < 0.6
, th-abstraction >= 0.4 && < 0.7

if flag(base-4-9)
build-depends: base >= 4.9 && < 5
cpp-options: "-DNEW_FUNCTOR_CLASSES"
else
build-depends: base >= 4.3 && < 4.9
, semigroups >= 0.6 && < 0.21

if flag(template-haskell-2-11)
build-depends: template-haskell >= 2.11 && < 2.21
build-depends: template-haskell >= 2.11 && < 2.22
, ghc-boot-th
else
build-depends: template-haskell >= 2.5 && < 2.11
Expand Down Expand Up @@ -189,7 +190,7 @@ test-suite spec
, hspec >= 1.8
, QuickCheck >= 2 && < 3
, tagged >= 0.7 && < 1
, template-haskell >= 2.5 && < 2.21
, template-haskell >= 2.5 && < 2.22
, void >= 0.5.10 && < 1
build-tool-depends: hspec-discover:hspec-discover >= 1.8

Expand Down
13 changes: 9 additions & 4 deletions src/Data/Bounded/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Data.Bounded.Deriving.Internal (
) where

import Data.Deriving.Internal
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty(..))

import Language.Haskell.TH.Datatype
import Language.Haskell.TH.Lib
Expand Down Expand Up @@ -90,7 +92,7 @@ makeBoundedFun bf name = do
-- given constructors. All constructors must be from the same type.
makeBoundedFunForCons :: BoundedFun -> Name -> [ConstructorInfo] -> Q Exp
makeBoundedFunForCons _ _ [] = noConstructorsError
makeBoundedFunForCons bf tyName cons
makeBoundedFunForCons bf tyName (con:cons')
| not (isProduct || isEnumeration)
= enumerationOrProductError $ nameBase tyName
| isEnumeration
Expand All @@ -102,9 +104,12 @@ makeBoundedFunForCons bf tyName cons
isProduct = isProductType cons
isEnumeration = isEnumerationType cons

cons :: NonEmpty ConstructorInfo
cons = con :| cons'

con1, conN :: Q Exp
con1 = conE $ constructorName $ head cons
conN = conE $ constructorName $ last cons
con1 = conE $ constructorName con
conN = conE $ constructorName $ NE.last cons

pickCon :: Q Exp
pickCon = case bf of
Expand All @@ -114,7 +119,7 @@ makeBoundedFunForCons bf tyName cons
pickConApp :: Q Exp
pickConApp = appsE
$ pickCon
: map varE (replicate (conArity $ head cons) (boundedFunName bf))
: map varE (replicate (conArity con) (boundedFunName bf))

-------------------------------------------------------------------------------
-- Class-specific constants
Expand Down
12 changes: 6 additions & 6 deletions src/Data/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Data.Functor.Classes
)
#endif
import qualified Data.List as List
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Maybe
Expand Down Expand Up @@ -824,15 +825,14 @@ conArity :: ConstructorInfo -> Int
conArity (ConstructorInfo { constructorFields = tys }) = length tys

-- | Returns 'True' if it's a datatype with exactly one, non-existential constructor.
isProductType :: [ConstructorInfo] -> Bool
isProductType [con] = null (constructorVars con)
isProductType _ = False
isProductType :: NonEmpty ConstructorInfo -> Bool
isProductType (con :| []) = null (constructorVars con)
isProductType _ = False

-- | Returns 'True' if it's a datatype with one or more nullary, non-GADT
-- constructors.
isEnumerationType :: [ConstructorInfo] -> Bool
isEnumerationType cons@(_:_) = all (App.liftA2 (&&) isNullaryCon isVanillaCon) cons
isEnumerationType _ = False
isEnumerationType :: NonEmpty ConstructorInfo -> Bool
isEnumerationType cons = F.all (App.liftA2 (&&) isNullaryCon isVanillaCon) cons

-- | Returns 'False' if we're dealing with existential quantification or GADTs.
isVanillaCon :: ConstructorInfo -> Bool
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Deriving/Via/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ deriveViaDecs instanceTy mbViaTy = do
_ -> fail $ "Not a type class: " ++ pprint clsTy
_ -> fail $ "Malformed instance: " ++ pprint instanceTy

deriveViaDecs' :: Name -> [TyVarBndrUnit] -> [Type] -> Type -> Dec -> Q (Maybe [Dec])
deriveViaDecs' :: Name -> [TyVarBndr_ flag] -> [Type] -> Type -> Dec -> Q (Maybe [Dec])
deriveViaDecs' clsName clsTvbs clsArgs repTy dec = do
let numExpectedArgs = length clsTvbs
numActualArgs = length clsArgs
Expand Down Expand Up @@ -187,7 +187,7 @@ deriveViaDecs' clsName clsTvbs clsArgs repTy dec = do

go _ = return Nothing

mkCoerceClassMethEqn :: [TyVarBndrUnit] -> [Type] -> Type -> Type -> (Type, Type)
mkCoerceClassMethEqn :: [TyVarBndr_ flag] -> [Type] -> Type -> Type -> (Type, Type)
mkCoerceClassMethEqn clsTvbs clsArgs repTy methTy
= ( applySubstitution rhsSubst methTy
, applySubstitution lhsSubst methTy
Expand Down
8 changes: 6 additions & 2 deletions src/Data/Enum/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Data.Enum.Deriving.Internal (
) where

import Data.Deriving.Internal
import Data.List.NonEmpty (NonEmpty(..))

import Language.Haskell.TH.Datatype
import Language.Haskell.TH.Lib
Expand Down Expand Up @@ -116,7 +117,7 @@ makeEnumFun ef name = do
-- given constructors. All constructors must be from the same type.
makeEnumFunForCons :: EnumFun -> Name -> Type -> [ConstructorInfo] -> Q Exp
makeEnumFunForCons _ _ _ [] = noConstructorsError
makeEnumFunForCons ef tyName ty cons
makeEnumFunForCons ef tyName ty (con:cons')
| not $ isEnumerationType cons
= enumerationError tyNameBase
| otherwise = case ef of
Expand Down Expand Up @@ -173,8 +174,11 @@ makeEnumFunForCons ef tyName ty cons
tyNameBase :: String
tyNameBase = nameBase tyName

cons :: NonEmpty ConstructorInfo
cons = con :| cons'

maxTagExpr :: Q Exp
maxTagExpr = integerE (length cons - 1) `sigE` conT intTypeName
maxTagExpr = integerE (length cons') `sigE` conT intTypeName

lamOne :: (Name -> Q Exp) -> Q Exp
lamOne f = do
Expand Down
11 changes: 6 additions & 5 deletions src/Data/Ix/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Data.Ix.Deriving.Internal (
) where

import Data.Deriving.Internal
import Data.List.NonEmpty (NonEmpty(..))

import Language.Haskell.TH.Datatype
import Language.Haskell.TH.Lib
Expand Down Expand Up @@ -95,7 +96,7 @@ makeIxFun ixf name = do
-- given constructors. All constructors must be from the same type.
makeIxFunForCons :: IxFun -> Name -> Type -> [ConstructorInfo] -> Q Exp
makeIxFunForCons _ _ _ [] = noConstructorsError
makeIxFunForCons ixf tyName ty cons
makeIxFunForCons ixf tyName ty (con:cons')
| not (isProduct || isEnumeration)
= enumerationOrProductError $ nameBase tyName
| isEnumeration
Expand Down Expand Up @@ -144,10 +145,7 @@ makeIxFunForCons ixf tyName ty cons
]

| otherwise -- It's a product type
= do let con :: ConstructorInfo
con = head cons

conName :: Name
= do let conName :: Name
conName = constructorName con

conFields :: Int
Expand Down Expand Up @@ -204,6 +202,9 @@ makeIxFunForCons ixf tyName ty cons
mkInRange a b c = varE inRangeValName `appE` tupE [varE a, varE b]
`appE` varE c
where
cons :: NonEmpty ConstructorInfo
cons = con :| cons'

isProduct, isEnumeration :: Bool
isProduct = isProductType cons
isEnumeration = isEnumerationType cons
Expand Down
32 changes: 19 additions & 13 deletions src/Data/Ord/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ module Data.Ord.Deriving.Internal (

import Data.Deriving.Internal
import Data.List (partition)
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Map as Map
import Data.Map (Map)

Expand Down Expand Up @@ -277,10 +279,6 @@ makeOrdFunForCons oFun instTypes cons = do
singleConType :: Bool
singleConType = isSingleton cons

firstConName, lastConName :: Name
firstConName = constructorName $ head cons
lastConName = constructorName $ last cons

-- Alternatively, we could look these up from dataConTagMap, but this
-- is slightly faster due to the lack of Map lookups.
firstTag, lastTag :: Int
Expand All @@ -290,22 +288,30 @@ makeOrdFunForCons oFun instTypes cons = do
dataConTagMap :: Map Name Int
dataConTagMap = Map.fromList $ zip (map constructorName cons) [0..]

ordMatches :: ConstructorInfo -> Q Match
ordMatches = makeOrdFunForCon oFun v2 v2Hash tvMap singleConType
firstTag firstConName lastTag lastConName
dataConTagMap

ordFunRhs :: Q Exp
ordFunRhs
| null cons
= conE eqDataName
ordFunRhs =
case cons of
[] -> conE eqDataName
c:cs -> ordFunRhsNonEmptyCons (c :| cs)

ordFunRhsNonEmptyCons :: NonEmpty ConstructorInfo -> Q Exp
ordFunRhsNonEmptyCons cs@(c :| _)
| length nullaryCons <= 2
= caseE (varE v1) $ map ordMatches cons
= caseE (varE v1) $ map ordMatches $ NE.toList cs
| null nonNullaryCons
= mkTagCmp
| otherwise
= caseE (varE v1) $ map ordMatches nonNullaryCons
++ [match wildP (normalB mkTagCmp) []]
where
firstConName, lastConName :: Name
firstConName = constructorName c
lastConName = constructorName $ NE.last cs

ordMatches :: ConstructorInfo -> Q Match
ordMatches = makeOrdFunForCon oFun v2 v2Hash tvMap singleConType
firstTag firstConName lastTag lastConName
dataConTagMap

mkTagCmp :: Q Exp
mkTagCmp = untagExpr [(v1, v1Hash), (v2, v2Hash)] $
Expand Down