probcomp / GenExperimental.jl

Featherweight embedded probabilistic programming language and compositional inference programming library
MIT License
17 stars 2 forks source link

Make use of generator registry optional and used for syntactic sugar only #51

Closed marcoct closed 6 years ago

marcoct commented 7 years ago

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 to FlipGenerator(0.5) within a tagged expression and generate(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.

marcoct commented 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
marcoct commented 6 years ago

There is also now a Generator{TraceType} abstract type, of which there are a few extant sub-types:

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 AtomicGenerators, and the value itself is stored in the program's trace instead of the (AtomicTrace) sub-trace.