Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something is wrong when using 'trace' on '+' and then calling 'reduce' #437

Open
masak opened this issue Jun 5, 2023 · 0 comments
Open

Comments

@masak
Copy link
Owner

masak commented Jun 5, 2023

I haven't been able to find the root cause here, but I can show the symptom pretty well. The following works as intended:

$ perl -Ilib bin/bel
Language::Bel 0.63 -- msys.
> (set old-+ +)
(lit clo nil ns (foldr (fn (x y) (apply buildnum ((of c+ cddr) x y))) 0 ns))
> (def + (n1 n2) (pr "(+ " n1 " " n2 ")" \lf) (old-+ n1 n2))
> (reduce + '(1 2 3 4 5))
(+ 4 5)
(+ 3 9)
(+ 2 12)
(+ 1 14)
15

Pretty cool, eh?

And then the following doesn't:

(def prlf args
  (atomic
    (apply pr args)
    (pr \lf)))

(set wrapped-fns-table (table))

(mac wrap (f w)
  (letu (vf vw)
    `(withs (,vf ,f
             ,vw ,w
             w2 (fn args (apply ,vw (cons ,vf args))))
       (set (wrapped-fns-table w2) ,vf
            ,f w2))))

(set *trace-depth* 0)

(mac trace (f)
  `(wrap ,f
         (fn (orig . args)
           (repeat *trace-depth* (pr "  "))
           (pr "Entering (" (nom ',f))
           (each a args
             (pr " " a))
           (prlf ")")
           (++ *trace-depth*)
           (let result (apply orig args)
             (-- *trace-depth*)
             (repeat *trace-depth* (pr "  "))
             (pr "Leaving " (nom ',f))
             (prlf " ==> " result)
             result))))

(trace +)

(reduce + '(1 2 3 4 5))

Gives the following output:

$ perl -Ilib bin/bel trace-plus.bel
Entering (+ 4 5)
Entering (+ 0 1)
Entering (+ 0 1)
Entering (+ 0 1)
Entering (+ 0 1)

I see three things that are wrong, offhand:

  • The second entry should say Entering (+ 3 9), just like the trace-less version above.
  • We have Entering lines but not Leaving lines, despite the fact that the + function is called as part of the reduce function does not contain any recursion.
  • We get stuck at the (+ 0 1) case forever.

Even though I haven't confirmed it, I suspect this is another re-occurrence of #273. Somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant