A question concerning functional dependencies

2002-09-02 Thread Jerzy Karczmarczuk

I wanted to write a small package implementing vector spaces,
etc. A part of it is

class Module v s
 where
  (*) :: s-v-v

defining the multiplication of a vector by a scalar: w = a*v
Now, as in many other circumstances, concrete vectors are based
on concrete scalars, and I defined really:   class Module v s | v-s  .

One typical instance of vectors is the orthodox functional 
construction

instance Num s = Module (v-s) s 
 where
  (s*f) x = s * (f x)

and such tests:  u = 2.5 * sin;   res = u 3.14
pass without tears.

But I wanted also that operators of the type (b-s) - (b-s),
for example:  inver f = recip . f . recip
be vectors. So:

instance ...= Module ((v-s)-(v-s)) s
 where
  (s*op) f = s*(op f)

But GHCi yells that two instances in view of the functional 
dependency declared are in conflict. Since I believe that 
I do not really understand fundeps in Haskell, and this is not
a GHC 'feature' only, I send this query to the haskell list.
I don't see this conflict. I could remove the fundep, but then
I have other (smaller, but a bit annoying) problems, so I want
to keep it, if only for my own instruction. Good people, help,
please.

Why v [-s]  cannot coexist in this context with
 ((v-s)-v) [-s]

Of course all extensions, including overlapping instances are on.

Jerzy Karczmarczuk
Caen, France
___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: A question concerning functional dependencies

2002-09-02 Thread Ashley Yakeley

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

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: A question concerning functional dependencies

2002-09-02 Thread Dylan Thurston

On Mon, Sep 02, 2002 at 03:11:58AM -0700, Ashley Yakeley wrote:
 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.

Even taking contexts into account, these two might still conflict: if
you had

instance Num ((v-s)-v)

for some values of v and s, you would get a conflict.

GHC (and Hugs) check for potential conflicts like this unless you
explicitly allow overlapping instances.

--Dylan



msg11456/pgp0.pgp
Description: PGP signature


Re: A question concerning functional dependencies

2002-09-02 Thread Ashley Yakeley

At 2002-09-02 07:47, Dylan Thurston wrote:

GHC (and Hugs) check for potential conflicts like this unless you
explicitly allow overlapping instances.

AFAIK, even with overlapping instances allowed, GHC will still complain 
if there's a fundep.

See 
http://sourceforge.net/tracker/?func=detailaid=441389group_id=8032atid=
108032.

-- 
Ashley Yakeley, Seattle WA

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell