anchor.diff:  (defun fapix-testa-restricoes (f restricoes)
     "Testa uma lista de restricoes e devolve dois valores. O primeiro, um boleano que indica se todas
     as restricoes se verificam e o segundo indica o numero de testes realizados."
  -  (let ((testes 0))
  -    (dolist (r restricoes (values t testes))
  -      (incf testes)
  -      (when (null (funcall (restricao-fapix-funcao-validacao r) f)) (return (values nil testes))))))
  +  (dolist (r restricoes (values t nil))
  +    (when (null (funcall (restricao-fapix-funcao-validacao r) f))
  +      (return (values nil r)))))
   
   (defun fapix-consistente-p (f)
     "Testa todas as restricoes do fapix."
  
positive.code: (defun procura-retrocesso-fc-mrv-fapix (f)
    ""
  ;  (format t "pr-fc-mrv~%")
    (let ((testes-totais 0))
      (when (fapix-completo-p f)
        (return-from procura-retrocesso-fc-mrv-fapix (values f testes-totais)))
  
      (let* ((ipix (fapix-mrv f))
  	   (d (fapix-pix-dominio f ipix)))
  
        (when (= (length d) 2)
          (when (= (aref (fapix-hv f) ipix) 1)
  	  (setf d (reverse d))))
  
  ;     (format t "Vou experimentar a variavel:~a cujo dominio e:~a~%" ipix (fapix-pix-dominio f ipix))
        (dolist (cor d)
  	(multiple-value-bind (consistente testes) (fapix-atribuicao-consistente-p f ipix cor)
  	  (incf testes-totais testes)
  	  (when consistente
  ;	    (format t "Atribuindo o valor:~a a variavel:~a~%" cor ipix)
  	    (fapix-adiciona-atribuicao! f ipix cor)
  	    (setf (fapix-natribuidos f) (remove ipix (fapix-natribuidos f)))
  ;	    (format t "natribuidos:~a~%" (fapix-natribuidos f))
  	    (setf (aref (fapix-dominio-len f) (length d))
  		  (remove ipix (aref (fapix-dominio-len f) (length d))))
  ;	    (format t "dominio-len:~a~%" (fapix-dominio-len f))
  ;	    (format t "Depois da atribuicao~%")
  ;	    (desenha-fill-a-pix (fapix->fill-a-pix f))
  	    (multiple-value-bind (inferencias testes) (fapix-forward-checking f ipix)
  ;	      (format t "PR: inferencias:~a~%" inferencias)
  	      (incf testes-totais testes)
  	      (when inferencias ; teste suficiente?
  		(let ((backup (make-hash-table :test 'eql)))
  		  (maphash #'(lambda (iipix id)
  ;			       (format t "iipix:~a id:~a~%" iipix id)
  			       (setf (gethash iipix backup) (fapix-pix-dominio f iipix))
  			       (fapix-altera-dominio! f iipix id))
  			   inferencias)
  
  ;		  (format t "Depois das inferencias~%")
  ;		  (desenha-fill-a-pix (fapix->fill-a-pix f))
  
  		  (multiple-value-bind (resultado testes) (procura-retrocesso-fc-mrv-fapix f)
  		    (incf testes-totais testes)
  		    (when resultado
  		      (return-from procura-retrocesso-fc-mrv-fapix (values resultado testes-totais))))
  
  		  (maphash #'(lambda (bipix bd)
  			       (fapix-altera-dominio! f bipix bd))
  			   backup)
  
  ;		  (print "Removidas as inferencias:")
  ;		  (desenha-fill-a-pix (fapix->fill-a-pix f))
  		  ))
  
  	      (fapix-remove-atribuicao! f ipix)
  	      (push ipix (fapix-natribuidos f))
  	      (push ipix (aref (fapix-dominio-len f) (length d)))
  
  ;	      (format t "Removendo a atribuicao da variavel:~a:~%" ipix)
  ;	      (desenha-fill-a-pix (fapix->fill-a-pix f))
  	      )))))
  
      (return-from procura-retrocesso-fc-mrv-fapix (values nil testes-totais))))
  
  (defun procura-retrocesso-mac-mrv-fapix (f)
    ""
  ;  (format t "pr-mac-mrv~%")
    (let ((testes-totais 0))
      (when (fapix-completo-p f)
        (return-from procura-retrocesso-mac-mrv-fapix (values f testes-totais)))
  
      (let* ((ipix (fapix-mrv f))
  	   (d (fapix-pix-dominio f ipix))
  	   )
  ;      (format t "Vou experimentar a variavel:~a cujo dominio e:~a~%" ipix (fapix-pix-dominio f ipix))
        ; TODO: heuristica de valor (media dos pixs a volta? implica mais um array)
        (when (= (length d) 2)
  	(let* ((a-volta (aref (fapix-a-volta f) ipix))
  	       (a-volta-len (length a-volta))
  	       (soma 0)
  	       (numero 0))
  
  	  (dolist (ipix-av a-volta)
  	    (let ((pix (aref (fapix-pix f) ipix-av)))
  	      (when pix (incf soma pix) (incf numero))))
  
  	  (when (and (not (= numero 0)) (= (round (round soma numero) a-volta-len) 1))
  	    (setf d (reverse d)))))
  
        (dolist (cor d)
  	(multiple-value-bind (consistente testes) (fapix-atribuicao-consistente-p f ipix cor)
  	  (incf testes-totais testes)
  	  (when consistente
  ;	    (format t "Atribuindo o valor:~a a variavel:~a~%" cor ipix)
  	    (fapix-adiciona-atribuicao! f ipix cor)
  	    (setf (fapix-natribuidos f) (remove ipix (fapix-natribuidos f)))
  ;	    (format t "natribuidos:~a~%" (fapix-natribuidos f))
  	    (setf (aref (fapix-dominio-len f) (length d))
  		  (remove ipix (aref (fapix-dominio-len f) (length d))))
  ;	    (format t "dominio-len:~a~%" (fapix-dominio-len f))
  ;	    (format t "Depois da atribuicao~%")
  ;	    (desenha-fill-a-pix (fapix->fill-a-pix f))
  	    (multiple-value-bind (inferencias testes) (fapix-mac f ipix)
  ;	      (format t "PR: inferencias:~a~%" inferencias)
  	      (incf testes-totais testes)
  	      (when inferencias ; teste suficiente?
  		(let ((backup (make-hash-table :test 'eql)))
  		  (maphash #'(lambda (iipix id)
  ;			       (format t "iipix:~a id:~a~%" iipix id)
  			       (setf (gethash iipix backup) (fapix-pix-dominio f iipix))
  			       (fapix-altera-dominio! f iipix id))
  			   inferencias)
  
  ;		  (format t "Depois das inferencias~%")
  ;		  (desenha-fill-a-pix (fapix->fill-a-pix f))
  
  		  (multiple-value-bind (resultado testes) (procura-retrocesso-mac-mrv-fapix f)
  		    (incf testes-totais testes)
  		    (when resultado
  		      (return-from procura-retrocesso-mac-mrv-fapix (values resultado testes-totais))))
  
  		  (maphash #'(lambda (bipix bd)
  			       (fapix-altera-dominio! f bipix bd))
  			   backup)
  
  ;		  (print "Removidas as inferencias:")
  ;		  (desenha-fill-a-pix (fapix->fill-a-pix f))
  		  ))
  
  	      (fapix-remove-atribuicao! f ipix)
  	      (push ipix (fapix-natribuidos f))
  ;;	      (push ipix (aref (fapix-dominio-len f) (length d)))
  	      (nconc (aref (fapix-dominio-len f) (length d)) (list ipix)) ; TODO:
  
  ;	      (print "Removendo a atribuicao:")
  ;	      (desenha-fill-a-pix (fapix->fill-a-pix f))
  	      )))))
  
      (return-from procura-retrocesso-mac-mrv-fapix (values nil testes-totais))))
  
  (defun fapix-forward-checking (f ipix)
    ""
    (let ((testes-totais 0)
  	(inferencias (make-hash-table :test 'eql))
  	(lista-arcos (fapix-arcos-vizinhos-nao-atribuidos f ipix)))
  ;    (format t "FC arcos:~a~%" lista-arcos)
      (dolist (arco lista-arcos)
        (let ((v2 (first arco))
  	    (v1 (rest arco)))
  
  	(multiple-value-bind (revise testes) (fapix-revise f v2 v1 inferencias)
  	  (incf testes-totais testes)
  	  (when revise
  	    (multiple-value-bind (d exists) (gethash v2 inferencias)
  	      (when exists
  		(when (= (length d) 0)
  		  (return-from fapix-forward-checking (values nil testes-totais)))))))))
  
      (return-from fapix-forward-checking (values inferencias testes-totais))))
  
  (defun fapix-mac (f ipix)
    ""
    (let ((testes-totais 0)
  	(inferencias (make-hash-table :test 'eql))
  	(lista-arcos (fapix-arcos-vizinhos-nao-atribuidos f ipix)))
  
      (dolist (arco lista-arcos)
        (let ((v2 (first arco))
  	    (v1 (rest arco)))
  
  	(multiple-value-bind (revise testes) (fapix-revise f v2 v1 inferencias)
  	  (incf testes-totais testes)
  	  (when revise
  	    (multiple-value-bind (d exists) (gethash v2 inferencias)
  	      (when exists
  		(when (= (length d) 0)
  		  (return-from fapix-mac (values nil testes-totais)))))
  
  	    (nconc lista-arcos (remove (cons v1 v2)
  				       (fapix-arcos-vizinhos-nao-atribuidos f v2)
  				       :test 'equal))))))
  
      (return-from fapix-mac (values inferencias testes-totais))))
  
  (defun fapix-arcos-vizinhos-nao-atribuidos (f ipix)
    ""
    (loop for relacionado in (aref (fapix-relacionados f) ipix) unless (fapix-pix-cor f relacionado)
         collect (cons relacionado ipix)))
  
  (defun fapix-revise (f x y inferencias)
    ""
    (let* ((testes-totais 0)
  	 (revised nil)
  	 (dominio-x (gethash x inferencias (fapix-pix-dominio f x)))
  	 (novo-dominio-x dominio-x)
  	 (dominio-y (if (fapix-pix-cor f y)
  			(list (fapix-pix-cor f y))
  			(gethash y inferencias (fapix-pix-dominio f y)))))
  
      (dolist (valor-x dominio-x)
        (when (dolist (valor-y dominio-y t)
  	      (multiple-value-bind (consistente testes)
  		  (fapix-atribuicoes-consistentes-arco-p f x valor-x y valor-y)
  		(incf testes-totais testes)
  		(when consistente (return nil))))
  
  	(setf revised t)
  	(setf novo-dominio-x (remove valor-x novo-dominio-x))))
  
      (when revised
        (setf (gethash x inferencias) novo-dominio-x)) ; TODO: sobrepor?
  
      (values revised testes-totais)))
  
  
negative.code:     (dotimes (i max-ipix)
        (when (not (aref pix-dominio-len-p i))
  	(push i (aref dominio-len 2)))
        (delete-duplicates (aref pix-relacionados i)))
  
      (make-fapix :linhas linhas
  		:colunas colunas
  		:max-ipix max-ipix
  