Я хочу реализовать стрелку с arr
-функцией-членом, показывающей различное поведение для аргументов функции с разными типами, например, arr (\x -> (x,x))
должно вести себя иначе, чем arr id
...
Вот код:
{-# LANGUAGE Arrows, OverlappingInstances, IncoherentInstances, FlexibleInstances#-}
import Control.Arrow
import Control.Category
import Prelude hiding (id, (.))
class ToPredefinedStr a where
toStr :: a -> String
instance ToPredefinedStr ((->) b (b,b)) where
toStr _ = "b -> (b,b)"
instance ToPredefinedStr (a -> (b,c)) where
toStr _ = "a -> (b,c)"
instance ToPredefinedStr ((a,b) -> c) where
toStr _ = "(a,b) -> c"
instance ToPredefinedStr (a -> b) where
toStr _ = "a -> b"
newtype MyArrow a b c = MA (a b (c, String))
instance (Category a, Arrow a) => Category (MyArrow a) where
-- irrelevant for this example ...
instance (Arrow a) => Arrow (MyArrow a) where
arr f = MA (arr (\x -> (f x, toStr f)))
appMyArr (MA a) = a
Но: Он показывает следующее очень странное поведение:
> toStr (\x -> (x,x)) -- that works as expected!
"b -> (b,b)"
> appMyArr (arr (\x -> (x,x))) () -- but this does'nt!!
(((),()),"a -> b")
Кто-нибудь может объяснить, как заставить ghci выбрать экземпляр b -> (b,b)
для выражения \x -> (x,x)
во втором примере?