-
Notifications
You must be signed in to change notification settings - Fork 9
Fix begin/end replacement in varname expansion (fixes #23) #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
401f8e4
Fix begin/end replacement in varname expansion (fixes #23)
phipsgabler 06f2998
More complicated reimplementation, partial caching
phipsgabler a278b5d
Try to fix caching
phipsgabler db991c2
Convert foldl to a loop; update doctests
phipsgabler c367825
Fix docstrings with eval
phipsgabler 5610351
Fix tests on versions before `begin` indexing
phipsgabler 94c6e15
Add more documentation comments
phipsgabler 6b65987
Switch from :ref to maybeview
phipsgabler ae461c5
Improve docstrings/comments
phipsgabler 41ab5ea
Bump version
phipsgabler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -241,8 +241,18 @@ julia> @varname(x[:, 1][2]).indexing | |||||
|
|
||||||
| julia> @varname(x[1,2][1+5][45][3]).indexing | ||||||
| ((1, 2), (6,), (45,), (3,)) | ||||||
|
|
||||||
| julia> let a = [42]; @varname(a[1][end][3]); end | ||||||
| a[1][1][3] | ||||||
| ``` | ||||||
|
|
||||||
| !!! warning | ||||||
| As you can see in the last example, `@varname` does not do any bounds checking! | ||||||
|
|
||||||
| Only `begin` and `end` indexing, which depend on the _runtime size_ of the array, does that! | ||||||
| Their usage _requires_ that the array over which the indexing expression is defined, in | ||||||
| order for `firstindex` and `lastindex` to work in the expanded code. | ||||||
|
|
||||||
| !!! compat "Julia 1.5" | ||||||
| Using `begin` in an indexing expression to refer to the first index requires at least | ||||||
| Julia 1.5. | ||||||
|
|
@@ -254,8 +264,9 @@ end | |||||
| 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)) | ||||||
| head = vsym(expr) | ||||||
| inds = vinds(expr, head) | ||||||
| return :($(AbstractPPL.VarName){$(QuoteNode(head))}($inds)) | ||||||
| else | ||||||
| error("Malformed variable name $(expr)!") | ||||||
| end | ||||||
|
|
@@ -334,37 +345,128 @@ macro vinds(expr::Union{Expr, Symbol}) | |||||
| end | ||||||
|
|
||||||
|
|
||||||
| @static if VERSION < v"1.5.0-DEV.666" | ||||||
| _replace_ref_begin_end(ex, withex) = Base.replace_ref_end_!(copy(ex), withex) | ||||||
| _replace_ref_begin_end(ex::Symbol, withex) = Base.replace_ref_end_!(ex, withex) | ||||||
| _index_replacement_for(s) = :($lastindex($s)) | ||||||
| _index_replacement_for(s, n) = :($lastindex($s, $n)) | ||||||
| else | ||||||
| _replace_ref_begin_end(ex, withex) = Base.replace_ref_begin_end_!(copy(ex), withex) | ||||||
| _replace_ref_begin_end(ex::Symbol, withex) = Base.replace_ref_begin_end_!(ex, withex) | ||||||
| _index_replacement_for(s) = :($firstindex($s)), :($lastindex($s)) | ||||||
| _index_replacement_for(s, n) = :($firstindex($s, $n)), :($lastindex($s, $n)) | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| """ | ||||||
| vinds(expr) | ||||||
|
|
||||||
| Return the indexing part of the [`@varname`](@ref)-compatible expression `expr` as an expression | ||||||
| suitable for input of the [`VarName`](@ref) constructor. | ||||||
| suitable for input of the [`VarName`](@ref) constructor (i.e., a tuple of tuples). | ||||||
|
|
||||||
| ## Examples | ||||||
|
|
||||||
| ```jldoctest | ||||||
| julia> vinds(:(x[end])) | ||||||
| :((((lastindex)(x),),)) | ||||||
| julia> x = [10, 20, 30]; eval(vinds(:(x[end]))) | ||||||
| ((3,),) | ||||||
|
|
||||||
|
|
||||||
| julia> x = [10 20]; eval(vinds(:(x[1, end]))) | ||||||
| ((1, 2),) | ||||||
|
|
||||||
| julia> x = [[1, 2]]; eval(vinds(:(x[1][end]))) | ||||||
| ((1,), (2,)) | ||||||
phipsgabler marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| julia> x = ([1, 2], ); eval(vinds(:(x[1][end]))) # tuple | ||||||
| ((1,), (2,)) | ||||||
|
|
||||||
| julia> x = [fill([[10], [20, 30]], 2, 2, 2)] | ||||||
| if VERSION < v"1.5.0-DEV.666" | ||||||
| eval(vinds(:(x[1][2, end, :][2][end]))) | ||||||
| else | ||||||
| eval(vinds(Meta.parse("x[begin][2, end, :][2][end]"))) | ||||||
| end | ||||||
| ((1,), (2, 2, Colon()), (2,), (2,)) | ||||||
|
|
||||||
| julia> vinds(:(x[1, end])) | ||||||
| :(((1, (lastindex)(x, 2)),)) | ||||||
| ``` | ||||||
| """ | ||||||
| function vinds end | ||||||
| function vinds(expr, head = vsym(expr)) | ||||||
| # see https://github.com/JuliaLang/julia/blob/bb5b98e72a151c41471d8cc14cacb495d647fb7f/base/views.jl#L17-L75 | ||||||
| indexing = _straighten_indexing(expr) | ||||||
| inds = Expr[] # collection of result indices | ||||||
| partial = head # partial :ref expressions, used in caching | ||||||
| cached_exprs = Vector{Pair{Symbol, Expr}}() # cache for partial expressions going into a let | ||||||
|
|
||||||
| for ixs in indexing | ||||||
| # S becomes the name of the cached variable | ||||||
| S = (partial == head) ? head : gensym(:S) | ||||||
| used_S = false | ||||||
|
|
||||||
| nixs = length(ixs) | ||||||
| if nixs == 1 | ||||||
| # for linear indexing, we use `lastindex(x)` | ||||||
| ixs[1], used = _replace_ref_begin_end(ixs[1], _index_replacement_for(S)) | ||||||
| used_S |= used | ||||||
| elseif nixs > 1 | ||||||
| # for cartesian indexing, we need `lastindex(x, i)` | ||||||
| for i in eachindex(ixs) | ||||||
| ixs[i], used = _replace_ref_begin_end(ixs[i], _index_replacement_for(S, i)) | ||||||
| used_S |= used | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| vinds(expr::Symbol) = Expr(:tuple) | ||||||
| function vinds(expr::Expr) | ||||||
| if Meta.isexpr(expr, :ref) | ||||||
| ex = copy(expr) | ||||||
| @static if VERSION < v"1.5.0-DEV.666" | ||||||
| Base.replace_ref_end!(ex) | ||||||
| if used_S && partial !== head | ||||||
| # cache that expression if we actually used it, and use the new name in the | ||||||
| # partial expression | ||||||
| push!(cached_exprs, S => partial) | ||||||
| partial = Expr(:call, Base.maybeview, S, ixs...) | ||||||
| else | ||||||
| Base.replace_ref_begin_end!(ex) | ||||||
| partial = Expr(:call, Base.maybeview, partial, ixs...) | ||||||
| end | ||||||
| last = Expr(:tuple, ex.args[2:end]...) | ||||||
| init = vinds(ex.args[1]).args | ||||||
| return Expr(:tuple, init..., last) | ||||||
|
|
||||||
| push!(inds, Expr(:tuple, ixs...)) | ||||||
| end | ||||||
|
|
||||||
| # finally make the tuple of tuples | ||||||
| tuple_expr = Expr(:tuple, inds...) | ||||||
|
|
||||||
| if length(cached_exprs) == 0 | ||||||
| return tuple_expr | ||||||
| else | ||||||
| # construct one big let expression | ||||||
| cached_assignments = [:($S = $partial) for (S, partial) in cached_exprs] | ||||||
| return Expr(:let, Expr(:block, cached_assignments...), tuple_expr) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| """ | ||||||
| _straighten_indexing(expr) | ||||||
|
|
||||||
| Extract a list of lists of (raw) indices of an iterated `:ref` expression. | ||||||
|
|
||||||
| ```julia | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does that work with a non-exported function? |
||||||
| julia> _straighten_indexing(:(x[begin][2, end, :][2][end])) | ||||||
| 4-element Array{Array{Any,1},1}: | ||||||
| [:begin] | ||||||
| [2, :end, :(:)] | ||||||
| [2] | ||||||
| [:end] | ||||||
| ``` | ||||||
| """ | ||||||
| _straighten_indexing(expr::Symbol) = Vector{Any}[] | ||||||
| function _straighten_indexing(expr::Expr) | ||||||
| if Meta.isexpr(expr, :ref) | ||||||
| init = _straighten_indexing(expr.args[1]) | ||||||
| last = expr.args[2:end] | ||||||
| return push!(init, last) | ||||||
| else | ||||||
| error("Mis-formed variable name $(expr)!") | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.