peculiar behaviour in REPL

23 views
Skip to first unread message

Siddhartha Gadgil

unread,
Apr 30, 2015, 2:29:21 AM4/30/15
to spire...@googlegroups.com
I have been experimenting with using spire for interval arithmetic. Strangely, I can run some code fine in a scala worksheet (in eclipse scala-ide), and run it by copy pasting in the console (both sbt console and Li Haoyi's ammonite REPL), but on importing and loading I get an error. Is there something I am misunderstading, or is this a bug? Below is the full code as well as the full console session. Any help is appreciated.

regards,
Siddhartha

Code (the problem is with J, so the stream may not be relevant):

package compact_enumeration

import spire.math._
import spire.algebra._
import spire.implicits._
import spire.syntax.literals._
import annotation.tailrec

import Stream._

/**
 * @author gadgil
 * 
 * Interval bounds for exp, log, sin, cos
 */
class ApproxTrig(N: SafeLong) {
  import ApproxTrig.{get, Nat}
  
  import spire.syntax.literals._
  
  val width = r"1" / N
  
  lazy val J = Interval.closed(r"0", width)

  
  import Interval._
  
  /**
   * stream of bounds on the exponential.
   * At n, this is an interval containing e^(n/ N)
   */
  lazy val expstream : Stream[Interval[Rational]] = Nat map ((n: SafeLong) => 
    if (n ==0) Interval.point(Rational(1))
    else
      {
      val b = get(expstream, n-1)
      val a = b * Interval.closed(r"1/2" , (1 + width) / (2 - (width * width)))
      (a * width * width) + (b * (width + 1))
    })
  
  
  
}

object ApproxTrig{
  val Nat: Stream[SafeLong] = 0 #:: (Nat map ((n) => n + 1))
  
  @tailrec def get[A](as: Stream[A], n: SafeLong) : A = {
    if (n ==0) as.head else get(as.tail, n-1)
  }
}

Running in REPL (ammonite-repl): similar errors in sbt-console

Loading Ammonite Repl...
@ import compact_enumeration._
import compact_enumeration._
@ import compact_enumeration.ApproxTrig
import compact_enumeration.ApproxTrig
@   import spire.math._
import  import spire.math._
@ import spire.algebra._
import spire.algebra._
@ import spire.implicits._
import spire.implicits._
@ import spire.syntax.literals._
import spire.syntax.literals._
@ val apr = new ApproxTrig(500)
apr: compact_enumeration.ApproxTrig = compact_enumeration.ApproxTrig@1c6ff4b
@ apr.width
res7: spire.math.Rational = 1/500
@ apr.J
java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
ammonite.repl.interp.Evaluator$$anon$3.ammonite$repl$interp$Evaluator$$anon$$evalMain(Evaluator.scala:148)
ammonite.repl.interp.Evaluator$$anon$3$$anonfun$processLine$1$$anonfun$apply$15$$anonfun$apply$16$$anonfun$apply$1.apply$mcV$sp(Evaluator.scala:208)
java.lang.NoSuchMethodError: spire.math.Interval$.closed(Ljava/lang/Object;Ljava/lang/Object;Lspire/algebra/Order;Lspire/algebra/AdditiveMonoid;)Lspire/math/Interval;
compact_enumeration.ApproxTrig.J$lzycompute(Unknown Source)
compact_enumeration.ApproxTrig.J(Unknown Source)
cmd8$.<init>(Main.scala:741)
cmd8$.<clinit>(Main.scala)
cmd8.$main(Main.scala)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
ammonite.repl.interp.Evaluator$$anon$3.ammonite$repl$interp$Evaluator$$anon$$evalMain(Evaluator.scala:148)
ammonite.repl.interp.Evaluator$$anon$3$$anonfun$processLine$1$$anonfun$apply$15$$anonfun$apply$16$$anonfun$apply$1.apply$mcV$sp(Evaluator.scala:208)



Erik Osheim

unread,
Apr 30, 2015, 10:35:36 AM4/30/15
to Siddhartha Gadgil, spire...@googlegroups.com
Hello,

So I pasted your code into a file called sid0.scala. I had to put the
companion object before the class (due to annoying limitations with
:load) but after that I was able to evaluate J correctly in the REPL:

scala> :load /Users/erik/t/sid/sid0.scala
Loading /Users/erik/t/sid/sid0.scala...
<console>:1: error: illegal start of definition
package compact_enumeration
^
import spire.math._
import spire.algebra._
import spire.implicits._
import spire.syntax.literals._
import annotation.tailrec
import Stream._
defined object ApproxTrig
defined class ApproxTrig
warning: previously defined object ApproxTrig is not a companion to class ApproxTrig.
Companions must be defined together; you may wish to use :paste mode for this.

scala> new ApproxTrig(33).J
res1: spire.math.Interval[spire.math.Rational] = [0, 1/33]

I'm not totally clear on which things work for you and which
don't. What would I have to do to replicate the error you are seeing?

-- Erik

On Wed, Apr 29, 2015 at 11:29:21PM -0700, Siddhartha Gadgil wrote:
> I have been experimenting with using spire for interval arithmetic.
> Strangely, I can run some code fine in a scala worksheet (in eclipse
> scala-ide), and run it by copy pasting in the console (both sbt console and
> Li Haoyi's ammonite REPL), but on importing and loading I get an error. Is
> there something I am misunderstading, or is this a bug? Below is the full
> code as well as the full console session. Any help is appreciated.
>
> regards,
> Siddhartha
>
> *Code (the problem is with J, so the stream may not be relevant):*
> *Running in REPL (ammonite-repl): similar errors in sbt-console*

Siddhartha Gadgil

unread,
Apr 30, 2015, 12:24:49 PM4/30/15
to Erik Osheim, spire...@googlegroups.com

Thanks.
It looks to me that the problem is with the ammonite REPL. My build.sbt has an initialcommand in console which loads it, which seems to be involved in the error message. I will confirm this and file an issue there.

Siddhartha

Som Snytt

unread,
Apr 30, 2015, 1:06:59 PM4/30/15
to Erik Osheim, spire...@googlegroups.com
> annoying limitations with :load

scala> :pa -raw /tmp/code.scala
Pasting file /tmp/code.scala...

scala> foo.Foo(7)
res0: foo.Foo = foo.Foo@41975e01


where

$ cat /tmp/code.scala

package foo

class Foo(val i: Int)
object Foo { def apply(i: Int) = new Foo(i) }



Eiríkr Åsheim

unread,
Apr 30, 2015, 1:44:37 PM4/30/15
to Som Snytt, Erik Osheim, spire...@googlegroups.com
Awesome! I've wanted that for some time -- thanks!

Siddhartha Gadgil

unread,
Apr 30, 2015, 2:08:27 PM4/30/15
to Eiríkr Åsheim, Som Snytt, Erik Osheim, spire...@googlegroups.com
Confirmed that the bug is in Ammonite-REPL. When this is disabled it loads fine.

thanks,
Siddhartha
Reply all
Reply to author
Forward
0 new messages