|
| 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license |
| 2 | + |
| 3 | +# Adapted from the TypeDomainNaturalNumbers.jl package. |
| 4 | +module _TypeDomainNumbers |
| 5 | + module Zeros |
| 6 | + export Zero |
| 7 | + struct Zero end |
| 8 | + end |
| 9 | + |
| 10 | + module PositiveIntegers |
| 11 | + module RecursiveStep |
| 12 | + using ...Zeros |
| 13 | + export recursive_step |
| 14 | + function recursive_step(@nospecialize t::Type) |
| 15 | + Union{Zero, t} |
| 16 | + end |
| 17 | + end |
| 18 | + module UpperBounds |
| 19 | + using ..RecursiveStep |
| 20 | + abstract type A end |
| 21 | + abstract type B{P <: recursive_step(A)} <: A end |
| 22 | + abstract type C{P <: recursive_step(B)} <: B{P} end |
| 23 | + abstract type D{P <: recursive_step(C)} <: C{P} end |
| 24 | + end |
| 25 | + using .RecursiveStep |
| 26 | + const PositiveIntegerUpperBound = UpperBounds.A |
| 27 | + const PositiveIntegerUpperBoundTighter = UpperBounds.D |
| 28 | + export |
| 29 | + natural_successor, natural_predecessor, |
| 30 | + NonnegativeInteger, NonnegativeIntegerUpperBound, |
| 31 | + PositiveInteger, PositiveIntegerUpperBound, |
| 32 | + type_assert_nonnegative_integer, type_assert_positive_integer |
| 33 | + struct PositiveInteger{ |
| 34 | + Predecessor <: recursive_step(PositiveIntegerUpperBoundTighter), |
| 35 | + } <: PositiveIntegerUpperBoundTighter{Predecessor} |
| 36 | + predecessor::Predecessor |
| 37 | + global const NonnegativeInteger = recursive_step(PositiveInteger) |
| 38 | + global const NonnegativeIntegerUpperBound = recursive_step(PositiveIntegerUpperBound) |
| 39 | + global function natural_successor(p::P) where {P <: NonnegativeInteger} |
| 40 | + ret = new{P}(p) |
| 41 | + type_assert_positive_integer(ret) |
| 42 | + end |
| 43 | + end |
| 44 | + function type_assert_nonnegative_integer(@nospecialize x::NonnegativeInteger) |
| 45 | + x |
| 46 | + end |
| 47 | + function type_assert_positive_integer(@nospecialize x::PositiveInteger) |
| 48 | + x |
| 49 | + end |
| 50 | + function natural_predecessor(@nospecialize o::PositiveInteger) |
| 51 | + ret = getfield(o, :predecessor) # avoid specializing `getproperty` for each number |
| 52 | + type_assert_nonnegative_integer(ret) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + module IntegersGreaterThanOne |
| 57 | + using ..PositiveIntegers |
| 58 | + export |
| 59 | + IntegerGreaterThanOne, IntegerGreaterThanOneUpperBound, |
| 60 | + type_assert_integer_greater_than_1 |
| 61 | + const IntegerGreaterThanOne = let t = PositiveInteger |
| 62 | + t{P} where {P <: t} |
| 63 | + end |
| 64 | + const IntegerGreaterThanOneUpperBound = let t = PositiveIntegerUpperBound |
| 65 | + PositiveIntegers.UpperBounds.B{P} where {P <: t} |
| 66 | + end |
| 67 | + function type_assert_integer_greater_than_1(@nospecialize x::IntegerGreaterThanOne) |
| 68 | + x |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + module Constants |
| 73 | + using ..Zeros, ..PositiveIntegers |
| 74 | + export n0, n1 |
| 75 | + const n0 = Zero() |
| 76 | + const n1 = natural_successor(n0) |
| 77 | + end |
| 78 | + |
| 79 | + module Utils |
| 80 | + using ..PositiveIntegers, ..IntegersGreaterThanOne, ..Constants |
| 81 | + using Base: @assume_effects |
| 82 | + export minus_two, half_floor, half_ceiling, half_floor_nontrivial, half_ceiling_nontrivial |
| 83 | + function minus_two(@nospecialize m::IntegerGreaterThanOne) |
| 84 | + natural_predecessor(natural_predecessor(m)) |
| 85 | + end |
| 86 | + @assume_effects :foldable :nothrow function half_floor(@nospecialize m::NonnegativeInteger) |
| 87 | + ret = if m isa IntegerGreaterThanOneUpperBound |
| 88 | + let n = minus_two(m), rec = half_floor(n) |
| 89 | + type_assert_positive_integer(natural_successor(rec)) |
| 90 | + end |
| 91 | + else |
| 92 | + n0 |
| 93 | + end |
| 94 | + type_assert_nonnegative_integer(ret) |
| 95 | + end |
| 96 | + @assume_effects :foldable :nothrow function half_ceiling(@nospecialize m::NonnegativeInteger) |
| 97 | + ret = if m isa IntegerGreaterThanOneUpperBound |
| 98 | + let n = minus_two(m), rec = half_ceiling(n) |
| 99 | + type_assert_positive_integer(natural_successor(rec)) |
| 100 | + end |
| 101 | + else |
| 102 | + if m isa PositiveIntegerUpperBound |
| 103 | + n1 |
| 104 | + else |
| 105 | + n0 |
| 106 | + end |
| 107 | + end |
| 108 | + type_assert_nonnegative_integer(ret) |
| 109 | + end |
| 110 | + function half_floor_nontrivial(@nospecialize m::IntegerGreaterThanOne) |
| 111 | + ret = half_floor(m) |
| 112 | + type_assert_positive_integer(ret) |
| 113 | + end |
| 114 | + function half_ceiling_nontrivial(@nospecialize m::IntegerGreaterThanOne) |
| 115 | + ret = half_ceiling(m) |
| 116 | + type_assert_positive_integer(ret) |
| 117 | + end |
| 118 | + end |
| 119 | +end |
| 120 | + |
| 121 | +module _TupleTypeByLength |
| 122 | + export |
| 123 | + Tuple1OrMore, Tuple2OrMore, Tuple3OrMore, Tuple4OrMore, Tuple32OrMore, |
| 124 | + type_assert_tuple_0_or_more, type_assert_tuple_1_or_more, type_assert_tuple_2_or_more, |
| 125 | + type_assert_tuple_3_or_more, type_assert_tuple_4_or_more, |
| 126 | + type_assert_tuple_1 |
| 127 | + const Tuple1OrMore = Tuple{Any, Vararg} |
| 128 | + const Tuple2OrMore = Tuple{Any, Any, Vararg} |
| 129 | + const Tuple3OrMore = Tuple{Any, Any, Any, Vararg} |
| 130 | + const Tuple4OrMore = Tuple{Any, Any, Any, Any, Vararg} |
| 131 | + const Tuple32OrMore = Base.Any32 |
| 132 | + function type_assert_tuple_0_or_more(@nospecialize x::Tuple) |
| 133 | + x |
| 134 | + end |
| 135 | + function type_assert_tuple_1_or_more(@nospecialize x::Tuple1OrMore) |
| 136 | + x |
| 137 | + end |
| 138 | + function type_assert_tuple_2_or_more(@nospecialize x::Tuple2OrMore) |
| 139 | + x |
| 140 | + end |
| 141 | + function type_assert_tuple_3_or_more(@nospecialize x::Tuple3OrMore) |
| 142 | + x |
| 143 | + end |
| 144 | + function type_assert_tuple_4_or_more(@nospecialize x::Tuple4OrMore) |
| 145 | + x |
| 146 | + end |
| 147 | +end |
| 148 | + |
| 149 | +module _TypeDomainNumberTupleUtils |
| 150 | + using |
| 151 | + .._TypeDomainNumbers.PositiveIntegers, .._TypeDomainNumbers.IntegersGreaterThanOne, |
| 152 | + .._TypeDomainNumbers.Constants, .._TupleTypeByLength |
| 153 | + using Base: @assume_effects, front, tail |
| 154 | + export |
| 155 | + tuple_type_domain_length, |
| 156 | + skip_from_front, skip_from_tail, |
| 157 | + skip_from_front_nontrivial, skip_from_tail_nontrivial |
| 158 | + @assume_effects :foldable :nothrow function tuple_type_domain_length(@nospecialize tup::Tuple) |
| 159 | + ret = if tup isa Tuple1OrMore |
| 160 | + let t = tail(tup), rec = tuple_type_domain_length(t) |
| 161 | + type_assert_positive_integer(natural_successor(rec)) |
| 162 | + end |
| 163 | + else |
| 164 | + n0 |
| 165 | + end |
| 166 | + type_assert_nonnegative_integer(ret) |
| 167 | + end |
| 168 | + @assume_effects :foldable function skip_from_front((@nospecialize tup::Tuple), @nospecialize skip_count::NonnegativeInteger) |
| 169 | + if skip_count isa PositiveIntegerUpperBound |
| 170 | + let cm1 = natural_predecessor(skip_count), t = tail(tup) |
| 171 | + @inline skip_from_front(t, cm1) |
| 172 | + end |
| 173 | + else |
| 174 | + tup |
| 175 | + end |
| 176 | + end |
| 177 | + @assume_effects :foldable function skip_from_tail((@nospecialize tup::Tuple), @nospecialize skip_count::NonnegativeInteger) |
| 178 | + if skip_count isa PositiveIntegerUpperBound |
| 179 | + let cm1 = natural_predecessor(skip_count), t = front(tup) |
| 180 | + @inline skip_from_tail(t, cm1) |
| 181 | + end |
| 182 | + else |
| 183 | + tup |
| 184 | + end |
| 185 | + end |
| 186 | + function skip_from_front_nontrivial((@nospecialize tup::Tuple2OrMore), @nospecialize skip_count::PositiveInteger) |
| 187 | + skip_from_front(tup, skip_count) |
| 188 | + end |
| 189 | + function skip_from_tail_nontrivial((@nospecialize tup::Tuple2OrMore), @nospecialize skip_count::PositiveInteger) |
| 190 | + skip_from_tail(tup, skip_count) |
| 191 | + end |
| 192 | +end |
0 commit comments