cscherrer / Soss.jl

Probabilistic programming via source rewriting

Home Page:https://cscherrer.github.io/Soss.jl/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

User-configurable `gensym`

cscherrer opened this issue · comments

What if I have another variable named _y_dist?

Great point. At one point, Soss used to use gensym for new symbols. But for looking at the source code, this can be pretty confusing. So currently I'm using a leading underscore for "internal" variables. For example.

julia> m = @model begin
           x ~ Normal()
       end;

julia> Soss.sourceLogpdf(m)
quote
    _ℓ = 0.0
    _ℓ += logpdf(Normal(), x)
    return _ℓ
end

Maybe a better way to do this would be to have a function makename (or something) that's user-configurable with some easy default. So currently it would be

makename(:ℓ) == :_ℓ

@DilumAluthge what's a sensible way to set this up so users can change it? Maybe as a Ref{Function}?

Originally posted by @cscherrer in #176 (comment)

Something simple like this should be sufficient:

const internal_name_prefix = Ref{Symbol}(:_)

function make_internal_name(original_name::Symbol)
    return Symbol(internal_name_prefix[], original_name)
end

Example usage:

julia> make_internal_name(:foo)
:_foo

julia> make_internal_name(:bar)
:_bar

julia> internal_name_prefix[] = :_internal_
:_internal_

julia> make_internal_name(:foo)
:_internal_foo

julia> make_internal_name(:bar)
:_internal_bar

Thanks @DilumAluthge! Just to check, this does disallow gensyming the names. Do you think that's ok? OTOH one problem with gensym is that you need to get all the variables you'll need ahead of time - otherwise calling it twice in the codegen would give different results.

Final concern is that we'll need this a lot, and make_internal_name is a lot to type. Maybe something like soss_name, soss_id, or soss_var, or one of those without the underscore? Or something similarly short?

Yeah I think it's fine to disallow gensyms. They make the code harder to read anyway.

soss_id seems good to me!