Using trace to display a procedure in racket
Asked Answered
I

2

6

I've been working through the last few exercises ch 1 of SICP, where several of the exercises uses higher-order functions. Currently I'm trying to debug a problem in my solution to 1.45, which is raising an arity mismatch. The function which is raising the error is the result of twice applying an averaging operation to a fixed-point function solver.

It would make my debugging efforts a lot easier if I could just dump some sort of representation of procedures, given that the procedure has been run through several other procedures that alter it before it raises an error. I've looked at the debugging documentation for DrRacket, added (require racket/trace) and (require errortrace) to my module and I think I'm familiar with the all the features of the debugging system -- but I still have no idea how to do this.

An answer for DrRacket would be ideal, but anything helps.

Island answered 15/6, 2014 at 5:46 Comment(3)
You should really add some code to illustrate the problem.Calumet
Possible duplicate of "How do you return the description of a procedure in Scheme?" ?Teth
What do you mean by "a representation of procedures"? This could mean an s-expression representing the source, the macro-expanded code, the compiled bytecode, or disassembled code from the JIT, or something else. Also, as an aside I think it's probably easier to write more unit tests, use contracts, or trace your functions rather than look at macro-expanded code or compiler output.Tung
I
7

Adding (require racket/trace) won't throw any procedure displays in the console. You want to use (trace function-name) this will print purple (default color) lines in the console when you use the given function in the trace call. Example

(define sum (λ (x y) (+ x y)))
(define multiply
  (λ (x y)
    (multiply-aux x y x)
    ))
(define multiply-aux (λ (x y res) 
                       (if (= y 0) 0 
                           (if (= y 1) res 
                               (multiply-aux x (- y 1) (sum res x))))))
(require racket/trace)
(trace sum)

In the console:

> (multiply 4 5)
>(sum 4 4)
<8
>(sum 8 4)
<12
>(sum 12 4)

Tested in DrRacket 6.0.1

Let me know if you need more help.

Insanity answered 25/8, 2014 at 2:36 Comment(1)
(trace +) produces an error in Racket 5.3.6 set!: cannot mutate module-required identifier in: +. On the other hand, (trace sum) does trace.Rosettarosette
A
2

One trick is to create an alias for a primitive function. So if you want to trace the addition operator somewhere, trace won't allow it unless you do this:

(require trace) (define *+ +)

Then use *+ anywhere in the code where you want to watch its output closely, without seeing the output of + used elsewhere.

Aldred answered 4/6, 2016 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.