   (println "Retracting entity " ?entity)
   (doseq [tuple ?entity] (retract! tuple)))
 
+;TODO. This doesn't work and we would like the first condition to be expressed with
+; square brackets optional.
+; Also need to handle cases of nested ops as in condition 2. Should do this on
+; same pass as adding support for matching in the rightmost part of the condition
+;(def-tuple-rule clear-completed-action-is-done-when-no-done-todos
+;  [?action <- [:ui/clear-completed]]
+;  [:not [:exists [:todo/done]]]
+;  =>
+;  (println "Clear-completed action finished. Retracting " ?action)
+;  (retract! ?action))
+
 (defrule clear-completed-action-is-done-when-no-done-todos
   [?action <- :ui/clear-completed]
   [:not [:exists [:todo/done]]]

;  [:todo/title [[e a v]] (= ?e e)]
;  =>
;  (insert! [?e :todo/visible :tag]))

(defrule todo-is-visible-when-filter-is-done-and-todo-done
  [:ui/visibility-filter [[e a v]] (= v :done)]
  [:todo/done [[e a v]] (= e ?e)]
  =>
  (insert! [?e :todo/visible :tag]))

(defrule todo-is-visible-when-filter-active-and-todo-not-done
  [:ui/visibility-filter [[e a v]] (= v :active)]

  (println "Marked done via toggle complete:" ?e)
  (insert-unconditional! [?e :todo/done :done]))

;TODO. check if :test is considered an op. Note that
; clara's syntax allows ?binding <- :attribute with no brackets
; so we need to support that as well
; Accum *shouldn't* be a problem.
; Debugging should be easier in CLJ at this point, should just need to compare expansions
;(def-tuple-rule remove-toggle-complete-when-all-todos-done
;  [?toggle <- :ui/toggle-complete]
;  [?total <- (acc/count) :from [:todo/title]]
;  [?total-done <- (acc/count) :from [:todo/done]]
;  [:test (not (not (= ?total ?total-done)))]
;  =>
;  (println "Total todos: " ?total)
;  (println "Total done: " ?total-done)
;  (println "Retracting toggle-all-complete action: " ?toggle)
;  (retract! ?toggle))

(defrule remove-toggle-complete-when-all-todos-done
  [?toggle <- :ui/toggle-complete]
  [?total <- (acc/count) :from [:todo/title]]
