次のコードで関数のパラメーターの数を知ることができます
{-#Language MultiParamTypeClasses#-}
{-#Language FunctionalDependencies#-}
{-#Language UndecidableInstances#-}
data Zero
data Succ a
class Number a
instance Number Zero
instance (Number a) => Number (Succ a)
class NotFunction a
instance NotFunction Int
instance NotFunction Float
instance NotFunction (IO a)
class (Number n) => FunctionLevel f n | f -> n where
functionLevel :: f -> n
instance FunctionLevel Int Zero where
functionLevel = undefined
instance FunctionLevel Float Zero where
functionLevel = undefined
instance FunctionLevel (IO a) Zero where
functionLevel = undefined
instance FunctionLevel Double Zero where
functionLevel = undefined
instance (FunctionLevel f' n) => FunctionLevel (a->f') (Succ n) where
functionLevel = undefined
そして、次のようになります。
*Main> :t functionLevel (undefined::a->b->Int)
functionLevel (undefined::a->b->Int) :: Succ (Succ Zero)
*Main> :t functionLevel (undefined::a->b->Double)
functionLevel (undefined::a->b->Double) :: Succ (Succ Zero)
*Main> :t functionLevel (undefined::a->b->c->d->IO a)
functionLevel (undefined::a->b->c->d->IO a)
:: Succ (Succ (Succ (Succ Zero)))
*Main> :t functionLevel (undefined::a->b->c->d->Int)
functionLevel (undefined::a->b->c->d->Int)
:: Succ (Succ (Succ (Succ Zero)))
ご覧のとおりfunctionLevel
、いくつかの特殊な型で「終了」した関数に対して期待どおりに動作します。私の質問は、これを一般化して、任意の関数のパラメーター番号を伝えることができるでしょうか?