A question concerning functional dependencies

Ashley Yakeley ashley@semantic.org
Mon, 2 Sep 2002 03:11:58 -0700


At 2002-09-02 02:46, Jerzy Karczmarczuk wrote:

>class Module v s | v->s  .
...
>instance Num s => Module (v->s) s 
...
>instance ...=> Module ((v->s)->(v->s)) s
...
>But GHCi yells that two instances in view of the functional 
>dependency declared are in conflict.

GHCi is correct. Bear in mind GHC ignores contexts in instance 
declarations when calculating conflicts. This is unfortunate, but 
apparently fixing it would be hard.

((v->s)->(v->s)) is a substitution instance of (v->s). So from the first 
instance,

  instance ...=> Module (v->s) s 

GHC can derive

  instance ...=> Module ((v->s)->(v->s)) (v->s)

...which conflicts with your second instance per the fundep:

  instance ...=> Module ((v->s)->(v->s)) s

The solution of course is to declare new datatypes:

  newtype Vector v s = MkVector (v->s)

  newtype Operator v s = MkOperator ((v->s)->(v->s))

etc.

-- 
Ashley Yakeley, Seattle WA