public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* ClassCastException (class java.lang.Integer cannot be cast to class gnu.math.IntNum)
       [not found] <e65a1af5-112e-4874-9640-4878a79be01d@Spark>
@ 2023-02-24 14:26 ` Zachary Kurmas
  2023-02-24 15:18   ` Per Bothner
  0 siblings, 1 reply; 3+ messages in thread
From: Zachary Kurmas @ 2023-02-24 14:26 UTC (permalink / raw)
  To: Andy Keep via Kawa

[-- Attachment #1: Type: text/plain, Size: 1613 bytes --]

Does anybody recognize this exception:

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class gnu.math.IntNum (java.lang.Integer is in module java.base of loader 'bootstrap'; gnu.math.IntNum is in unnamed module of loader 'app')

I apologize for the overly vague question, but I can’t seem to come with a minimum example, so I was hoping someone had seen the error before and could point me in a useful direction.

If you are curious, here is an example of why I’m having trouble isolating the source of the error.

  *   I am having my students write Connect 4 in Kawa.  Only one student has run into this problem.
  *   The basic structure of the project is that
     *    (1) connect4.scm parses the command line (which is mostly code I wrote)
     *    (2) connect4_engine.scm has the students’ code.
  *   My code parses the command line, calls include-relative on the “engine” code then calls the function play-connect-4
  *   I see the bug when running connect4.scm — even if I hard-code all the parameters to play-connect-4.
  *   I don’t see the bug if I copy the call to play-connect-4 (with hard-coded parameters) to the end of connect4_engine.scm and run that file directly.

So, trying to isolate the bug makes it go away.  Also, when connect4.scm calls connet4_engine.scm the stack trace does not contain line numbers of connect4_engine, so I don’t have a good sense of where the bug is popping up.


Thanks,
Zack

--
Zachary Kurmas
Associate Professor of Computer Science
Grand Valley State University
https://kurmasgvsu.github.io

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ClassCastException (class java.lang.Integer cannot be cast to class gnu.math.IntNum)
  2023-02-24 14:26 ` ClassCastException (class java.lang.Integer cannot be cast to class gnu.math.IntNum) Zachary Kurmas
@ 2023-02-24 15:18   ` Per Bothner
  2023-02-24 15:41     ` Zachary Kurmas
  0 siblings, 1 reply; 3+ messages in thread
From: Per Bothner @ 2023-02-24 15:18 UTC (permalink / raw)
  To: Zachary Kurmas, Andy Keep via Kawa; +Cc: Kawa Community

(Please don't use Reply-to without the list.  If you post a question to the
list, the answer should also go to the list, with few exceptions.  One reason
is to avoid wasting people's time if multiple people answer.)

On 2/24/23 06:26, Zachary Kurmas via Kawa wrote:
> Does anybody recognize this exception:
> 
> java.lang.ClassCastException: class java.lang.Integer cannot be cast to class gnu.math.IntNum (java.lang.Integer is in module java.base of loader 'bootstrap'; gnu.math.IntNum is in unnamed module of loader 'app')

If it helps: java.lang.Integer is the standard "boxing" (i.e. conversion to Object) of 32-bit int values.
gnu.math.IntNum handles "infinite-precision" integers. It is like (but predates) java.math.BigInteger,
but with some optmizations and it is part of the gnu.math.Numeric hierarchy, which is used
to implement the Scheme "number tower".

Kawa can certainly convert java.lang.Integer to gnu.math.IntNum but it needs to actually
generate some code to do so - it can't just use a primitive cast. if you have Java code
that calls a Kawa method that expects a gnu.math.IntNum you may have to do the
conversion yourself.  (If you call the method indirectly as a Kawa Procedure object,
using one of the 'apply' method, I believe the 'apply' glue should be able to take
care of the conversion - but I'm not 100% sure.)

> So, trying to isolate the bug makes it go away.  Also, when connect4.scm calls connet4_engine.scm the stack trace does not contain line numbers of connect4_engine, so I don’t have a good sense of where the bug is popping up.

Are you getting a stack trace? If not, that might be helpful.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ClassCastException (class java.lang.Integer cannot be cast to class gnu.math.IntNum)
  2023-02-24 15:18   ` Per Bothner
@ 2023-02-24 15:41     ` Zachary Kurmas
  0 siblings, 0 replies; 3+ messages in thread
From: Zachary Kurmas @ 2023-02-24 15:41 UTC (permalink / raw)
  To: Andy Keep via Kawa, Per Bothner; +Cc: Kawa Community

[-- Attachment #1: Type: text/plain, Size: 5386 bytes --]

Writing this email turned into a little bit of “rubber-duck” debugging.
I think I see the source of the problem; but, I don’t understand why I’m getting the observed behavior.

TLDR:

The code I provided has an include-relative inside another function.  (I forget why I did this.)


(define (play-connect-4 num_rows num_columns win_length)
    ; TODO:  Load the .scm file containing your connect 4 code
    ; Then launch a connect4 game.
    (include-relative "connect4_engine.scm")

    (connect4-game num_rows num_columns win_length -1 '() 0 '())
)


The problematic function, rightDiagBounds (which is defined in connect4_engine) has a parameter named num_rows

(define (rightDiagBounds iterRow iterCol num_rows)
    (display "Here A.1\n")
    (force-output)
    (if(or (= iterRow (- num_rows 1)) (= iterCol 0))
        (cons iterRow (list iterCol))
        (rightDiagBounds (+ iterRow 1) (- iterCol 1) num_rows)
    )
)

I wonder if there is a problem with the way num_rows is shadowed.

The strange thing is that adding a “dummy” call to rightDiagBounds between the include-relative and connect4-game solves the problem.


Long version:

Here is the stack trace:

Argument #1 '0' to 'rightDiagBounds' has wrong type (java.lang.Integer) (class java.lang.Integer cannot be cast to class gnu.math.IntNum (java.lang.Integer is in module java.base of loader 'bootstrap'; gnu.math.IntNum is in unnamed module of loader 'app'))
 at connect4.playConnect$Mn4(connect4.scm:10397)
 at connect4.playConnect$Mn4$check(connect4.scm:30)
 at gnu.mapping.Procedure.applyToConsumerDefault(Procedure.java:75)
 at gnu.mapping.CallContext.runUntilDone(CallContext.java:586)
 at connect4.run(connect4.scm:44)
 at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:290)
 at gnu.expr.CompiledModule.evalModule(CompiledModule.java:42)
 at gnu.expr.CompiledModule.evalModule(CompiledModule.java:60)
 at kawa.Shell.runFile(Shell.java:571)
 at kawa.Shell.runFileOrClass(Shell.java:474)
 at kawa.repl.processArgs(repl.java:710)
 at kawa.repl.main(repl.java:830)
Caused by: java.lang.ClassCastException: class java.lang.Integer cannot be cast to class gnu.math.IntNum (java.lang.Integer is in module java.base of loader 'bootstrap'; gnu.math.IntNum is in unnamed module of loader 'app')
 ... 12 more

Does this mean that rightDiagBounds is expecting a gnu.math.IntNum, or that the code is somehow trying to provide one?  (If the latter, I don’t see where the gnu.math.IntNum is coming from.)

Here is the strange thing:

If I add a “dummy” call to rightDiagBounds after the include-relative but before calling connect4-game (the main entry point to the student’s code) the problem goes away.

If it helps, this is the starter code I provide the students:
https://github.com/kurmasz-assignments/cis343-connect4-scheme/blob/main/connect4.scm

They add their code in a file named connect4_engine.scm.
The problem goes a way when I add a call to rightDiagBounds (which the student wrote and put in connect4_engine.scm) on line 41.
The problem also goes away if I move the include-relative outside of play-connect-4.

I which I could remember why I put the include inside the function.  (I think I was trying to set it up so that my starter code would do something if the students ran it before writing their own code; but, I can’t remember exactly what I was thinking — and I still don’t understand the behavior I’m seeing)

--
Zachary Kurmas
Associate Professor of Computer Science
Grand Valley State University
https://kurmasgvsu.github.io
On Feb 24, 2023, 10:18 AM -0500, Per Bothner <per@bothner.com>, wrote:
(Please don't use Reply-to without the list. If you post a question to the
list, the answer should also go to the list, with few exceptions. One reason
is to avoid wasting people's time if multiple people answer.)

On 2/24/23 06:26, Zachary Kurmas via Kawa wrote:
Does anybody recognize this exception:

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class gnu.math.IntNum (java.lang.Integer is in module java.base of loader 'bootstrap'; gnu.math.IntNum is in unnamed module of loader 'app')

If it helps: java.lang.Integer is the standard "boxing" (i.e. conversion to Object) of 32-bit int values.
gnu.math.IntNum handles "infinite-precision" integers. It is like (but predates) java.math.BigInteger,
but with some optmizations and it is part of the gnu.math.Numeric hierarchy, which is used
to implement the Scheme "number tower".

Kawa can certainly convert java.lang.Integer to gnu.math.IntNum but it needs to actually
generate some code to do so - it can't just use a primitive cast. if you have Java code
that calls a Kawa method that expects a gnu.math.IntNum you may have to do the
conversion yourself. (If you call the method indirectly as a Kawa Procedure object,
using one of the 'apply' method, I believe the 'apply' glue should be able to take
care of the conversion - but I'm not 100% sure.)

So, trying to isolate the bug makes it go away. Also, when connect4.scm calls connet4_engine.scm the stack trace does not contain line numbers of connect4_engine, so I don’t have a good sense of where the bug is popping up.

Are you getting a stack trace? If not, that might be helpful.
--
--Per Bothner
per@bothner.com http://per.bothner.com/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-24 15:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <e65a1af5-112e-4874-9640-4878a79be01d@Spark>
2023-02-24 14:26 ` ClassCastException (class java.lang.Integer cannot be cast to class gnu.math.IntNum) Zachary Kurmas
2023-02-24 15:18   ` Per Bothner
2023-02-24 15:41     ` Zachary Kurmas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).