+ (if (not (zerop type)) type
+ (let ((scope (set-order (topic-name-scope a) (topic-name-scope b) #'topic-order)))
+ (if (not (zerop scope)) scope (topic-order (topic-name-parent a) (topic-name-parent b)))))))))
+(defun literal-value-order (value-a datatype-a value-b datatype-b)
+ ;; TMDM 4.3/5.5/5.6: anyURI values are locators; other supported values are strings.
+ ;; CXTM 4.2 orders strings before locators, independent of their spelling.
+ (let ((iri-a (string= datatype-a (iri-datatype)))
+ (iri-b (string= datatype-b (iri-datatype))))
+ (cond ((and iri-a iri-b) (locator-order value-a value-b))
+ (iri-a 1) (iri-b -1)
+ (t (string-order (nfc value-a) (nfc value-b))))))
+(defun occurrence-value-order (a b)
+ (literal-value-order (occurrence-value a) (occurrence-datatype a)
+ (occurrence-value b) (occurrence-datatype b)))
+(defun variant-order (a b)
+ (loop for comparison in
+ (list (literal-value-order (variant-value a) (variant-datatype a) (variant-value b) (variant-datatype b))
+ (locator-order (variant-datatype a) (variant-datatype b))
+ (set-order (variant-scope a) (variant-scope b) #'topic-order)
+ (name-order (variant-parent a) (variant-parent b)))
+ when (not (zerop comparison)) return comparison finally (return 0)))
+
+(defun canonical-literal-value (value datatype)
+ "CXTM 3.16: shared Occurrence/Variant representation; never rewrite model data."
+ (cond ((string= datatype (iri-datatype)) (normalized-locator value))
+ ((string= datatype (integer-datatype)) (canonical-integer-lexical-form value))
+ ((string= datatype (decimal-datatype)) (canonical-decimal-lexical-form value))
+ ((string= datatype (date-datatype)) (canonical-date-lexical-form value))
+ ((string= datatype (datetime-datatype)) (canonical-datetime-lexical-form value))
+ (t value)))
+(defun occurrence-order (a b)
+ (loop for comparison in
+ (list (occurrence-value-order a b)
+ (locator-order (occurrence-datatype a) (occurrence-datatype b))
+ (topic-order (occurrence-type a) (occurrence-type b))
+ (set-order (occurrence-scope a) (occurrence-scope b) #'topic-order)
+ (topic-order (occurrence-parent a) (occurrence-parent b)))
+ when (not (zerop comparison)) return comparison finally (return 0)))
+