This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def players-roster-rules | |
"Rules to test if a collection of players are a subset of the players | |
in a roster. | |
Usage Constraints: | |
- ?players must always be bound | |
- ?roster may be bound or unbound | |
Valid Uses: | |
- test if a specific ?roster contains all ?players | |
- find all instances of ?roster that contain all ?players | |
" | |
'[ | |
;; Assumptions: | |
;; 1. a roster has a set of players | |
;; 2. the set of players may be empty or non-empty | |
;; Case 1: Does a roster contain all players in the empty set? | |
;; | |
[(players-roster [?players] ?roster) | |
;; The empty-set is a subset of all other sets, therefore | |
;; (players-roster #{} ?roster) is true for any ?roster. | |
;; Note: this acts as the base-case for the recursive rule in Case 2. | |
[(empty? ?players)]] | |
;; Case 2: Does a roster contain all players in a non-empty set? | |
;; | |
[(players-roster [?players] ?roster) | |
;; given a non-empty set of ?players | |
[(not-empty ?players)] | |
;; consider one player and call it ?p1 | |
[(first ?players) ?p1] | |
;; consider the other ?players without ?p1: ?players - ?p1 | |
;; and call this set the ?remaining-players | |
[(rest ?players) ?remaining-players] | |
;; ?players is a subset of ?roster, iff: | |
;; 1. the ?roster has player ?p1, | |
[?roster :roster/players ?p1] | |
;; 2. and all the ?remaining-players are also in the ?roster | |
(players-roster ?remaining-players ?roster) | |
;; Note: the Case 1 is necessary when the preceeding clause matches the state: | |
;; | |
;; (players-roster [a-player] ?roster) | |
;; | |
;; which can be logically expanded into a sub-query conjunction containing | |
;; the clauses: | |
;; | |
;; [?roster :roster/players a-player] | |
;; (players-roster [] ?roster) ;;<= matches Case 1 rule | |
] | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is from a conversation with calvin.sauer in Clojurians/#datomic