3
votes

What are the equivalents in Guile Scheme of the (trace procedure) and (trace-let (bindings) body) tracing facilities from Chez Scheme.

I've reviewed the documentation at https://www.gnu.org/software/guile/manual/html_node/Tracing-Traps.html, but I cannot figure out how to use the Guile tracing procedures from the source code file, not from the Guile REPL which may be done with ,trace (procedure application) given previously imported module (use-modules (system vm trace)).

I'm interested in tracing from the source code the application of recursive procedures similar to (trace fact1) with the below output to the console

trace: (fact1 4)
trace: |  (fact1 3)
trace: |  |  (fact1 2)
trace: |  |  |  (fact1 1)
trace: |  |  |  |  (fact1 0)
trace: |  |  |  |  1
trace: |  |  |  1
trace: |  |  2
trace: |  6
trace: 24

Could the named let (let name (bindings) body) syntactic extension be traced in Guile? The need for this arises when investigating tail recursive implementations of procedures.

Thank you very much!

1

1 Answers

0
votes

See add-trap-at-procedure-call! on the page https://www.gnu.org/software/guile/docs/docs-2.0/guile-ref/High_002dLevel-Traps.html.

An example:

(define (factorial n)
  (fact-iter 1 1 n))
(define (fact-iter product counter max-count)
  (if (> counter max-count)
      product
      (fact-iter (* counter product)
 (+ 1 counter)
 max-count)))
(add-trace-at-procedure-call! fact-iter)
(add-trace-at-procedure-call! factorial)
(factorial 6)
$41 = 7
$42 = 8
Trap 8: (factorial 6)
Trap 7: (fact-iter 1 1 6)
Trap 7: |  (fact-iter 1 2 6)
Trap 7: |  |  (fact-iter 2 3 6)
Trap 7: |  |  |  (fact-iter 6 4 6)
Trap 7: |  |  |  |  (fact-iter 24 5 6)
Trap 7: |  |  |  |  |  (fact-iter 120 6 6)
Trap 7: |  |  |  |  |  |  (fact-iter 720 7 6)
Trap 7: |  |  |  |  |  |  720
Trap 7: |  |  |  |  |  720
Trap 7: |  |  |  |  720
Trap 7: |  |  |  720
Trap 7: |  |  720
Trap 7: |  720
Trap 7: 720
Trap 8: 720
$43 = 720

factorial & fact-iter taken from SICP 1.2.1.

I don't know how to make it show tail recursion or if this means there is no tail recursive call optimization. But it traces.