{- | The 1-dimensional Lebesgue measure ring.
-
- It is generated by finite lists of intervals, identified when they contain the same rational points.
- Everything is computed on a common refinement obtained by sorting the endpoints:
- a sorted list `t₀ ≤ … ≤ t_k` containing all endpoints cuts the line into cells `[tⱼ,tⱼ₊₁)`,
- and every interval is either full or empty on each cell.
- The ring operations take the cells on which the boolean combination holds,
- and the measure is `∑ⱼ (tⱼ₊₁ - tⱼ)` over the cells in the set.
- Inserting a point splits a cell into two with the same total length, so the measure does not depend on the refinement.
-
- Points are rational: over the reals, finite unions of intervals do not form a Boolean ring constructively.
-}
\import Algebra.Group
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Ring.Boolean
\import Analysis.Measure.MeasureRing
\import Arith.Rat
\import Arith.Real
\import Data.Bool
\import Data.List (++, ++_nil, InList, InList_++-left, InList_++-right, List, ListSub, Sort)
\import Data.Or
\import Function.Meta
\import Logic
\import Logic.Meta
\import Meta
\import Order.Biordered
\import Order.Lattice
\import Order.LinearOrder
\import Order.PartialOrder
\import Order.StrictOrder
\import Paths
\import Paths.Meta
\import Relation.Equivalence
\import Set
\open Sort
\open Sort.Insertion
\open Sort.Insertion.sort
\open InList (here, there)
-- | The decidable order `x < y` of rational numbers as a boolean.
\func ratLt (x y : Rat) : Bool
=> orBool (LinearOrder.dec<_<= x y)
\lemma ratLt-char {x y : Rat} : ratLt x y = true <-> x < y
=> (aux1 (LinearOrder.dec<_<= x y), aux2 (LinearOrder.dec<_<= x y))
\where \private {
\lemma aux1 {x y : Rat} (o : Or (x < y) (y <= x)) (p : orBool o = true) : x < y \elim o
| inl q => q
| inr q => absurd (true/=false (inv p))
\lemma aux2 {x y : Rat} (o : Or (x < y) (y <= x)) (x<y : x < y) : orBool o = true \elim o
| inl _ => idp
| inr q => absurd $ <-irreflexive (x<y <∘l q)
}
\lemma ratLt-false {x y : Rat} : ratLt x y = false <-> y <= x
=> (\lam p => \case LinearOrder.dec<_<= x y \with {
| inl q => absurd $ true/=false $ inv (ratLt-char.2 q) *> p
| inr q => q
}, \lam q => bool-false \lam p => <-irreflexive (ratLt-char.1 p <∘l q))
-- | A half-open interval `[p,q)`.
\type RatInterval => \Sigma Rat Rat
\func inRI (c : RatInterval) (t : Rat) : Bool
=> not (ratLt t c.1) and ratLt t c.2
\lemma inRI-char {c : RatInterval} {t : Rat} : inRI c t = true <-> (\Sigma (c.1 <= t) (t < c.2))
=> (\lam p => \have s => and.toSigma p \in (ratLt-false.1 (not-char.1 s.1), ratLt-char.1 s.2),
\lam s => and.fromSigma (not-char.2 (ratLt-false.2 s.1), ratLt-char.2 s.2))
-- | The union of a finite list of intervals.
\func inRIs (l : Array RatInterval) (t : Rat) : Bool \elim l
| nil => false
| c :: l => inRI c t or inRIs l t
-- | The endpoints of a list of intervals.
\func ends (l : Array RatInterval) : List Rat \elim l
| nil => List.nil
| c :: l => c.1 List.:: c.2 List.:: ends l
{- | Refinements: a sorted list `T = t₀ ≤ … ≤ t_k` of cut points.
- `snap T P t` is the value of `P` at the left end of the cell `[tⱼ,tⱼ₊₁)` containing `t`, and `false` outside `[t₀,t_k)`.
-}
\func snap (T : List Rat) (P : Rat -> Bool) (t : Rat) : Bool \elim T
| List.nil => false
| _ List.:: List.nil => false
| t0 List.:: (t1 List.:: T) => if (ratLt t t0) false (if (ratLt t t1) (P t0) (snap (t1 List.:: T) P t))
-- | {P} is adapted to {T} if it is constant on the cells of {T} and vanishes outside them.
\type Adapted (T : List Rat) (P : Rat -> Bool) : \Prop
=> \Pi (t : Rat) -> P t = snap T P t
-- | The list of cells of `T` on which `P` holds.
\func cells (T : List Rat) (P : Rat -> Bool) : Array RatInterval \elim T
| List.nil => nil
| _ List.:: List.nil => nil
| t0 List.:: (t1 List.:: T) => if (P t0) ((t0,t1) :: cells (t1 List.:: T) P) (cells (t1 List.:: T) P)
-- | The total length of the cells of `T` on which `P` holds.
\func measT (T : List Rat) (P : Rat -> Bool) : Rat \elim T
| List.nil => 0
| _ List.:: List.nil => 0
| t0 List.:: (t1 List.:: T) => if (P t0) (t1 - t0) 0 + measT (t1 List.:: T) P
\module Refinement \where {
\lemma ratLt-true {x y : Rat} (p : x < y) : ratLt x y = true
=> ratLt-char.2 p
\lemma sorted-tail {t0 : Rat} {T : List Rat} (s : Sorted (t0 List.:: T)) : Sorted T \elim s
| sorted-cons _ s => s
\lemma sorted-InList {t0 e : Rat} {T : List Rat} (s : Sorted (t0 List.:: T)) (i : InList e (t0 List.:: T)) : t0 <= e \elim T, s, i
| _, _, here p => =_<= (inv p)
| List.nil, _, there i => \case i
| t1 List.:: T, sorted-cons h s, there i => h <=∘ sorted-InList s i
\lemma snap-below {t t1 : Rat} {T : List Rat} {P : Rat -> Bool} (h : t < t1) : snap (t1 List.:: T) P t = false \elim T
| List.nil => idp
| t2 List.:: T => rewrite (ratLt-true h) idp
\lemma snap-skip {t t0 t1 : Rat} {T : List Rat} {P : Rat -> Bool} (h0 : t0 <= t) (h1 : t1 <= t)
: snap (t0 List.:: t1 List.:: T) P t = snap (t1 List.:: T) P t
=> rewrite (ratLt-false.2 h0, ratLt-false.2 h1) idp
\lemma snap-cell {t t0 t1 : Rat} {T : List Rat} {P : Rat -> Bool} (h0 : t0 <= t) (h1 : t < t1)
: snap (t0 List.:: t1 List.:: T) P t = P t0
=> rewrite (ratLt-false.2 h0, ratLt-true h1) idp
\lemma inRIs-if (b : Bool) (x y : Array RatInterval) (t : Rat) : inRIs (if b x y) t = if b (inRIs x t) (inRIs y t) \elim b
| true => idp
| false => idp
\lemma cells-step (p u v r : Bool) (uv : u = true -> v = true) (vr : v = true -> r = false)
: if p ((not u and v) or r) r = if u false (if v p r) \elim u, v, r
| true, true, true => absurd (true/=false (vr idp))
| true, true, false => cases p idp
| true, false, _ => absurd (true/=false (inv (uv idp)))
| false, true, true => absurd (true/=false (vr idp))
| false, true, false => cases p idp
| false, false, r => cases (p,r) idp
-- | The cells of `T` on which `P` holds contain exactly the points where `snap T P` holds.
\lemma inRIs-cells {T : List Rat} {P : Rat -> Bool} (s : Sorted T) (t : Rat) : inRIs (cells T P) t = snap T P t \elim T, s
| List.nil, _ => idp
| _ List.:: List.nil, _ => idp
| t0 List.:: (t1 List.:: T), sorted-cons h s =>
inRIs-if (P t0) ((t0,t1) :: cells (t1 List.:: T) P) (cells (t1 List.:: T) P) t
*> pmap (\lam r => if (P t0) (inRI (t0,t1) t or r) r) (inRIs-cells s t)
*> cells-step (P t0) (ratLt t t0) (ratLt t t1) (snap (t1 List.:: T) P t)
(\lam e => ratLt-true (ratLt-char.1 e <∘l h)) (\lam e => snap-below (ratLt-char.1 e))
\lemma if-op (f : Bool -> Bool -> Bool) (f0 : f false false = false) (u v a b r r' : Bool)
: if u false (if v (f a b) (f r r')) = f (if u false (if v a r)) (if u false (if v b r')) \elim u, v
| true, _ => inv f0
| false, true => idp
| false, false => idp
\lemma snap-op (f : Bool -> Bool -> Bool) (f0 : f false false = false) {P Q : Rat -> Bool} (T : List Rat) (t : Rat)
: snap T (\lam s => f (P s) (Q s)) t = f (snap T P t) (snap T Q t) \elim T
| List.nil => inv f0
| _ List.:: List.nil => inv f0
| t0 List.:: (t1 List.:: T) => pmap (\lam r => if (ratLt t t0) false (if (ratLt t t1) (f (P t0) (Q t0)) r)) (snap-op f f0 (t1 List.:: T) t)
*> if-op f f0 (ratLt t t0) (ratLt t t1) (P t0) (Q t0) (snap (t1 List.:: T) P t) (snap (t1 List.:: T) Q t)
\lemma if-false (u v : Bool) : if u false (if v false false) = false \elim u, v
| true, _ => idp
| false, true => idp
| false, false => idp
\lemma snap-false (T : List Rat) (t : Rat) : snap T (\lam _ => false) t = false \elim T
| List.nil => idp
| _ List.:: List.nil => idp
| t0 List.:: (t1 List.:: T) => pmap (\lam r => if (ratLt t t0) false (if (ratLt t t1) false r)) (snap-false (t1 List.:: T) t) *> if-false _ _
\lemma adapted-op (f : Bool -> Bool -> Bool) (f0 : f false false = false) {T : List Rat} {P Q : Rat -> Bool} (a : Adapted T P) (b : Adapted T Q)
: Adapted T (\lam s => f (P s) (Q s))
=> \lam t => pmap2 f (a t) (b t) *> inv (snap-op f f0 T t)
\lemma adapted-false {T : List Rat} : Adapted T (\lam _ => false)
=> \lam t => inv (snap-false T t)
\lemma endpoint-tail {e t0 t1 : Rat} {T : List Rat} (s : Sorted (t0 List.:: t1 List.:: T)) (h : InList e (t0 List.:: t1 List.:: T) || e <= t0)
: InList e (t1 List.:: T) || e <= t1
=> \case h \with {
| byLeft (here p) => byRight (=_<= p <=∘ \case s \with { | sorted-cons q _ => q })
| byLeft (there i) => byLeft i
| byRight q => byRight (q <=∘ \case s \with { | sorted-cons q' _ => q' })
}
\lemma inRI-adapted-aux {c : RatInterval} {t0 : Rat} {T : List Rat} (s : Sorted (t0 List.:: T))
(hp : InList c.1 (t0 List.:: T) || c.1 <= t0) (hq : InList c.2 (t0 List.:: T) || c.2 <= t0)
{t : Rat} (t0<=t : t0 <= t) : inRI c t = snap (t0 List.:: T) (inRI c) t \elim T
| List.nil =>
\have q<=t : c.2 <= t => \case hq \with {
| byLeft (here e) => =_<= e <=∘ t0<=t
| byLeft (there i) => \case i
| byRight q => q <=∘ t0<=t
}
\in bool-false \lam h => <-irreflexive ((inRI-char.1 h).2 <∘l q<=t)
| t1 List.:: T => \case LinearOrder.dec<_<= t t1 \with {
| inl t<t1 => bool-ext (\lam h => \have hh => inRI-char.1 h \in inRI-char.2 (\case hp \with {
| byLeft (here e) => =_<= e
| byLeft (there i) => absurd $ <-irreflexive (t<t1 <∘l sorted-InList (sorted-tail s) i <∘l hh.1)
| byRight q => q
}, t0<=t <∘r hh.2))
(\lam h => \have hh => inRI-char.1 h \in inRI-char.2 (hh.1 <=∘ t0<=t, \case hq \with {
| byLeft (here e) => absurd $ <-irreflexive (transport (t0 <) e hh.2)
| byLeft (there i) => t<t1 <∘l sorted-InList (sorted-tail s) i
| byRight q => absurd $ <-irreflexive (hh.2 <∘l q)
}))
*> inv (snap-cell t0<=t t<t1)
| inr t1<=t => inRI-adapted-aux (sorted-tail s) (endpoint-tail s hp) (endpoint-tail s hq) t1<=t *> inv (snap-skip t0<=t t1<=t)
}
-- | An interval is adapted to every sorted list containing its endpoints.
\lemma inRI-adapted {c : RatInterval} {T : List Rat} (s : Sorted T) (hp : InList c.1 T) (hq : InList c.2 T) : Adapted T (inRI c) \elim T
| List.nil => \case hp
| t0 List.:: T => \lam t => \case LinearOrder.dec<_<= t t0 \with {
| inl t<t0 => bool-false (\lam h => <-irreflexive ((t<t0 <∘l sorted-InList s hp) <∘l (inRI-char.1 h).1)) *> inv (snap-below t<t0)
| inr t0<=t => inRI-adapted-aux s (byLeft hp) (byLeft hq) t0<=t
}
-- | A finite union of intervals is adapted to every sorted list containing its endpoints.
\lemma adapted-inRIs {l : Array RatInterval} {T : List Rat} (s : Sorted T) (sub : ListSub (ends l) T) : Adapted T (inRIs l) \elim l
| nil => adapted-false
| c :: l => adapted-op (\lam a b => a or b) idp (inRI-adapted s (sub (here idp)) (sub (there (here idp)))) (adapted-inRIs s \lam i => sub (there (there i)))
\lemma perm-InList {a : Rat} {xs ys : List Rat} (p : Perm xs ys) (i : InList a xs) : InList a ys \elim xs, ys, p
| List.nil, List.nil, perm-nil => \case i
| x List.:: xs, y List.:: ys, perm-:: e q => \case \elim i \with {
| here h => here (h *> e)
| there i => there (perm-InList q i)
}
| x List.:: (x' List.:: xs), y List.:: (y' List.:: ys), perm-swap e1 e2 e3 => \case \elim i \with {
| here h => there (here (h *> e1))
| there (here h) => here (h *> e2)
| there (there i) => there (there (transport (InList a) e3 i))
}
| xs, ys, perm-trans p1 p2 => perm-InList p2 (perm-InList p1 i)
\func perm-++-comm {T S : List Rat} : Perm (T ++ S) (S ++ T) \elim T
| List.nil => transportInv (Perm S) ++_nil Perm.perm-refl
| a List.:: T => perm-trans (perm-:: idp perm-++-comm) Perm.perm-head
\lemma in-sort {e : Rat} {E : List Rat} (i : InList e E) : InList e (sort E)
=> perm-InList (sort-perm E) i
\lemma insert-lt {s x : Rat} {xs : List Rat} (h : x < s) : insert s (x List.:: xs) = x List.:: insert s xs
=> mcases \with {
| inl _ => idp
| inr s<=x => absurd $ <-irreflexive (h <∘l s<=x)
}
\lemma insert-ge {s x : Rat} {xs : List Rat} (h : s <= x) : insert s (x List.:: xs) = s List.:: x List.:: xs
=> mcases \with {
| inl x<s => absurd $ <-irreflexive (x<s <∘l h)
| inr _ => idp
}
\lemma insert-step (u v w a b r : Bool) (wv : w = true -> v = true) (vw : v = true -> w = false -> a = b)
: if u false (if w a (if w false (if v b r))) = if u false (if v a r) \elim u, v, w
| true, _, _ => idp
| false, true, true => idp
| false, false, true => absurd (true/=false (inv (wv idp)))
| false, true, false => inv (vw idp idp)
| false, false, false => idp
\lemma ins-front (w u b r : Bool) (wu : w = true -> u = true) (ur : u = true -> r = false) (ub : u = true -> w = false -> b = false)
: if w false (if u b r) = r \elim w, u
| true, true => inv (ur idp)
| true, false => absurd (true/=false (inv (wu idp)))
| false, true => ub idp idp *> inv (ur idp)
| false, false => idp
\lemma snap-insert-above {t0 s : Rat} {T : List Rat} {P : Rat -> Bool} (S : Sorted (t0 List.:: T))
(A : \Pi (t : Rat) -> t0 <= t -> P t = snap (t0 List.:: T) P t) (t0<s : t0 < s) (t : Rat)
: snap (insert s (t0 List.:: T)) P t = snap (t0 List.:: T) P t \elim T
| List.nil => pmap (\lam X => snap X P t) (insert-lt {s} {t0} {List.nil} t0<s) *> pmap (\lam b => if (ratLt t t0) false (if (ratLt t s) b false)) (A t0 <=-refl) *> if-false _ _
| t1 List.:: T => pmap (\lam X => snap X P t) (insert-lt {s} {t0} {t1 List.:: T} t0<s) *> \case LinearOrder.dec<_<= t1 s \with {
| inl t1<s =>
\have | t0<=t1 : t0 <= t1 => \case S \with { | sorted-cons q _ => q }
| IH => snap-insert-above (sorted-tail S) (\lam t' t1<=t' => A t' (t0<=t1 <=∘ t1<=t') *> snap-skip (t0<=t1 <=∘ t1<=t') t1<=t') t1<s t
\in pmap (\lam X => snap (t0 List.:: X) P t) (insert-lt t1<s) *> pmap (\lam r => if (ratLt t t0) false (if (ratLt t t1) (P t0) r)) (pmap (\lam X => snap X P t) (inv (insert-lt t1<s)) *> IH)
| inr s<=t1 => pmap (\lam X => snap (t0 List.:: X) P t) (insert-ge s<=t1)
*> insert-step (ratLt t t0) (ratLt t t1) (ratLt t s) (P t0) (P s) (snap (t1 List.:: T) P t)
(\lam e => ratLt-true (ratLt-char.1 e <∘l s<=t1))
(\lam e e' => inv (A s (<=-less t0<s) *> snap-cell (<=-less t0<s) (ratLt-false.1 e' <∘r ratLt-char.1 e)))
}
-- | Inserting a point into an adapted refinement does not change the snapped values.
\lemma snap-insert {T : List Rat} {P : Rat -> Bool} (S : Sorted T) (A : Adapted T P) (s t : Rat) : snap (insert s T) P t = snap T P t \elim T
| List.nil => idp
| t0 List.:: T => \case LinearOrder.dec<_<= t0 s \with {
| inl t0<s => snap-insert-above S (\lam t _ => A t) t0<s t
| inr s<=t0 => pmap (\lam X => snap X P t) (insert-ge s<=t0)
*> ins-front (ratLt t s) (ratLt t t0) (P s) (snap (t0 List.:: T) P t)
(\lam e => ratLt-true (ratLt-char.1 e <∘l s<=t0)) (\lam e => snap-below (ratLt-char.1 e))
(\lam e e' => A s *> snap-below (ratLt-false.1 e' <∘r ratLt-char.1 e))
}
\lemma adapted-insert {T : List Rat} {P : Rat -> Bool} (S : Sorted T) (A : Adapted T P) (s : Rat) : Adapted (insert s T) P
=> \lam t => A t *> inv (snap-insert S A s t)
\lemma if-zero (b : Bool) {x : Rat} (p : x = 0) : if b x 0 = 0 \elim b
| true => p
| false => idp
\lemma split-term (b : Bool) (s t0 t1 M : Rat) : if b (s - t0) 0 + (if b (t1 - s) 0 + M) = if b (t1 - t0) 0 + M \elim b
| true => equation
| false => zro-left
\lemma measT-insert-above {t0 s : Rat} {T : List Rat} {P : Rat -> Bool} (S : Sorted (t0 List.:: T))
(A : \Pi (t : Rat) -> t0 <= t -> P t = snap (t0 List.:: T) P t) (t0<s : t0 < s)
: measT (insert s (t0 List.:: T)) P = measT (t0 List.:: T) P \elim T
| List.nil => pmap (measT __ P) (insert-lt {s} {t0} {List.nil} t0<s) *> pmap (\lam b => if b (s - t0) 0 + 0) (A t0 <=-refl) *> zro-left
| t1 List.:: T => pmap (measT __ P) (insert-lt {s} {t0} {t1 List.:: T} t0<s) *>
\have t0<=t1 : t0 <= t1 => \case S \with { | sorted-cons q _ => q }
\in \case LinearOrder.dec<_<= t1 s \with {
| inl t1<s =>
\have IH => measT-insert-above (sorted-tail S) (\lam t' t1<=t' => A t' (t0<=t1 <=∘ t1<=t') *> snap-skip (t0<=t1 <=∘ t1<=t') t1<=t') t1<s
\in pmap (\lam X => measT (t0 List.:: X) P) (insert-lt t1<s) *> pmap (if (P t0) (t1 - t0) 0 +) (pmap (measT __ P) (inv (insert-lt t1<s)) *> IH)
| inr s<=t1 => pmap (\lam X => measT (t0 List.:: X) P) (insert-ge s<=t1) *> \case LinearOrder.dec<_<= s t1 \with {
| inl s<t1 => pmap (\lam b => if (P t0) (s - t0) 0 + (if b (t1 - s) 0 + measT (t1 List.:: T) P)) (A s (<=-less t0<s) *> snap-cell (<=-less t0<s) s<t1)
*> split-term (P t0) s t0 t1 (measT (t1 List.:: T) P)
| inr t1<=s => \have s=t1 : s = t1 => <=-antisymmetric s<=t1 t1<=s
\in pmap (\lam x => if (P t0) (x - t0) 0 + (if (P x) (t1 - x) 0 + measT (t1 List.:: T) P)) s=t1
*> pmap (if (P t0) (t1 - t0) 0 +) (pmap (+ _) (if-zero (P t1) negative-right) *> zro-left)
}
}
-- | Inserting a point into an adapted refinement does not change the measure.
\lemma measT-insert {T : List Rat} {P : Rat -> Bool} (S : Sorted T) (A : Adapted T P) (s : Rat) : measT (insert s T) P = measT T P \elim T
| List.nil => idp
| t0 List.:: T => \case LinearOrder.dec<_<= t0 s \with {
| inl t0<s => measT-insert-above S (\lam t _ => A t) t0<s
| inr s<=t0 => pmap (measT __ P) (insert-ge s<=t0) *> pmap (+ _) (\case LinearOrder.dec<_<= s t0 \with {
| inl s<t0 => pmap (\lam b => if b (t0 - s) 0) (A s *> snap-below s<t0)
| inr t0<=s => if-zero (P s) (pmap (t0 -) (<=-antisymmetric s<=t0 t0<=s) *> negative-right)
}) *> zro-left
}
-- | Refining an adapted refinement by further points preserves adaptedness and the measure.
\lemma measT-refine (E : List Rat) {T : List Rat} {P : Rat -> Bool} (S : Sorted T) (A : Adapted T P)
: \Sigma (Adapted (sort (E ++ T)) P) (measT (sort (E ++ T)) P = measT T P) \elim E
| List.nil => transportInv (\lam X => \Sigma (Adapted X P) (measT X P = measT T P)) (sorted_sort S) (A, idp)
| e List.:: E => \have IH => measT-refine E S A
\in (adapted-insert (sort-sorted (E ++ T)) IH.1 e, measT-insert (sort-sorted (E ++ T)) IH.1 e *> IH.2)
\lemma measT>=0 {T : List Rat} {P : Rat -> Bool} (S : Sorted T) : (0 : Rat) <= measT T P \elim T, S
| List.nil, _ => <=-refl
| _ List.:: List.nil, _ => <=-refl
| t0 List.:: (t1 List.:: T), sorted-cons h S =>
\have | a : (0 : Rat) <= if (P t0) (t1 - t0) 0 => cases (P t0) \with {
| true => linarith
| false => <=-refl
}
| b => measT>=0 {t1 List.:: T} {P} S
\in linarith
\lemma if-or (a b : Bool) (x : Rat) (d : a and b = false) : if (a or b) x 0 = if a x 0 + if b x 0 \elim a, b
| true, true => absurd (true/=false d)
| true, false => inv zro-right
| false, true => inv zro-left
| false, false => inv zro-left
\lemma measT-or {T : List Rat} {P Q : Rat -> Bool} (d : \Pi (t : Rat) -> P t and Q t = false)
: measT T (\lam t => P t or Q t) = measT T P + measT T Q \elim T
| List.nil => inv zro-left
| _ List.:: List.nil => inv zro-left
| t0 List.:: (t1 List.:: T) => pmap2 (+) (if-or (P t0) (Q t0) (t1 - t0) (d t0)) (measT-or {t1 List.:: T} d) *> equation
\lemma zero-cell (b u v : Bool) (h : b = true -> u = false -> v = false) : if u false (if v b false) = false \elim b, u, v
| _, true, _ => idp
| true, false, true => absurd (true/=false (h idp idp))
| true, false, false => idp
| false, false, true => idp
| false, false, false => idp
-- | If the measure vanishes, then the snapped predicate is empty.
\lemma measT-zero {T : List Rat} {P : Rat -> Bool} (S : Sorted T) (p : measT T P = 0) (t : Rat) : snap T P t = false \elim T, S
| List.nil, _ => idp
| _ List.:: List.nil, _ => idp
| t0 List.:: (t1 List.:: T), sorted-cons h S =>
\have | a0 : (0 : Rat) <= if (P t0) (t1 - t0) 0 => cases (P t0) \with {
| true => linarith
| false => <=-refl
}
| M0 => measT>=0 {t1 List.:: T} {P} S
| a=0 : if (P t0) (t1 - t0) 0 = 0 => linarith
| M=0 : measT (t1 List.:: T) P = 0 => linarith
\in pmap (\lam r => if (ratLt t t0) false (if (ratLt t t1) (P t0) r)) (measT-zero S M=0 t)
*> zero-cell (P t0) (ratLt t t0) (ratLt t t1) (\lam e e' => ratLt-false.2 $ =_<= (inv (term-zero (P t0) a=0 e)) <=∘ ratLt-false.1 e')
\where
\lemma term-zero (b : Bool) {x y : Rat} (p : if b (y - x) 0 = 0) (q : b = true) : x = y \elim b
| true => inv (AddGroup.fromZero p)
| false => absurd (true/=false (inv q))
-- | If the measure does not vanish, then the predicate holds somewhere.
\lemma measT-nonzero {T : List Rat} {P : Rat -> Bool} (p : measT T P /= 0) : ∃ (t : Rat) (P t = true) \elim T
| List.nil => absurd (p idp)
| _ List.:: List.nil => absurd (p idp)
| t0 List.:: (t1 List.:: T) => \case P t0 \as b, idp : P t0 = b \with {
| true, e => inP (t0, e)
| false, e => measT-nonzero {t1 List.:: T} \lam M=0 => p $ pmap (\lam b => if b (t1 - t0) 0 + measT (t1 List.:: T) P) e *> zro-left *> M=0
}
}
\open Refinement
-- | The measure of a list of intervals, computed on the sorted list of its endpoints.
\func measL (l : Array RatInterval) : Rat
=> measT (sort (ends l)) (inRIs l)
-- | The measure can be computed on any adapted refinement.
\lemma measL-char {l : Array RatInterval} {T : List Rat} {P : Rat -> Bool} (S : Sorted T) (A : Adapted T P) (e : \Pi (t : Rat) -> inRIs l t = P t)
: measL l = measT T P
=> \let | SL => sort (ends l)
| ep : inRIs l = P => ext e
| AL : Adapted SL P => transport (Adapted SL) ep (adapted-inRIs (sort-sorted (ends l)) \lam i => in-sort i)
\in pmap (measT SL) ep *> inv (measT-refine T (sort-sorted (ends l)) AL).2 *> pmap (measT __ P) (perm_sort perm-++-comm) *> (measT-refine SL S A).2
-- | The common refinement of two lists of intervals.
\func refine (l l' : Array RatInterval) : List Rat
=> sort (ends l ++ ends l')
\lemma refine-left {l l' : Array RatInterval} : Adapted (refine l l') (inRIs l)
=> adapted-inRIs (sort-sorted _) \lam i => in-sort (InList_++-left i)
\lemma refine-right {l l' : Array RatInterval} : Adapted (refine l l') (inRIs l')
=> adapted-inRIs (sort-sorted _) \lam i => in-sort (InList_++-right i)
\lemma measL-equiv {l l' : Array RatInterval} (r : \Pi (t : Rat) -> inRIs l t = inRIs l' t) : measL l = measL l'
=> measL-char (sort-sorted _) (refine-left {l} {l'}) (\lam t => idp) *> inv (measL-char (sort-sorted _) (refine-left {l} {l'}) (\lam t => inv (r t)))
-- | Two lists of intervals are equivalent if they contain the same points.
\func RatIntervalEq (l l' : Array RatInterval) : \Prop
=> \Pi (t : Rat) -> inRIs l t = inRIs l' t
\func RatIntervalSet => Quotient RatIntervalEq
-- | The finite union of a list of intervals.
\func ratIntervals (l : Array RatInterval) : RatIntervalSet
=> in~ l
-- | The points of an element of `B₀⁽¹⁾`.
\func ratPoints (u : RatIntervalSet) (t : Rat) : Bool \elim u
| in~ l => inRIs l t
| ~-equiv l l' r => r t
\lemma ratPoints-ext {u v : RatIntervalSet} (p : \Pi (t : Rat) -> ratPoints u t = ratPoints v t) : u = v \elim u, v
| in~ l, in~ l' => path (~-equiv l l' p)
-- | A boolean combination of two lists of intervals: the cells of their common refinement on which it holds.
\func opRIs (f : Bool -> Bool -> Bool) (l l' : Array RatInterval) : Array RatInterval
=> cells (refine l l') (\lam t => f (inRIs l t) (inRIs l' t))
\lemma inRIs-opRIs (f : Bool -> Bool -> Bool) (f0 : f false false = false) {l l' : Array RatInterval} {t : Rat}
: inRIs (opRIs f l l') t = f (inRIs l t) (inRIs l' t)
=> inRIs-cells (sort-sorted _) t *> inv (adapted-op f f0 (refine-left {l} {l'}) (refine-right {l} {l'}) t)
\func opRISet (f : Bool -> Bool -> Bool) (f0 : f false false = false) (u v : RatIntervalSet) : RatIntervalSet \elim u, v
| in~ l, in~ l' => in~ (opRIs f l l')
| in~ l, ~-equiv l1 l2 r => path (~-equiv _ _ \lam t => inRIs-opRIs f f0 *> pmap (f _) (r t) *> inv (inRIs-opRIs f f0 {l} {l2}))
| ~-equiv l1 l2 r, in~ l' => path (~-equiv _ _ \lam t => inRIs-opRIs f f0 *> pmap (f __ _) (r t) *> inv (inRIs-opRIs f f0 {l2} {l'}))
\lemma ratPoints_opRISet (f : Bool -> Bool -> Bool) (f0 : f false false = false) {u v : RatIntervalSet} {t : Rat} : ratPoints (opRISet f f0 u v) t = f (ratPoints u t) (ratPoints v t) \elim u, v
| in~ l, in~ l' => inRIs-opRIs f f0
-- | The Boolean ring `B₀⁽¹⁾`: addition is symmetric difference and multiplication is intersection.
\instance IntervalBRing : BooleanRing RatIntervalSet
| zro => in~ nil
| + => opRISet (xor) idp
| zro-left => ratPoints-ext \lam t => ratPoints_opRISet (xor) idp *> xor_false-left
| +-assoc => ratPoints-ext \lam t => ratPoints_opRISet (xor) idp *> pmap (xor _) (ratPoints_opRISet (xor) idp) *> xor-assoc *> inv (ratPoints_opRISet (xor) idp *> pmap (_ xor) (ratPoints_opRISet (xor) idp))
| +-comm => ratPoints-ext \lam t => ratPoints_opRISet (xor) idp *> xor-comm *> inv (ratPoints_opRISet (xor) idp)
| negative u => u
| negative-left => ratPoints-ext \lam t => ratPoints_opRISet (xor) idp *> xor-self
| * => opRISet (and) idp
| *-assoc => ratPoints-ext \lam t => ratPoints_opRISet (and) idp *> pmap (and _) (ratPoints_opRISet (and) idp) *> and-assoc *> inv (ratPoints_opRISet (and) idp *> pmap (_ and) (ratPoints_opRISet (and) idp))
| ldistr => ratPoints-ext \lam t => ratPoints_opRISet (and) idp *> pmap (_ and) (ratPoints_opRISet (xor) idp) *> and-xor-ldistr *> inv (ratPoints_opRISet (xor) idp *> pmap2 (xor) (ratPoints_opRISet (and) idp) (ratPoints_opRISet (and) idp))
| *-comm => ratPoints-ext \lam t => ratPoints_opRISet (and) idp *> and-comm *> inv (ratPoints_opRISet (and) idp)
| isBooleanRing => ratPoints-ext \lam t => ratPoints_opRISet (and) idp *> and-idemp
\lemma ratPoints_+ {u v : IntervalBRing} {t : Rat} : ratPoints (u + v) t = ratPoints u t xor ratPoints v t
=> ratPoints_opRISet (xor) idp
\lemma ratPoints_* {u v : IntervalBRing} {t : Rat} : ratPoints (u * v) t = ratPoints u t and ratPoints v t
=> ratPoints_opRISet (and) idp
\lemma ratPoints_join {u v : IntervalBRing} {t : Rat} : ratPoints (u ∨ v) t = ratPoints u t or ratPoints v t
=> ratPoints_+ *> pmap2 (xor) ratPoints_+ ratPoints_* *> xor-or-and
\lemma ratPoints-interval {c : RatInterval} {t : Rat} : ratPoints (ratIntervals (c :: nil)) t = inRI c t
=> or-false
\func measQ (u : RatIntervalSet) : Rat \elim u
| in~ l => measL l
| ~-equiv l l' r => measL-equiv r
-- | The measure ring `B₀⁽¹⁾` of finite unions of rational intervals, with the measure given by length.
\instance IntervalRing : MeasureRing RatIntervalSet
| BooleanRing => IntervalBRing
| meas u => Real.fromRat (measQ u)
| meas_zro => idp
| meas>=0 {u} => rat_real_<=.1 (measQ>=0 u)
| meas-disjoint {x} {y} p => \case \elim x, \elim y, p \with {
| in~ l, in~ l', p =>
\have | S : Sorted (refine l l') => sort-sorted _
| d (t : Rat) : inRIs l t and inRIs l' t = false => inv (ratPoints_* {in~ l} {in~ l'}) *> pmap (ratPoints __ t) p
\in pmap Real.fromRat (measL-char S (adapted-op (\lam a b => a or b) idp (refine-left {l} {l'}) (refine-right {l} {l'})) (\lam t => ratPoints_join {in~ l} {in~ l'})
*> measT-or d *> inv (pmap2 (+) (measL-char S (refine-left {l} {l'}) (\lam t => idp)) (measL-char S (refine-right {l} {l'}) (\lam t => idp)))) *> inv RealAbGroup.+-rat
}
| meas-ext => \lam {u} => \case \elim u \with {
| in~ l => \lam p => ratPoints-ext \lam t => adapted-inRIs (sort-sorted (ends l)) (\lam i => in-sort i) t *> measT-zero (sort-sorted _) (Real.fromRat-inj p) t
}
\where
\lemma measQ>=0 (u : RatIntervalSet) : (0 : Rat) <= measQ u \elim u
| in~ l => measT>=0 (sort-sorted _)
\lemma IntervalRing-rational : IntervalRing.IsRationalMeasure
=> \lam u => inP (measQ u, idp)
-- | Emptiness is decidable: an element is either `0` or contains a point.
\lemma IntervalRing-point (u : RatIntervalSet) : (u = in~ nil) || ∃ (t : Rat) (ratPoints u t = true) \elim u
| in~ l => \case decideEq (measL l) 0 \with {
| yes e => byLeft (meas-ext (pmap Real.fromRat e))
| no q => byRight (measT-nonzero q)
}
-- | The measure of a rational interval is its length.
\lemma meas_interval {p q : Rat} : meas {IntervalRing} (ratIntervals ((p,q) :: nil)) = Real.fromRat ((q - p) ∨ 0)
=> pmap Real.fromRat $ \case LinearOrder.dec<_<= q p \with {
| inl q<p =>
\have | S : Sorted (q List.:: p List.:: List.nil) => sorted-cons (<=-less q<p) (sorted-cons <=-refl sorted-nil)
| sub : ListSub (ends ((p,q) :: nil)) (q List.:: p List.:: List.nil) => \lam {e} i => \case \elim i \with {
| here h => there (here h)
| there (here h) => here h
| there (there i) => \case i
}
| h : inRI (p,q) q = false => bool-false \lam t => <-irreflexive (q<p <∘l ((inRI-char {p,q}).1 t).1)
\in measL-char {(p,q) :: nil} S (adapted-inRIs {(p,q) :: nil} S sub) (\lam t => idp) *> pmap (\lam b => if b (p - q) 0 + 0) (or-false *> h) *> zro-left *> inv (join0-neg linarith)
| inr p<=q =>
\have S : Sorted (p List.:: q List.:: List.nil) => sorted-cons p<=q (sorted-cons <=-refl sorted-nil)
\in measL-char {(p,q) :: nil} S (adapted-inRIs {(p,q) :: nil} S \lam i => i) (\lam t => idp) *> \case LinearOrder.dec<_<= p q \with {
| inl p<q => pmap (\lam b => if b (q - p) 0 + 0) (or-false *> (inRI-char {p,q}).2 (<=-refl, p<q)) *> zro-right *> inv (<=-antisymmetric (join-univ <=-refl linarith) join-left)
| inr q<=p => pmap (\lam b => if b (q - p) 0 + 0) (or-false *> bool-false \lam t => <-irreflexive (((inRI-char {p,q}).1 t).2 <∘l q<=p)) *> zro-left
*> inv (join0-neg (=_<= (pmap (q -) (<=-antisymmetric p<=q q<=p) *> negative-right)))
}
}
\where {
\lemma join0-neg {x : Rat} (p : x <= 0) : x ∨ 0 = 0
=> <=-antisymmetric (join-univ p <=-refl) join-right
}