Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GHC chooses different instances for the same expression?

I want to implement an arrow with an arr-member-function showing a different behavior for function arguments with different types, for instance arr (\x -> (x,x)) should behave differently from arr id...

Here's the code:

{-# 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

But: It shows the following very strange behavor:

> toStr (\x -> (x,x)) -- that works as expected!
"b -> (b,b)" 
> appMyArr (arr (\x -> (x,x))) () -- but this does'nt!!
(((),()),"a -> b")

Can anyone explain how to get ghci to choose the b -> (b,b)-instance for the expression \x -> (x,x) in the second example?

like image 514
phynfo Avatar asked Jan 24 '26 07:01

phynfo


1 Answers

If you use IncoherentInstances anything can happen. There is no longer any promise the instances are picked in a coherent way.

like image 195
augustss Avatar answered Jan 27 '26 00:01

augustss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!