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> </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> </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> </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> Plus a CF =
CF=20
where --to support 4.0 \+ fc</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> f =
\+ g =3D \c=20
-> (realToFrac f) \+ (g c)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>I did not do : </FONT><FONT =
face=3DArial=20
size=3D2> instance Plus Float CF CF where .. so that I =
don't have=20
to write </FONT><FONT face=3DArial size=3D2> (4::Float) =
\+ fc.=20
</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>But it complains because, it violates =
functional=20
dependency a b -> c. Is there any other way to avoid writing =
(4::Float) in=20
this case.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Well I tried this:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>(\+) :: (Lift a c, Lift b c) =3D> a =
-> b ->=20
(c -> Float)<BR>f \+ g =3D \x -> (lift f) x + (lift f) =
x</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>class Lift a b where<BR> lift :: a =
-> (b=20
-> Float)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift CF Char =
where<BR> lift =3D=20
id</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift CF (Char,Integer) =
where<BR> lift=20
f =3D \(c,i) -> f c</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift IF Integer =
where<BR> lift =3D=20
id</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift IF (Char,Integer) =
where<BR> lift=20
f =3D \(c,i) -> f i</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>instance Lift CIF (Char,Integer)=20
where<BR> lift =3D id</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>But it 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 -> =
Float) Char, Lift=20
(Char -> FLoat) a)</DIV></FONT>
<DIV> </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--