Skip to content

Instantly share code, notes, and snippets.

@eraserhd
Created October 28, 2019 19:41
Show Gist options
  • Save eraserhd/449ab98c03b99f8f333d585794def6f5 to your computer and use it in GitHub Desktop.
Save eraserhd/449ab98c03b99f8f333d585794def6f5 to your computer and use it in GitHub Desktop.
(defn- sort-name
[t]
(s/join \space (flatten [t])))
(defn- sorted [head-symbol members]
(let [sorted-members (sort-by sort-name members)]
(if (= members sorted-members)
(r/fail members)
(cons head-symbol sorted-members))))
(def ^:private sort-members
(r/match
(Union . !ts ...) (sorted 'Union !ts)
(Intersection . !ts ...) (sorted 'Intersection !ts)))
(def ^:private normalize
(r/repeat
(r/some-bu
(r/choice
sort-members
(r/rewrite
(Intersection . !xs ... (Array ?a) . !ys ... (Array ?b) . !zs ...)
(Intersection (Array (Intersection ?a ?b)) . !xs ... . !ys ... . !zs ...)
(Intersection . ?t ..1)
?t
(Intersection . !xs ... Any . !ys ...)
(Intersection . !xs ... . !ys ...)
(Intersection . !xs ... (Intersection . !ys ...) . !zs ...)
(Intersection . !xs ... . !ys ... . !zs ...)
(Intersection . !xs ... String . !ys ... Uri . !zs ...)
(Intersection Uri . !xs ... . !ys ... . !zs ...)
(Intersection . !xs ... String . !ys ... Uuid . !zs ...)
(Intersection Uuid . !xs ... . !ys ... . !zs ...)
(Intersection . !xs ... Instant . !ys ... String . !zs ...)
(Intersection Instant . !xs ... . !ys ... . !zs ...)
(Union . !xs ... (Array ?a) . !ys ... (Array ?b) . !zs ...)
(Union (Array (Union ?a ?b)) . !xs ... . !ys ... . !zs ...)
(Union . ?t ..1)
?t
(Union . _ ... Any . _ ...)
Any
(Union . !xs ... Instant . !ys ... String . !zs ...)
(Union String . !xs ... . !ys ... . !zs ...)
(Union . !xs ... (Union . !ys ...) . !zs ...)
(Union . !xs ... . !ys ... . !zs ...)
;; Intersections can be resolved when pushed inside the unions
(Intersection . !xs ... (Union . !as ...) . !ys ...)
(Union . (m/app (fn [?a]
(m/subst (Intersection . !xs ... . !ys ... ?a)))
!as) ...)
(Union . !xs ... (Intersection . !as ... ?t . !bs ...) . !ys ... ?t . !zs ...)
(Union ?t (Intersection . !as ... . !bs ...) . !xs ... . !ys ... . !zs ...))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment