Рассмотрим следующий пример кода:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-} -- Is there a way to avoid this?
-- A generic class with a generic function.
class Foo a where
foo :: a -> a
-- A specific class with specific functions.
class Bar a where
bar :: a -> a
baz :: a -> a
-- Given the specific class functions, we can implement the generic class function.
instance Bar a => Foo a where
foo = bar. baz
-- So if a type belongs to the specific class...
instance Bar String where
bar = id
baz = id
-- We can invoke the generic function on it.
main :: IO ()
main =
putStrLn (foo "bar")
(Мой реальный код намного сложнее; это минимальный уваренный -вниз случай для демонстрации паттерна.)
Мне непонятно, зачем UndecidableInstances
нужны здесь -параметр типа a
появляется один раз в обеих сторонах Bar a => Foo a
, поэтому я ожидал, что все будет «просто работать». Я явно что-то здесь упускаю. Но в любом случае есть ли способ сделать это без использования UndecidableInstances
?