Skip to content

Commit 82fe21c

Browse files
committed
make singleton objects more inferable and remove invalid @generated functions from irrationals
1 parent 48e43a6 commit 82fe21c

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

base/inference.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ typealias VarTable Array{Any,1}
3434
type VarState
3535
typ
3636
undef::Bool
37+
function VarState(typ::ANY, undef::Bool)
38+
if isa(typ, DataType) && isdefined(typ, :instance)
39+
# replace singleton types with their equivalent Const object
40+
typ = Const(typ.instance)
41+
end
42+
return new(typ, undef)
43+
end
3744
end
3845

3946
immutable Const
4047
val
48+
Const(v::ANY) = new(v)
4149
end
4250

4351
type InferenceState

base/irrationals.jl

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,44 @@ end
5757
x < big(y)
5858
end
5959

60-
<=(x::Irrational,y::AbstractFloat) = x < y
61-
<=(x::AbstractFloat,y::Irrational) = x < y
60+
<=(x::Irrational, y::AbstractFloat) = x < y
61+
<=(x::AbstractFloat, y::Irrational) = x < y
6262

6363
# Irrational vs Rational
64-
@generated function <{T}(x::Irrational, y::Rational{T})
65-
bx = big(x())
66-
bx < 0 && T <: Unsigned && return true
67-
rx = rationalize(T,bx,tol=0)
68-
rx < bx ? :($rx < y) : :($rx <= y)
64+
@pure function rationalize{T<:Integer}(::Type{T}, x::Irrational; tol::Real=0)
65+
return rationalize(T, big(x), tol=tol)
6966
end
70-
@generated function <{T}(x::Rational{T}, y::Irrational)
71-
by = big(y())
72-
by < 0 && T <: Unsigned && return false
73-
ry = rationalize(T,by,tol=0)
74-
ry < by ? :(x <= $ry) : :(x < $ry)
67+
@pure function rationalize{T<:Integer}(::Type{T}, x::Irrational)
68+
return rationalize(T, big(x), tol=0)
69+
end
70+
@pure function lessrational{T<:Integer}(rx::Rational{T}, x::Irrational)
71+
# an @pure version of `<` for determining if the rationalization of
72+
# an irrational number required rounding up or down
73+
return rx < big(x)
74+
end
75+
function <{T}(x::Irrational, y::Rational{T})
76+
T <: Unsigned && x < 0.0 && return true
77+
rx = rationalize(T, x)
78+
if lessrational(rx, x)
79+
return rx < y
80+
else
81+
return rx <= y
82+
end
83+
end
84+
function <{T}(x::Rational{T}, y::Irrational)
85+
T <: Unsigned && y < 0.0 && return false
86+
ry = rationalize(T, y)
87+
if lessrational(ry, y)
88+
return x <= ry
89+
else
90+
return x < ry
91+
end
7592
end
7693
<(x::Irrational, y::Rational{BigInt}) = big(x) < y
7794
<(x::Rational{BigInt}, y::Irrational) = x < big(y)
7895

79-
<=(x::Irrational,y::Rational) = x < y
80-
<=(x::Rational,y::Irrational) = x < y
96+
<=(x::Irrational, y::Rational) = x < y
97+
<=(x::Rational, y::Irrational) = x < y
8198

8299
isfinite(::Irrational) = true
83100

0 commit comments

Comments
 (0)