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