probcomp / Gen.jl

A general-purpose probabilistic programming system with programmable inference
https://gen.dev
Apache License 2.0
1.79k stars 160 forks source link

Extend static modeling language syntax #213

Open marcoct opened 4 years ago

marcoct commented 4 years ago

The implementation of the SML (static modeling language) is currently broken down into stages:

There are many extensions to the surface syntax that would make it less restricted, that would not require changes to the underlying IR. This includes:

I am putting these extensions into one issue, because I think that the SML surface syntax code could probably be redesigned with this set of capabilities in mind, while provisioning for yet additional extensions in the future.

marcoct commented 4 years ago

A revisiting of the SML code should also address the poor error messages that are currently thrown during syntax errors involving @trace: https://github.com/probcomp/Gen/issues/90

AriMKatz commented 4 years ago

Would loops based on foldl help, by preserving more structure? see discussion here https://github.com/JuliaLang/julia/pull/35074#issuecomment-597803494

ztangent commented 4 years ago

I've started taking a stab at this in ztangent:sml-refactor! Right now I've just switched over some of the pattern-matching logic to use @capture from MacroTools so it's a little neater, but I've a good sense of how the parsing code is like now and how it might need to change.

ztangent commented 4 years ago

Partially addressed by #214! Still unaddressed:

femtomc commented 4 years ago

One comment I noticed about the extensions outlined above: it looks like they could be addressed by an SSA form analysis and transformation using the IRTools representation (before constructing the static DAG representation).

Although @ztangent has already introduced a number of well-tested extensions using syntax capture - some of the issues (e.g. capturing control flow with an expansion-time bounds check into a combinator) can most easily be handled when all control flow is branch unless statements between basic blocks. The main benefit of this approach is that everything is de-sugared to branch unless (it might also make the analysis performed with syntax capture easier to express as mutation is explicit).

One thing I’m not sure about with this representation is how macro calls are represented. It may be possible to expand and inline calls in the representation.

Edit: I’ll post a few examples when I’m rested today or tomorrow.

Edit2: After looking at the parsing functions for the static DSL, this suggestion may not be a good fit solution. It’s certainly true that you don’t need more complex machinery for determining if there’s a constant loop bound - assuming it’s just a literal. As a first start, it’s likely overkill to setup the infrastructure to do this.

marcoct commented 3 years ago

Support for @assert statements is needed. (Support should be added at the static IR level).

femtomc commented 3 years ago

@marcoct if you provide a usage example, I could likely do this.

femtomc commented 3 years ago

In particular, at parse time, could move the macro into a Expr(:block, ...) inside the thunk we put in JuliaNode instances. This defers the requirement of having a special node to handle macros, for example.