Skip to content
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

Remove invalidating ! overloads #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/Static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Static
import IfElse: ifelse

export StaticInt, StaticFloat64, StaticSymbol, True, False, StaticBool, NDIndex
export dynamic, is_static, known, static, static_promote
export dynamic, is_static, known, static, static_promote, static_!

"""
StaticSymbol
Expand Down Expand Up @@ -315,7 +315,6 @@ Base.inv(x::StaticNumber{N}) where {N} = one(x) / x
@inline Base.iseven(@nospecialize x::StaticNumber) = iseven(known(x))
@inline Base.isodd(@nospecialize x::StaticNumber) = isodd(known(x))


Base.AbstractFloat(x::StaticNumber) = StaticFloat64(x)

Base.abs(::StaticNumber{N}) where {N} = static(abs(N))
Expand Down Expand Up @@ -423,8 +422,13 @@ Base.xor(::StaticInteger{X}, ::StaticInteger{Y}) where {X, Y} = static(xor(X, Y)
Base.xor(::StaticInteger{X}, y::Union{Integer, Missing}) where {X} = xor(X, y)
Base.xor(x::Union{Integer, Missing}, ::StaticInteger{Y}) where {Y} = xor(x, Y)

Base.:(!)(::True) = False()
Base.:(!)(::False) = True()
# These are heavily invalidating, leading to major compile time increases downstream
#Base.:(!)(::True) = False()
#Base.:(!)(::False) = True()
# Instead, use the not invalidating form
static_!(x::Bool) = !x
static_!(::True) = False()
static_!(::False) = True()

Base.all(::Tuple{Vararg{True}}) = true
Base.all(::Tuple{Vararg{Union{True, False}}}) = false
Expand Down Expand Up @@ -745,47 +749,47 @@ end
"""
eq(x, y)

Equivalent to `!=` but if `x` and `y` are both static returns a `StaticBool.
Equivalent to `!=` but if `x` and `y` are both static returns a `StaticBool`.
"""
eq(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x == y)
eq(x) = Base.Fix2(eq, x)

"""
ne(x, y)

Equivalent to `!=` but if `x` and `y` are both static returns a `StaticBool.
Equivalent to `!=` but if `x` and `y` are both static returns a `StaticBool`.
"""
ne(x::X, y::Y) where {X, Y} = !eq(x, y)
ne(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x != y)
ne(x) = Base.Fix2(ne, x)

"""
gt(x, y)

Equivalent to `>` but if `x` and `y` are both static returns a `StaticBool.
Equivalent to `>` but if `x` and `y` are both static returns a `StaticBool`.
"""
gt(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x > y)
gt(x) = Base.Fix2(gt, x)

"""
ge(x, y)

Equivalent to `>=` but if `x` and `y` are both static returns a `StaticBool.
Equivalent to `>=` but if `x` and `y` are both static returns a `StaticBool`.
"""
ge(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x >= y)
ge(x) = Base.Fix2(ge, x)

"""
le(x, y)

Equivalent to `<=` but if `x` and `y` are both static returns a `StaticBool.
Equivalent to `<=` but if `x` and `y` are both static returns a `StaticBool`.
"""
le(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x <= y)
le(x) = Base.Fix2(le, x)

"""
lt(x, y)

Equivalent to `<` but if `x` and `y` are both static returns a `StaticBool.
Equivalent to `<` but if `x` and `y` are both static returns a `StaticBool`.
"""
lt(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x < y)
lt(x) = Base.Fix2(lt, x)
Expand Down
2 changes: 0 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ end

@test @inferred(~t) === f
@test @inferred(~f) === t
@test @inferred(!t) === f
@test @inferred(!f) === t
@test @inferred(+t) === StaticInt(1)
@test @inferred(+f) === StaticInt(0)
@test @inferred(-t) === StaticInt(-1)
Expand Down