Cons equality in Kawa Scheme

Skip to first unread message

Maciek Godek

Sep 18, 2021, 2:44:30 AMSep 18
I would normally post this to the Kawa mailing list, but for some reason my messages fail to pass through. So maybe someone here will be able to help.

For some time, I've been working on a graphical Scheme programming environment called GRASP. I recently managed to embed the Kawa evaluator, and I really love the effects:

I have been considering an even tighter integration with Kawa: while most of the source code is written in Java, it would make sense to port at least some bits of it to Kawa, bscause then I could develop the editor in itself.

Also, the representation of S-sxpressions that I've bee using internally, is really weird, and I would prefer to use regular cons cells instead. But I need to store some meta-data somewhere (mostly about whitespace between elements)

I thought that it would be nice to store that informatio in weak hash tables. I wrote a prototype parser in Guile:

I tried porting this parser to Kawa, using their WeakHasbMap class. Unfortunately, it turnsd out that Kawa's gnu.lists.Pair class overrides the 'equals' method, providing it with equal?-like semantics (rather than eq?-like), which messes everything up.

So I've been wondering what would be the optimal workaround in this situation. I've been considering making a fork of Kawa, where I would remove those overrides, but I don't know to what extent Kawa relies on that semantics internally.

On the other hand, it would be preferable to come up with a less intrusive solution, so that I could go with tge generic kawa jar bundle.

Is there anyone here who could help?

Maciek Godek

Sep 19, 2021, 7:18:30 AMSep 19
So, I have managed to solve the issue without modifying Kawa sources,
by subclassing Kawa's Pair class and overriding its ‘equals' and 'hashCode' methods,
and then replace the reference to cons with the constructor of the derived class.

In Kawa, it looks like this

(import (class gnu.lists Pair))
(import (class java.lang System))

(define-simple-class cons (Pair)
((*init* a d) (invoke-special Pair (this) '*init* a d))
((equals object) ::boolean (eq? object this))
((hash-code) ::int (System:identity-hash-code this)))

or in Java:

import gnu.lists.Pair;
import java.lang.System;

class Cons extends Pair {
public Cons(Object a, Object d) {
super(a, d);

public boolean equals(Object x) {
return this == x;

public int hashCode() {
return System.identityHashCode(this);

Here's a complete solution that works identically on both Kawa and Guile:
Reply all
Reply to author
0 new messages