public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* learning scheme with kawa
@ 2015-10-25 17:59 Debabrata Pani
  2015-10-25 18:36 ` Per Bothner
  0 siblings, 1 reply; 3+ messages in thread
From: Debabrata Pani @ 2015-10-25 17:59 UTC (permalink / raw)
  To: kawa

Hi,

I was reading The Scheme Programming Language 4th Edition and
practicing on Kawa. I wanted to use kawa because it sits atop jvm and
this can help in leveraging my experience in using java apis.

I ran into some issues while solving the following exercise

Exercise 2.7.2 had you use length in the definition of shorter, which
returns the shorter of its two list arguments, or the first if the two
have the same length. Write shorter without using length. [Hint:
Define a recursive helper, shorter?, and use it in place of the length
comparison.]

(reference : http://scheme.com/tspl4/start.html#./start:h2)

I wrote up the following(see below) in a file reciprocal.ss . The
expectation was that the out of order definition (of shorter and
shorter?) should not matter.

(define shorter
;; (shorter ‘(a b c) ‘(d e f g)) => (a b c)
;; (shorter ‘(a b c) ‘(d e f)) => (a b c)
;; (shorter ‘(a b c) ‘(d e)) => (d e)
;; don’t use the length function
(lambda (l1 l2)
(let ((val (shorter? l1 l2)))
(if (= 0 val)
l1
l2))))

(define shorter?
(lambda (l1 l2)
(if (null? l1)
0
(if (null? l2)
1
(shorter? (cdr l1) (cdr l2))))))

But in KAWA it does matter.

I got the following error:
|kawa:1|# (load “reciprocal.ss”)

reciprocal.ss:67:20: call to ‘shorter?’ has too many arguments (2; must be 1)
|kawa:2|# (shorter ‘(a b) ‘(d e f))

/dev/stdin:2:1: warning - no declaration seen for shorter
/dev/stdin:2:1: unbound location: shorter
at gnu.mapping.SharedLocation.get(SharedLocation.java:22)
at gnu.mapping.DynamicLocation.get(DynamicLocation.java:28)
at atInteractiveLevel$13.run(stdin:2)
at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:317)
at gnu.expr.ModuleExp.evalModule(ModuleExp.java:219)
at kawa.Shell.run(Shell.java:291)
at kawa.Shell.run(Shell.java:203)
at kawa.Shell.run(Shell.java:184)
at kawa.repl.main(repl.java:892)
|kawa:3|

But in Chicken scheme, I am able to go through :
;4> (load “reciprocal.ss”)

; loading reciprocal.ss …
;5> (shorter ‘(a b) ‘(d e f))

(a b)
;6>

I get the same error when executing the script using kawa

java -cp "/usr/local/Cellar/kawa/2.0/kawa-2.0.jar" kawa.repl -f reciprocal.ss
reciprocal.ss:67:20: call to 'shorter?' has too many arguments (2; must be 1)
reciprocal.ss:80:10: warning - no declaration seen for shorter
reciprocal.ss:80:10: unbound location: shorter
    at gnu.mapping.SharedLocation.get(SharedLocation.java:22)
    at gnu.mapping.DynamicLocation.get(DynamicLocation.java:28)
    at atInteractiveLevel$11.run(reciprocal.ss:80)
    at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:317)
    at gnu.expr.ModuleExp.evalModule(ModuleExp.java:219)
    at kawa.Shell.run(Shell.java:291)
    at kawa.Shell.runFile(Shell.java:523)
    at kawa.Shell.runFileOrClass(Shell.java:447)
    at kawa.repl.processArgs(repl.java:260)
    at kawa.repl.main(repl.java:871)


Query

Is this a known discrepancy in the behavior of kawa?

Are we violating any RNRS rule when we do this ?


Regards,

Debabrata Pani

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

* Re: learning scheme with kawa
  2015-10-25 17:59 learning scheme with kawa Debabrata Pani
@ 2015-10-25 18:36 ` Per Bothner
  2015-10-26  3:43   ` Debabrata Pani
  0 siblings, 1 reply; 3+ messages in thread
From: Per Bothner @ 2015-10-25 18:36 UTC (permalink / raw)
  To: Debabrata Pani, kawa



On 10/25/2015 10:59 AM, Debabrata Pani wrote:
> Is this a known discrepancy in the behavior of kawa?
>
> Are we violating any RNRS rule when we do this ?

It's a Kawa feature that is misbehaving.

As a work-around, try using require instead of load:

(require “reciprocal.ss”)

This causes the whole file to be processed as a unit,
so the definition of shorter? is visible when compiling shorter.

The gory details:

The "feature" is that Kawa has special handling when it
sees TYPE? .  If there is no known definitions of TYPE?,
but there is a known definition of TYPE then it will
convert the TYPE? to '(lambda (obj) (instance? obj TYPE))'

In this case, the Kawa compiler sees shorter?, there is (yet) no
definition of shorter?, but there is one of shorter, so it
does the above transformation.

I think this is a Kawa bug.  It should only convert TYPE?
to (lambda (obj) (instance? obj TYPE)) when TYPE is actually
bound to a class or a type.  I'll have to think about the best
way to do this.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

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

* Re: learning scheme with kawa
  2015-10-25 18:36 ` Per Bothner
@ 2015-10-26  3:43   ` Debabrata Pani
  0 siblings, 0 replies; 3+ messages in thread
From: Debabrata Pani @ 2015-10-26  3:43 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

Thank you for the information, Per.
On changing the method name shorter? -> is_shorter? it works fine.

Of course your suggestion to modify the behavior makes sense as well.

Regards,
Debabrata Pani

On Mon, Oct 26, 2015 at 12:06 AM, Per Bothner <per@bothner.com> wrote:
>
>
> On 10/25/2015 10:59 AM, Debabrata Pani wrote:
>>
>> Is this a known discrepancy in the behavior of kawa?
>>
>> Are we violating any RNRS rule when we do this ?
>
>
> It's a Kawa feature that is misbehaving.
>
> As a work-around, try using require instead of load:
>
> (require “reciprocal.ss”)
>
> This causes the whole file to be processed as a unit,
> so the definition of shorter? is visible when compiling shorter.
>
> The gory details:
>
> The "feature" is that Kawa has special handling when it
> sees TYPE? .  If there is no known definitions of TYPE?,
> but there is a known definition of TYPE then it will
> convert the TYPE? to '(lambda (obj) (instance? obj TYPE))'
>
> In this case, the Kawa compiler sees shorter?, there is (yet) no
> definition of shorter?, but there is one of shorter, so it
> does the above transformation.
>
> I think this is a Kawa bug.  It should only convert TYPE?
> to (lambda (obj) (instance? obj TYPE)) when TYPE is actually
> bound to a class or a type.  I'll have to think about the best
> way to do this.
> --
>         --Per Bothner
> per@bothner.com   http://per.bothner.com/

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

end of thread, other threads:[~2015-10-26  3:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-25 17:59 learning scheme with kawa Debabrata Pani
2015-10-25 18:36 ` Per Bothner
2015-10-26  3:43   ` Debabrata Pani

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).