 ;; A version of eval subproblems where we explicitly represent the relationships
 ;; between subproblems, and allow "listeners" that wait for a subproblem to be
 ;; evaluated.
-;; Subproblems where the expand operation evaluates the output set, and generates
-;; the children.  Putting the focus on evaluation gives us finer granularity,
-;; allowing full utilizatino of subsumption, plus is more natural in a decomposed
-;; setting where we also have to "evaluate" existing actions in new contexts.
-
-;; TODO: assuming consistency and evaluation s.t. no bounding is necessary. Otherwise, downwards would be better anyway?
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Subproblem Protocol ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-;; A subproblem should also implement Summarizable and Refinable implementations.
-;; The non-viable (pre-refinement) subproblem is always represented as nil.
-
-;; Here, get-child and refine-input always return subproblems,
-;; but output-set can return "nil"
-;; get-child and refine-input are allow to return nil
-
-
-(defprotocol OutputWatched
-  (add-watcher!   [s w] "Add a watcher to be notified of all outputs to this.
-                         A watcher is just a fn of [watched-sp/stub new-subproblem]."))
-
-(defprotocol Evaluable
-  (evaluate!      [s]))
-
-(defn can-evaluate? [s]  (satisfies? Evaluable s))
-
-(defprotocol Subproblem
-  (subproblem-name   [s])
-  (input-set         [s])
-  (output-set        [s])
-  (tree-summarizer   [s] "Summarizer that includes children."))
-
-(defn sp-str [sp] (format  "#<Subproblem$%h %s>" (System/identityHashCode sp) (subproblem-name sp)) )
-(defmethod print-method angelic.search.implicit.fah_astar_eval2.Subproblem [ss o] (print-method (sp-str ss) o))
-
-
-(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."))
-
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Summarizers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Assuming consistency and evaluation s.t. no bounding is necessary.
+;; Otherwise, downwards would be better anyway?
+
+;; Breaks down into "subproblems" with well-defined action seqs, inputs, outputs,
+;; and "stubs" where output is not known yet.
+;; Summary of either represents child subproblems not yet produced.
+;; These are produced into "tree summary", which has semantic view.
+
+;; One issue with this formulation is that pair-subproblems can
+;; decrease and increase reward arbitrarily (with shortcutting)
+;; as they get cut off by left sp, then recreated by children.
+
 
 #_ (def  ^{:private true} cache-trait summaries/uncached-summarizer-node)
  (def ^{:private true} cache-trait summaries/eager-cached-summarizer-node)

                 (cons (summary/+ (summaries/summary left-sp) (summaries/summary right-stub) s)
                       (map summaries/summary (summaries/node-ordinary-children s)))
                  s Double/POSITIVE_INFINITY)))]
    (add-watcher! right-stub (fn [_ o] (add-output! ret (make-right-subproblem ret left-sp o))))
    (summaries/add-parent! left-sp ret false)
    (summaries/add-parent! right-stub ret false)

    (add-watcher! (tree-summarizer left-sp) 
      (fn [_ left-child]
        ;; Note: this right-sharing is crucial for efficiency! 
        (let [right-right (if (= (output-set left-child) (output-set left-sp)) right-stub (right-stub-fn (output-set left-child)))
              new-right-stub (make-right-stub left-child right-right right-stub-fn)]
          (summaries/connect! ret new-right-stub false)

    (swap! forwards conj child-sw)
    (doseq [w @watchers] (add-watcher! child-sw w))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;     Subproblems     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;     Protocols     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defprotocol Evaluable
  (evaluate!      [s]))

(defn can-evaluate? [s]  (satisfies? Evaluable s))

(defprotocol Subproblem
  (subproblem-name   [s])
  (input-set         [s])
  (output-set        [s])
  (tree-summarizer   [s] "Summarizer that includes children."))

(defn sp-str [sp] (format  "#<Subproblem$%h %s>" (System/identityHashCode sp) (subproblem-name sp)) )
(defmethod print-method angelic.search.implicit.fah_astar_eval2.Subproblem [ss o] (print-method (sp-str ss) o))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  Tree Summarizer  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Expects to be a parent of inside, but not have inside as a child.
;; All children are tree summarizers.
