User-Defined Operators, Re: Function composition and currying

Dylan Thurston dpt@math.harvard.edu
Fri, 18 Jul 2003 04:08:25 -0400


--VS++wcV0S1rZb1Fb
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Jul 18, 2003 at 11:39:48AM +1000, Andrew J Bromage wrote:
> > Someone mentioned multiplying by a scalar. I think this is a
> > good application, but what we need is to agree (somehow) on
> > the symbol used. I've used (*.) and (.*), with the dot being
> > on the side the scalar is on (on the grounds that . is a
> > scalar product elsewhere), but without wide agreement I
> > agree that this sort of thing reduces readability, because
> > while I can read these programmes, it's harder for everyone
> > else.
>=20
> Yuck. :-)

What's wrong with that solution?  You might hope for something better,
but it seems like it would work for, e.g., your situation below.  Here
are my concrete suggestions, using a variant of this notation;
however, I mark the side that does _not_ have the scalar, on the
grounds that scalar * scalar should be the original operator, and use
'*>' or '<*' to do it.



> I've run into the same problem with affine algebra, which has two
> types, the Point and the Vector, where a Vector is the difference
> between two Points:
>=20
> 	Vector + Vector =3D Vector
 v1 + v2 =3D v3
> 	Vector + Point =3D Point
  v1 +> p1 =3D p2
> 	Point + Vector =3D Point
  p1 <+ v1 =3D p2
> 	Point + Point is an error
>=20
> 	Vector - Vector =3D Vector
  v1 - v2 =3D v3
> 	Point - Vector =3D Point
  p1 <- v1 =3D p2
> 	Vector - Point =3D Point	-- (this rule is a bit controversial)
This one is obviously an error.  Add Point to both sides to get the
error that you noted above.
> 	Point - Point =3D Vector
  p1 <-> p2 =3D v1

The other potential solution is to use an 'Additive' class

class Additive a b c | a b -> c, c a -> b, c b -> a where
  (+) :: a -> b -> c
class (Additive c b a) =3D> Subtractive a b c where
  (-) :: a -> b -> c

> One solution might be to relax the rules about how the types of
> operators are resolved.  At the moment, you can define function
> names from different modules and all you need to do is qualify
> them when you use them.  It's a little odd that you can't do
> something similar with operators, though no succinct syntax leaps
> to mind.

But you can use qualified names for operators!  It just looks
incredibly ugly.  But you could write, say,

  1 Float.+ 2

Peace,
	Dylan

--VS++wcV0S1rZb1Fb
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/F6r5Veybfhaa3tcRAgePAJ0SvFAzTJuoUnWcgGazFXpieKfaZgCcCnU2
m9EykAlMefniXzsPBarc3N8=
=tx8Z
-----END PGP SIGNATURE-----

--VS++wcV0S1rZb1Fb--