Can Class do this?

Saswat Anand iscp9157@nus.edu.sg
Wed, 7 Mar 2001 01:00:05 -0800


This is a multi-part message in MIME format.

------=_NextPart_000_0015_01C0A6A1.F26DC700
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Thanks a lot. It works great with Hugs.=20

But it has a small problem. I also want to be able to write=20
=20
4 \+ fc :: CF
fc \+ 4 :: CF
ic \+ 4 :: IF
4 \+ fc \+ ic :: CIF

Well for that I did this,
instance (Rational a) =3D> Plus a CF CF where     --to support 4.0 \+ fc
       f \+ g =3D \c -> (realToFrac f) \+ (g c)

I did not do :  instance Plus Float CF CF where ..  so that I don't have =
to write    (4::Float) \+ fc.=20

But it complains because, it violates functional dependency a b -> c. Is =
there any other way to avoid writing (4::Float) in this case.

Well I tried this:

(\+) :: (Lift a c, Lift b c) =3D> a -> b -> (c -> Float)
f \+ g =3D \x -> (lift f) x + (lift f) x

class Lift a b where
 lift :: a -> (b -> Float)

instance Lift CF Char where
 lift =3D id

instance Lift CF (Char,Integer) where
 lift f =3D \(c,i) -> f c

instance Lift IF Integer where
 lift =3D id

instance Lift IF (Char,Integer) where
 lift f =3D \(c,i) -> f i

instance Lift CIF (Char,Integer) where
 lift =3D id

But it does not take following:
x :: CF
x =3D cf \+ cf \+ cf

Cannot justify constraints in explicitly typed binding
*****Expresssion : x
*****Type : CF
*****Given Context : ()
*****Constraints : (Lift (a -> Float) Char, Lift (Char -> FLoat) a)

Thanks,
Saswat

------=_NextPart_000_0015_01C0A6A1.F26DC700
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>
<DIV><FONT face=3DArial size=3D2>Thanks a lot. It works great with Hugs. =

</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>But it has a small problem. I also want =
to be able=20
to write </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>4 \+ fc :: CF</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>fc \+ 4 :: CF</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>ic \+ 4 :: IF</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>4 \+ fc \+ ic :: CIF</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Well for that I did this,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>instance (Rational a) =3D&gt; Plus a CF =
CF=20
where&nbsp;&nbsp;&nbsp;&nbsp; --to support 4.0 \+ fc</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f =
\+ g =3D \c=20
-&gt; (realToFrac f) \+ (g c)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I did not do :&nbsp;</FONT><FONT =
face=3DArial=20
size=3D2>&nbsp;instance Plus Float CF CF where&nbsp;..&nbsp; so that I =
don't have=20
to write&nbsp;</FONT><FONT face=3DArial size=3D2>&nbsp;&nbsp; (4::Float) =
\+ fc.=20
</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>But it complains because, it violates =
functional=20
dependency a b -&gt; c. Is there any other way to avoid writing =
(4::Float) in=20
this case.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Well I tried this:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>(\+) :: (Lift a c, Lift b c) =3D&gt; a =
-&gt; b -&gt;=20
(c -&gt; Float)<BR>f \+ g =3D \x -&gt; (lift f) x + (lift f) =
x</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class Lift a b where<BR>&nbsp;lift :: a =
-&gt; (b=20
-&gt; Float)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift CF Char =
where<BR>&nbsp;lift =3D=20
id</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift CF (Char,Integer) =
where<BR>&nbsp;lift=20
f =3D \(c,i) -&gt; f c</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift IF Integer =
where<BR>&nbsp;lift =3D=20
id</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift IF (Char,Integer) =
where<BR>&nbsp;lift=20
f =3D \(c,i) -&gt; f i</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift CIF (Char,Integer)=20
where<BR>&nbsp;lift =3D id</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>But it&nbsp;does not take =
following:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>x :: CF<BR>x =3D cf \+ cf \+ =
cf<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Cannot justify constraints in =
explicitly typed=20
binding</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>*****Expresssion : x</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>*****Type : CF</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>*****Given Context : ()</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>*****Constraints : (Lift (a -&gt; =
Float) Char, Lift=20
(Char -&gt; FLoat) a)</DIV></FONT>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2>Saswat</FONT></DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_0015_01C0A6A1.F26DC700--