|
| 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 | + struct PositiveInteger{ |
| 33 | + Predecessor <: recursive_step(PositiveIntegerUpperBoundTighter), |
| 34 | + } <: PositiveIntegerUpperBoundTighter{Predecessor} |
| 35 | + predecessor::Predecessor |
| 36 | + global const NonnegativeInteger = recursive_step(PositiveInteger) |
| 37 | + global const NonnegativeIntegerUpperBound = recursive_step(PositiveIntegerUpperBound) |
| 38 | + global function natural_successor(p::P) where {P <: NonnegativeInteger} |
| 39 | + new{P}(p) |
| 40 | + end |
| 41 | + end |
| 42 | + function natural_predecessor(@nospecialize o::PositiveInteger) |
| 43 | + getfield(o, :predecessor) # avoid specializing `getproperty` for each number |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + module IntegersGreaterThanOne |
| 48 | + using ..PositiveIntegers |
| 49 | + export |
| 50 | + IntegerGreaterThanOne, IntegerGreaterThanOneUpperBound, |
| 51 | + natural_predecessor_predecessor |
| 52 | + const IntegerGreaterThanOne = let t = PositiveInteger |
| 53 | + t{P} where {P <: t} |
| 54 | + end |
| 55 | + const IntegerGreaterThanOneUpperBound = let t = PositiveIntegerUpperBound |
| 56 | + PositiveIntegers.UpperBounds.B{P} where {P <: t} |
| 57 | + end |
| 58 | + function natural_predecessor_predecessor(@nospecialize x::IntegerGreaterThanOne) |
| 59 | + natural_predecessor(natural_predecessor(x)) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + module Constants |
| 64 | + using ..Zeros, ..PositiveIntegers |
| 65 | + export n0, n1 |
| 66 | + const n0 = Zero() |
| 67 | + const n1 = natural_successor(n0) |
| 68 | + end |
| 69 | + |
| 70 | + module Utils |
| 71 | + using ..PositiveIntegers, ..IntegersGreaterThanOne, ..Constants |
| 72 | + using Base: @assume_effects |
| 73 | + export half_floor, half_ceiling, half_floor_nontrivial, half_ceiling_nontrivial |
| 74 | + @assume_effects :foldable :nothrow function half_floor(@nospecialize m::NonnegativeInteger) |
| 75 | + if m isa IntegerGreaterThanOneUpperBound |
| 76 | + let n = natural_predecessor_predecessor(m), rec = half_floor(n) |
| 77 | + natural_successor(rec) |
| 78 | + end |
| 79 | + else |
| 80 | + n0 |
| 81 | + end |
| 82 | + end |
| 83 | + @assume_effects :foldable :nothrow function half_ceiling(@nospecialize m::NonnegativeInteger) |
| 84 | + if m isa IntegerGreaterThanOneUpperBound |
| 85 | + let n = natural_predecessor_predecessor(m), rec = half_ceiling(n) |
| 86 | + natural_successor(rec) |
| 87 | + end |
| 88 | + else |
| 89 | + if m isa PositiveIntegerUpperBound |
| 90 | + n1 |
| 91 | + else |
| 92 | + n0 |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + function half_floor_nontrivial(@nospecialize m::IntegerGreaterThanOne) |
| 97 | + half_floor(m) |
| 98 | + end |
| 99 | + function half_ceiling_nontrivial(@nospecialize m::IntegerGreaterThanOne) |
| 100 | + half_ceiling(m) |
| 101 | + end |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +module _TupleTypeByLength |
| 106 | + export Tuple1OrMore, Tuple2OrMore, Tuple3OrMore, Tuple4OrMore, Tuple32OrMore |
| 107 | + const Tuple1OrMore = Tuple{Any, Vararg} |
| 108 | + const Tuple2OrMore = Tuple{Any, Any, Vararg} |
| 109 | + const Tuple3OrMore = Tuple{Any, Any, Any, Vararg} |
| 110 | + const Tuple4OrMore = Tuple{Any, Any, Any, Any, Vararg} |
| 111 | + const Tuple32OrMore = Base.Any32 |
| 112 | +end |
| 113 | + |
| 114 | +module _TypeDomainNumberTupleUtils |
| 115 | + using |
| 116 | + .._TypeDomainNumbers.PositiveIntegers, .._TypeDomainNumbers.IntegersGreaterThanOne, |
| 117 | + .._TypeDomainNumbers.Constants, .._TupleTypeByLength |
| 118 | + using Base: @assume_effects, front, tail |
| 119 | + export |
| 120 | + tuple_type_domain_length, |
| 121 | + skip_from_front, skip_from_tail, |
| 122 | + skip_from_front_nontrivial, skip_from_tail_nontrivial |
| 123 | + @assume_effects :foldable :nothrow function tuple_type_domain_length(@nospecialize tup::Tuple) |
| 124 | + if tup isa Tuple1OrMore |
| 125 | + let t = tail(tup), rec = tuple_type_domain_length(t) |
| 126 | + natural_successor(rec) |
| 127 | + end |
| 128 | + else |
| 129 | + n0 |
| 130 | + end |
| 131 | + end |
| 132 | + @assume_effects :foldable function skip_from_front((@nospecialize tup::Tuple), @nospecialize skip_count::NonnegativeInteger) |
| 133 | + if skip_count isa PositiveIntegerUpperBound |
| 134 | + let cm1 = natural_predecessor(skip_count), t = tail(tup) |
| 135 | + @inline skip_from_front(t, cm1) |
| 136 | + end |
| 137 | + else |
| 138 | + tup |
| 139 | + end |
| 140 | + end |
| 141 | + @assume_effects :foldable function skip_from_tail((@nospecialize tup::Tuple), @nospecialize skip_count::NonnegativeInteger) |
| 142 | + if skip_count isa PositiveIntegerUpperBound |
| 143 | + let cm1 = natural_predecessor(skip_count), t = front(tup) |
| 144 | + @inline skip_from_tail(t, cm1) |
| 145 | + end |
| 146 | + else |
| 147 | + tup |
| 148 | + end |
| 149 | + end |
| 150 | + function skip_from_front_nontrivial((@nospecialize tup::Tuple2OrMore), @nospecialize skip_count::PositiveInteger) |
| 151 | + skip_from_front(tup, skip_count) |
| 152 | + end |
| 153 | + function skip_from_tail_nontrivial((@nospecialize tup::Tuple2OrMore), @nospecialize skip_count::PositiveInteger) |
| 154 | + skip_from_tail(tup, skip_count) |
| 155 | + end |
| 156 | +end |
0 commit comments