                                       (let [plan @main-plan
                                             [plan node vars] (recall->nodes plan recall)
                                             [plan node vars computes-applied] (computes->nodes plan node vars computes)]
-                                        (swap! main-plan plan)
+                                        (reset! main-plan plan)
                                         [node vars computes-applied]))
         [nodes vars computes-applied] (reduce (fn [[nodes-a vars-a computes-applied-a] [nodes-b vars-b computes-applied-b]]
                                                (let [plan @main-plan
                                                      [plan nodes vars computes-applied-old] (join->nodes plan nodes-a vars-a computes-applied-a nodes-b vars-b computes-applied-b)
                                                      [plan nodes vars computes-applied-new] (computes->nodes plan nodes vars (difference computes computes-applied-old))]
-                                                 (swap! main-plan plan)
+                                                 (reset! main-plan plan)
                                                  [nodes vars (union computes-applied-old computes-applied-new)]))
                                               nodes&vars&computes-applied)]
     (assert (= computes-applied (set computes)) (str "Could not apply " (pr-str (difference (set computes) computes-applied))))
     (doseq [output outputs]
-      (swap! main-plan (output->nodes @main-plan nodes vars output)))))
+      (reset! main-plan (output->nodes @main-plan nodes vars output)))
+    @main-plan))
+
+
+  (deffact edge "[x] has an edge to [y]")
+  (deffact connected "[x] is connected to [y]")
+
+(rule->flow-plan (Rule. [(Recall. :known&pretended (->edge 'x 'y))
+                         (Output. :pretended (->connected 'x 'y))]))
+
+(rule->flow-plan (Rule. [(Recall. :known&pretended (->edge 'x 'y))
+                         (Recall. :known&pretended (->connected 'y 'z))
+                         (Output. :pretended (->connected 'x 'z))]))
+

                 [plan node] (add-flow plan (->FilterMap nodes filter-map-fun))]
             [plan [node] vars]))))

(defn computes->nodes [plan nodes vars computes]
    (loop [orig-nodes nodes
           plan plan

        [plan lookup-b] (add-flow plan (->Lookup [index-b] index-a key-ixes-b val-ixes-b))]
    [plan [lookup-a lookup-b] (distinct (concat vars-a vars-b)) (union computes-applied-a computes-applied-b)]))

(comment
  (let [plan empty-flow-plan
        [plan nodes-a vars-a] (recall->nodes plan (Recall. :known&pretended (->edge 'x 'y)))
        [plan nodes-b vars-b] (recall->nodes plan (Recall. :known (->connected 'y 'z)))
        [plan nodes-c vars-c computes-applied] (join->nodes plan nodes-a vars-a #{} nodes-b vars-b #{})]
    [plan nodes-c vars-c])
  )

;; RULES

(defrecord Rule [clauses])
