 ;; Difficulty: nodes with multiple ancestors.
 
 (defprotocol HasCutoff (cutoff [x]))
-(defprotocol HasQueue (queue [x]))
+(defprotocol HasQueue  (queue [x]))
 
 (deftype PartialResult [result-map cutoff]
   HasCutoff (cutoff [] cutoff))

            [ss r] (when-let [x (and prim? (env/applicable? a s) (env/successor a s))] x)] ;pun        
        (SANode context a 
          (atom (if ss {(vary-meta (env/extract-effects ss context) assoc :opt [a]) r} {})) 
          (make-queue
           (for [ref (when-not prim? (hierarchy/immediate-refinements a s))]
             [(SeqNode (make-queue [[(SeqNodeEntry s (when (seq ref) (get-sa-node cache s (first ref))) 0.0 ref) 0.0]])) 0.0])))))))


;; Result-fn returns 
(defn expand-helper [node cache next-best init-results result-fn]
  (if (< (cutoff node) next-best)
    (do ;#_ (println "done " (type node)  (cutoff node) init-results) 
        (PartialResult init-results (cutoff node)))
    (let [[best-child neg-reward] (queues/pq-remove-min-with-cost! (queue node))
          next-best (max next-best (cutoff node))
          [new-states adj-reward new-pairs] (result-fn best-child (- neg-reward) next-best)]
      (when (> adj-reward Double/NEGATIVE_INFINITY) (queues/pq-replace! (queue node) best-child (- adj-reward)))
      (doseq [[n r] new-pairs] (queues/pq-add! (queue node) n (- r)))
      (recur node cache next-best (merge init-results new-states) result-fn))))


(defn stitch-results [effect-map state reward-to-state]

  HasCutoff (cutoff [] (- (second (queues/pq-peek-min queue))))
  HasQueue  (queue [] queue))

;; Represents an action sequence from a state, with sanode representing the first action in remaining-actions.
 ; (or nil, if remaining-actions is empty.)
(deftype SANodeEntry [state sanode reward-to-state remaining-actions]
  Object
   (equals [y] (and (= state (:state y)) (= remaining-actions (:remaining-actions y))))
   (hashCode [] (unchecked-add (int (hash state)) 
                               (unchecked-multiply (int 13) (int (hash remaining-actions))))))


(defn make-queue [initial-elements]
