;; lib/relations/schema.sx — relationship fact vocabulary over lib/datalog/.
;;
;; relations is content-agnostic: a node is an opaque id (a symbol or string);
;; domains own what ids mean. A relationship is a single Datalog fact
;;
;;     rel(Src, Dst, Kind)
;;
;; meaning "Src is related to Dst under Kind" (read directionally: Src is the
;; parent/owner/origin, Dst the child/member/reply). Kind is an open vocabulary;
;; the names below are the platform's well-known kinds but relate accepts any
;; kind symbol — Datalog is untyped and domains may coin their own.

(define relations-kinds (quote (parent member reply variant origin link)))

(define relations-rel (fn (src dst kind) (list (quote rel) src dst kind)))

(define relations-rel-src (fn (f) (nth f 1)))

(define relations-rel-dst (fn (f) (nth f 2)))

(define relations-rel-kind (fn (f) (nth f 3)))

(define
  relations-member?
  (fn
    (x xs)
    (cond
      ((= (len xs) 0) false)
      ((= (first xs) x) true)
      (else (relations-member? x (rest xs))))))

(define
  relations-known-kind?
  (fn (k) (relations-member? k relations-kinds)))

(define
  relations-fact-valid?
  (fn
    (f)
    (and (list? f) (= (len f) 4) (= (first f) (quote rel)))))
