Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reals are less than ints

2 views
Skip to first unread message

Niklas Höglund

unread,
Jun 2, 2003, 12:39:46 PM6/2/03
to
Sometimes, I use reals in Standard ML. They are sometimes a bit
annoying for someone using my coding style. Look for example at the
following program:

signature EX = sig
val double : real -> real
end

structure Ex :> EX =
struct
fun double x =
x+x
end

I know I can fix this program by specifying the type of double in the
structure, but I prefer to specify all types in the signature, and leave
the code in the structure uncluttered by types. Adding types to
structures does add unneccessary redundancy.

A clever compiler could handle this case.

Overall, I think SML is a bit schizofrenic when it comes to +-*/. I'd
like to be able to use + to add integers, reals, fractions, matrices,
etc. (Ad hoc overloading or something.) I can also buy the OCaml way
of no overloading, but I think the current way of "a little" overloading
is suboptimal.

The following would be a good solution, I think:

structure Int =
struct
val + = ...
infix +
overloaded +
end

structure Matrix =
struct
val + = ...
infix +
overloaded +
end

open Int

(* + here is Int.+ *)

open Matrix

(* + here is either Int.+ or Matrix.+, like the current situation with
Int and Real, but not hardcoded *)


The top-level environment could contain the following:

overloaded +
val + = Int.+
val + = Real.+

An added benefit of this is that opening say Word to get easy access to
andb, orb, ... would not destroy + as it currently does.

My suggestion of syntax may not be the greatest. Maybe something in
line with "val rec" should be used: "val overloaded + = Int.+". With
this syntax, doing "val + = Int.+" could remove all overloaded
instances.

What do you think? How should this work?

--
Niklas

Aaron Denney

unread,
Jun 2, 2003, 4:29:37 PM6/2/03
to
In article <bbfuki$b18$1...@wolfberry.srv.cs.cmu.edu>, Niklas Höglund wrote:
> What do you think? How should this work?

Have you looked at the Numeric typeclasses of Haskell?

--
Aaron Denney
-><-

Niklas Höglund

unread,
Jun 4, 2003, 5:18:52 PM6/4/03
to
In article <bbgc3h$idd$1...@wolfberry.srv.cs.cmu.edu>, Aaron Denney wrote:
> In article <bbfuki$b18$1...@wolfberry.srv.cs.cmu.edu>, Niklas Höglund wrote:
>> What do you think? How should this work?
>
> Have you looked at the Numeric typeclasses of Haskell?

Yes. That may be a pretty good solution.

There could be some problems, though. I believe there's too much
structure/organization in the Haskell type classes. This is just a gut
fealing, though.

If I have a type class for the operations +, -, * and /, and want to add
matrices that don't have a / operator, I can't leave that operation out.
On the other hand, that might be an indication that the original classes
were wrong and should be separated into one class for +, - and * and one
for /.

--
Niklas

Aaron Denney

unread,
Jun 4, 2003, 7:16:53 PM6/4/03
to
In article <bblnns$r4i$1...@wolfberry.srv.cs.cmu.edu>, Niklas Höglund wrote:
> If I have a type class for the operations +, -, * and /, and want to add
> matrices that don't have a / operator, I can't leave that operation out.
> On the other hand, that might be an indication that the original classes
> were wrong and should be separated into one class for +, - and * and one
> for /.

Yes, there was some discussion on the lists about this a while back.
There were a couple of proposals to restructure the numeric typeclasses,
but no consensus was achieved -- some of the proposals tried to break
things up too far for convenience.

Alex Drummond

unread,
Jun 17, 2003, 1:46:41 PM6/17/03
to
Niklas Hglund wrote:

> Yes. That may be a pretty good solution.
>
> There could be some problems, though. I believe there's too much
> structure/organization in the Haskell type classes. This is just a gut
> fealing, though.

> If I have a type class for the operations +, -, * and /, and want to add
> matrices that don't have a / operator, I can't leave that operation out.

True, but you could just give a dummy implementation of division, or one
that exited the program with an error message. It's ugly, but workable.

> On the other hand, that might be an indication that the original classes
> were wrong and should be separated into one class for +, - and * and one
> for /.

I think Clean might use this system.

Alex


0 new messages