Skip to content

Commit b4157a4

Browse files
penelopeysmsunxd3
andauthored
VarName rework (#150)
Co-authored-by: Xianda Sun <5433119+sunxd3@users.noreply.github.com>
1 parent 0086beb commit b4157a4

27 files changed

Lines changed: 2478 additions & 1463 deletions

HISTORY.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,56 @@
1+
## 0.14.0
2+
3+
This release overhauls the `VarName` type.
4+
Much of the external API for traversing and manipulating `VarName`s (once they have been constructed) has been preserved, but if you use the `VarName` type directly, there are significant changes.
5+
6+
**Internal representation**
7+
8+
The `optic` field of VarName now uses our hand-rolled optic types, which are subtypes of `AbstractPPL.AbstractOptic`.
9+
Previously these were optics from Accessors.jl.
10+
11+
This change was made for two reasons: firstly, it is easier to provide custom behaviour for VarNames as we avoid running into possible type piracy issues, and secondly, the linked-list data structure used in `AbstractOptic` is easier to work with than Accessors.jl, which used `Base.ComposedFunction` to represent optic compositions and required a lot of care to avoid a litany of issues with associativity and identity optics (see e.g. https://github.com/JuliaLang/julia/pull/54877).
12+
13+
To construct an optic, the easiest way is to use the `@opticof` macro, which superficially behaves similarly to `Accessors.@optic` (for example, you can write `@opticof _[1].y.z`), but also supports automatic concretization by passing a second parameter (just like `@varname`).
14+
15+
**Concretization**
16+
17+
VarNames using 'dynamic' indices, i.e., `begin` and `end`, are now instantiated in a 'dynamic' form, meaning that these indices are unresolved.
18+
These indices need to be resolved, or concretized, against the actual container.
19+
For example, `@varname(x[end])` is dynamic, but when concretized against `x = randn(3)`, this becomes `@varname(x[3])`.
20+
This can be done using `concretize(varname, x)`.
21+
22+
The idea of concretization is not new to AbstractPPL.
23+
However, there are some differences:
24+
25+
- Colons are no longer concretized: they *always* remain as Colons, even after calling `concretize`.
26+
- Previously, AbstractPPL would refuse to allow you to construct unconcretized versions of `begin` and `end`. This is no longer the case; you can now create such VarNames in their unconcretized forms.
27+
This is useful, for example, when indexing into a chain that contains `x` as a variable-length vector. This change allows you to write `chain[@varname(x[end])]` without having AbstractPPL throw an error.
28+
29+
**Keyword arguments to `getindex`**
30+
31+
VarNames can now be constructed with keyword arguments in `Index` optics, for example `@varname(x[i=1])`.
32+
This is specifically implemented to support DimensionalData.jl's DimArrays.
33+
34+
**Other interface functions**
35+
36+
The `vsym` function (and `@vsym`) has been removed; you should use `getsym(vn)` instead.
37+
38+
The `Base.get` and `Accessors.set` methods for VarNames have been removed (these were responsible for method ambiguities).
39+
Instead of using these methods you can first convert the `VarName` to an optic using `varname_to_optic(vn)`, and then use the getter and setter methods on the optics.
40+
41+
VarNames cannot be composed with optics now (compose the optics yourself).
42+
43+
The `inspace` function has been removed.
44+
It used to be relevant for Turing's old Gibbs sampler; but now it no longer serves any use.
45+
46+
`ConcretizedSlice` has been removed (since colons are no longer concretized).
47+
48+
The subsumption interface has been pared down to just a single function, `subsumes`.
49+
All other functions, such as `subsumedby`, `uncomparable`, and the Unicode operators, have been removed.
50+
51+
Serialization still works exactly as before.
52+
However, you will see differences in the serialization output compared to previous versions, due to the changes in the internal structure.
53+
154
## 0.13.6
255

356
Fix a missing qualifier in AbstractPPLDistributionsExt.

Project.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
33
keywords = ["probablistic programming"]
44
license = "MIT"
55
desc = "Common interfaces for probabilistic programming"
6-
version = "0.13.6"
6+
version = "0.14.0"
77

88
[deps]
99
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
1010
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
11+
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"
1112
DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
1213
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1314
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
15+
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
16+
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
1417
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1518
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1619

1720
[weakdeps]
1821
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1922

2023
[extensions]
21-
AbstractPPLDistributionsExt = ["Distributions"]
24+
AbstractPPLDistributionsExt = ["Distributions", "LinearAlgebra"]
2225

2326
[compat]
2427
AbstractMCMC = "2, 3, 4, 5"
2528
Accessors = "0.1"
29+
BangBang = "0.4"
2630
DensityInterface = "0.4"
2731
Distributions = "0.25"
2832
JSON = "0.19 - 0.21, 1"
29-
LinearAlgebra = "<0.0.1, 1.10"
33+
LinearAlgebra = "<0.0.1, 1"
34+
MacroTools = "0.5"
35+
OrderedCollections = "1.8.1"
3036
Random = "1.6"
3137
StatsBase = "0.32, 0.33, 0.34"
32-
julia = "1.10"
38+
julia = "1.10.8"

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ AbstractPPL = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
33
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
44
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
55
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
6+
7+
[sources]
8+
AbstractPPL = {path = "../"}

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ DocMeta.setdocmeta!(AbstractPPL, :DocTestSetup, :(using AbstractPPL); recursive=
99
makedocs(;
1010
sitename="AbstractPPL",
1111
modules=[AbstractPPL, Base.get_extension(AbstractPPL, :AbstractPPLDistributionsExt)],
12-
pages=["index.md", "api.md", "interface.md"],
12+
pages=["index.md", "varname.md", "pplapi.md", "interface.md"],
1313
checkdocs=:exports,
1414
doctest=false,
1515
)

docs/src/api.md

Lines changed: 0 additions & 64 deletions
This file was deleted.

docs/src/pplapi.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Probabilistic programming API
2+
3+
## Abstract model functions
4+
5+
```@docs
6+
AbstractProbabilisticProgram
7+
condition
8+
decondition
9+
fix
10+
unfix
11+
logdensityof
12+
AbstractContext
13+
evaluate!!
14+
```
15+
16+
## Abstract traces
17+
18+
```@docs
19+
AbstractModelTrace
20+
```

0 commit comments

Comments
 (0)