Skip to content

Commit 3a854e2

Browse files
committed
formatting
1 parent 8c1d193 commit 3a854e2

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

src/AbstractPPL.jl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
module AbstractPPL
22

33
# VarName
4-
export VarName,
5-
getsym,
6-
getindexing,
7-
inspace,
8-
subsumes,
9-
varname,
10-
vsym,
11-
@varname,
12-
@vsym
4+
export VarName, getsym, getindexing, inspace, subsumes, varname, vsym, @varname, @vsym
135

146

157
# Abstract model functions
16-
export AbstractProbabilisticProgram,
17-
condition,
18-
decondition,
19-
logdensity
8+
export AbstractProbabilisticProgram, condition, decondition, logdensity
209

2110

2211
# Abstract traces

src/varname.jl

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ julia> @varname x[:, 1][1+1]
2929
x[:,1][2]
3030
```
3131
"""
32-
struct VarName{sym, T<:Lens}
32+
struct VarName{sym,T<:Lens}
3333
indexing::T
3434

35-
VarName{sym}(indexing=IdentityLens()) where {sym} = new{sym,typeof(indexing)}(indexing)
35+
VarName{sym}(indexing = IdentityLens()) where {sym} =
36+
new{sym,typeof(indexing)}(indexing)
3637
end
3738

3839
# A bit of backwards compatibility.
@@ -56,15 +57,16 @@ julia> VarName(@varname(x[1][2:3]))
5657
x
5758
```
5859
"""
59-
VarName(vn::VarName, indexing::Lens=IdentityLens()) = VarName{getsym(vn)}(indexing)
60+
VarName(vn::VarName, indexing::Lens = IdentityLens()) = VarName{getsym(vn)}(indexing)
6061

6162
function VarName(vn::VarName, indexing::Tuple)
6263
return VarName{getsym(vn)}(tupleindex2lens(indexing))
6364
end
6465

6566
tupleindex2lens(indexing::Tuple{}) = IdentityLens()
6667
tupleindex2lens(indexing::Tuple{<:Tuple}) = IndexLens(first(indexing))
67-
tupleindex2lens(indexing::Tuple) = IndexLens(first(indexing)) tupleindex2lens(indexing[2:end])
68+
tupleindex2lens(indexing::Tuple) =
69+
IndexLens(first(indexing)) tupleindex2lens(indexing[2:end])
6870

6971
"""
7072
getsym(vn::VarName)
@@ -81,7 +83,7 @@ julia> getsym(@varname(y))
8183
:y
8284
```
8385
"""
84-
getsym(vn::VarName{sym}) where sym = sym
86+
getsym(vn::VarName{sym}) where {sym} = sym
8587

8688

8789
"""
@@ -121,14 +123,14 @@ end
121123

122124

123125
Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getindexing(vn)), h)
124-
Base.:(==)(x::VarName, y::VarName) = getsym(x) == getsym(y) && getindexing(x) == getindexing(y)
126+
Base.:(==)(x::VarName, y::VarName) =
127+
getsym(x) == getsym(y) && getindexing(x) == getindexing(y)
125128

126-
# Composition rules similar to the standard one for lenses, but we need a special
127-
# one for the "empty" `VarName{..., Tuple{}}`.
128-
Base.:(vn::VarName{sym,<:IdentityLens}, lens::Lens) where {sym} = VarName{sym}(lens)
129-
Base.:(vn::VarName{sym,<:Lens}, lens::Lens) where {sym} = VarName{sym}(vn.indexing lens)
129+
# Allow compositions with lenses.
130+
Setfield.compose(vn::VarName{sym,<:Lens}, lens::Lens) where {sym} =
131+
VarName{sym}(vn.indexing lens)
130132

131-
function Base.show(io::IO, vn::VarName{<:Any, <:Lens})
133+
function Base.show(io::IO, vn::VarName{<:Any,<:Lens})
132134
print(io, getsym(vn))
133135
_print_application(io, vn.indexing)
134136
end
@@ -138,7 +140,8 @@ function _print_application(io::IO, l::ComposedLens)
138140
_print_application(io, l.outer)
139141
_print_application(io, l.inner)
140142
end
141-
_print_application(io::IO, l::IndexLens) = print(io, "[", join(map(prettify_index, l.indices), ","), "]")
143+
_print_application(io::IO, l::IndexLens) =
144+
print(io, "[", join(map(prettify_index, l.indices), ","), "]")
142145
# This is a bit weird but whatever. We're almost always going to
143146
# `concretize` anyways.
144147
_print_application(io::IO, l::DynamicIndexLens) = print(io, l, "(_)")
@@ -247,7 +250,8 @@ end
247250
subsumes(t::IdentityLens, u::Lens) = true
248251
subsumes(t::Lens, u::IdentityLens) = false
249252

250-
subsumes(t::ComposedLens, u::ComposedLens) = subsumes(t.outer, u.outer) && subsumes(t.inner, u.inner)
253+
subsumes(t::ComposedLens, u::ComposedLens) =
254+
subsumes(t.outer, u.outer) && subsumes(t.inner, u.inner)
251255

252256
# If `t` is still a composed lens, then there is no way it can subsume `u` since `u` is a
253257
# leaf of the "lens-tree".
@@ -345,7 +349,7 @@ e.g. `x[:][1][2]` becomes `((Colon(), ), (1, ), (2, ))`.
345349
The result is compatible with [`subsumes_index`](@ref) for `Tuple` input.
346350
"""
347351
combine_indices(lens::Lens) = (), lens
348-
combine_indices(lens::IndexLens) = (lens.indices, ), nothing
352+
combine_indices(lens::IndexLens) = (lens.indices,), nothing
349353
function combine_indices(lens::ComposedLens{<:IndexLens})
350354
indices, next = combine_indices(lens.inner)
351355
return (lens.outer.indices, indices...), next
@@ -369,13 +373,13 @@ function subsumes_index(t::Tuple, u::Tuple) # does x[i]... subsume x[j]...?
369373
return _issubindex(first(t), first(u)) && subsumes_index(Base.tail(t), Base.tail(u))
370374
end
371375

372-
const AnyIndex = Union{Int, AbstractVector{Int}, Colon}
376+
const AnyIndex = Union{Int,AbstractVector{Int},Colon}
373377
_issubindex_(::Tuple{Vararg{AnyIndex}}, ::Tuple{Vararg{AnyIndex}}) = false
374-
function _issubindex(t::NTuple{N, AnyIndex}, u::NTuple{N, AnyIndex}) where {N}
378+
function _issubindex(t::NTuple{N,AnyIndex}, u::NTuple{N,AnyIndex}) where {N}
375379
return all(_issubrange(j, i) for (i, j) in zip(t, u))
376380
end
377381

378-
const ConcreteIndex = Union{Int, AbstractVector{Int}} # this include all kinds of ranges
382+
const ConcreteIndex = Union{Int,AbstractVector{Int}} # this include all kinds of ranges
379383

380384
"""Determine whether indices `i` are contained in `j`, treating `:` as universal set."""
381385
_issubrange(i::ConcreteIndex, j::ConcreteIndex) = issubset(i, j)
@@ -455,12 +459,13 @@ julia> @varname(x[1,2][1+5][45][3]).indexing
455459
Using `begin` in an indexing expression to refer to the first index requires at least
456460
Julia 1.5.
457461
"""
458-
macro varname(expr::Union{Expr, Symbol}, concretize::Bool=false)
462+
macro varname(expr::Union{Expr,Symbol}, concretize::Bool = false)
459463
return varname(expr, concretize)
460464
end
461465

462-
varname(sym::Symbol, concretize::Bool=false) = :($(AbstractPPL.VarName){$(QuoteNode(sym))}())
463-
function varname(expr::Expr, concretize::Bool=false)
466+
varname(sym::Symbol, concretize::Bool = false) =
467+
:($(AbstractPPL.VarName){$(QuoteNode(sym))}())
468+
function varname(expr::Expr, concretize::Bool = false)
464469
if Meta.isexpr(expr, :ref) || Meta.isexpr(expr, :.)
465470
# Split into object/base symbol and lens.
466471
sym_escaped, lens = Setfield.parse_obj_lens(expr)
@@ -469,7 +474,10 @@ function varname(expr::Expr, concretize::Bool=false)
469474
sym = drop_escape(sym_escaped)
470475

471476
return if concretize && Setfield.need_dynamic_lens(expr)
472-
:($(AbstractPPL.concretize)($(AbstractPPL.VarName){$(QuoteNode(sym))}($lens), $sym_escaped))
477+
:($(AbstractPPL.concretize)(
478+
$(AbstractPPL.VarName){$(QuoteNode(sym))}($lens),
479+
$sym_escaped,
480+
))
473481
else
474482
:($(AbstractPPL.VarName){$(QuoteNode(sym))}($lens))
475483
end
@@ -503,7 +511,7 @@ julia> @vsym x[end]
503511
:x
504512
```
505513
"""
506-
macro vsym(expr::Union{Expr, Symbol})
514+
macro vsym(expr::Union{Expr,Symbol})
507515
return QuoteNode(vsym(expr))
508516
end
509517

@@ -522,4 +530,3 @@ function vsym(expr::Expr)
522530
error("Malformed variable name $(expr)!")
523531
end
524532
end
525-

0 commit comments

Comments
 (0)