From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Taylan Kammer <taylan.kammer@gmail.com>
Newsgroups: gmane.lisp.guile.user
Subject: Re: Equality and hashing for new datatypes
Date: Thu, 22 Jun 2023 13:33:06 +0200
Message-ID: <01fa3142-02d8-9bb0-9dfd-5b233aa57bbf@gmail.com>
References: <3759578.e9J7NaK4W3@bastet>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="1888"; mail-complaints-to="usenet@ciao.gmane.io"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
 Thunderbird/102.12.0
To: Philip McGrath <philip@philipmcgrath.com>, guile-user <guile-user@gnu.org>
Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Jun 22 13:33:56 2023
Return-path: <guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org>
Envelope-to: guile-user@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org>)
	id 1qCIZX-0000Bi-6n
	for guile-user@m.gmane-mx.org; Thu, 22 Jun 2023 13:33:55 +0200
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <guile-user-bounces@gnu.org>)
	id 1qCIYu-0007eK-N7; Thu, 22 Jun 2023 07:33:16 -0400
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <taylan.kammer@gmail.com>)
 id 1qCIYt-0007e7-9f
 for guile-user@gnu.org; Thu, 22 Jun 2023 07:33:15 -0400
Original-Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <taylan.kammer@gmail.com>)
 id 1qCIYq-0002RA-5g
 for guile-user@gnu.org; Thu, 22 Jun 2023 07:33:14 -0400
Original-Received: by mail-lf1-x132.google.com with SMTP id
 2adb3069b0e04-4f8775126d3so5241532e87.1
 for <guile-user@gnu.org>; Thu, 22 Jun 2023 04:33:10 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20221208; t=1687433589; x=1690025589;
 h=content-transfer-encoding:in-reply-to:from:content-language
 :references:to:subject:user-agent:mime-version:date:message-id:from
 :to:cc:subject:date:message-id:reply-to;
 bh=luenOK7zgY+lUQcz93CZ2MFVDGnXuxKzpmMXxVstsIg=;
 b=hre/+FTv1PGQMPs+go1lv8VMhDzADs4zktkOjiWMNwPvO7C5j+BsLCqqAnLYbHcW80
 cWx1GcHrfPzbRhKrSo0FvOkLQLg1EJCNpoklEX5AzbacChHHlg3Z7TMmel3V7CCnU3uz
 hR5G1o5yPywshOBufC6D0eb1rJ5vpU9+JWi/4M6voixv+OXaomgtM9edd+4XkLpJxEE0
 3L3k3wnuUwuAHDZGTwW9AoZBz/FAzqTrnFk06oDOypKeT3NfuegTngOTmXiZUasOV75G
 p/A4tG6y7ZpZ67XbAGX1bGEQCvjNZ+cL7hNvU9Sz7dcoqOEeaOcHyR7Qev8MAOYxPdzB
 p7oQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20221208; t=1687433589; x=1690025589;
 h=content-transfer-encoding:in-reply-to:from:content-language
 :references:to:subject:user-agent:mime-version:date:message-id
 :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=luenOK7zgY+lUQcz93CZ2MFVDGnXuxKzpmMXxVstsIg=;
 b=WWhLX4/EFWBroo0JKDofB/jjBN5hK4vo3shK2WXcV8b7TMGcAgcbWfqcmw8s1QWS50
 4eVVuUyuFIgeP7a2yNc7BQZnTUfXh7/ABL7kCniLmKMk+KTry7+zH/Zc9Itl5I/ryaF1
 PpX4gKs3yLIcA8V0A1jE4E9DUetIqEDzHzVmrY0a8iq2fF4e9uc6mLUGgZGMk1FpQJJf
 5biC23auil/JyCT8XkXl782duDTfU5dEaBK542zjeB1Zd7lWhPZ07xaXpJSFtYK9kACO
 d26G4P3NSiZpO1pIBISuqFZzMMRle77p6L9GNB5KJQ20EXzmbJihZJjTCwQ9gM4r3Wkx
 Jdfw==
X-Gm-Message-State: AC+VfDysQ1SH7P3qXAwpbELINOb/6nowYZZdpvRggZAdan0PuvrEcl0/
 L0cnnPuZwSaVeYD7RilcOurkL0+lCto=
X-Google-Smtp-Source: ACHHUZ6ds7Gp6r08YIBc5HJafVWvVjCW5Bs0grb/DPV+L5Chf0oT9z0xipEOVAL1oHHkz06uFRFI7w==
X-Received: by 2002:a05:6512:1094:b0:4f9:6965:4d61 with SMTP id
 j20-20020a056512109400b004f969654d61mr603259lfg.42.1687433588625; 
 Thu, 22 Jun 2023 04:33:08 -0700 (PDT)
Original-Received: from [192.168.178.20] (ip-109-090-125-150.um36.pools.vodafone-ip.de.
 [109.90.125.150]) by smtp.gmail.com with ESMTPSA id
 l5-20020a1ced05000000b003f70a7b4537sm18538707wmh.36.2023.06.22.04.33.07
 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
 Thu, 22 Jun 2023 04:33:07 -0700 (PDT)
Content-Language: en-US
In-Reply-To: <3759578.e9J7NaK4W3@bastet>
Received-SPF: pass client-ip=2a00:1450:4864:20::132;
 envelope-from=taylan.kammer@gmail.com; helo=mail-lf1-x132.google.com
X-Spam_score_int: -21
X-Spam_score: -2.2
X-Spam_bar: --
X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 NICE_REPLY_A=-0.09, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: guile-user@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: General Guile related discussions <guile-user.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/guile-user>,
 <mailto:guile-user-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/guile-user>
List-Post: <mailto:guile-user@gnu.org>
List-Help: <mailto:guile-user-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/guile-user>,
 <mailto:guile-user-request@gnu.org?subject=subscribe>
Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org
Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.lisp.guile.user:19058
Archived-At: <http://permalink.gmane.org/gmane.lisp.guile.user/19058>

On 22.06.2023 00:25, Philip McGrath wrote:
> Hi,
> 
> What is the recommended way for a library to customize `equal?` and `equal?`
> -based hashing for new datatypes it defines?
> 
> I know from [1] that `equal?` can be customized using GOOPS; however, some
> projects do not use GOOPS (e.g. [2]), so it would seem more friendly for a
> generic library to avoid it. Also, it is not clear if the same mechanism is
> even an option for customizing `equal-hash` to match `equal?`.
> 
> I'm looking for something like Racket's `gen:equal+hash` [3] or Chez Scheme's
> `record-equal-procedure` and `record-hash-procedure` [4].
> 
> This came up while I was working on a portable version of the immutable
> hash-table implementations from Racket-on-Chez.
> 
> -Philip
> 
> [1]: https://www.gnu.org/software/guile/manual/html_node/GOOPS-Object-Miscellany.html
> [2]: https://lists.gnu.org/archive/html/info-gnu/2023-05/msg00005.html
> [3]: https://docs.racket-lang.org/reference/Equality.html#%28part._.Implementing_.Equality_for_.Custom_.Types%29
> [4]: https://cisco.github.io/ChezScheme/csug9.5/objects.html#./objects:h14

Hi Philip,

You can use SRFI 69 hash tables in Guile.  This library offers a way to specify
the equality and hash procedures that should be used by a hash table, e.g.:

  (import (srfi srfi-69))

  (define (my-equal? x y)
    (equal? (foo x) (foo y)))

  (define (my-hash x)
    (hash (foo x)))  ; standard 'hash' procedure also imported from SRFI 69

  (define my-table
    (make-hash-table my-equal? my-hash))

Now any operation with my-table will use my-equal? and my-hash, which implement
an equality logic based on the result of calling 'foo' on objects.

Note that this doesn't affect the behavior of the standard 'equal?' procedure,
it only affects the SRFI 69 hash table operations on the created hash table.

I currently don't remember whether Guile's own non-portable hash table library
supports the same feature.  It probably does in some way.

The R6RS, SRFI 125, and SRFI 126 libraries also support it.  Guile supports R6RS,
so you could use the (rnrs hashtables) library instead of SRFI 69 if you desire.
SRFI 125 is an extension of 69, and SRFI 126 is an extension to R6RS hashtables,
but neither is supported in Guile I believe.
-- 
Taylan