Functional Dependency

Saswat Anand iscp9157@nus.edu.sg
Wed, 6 Jun 2001 19:30:42 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_0007_01C0EEBF.2CC13E40
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,
I am trying to define a commutative operator :
 (%+) :: a -> b -> c
 (%+) :: b -> a -> c

instead of defining a class and instantiating twice every time, I am =
trying to do the following:

class Commutative a b c where
 (%+) :: a -> b -> c

class Operation a b c | a b -> c where
 (%+%) :: a -> b -> c

instance (Operation a b c) =3D> Commutative a b c where
  (%+) =3D  (%+%)

instance (Operation b a c) =3D> Commutative a b c where
  (%+) =3D flip (%+%)

--=20
instance Operation Int (Maybe Int) Int where
 i %+% (Just s) =3D i + s
---

With hugs -98 +mo
((2::Int) %+ (Just (2::Int)))::Int gives 4
But (2::Int) %+ (Just (2::Int)) is of type  Commutative Int (Maybe Int) =
a =3D> a. So is (Just (2::Int)) %+ (2::Int)

Is the dependency in Operation class not sufficient. Any work around?

Which flags to be set for Ghc to accept this code. I tried =
-fallow-overlapping-instances and
-fallow-undecidable-instances.

Thanks,
S










------=_NextPart_000_0007_01C0EEBF.2CC13E40
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi,<BR>I am trying to define a =
commutative operator=20
:<BR>&nbsp;(%+) :: a -&gt; b -&gt; c<BR>&nbsp;(%+) :: b -&gt; a -&gt;=20
c</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instead of defining a class and =
instantiating twice=20
every time, I am trying to do the following:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><BR>class Commutative a b c =
where<BR>&nbsp;(%+) ::=20
a -&gt; b -&gt; c</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class Operation a b c | a b -&gt; c=20
where<BR>&nbsp;(%+%) :: a -&gt; b -&gt; c</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instance (Operation a b c) =3D&gt; =
Commutative a b c=20
where<BR>&nbsp; (%+) =3D&nbsp; (%+%)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instance (Operation b a c) =3D&gt; =
Commutative a b c=20
where<BR>&nbsp; (%+) =3D flip (%+%)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>-- </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>instance Operation Int (Maybe Int) Int=20
where<BR>&nbsp;i %+% (Just s) =3D i + s</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>---</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><BR>With hugs -98 +mo<BR>((2::Int) %+ =
(Just=20
(2::Int)))::Int gives 4<BR>But (2::Int) %+ (Just (2::Int)) is of =
type&nbsp;=20
Commutative Int (Maybe Int) a =3D&gt; a. So is (Just (2::Int)) %+=20
(2::Int)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Is the dependency in Operation class =
not=20
sufficient. Any work around?</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Which flags to be set for Ghc to accept =
this code.=20
I tried -fallow-overlapping-instances=20
and<BR>-fallow-undecidable-instances.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,<BR>S</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><BR></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0007_01C0EEBF.2CC13E40--