@@ -2,42 +2,41 @@ using Setfield
2
2
using Setfield: PropertyLens, ComposedLens, IdentityLens, IndexLens, DynamicIndexLens
3
3
4
4
"""
5
- VarName{sym}(indexing::Tuple= ())
5
+ VarName{sym}(lens::Lens=IdentityLens ())
6
6
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`.
9
8
10
9
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 `
12
11
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
14
13
operation.
15
14
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.
18
17
19
18
# Examples
20
19
21
20
```jldoctest; setup=:(using Setfield)
22
21
julia> vn = VarName{:x}(Setfield.IndexLens((Colon(), 1)) ∘ Setfield.IndexLens((2, )))
23
22
x[:,1][2]
24
23
25
- julia> vn.indexing
24
+ julia> getlens(vn)
26
25
(@lens _[Colon(), 1][2])
27
26
28
27
julia> @varname x[:, 1][1+1]
29
28
x[:,1][2]
30
29
```
31
30
"""
32
31
struct VarName{sym,T<: Lens }
33
- indexing :: T
32
+ lens :: T
34
33
35
- function VarName {sym} (indexing = IdentityLens ()) where {sym}
34
+ function VarName {sym} (lens = IdentityLens ()) where {sym}
36
35
# 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 ))) " )
39
38
end
40
- return new {sym,typeof(indexing )} (indexing )
39
+ return new {sym,typeof(lens )} (lens )
41
40
end
42
41
end
43
42
@@ -56,14 +55,13 @@ function is_static_lens(::Type{ComposedLens{LO, LI}}) where {LO, LI}
56
55
end
57
56
58
57
# A bit of backwards compatibility.
59
- # TODO : Should we deprecate this?
60
58
VarName {sym} (indexing:: Tuple ) where {sym} = VarName {sym} (tupleindex2lens (indexing))
61
59
62
60
"""
63
- VarName(vn::VarName, indexing ::Lens)
61
+ VarName(vn::VarName, lens ::Lens)
64
62
VarName(vn::VarName, indexing::Tuple)
65
63
66
- Return a copy of `vn` with a new index `indexing`.
64
+ Return a copy of `vn` with a new index `lens`/` indexing`.
67
65
68
66
```jldoctest; setup=:(using Setfield)
69
67
julia> VarName(@varname(x[1][2:3]), Setfield.IndexLens((2,)))
@@ -76,7 +74,7 @@ julia> VarName(@varname(x[1][2:3]))
76
74
x
77
75
```
78
76
"""
79
- VarName (vn:: VarName , indexing :: Lens = IdentityLens ()) = VarName {getsym(vn)} (indexing )
77
+ VarName (vn:: VarName , lens :: Lens = IdentityLens ()) = VarName {getsym(vn)} (lens )
80
78
81
79
function VarName (vn:: VarName , indexing:: Tuple )
82
80
return VarName {getsym(vn)} (tupleindex2lens (indexing))
@@ -105,58 +103,59 @@ julia> getsym(@varname(y))
105
103
"""
106
104
getsym (vn:: VarName{sym} ) where {sym} = sym
107
105
108
-
109
106
"""
110
- getindexing (vn::VarName)
107
+ getlens (vn::VarName)
111
108
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`.
113
110
114
111
## Examples
115
112
116
113
```jldoctest
117
- julia> getindexing (@varname(x[1][2:3]))
114
+ julia> getlens (@varname(x[1][2:3]))
118
115
(@lens _[1][2:3])
119
116
120
- julia> getindexing (@varname(y))
117
+ julia> getlens (@varname(y))
121
118
(@lens _)
122
119
```
123
120
"""
124
- getindexing (vn:: VarName ) = vn. indexing
121
+ getlens (vn:: VarName ) = vn. lens
122
+
123
+ @deprecate getindexing (vn:: VarName ) getlens (vn)
125
124
126
125
"""
127
126
get(obj, vn::VarName{sym})
128
127
129
- Alias for `get(obj, PropertyLens{sym}() ∘ vn.indexing )`.
128
+ Alias for `get(obj, PropertyLens{sym}() ∘ getlens(vn) )`.
130
129
"""
131
130
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) )
133
132
end
134
133
135
134
"""
136
135
set(obj, vn::VarName{sym}, value)
137
136
138
- Alias for `set(obj, PropertyLens{sym}() ∘ vn.indexing , value)`.
137
+ Alias for `set(obj, PropertyLens{sym}() ∘ getlens(vn) , value)`.
139
138
"""
140
139
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)
142
141
end
143
142
144
143
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)
146
145
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)
148
147
end
149
148
150
149
# Allow compositions with lenses.
151
150
function Base.:∘ (vn:: VarName{sym,<:Lens} , lens:: Lens ) where {sym}
152
- return VarName {sym} (vn . indexing ∘ lens)
151
+ return VarName {sym} (getlens (vn) ∘ lens)
153
152
end
154
153
155
154
function Base. show (io:: IO , vn:: VarName{<:Any,<:Lens} )
156
155
# No need to check `Setfield.has_atlens_support` since
157
156
# `VarName` does not allow dynamic lenses.
158
157
print (io, getsym (vn))
159
- _print_application (io, vn . indexing )
158
+ _print_application (io, getlens (vn) )
160
159
end
161
160
162
161
# This is all just to allow to convert `Colon()` into `:`.
@@ -268,7 +267,7 @@ Currently _not_ supported are:
268
267
- Trailing ones: `x[2, 1]` does not subsume `x[2]` for a vector `x`
269
268
"""
270
269
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 )
272
271
end
273
272
274
273
# 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)
443
442
x.a[1,2][:]
444
443
```
445
444
"""
446
- concretize (vn:: VarName , x) = VarName (vn, concretize (vn . indexing , x))
445
+ concretize (vn:: VarName , x) = VarName (vn, concretize (getlens (vn) , x))
447
446
448
447
"""
449
448
@varname(expr)
@@ -479,32 +478,32 @@ x[2]
479
478
Under the hood Setfield.jl's `Lens` are used for the indexing:
480
479
481
480
```jldoctest
482
- julia> @varname(x).indexing
481
+ julia> getlens( @varname(x))
483
482
(@lens _)
484
483
485
- julia> @varname(x[1]).indexing
484
+ julia> getlens( @varname(x[1]))
486
485
(@lens _[1])
487
486
488
- julia> @varname(x[:, 1]).indexing
487
+ julia> getlens( @varname(x[:, 1]))
489
488
(@lens _[Colon(), 1])
490
489
491
- julia> @varname(x[:, 1][2]).indexing
490
+ julia> getlens( @varname(x[:, 1][2]))
492
491
(@lens _[Colon(), 1][2])
493
492
494
- julia> @varname(x[1,2][1+5][45][3]).indexing
493
+ julia> getlens( @varname(x[1,2][1+5][45][3]))
495
494
(@lens _[1, 2][6][45][3])
496
495
```
497
496
498
497
This also means that we support property access:
499
498
500
499
```jldoctest
501
- julia> @varname(x.a).indexing
500
+ julia> getlens( @varname(x.a))
502
501
(@lens _.a)
503
502
504
- julia> @varname(x.a[1]).indexing
503
+ julia> getlens( @varname(x.a[1]))
505
504
(@lens _.a[1])
506
505
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]))
508
507
(@lens _.a[1].b[2])
509
508
```
510
509
0 commit comments