Saturday, April 13, 2019

Clojure multisets

The only core mathematical data structure that is missing from Clojure is multisets. Clojure already provides implementations of all the other core data structures. One good thing about Clojure is its generic collections interfaces, so all collections are seqable, countable, etc. This solves the problem of providing all the core data structures necessary for Clojure. Of course, the same core data structures are the basis of any ideal Lisp dialect so for Common Lisp you could use FSet.
(deftype Multiset [multiplicities]
  clojure.lang.Seqable
  (seq [this]
    (if (empty? multiplicities)
      nil
      (apply
       concat
       (map
        (fn [key]
          (repeat (get multiplicities key) key))
        (keys multiplicities)))))
  
  clojure.lang.Counted
  (count [this]
    (apply + (vals multiplicities)))

  Object
  (equals [this x]
    (and
     (instance? Multiset x)
     (.equals (.multiplicities this) (.multiplicities x))))
  (hashCode [this]
    (hash-combine (hash multiplicities) Multiset))
  (toString [this]
    (str "*{" (clojure.string/join " " (seq this)) "}")))

(defmethod print-method Multiset [v ^java.io.Writer w]
  (.write w (.toString v)))
In my posts last month on degree reductions and set theory in a multiset theoretic context I explained the need for multisets and why they are fundamentally useful even for combinatorial set theory. It is easy then to get the multiset of some collection which is why every collection type is a structural multiset.
(defn multiset
  [coll]

  (Multiset. (frequencies coll)))
The core collection types are lists, sets, and multisets as they are general collections of items. Most other concepts can be constructed from them. An ideal Lisp dialect should focus on lists, sets, and multisets. A great deal can be done by restricting yourself to a few core data types. Maps like {:x 1, :y 2} are not fundamental because they are a special type of binary relation. What interests me is how to build an ontology of these core data types to begin with consisting of computable predicate functions.

No comments:

Post a Comment