Closed marcoct closed 6 years ago
In the refactoring that is in progress on https://github.com/probcomp/Gen.jl/tree/20170713-marcoct-generators, this is no longer an issue. There is no global registry. There is now a register_primitive
that simply does a bit of code generation:
function register_primitive(shortname::Symbol, generator_type::Type)
eval(quote $shortname = $generator_type() end)
eval(quote (g::$generator_type)(args...) = (g, args) end)
eval(quote export $shortname end)
eval(quote export $(Symbol(generator_type)) end)
end
There is also now a Generator{TraceType}
abstract type, of which there are a few extant sub-types:
AtomicGenerator{T} = Generator{AtomicTrace{T}}
(generators that only have a exposed value, i.e. "probabilistic modules" of https://arxiv.org/abs/1612.04759)ProbabilisticProgram <: Generator{AbstractTrace}
(a probabilitsic program is a Generator now, the trace name should be changed)The basic Generator
interface is score = generate!(generator, args, trace)
with some syntactic sugars for, e.g. score = generate!(generator(args), trace)
.
Currently, the only compositional generator is a ProbabilisticProgram
, and for these the only sub-regenerators are currently AtomicGenerator
s, and the value itself is stored in the program's trace instead of the (AtomicTrace
) sub-trace.
Generators should just be Julia objects. You should be able to construct a generator and use it without need for a global registry. The registry is only used syntactic sugar that allows e.g.
flip(0.5)
to be expanded toFlipGenerator(0.5)
within a tagged expression andgenerate(FlipGenerator(0.5))
outside of a tagged expression. However, use of that syntactic sugar is completely optional, and there is no need to use the global registry for new generators.See https://github.com/probcomp/gen-examples/commit/aae30377e5b1f9365606ffd6bb4b2dd33e85dc58 for an experimental implementation of this.