changelog shortlog graph tags branches changeset files file revisions raw help

Mercurial > core / annotate lisp/lib/obj/equiv.lisp

changeset 384: 8fe057887c17
parent: cb6effbda1dd
author: Richard Westhaver <ellis@rwest.io>
date: Wed, 29 May 2024 23:29:40 -0400
permissions: -rw-r--r--
description: skel refactor1
230
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
1
 ;;; obj/equiv.lisp --- Object Equivalence
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
2
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
3
 ;; extended equivalence API for Lisp objects
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
4
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
5
 ;;; Refs:
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
6
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
7
 ;; https://en.wikipedia.org/wiki/E-graph
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
8
 
236
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
9
 ;; https://en.wikipedia.org/wiki/Equivalence_relation
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
10
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
11
 ;; https://doc.rust-lang.org/std/cmp/index.html
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
12
 
230
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
13
 ;; https://gitlab.com/fstamour/catchall/-/tree/master/egraph
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
14
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
15
 ;; https://en.wikipedia.org/wiki/Disjoint-set_data_structure
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
16
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
17
 ;; https://clojure.org/guides/equality
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
18
 
236
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
19
 ;;; Commentary:
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
20
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
21
 ;; A valid complaint of Common Lisp is the lack of an intuitive
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
22
 ;; equality mechanism. We have the following symbols in the
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
23
 ;; standard. If you want to write CL you need to have a very firm
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
24
 ;; grasp on these:
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
25
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
26
 #|
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
27
 = EQ EQL EQUAL EQUALP TREE-EQUAL
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
28
 |#
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
29
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
30
 ;; EQUALP is the most flexible, but it is still explicitly conforming
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
31
 ;; to the spec. It does handle STRUCTURE objects as one might expect,
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
32
 ;; but it will not handle CLASSes. 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
33
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
34
 ;; As a rule, don't mess with these (i.e. redefine them, or anything
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
35
 ;; in the standard for that matter) and use them wherever
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
36
 ;; possible. Anything that is testing equality between two lisp
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
37
 ;; objects is very likely to boil down to one of the symbols above,
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
38
 ;; and the compiler handles the rest.
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
39
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
40
 ;; For simplicity, let us consider these symbols provided by the
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
41
 ;; standard to be of the EQUALITY category.
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
42
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
43
 ;; This package in essence provides a superset of this, called the
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
44
 ;; EQUIVALENCE category. These functions are generic and designed to
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
45
 ;; be implemented on user-defined objects with support for highly
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
46
 ;; complex relationships, partial equality, and more.
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
47
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
48
 ;; Here are some things to keep in mind for this potion of code:
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
49
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
50
 ;; - I'm not as familiar with Clojure (which has equiv) and JVM
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
51
 ;;   semantics as much as I'd like to be on this topic.
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
52
 
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
53
 ;; - I believe that Rust got it right for the most part. PartialEq and
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
54
 ;;   Eq are the two equality traits, with PartialOrd and Ord being the
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
55
 ;;   ordering traits which are a natural extension to equality. In
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
56
 ;;   turn the MAX and MIN functions build off of Ord (total
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
57
 ;;   order). The APIs that follow (order.lisp,limit.lisp) will be
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
58
 ;;   designed with these relationships in mind.
cb6effbda1dd more meta
Richard Westhaver <ellis@rwest.io>
parents: 230
diff changeset
59
 
230
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
60
 ;;; Code:
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
61
 (in-package :obj/equiv)
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
62
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
63
 (defgeneric equiv (a b))
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
64
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
65
 (defgeneric eqv (a b))
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
66
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
67
 (defgeneric nequiv (a b))
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
68
 
282991a71fe5 fix ansi and add equiv
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
69
 (defgeneric neqv (a b))
384
8fe057887c17 skel refactor1
Richard Westhaver <ellis@rwest.io>
parents: 236
diff changeset
70
 
8fe057887c17 skel refactor1
Richard Westhaver <ellis@rwest.io>
parents: 236
diff changeset
71
 (defgeneric equals (a b &rest args))