Created
May 29, 2019 00:01
-
-
Save Lysxia/10f1da354f051b2d2eb24f6aace1bf9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE | |
AllowAmbiguousTypes, | |
ScopedTypeVariables, | |
TypeApplications, | |
TypeFamilies, | |
UndecidableInstances, | |
FlexibleContexts, | |
DataKinds, | |
DeriveGeneric, | |
TypeOperators | |
#-} | |
import Data.Kind (Type) | |
import Data.Proxy | |
import Numeric.Natural | |
import GHC.Generics | |
import GHC.TypeNats | |
type family Arity (f :: Type -> Type) :: Nat | |
type instance Arity (M1 i c f) = Arity f | |
type instance Arity (f :*: g) = Arity f + Arity g | |
type instance Arity U1 = 0 | |
type instance Arity (K1 i a) = 1 | |
arity :: forall a. (Generic a, KnownNat (Arity (Rep a))) => Natural | |
arity = natVal (Proxy @(Arity (Rep a))) | |
data T = T Int Bool String | |
deriving Generic | |
main = print (arity @T) -- 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The type errors will be horrible. You can fix that using something like this: