[racket] trace: set!: cannot mutate module-required identifier - alright .. how do you do this?

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Mon Jan 19 10:03:32 EST 2015

To be fair, trace is more than that.

Vincent's solution does work, but you have to modify the file - you can't do this from the REPL.
You can modify the file containing the identifier you want to trace, if changing call sites is not palatable to you. Add `(require racket/trace)` at the top, and after the function definition `(trace function)`. Alternatively, if you only want to trace at given points in time, define and provide a function `(define (add-trace) (trace function))` so you can call `add-trace` when you want to turn on tracing.

A missing feature for racket/trace is to provide a setter function so that modules that export a mutating function can be traced without depending on racket/trace. If a module contains a `set!` for an identifier, it disables any inlining for that identifier.
e.g.
(trace #:setter set-function!)

-Ian

----- Original Message -----
From: "Thomas Lynch" <thomas.lynch at reasoningtechnology.com>
To: "J. Ian Johnson" <ianj at ccs.neu.edu>
Cc: "users" <users at racket-lang.org>
Sent: Monday, January 19, 2015 9:49:51 AM
Subject: Re: [racket] trace: set!: cannot mutate module-required identifier - alright .. how do you do this?

So to trace calls to the interface functions of a library I am to write a
gasket for the library.  I may as well put a display of the args in the
gasket functions as at this point it is the same amount of code as hooking
the trace.   Of course then I have implemented trace.

So to use trace in racket all you have to do is implement trace ..
really??

On Mon, Jan 19, 2015 at 10:06 PM, J. Ian Johnson <ianj at ccs.neu.edu> wrote:

> Yes, racket 101 (305?) is that identifiers can only be set!'d in their
> defining module. This is to allow necessary optimizations such as inlining.
>
> If you want to trace `f` from a different module. you can `(define (g .
> args) (apply f args)) (trace g)`, use `g` wherever you use `f`. You won't
> get any calls the library makes internally, but if you're just tracing for
> your own code, you'll be golden.
>
> -Ian
>
> ----- Original Message -----
> From: "Thomas Lynch" <thomas.lynch at reasoningtechnology.com>
> To: "users" <users at racket-lang.org>
> Sent: Monday, January 19, 2015 8:59:55 AM
> Subject: [racket] trace: set!: cannot mutate module-required identifier -
> alright .. how do you do this?
>
> Saw this in another thread here, but the proposed solution didnt work,
> well at least the way I understood it. So ..
>
> In file db-lib.rkt I have a definition:
>
>
>
>
> (provide db-exec)
> (define (db-exec a) ...)
>
> Then in racket
>
>
>
>
> > (enter! "db-user.rkt")
>
> Where db-citation.rkt has
>
>
>
>
> (require "db-lib.rkt")
>
> Now at the read-eval-print prompt I try
>
>
>
>
> (enter! "db-citation.rkt")
>
> And when I do (trace db-exec) I get the cannot mutate error. (Surely this
> must be a racket 101 type problem.) So I try to include the module at the
> current scope..
>
>
>
>
> racket at db-citation.rkt> (require "db-lib.rkt")
> racket at db-citation.rkt> (require racket/trace)
> racket at db-citation.rkt> (trace db-exec)
>
> racket at db-citation.rkt> stdin::1130: set!: cannot mutate module-required
> identifier in: db-exec
> ok, so that doesn't work, so I start over again. This time after reading
> the thread on this list
>
>
>
>
> From: Vincent St-Amour (stamourv at ccs.neu.edu )
> Date: Mon Oct 10 13:49:46 EDT 2011
>
> which concludes by saying 'put the trace in your program that should
> work', so I add this to "db-citation.rkt"
>
>
>
>
> (require racket/trace)
> (trace db-exec)
>
> And then at the prompt I get the following:
>
>
>
>
> Welcome to Racket v5.2.1.
> racket@> (enter! "db-citation.rkt")
> db-citation.rkt:26:11: set!: cannot mutate module-required identifier in:
> db-exec
>
> Alright, so what is the magic incantation here that allows one to
> debug/trace calls in included modules?
>
> Thanks!
>
> Thomas
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

Posted on the users mailing list.