generic-constraints 1.0.0 → 1.1.0
raw patch · 4 files changed
+49/−7 lines, 4 filesdep +template-haskelldep +th-abstractionPVP ok
version bump matches the API change (PVP)
Dependencies added: template-haskell, th-abstraction
API changes (from Hackage documentation)
+ Generics.Constraints: makeDeriving :: Name -> Name -> DecsQ
+ Generics.Constraints: makeDerivings :: [Name] -> [Name] -> DecsQ
+ Generics.Constraints: makeInstance :: Name -> Name -> DecsQ
+ Generics.Constraints: makeInstances :: [Name] -> [Name] -> DecsQ
Files
- CHANGELOG +3/−0
- generic-constraints.cabal +3/−1
- src/Generics/Constraints.hs +38/−3
- test/unittests.hs +5/−3
CHANGELOG view
@@ -1,4 +1,7 @@ CHANGELOG +1.1+ - Added TH to generate standalone deriving clauses+ 1.0 - Extracted generic-constraints from Sjoerd Visscher's & Xia Li-yao's one-liner library, as advised by Sjoerd
generic-constraints.cabal view
@@ -1,5 +1,5 @@ Name: generic-constraints-Version: 1.0.0+Version: 1.1.0 Synopsis: Constraints via Generic Description: Write short and concise contexts based on generics Homepage: https://github.com/yairchu/generic-constraints@@ -23,6 +23,8 @@ Build-depends: base >= 4.9 && < 5+ , template-haskell+ , th-abstraction ghc-options: -Wall
src/Generics/Constraints.hs view
@@ -3,11 +3,18 @@ , TypeFamilies , TypeOperators , ConstraintKinds+ , TemplateHaskellQuotes #-}-module Generics.Constraints (Constraints) where+module Generics.Constraints+ ( Constraints+ , makeDeriving, makeDerivings+ , makeInstance, makeInstances+ ) where -import Data.Kind (Constraint, Type)-import GHC.Generics+import Data.Kind (Constraint, Type)+import GHC.Generics+import qualified Language.Haskell.TH as T+import qualified Language.Haskell.TH.Datatype as D type family Constraints' (t :: Type -> Type) (c :: Type -> Constraint) :: Constraint type instance Constraints' V1 c = ()@@ -24,3 +31,31 @@ -- requirements for an instance for `t` of class `c`. -- It requires an instance of class `c` for each component of `t`. type Constraints t c = Constraints' (Rep t) c++makeDerivings :: [T.Name] -> [T.Name] -> T.DecsQ+makeDerivings = makeMany makeDeriving++makeInstances :: [T.Name] -> [T.Name] -> T.DecsQ+makeInstances = makeMany makeDeriving++makeMany :: (T.Name -> T.Name -> T.DecsQ) -> [T.Name] -> [T.Name] -> T.DecsQ+makeMany f classes types = concat <$> sequence (f <$> classes <*> types)++makeDeriving :: T.Name -> T.Name -> T.DecsQ+makeDeriving = makeCommon (T.StandaloneDerivD Nothing)++makeInstance :: T.Name -> T.Name -> T.DecsQ+makeInstance = makeCommon (\c i -> T.InstanceD Nothing c i [])++makeCommon :: ([T.Type] -> T.Type -> T.Dec) -> T.Name -> T.Name -> T.DecsQ+makeCommon f clsName typName =+ r <$> D.reifyDatatype typName+ where+ r info =+ [ f [T.ConT ''Constraints `T.AppT` typ `T.AppT` T.ConT clsName]+ (T.ConT clsName `T.AppT` typ)+ ]+ where+ typ =+ foldl T.AppT (T.ConT typName)+ (T.VarT . D.tvName <$> D.datatypeVars info)
test/unittests.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE DeriveGeneric+ , TemplateHaskell , StandaloneDeriving , UndecidableInstances #-}@@ -11,10 +12,11 @@ data T a = T (a Int) deriving Generic -deriving instance Constraints (T a) Eq => Eq (T a)-deriving instance Constraints (T a) Ord => Ord (T a)-deriving instance Constraints (T a) Show => Show (T a)+makeDeriving ''Eq ''T+makeDeriving ''Ord ''T+makeDeriving ''Show ''T +main :: IO Counts main = runTestTT $ test [ assert (T [5] > T [3]) ]