Todd Waugh Ambridge, January 2024

# Ternary signed-digit encodings' suitability for search, optimisation and regression

\begin{code}
{-# OPTIONS --without-K --safe #-}

open import MLTT.Spartan
open import UF.DiscreteAndSeparated
open import UF.Subsingletons
open import UF.FunExt
open import MLTT.Two-Properties
open import UF.SubtypeClassifier

module TWA.Thesis.Chapter6.SignedDigitSearch
  (fe : FunExt) (pe : PropExt) where

open import TWA.Thesis.Chapter2.Finite
open import TWA.Thesis.Chapter2.Sequences
open import TWA.Thesis.Chapter3.ClosenessSpaces fe
 hiding (decidable-predicate;decidable-uc-predicate)
open import TWA.Thesis.Chapter3.ClosenessSpaces-Examples fe
open import TWA.Thesis.Chapter3.SearchableTypes fe
open import TWA.Thesis.Chapter3.SearchableTypes-Examples fe pe
open import TWA.Thesis.Chapter4.ApproxOrder fe
open import TWA.Thesis.Chapter4.ApproxOrder-Examples fe
open import TWA.Thesis.Chapter4.GlobalOptimisation fe
open import TWA.Thesis.Chapter5.SignedDigit
open import TWA.Thesis.Chapter6.SequenceContinuity fe
open import TWA.Thesis.Chapter6.SignedDigitOrder fe
\end{code}

## Totally bounded

\begin{code}
πŸ›α΄Ί-totally-bounded : totally-bounded πŸ›α΄Ί-ClosenessSpace 𝓀₀
πŸ›α΄Ί-totally-bounded = β„•β†’F-totally-bounded πŸ›-is-discrete πŸ›-is-finite O

πŸ›α΄ΊΓ—πŸ›α΄Ί-totally-bounded : totally-bounded πŸ›α΄ΊΓ—πŸ›α΄Ί-ClosenessSpace 𝓀₀
πŸ›α΄ΊΓ—πŸ›α΄Ί-totally-bounded
 = Γ—-totally-bounded
    πŸ›α΄Ί-ClosenessSpace πŸ›α΄Ί-ClosenessSpace
    πŸ›α΄Ί-totally-bounded πŸ›α΄Ί-totally-bounded
\end{code}

## Global optimisation

\begin{code}
πŸ›α΄Ίβ†’πŸ›α΄Ί-global-opt : (f : πŸ›α΄Ί β†’ πŸ›α΄Ί)
                 β†’ f-ucontinuous πŸ›α΄Ί-ClosenessSpace πŸ›α΄Ί-ClosenessSpace f
                 β†’ (Ο΅ : β„•)
                 β†’ (has Ο΅ global-minimal) _β‰€βΏπŸ›α΄Ί_ f
πŸ›α΄Ίβ†’πŸ›α΄Ί-global-opt f Ο• Ο΅
 = global-opt πŸ›α΄Ί-ClosenessSpace πŸ›α΄Ί-ClosenessSpace
    (repeat O)
    _β‰€βΏπŸ›α΄Ί_
    β‰€βΏπŸ›α΄Ί-is-approx-order
    Ο΅ f Ο•
    πŸ›α΄Ί-totally-bounded
\end{code}

## Uniformly continuously searchable

\begin{code}
πŸ›α΄Ί-csearchable-tb πŸ›α΄Ί-csearchable
 : {𝓦 : Universe} β†’ csearchable 𝓦 πŸ›α΄Ί-ClosenessSpace
πŸ›α΄Ί-csearchable-tb
 = totally-bounded-csearchable
     πŸ›α΄Ί-ClosenessSpace (repeat O) πŸ›α΄Ί-totally-bounded
πŸ›α΄Ί-csearchable
 = discrete-finite-seq-csearchable O πŸ›-is-finite πŸ›-is-discrete

πŸ›α΄ΊΓ—πŸ›α΄Ί-csearchable-tb πŸ›α΄ΊΓ—πŸ›α΄Ί-csearchable
 : {𝓦 : Universe} β†’ csearchable 𝓦 πŸ›α΄ΊΓ—πŸ›α΄Ί-ClosenessSpace
πŸ›α΄ΊΓ—πŸ›α΄Ί-csearchable-tb
 = totally-bounded-csearchable
    πŸ›α΄ΊΓ—πŸ›α΄Ί-ClosenessSpace (repeat O , repeat O) πŸ›α΄ΊΓ—πŸ›α΄Ί-totally-bounded
πŸ›α΄ΊΓ—πŸ›α΄Ί-csearchable
 = Γ—-csearchable πŸ›α΄Ί-ClosenessSpace πŸ›α΄Ί-ClosenessSpace
    πŸ›α΄Ί-csearchable πŸ›α΄Ί-csearchable
\end{code}

## Cantor space search and optimisation

\begin{code}
≀₂-is-preorder : is-preorder _≀₂_
≀₂-is-preorder
 = (Ξ» _ β†’ ≀₂-refl) , ≀₂-trans , Ξ» _ _ β†’ ≀₂-is-prop-valued

≀₂-is-antisym-preorder : is-partial-order _≀₂_
≀₂-is-antisym-preorder = ≀₂-is-preorder , Ξ» _ _ β†’ ≀₂-anti

≀₂-is-antisym-linear-preorder : is-linear-order _≀₂_
≀₂-is-antisym-linear-preorder = ≀₂-is-antisym-preorder , ≀₂-linear
 where
  ≀₂-linear : linear _≀₂_
  ≀₂-linear β‚€ _ = inl ⋆
  ≀₂-linear ₁ β‚€ = inr ⋆
  ≀₂-linear ₁ ₁ = inl ⋆

𝟚ᴺ : 𝓀₀ Μ‡
𝟚ᴺ = β„• β†’ 𝟚

𝟚ᴺ-lexicorder : 𝟚ᴺ β†’ 𝟚ᴺ β†’ 𝓀₀ Μ‡
𝟚ᴺ-lexicorder = discrete-lexicorder 𝟚-is-discrete _≀₂_

𝟚ᴺ-lexicorder-is-preorder : is-preorder 𝟚ᴺ-lexicorder
𝟚ᴺ-lexicorder-is-preorder
 = discrete-lexicorder-is-preorder
    𝟚-is-discrete _≀₂_ ≀₂-is-antisym-preorder

𝟚ᴺ-approx-lexicorder : 𝟚ᴺ β†’ 𝟚ᴺ β†’ β„• β†’ 𝓀₀ Μ‡ 
𝟚ᴺ-approx-lexicorder = discrete-approx-lexicorder 𝟚-is-discrete _≀₂_

𝟚ᴺ-approx-lexicorder-is-approx-order
 : is-approx-order 𝟚ᴺ-ClosenessSpace 𝟚ᴺ-approx-lexicorder
𝟚ᴺ-approx-lexicorder-is-approx-order
 = discrete-approx-lexicorder-is-approx-order
    𝟚-is-discrete _≀₂_
    ≀₂-is-antisym-linear-preorder 

𝟚ᴺ-approx-lexicorder' : 𝟚ᴺ β†’ 𝟚ᴺ β†’ β„• β†’ Ξ© 𝓀₀
𝟚ᴺ-approx-lexicorder' α β n
 = 𝟚ᴺ-approx-lexicorder α β n
 , ≀ⁿ-prop 𝟚ᴺ-ClosenessSpace 𝟚ᴺ-approx-lexicorder-is-approx-order n Ξ± Ξ²

𝟚ᴺ-totally-bounded : totally-bounded 𝟚ᴺ-ClosenessSpace 𝓀₀
𝟚ᴺ-totally-bounded = β„•β†’F-totally-bounded 𝟚-is-discrete 𝟚-is-finite β‚€

πŸšα΄ΊΓ—πŸšα΄Ί-totally-bounded : totally-bounded πŸšα΄ΊΓ—πŸšα΄Ί-ClosenessSpace 𝓀₀
πŸšα΄ΊΓ—πŸšα΄Ί-totally-bounded
 = Γ—-totally-bounded
    𝟚ᴺ-ClosenessSpace 𝟚ᴺ-ClosenessSpace
    𝟚ᴺ-totally-bounded 𝟚ᴺ-totally-bounded

πŸšα΄Ίβ†’πŸ›α΄Ί-global-opt : (f : 𝟚ᴺ β†’ πŸ›α΄Ί)
                 β†’ f-ucontinuous 𝟚ᴺ-ClosenessSpace πŸ›α΄Ί-ClosenessSpace f
                 β†’ (Ο΅ : β„•)
                 β†’ (has Ο΅ global-minimal) _β‰€βΏπŸ›α΄Ί_ f
πŸšα΄Ίβ†’πŸ›α΄Ί-global-opt f Ο• Ο΅
 = global-opt 𝟚ᴺ-ClosenessSpace πŸ›α΄Ί-ClosenessSpace
    (repeat β‚€)
    _β‰€βΏπŸ›α΄Ί_
    β‰€βΏπŸ›α΄Ί-is-approx-order
    Ο΅ f Ο•
    𝟚ᴺ-totally-bounded

𝟚ᴺ-csearchable-tb 𝟚ᴺ-csearchable
 : {𝓦 : Universe} β†’ csearchable 𝓦 𝟚ᴺ-ClosenessSpace
𝟚ᴺ-csearchable-tb
 = totally-bounded-csearchable
    𝟚ᴺ-ClosenessSpace (repeat β‚€) 𝟚ᴺ-totally-bounded
𝟚ᴺ-csearchable
 = discrete-finite-seq-csearchable β‚€ 𝟚-is-finite 𝟚-is-discrete

πŸšα΄ΊΓ—πŸšα΄Ί-csearchable-tb πŸšα΄ΊΓ—πŸšα΄Ί-csearchable
 : {𝓦 : Universe} β†’ csearchable 𝓦 πŸšα΄ΊΓ—πŸšα΄Ί-ClosenessSpace
πŸšα΄ΊΓ—πŸšα΄Ί-csearchable-tb
 = totally-bounded-csearchable
    πŸšα΄ΊΓ—πŸšα΄Ί-ClosenessSpace (repeat β‚€ , repeat β‚€) πŸšα΄ΊΓ—πŸšα΄Ί-totally-bounded
πŸšα΄ΊΓ—πŸšα΄Ί-csearchable
 = Γ—-csearchable 𝟚ᴺ-ClosenessSpace 𝟚ᴺ-ClosenessSpace
    𝟚ᴺ-csearchable 𝟚ᴺ-csearchable
\end{code}

## Conversion from Cantor sequence to ternary signed-digit encoding

\begin{code}
πŸšβ†’πŸ› : 𝟚 β†’ πŸ›
πŸšβ†’πŸ› β‚€ = βˆ’1
πŸšβ†’πŸ› ₁ = +1

_↑ : 𝟚ᴺ β†’ πŸ›α΄Ί
_↑ = map πŸšβ†’πŸ›

_  : 𝟚ᴺ Γ— 𝟚ᴺ β†’ πŸ›α΄Ί Γ— πŸ›α΄Ί
_  (Ξ± , Ξ²) = Ξ± ↑ , Ξ² ↑

↑-ucontinuous : f-ucontinuous 𝟚ᴺ-ClosenessSpace πŸ›α΄Ί-ClosenessSpace _↑
↑-ucontinuous
 = seq-f-ucontinuousΒΉ-to-closeness
    𝟚-is-discrete πŸ›-is-discrete
    _↑ (map-ucontinuous' πŸšβ†’πŸ›)

 -ucontinuous
 : f-ucontinuous πŸšα΄ΊΓ—πŸšα΄Ί-ClosenessSpace πŸ›α΄ΊΓ—πŸ›α΄Ί-ClosenessSpace _ 
 -ucontinuous ϡ
 = Ο΅
 , (Ξ» x₁ xβ‚‚ Cx₁xβ‚‚
 β†’ Γ—-C-combine πŸ›α΄Ί-ClosenessSpace πŸ›α΄Ί-ClosenessSpace
    (pr₁ (x₁  )) (pr₁ (xβ‚‚  ))
    (prβ‚‚ (x₁  )) (prβ‚‚ (xβ‚‚  ))
    Ο΅
    (prβ‚‚ (↑-ucontinuous Ο΅) (pr₁ x₁) (pr₁ xβ‚‚)
     (Γ—-C-left 𝟚ᴺ-ClosenessSpace 𝟚ᴺ-ClosenessSpace
      (pr₁ x₁) (pr₁ xβ‚‚)
      (prβ‚‚ x₁) (prβ‚‚ xβ‚‚)
      Ο΅ Cx₁xβ‚‚))
     (prβ‚‚ (↑-ucontinuous Ο΅) (prβ‚‚ x₁) (prβ‚‚ xβ‚‚)
      (Γ—-C-right 𝟚ᴺ-ClosenessSpace 𝟚ᴺ-ClosenessSpace
       (pr₁ x₁) (pr₁ xβ‚‚)
       (prβ‚‚ x₁) (prβ‚‚ xβ‚‚)
       Ο΅ Cx₁xβ‚‚)))

↑-pred : decidable-uc-predicate 𝓦 πŸ›α΄Ί-ClosenessSpace
       β†’ decidable-uc-predicate 𝓦 𝟚ᴺ-ClosenessSpace
↑-pred ((p , d) , Ο•)
 = (p ∘ _↑ , d ∘ _↑)
 , p-ucontinuous-comp 𝟚ᴺ-ClosenessSpace πŸ›α΄Ί-ClosenessSpace
    _↑ ↑-ucontinuous p Ο•

 -pred : decidable-uc-predicate 𝓦 πŸ›α΄ΊΓ—πŸ›α΄Ί-ClosenessSpace
        β†’ decidable-uc-predicate 𝓦 πŸšα΄ΊΓ—πŸšα΄Ί-ClosenessSpace
 -pred ((p , d) , Ο•)
 = (p ∘ _  , d ∘ _ )
 , p-ucontinuous-comp πŸšα΄ΊΓ—πŸšα΄Ί-ClosenessSpace πŸ›α΄ΊΓ—πŸ›α΄Ί-ClosenessSpace
    _   -ucontinuous p Ο•
\end{code}