Skip to content

Commit

Permalink
Merge pull request #17 from TuringLang/dw/varname
Browse files Browse the repository at this point in the history
  • Loading branch information
phipsgabler committed Apr 7, 2021
2 parents b7a2e48 + b315b92 commit 0d31764
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Expand Up @@ -3,7 +3,7 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
keywords = ["probablistic programming"]
license = "MIT"
desc = "Common interfaces for probabilistic programming"
version = "0.1.2"
version = "0.1.3"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
10 changes: 4 additions & 6 deletions src/AbstractPPL.jl
@@ -1,11 +1,5 @@
module AbstractPPL


include("varname.jl")
include("abstractprobprog.jl")
include("abstractmodeltrace.jl")


# VarName
export VarName,
getsym,
Expand All @@ -27,5 +21,9 @@ export AbstractProbabilisticProgram
# Abstract traces
export AbstractModelTrace

include("varname.jl")
include("abstractprobprog.jl")
include("abstractmodeltrace.jl")
include("deprecations.jl")

end # module
2 changes: 2 additions & 0 deletions src/deprecations.jl
@@ -0,0 +1,2 @@
@deprecate VarName(sym::Symbol) VarName{sym}()
@deprecate VarName(sym::Symbol, indexing::Tuple) VarName{sym}(indexing)
40 changes: 20 additions & 20 deletions src/varname.jl
@@ -1,5 +1,5 @@
"""
VarName(sym[, indexing=()])
VarName{sym}(indexing::Tuple=())
A variable identifier for a symbol `sym` and indices `indexing` in the format
returned by [`@vinds`](@ref).
Expand All @@ -10,30 +10,30 @@ stores the indices requires to access the random variable from the Julia variabl
as a tuple of tuples. Each element of the tuple thereby contains the indices of one indexing
operation.
`VarName`s can be manually constructed using the `VarName(sym, indexing)` constructor, or from an
`VarName`s can be manually constructed using the `VarName{sym}(indexing)` constructor, or from an
indexing expression through the [`@varname`](@ref) convenience macro.
# Examples
```jldoctest
julia> vn = VarName(:x, ((Colon(), 1), (2,)))
julia> vn = VarName{:x}(((Colon(), 1), (2,)))
x[Colon(),1][2]
julia> vn.indexing
((Colon(), 1), (2,))
julia> VarName(AbstractPPL.@vsym(x[:, 1][1+1]), AbstractPPL.@vinds(x[:, 1][1+1]))
julia> @varname x[:, 1][1+1]
x[Colon(),1][2]
```
"""
struct VarName{sym, T<:Tuple}
indexing::T
end

VarName(sym::Symbol, indexing::Tuple = ()) = VarName{sym, typeof(indexing)}(indexing)
VarName{sym}(indexing::Tuple=()) where {sym} = new{sym,typeof(indexing)}(indexing)
end

"""
VarName(vn::VarName[, indexing=()])
VarName(vn::VarName, indexing=())
Return a copy of `vn` with a new index `indexing`.
Expand All @@ -46,7 +46,7 @@ x
```
"""
function VarName(vn::VarName, indexing::Tuple = ())
return VarName{getsym(vn), typeof(indexing)}(indexing)
return VarName{getsym(vn)}(indexing)
end


Expand Down Expand Up @@ -249,11 +249,11 @@ macro varname(expr::Union{Expr, Symbol})
return esc(varname(expr))
end

varname(expr::Symbol) = VarName(expr)
varname(sym::Symbol) = :($(AbstractPPL.VarName){$(QuoteNode(sym))}())
function varname(expr::Expr)
if Meta.isexpr(expr, :ref)
sym, inds = vsym(expr), vinds(expr)
return :($(AbstractPPL.VarName)($(QuoteNode(sym)), $inds))
return :($(AbstractPPL.VarName){$(QuoteNode(sym))}($inds))
else
throw("Malformed variable name $(expr)!")
end
Expand All @@ -269,13 +269,13 @@ For example, `@vsym x[1]` returns `:x`.
## Examples
```jldoctest
julia> AbstractPPL.@vsym x
julia> @vsym x
:x
julia> AbstractPPL.@vsym x[1,1][2,3]
julia> @vsym x[1,1][2,3]
:x
julia> AbstractPPL.@vsym x[end]
julia> @vsym x[end]
:x
```
"""
Expand Down Expand Up @@ -307,19 +307,19 @@ Returns a tuple of tuples of the indices in `expr`.
## Examples
```jldoctest
julia> AbstractPPL.@vinds x
julia> @vinds x
()
julia> AbstractPPL.@vinds x[1,1][2,3]
julia> @vinds x[1,1][2,3]
((1, 1), (2, 3))
julia> AbstractPPL.@vinds x[:,1][2,:]
julia> @vinds x[:,1][2,:]
((Colon(), 1), (2, Colon()))
julia> AbstractPPL.@vinds x[2:3,1][2,1:2]
julia> @vinds x[2:3,1][2,1:2]
((2:3, 1), (2, 1:2))
julia> AbstractPPL.@vinds x[2:3,2:3][[1,2],[1,2]]
julia> @vinds x[2:3,2:3][[1,2],[1,2]]
((2:3, 2:3), ([1, 2], [1, 2]))
```
Expand All @@ -341,10 +341,10 @@ suitable for input of the [`VarName`](@ref) constructor.
## Examples
```jldoctest
julia> AbstractPPL.vinds(:(x[end]))
julia> vinds(:(x[end]))
:((((lastindex)(x),),))
julia> AbstractPPL.vinds(:(x[1, end]))
julia> vinds(:(x[1, end]))
:(((1, (lastindex)(x, 2)),))
```
"""
Expand Down
4 changes: 4 additions & 0 deletions test/deprecations.jl
@@ -0,0 +1,4 @@
@testset "deprecations.jl" begin
@test (@test_deprecated VarName(:x)) == VarName{:x}()
@test (@test_deprecated VarName(:x, ((1,), (:, 2)))) == VarName{:x}(((1,), (:, 2)))
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Expand Up @@ -11,6 +11,8 @@ using Documenter
using Test

@testset "AbstractPPL.jl" begin
include("deprecations.jl")

@testset "doctests" begin
DocMeta.setdocmeta!(
AbstractPPL,
Expand Down

3 comments on commit 0d31764

@devmotion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/33743

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.3 -m "<description of version>" 0d31764f7104392a28d70b50637f554b416b21be
git push origin v0.1.3

@phipsgabler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @devmotion! When I woke up this morning, my first thought was that I forgot to register yesterday, and should do it ASAP to keep you happy :D

Please sign in to comment.