2424
2525Base. Symbol (@nospecialize (s:: StaticSymbol )) = known (s)
2626
27- abstract type StaticNumber {N} <: Number end
27+ abstract type StaticInteger {N} <: Number end
2828
29- abstract type StaticInteger{N} <: StaticNumber{N} end
29+ """
30+ StaticBool(x::Bool) -> True/False
31+
32+ A statically typed `Bool`.
33+ """
34+ abstract type StaticBool{bool} <: StaticInteger{bool} end
35+
36+ struct True <: StaticBool{true} end
37+
38+ struct False <: StaticBool{false} end
39+
40+ StaticBool {true} () = True ()
41+ StaticBool {false} () = False ()
42+ StaticBool (x:: StaticBool ) = x
43+ function StaticBool (x:: Bool )
44+ if x
45+ return True ()
46+ else
47+ return False ()
48+ end
49+ end
3050
3151"""
3252 StaticInt(N::Int) -> StaticInt{N}()
@@ -41,6 +61,10 @@ struct StaticInt{N} <: StaticInteger{N}
4161 StaticInt (:: Val{N} ) where {N} = StaticInt (N)
4262end
4363
64+ include (" float.jl" )
65+
66+ const StaticNumber{N} = Union{StaticInt{N}, StaticBool{N}, StaticFloat64{N}}
67+
4468Base. getindex (x:: Tuple , :: StaticInt{N} ) where {N} = getfield (x, N)
4569
4670Base. zero (@nospecialize (:: StaticInt )) = StaticInt {0} ()
@@ -50,42 +74,6 @@ function Base.checkindex(::Type{Bool}, inds::AbstractUnitRange, ::StaticNumber{N
5074 checkindex (Bool, inds, N)
5175end
5276
53- """
54- StaticFloat64{N}
55-
56- A statically sized `Float64`.
57- Use `StaticInt(N)` instead of `Val(N)` when you want it to behave like a number.
58- """
59- struct StaticFloat64{N} <: StaticNumber{N}
60- StaticFloat64 {N} () where {N} = new {N::Float64} ()
61- StaticFloat64 (x:: Float64 ) = new {x} ()
62- StaticFloat64 (x:: Int ) = new {Base.sitofp(Float64, x)::Float64} ()
63- StaticFloat64 (x:: StaticInt{N} ) where {N} = StaticFloat64 (convert (Float64, N))
64- StaticFloat64 (x:: Complex ) = StaticFloat64 (convert (Float64, x))
65- end
66-
67- """
68- StaticBool(x::Bool) -> True/False
69-
70- A statically typed `Bool`.
71- """
72- abstract type StaticBool{bool} <: StaticInteger{bool} end
73-
74- struct True <: StaticBool{true} end
75-
76- struct False <: StaticBool{false} end
77-
78- StaticBool {true} () = True ()
79- StaticBool {false} () = False ()
80- StaticBool (x:: StaticBool ) = x
81- function StaticBool (x:: Bool )
82- if x
83- return True ()
84- else
85- return False ()
86- end
87- end
88-
8977ifelse (:: True , @nospecialize (x), @nospecialize (y)) = x
9078ifelse (:: False , @nospecialize (x), @nospecialize (y)) = y
9179
@@ -304,16 +292,22 @@ function Base.promote_rule(@nospecialize(T1::Type{<:StaticNumber}),
304292 promote_rule (T2, eltype (T1))
305293end
306294
307- Base.:(~ )(:: StaticNumber {N} ) where {N} = static (~ N)
295+ Base.:(~ )(:: StaticInteger {N} ) where {N} = static (~ N)
308296
309- Base. inv (x:: StaticNumber{N} ) where {N} = one (x) / x
297+ Base. inv (x:: StaticInteger{N} ) where {N} = one (x) / x
298+
299+ Base. zero (@nospecialize T:: Type{<:StaticInt} ) = StaticInt (0 )
300+ Base. zero (@nospecialize T:: Type{<:StaticBool} ) = False ()
301+
302+ Base. one (@nospecialize T:: Type{<:StaticBool} ) = True ()
303+ Base. one (@nospecialize T:: Type{<:StaticInt} ) = StaticInt (1 )
310304
311- @inline Base. one (@nospecialize T:: Type{<:StaticNumber} ) = static (one (eltype (T)))
312- @inline Base. zero (@nospecialize T:: Type{<:StaticNumber} ) = static (zero (eltype (T)))
313305@inline Base. iszero (:: Union{StaticInt{0}, StaticFloat64{0.0}, False} ) = true
314- @inline Base. iszero (@nospecialize x:: StaticNumber ) = false
315- @inline Base. isone (:: Union{One, FloatOne, True} ) = true
316- @inline Base. isone (@nospecialize x:: StaticNumber ) = false
306+ @inline Base. iszero (@nospecialize x:: Union{StaticInt, True, StaticFloat64} ) = false
307+
308+ @inline Base. isone (:: Union{One, True, StaticFloat64{1.0}} ) = true
309+ @inline Base. isone (@nospecialize x:: Union{StaticInt, False, StaticFloat64} ) = false
310+
317311@inline Base. iseven (@nospecialize x:: StaticNumber ) = iseven (known (x))
318312@inline Base. isodd (@nospecialize x:: StaticNumber ) = isodd (known (x))
319313
332326# Base.Bool(::StaticInt{N}) where {N} = Bool(N)
333327
334328Base. Integer (@nospecialize (x:: StaticInt )) = x
335- (:: Type{T} )(x:: StaticNumber ) where {T <: Real } = T (known (x))
329+ (:: Type{T} )(x:: StaticInteger ) where {T <: Real } = T (known (x))
336330function (@nospecialize (T:: Type{<:StaticNumber} ))(x:: Union {AbstractFloat,
337331 AbstractIrrational, Integer,
338332 Rational})
@@ -342,10 +336,10 @@ end
342336@inline Base.:(- )(:: StaticNumber{N} ) where {N} = static (- N)
343337Base.:(* )(:: Union{AbstractFloat, AbstractIrrational, Integer, Rational} , y:: Zero ) = y
344338Base.:(* )(x:: Zero , :: Union{AbstractFloat, AbstractIrrational, Integer, Rational} ) = x
345- Base.:(* )(:: StaticNumber {X} , :: StaticNumber {Y} ) where {X, Y} = static (X * Y)
346- Base.:(/ )(:: StaticNumber {X} , :: StaticNumber {Y} ) where {X, Y} = static (X / Y)
347- Base.:(- )(:: StaticNumber {X} , :: StaticNumber {Y} ) where {X, Y} = static (X - Y)
348- Base.:(+ )(:: StaticNumber {X} , :: StaticNumber {Y} ) where {X, Y} = static (X + Y)
339+ Base.:(* )(:: StaticInteger {X} , :: StaticInteger {Y} ) where {X, Y} = static (X * Y)
340+ Base.:(/ )(:: StaticInteger {X} , :: StaticInteger {Y} ) where {X, Y} = static (X / Y)
341+ Base.:(- )(:: StaticInteger {X} , :: StaticInteger {Y} ) where {X, Y} = static (X - Y)
342+ Base.:(+ )(:: StaticInteger {X} , :: StaticInteger {Y} ) where {X, Y} = static (X + Y)
349343Base.:(- )(x:: Ptr , :: StaticInt{N} ) where {N} = x - N
350344Base.:(- )(:: StaticInt{N} , y:: Ptr ) where {N} = y - N
351345Base.:(+ )(x:: Ptr , :: StaticInt{N} ) where {N} = x + N
@@ -356,39 +350,32 @@ Base.:(+)(::StaticInt{N}, y::Ptr) where {N} = y + N
356350function Base. div (:: StaticNumber{X} , :: StaticNumber{Y} , m:: RoundingMode ) where {X, Y}
357351 static (div (X, Y, m))
358352end
359- Base. div (x:: Real , :: StaticNumber {Y} , m:: RoundingMode ) where {Y} = div (x, Y, m)
353+ Base. div (x:: Real , :: StaticInteger {Y} , m:: RoundingMode ) where {Y} = div (x, Y, m)
360354Base. div (:: StaticNumber{X} , y:: Real , m:: RoundingMode ) where {X} = div (X, y, m)
361355Base. div (x:: StaticBool , y:: False ) = throw (DivideError ())
362356Base. div (x:: StaticBool , y:: True ) = x
363357
364358Base. rem (@nospecialize (x:: StaticNumber ), T:: Type{<:Integer} ) = rem (known (x), T)
365359Base. rem (:: StaticNumber{X} , :: StaticNumber{Y} ) where {X, Y} = static (rem (X, Y))
366- Base. rem (x:: Real , :: StaticNumber{Y} ) where {Y} = rem (x, Y)
367- Base. rem (:: StaticNumber{X} , y:: Real ) where {X} = rem (X, y)
368-
360+ Base. rem (x:: Real , :: StaticInteger{Y} ) where {Y} = rem (x, Y)
361+ Base. rem (:: StaticInteger{X} , y:: Real ) where {X} = rem (X, y)
369362Base. mod (:: StaticNumber{X} , :: StaticNumber{Y} ) where {X, Y} = static (mod (X, Y))
370363
371- Base. round (:: StaticFloat64{M} ) where {M} = StaticFloat64 (round (M))
372- roundtostaticint (:: StaticFloat64{M} ) where {M} = StaticInt (round (Int, M))
373- roundtostaticint (x:: AbstractFloat ) = round (Int, x)
374- floortostaticint (:: StaticFloat64{M} ) where {M} = StaticInt (Base. fptosi (Int, M))
375- floortostaticint (x:: AbstractFloat ) = Base. fptosi (Int, x)
376-
377364Base.:(== )(:: StaticNumber{X} , :: StaticNumber{Y} ) where {X, Y} = == (X, Y)
378365
379366Base.:(< )(:: StaticNumber{X} , :: StaticNumber{Y} ) where {X, Y} = < (X, Y)
380367
381- Base. isless (:: StaticNumber {X} , :: StaticNumber {Y} ) where {X, Y} = isless (X, Y)
382- Base. isless (:: StaticNumber {X} , y:: Real ) where {X} = isless (X, y)
368+ Base. isless (:: StaticInteger {X} , :: StaticInteger {Y} ) where {X, Y} = isless (X, Y)
369+ Base. isless (:: StaticInteger {X} , y:: Real ) where {X} = isless (X, y)
383370Base. isless (x:: Real , :: StaticInteger{Y} ) where {Y} = isless (x, Y)
384371
385- Base. min (:: StaticNumber {X} , :: StaticNumber {Y} ) where {X, Y} = static (min (X, Y))
386- Base. min (:: StaticNumber {X} , y:: Number ) where {X} = min (X, y)
387- Base. min (x:: Number , :: StaticNumber {Y} ) where {Y} = min (x, Y)
372+ Base. min (:: StaticInteger {X} , :: StaticInteger {Y} ) where {X, Y} = static (min (X, Y))
373+ Base. min (:: StaticInteger {X} , y:: Number ) where {X} = min (X, y)
374+ Base. min (x:: Number , :: StaticInteger {Y} ) where {Y} = min (x, Y)
388375
389- Base. max (:: StaticNumber {X} , :: StaticNumber {Y} ) where {X, Y} = static (max (X, Y))
390- Base. max (:: StaticNumber {X} , y:: Number ) where {X} = max (X, y)
391- Base. max (x:: Number , :: StaticNumber {Y} ) where {Y} = max (x, Y)
376+ Base. max (:: StaticInteger {X} , :: StaticInteger {Y} ) where {X, Y} = static (max (X, Y))
377+ Base. max (:: StaticInteger {X} , y:: Number ) where {X} = max (X, y)
378+ Base. max (x:: Number , :: StaticInteger {Y} ) where {Y} = max (x, Y)
392379
393380Base. minmax (:: StaticNumber{X} , :: StaticNumber{Y} ) where {X, Y} = static (minmax (X, Y))
394381
@@ -448,57 +435,6 @@ Functionally equivalent to `fieldtype(T, f)` except `f` may be a static type.
448435@inline field_type (:: Type{T} , :: StaticInt{N} ) where {T, N} = fieldtype (T, N)
449436@inline field_type (:: Type{T} , :: StaticSymbol{S} ) where {T, S} = fieldtype (T, S)
450437
451- Base. rad2deg (:: StaticFloat64{M} ) where {M} = StaticFloat64 (rad2deg (M))
452- Base. deg2rad (:: StaticFloat64{M} ) where {M} = StaticFloat64 (deg2rad (M))
453- @generated Base. cbrt (:: StaticFloat64{M} ) where {M} = StaticFloat64 (cbrt (M))
454- Base. mod2pi (:: StaticFloat64{M} ) where {M} = StaticFloat64 (mod2pi (M))
455- @generated Base. sinpi (:: StaticFloat64{M} ) where {M} = StaticFloat64 (sinpi (M))
456- @generated Base. cospi (:: StaticFloat64{M} ) where {M} = StaticFloat64 (cospi (M))
457- Base. exp (:: StaticFloat64{M} ) where {M} = StaticFloat64 (exp (M))
458- Base. exp2 (:: StaticFloat64{M} ) where {M} = StaticFloat64 (exp2 (M))
459- Base. exp10 (:: StaticFloat64{M} ) where {M} = StaticFloat64 (exp10 (M))
460- @generated Base. expm1 (:: StaticFloat64{M} ) where {M} = StaticFloat64 (expm1 (M))
461- @generated Base. log (:: StaticFloat64{M} ) where {M} = StaticFloat64 (log (M))
462- @generated Base. log2 (:: StaticFloat64{M} ) where {M} = StaticFloat64 (log2 (M))
463- @generated Base. log10 (:: StaticFloat64{M} ) where {M} = StaticFloat64 (log10 (M))
464- @generated Base. log1p (:: StaticFloat64{M} ) where {M} = StaticFloat64 (log1p (M))
465- @generated Base. sin (:: StaticFloat64{M} ) where {M} = StaticFloat64 (sin (M))
466- @generated Base. cos (:: StaticFloat64{M} ) where {M} = StaticFloat64 (cos (M))
467- @generated Base. tan (:: StaticFloat64{M} ) where {M} = StaticFloat64 (tan (M))
468- Base. sec (x:: StaticFloat64{M} ) where {M} = inv (cos (x))
469- Base. csc (x:: StaticFloat64{M} ) where {M} = inv (sin (x))
470- Base. cot (x:: StaticFloat64{M} ) where {M} = inv (tan (x))
471- @generated Base. asin (:: StaticFloat64{M} ) where {M} = StaticFloat64 (asin (M))
472- @generated Base. acos (:: StaticFloat64{M} ) where {M} = StaticFloat64 (acos (M))
473- @generated Base. atan (:: StaticFloat64{M} ) where {M} = StaticFloat64 (atan (M))
474- @generated Base. sind (:: StaticFloat64{M} ) where {M} = StaticFloat64 (sind (M))
475- @generated Base. cosd (:: StaticFloat64{M} ) where {M} = StaticFloat64 (cosd (M))
476- Base. tand (x:: StaticFloat64{M} ) where {M} = sind (x) / cosd (x)
477- Base. secd (x:: StaticFloat64{M} ) where {M} = inv (cosd (x))
478- Base. cscd (x:: StaticFloat64{M} ) where {M} = inv (sind (x))
479- Base. cotd (x:: StaticFloat64{M} ) where {M} = inv (tand (x))
480- Base. asind (x:: StaticFloat64{M} ) where {M} = rad2deg (asin (x))
481- Base. acosd (x:: StaticFloat64{M} ) where {M} = rad2deg (acos (x))
482- Base. asecd (x:: StaticFloat64{M} ) where {M} = rad2deg (asec (x))
483- Base. acscd (x:: StaticFloat64{M} ) where {M} = rad2deg (acsc (x))
484- Base. acotd (x:: StaticFloat64{M} ) where {M} = rad2deg (acot (x))
485- Base. atand (x:: StaticFloat64{M} ) where {M} = rad2deg (atan (x))
486- @generated Base. sinh (:: StaticFloat64{M} ) where {M} = StaticFloat64 (sinh (M))
487- Base. cosh (:: StaticFloat64{M} ) where {M} = StaticFloat64 (cosh (M))
488- Base. tanh (x:: StaticFloat64{M} ) where {M} = StaticFloat64 (tanh (M))
489- Base. sech (x:: StaticFloat64{M} ) where {M} = inv (cosh (x))
490- Base. csch (x:: StaticFloat64{M} ) where {M} = inv (sinh (x))
491- Base. coth (x:: StaticFloat64{M} ) where {M} = inv (tanh (x))
492- @generated Base. asinh (:: StaticFloat64{M} ) where {M} = StaticFloat64 (asinh (M))
493- @generated Base. acosh (:: StaticFloat64{M} ) where {M} = StaticFloat64 (acosh (M))
494- @generated Base. atanh (:: StaticFloat64{M} ) where {M} = StaticFloat64 (atanh (M))
495- Base. asech (x:: StaticFloat64{M} ) where {M} = acosh (inv (x))
496- Base. acsch (x:: StaticFloat64{M} ) where {M} = asinh (inv (x))
497- Base. acoth (x:: StaticFloat64{M} ) where {M} = atanh (inv (x))
498- Base. asec (x:: StaticFloat64{M} ) where {M} = acos (inv (x))
499- Base. acsc (x:: StaticFloat64{M} ) where {M} = asin (inv (x))
500- Base. acot (x:: StaticFloat64{M} ) where {M} = atan (inv (x))
501-
502438@inline Base. exponent (:: StaticNumber{M} ) where {M} = static (exponent (M))
503439
504440Base.:(^ )(:: StaticFloat64{x} , y:: Float64 ) where {x} = exp2 (log2 (x) * y)
@@ -935,10 +871,12 @@ end
935871function Base. show (io:: IO , :: MIME"text/plain" ,
936872 @nospecialize (x:: Union{StaticNumber, StaticSymbol} ))
937873 print (io, " static(" * repr (known (typeof (x))) * " )" )
874+ nothing
938875end
939876function Base. show (io:: IO , m:: MIME"text/plain" , @nospecialize (x:: NDIndex ))
940877 print (io, " NDIndex" )
941878 show (io, m, Tuple (x))
879+ nothing
942880end
943881
944882end
0 commit comments