Strange behavior when adding constraint to instance

I'm using the syntactic library to work with ASTs. I'm getting some strange behavior, and I'm not what's happening.

{-# LANGUAGE TypeOperators, GADTs, FlexibleInstances,
    FlexibleContexts, UndecidableInstances #-}
module Foo where

import Data.Syntactic
import Data.Syntactic.Functional

data Const a where Const :: Const (Full a)
instance Render Const where renderSym Const = "const"

main :: ASTF Const Int
main = foo $ inj Const

class Foo dom where
  foo :: ASTF dom a -> ASTF dom a

instance --(Const :<: dom) => 
    Foo dom where
  foo node | Just Const <- prj node = error "PASS"
  foo _ = error "FAIL"

bar :: (Const :<: dom) => ASTF dom a -> ASTF dom a
bar node | Just Const <- prj node = error "PASS"
bar _ = error "FAIL"

Question 1

I can define a Foo instance without the constraint Const :<: dom , but I don't know of any way to make bar compile without that constraint (GHC suggests IncoherentInstances if I leave the constraint off of bar ). What is different between bar and foo that allows me to not use the constraint with foo ?

Question 2

Although I can define the Foo instance without the constraint, running main in the code above prints "FAIL". However, if I add (uncomment) the constraint Const :<: dom in the instance, main prints "PASS". What Haskell machinery makes it possible to add an unnecessary constraint and change the behavior? What specifically is happening in this example to cause this behavior? I suspect it might have something to do with overlapping/undecidable/incoherent instances in the Syntactic library. (Note: bar always prints "PASS".)

链接地址: http://www.djcxy.com/p/43260.html

上一篇: GHC选择何种字典时,多于一个字典?

下一篇: 将约束添加到实例时出现奇怪的行为