         (do
           (apush constant-values value)
           (apush constant-ixes ix))))
-    (fn [fact]
-      (when (= shape (.-shape fact))
-        (let [source (.-values fact)]
+    `(pattern->deconstructor* ~constant-values ~constant-ixes ~var-ixes ~dup-value-ixes ~dup-var-ixes)))
+
+(defn pattern->deconstructor* [constant-values constant-ixes var-ixes dup-value-ixes dup-var-ixes]
+  (fn [fact]
+    (let [source (.-values fact)]
+      (loop [i 0]
+        (if (< i (alength constant-values))
+          (when (= (aget constant-values i) (aget source (aget constant-ixes i)))
+            (recur (+ i 1)))
           (loop [i 0]
-            (if (< i (alength constant-values))
-              (when (= (aget constant-values i) (aget source (aget constant-ixes i)))
+            (if (< i (alength dup-value-ixes))
+              (when (= (aget source (aget dup-value-ixes i)) (aget source (aget dup-var-ixes i)))
                 (recur (+ i 1)))
-              (loop [i 0]
-                (if (< i (alength dup-value-ixes))
-                  (when (= (aget source (aget dup-value-ixes i)) (aget source (aget dup-var-ixes i)))
-                    (recur (+ i 1)))
-                  (let [sink (make-array (alength var-ixes))]
-                    (dotimes [i (alength var-ixes)]
-                      (aset sink i (aget source (aget var-ixes i))))
-                    (Fact. nil sink nil)))))))))))
+              (let [sink (make-array (alength var-ixes))]
+                (dotimes [i (alength var-ixes)]
+                  (aset sink i (aget source (aget var-ixes i))))
+                (Fact. nil sink nil)))))))))
 
 (comment
   (deffact eg "[a] has a [b] with a [c]")

        (do
          (apush source-ixes (ix-of source-vars value))
          (apush sink-ixes ix))))
    (fn [fact]
      (let [source (.-values fact)
            sink (aclone values)]
        (dotimes [i (alength source-ixes)]
          (aset sink (aget sink-ixes i) (aget source (aget source-ixes i))))
        (Fact. shape sink nil)))))

(defn pattern->deconstructor [pattern]
  (let [shape (.-shape pattern)

  ((pattern->deconstructor (->eg 'a 'a 'a)) (->eg "a" "a" "a"))
  )

;; CLAUSES

(defrecord Recall [memory pattern]) ;; memory is one of :known :pretended :remembered :forgotten :known&pretended
