                                 (unifyc
                                  spec
                                  {:synsem {:sem (strip-refs (get-in expression [:synsem :sem]))}}))))
-              (log/info (str "generate: no expression could be generated for spec:" (strip-refs spec))))
+              (log/warn (str "generate: no expression could be generated for spec:" (strip-refs spec))))
             expression))))
 
 (declare add-complements-to-bolts)
+(declare add-all-comps)
+(declare candidate-parents)
+(declare lazy-shuffle)
 
 (defn generate-all [spec grammar lexicon index morph total-depth
                     & {:keys [max-total-depth truncate-children]
                        :or {max-total-depth max-total-depth
                             truncate-children true}}]
-  (log/trace (str "generate-all: generating from spec with cat " (get-in spec [:synsem :cat])))
-  (log/debug (str "generate-all: generating from spec with spec " (strip-refs spec) "; max total depth: " max-total-depth))
+  (log/trace (str "generate-all: generating from spec with cat "
+                  (get-in spec [:synsem :cat])))
+  (log/debug (str "generate-all: generating from spec with spec "
+                  (strip-refs spec) "; max total depth: " max-total-depth))
   
   (let [total-depth (if total-depth total-depth 0)]
     (map (fn [expr]

             (truncate expr [[:head][:comp]])
             ;; else, don't truncate for efficiency (i.e. don't remove :head and :comp)
             expr))
         (-> (lightning-bolts grammar lexicon spec 0 index morph total-depth :max-total-depth max-total-depth)
             ;; TODO: allow more than a fixed maximum depth of generation (here, 4 levels from top of tree).
             (add-complements-to-bolts [:head :head :head :comp] grammar lexicon index morph total-depth
                                       :max-total-depth max-total-depth)
             (add-complements-to-bolts [:head :head :comp] grammar lexicon index morph total-depth
                                       :max-total-depth max-total-depth)
             (add-complements-to-bolts [:head :comp] grammar lexicon index morph total-depth
                                       :max-total-depth max-total-depth)
             (add-complements-to-bolts [:comp] grammar lexicon index morph total-depth
                                       :max-total-depth max-total-depth)))))

(defn add-complements-to-bolts [bolts path grammar lexicon index morph total-depth
                                & {:keys [max-total-depth]
                                   :or {max-total-depth max-total-depth}}]
  (lazy-mapcat
   (fn [bolt]
     (if (not (= :none (get-in bolt path :none)))
      (do
        (log/debug (str "add-complements-to-bolts@" path))
        (let [add-complement add-complement]
          (add-complement bolt path :top grammar lexicon index morph 0 (+ total-depth (count path))
                          :max-total-depth max-total-depth)))
      [bolt]))
   bolts))

(defn candidate-parents [rules spec]
  "find subset of _rules_ for which each member unifies successfully with _spec_"
  (log/trace (str "candidate-parents: spec: " (strip-refs spec)))
  (filter #(not (fail? %))
          (mapfn (fn [rule]
                   (if (and (not (fail? (unifyc (get-in rule [:synsem :cat] :top)
                                                (get-in spec [:synsem :cat] :top))))
                            (not (fail? (unifyc (get-in rule [:synsem :infl] :top)
                                                (get-in spec [:synsem :infl] :top))))
                            (not (fail? (unifyc (get-in rule [:synsem :sem :tense] :top)
                                                (get-in spec [:synsem :sem :tense] :top))))
                            (not (fail? (unifyc (get-in rule [:synsem :modified] :top)
                                                (get-in spec [:synsem :modified] :top)))))
                     (unifyc spec rule)
                     :fail))
                 rules)))

(declare morph-with-recovery)

(defn lazy-shuffle [seq]
  (lazy-seq (shuffle seq)))

(defn lightning-bolts [grammar lexicon spec depth index morph total-depth
                       & {:keys [max-total-depth]

                 result)]
    result))


(defn path-to-map [path val]
  (let [feat (first path)]
    (if feat
      {feat (path-to-map (rest path) val)}
      val)))

(defn lexemes-before-phrases [depth]
  "returns true or false: true means generate by adding lexemes first; otherwise, by adding phrases first. Takes depth as an argument, which makes returning true (i.e. lexemes first) increasingly likely as depth increases."
