{-# OPTIONS --safe --cubical-compatible --guardedness --exact-split --double-check #-}
module Real.Interval.Pow where
open import Data.Nat using (ℕ; zero; suc; _+_; _*_)
open import Real.Interval using (RealI)
open import Real.Interval.Arith using (0ℝ; 1ℝ)
open import Real.Interval.Embed using (0ℝ<ℝ1ℝ)
open import Real.Interval.Equiv using (_≈ℝ_; ≈ℝ-refl; ≈ℝ-sym)
open import Real.Interval.Mul using (_*ℝ_)
open import Real.Interval.Mul.Laws using
(*ℝ-comm; *ℝ-assoc; *ℝ-identityˡ; *ℝ-identityʳ; *ℝ-zeroˡ)
open import Real.Interval.Mul.Cong using (*ℝ-cong)
open import Real.Interval.Mul.Order using
(pos*pos; *ℝ-nonNeg; *ℝ-≤-mono; ≤-resp-≈ˡ; ≤-resp-≈ʳ)
open import Real.Interval.Order using (_<ℝ_)
open import Real.Interval.OrderComplete.Base using (_≤ℝ_; ≤ℝ-trans)
open import Real.Interval.OrderLe using (≤ℝ-refl; <⇒≤ℝ)
open import Real.Interval.Reasoning.Equiv using (beginℝ_; _≈ℝ⟨_⟩_; _∎ℝ)
infixr 8 _^ℝ_
_^ℝ_ : RealI → ℕ → RealI
x ^ℝ zero = 1ℝ
x ^ℝ suc n = (x ^ℝ n) *ℝ x
^ℝ-zero : (x : RealI) → x ^ℝ 0 ≈ℝ 1ℝ
^ℝ-zero x = ≈ℝ-refl
^ℝ-suc : (x : RealI) (n : ℕ) → x ^ℝ suc n ≈ℝ (x ^ℝ n) *ℝ x
^ℝ-suc x n = ≈ℝ-refl
^ℝ-one : (x : RealI) → x ^ℝ 1 ≈ℝ x
^ℝ-one x = *ℝ-identityˡ x
^ℝ-two : (x : RealI) → x ^ℝ 2 ≈ℝ x *ℝ x
^ℝ-two x =
beginℝ
(1ℝ *ℝ x) *ℝ x
≈ℝ⟨ *ℝ-cong (*ℝ-identityˡ x) ≈ℝ-refl ⟩
x *ℝ x
∎ℝ
^ℝ-cong : {x y : RealI} → x ≈ℝ y → (n : ℕ) → x ^ℝ n ≈ℝ y ^ℝ n
^ℝ-cong {x} {y} x≈y zero = ≈ℝ-refl
^ℝ-cong {x} {y} x≈y (suc n) =
*ℝ-cong (^ℝ-cong x≈y n) x≈y
^ℝ-pos : (x : RealI) → 0ℝ <ℝ x → (n : ℕ) → 0ℝ <ℝ x ^ℝ n
^ℝ-pos x 0<x zero = 0ℝ<ℝ1ℝ
^ℝ-pos x 0<x (suc n) = pos*pos (x ^ℝ n) x (^ℝ-pos x 0<x n) 0<x
^ℝ-+ : (x : RealI) (m n : ℕ) → x ^ℝ (m + n) ≈ℝ (x ^ℝ m) *ℝ (x ^ℝ n)
^ℝ-+ x zero n = ≈ℝ-sym (*ℝ-identityˡ (x ^ℝ n))
^ℝ-+ x (suc m) n =
beginℝ
(x ^ℝ (m + n)) *ℝ x
≈ℝ⟨ *ℝ-cong (^ℝ-+ x m n) ≈ℝ-refl ⟩
((x ^ℝ m) *ℝ (x ^ℝ n)) *ℝ x
≈ℝ⟨ *ℝ-assoc (x ^ℝ m) (x ^ℝ n) x ⟩
(x ^ℝ m) *ℝ ((x ^ℝ n) *ℝ x)
≈ℝ⟨ *ℝ-cong ≈ℝ-refl (*ℝ-comm (x ^ℝ n) x) ⟩
(x ^ℝ m) *ℝ (x *ℝ (x ^ℝ n))
≈ℝ⟨ ≈ℝ-sym (*ℝ-assoc (x ^ℝ m) x (x ^ℝ n)) ⟩
((x ^ℝ m) *ℝ x) *ℝ (x ^ℝ n)
∎ℝ
^ℝ-* : (x y : RealI) (n : ℕ) → (x *ℝ y) ^ℝ n ≈ℝ (x ^ℝ n) *ℝ (y ^ℝ n)
^ℝ-* x y zero = ≈ℝ-sym (*ℝ-identityˡ 1ℝ)
^ℝ-* x y (suc n) =
beginℝ
((x *ℝ y) ^ℝ n) *ℝ (x *ℝ y)
≈ℝ⟨ *ℝ-cong (^ℝ-* x y n) ≈ℝ-refl ⟩
((x ^ℝ n) *ℝ (y ^ℝ n)) *ℝ (x *ℝ y)
≈ℝ⟨ *ℝ-assoc (x ^ℝ n) (y ^ℝ n) (x *ℝ y) ⟩
(x ^ℝ n) *ℝ ((y ^ℝ n) *ℝ (x *ℝ y))
≈ℝ⟨ *ℝ-cong ≈ℝ-refl (≈ℝ-sym (*ℝ-assoc (y ^ℝ n) x y)) ⟩
(x ^ℝ n) *ℝ (((y ^ℝ n) *ℝ x) *ℝ y)
≈ℝ⟨ *ℝ-cong ≈ℝ-refl
(*ℝ-cong (*ℝ-comm (y ^ℝ n) x) ≈ℝ-refl) ⟩
(x ^ℝ n) *ℝ ((x *ℝ (y ^ℝ n)) *ℝ y)
≈ℝ⟨ *ℝ-cong ≈ℝ-refl (*ℝ-assoc x (y ^ℝ n) y) ⟩
(x ^ℝ n) *ℝ (x *ℝ ((y ^ℝ n) *ℝ y))
≈ℝ⟨ ≈ℝ-sym (*ℝ-assoc (x ^ℝ n) x ((y ^ℝ n) *ℝ y)) ⟩
((x ^ℝ n) *ℝ x) *ℝ ((y ^ℝ n) *ℝ y)
∎ℝ
^ℝ-one-base : (n : ℕ) → 1ℝ ^ℝ n ≈ℝ 1ℝ
^ℝ-one-base zero = ≈ℝ-refl
^ℝ-one-base (suc n) =
beginℝ
(1ℝ ^ℝ n) *ℝ 1ℝ
≈ℝ⟨ *ℝ-cong (^ℝ-one-base n) ≈ℝ-refl ⟩
1ℝ *ℝ 1ℝ
≈ℝ⟨ *ℝ-identityʳ 1ℝ ⟩
1ℝ
∎ℝ
^ℝ-suc-comm : (x : RealI) (n : ℕ) → x ^ℝ suc n ≈ℝ x *ℝ (x ^ℝ n)
^ℝ-suc-comm x n = *ℝ-comm (x ^ℝ n) x
^ℝ-pow : (x : RealI) (m n : ℕ) → (x ^ℝ m) ^ℝ n ≈ℝ x ^ℝ (n * m)
^ℝ-pow x m zero = ≈ℝ-refl
^ℝ-pow x m (suc n) =
beginℝ
((x ^ℝ m) ^ℝ n) *ℝ (x ^ℝ m)
≈ℝ⟨ *ℝ-cong (^ℝ-pow x m n) ≈ℝ-refl ⟩
(x ^ℝ (n * m)) *ℝ (x ^ℝ m)
≈ℝ⟨ *ℝ-comm (x ^ℝ (n * m)) (x ^ℝ m) ⟩
(x ^ℝ m) *ℝ (x ^ℝ (n * m))
≈ℝ⟨ ≈ℝ-sym (^ℝ-+ x m (n * m)) ⟩
x ^ℝ (m + n * m)
∎ℝ
^ℝ-zero-mul : (n : ℕ) → (0ℝ ^ℝ n) *ℝ 0ℝ ≈ℝ 0ℝ
^ℝ-zero-mul zero = *ℝ-identityˡ 0ℝ
^ℝ-zero-mul (suc n) =
beginℝ
((0ℝ ^ℝ n) *ℝ 0ℝ) *ℝ 0ℝ
≈ℝ⟨ *ℝ-cong (^ℝ-zero-mul n) ≈ℝ-refl ⟩
0ℝ *ℝ 0ℝ
≈ℝ⟨ *ℝ-zeroˡ 0ℝ ⟩
0ℝ
∎ℝ
^ℝ-of-zero : (x : RealI) → x ≈ℝ 0ℝ → (n : ℕ) → x ^ℝ suc n ≈ℝ 0ℝ
^ℝ-of-zero x x≈0 n =
beginℝ
(x ^ℝ n) *ℝ x
≈ℝ⟨ *ℝ-cong (^ℝ-cong x≈0 n) x≈0 ⟩
(0ℝ ^ℝ n) *ℝ 0ℝ
≈ℝ⟨ ^ℝ-zero-mul n ⟩
0ℝ
∎ℝ
^ℝ-nonNeg : (x : RealI) → 0ℝ ≤ℝ x → (n : ℕ) → 0ℝ ≤ℝ x ^ℝ n
^ℝ-nonNeg x 0≤x zero = <⇒≤ℝ 0ℝ<ℝ1ℝ
^ℝ-nonNeg x 0≤x (suc n) =
*ℝ-nonNeg (x ^ℝ n) x (^ℝ-nonNeg x 0≤x n) 0≤x
^ℝ-mono : (x y : RealI) → 0ℝ ≤ℝ x → x ≤ℝ y →
(n : ℕ) → x ^ℝ n ≤ℝ y ^ℝ n
^ℝ-mono x y 0≤x x≤y zero = ≤ℝ-refl
^ℝ-mono x y 0≤x x≤y (suc n) =
let
ih : x ^ℝ n ≤ℝ y ^ℝ n
ih = ^ℝ-mono x y 0≤x x≤y n
left : (x ^ℝ n) *ℝ x ≤ℝ (y ^ℝ n) *ℝ x
left = *ℝ-≤-mono (x ^ℝ n) (y ^ℝ n) x ih 0≤x
0≤y : 0ℝ ≤ℝ y
0≤y = ≤ℝ-trans 0≤x x≤y
raw : x *ℝ (y ^ℝ n) ≤ℝ y *ℝ (y ^ℝ n)
raw = *ℝ-≤-mono x y (y ^ℝ n) x≤y (^ℝ-nonNeg y 0≤y n)
right : (y ^ℝ n) *ℝ x ≤ℝ (y ^ℝ n) *ℝ y
right =
≤-resp-≈ʳ
(≤-resp-≈ˡ (*ℝ-comm x (y ^ℝ n)) raw)
(*ℝ-comm y (y ^ℝ n))
in
≤ℝ-trans left right