Number[A] trait?

Hello,

In Haskell one can define a simple function like this:

Prelude> let add1 x = x + 1
3
3.0
add1 :: (Num a) => a -> a

Would it be possible to do the same thing in Scala? I mean:
def add1[U <% Number[U]](x:U) = x + 1

I made several attempt to define a trait Number[A] like in Ordered[A] but all of them failed. I guess if it was easy it would already be there...

Thanks,
Sebastien

Re: Number[A] trait?

Couldn't you just use a structural type?
Something like
def add1[T](v: { def +(T):T }):T = v + 1

(Not entirely sure about the syntax...
BR,John
On Fri, Jan 9, 2009 at 12:26 AM, Sebastien Bocq <sebastien [dot] bocq [at] gmail [dot] com> wrote:
Hello,

In Haskell one can define a simple function like this:

Prelude> let add1 x = x + 1
3
3.0
add1 :: (Num a) => a -> a

Would it be possible to do the same thing in Scala? I mean:
def add1[U <% Number[U]](x:U) = x + 1

I made several attempt to define a trait Number[A] like in Ordered[A] but all of them failed. I guess if it was easy it would already be there...

Thanks,
Sebastien

Re: Number[A] trait?

John,

In theory, yes.  But it doesn't work in Scala.

Ricky.

2009/1/9 John Nilsson <john [at] milsson [dot] nu>
Couldn't you just use a structural type?
Something like
def add1[T](v: { def +(T):T }):T = v + 1

(Not entirely sure about the syntax...
BR,John
On Fri, Jan 9, 2009 at 12:26 AM, Sebastien Bocq <sebastien [dot] bocq [at] gmail [dot] com> wrote:
Hello,

In Haskell one can define a simple function like this:

Prelude> let add1 x = x + 1
3
3.0
add1 :: (Num a) => a -> a

Would it be possible to do the same thing in Scala? I mean:
def add1[U <% Number[U]](x:U) = x + 1

I made several attempt to define a trait Number[A] like in Ordered[A] but all of them failed. I guess if it was easy it would already be there...

Thanks,
Sebastien

Re: Number[A] trait?

Sebastien Bocq wrote:
> a trait Number[A] like in Ordered[A]

http://lampsvn.epfl.ch/svn-repos/scala/scala/branches/devel-base-2.8.0/s...

The code appears in the recent scala-2.8.0... nightly builds from
http://www.scala-lang.org/node/212 : download and install, then expand
src/scala-library-src.jar and see scalax/Numeric.scala

Re: Re: Number[A] trait?

Along the same lines:

We all know that equality is a tricky issue
(cfr. discussion in one of the chapters of the Scala Book)

... just out of interest ...

I guess that defining equality in terms of a trait Equal[A]
has never been an option because of Java compatibility issues?

Or am I plain wrong here?

Luc

On Fri, Jan 9, 2009 at 10:37 AM, Eric Willigers <ewilligers [at] gmail [dot] com> wrote:
Sebastien Bocq wrote:
a trait Number[A] like in Ordered[A]

http://lampsvn.epfl.ch/svn-repos/scala/scala/branches/devel-base-2.8.0/src/library/scalax/Numeric.scala

The code appears in the recent scala-2.8.0... nightly builds from http://www.scala-lang.org/node/212 : download and install, then expand src/scala-library-src.jar and see scalax/Numeric.scala

--
__~O
-\ <,
(*)/ (*)

reality goes far beyond imagination

Re: Re: Number[A] trait?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Luc,
I prefer the flexibility of such a trait:

Then I use === (that's 3 =) and /= methods for comparing for equality:

Luc Duponcheel wrote:
> Along the same lines:
>
> We all know that equality is a tricky issue
> (cfr. discussion in one of the chapters of the Scala Book)
>
> ... just out of interest ...
>
> I guess that defining equality in terms of a trait Equal[A]
> has never been an option because of Java compatibility issues?
>
> Or am I plain wrong here?
>
> Luc
>
> On Fri, Jan 9, 2009 at 10:37 AM, Eric Willigers
> > wrote:
>
> Sebastien Bocq wrote:
>
> a trait Number[A] like in Ordered[A]
>
>
>
http://lampsvn.epfl.ch/svn-repos/scala/scala/branches/devel-base-2.8.0/s...
>
> The code appears in the recent scala-2.8.0... nightly builds
> from http://www.scala-lang.org/node/212 : download and install,
> then expand src/scala-library-src.jar and see scalax/Numeric.scala
>
>
>
>
> --
> __~O
> -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
>

- --
Tony Morris
http://tmorris.net/

S, K and I ought to be enough for anybody.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklnM3kACgkQmnpgrYe6r63n1ACfdhZexlxixwk6Appfx9RKWSPx
Tj4AoMWLbhjYebFFvUQl5IScFNUpAsSj
=8dqs
-----END PGP SIGNATURE-----

Re: Re: Number[A] trait?

Just add some prototypes and get rid of static typing, and I'll be in heaven... or javascript whichever is closer.

On Fri, Jan 9, 2009 at 6:22 AM, Tony Morris <tmorris [at] tmorris [dot] net> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Luc,
I prefer the flexibility of such a trait:

Then I use === (that's 3 =) and /= methods for comparing  for equality:

Luc Duponcheel wrote:
> Along the same lines:
>
> We all know that equality is a tricky issue
> (cfr. discussion in one of the chapters of the Scala Book)
>
> ... just out of interest ...
>
> I guess that defining equality in terms of a trait Equal[A]
> has never been an option because of Java compatibility issues?
>
> Or am I plain wrong here?
>
> Luc
>
> On Fri, Jan 9, 2009 at 10:37 AM, Eric Willigers
> <ewilligers [at] gmail [dot] com <mailto:ewilligers [at] gmail [dot] com>> wrote:
>
>     Sebastien Bocq wrote:
>
>         a trait Number[A] like in Ordered[A]
>
>
>
http://lampsvn.epfl.ch/svn-repos/scala/scala/branches/devel-base-2.8.0/src/library/scalax/Numeric.scala
>
>     The code appears in the recent scala-2.8.0... nightly builds
>     from http://www.scala-lang.org/node/212 : download and install,
>     then expand src/scala-library-src.jar and see scalax/Numeric.scala
>
>
>
>
> --
>   __~O
>  -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
>

- --
Tony Morris
http://tmorris.net/

S, K and I ought to be enough for anybody.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklnM3kACgkQmnpgrYe6r63n1ACfdhZexlxixwk6Appfx9RKWSPx
Tj4AoMWLbhjYebFFvUQl5IScFNUpAsSj
=8dqs
-----END PGP SIGNATURE-----

Re: Re: Number[A] trait?

trait Dynamic { def apply(sym: Symbol, arg: Dynamic): Dynamic }

The remainder left as an exercise.

2009/1/9 Josh Suereth <joshua [dot] suereth [at] gmail [dot] com>
Just add some prototypes and get rid of static typing, and I'll be in heaven... or javascript whichever is closer.

On Fri, Jan 9, 2009 at 6:22 AM, Tony Morris <tmorris [at] tmorris [dot] net> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Luc,
I prefer the flexibility of such a trait:

Then I use === (that's 3 =) and /= methods for comparing  for equality:

Luc Duponcheel wrote:
> Along the same lines:
>
> We all know that equality is a tricky issue
> (cfr. discussion in one of the chapters of the Scala Book)
>
> ... just out of interest ...
>
> I guess that defining equality in terms of a trait Equal[A]
> has never been an option because of Java compatibility issues?
>
> Or am I plain wrong here?
>
> Luc
>
> On Fri, Jan 9, 2009 at 10:37 AM, Eric Willigers
> <ewilligers [at] gmail [dot] com <mailto:ewilligers [at] gmail [dot] com>> wrote:
>
>     Sebastien Bocq wrote:
>
>         a trait Number[A] like in Ordered[A]
>
>
>
http://lampsvn.epfl.ch/svn-repos/scala/scala/branches/devel-base-2.8.0/src/library/scalax/Numeric.scala
>
>     The code appears in the recent scala-2.8.0... nightly builds
>     from http://www.scala-lang.org/node/212 : download and install,
>     then expand src/scala-library-src.jar and see scalax/Numeric.scala
>
>
>
>
> --
>   __~O
>  -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
>

- --
Tony Morris
http://tmorris.net/

S, K and I ought to be enough for anybody.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklnM3kACgkQmnpgrYe6r63n1ACfdhZexlxixwk6Appfx9RKWSPx
Tj4AoMWLbhjYebFFvUQl5IScFNUpAsSj
=8dqs
-----END PGP SIGNATURE-----

Re: Number[A] trait?

I blogged about something related to this recently, and while my blog doesn't answer your question, James Iry provided this code in a comment that does:

http://paste.pocoo.org/show/97661/

And a bit of context: http://rickyclarkson.blogspot.com/2009/01/typeclass-pattern.html

2009/1/8 Sebastien Bocq <sebastien [dot] bocq [at] gmail [dot] com>
Hello,

In Haskell one can define a simple function like this:

Prelude> let add1 x = x + 1
3
3.0
add1 :: (Num a) => a -> a

Would it be possible to do the same thing in Scala? I mean:
def add1[U <% Number[U]](x:U) = x + 1

I made several attempt to define a trait Number[A] like in Ordered[A] but all of them failed. I guess if it was easy it would already be there...

Thanks,
Sebastien

Re: Number[A] trait?

On Thu, Jan 08, 2009 at 11:53:48PM +0000, Ricky Clarkson said
> I blogged about something related to this recently, and while my blog
> doesn't answer your question, James Iry provided this code in a comment that
> does:
>
> http://paste.pocoo.org/show/97661/
>
> And a bit of context:
> http://rickyclarkson.blogspot.com/2009/01/typeclass-pattern.html
>

This still does not allow you to write generic sections. I'm not
entirely sure but I think this is because in haskell numeric constants
are polymorphic:

Prelude> :t 1
1 :: (Num t) => t

while in scala they are monomorphic:

scala> 1
res0: Int = 1

This means in haskell the operation will drive the type of the constant
but in scala the constant will drive the type of the operation.

Re: Number[A] trait?

The technique in my blog post allows generic sections, whereas James' code snippet doesn't.

2009/1/9 Geoff Reedy <geoff [at] programmer-monk [dot] net>
On Thu, Jan 08, 2009 at 11:53:48PM +0000, Ricky Clarkson <ricky [dot] clarkson [at] gmail [dot] com> said
> I blogged about something related to this recently, and while my blog
> doesn't answer your question, James Iry provided this code in a comment that
> does:
>
> http://paste.pocoo.org/show/97661/
>
> And a bit of context:
> http://rickyclarkson.blogspot.com/2009/01/typeclass-pattern.html
>

This still does not allow you to write generic sections. I'm not
entirely sure but I think this is because in haskell numeric constants
are polymorphic:

Prelude> :t 1
1 :: (Num t) => t

while in scala they are monomorphic:

scala> 1
res0: Int = 1

This means in haskell the operation will drive the type of the constant
but in scala the constant will drive the type of the operation.

Re: Number[A] trait?

Suppose I use the trait with implicit conversions, if implicit conversions are performed for every numeric value wouldn't that kill off performance?

2009/1/9 Ricky Clarkson <ricky [dot] clarkson [at] gmail [dot] com>
The technique in my blog post allows generic sections, whereas James' code snippet doesn't.

2009/1/9 Geoff Reedy <geoff [at] programmer-monk [dot] net>
On Thu, Jan 08, 2009 at 11:53:48PM +0000, Ricky Clarkson <ricky [dot] clarkson [at] gmail [dot] com> said
> I blogged about something related to this recently, and while my blog
> doesn't answer your question, James Iry provided this code in a comment that
> does:
>
> http://paste.pocoo.org/show/97661/
>
> And a bit of context:
> http://rickyclarkson.blogspot.com/2009/01/typeclass-pattern.html
>

This still does not allow you to write generic sections. I'm not
entirely sure but I think this is because in haskell numeric constants
are polymorphic:

Prelude> :t 1
1 :: (Num t) => t

while in scala they are monomorphic:

scala> 1
res0: Int = 1

This means in haskell the operation will drive the type of the constant
but in scala the constant will drive the type of the operation.

Copyright © 2013 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland