# `ReductionBounds.IntegerSize`
[🔗](https://github.com/zacky1972/reduction_bounds/blob/v0.1.0/lib/reduction_bounds/integer_size.ex#L1)

Represents integer size information, including an unreachable state.

The lower and upper bounds describe the same integer as the magnitude
bit-length bound. A finite lower bound `lower` means `lower <= value`,
and a finite upper bound `upper` means `value <= upper`.

A magnitude bit-length bound `bits` means `abs(value) < 2^bits`. It
measures the magnitude independently of the sign, so a bound of zero
describes only the integer zero. For example, values from -8 through 7
have a magnitude bit-length bound of 4.

Each field can be supplied independently. The default value represents
any integer, with both endpoints unbounded and its magnitude bit-length
bound unknown.

The mathematical meaning is the set of integers satisfying all supplied
constraints simultaneously. Contradictory constraints denote the empty
set. `valid?/1` checks field types, and `contains?/2` tests membership.
These definitions use mathematical integers and do not assume any
particular runtime representation.

`unknown/0` returns the default struct, which admits every integer.
`unreachable/0` returns `:unreachable`, which admits no integer.
`normalize/1` converts contradictory constraints to `:unreachable`
and preserves every satisfiable struct unchanged. Unknown information
must not be used to represent an impossible branch. This domain does
not by itself establish whether a program branch can execute.

`from_integer/1` describes a constant. `add/2`, `subtract/2`, and
`multiply/2` propagate size information for independently chosen operand
values. They validate and normalize both operands, propagate unreachable
states, and return conservative descriptions of the results. They are
reusable domain operations. `ReductionBounds.infer_integer_sizes/2`
applies them to a supported Core Erlang fragment.
`ReductionBounds.infer_integer_cost_bound/2` connects these descriptions
to symbolic size-dependent costs for its supported fragment.

`shift_left/2`, `shift_right/2`, and `mask/2` handle constant
nonnegative shift amounts and mask widths. `bit_xor/2` covers signed
integer XOR. They preserve intermediate-size descriptions: applying
a mask does not change a description already obtained for its input.

`assume/4` narrows a description using a comparison with a constant.
`join/2` combines alternative results, retaining bounds that cover both
alternatives. Unreachable information is the identity of this join.

## Examples

    iex> alias ReductionBounds.IntegerSize
    iex> size = %IntegerSize{lower: -8, upper: 7, magnitude_bits: 4}
    iex> {size.lower, size.upper, size.magnitude_bits}
    {-8, 7, 4}

    iex> alias ReductionBounds.IntegerSize
    iex> size = %IntegerSize{lower: 0}
    iex> {size.lower, size.upper, size.magnitude_bits}
    {0, :unbounded, :unknown}

# `bound`

```elixir
@type bound() :: integer() | :unbounded
```

An inclusive integer endpoint, or an absent bound.

# `constraints`

```elixir
@type constraints() :: %ReductionBounds.IntegerSize{
  lower: bound(),
  magnitude_bits: magnitude_bits(),
  upper: bound()
}
```

Value-range and magnitude constraints before normalization.

# `magnitude_bits`

```elixir
@type magnitude_bits() :: non_neg_integer() | :unknown
```

An upper bound on magnitude bit length, or unknown information.

# `t`

```elixir
@type t() :: constraints() | :unreachable
```

Integer constraints, or an explicit state admitting no integer.

# `add`

```elixir
@spec add(t(), t()) :: t()
```

Bounds addition for every pair of values in the operand descriptions.

Finite lower bounds are added, as are finite upper bounds. If both
magnitude bounds are known, the result uses `max(left_bits, right_bits) + 1`.
An unknown component stays unknown when the rule cannot bound it.
An unreachable operand makes the result unreachable.

    iex> alias ReductionBounds.IntegerSize
    iex> IntegerSize.add(IntegerSize.from_integer(7), IntegerSize.from_integer(1))
    %IntegerSize{lower: 8, upper: 8, magnitude_bits: 4}

# `assume`

```elixir
@spec assume(t(), :lt | :le | :eq | :ne | :ge | :gt, integer(), boolean()) :: t()
```

Narrows a description under a comparison with an integer constant.

Comparisons are :lt, :le, :eq, :ne, :ge, and :gt. The Boolean selects
the true or false branch. A comparison excluding one value leaves the description unchanged
because this domain cannot represent an interval with one value removed.
Contradictions become :unreachable.

# `bit_xor`

```elixir
@spec bit_xor(t(), t()) :: t()
```

Bounds signed XOR. Known operand widths give max(left_bits, right_bits) + 1; ranges are unbounded.

# `contains?`

```elixir
@spec contains?(t(), integer()) :: boolean()
```

Returns whether an integer satisfies all supplied constraints.

Endpoints are inclusive; the magnitude limit is strict. Unspecified
fields impose no constraint. Contradictory constraints represent the
empty set and never contain a value. The explicit `:unreachable`
state also returns false for every integer.

Raises `ArgumentError` for a malformed representation or a non-integer
value. This function does not infer or normalize bounds.

## Examples

    iex> alias ReductionBounds.IntegerSize
    iex> size = %IntegerSize{lower: -8, upper: 7, magnitude_bits: 3}
    iex> Enum.map([-8, -7, 0, 7, 8], &IntegerSize.contains?(size, &1))
    [false, true, true, true, false]

    iex> ReductionBounds.IntegerSize.contains?(%ReductionBounds.IntegerSize{}, -123)
    true

# `from_integer`

```elixir
@spec from_integer(integer()) :: constraints()
```

Returns exact range and magnitude information for an integer constant.

    iex> ReductionBounds.IntegerSize.from_integer(-8)
    %ReductionBounds.IntegerSize{lower: -8, upper: -8, magnitude_bits: 4}
    iex> ReductionBounds.IntegerSize.from_integer(0)
    %ReductionBounds.IntegerSize{lower: 0, upper: 0, magnitude_bits: 0}

# `join`

```elixir
@spec join(t(), t()) :: t()
```

Returns a description containing both operand sets. Unreachable information is the identity.

# `mask`

```elixir
@spec mask(t(), non_neg_integer()) :: t()
```

Bounds retaining the low `width` bits of any signed integer.

The value operation is `Integer.mod(value, 2^width)`, the nonnegative
residue that represents the low bits of an integer in two's complement.
It corresponds to masking with `2^width - 1`. The general result has
range `0..(2^width - 1)` and magnitude bound `width`. This first rule
discards other input range information, except for known zero.
A width of zero also gives exact zero. Unreachable inputs remain
unreachable. The width must be a nonnegative integer constant.

    iex> alias ReductionBounds.IntegerSize
    iex> IntegerSize.mask(IntegerSize.from_integer(-1), 4)
    %IntegerSize{lower: 0, upper: 15, magnitude_bits: 4}
    iex> IntegerSize.mask(IntegerSize.unknown(), 0)
    %IntegerSize{lower: 0, upper: 0, magnitude_bits: 0}

# `multiply`

```elixir
@spec multiply(t(), t()) :: t()
```

Bounds multiplication for every pair of values in the operand descriptions.

When all endpoints are finite, the range uses the minimum and maximum
of the four endpoint products. Otherwise both range endpoints are
unbounded. Known magnitude bounds are added. A known zero operand
yields exact zero unless an operand is unreachable.

This rule is sound but may lose information for one-sided ranges.
These functions do not assign costs or model a runtime integer format.

    iex> alias ReductionBounds.IntegerSize
    iex> IntegerSize.multiply(IntegerSize.from_integer(-8), IntegerSize.from_integer(2))
    %IntegerSize{lower: -16, upper: -16, magnitude_bits: 6}

# `normalize`

```elixir
@spec normalize(t()) :: t()
```

Converts constraints admitting no integer to `:unreachable`.

All satisfiable structs are returned unchanged. The check tests the
interval's point nearest zero against all constraints. If any integer
satisfies them, that point does too. No enumeration is required.

Raises `ArgumentError` for malformed information.

    iex> alias ReductionBounds.IntegerSize
    iex> IntegerSize.normalize(IntegerSize.unknown())
    %IntegerSize{}
    iex> IntegerSize.normalize(%IntegerSize{lower: 8, magnitude_bits: 3})
    :unreachable
    iex> IntegerSize.normalize(%IntegerSize{lower: 0, magnitude_bits: 0})
    %IntegerSize{lower: 0, magnitude_bits: 0}

# `shift_left`

```elixir
@spec shift_left(t(), non_neg_integer()) :: t()
```

Bounds a left shift by a fixed nonnegative number of bits.

Finite endpoints are multiplied by `2^amount`, including negative
endpoints. A known magnitude bound grows by `amount`. Known zero
stays exact zero. The shift amount must be an integer constant at
analysis time; negative amounts are rejected rather than interpreted
as right shifts.

    iex> alias ReductionBounds.IntegerSize
    iex> IntegerSize.shift_left(IntegerSize.from_integer(-3), 2)
    %IntegerSize{lower: -12, upper: -12, magnitude_bits: 4}

# `shift_right`

```elixir
@spec shift_right(t(), non_neg_integer()) :: t()
```

Bounds arithmetic right shift by a nonnegative constant; known magnitude bounds do not grow.

# `subtract`

```elixir
@spec subtract(t(), t()) :: t()
```

Bounds subtraction for every pair of values in the operand descriptions.

The lower endpoint is `left.lower - right.upper`, and the upper endpoint
is `left.upper - right.lower`, when the required endpoints are finite.
Known magnitude bounds use `max(left_bits, right_bits) + 1`.
Reusing the same description on both sides does not imply that the
concrete operands are equal; this domain does not track correlations.

    iex> alias ReductionBounds.IntegerSize
    iex> IntegerSize.subtract(IntegerSize.from_integer(8), IntegerSize.from_integer(8))
    %IntegerSize{lower: 0, upper: 0, magnitude_bits: 5}

# `unknown`

```elixir
@spec unknown() :: constraints()
```

Returns unconstrained information, which admits every integer.

    iex> ReductionBounds.IntegerSize.unknown()
    %ReductionBounds.IntegerSize{}

# `unreachable`

```elixir
@spec unreachable() :: :unreachable
```

Returns the state admitting no integer.

    iex> ReductionBounds.IntegerSize.unreachable()
    :unreachable

# `unreachable?`

```elixir
@spec unreachable?(t()) :: boolean()
```

Returns whether the information admits no integer.

This checks both the explicit state and contradictory constraints.
Unknown information returns false. Program reachability requires
separate evidence connecting these constraints to a program state.

    iex> alias ReductionBounds.IntegerSize
    iex> IntegerSize.unreachable?(IntegerSize.unknown())
    false
    iex> IntegerSize.unreachable?(%IntegerSize{lower: 2, upper: 1})
    true

# `valid?`

```elixir
@spec valid?(term()) :: boolean()
```

Returns whether the information has a valid representation.

This checks the representation, not whether its constraints admit an
integer. The explicit `:unreachable` state is valid. For example,
`lower: 2, upper: 1` is a valid representation of the empty set.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
