👾

getArity

2021/05/09に公開
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE IncoherentInstances #-}
class Arity a where
  getArity :: a -> Int 
  
instance {-# OVERLAPPABLE #-} Arity a where
  getArity = const 0
  
instance Arity b => Arity (a -> b) where
  getArity f = 1 + getArity (f undefined)

a1, a2 :: Int
a1 = getArity id    -- 1
a2 = getArity const -- 2

h :: (Arity a, Arity b) => a -> b -> Int
h f g = getArity f + getArity g

a3 :: Int
a3 = h id const -- 3

Discussion