Nicolás Berger <nicober...@gmail.com> writes:

Hi Nicolás,

> Sounds interesting :). I hope I get some time to take a look into it
> soon.

I appreciate your feedback!

> In the meantime, have you tried playing with the log and trace
> "goals"?  I mean log, trace-s and trace-lvar.  They might be of help
> in trying to discover where it's going that makes it hang.

Oh, indeed, those seem to be very useful.  Unfortunately they produce
that much output that it'll freeze my CIDER/Emacs pretty soon because in
backwards direction, `inserto` cycles.

I've simplified the code a bit so that I don't need the `cconso`
relation.

--8<---------------cut here---------------start------------->8---
(declare inserto)
(defn isorto
  ([l sl]
   (isorto l () sl))
  ([l acc sl]
   (all
    (trace-lvars "isorto" l acc sl)
    (conde
     [(== l ()) (== acc sl)]
     [(fresh [f r nacc]
        (conso f r l)
        (inserto f acc nacc)
        (isorto r nacc sl))]))))

(defn inserto [x l nl]
  (all
   ;;(trace-s)
   (trace-lvars "inserto" x l nl)
   (conde
    [(== l ()) (== nl (list x))]
    [(fresh [f r nf nr]
       (conso f r l)
       (conso nf nr nl)
       (conda
        [(fd/<= x f) (conso x l nl)]
        [(fd/> x f)  (== f nf) (inserto x r nr)]))])))
--8<---------------cut here---------------end--------------->8---

and as you see, now I also trace the relation arguments.  In the working
forward direction, I get this trace:

--8<---------------cut here---------------start------------->8---
(run* [q]
  (isorto (list 3 2 1) q))
;; isorto
;;     l = clojure.lang.LazySeq@7fe1
;;   acc = clojure.lang.LazySeq@1
;;    sl = _0
;; inserto
;;     x = 3
;;     l = clojure.lang.LazySeq@1
;;    nl = _0
;; isorto
;;     l = clojure.lang.LazySeq@400
;;   acc = clojure.lang.LazySeq@22
;;    sl = _0
;; inserto
;;     x = 2
;;     l = clojure.lang.LazySeq@22
;;    nl = _0
;; isorto
;;     l = clojure.lang.LazySeq@20
;;   acc = clojure.lang.LazySeq@402
;;    sl = _0
;; inserto
;;     x = 1
;;     l = clojure.lang.LazySeq@402
;;    nl = _0
;; isorto
;;     l = clojure.lang.LazySeq@1
;;   acc = clojure.lang.LazySeq@7861
;;    sl = _0
;=> ((1 2 3))
--8<---------------cut here---------------end--------------->8---

Too bad that lazy sequences aren't printed in a readable way.  And why
are the sorted lists sl of isorto and nl of inserto always (the same?)
fresh variable _0?  I had expected that during the recursion, more
information should be added so that their value builds up like

  sl = (1 2 . some-lvar)

where some-lvar will eventually during the next recursion be fixed to
the list (3).

Anyway, this is what I get in the "predicate" sort of application, i.e.,
which answers the question if a given list will equal another list if
being sorted.

--8<---------------cut here---------------start------------->8---
(run* [q]
  (isorto (list 3 2 1) (list 1 2 3)))
;; isorto
;;     l = clojure.lang.LazySeq@7fe1
;;   acc = clojure.lang.LazySeq@1
;;    sl = clojure.lang.LazySeq@7861
;; inserto
;;     x = 3
;;     l = clojure.lang.LazySeq@1
;;    nl = _0
;; isorto
;;     l = clojure.lang.LazySeq@400
;;   acc = clojure.lang.LazySeq@22
;;    sl = clojure.lang.LazySeq@7861
;; inserto
;;     x = 2
;;     l = clojure.lang.LazySeq@22
;;    nl = _0
;; isorto
;;     l = clojure.lang.LazySeq@20
;;   acc = clojure.lang.LazySeq@402
;;    sl = clojure.lang.LazySeq@7861
;; inserto
;;     x = 1
;;     l = clojure.lang.LazySeq@402
;;    nl = _0
;; isorto
;;     l = clojure.lang.LazySeq@1
;;   acc = clojure.lang.LazySeq@7861
;;    sl = clojure.lang.LazySeq@7861
;=> (_0)
--8<---------------cut here---------------end--------------->8---

Now that looks good, at least the final trace.  The intermediate
accumulator acc indeed equals (or is in fact identical to) sl.

Ok, and now the backwards case:

--8<---------------cut here---------------start------------->8---
(run* [q]
  (isorto q (list 1)))
;; inserto
;;     x = _0
;;     l = clojure.lang.LazySeq@1
;;    nl = _0
;; inserto
;;     x = _0
;;     l = clojure.lang.LazySeq@9e3a6373
;;    nl = _0
;; inserto
;;     x = _0
;;     l = clojure.lang.LazySeq@c74c6d84
;;    nl = _0
;; (loops forever...)
--8<---------------cut here---------------end--------------->8---

Well, that loops forever.  But why the heck are x and nl always _0?  To
my best understanding, that means that x and nl are unified, no?  That
would obviously be wrong but I can't see where I am doing that...

Bye,
Tassilo

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to