2015-11-17 22:41:06 +03:00
|
|
|
;; equality.mal
|
2019-05-15 06:05:26 +03:00
|
|
|
|
2015-11-17 22:41:06 +03:00
|
|
|
;; This file checks whether the `=` function correctly implements equality of
|
|
|
|
;; hash-maps and sequences (lists and vectors). If not, it redefines the `=`
|
|
|
|
;; function with a pure mal (recursive) implementation that only relies on the
|
|
|
|
;; native original `=` function for comparing scalars (integers, booleans,
|
|
|
|
;; symbols, strings).
|
|
|
|
|
|
|
|
;; Save the original (native) `=` as scalar-equal?
|
|
|
|
(def! scalar-equal? =)
|
|
|
|
|
2019-05-15 06:05:26 +03:00
|
|
|
;; A faster `and` macro which doesn't use `=` internally.
|
2019-05-15 16:49:35 +03:00
|
|
|
(defmacro! and2 ; `true` or `nil`
|
|
|
|
(fn* [& xs] ; interpreted as logical values
|
|
|
|
(if (empty? xs)
|
|
|
|
true
|
|
|
|
`(if ~(first xs) (and2 ~(rest xs))))))
|
|
|
|
(defmacro! ord2 ; `true` or `false`
|
|
|
|
(fn* [& xs] ; interpreted as logical values
|
|
|
|
(if (empty? xs)
|
|
|
|
false
|
|
|
|
`(if ~(first xs) true (ord2 ~(rest xs))))))
|
2019-05-15 06:05:26 +03:00
|
|
|
|
2015-11-17 22:41:06 +03:00
|
|
|
;; Implement `=` for two sequential arguments
|
|
|
|
(def! sequential-equal?
|
|
|
|
(fn* [a b]
|
2019-05-15 16:49:35 +03:00
|
|
|
(and2 (scalar-equal? (count a) (count b))
|
|
|
|
(or2 (empty? a)
|
|
|
|
(and2 (mal-equal? (first a) (first b))
|
|
|
|
(sequential-equal? (rest a) (rest b)))))))
|
2015-11-17 22:41:06 +03:00
|
|
|
|
|
|
|
;; Helper function
|
|
|
|
(def! hash-map-vals-equal?
|
|
|
|
(fn* [a b map-keys]
|
2019-05-15 16:49:35 +03:00
|
|
|
(or2 (scalar-equal? 0 (count map-keys))
|
|
|
|
(let* [key (first map-keys)]
|
|
|
|
(and2 (contains? a key)
|
|
|
|
(contains? b key)
|
|
|
|
(mal-equal? (get a key) (get b key))
|
|
|
|
(hash-map-vals-equal? a b (rest map-keys)))))))
|
2015-11-17 22:41:06 +03:00
|
|
|
|
|
|
|
;; Implement `=` for two hash-maps
|
|
|
|
(def! hash-map-equal?
|
|
|
|
(fn* [a b]
|
|
|
|
(let* [keys-a (keys a)]
|
2019-05-15 16:49:35 +03:00
|
|
|
(and2 (scalar-equal? (count keys-a) (count (keys b)))
|
|
|
|
(hash-map-vals-equal? a b keys-a)))))
|
2015-11-17 22:41:06 +03:00
|
|
|
|
|
|
|
;; This implements = in pure mal (using only scalar-equal? as native impl)
|
|
|
|
(def! mal-equal?
|
|
|
|
(fn* [a b]
|
|
|
|
(cond
|
|
|
|
(and2 (sequential? a) (sequential? b)) (sequential-equal? a b)
|
|
|
|
(and2 (map? a) (map? b)) (hash-map-equal? a b)
|
|
|
|
true (scalar-equal? a b))))
|
|
|
|
|
|
|
|
(def! hash-map-equality-correct?
|
|
|
|
(fn* []
|
|
|
|
(try*
|
|
|
|
(and2 (= {:a 1} {:a 1})
|
|
|
|
(not (= {:a 1} {:a 1 :b 2})))
|
|
|
|
(catch* _ false))))
|
|
|
|
|
|
|
|
(def! sequence-equality-correct?
|
|
|
|
(fn* []
|
|
|
|
(try*
|
|
|
|
(and2 (= [:a :b] (list :a :b))
|
|
|
|
(not (= [:a :b] [:a :b :c])))
|
|
|
|
(catch* _ false))))
|
|
|
|
|
|
|
|
;; If the native `=` implementation doesn't support sequences or hash-maps
|
|
|
|
;; correctly, replace it with the pure mal implementation
|
|
|
|
(if (not (and2 (hash-map-equality-correct?) (sequence-equality-correct?)))
|
|
|
|
(do
|
|
|
|
(def! = mal-equal?)
|
|
|
|
(println "equality.mal: Replaced = with pure mal implementation")))
|
2019-05-15 06:05:26 +03:00
|
|
|
|
|
|
|
nil
|