 (defn lvar? [x] (instance? LVar x))
 
 
-;; ## Substitutions and Walking
+;; ## <a name="substitutions"></a><a name="walk"></a>Substitutions and Walking
 ;;
 ;; A substitution map (*s*) gives mappings from logic variables to
 ;; their values.

                   (fresh [~@(rest var-vec)]
                     ~@clauses)))))


;; ## Reificiation

;; In miniKanren, reification refers to extracting the desired values
;; from the stream of states you get as a result of executing a goal.
(defn reify-name [n]
  (symbol (str "_." n)))

(defn reify-s [v s]
  (reify-s* (walk v s) s))


(extend-protocol IReifySubstitution
  LVar
  (reify-s* [v s] (let [n (reify-name (count s))]
                    (assoc s v n)))

  Object
  (reify-s* [v s] s))


(defn walk* [v s]
  (let [v' (walk v s)]
    (deep-walk v' s)))

(extend-protocol IDeepWalk
  LVar
  (deep-walk [v s] v)

  Object
  (deep-walk [v s] v))


(defn reify-state-first-var [st]
  (let [v (walk* (lvar 0) (.s st))]
    (walk* v (reify-s v {}))))

(defn reify-state-lvar [st lv]
  (let [v (walk* lv (.s st))]
    (walk* v (reify-s v {}))))


;;; run interface

(defn call-empty-state [g]
  (g empty-state))


       (cons (.head $') (stream-to-seq (.next $')))))))


;; ## <a name="goals"></a>Goals

;; A *state* is a record containing a substitution map *s* and the id
;; of the next unbound logic variable *c*.
