Skip to content

Commit 76f64dd

Browse files
authored
remove and deprecate references to indexing in favour of lens (#31)
1 parent 33f896c commit 76f64dd

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

src/AbstractPPL.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module AbstractPPL
22

33
# VarName
4-
export VarName, getsym, getindexing, inspace, subsumes, varname, vsym, @varname, @vsym
4+
export VarName, getsym, getindexing, getlens, inspace, subsumes, varname, vsym, @varname, @vsym
55

66

77
# Abstract model functions

src/varname.jl

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,41 @@ using Setfield
22
using Setfield: PropertyLens, ComposedLens, IdentityLens, IndexLens, DynamicIndexLens
33

44
"""
5-
VarName{sym}(indexing::Tuple=())
5+
VarName{sym}(lens::Lens=IdentityLens())
66
7-
A variable identifier for a symbol `sym` and indices `indexing` in the format
8-
returned by [`@vinds`](@ref).
7+
A variable identifier for a symbol `sym` and lens `lens`.
98
109
The Julia variable in the model corresponding to `sym` can refer to a single value or to a
11-
hierarchical array structure of univariate, multivariate or matrix variables. The field `indexing`
10+
hierarchical array structure of univariate, multivariate or matrix variables. The field `lens`
1211
stores the indices requires to access the random variable from the Julia variable indicated by `sym`
13-
as a tuple of tuples. Each element of the tuple thereby contains the indices of one indexing
12+
as a tuple of tuples. Each element of the tuple thereby contains the indices of one lens
1413
operation.
1514
16-
`VarName`s can be manually constructed using the `VarName{sym}(indexing)` constructor, or from an
17-
indexing expression through the [`@varname`](@ref) convenience macro.
15+
`VarName`s can be manually constructed using the `VarName{sym}(lens)` constructor, or from an
16+
lens expression through the [`@varname`](@ref) convenience macro.
1817
1918
# Examples
2019
2120
```jldoctest; setup=:(using Setfield)
2221
julia> vn = VarName{:x}(Setfield.IndexLens((Colon(), 1)) ∘ Setfield.IndexLens((2, )))
2322
x[:,1][2]
2423
25-
julia> vn.indexing
24+
julia> getlens(vn)
2625
(@lens _[Colon(), 1][2])
2726
2827
julia> @varname x[:, 1][1+1]
2928
x[:,1][2]
3029
```
3130
"""
3231
struct VarName{sym,T<:Lens}
33-
indexing::T
32+
lens::T
3433

35-
function VarName{sym}(indexing=IdentityLens()) where {sym}
34+
function VarName{sym}(lens=IdentityLens()) where {sym}
3635
# TODO: Should we completely disallow or just `@warn` of limited support?
37-
if !is_static_lens(indexing)
38-
error("attempted to construct `VarName` with dynamic lens of type $(nameof(typeof(indexing)))")
36+
if !is_static_lens(lens)
37+
error("attempted to construct `VarName` with dynamic lens of type $(nameof(typeof(lens)))")
3938
end
40-
return new{sym,typeof(indexing)}(indexing)
39+
return new{sym,typeof(lens)}(lens)
4140
end
4241
end
4342

@@ -56,14 +55,13 @@ function is_static_lens(::Type{ComposedLens{LO, LI}}) where {LO, LI}
5655
end
5756

5857
# A bit of backwards compatibility.
59-
# TODO: Should we deprecate this?
6058
VarName{sym}(indexing::Tuple) where {sym} = VarName{sym}(tupleindex2lens(indexing))
6159

6260
"""
63-
VarName(vn::VarName, indexing::Lens)
61+
VarName(vn::VarName, lens::Lens)
6462
VarName(vn::VarName, indexing::Tuple)
6563
66-
Return a copy of `vn` with a new index `indexing`.
64+
Return a copy of `vn` with a new index `lens`/`indexing`.
6765
6866
```jldoctest; setup=:(using Setfield)
6967
julia> VarName(@varname(x[1][2:3]), Setfield.IndexLens((2,)))
@@ -76,7 +74,7 @@ julia> VarName(@varname(x[1][2:3]))
7674
x
7775
```
7876
"""
79-
VarName(vn::VarName, indexing::Lens = IdentityLens()) = VarName{getsym(vn)}(indexing)
77+
VarName(vn::VarName, lens::Lens = IdentityLens()) = VarName{getsym(vn)}(lens)
8078

8179
function VarName(vn::VarName, indexing::Tuple)
8280
return VarName{getsym(vn)}(tupleindex2lens(indexing))
@@ -105,58 +103,59 @@ julia> getsym(@varname(y))
105103
"""
106104
getsym(vn::VarName{sym}) where {sym} = sym
107105

108-
109106
"""
110-
getindexing(vn::VarName)
107+
getlens(vn::VarName)
111108
112-
Return the indexing tuple of the Julia variable used to generate `vn`.
109+
Return the lens of the Julia variable used to generate `vn`.
113110
114111
## Examples
115112
116113
```jldoctest
117-
julia> getindexing(@varname(x[1][2:3]))
114+
julia> getlens(@varname(x[1][2:3]))
118115
(@lens _[1][2:3])
119116
120-
julia> getindexing(@varname(y))
117+
julia> getlens(@varname(y))
121118
(@lens _)
122119
```
123120
"""
124-
getindexing(vn::VarName) = vn.indexing
121+
getlens(vn::VarName) = vn.lens
122+
123+
@deprecate getindexing(vn::VarName) getlens(vn)
125124

126125
"""
127126
get(obj, vn::VarName{sym})
128127
129-
Alias for `get(obj, PropertyLens{sym}() ∘ vn.indexing)`.
128+
Alias for `get(obj, PropertyLens{sym}() ∘ getlens(vn))`.
130129
"""
131130
function Setfield.get(obj, vn::VarName{sym}) where {sym}
132-
return Setfield.get(obj, PropertyLens{sym}() vn.indexing)
131+
return Setfield.get(obj, PropertyLens{sym}() getlens(vn))
133132
end
134133

135134
"""
136135
set(obj, vn::VarName{sym}, value)
137136
138-
Alias for `set(obj, PropertyLens{sym}() ∘ vn.indexing, value)`.
137+
Alias for `set(obj, PropertyLens{sym}() ∘ getlens(vn), value)`.
139138
"""
140139
function Setfield.set(obj, vn::VarName{sym}, value) where {sym}
141-
return Setfield.set(obj, PropertyLens{sym}() vn.indexing, value)
140+
return Setfield.set(obj, PropertyLens{sym}() getlens(vn), value)
142141
end
143142

144143

145-
Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getindexing(vn)), h)
144+
Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getlens(vn)), h)
146145
function Base.:(==)(x::VarName, y::VarName)
147-
return getsym(x) == getsym(y) && getindexing(x) == getindexing(y)
146+
return getsym(x) == getsym(y) && getlens(x) == getlens(y)
148147
end
149148

150149
# Allow compositions with lenses.
151150
function Base.:(vn::VarName{sym,<:Lens}, lens::Lens) where {sym}
152-
return VarName{sym}(vn.indexing lens)
151+
return VarName{sym}(getlens(vn) lens)
153152
end
154153

155154
function Base.show(io::IO, vn::VarName{<:Any,<:Lens})
156155
# No need to check `Setfield.has_atlens_support` since
157156
# `VarName` does not allow dynamic lenses.
158157
print(io, getsym(vn))
159-
_print_application(io, vn.indexing)
158+
_print_application(io, getlens(vn))
160159
end
161160

162161
# This is all just to allow to convert `Colon()` into `:`.
@@ -268,7 +267,7 @@ Currently _not_ supported are:
268267
- Trailing ones: `x[2, 1]` does not subsume `x[2]` for a vector `x`
269268
"""
270269
function subsumes(u::VarName, v::VarName)
271-
return getsym(u) == getsym(v) && subsumes(u.indexing, v.indexing)
270+
return getsym(u) == getsym(v) && subsumes(u.lens, v.lens)
272271
end
273272

274273
# Idea behind `subsumes` for `Lens` is that we traverse the two lenses in parallel,
@@ -443,7 +442,7 @@ julia> AbstractPPL.concretize(@varname(x.a[1, end][:]), x)
443442
x.a[1,2][:]
444443
```
445444
"""
446-
concretize(vn::VarName, x) = VarName(vn, concretize(vn.indexing, x))
445+
concretize(vn::VarName, x) = VarName(vn, concretize(getlens(vn), x))
447446

448447
"""
449448
@varname(expr)
@@ -479,32 +478,32 @@ x[2]
479478
Under the hood Setfield.jl's `Lens` are used for the indexing:
480479
481480
```jldoctest
482-
julia> @varname(x).indexing
481+
julia> getlens(@varname(x))
483482
(@lens _)
484483
485-
julia> @varname(x[1]).indexing
484+
julia> getlens(@varname(x[1]))
486485
(@lens _[1])
487486
488-
julia> @varname(x[:, 1]).indexing
487+
julia> getlens(@varname(x[:, 1]))
489488
(@lens _[Colon(), 1])
490489
491-
julia> @varname(x[:, 1][2]).indexing
490+
julia> getlens(@varname(x[:, 1][2]))
492491
(@lens _[Colon(), 1][2])
493492
494-
julia> @varname(x[1,2][1+5][45][3]).indexing
493+
julia> getlens(@varname(x[1,2][1+5][45][3]))
495494
(@lens _[1, 2][6][45][3])
496495
```
497496
498497
This also means that we support property access:
499498
500499
```jldoctest
501-
julia> @varname(x.a).indexing
500+
julia> getlens(@varname(x.a))
502501
(@lens _.a)
503502
504-
julia> @varname(x.a[1]).indexing
503+
julia> getlens(@varname(x.a[1]))
505504
(@lens _.a[1])
506505
507-
julia> x = (a = [(b = rand(2), )], ); @varname(x.a[1].b[end]).indexing
506+
julia> x = (a = [(b = rand(2), )], ); getlens(@varname(x.a[1].b[end]))
508507
(@lens _.a[1].b[2])
509508
```
510509

0 commit comments

Comments
 (0)