# Interval

The `Interval` type. An `Interval` is a
half-open `[start, end)` range over either two `Time` values or
two `Date` values; the operand type determines the interval
type.

Paired with:
- [LTIM] for the `Time`, `Date`, and `Duration`
  types and their semantics.
- [LSTR] for the JSON wire-format conventions.
- [LEXP] for the broader expression
  surface (`in`, `overlaps`, `contains`, `..`).

## What this is not

- Not a general range type over integers or floats. `Interval`
  is `Time`-or-`Date` only. Numeric ranges remain expressed by
  comparison.
- Not a closed-interval type. `start .. end` always means
  `[start, end)`. Authors who need the closed form add `1us`
  (Time) or `1_d` (Date).
- Not a set-operations library. Union, intersection, and
  difference on intervals are deferred per [LINT.10.3].

---

(LINT-1)=
## The `Interval` type [LINT.1]

(LINT-1-1)=
[LINT.1.1] An `Interval` value is a half-open range
`[start, end)` over one element type `T`:

```
Interval<T>: (start: T, end: T)
```

`T` is one of the types whose values `<` orders, and no other: `Int` and
`Float` ([LEXP.7.3]), `String` ([LSTR.3.2]), `Time`, `Date` and `Duration`
([LTIM.5]). `Interval<Time>` and `Interval<Date>` are the time windows
the rest of this document mostly speaks of; every rule here that does not name
an element type holds for all of them.

*ratified*  —  *checks:* `alloy×2`  —  *see also:* [LEXP.7.3](#LEXP-7-3), [LSTR.3.2](#LSTR-3-2), [LTIM.5](#LTIM-5)

(LINT-1-2)=
[LINT.1.2] The element type is the type the operands of `..` share
([LINT.2]). Intervals of different element types are distinct types; the
compiler must reject mixing them in operations that take two intervals. An
`Int` and a `Float` are one numeric element type, compared as [LEXP.7.3]
compares them.

*see also:* [LEXP.7.3](#LEXP-7-3), [LINT.2](#LINT-2)

(LINT-1-3)=
[LINT.1.3] An `Interval<Time>` carries a timezone offset on
each endpoint; the offsets need not match. Operations that
read or compare endpoints follow the rules in [LTIM.5] and
[LTIM.6].

---

*see also:* [LTIM.5](#LTIM-5), [LTIM.6](#LTIM-6)

(LINT-1-4)=
[LINT.1.4] **An `Interval` is not a slot type.** It is a value of the
expression language with no slot representation, so the compiler **must**
refuse a schema slot declared `Interval` or `Interval<T>` for any `T` with
`DS_VALIDATE_BAD_TYPE`. A fact carries an interval as its two endpoints, or, for
an `Interval<Time>` or `Interval<Date>`, as a `Json` string in the form
[LINT.8.1] gives.

*ratified*  —  *see also:* [LINT.8.1](#LINT-8-1)

(LINT-2)=
## Construction [LINT.2]

(LINT-2-1)=
[LINT.2.1] The `..` operator constructs an `Interval` from two
operands: `a .. b` is the `Interval<T>` whose `start` is `a` and whose `end`
is `b`, `T` being the operands' one element type ([LINT.1.1], [LINT.2.2]).

*see also:* [LINT.1.1](#LINT-1-1), [LINT.2.2](#LINT-2-2)

(LINT-2-2)=
[LINT.2.2] Both operands must have one element type ([LINT.1.1]); an `Int`
and a `Float` have one ([LINT.1.2]). The compiler must refuse:

- operands of two element types, such as `some_date .. some_time` or an `Int`
  with a `String`, with `DS_VALIDATE_INTERVAL_MIXED_OPERANDS`;
- an operand of any type [LINT.1.1] does not list, with
  `DS_VALIDATE_UNORDERED_TYPE`: among them a `Bool`, a `Bytes` value, a whole
  `Json` value, a fact reference ([LEXP.7.19], [LEXP.7.20]), a value of an
  opaque type, whose `<` is sealed ([LOPQ.3.3]), and an interval.

An operand whose type is not known statically (a `Json` path read) is judged
by the ordinary type rules when the interval is used. The operands' dimensions
are [LUNI.8.8]'s, and a `Duration` beside a bare number is refused there.

*ratified*  —  *see also:* [LEXP.7.19](#LEXP-7-19), [LEXP.7.20](#LEXP-7-20), [LINT.1.1](#LINT-1-1), [LINT.1.2](#LINT-1-2), [LOPQ.3.3](#LOPQ-3-3), [LUNI.8.8](#LUNI-8-8)

(LINT-2-3)=
[LINT.2.3] The result is always half-open. An interval that includes `end` is written
with its `end` one step later: one nanosecond for a `Time` or a `Duration`,
one day for a `Date`, one for an `Int`. A `Float` or `String` interval has no
next value, so it cannot include its `end` ([LINT.1.1]).

*see also:* [LINT.1.1](#LINT-1-1)

(LINT-2-4)=
[LINT.2.4] `..` binds tighter than logical operators and looser
than additive arithmetic. `m.fired_at .. m.fired_at + 1_h`
parses as `m.fired_at .. (m.fired_at + 1_h)`.

---

(LINT-3)=
## Containment [LINT.3]

(LINT-3-1)=
### `t in i` (point in interval) [LINT.3.1]

(LINT-3-1-1)=
[LINT.3.1.1] `t in i` is `Bool`. For an `Interval<T> i` and a `t` of its
element type `T` ([LINT.1.1]):

```
t in i  iff  i.start <= t < i.end
```

*ratified*  —  *checks:* `alloy`  —  *see also:* [LINT.1.1](#LINT-1-1)

(LINT-3-1-3)=
[LINT.3.1.3] The point and interval must have matching element
type. `Time in Interval<Date>` is a compile error
(`DS_VALIDATE_TYPE_MISMATCH`); `Date in Interval<Time>` likewise. Their dimensions are [LUNI.8.8]'s.

*ratified*  —  *see also:* [LUNI.8.8](#LUNI-8-8)

(LINT-3-2)=
### `i1 contains i2` (interval in interval) [LINT.3.2]

(LINT-3-2-1)=
[LINT.3.2.1] `i1 contains i2` is `Bool` and true iff `i1`
fully covers `i2`:

```
i1 contains i2  iff  i1.start <= i2.start and i2.end <= i1.end
```

*checks:* `alloy×3`

(LINT-3-2-2)=
[LINT.3.2.2] Both intervals must have matching element type.
The compiler must reject mixed-type containment (`DS_VALIDATE_TYPE_MISMATCH`).

*ratified*

(LINT-3-2-3)=
[LINT.3.2.3] An empty interval `i2` (per [LINT.6]) is
contained by any `i1` whose range includes `i2.start`.
`(a..a) contains (a..a)` is true because both
inequalities reduce to `a <= a`.

---

*checks:* `alloy`  —  *see also:* [LINT.6](#LINT-6)

(LINT-4)=
## Overlap [LINT.4]

(LINT-4-1)=
[LINT.4.1] `i1 overlaps i2` is `Bool` and true iff the two
intervals' intersection is non-empty. The intersection of two
half-open intervals is itself half-open, running from the later
start to the earlier end, so the test is that one precedes the
other:

```
i1 overlaps i2  iff  max(i1.start, i2.start) < min(i1.end, i2.end)
```

**This is the definition rather than a consequence of it**, which is
why it is stated over the intersection bounds instead of as a pair
of cross-interval comparisons. An implementation may evaluate the
equivalent conjunction

```
i1.start < i1.end  and  i2.start < i2.end          # both non-empty
    and  i1.start < i2.end  and  i2.start < i1.end
```

but the two non-emptiness conjuncts are **not optional**: without
them the form admits an empty interval lying strictly inside a
non-empty one, which has no instant in common with it
([LINT.7.1]) and therefore does not overlap it.

*ratified*  —  *checks:* `alloy×2`  —  *see also:* [LINT.7.1](#LINT-7-1)

(LINT-4-2)=
[LINT.4.2] Both intervals must have matching element type. The
compiler must reject mixed-type overlap tests (`DS_VALIDATE_TYPE_MISMATCH`).

*ratified*

(LINT-4-3)=
[LINT.4.3] Adjacent intervals (`i1.end == i2.start`) do not
overlap. The half-open semantics make adjacent ranges tile
without overlap by construction: the interval of one hour and the interval of
the next share an endpoint and no instant.

*checks:* `alloy`

(LINT-4-4)=
[LINT.4.4] An empty interval (per [LINT.6]) overlaps no
interval, including another empty one. An empty interval has
`start == end`, so `min(i1.end, i2.end) <= i.end` and
`i.start <= max(i1.start, i2.start)` — the earlier end is at or
before the later start, and [LINT.4.1]'s strict inequality is
false.

This is a **consequence** of [LINT.4.1] and not a separate rule.
It follows directly from [LINT.7.1]: an empty interval admits no
`t`, so it shares no instant with anything, so no intersection
with it is non-empty.

*ratified*  —  *checks:* `alloy`  —  *see also:* [LINT.4.1](#LINT-4-1), [LINT.6](#LINT-6), [LINT.7.1](#LINT-7-1)

(LINT-4-5)=
[LINT.4.5] **Interval relational operators require Interval
operands.** Where an operand is a non-Interval *literal* —
`5 contains 3` — it is a compile error
(`DS_VALIDATE_INTERVAL_REL_NON_INTERVAL`). An operand whose
type is not statically known (a `Json` value, a bound slot) is
not rejected here; it resolves under the ordinary type rules.

---

*ratified*

(LINT-5)=
## Equality [LINT.5]

(LINT-5-1)=
[LINT.5.1] `i1 == i2` is `Bool` and true iff `i1.start ==
i2.start and i1.end == i2.end` under the equality rules of the
element type per [LTIM.5].

*see also:* [LTIM.5](#LTIM-5)

(LINT-5-2)=
[LINT.5.2] `Interval<Time>` equality compares endpoints on
`utc_ns`; `tz_offset_ns` does not affect equality. Two intervals
with the same instants but different declared timezones are
equal.

(LINT-5-3)=
[LINT.5.3] `i1 != i2` is the negation. No ordering operators
(`<`, `>`, `<=`, `>=`) are defined on intervals; intervals do
not admit a canonical total order. Authors who need to
order intervals write `a.start < b.start` explicitly.

*ratified*

(LINT-5-4)=
[LINT.5.4] Cross-type equality (`Interval<Time> == Interval<Date>`)
is a compile error (`DS_VALIDATE_TYPE_MISMATCH`).

---

*ratified*

(LINT-6)=
## Extraction [LINT.6]

(LINT-6-1)=
[LINT.6.1] An `Interval<T>` value has these readable components:

| Component | Type | Meaning |
|---|---|---|
| `i.start` | `T` | Inclusive start endpoint |
| `i.end`   | `T` | Exclusive end endpoint |
| `i.duration` | `Duration` | `i.end - i.start`, for `T` `Time` or `Date` only |

The compiler must refuse any other component, and `i.duration` on an interval
whose element type is neither `Time` nor `Date`, with
`DS_VALIDATE_BAD_INTERVAL_COMPONENT`.

*ratified*

(LINT-6-2)=
[LINT.6.2] `i.duration` is a `Duration` for both element types it is
defined on ([LINT.6.1]). For `Interval<Date>`, the duration is a whole-day
multiple. For `Interval<Time>`, the duration matches the nanosecond difference
of the endpoints.

*ratified*  —  *see also:* [LINT.6.1](#LINT-6-1)

(LINT-6-3)=
[LINT.6.3] Endpoint extraction follows the rules of the
element type. Calling `.hour` on `i.start` (an
`Interval<Time>` endpoint) requires the same `.utc` /
`.local(tz)` context that `.hour` requires on a bare `Time` per
[LTIM.6.2].

---

*see also:* [LTIM.6.2](#LTIM-6-2)

(LINT-7)=
## Empty and invalid intervals [LINT.7]

(LINT-7-1)=
[LINT.7.1] An interval with `start == end` is **empty**. Empty
intervals are legal. They have zero duration and no
containment surface (per [LINT.3.1.1]: `t < i.end` is
`t < i.start`, which combined with `i.start <= t` admits no
`t`).

*ratified*  —  *checks:* `alloy`  —  *see also:* [LINT.3.1.1](#LINT-3-1-1)

(LINT-7-2)=
[LINT.7.2] **An interval is valid only when `start <= end`** under its element
type's `<=` ([LINT.1.1]). Every other interval is **invalid**: one whose
`end < start`, and one with a NaN endpoint, for which `start <= end` is false
([LEXP.4.2]). A `null` endpoint does not make an interval invalid.

When the engine evaluates `start .. end` and the endpoints are two values, neither
`null`, that are not `start <= end`, it **must** raise a `ComputationError` of
kind `DOMAIN` ([LERR.1.4.10]). The fault is the `..` operation's own, so every
operation on the interval — `in`, `contains`, `overlaps`, `==`, `!=` and a
component read ([LINT.3] to [LINT.6]) — yields it whatever its other operand is,
`null` included ([LNUL.4.10]), and the engine handles it as it handles every
`ComputationError` ([LERR.1.4.7]). A `let` bound to an interval is evaluated
where it is used ([LLET.10.3]), so an invalid interval that no operation uses
raises nothing.

*ratified*  —  *see also:* [LERR.1.4.10](#LERR-1-4-10), [LERR.1.4.7](#LERR-1-4-7), [LEXP.4.2](#LEXP-4-2), [LINT.1.1](#LINT-1-1), [LINT.3](#LINT-3), [LINT.6](#LINT-6), [LLET.10.3](#LLET-10-3), [LNUL.4.10](#LNUL-4-10)

(LINT-7-3)=
[LINT.7.3] Invalid-interval rejection is hard-fail, parallel
to the timezone-resolution policy in [LTIM.10.5]. A misordered
endpoint indicates programmer error and must be visible.

*see also:* [LTIM.10.5](#LTIM-10-5)

(LINT-7-4)=
[LINT.7.4] Invalid-interval rejection at compile time is
required when both endpoints are statically determinable
literals. Computed-endpoint cases are runtime errors.

---

*ratified*

(LINT-8)=
## Wire format [LINT.8]

(LINT-8-1)=
[LINT.8.1] JSON-typed slots delivering an `Interval<Time>` or
`Interval<Date>` use the ISO 8601 interval form: a single string with the start
and end joined by `/`. No other interval has a wire form; a fact carries one as
its two endpoints ([LINT.1.4]).

| Type | JSON form |
|---|---|
| `Interval<Time>` | `"2024-01-15T08:00:00-08:00/2024-01-15T12:00:00-08:00"` |
| `Interval<Date>` | `"2024-01-15/2024-01-22"` |

*see also:* [LINT.1.4](#LINT-1-4)

(LINT-8-2)=
[LINT.8.2] Adapters delivering different forms must convert at
the wire boundary. The engine validates wire form on
ingestion; malformed inputs surface as adapter errors per
[LECC.3.1.2].

*see also:* [LECC.3.1.2](#LECC-3-1-2)

(LINT-8-3)=
[LINT.8.3] Round-trip: an `Interval` value emitted to JSON and
re-parsed must produce a value equal to the original.
