TuringLang / TuringLang/AbstractPPL.jl
isless on VarName
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 28
- Forks
- 12
- PR merge metrics
- No merged PRs in 30d
Description
Sometimes, it's handy to be able to sort VarNames.
If you naively do sort(vns; by=repr) i.e. order them according to lexicographic order of their string representations, then you get things like x[10] coming before x[1].
I already actually wrote some code here to do this:
using AbstractPPL: IndexLens, PropertyLens, ComposedFunction
function Base.isless(::typeof(identity), ::Union{IndexLens,PropertyLens,ComposedFunction})
return true
end
function Base.isless(::Union{IndexLens,PropertyLens,ComposedFunction}, ::typeof(identity))
return false
end
Base.isless(opt1::IndexLens, opt2::PropertyLens) = true
Base.isless(opt1::PropertyLens, opt2::IndexLens) = false
function Base.isless(opt1::IndexLens, opt2::IndexLens)
return isless(opt1.indices, opt2.indices)
end
function Base.isless(opt1::PropertyLens{sym1}, opt2::PropertyLens{sym2}) where {sym1,sym2}
return isless(sym1, sym2)
end
function Base.isless(opt1::Union{IndexLens,PropertyLens}, opt2::ComposedFunction)
if isequal(opt1, opt2.outer)
return true
else
return isless(opt1, opt2.outer)
end
end
function Base.isless(opt1::ComposedFunction, opt2::Union{IndexLens,PropertyLens})
if isequal(opt1.outer, opt2)
return false
else
return isless(opt1.outer, opt2)
end
end
function Base.isless(opt1::ComposedFunction, opt2::ComposedFunction)
if isequal(opt1.outer, opt2)
return isless(opt1.inner, opt2.inner)
else
return isless(opt1.outer, opt2)
end
end
function Base.isless(vn1::VarName{sym1}, vn2::VarName{sym2}) where {sym1,sym2}
if sym1 == sym2
return isless(AbstractPPL.getoptic(vn1), AbstractPPL.getoptic(vn2))
else
return isless(sym1, sym2)
end
end
function Base.isless(vn1::VarName{sym1,typeof(identity)}, vn2::VarName{sym2,typeof(identity)}) where {sym1,sym2}
return isless(sym1, sym2)
end
This was originally part of FlexiChains, but it's of course quite nasty piracy which is why I removed it. Its natural home would be AbstractPPL. And tests should be added, of course.
Although note that this is actually still kind of piracy. The functions on the optics for example should recurse into a different function that's not called Base.isless.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the definitions and existing uses of VarName, IndexLens, PropertyLens, and ComposedFunction, then inspect AbstractPPL.getoptic and the relevant ordering interfaces. Determine whether the proposed comparisons belong in AbstractPPL without piracy, add ordering tests for variable names and optics, and consider the issue's stated recursion concern before marking the behavior complete.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100