{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Language.Haskell.Homplexity.TypeClassComplexity
( NonTypeDeclCount
, nonTypeDeclCountT
, AssocTypeCount
, assocTypeCountT
) where
import Data.Data
import Data.Generics.Uniplate.Data ()
import Data.Maybe
import Language.Haskell.Exts.Syntax
import Language.Haskell.Homplexity.CodeFragment
import Language.Haskell.Homplexity.Metric
import Language.Haskell.Homplexity.Utilities
newtype NonTypeDeclCount = NonTypeDeclCount { unNonTypeDeclCount :: Int }
deriving (Eq, Ord, Enum, Num, Real, Integral)
nonTypeDeclCountT :: Proxy NonTypeDeclCount
nonTypeDeclCountT = Proxy
instance Show NonTypeDeclCount where
showsPrec _ (NonTypeDeclCount mc) = ("method + value count of " ++)
. shows mc
instance Metric NonTypeDeclCount TypeClass where
measure = NonTypeDeclCount . sumOf method . fromMaybe [] . tcDecls where
method :: ClassDecl l -> Int
method (ClsDecl _ TypeSig{}) = 1
method _ = 0
newtype AssocTypeCount = AssocTypeCount { unAssocTypeCount :: Int }
deriving (Eq, Ord, Enum, Num, Real, Integral)
assocTypeCountT :: Proxy AssocTypeCount
assocTypeCountT = Proxy
instance Show AssocTypeCount where
showsPrec _ (AssocTypeCount atc) = ("associated type count of " ++)
. shows atc
instance Metric AssocTypeCount TypeClass where
measure = AssocTypeCount . sumOf assocType . fromMaybe [] . tcDecls where
assocType :: ClassDecl l -> Int
assocType ClsTyFam{} = 1
assocType ClsTyDef{} = 1
assocType ClsDataFam{} = 1
assocType _ = 0