型クラスがあるとします:
data Proxy a = Proxy
class Fixed a where
fixed :: Proxy a -> Int
の定義fixed
は非常に簡単なので、次を使用して導出しGHC.Generics
ます。
class GFixed f where
gfixed :: Proxy (f a) -> Int
instance (GFixed f, GFixed g) => GFixed (f :*: g) where ...
instance (GFixed f, GFixed g) => GFixed (f :+: g) where ...
instance GFixed f => GFixed (M1 i c f) where ...
instance Fixed a => GFixed (K1 i a) where ...
....
default fixed :: (Generic a, GFixed (Rep a)) => Proxy a -> Int
fixed _ = fixed (Proxy :: Proxy (Rep a b))
for void 型GFixed U1
のインスタンスを持つ意味がないため、インスタンスの for は含めません。Fixed
機械についての私の理解Generics
はあまりよくありません。具体的には、機械の種類M1
とK1
意味についてです。質問は次のとおりです:の既定の定義が再帰型で機能しないGFixed
ように、型レベルで制限できますか?fixed
たとえば、次のように記述します。
data Void
instance Fixed Void
型エラーが発生します: No instance for (GFixed V1)
. などの型エラーを取得したいと思いinstance Fixed [Int]
ます。