 (def ^:dynamic truncate? false)
 (def ^:dynamic println? false)
 (def ^:dynamic use-spec-cache? false)
+(def ^:dynamic model)
+
 (declare gen)
 (declare get-lexemes)
 (declare grow)

                  
                  true ;; generate children that are leaves before children that are trees.
                  (lazy-cat (child-lexemes) (child-trees)))]
            (assoc-children tree children f model))
          model)
         [tree])
       (grow (rest trees) model)))))



(defn generate
  "Return one expression matching spec _spec_ given the model _model_."
  ([spec model]
   (log/debug (str "(generate) with model named: " (:name model)
                   "; "truncate? " truncate? "; "))
                   "use-spec-cache? " use-spec-cache?))
   (binding [model model]
     (first (gen spec)))))

(defn gen
  "generate a potentially infinite (depending on given _spec_ and _model_) 
   list of expressions that match the given _spec_."
  [spec]
  (grow (parent-with-head spec 0)))

(defn matching-rules [spec]
  (->> (:grammar model)
       (map #(unify % spec))
       (filter #(not (= :fail %)))))

(defn rules-for-spec [spec]
  (if (not use-spec-cache?)
    (matching-rules spec)
    (let [plain-spec (strip-refs spec)
          looked-up
          (cond
