 
 
 
-(comment ; TODO: refine-input must go somewhere
-  (refine-input      [s maybe-refined-input-set])  
-  (refine-input-   [s refined-input-set] "must be a strict subset of input-set."))
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  Subproblem Impl  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
-(defn make-simple-subproblem [name inp-set out-set summary-fn inner-children copy-children child-transform]
+(defn make-simple-subproblem [name inp-set out-set summary-fn refine-input-fn]
   (let [ts-atom (atom nil)
         ret (traits/reify-traits [cache-trait summaries/simple-node]
              summaries/Summarizable (summarize [s] (summary-fn s))
              Subproblem             (subproblem-name [s] name)
                                     (input-set       [s] inp-set)
                                     (output-set       [s] out-set)
-                                    (tree-summarizer [s] @ts-atom))
+                                    (tree-summarizer [s] @ts-atom)
+                                    (refine-input    [s new-inp-set]
+                                     (if (= inp-set new-inp-set) s (refine-input-fn s new-inp-set))))        
         ts  (make-tree-summarizer ret)]
     (summaries/add-parent! ret ts false)
     (reset! ts-atom ts)
-
-    (doseq [child inner-children]
-      (summaries/connect! ret child false))
-
-    (doseq [child copy-children]
-      (add-watcher! child (fn [_ out-sp] (connect-child-sp! ts (child-transform out-sp)))))
-
     ret))
 
-
-
-(comment
-     (refine-input    [s maybe-refined-input-set]
-      (if (= input-set maybe-refined-input-set)
-        s
-        (let [refined (refine-input- s maybe-refined-input-set)]
-          (assert refined)
-          (summaries/connect! (tree-summarizer s) (tree-summarizer refined) true)
-          refined))))
+(defn setup-ordinary-subproblem! [sp inner-children copy-children child-transform]
+  (doseq [child inner-children]
+    (summaries/connect! sp child false))
+  (doseq [child copy-children]
+    (add-watcher! child (fn [_ out-sp] (connect-child-sp! (tree-summarizer sp) (child-transform out-sp))))))
+
+(defn setup-subsumed-subproblem! [sp subsuming-sp]
+  (summaries/connect! (tree-summarizer sp) (tree-summarizer subsuming-sp) true)
+  (add-watcher! subsuming-sp
+     (fn [_ sub-out]
+       (let [sub-stub (refine-input sub-out (input-set sp))]
+         (summaries/connect! sp sub-stub)
+         (add-watcher! sub-stub
+           (fn [_ out-sp] (connect-child-sp! (tree-summarizer sp) out-sp)))))))
+
+
 
 
 

     (add-output! ts child-sp)
     (let [my-sum (summaries/summary ts) child-sum (summaries/summary (tree-summarizer child-sp))]
;       (util/assert-is (>= (summary/max-reward my-sum) (summary/max-reward child-sum)))  Cannot assert this -- see above.
       (when-not (summary/>= my-sum child-sum) (summaries/summary-changed! ts))))
    
   summaries/Summarizable
   (summarize [s]

   #(let [n (second (subproblem-name %))] (when-not (= (first n) :noop) n))))

(defn- get-root-subproblem [inp-set fs]
  (let [root-stub (make-atomic-stub nil false inp-set fs)
        ret       (atom nil)]
    (add-watcher! root-stub (fn [_ root] (reset! ret root)))
    (evaluate! root-stub)
