@@ -595,9 +595,9 @@ julia> (parent ∘ parent ∘ parent)(@varname(x.a[1]))
595
595
x
596
596
```
597
597
"""
598
- function parent (vn:: VarName )
598
+ function parent (vn:: VarName{sym} ) where {sym}
599
599
p = parent (getoptic (vn))
600
- return p === nothing ? VarName (vn, identity) : VarName (vn, p)
600
+ return p === nothing ? VarName {sym} ( identity) : VarName {sym} ( p)
601
601
end
602
602
603
603
"""
@@ -712,7 +712,7 @@ ERROR: Could not find x.a[2] in x.a[1]
712
712
function remove_parent_optic (vn_parent:: VarName{sym} , vn_child:: VarName{sym} ) where {sym}
713
713
_, child, issuccess = splitoptic (getoptic (vn_child)) do optic
714
714
o = optic === nothing ? identity : optic
715
- VarName (vn_child, o) == vn_parent
715
+ o == getoptic ( vn_parent)
716
716
end
717
717
718
718
issuccess || error (" Could not find $vn_parent in $vn_child " )
898
898
899
899
# For `dictlike` we need to check wether `vn` is "immediately" present, or
900
900
# if some ancestor of `vn` is present in `dictlike`.
901
- function hasvalue (vals:: AbstractDict , vn:: VarName )
901
+ function hasvalue (vals:: AbstractDict , vn:: VarName{sym} ) where {sym}
902
902
# First we check if `vn` is present as is.
903
903
haskey (vals, vn) && return true
904
904
@@ -907,7 +907,7 @@ function hasvalue(vals::AbstractDict, vn::VarName)
907
907
# If `issuccess` is `true`, we found such a split, and hence `vn` is present.
908
908
parent, child, issuccess = splitoptic (getoptic (vn)) do optic
909
909
o = optic === nothing ? identity : optic
910
- haskey (vals, VarName (vn, o))
910
+ haskey (vals, VarName {sym} ( o))
911
911
end
912
912
# When combined with `VarInfo`, `nothing` is equivalent to `identity`.
913
913
keyoptic = parent === nothing ? identity : parent
@@ -916,7 +916,7 @@ function hasvalue(vals::AbstractDict, vn::VarName)
916
916
issuccess || return false
917
917
918
918
# At this point we just need to check that we `canview` the value.
919
- value = vals[VarName (vn, keyoptic)]
919
+ value = vals[VarName {sym} ( keyoptic)]
920
920
921
921
return canview (child, value)
922
922
end
927
927
Return value corresponding to `vn` in `values` by also looking
928
928
in the the actual values of the dict.
929
929
"""
930
- function nested_getindex (values:: AbstractDict , vn:: VarName )
930
+ function nested_getindex (values:: AbstractDict , vn:: VarName{sym} ) where {sym}
931
931
maybeval = get (values, vn, nothing )
932
932
if maybeval != = nothing
933
933
return maybeval
@@ -936,7 +936,7 @@ function nested_getindex(values::AbstractDict, vn::VarName)
936
936
# Split the optic into the key / `parent` and the extraction optic / `child`.
937
937
parent, child, issuccess = splitoptic (getoptic (vn)) do optic
938
938
o = optic === nothing ? identity : optic
939
- haskey (values, VarName (vn, o))
939
+ haskey (values, VarName {sym} ( o))
940
940
end
941
941
# When combined with `VarInfo`, `nothing` is equivalent to `identity`.
942
942
keyoptic = parent === nothing ? identity : parent
@@ -949,7 +949,7 @@ function nested_getindex(values::AbstractDict, vn::VarName)
949
949
950
950
# TODO : Should we also check that we `canview` the extracted `value`
951
951
# rather than just let it fail upon `get` call?
952
- value = values[VarName (vn, keyoptic)]
952
+ value = values[VarName {sym} ( keyoptic)]
953
953
return child (value)
954
954
end
955
955
@@ -1065,22 +1065,24 @@ x.z[2][1]
1065
1065
```
1066
1066
"""
1067
1067
varname_leaves (vn:: VarName , :: Real ) = [vn]
1068
- function varname_leaves (vn:: VarName , val:: AbstractArray{<:Union{Real,Missing}} )
1068
+ function varname_leaves (
1069
+ vn:: VarName{sym} , val:: AbstractArray{<:Union{Real,Missing}}
1070
+ ) where {sym}
1069
1071
return (
1070
- VarName (vn, Accessors. IndexLens (Tuple (I)) ∘ getoptic (vn)) for
1072
+ VarName {sym} ( Accessors. IndexLens (Tuple (I)) ∘ getoptic (vn)) for
1071
1073
I in CartesianIndices (val)
1072
1074
)
1073
1075
end
1074
- function varname_leaves (vn:: VarName , val:: AbstractArray )
1076
+ function varname_leaves (vn:: VarName{sym} , val:: AbstractArray ) where {sym}
1075
1077
return Iterators. flatten (
1076
- varname_leaves (VarName (vn, Accessors. IndexLens (Tuple (I)) ∘ getoptic (vn)), val[I])
1078
+ varname_leaves (VarName {sym} ( Accessors. IndexLens (Tuple (I)) ∘ getoptic (vn)), val[I])
1077
1079
for I in CartesianIndices (val)
1078
1080
)
1079
1081
end
1080
- function varname_leaves (vn:: VarName , val:: NamedTuple )
1081
- iter = Iterators. map (keys (val)) do sym
1082
- optic = Accessors. PropertyLens {sym } ()
1083
- varname_leaves (VarName (vn, optic ∘ getoptic (vn)), optic (val))
1082
+ function varname_leaves (vn:: VarName{sym} , val:: NamedTuple ) where {sym}
1083
+ iter = Iterators. map (keys (val)) do k
1084
+ optic = Accessors. PropertyLens {k } ()
1085
+ varname_leaves (VarName {sym} ( optic ∘ getoptic (vn)), optic (val))
1084
1086
end
1085
1087
return Iterators. flatten (iter)
1086
1088
end
@@ -1225,30 +1227,26 @@ value(leaf::Leaf) = leaf.value
1225
1227
# Leaf-types.
1226
1228
varname_and_value_leaves_inner (vn:: VarName , x:: Real ) = [Leaf (vn, x)]
1227
1229
function varname_and_value_leaves_inner (
1228
- vn:: VarName , val:: AbstractArray{<:Union{Real,Missing}}
1229
- )
1230
+ vn:: VarName{sym} , val:: AbstractArray{<:Union{Real,Missing}}
1231
+ ) where {sym}
1230
1232
return (
1231
1233
Leaf (
1232
- VarName (vn, DynamicPPL. Accessors. IndexLens (Tuple (I)) ∘ DynamicPPL. getoptic (vn)),
1233
- val[I],
1234
+ VarName {sym} (Accessors. IndexLens (Tuple (I)) ∘ AbstractPPL. getoptic (vn)), val[I]
1234
1235
) for I in CartesianIndices (val)
1235
1236
)
1236
1237
end
1237
1238
# Containers.
1238
- function varname_and_value_leaves_inner (vn:: VarName , val:: AbstractArray )
1239
+ function varname_and_value_leaves_inner (vn:: VarName{sym} , val:: AbstractArray ) where {sym}
1239
1240
return Iterators. flatten (
1240
1241
varname_and_value_leaves_inner (
1241
- VarName (vn, DynamicPPL. Accessors. IndexLens (Tuple (I)) ∘ DynamicPPL. getoptic (vn)),
1242
- val[I],
1242
+ VarName {sym} (Accessors. IndexLens (Tuple (I)) ∘ AbstractPPL. getoptic (vn)), val[I]
1243
1243
) for I in CartesianIndices (val)
1244
1244
)
1245
1245
end
1246
- function varname_and_value_leaves_inner (vn:: DynamicPPL.VarName , val:: NamedTuple )
1247
- iter = Iterators. map (keys (val)) do sym
1248
- optic = DynamicPPL. Accessors. PropertyLens {sym} ()
1249
- varname_and_value_leaves_inner (
1250
- VarName {getsym(vn)} (optic ∘ getoptic (vn)), optic (val)
1251
- )
1246
+ function varname_and_value_leaves_inner (vn:: VarName{sym} , val:: NamedTuple ) where {sym}
1247
+ iter = Iterators. map (keys (val)) do k
1248
+ optic = Accessors. PropertyLens {k} ()
1249
+ varname_and_value_leaves_inner (VarName {sym} (optic ∘ getoptic (vn)), optic (val))
1252
1250
end
1253
1251
1254
1252
return Iterators. flatten (iter)
@@ -1262,22 +1260,20 @@ function varname_and_value_leaves_inner(vn::VarName, x::Cholesky)
1262
1260
varname_and_value_leaves_inner (Accessors. PropertyLens {:U} () ∘ vn, x. U)
1263
1261
end
1264
1262
end
1265
- function varname_and_value_leaves_inner (vn:: VarName , x:: LinearAlgebra.LowerTriangular )
1263
+ function varname_and_value_leaves_inner (
1264
+ vn:: VarName{sym} , x:: LinearAlgebra.LowerTriangular
1265
+ ) where {sym}
1266
1266
return (
1267
- Leaf (
1268
- VarName (vn, DynamicPPL. Accessors. IndexLens (Tuple (I)) ∘ DynamicPPL. getoptic (vn)),
1269
- x[I],
1270
- )
1267
+ Leaf (VarName {sym} (Accessors. IndexLens (Tuple (I)) ∘ AbstractPPL. getoptic (vn)), x[I])
1271
1268
# Iteration over the lower-triangular indices.
1272
1269
for I in CartesianIndices (x) if I[1 ] >= I[2 ]
1273
1270
)
1274
1271
end
1275
- function varname_and_value_leaves_inner (vn:: VarName , x:: LinearAlgebra.UpperTriangular )
1272
+ function varname_and_value_leaves_inner (
1273
+ vn:: VarName{sym} , x:: LinearAlgebra.UpperTriangular
1274
+ ) where {sym}
1276
1275
return (
1277
- Leaf (
1278
- VarName (vn, DynamicPPL. Accessors. IndexLens (Tuple (I)) ∘ DynamicPPL. getoptic (vn)),
1279
- x[I],
1280
- )
1276
+ Leaf (VarName {sym} (Accessors. IndexLens (Tuple (I)) ∘ AbstractPPL. getoptic (vn)), x[I])
1281
1277
# Iteration over the upper-triangular indices.
1282
1278
for I in CartesianIndices (x) if I[1 ] <= I[2 ]
1283
1279
)
0 commit comments