Chuangjie Xu, 2013

\begin{code}

module HAomega where

open import MiniLibrary
open import Sequence
open import Space
open import Space-product
open import Space-coproduct
open import Space-exponential
open import Space-discrete
open import Inequality
open import UniformContinuity
open import Fan

\end{code}

Vectors, to represent contexts:

\begin{code}

infixl 10 _₊_

data _^_ (X : Set) :   Set where
 ε : X ^ 0
 _₊_ : {n : }  X ^ n  X  X ^ (succ n)

\end{code}

Index function of vectors

\begin{code}

_₍_₎ : {X : Set} {n : }  X ^ n  Fin n  X
(xs  x)  zero    = x
(xs  x)  succ i  = xs  i 

\end{code}

Types of system T:

\begin{code}

infixl 11 _⊠_
infixr 10 _⇨_

data Ty : Set where
  : Ty
  : Ty
  : Ty
  : Ty
 _⊠_ : Ty  Ty  Ty
 _⊞_ : Ty  Ty  Ty
 _⇨_ : Ty  Ty  Ty

\end{code}

Contexts of system T

\begin{code}

Cxt :   Set
Cxt n = Ty ^ n

\end{code}

Terms in context of system T

\begin{code}

infixl 10 _∙_
infixl 10 _ˣ_

data Tm : {n : }  Cxt n  Ty  Set where
 EMPTY : {n : }{Γ : Cxt n}{σ : Ty}      Tm Γ (  σ)
      : {n : }{Γ : Cxt n}              Tm Γ 
 UNIT  : {n : }{Γ : Cxt n}{σ : Ty}      Tm Γ (σ  )
      : {n : }{Γ : Cxt n}              Tm Γ 
      : {n : }{Γ : Cxt n}              Tm Γ 
 IF    : {n : }{Γ : Cxt n}{σ : Ty}      Tm Γ (σ  σ    σ)
 ZERO  : {n : }{Γ : Cxt n}              Tm Γ 
 SUCC  : {n : }{Γ : Cxt n}              Tm Γ (  )
 REC   : {n : }{Γ : Cxt n}{σ : Ty}      Tm Γ (σ  (  σ  σ)    σ)
 VAR   : {n : }{Γ : Cxt n}              (i : Fin n)  Tm Γ (Γ  i )
 _ˣ_   : {n : }{Γ : Cxt n}{σ τ : Ty}    Tm Γ σ  Tm Γ τ  Tm Γ (σ  τ)
 PRJ₀  : {n : }{Γ : Cxt n}{σ τ : Ty}    Tm Γ ((σ  τ)  σ)
 PRJ₁  : {n : }{Γ : Cxt n}{σ τ : Ty}    Tm Γ ((σ  τ)  τ)
 IN₀   : {n : }{Γ : Cxt n}{σ τ : Ty}    Tm Γ (σ  (σ  τ))
 IN₁   : {n : }{Γ : Cxt n}{σ τ : Ty}    Tm Γ (τ  (σ  τ))
 CASE  : {n : }{Γ : Cxt n}{σ τ δ : Ty}  Tm Γ ((σ  δ)  (τ  δ)  (σ  τ)  δ)
 LAM   : {n : }{Γ : Cxt n}{σ τ : Ty}    Tm (Γ  σ) τ  Tm Γ (σ  τ)
 _∙_   : {n : }{Γ : Cxt n}{σ τ : Ty}    Tm Γ (σ  τ)  Tm Γ σ  Tm Γ τ

\end{code}

Interpretation of types in C-spaces:

\begin{code}

【_】 : Ty  Space
   = ∅Space
   = ⒈Space
   = ₂Space
   = ℕSpace
 σ  τ  =  σ    τ 
 σ  τ  =  σ    τ 
 σ  τ  =  σ    τ 

\end{code}

Interpretation of contexts in C-spaces:

\begin{code}

〖_〗 : {n : }  Cxt n  Space
 ε       = ⒈Space
 σs  σ  =  σs    σ 

\end{code}

Interpretation of the index function in C-spaces, which is continuous:

\begin{code}

_﹝_﹞ : {n : }{Γ : Cxt n}  U  Γ   (i : Fin n)  U  Γ  i  
_﹝_﹞ {0}      {ε}              ()
_﹝_﹞ {succ n} {σs  σ} (xs , x) zero     = x
_﹝_﹞ {succ n} {σs  σ} (xs , x) (succ i) = xs  i 

﹝﹞-is-continuous : {n : }{Γ : Cxt n}{i : Fin n}  continuous { Γ } { Γ  i  }  xs  xs  i )
﹝﹞-is-continuous {0}      {ε}     {()}
﹝﹞-is-continuous {succ n} {Γ  σ} {zero}   = λ _  ∧-elim₁
﹝﹞-is-continuous {succ n} {Γ  σ} {succ i} = λ p pΓσ  IH (π₀  p) (∧-elim₀ pΓσ)
 where
  IH : continuous { Γ } { Γ  i  }  xs  xs  i )
  IH = ﹝﹞-is-continuous {n} {Γ} {i}

\end{code}

Interpretations of terms in context, which are continuous functions from the
interpretations of contexts to the ones of types:

\begin{code}

⟦_⟧ : {n : }{Γ : Cxt n}{σ : Ty}  Tm Γ σ  Map  Γ   σ 
 EMPTY {n} {Γ} {σ}         =  _  continuous-empty { σ }) ,
                               _ _ p p⓪ _ _  π₁ (continuous-empty { σ }) p p⓪)
                          =  _  ) ,  _ _  )
 UNIT {n} {Γ} {σ}          =  _  continuous-unit { σ }) ,
                               _ _ p  _ _  π₁ (continuous-unit { σ }) p )
                          =  _  ) ,  _ _  0 ,  _ _ _  refl) ,  _ _  ≤-zero))
                          =  _  ) ,  _ _  0 ,  _ _ _  refl) ,  _ _  ≤-zero))
 IF {n} {Γ} {σ}            =  _  continuous-if { σ }) ,
                               _ _ p  _ _  π₁ (continuous-if { σ }) p )
 ZERO                      =  _  0) ,  _ _  0 ,  _ _ _  refl) ,  _ _  ≤-zero))
 SUCC                      =  _  continuous-succ) ,  _ _ p pN _ _  π₁ continuous-succ p pN)
 REC {n} {Γ} {σ}           =  _  continuous-rec { σ }) ,
                               _ _ p  _ _  π₁ (continuous-rec { σ }) p )
 VAR {n} {Γ} i             =  ρ  ρ  i ) , ﹝﹞-is-continuous {n} {Γ} {i}
 M ˣ N                     =  ρ  π₀  M  ρ , π₀  N  ρ) ,
                               p   π₁  M  p  , π₁  N  p )
 PRJ₀ {n} {Γ} {σ} {τ}      =  _  continuous-π₀ { σ } { τ }) ,
                               _ _ p pστ _ _  π₁ (continuous-π₀ { σ } { τ }) p pστ)
 PRJ₁ {n} {Γ} {σ} {τ}      =  _  continuous-π₁ { σ } { τ }) ,
                               _ _ p pστ _ _  π₁ (continuous-π₁ { σ } { τ }) p pστ)
 IN₀ {n} {Γ} {σ} {τ}       =  _  continuous-in₀ { σ } { τ }) ,
                               _ _ p  _ _  π₁ (continuous-in₀ { σ } { τ }) p )
 IN₁ {n} {Γ} {σ} {τ}       =  _  continuous-in₁ { σ } { τ }) ,
                               _ _ p  _ _  π₁ (continuous-in₁ { σ } { τ }) p )
 CASE {n} {Γ} {σ} {τ} {δ}  =  _  continuous-case { σ } { τ } { δ }) ,
                               _ _ p pσδ _ _  π₁ (continuous-case { σ } { τ } { δ }) p pσδ)
 LAM {n} {Γ} {σ} {τ} M     = f , cts-f
 where
  f : U  Γ   U  σ  τ 
  f xs = g , cts-g
   where
    g : U  σ   U  τ 
    g x = π₀  M  (xs , x)
    cts-g : continuous { σ } { τ } g
    cts-g p  = π₁  M  q qΓσ
     where
      q : ₂ℕ  U  Γ  σ 
      q α = (xs , p α)
      qΓσ : q  Probe  Γ  σ 
      qΓσ = cond₀  Γ   _  xs)  _ _  refl) , 
  cts-f : continuous { Γ } { σ  τ } f
  cts-f p  q  t uc = π₁  M  r rΓσ
   where
    r : ₂ℕ  U  Γ  σ 
    r α = (p(t α) , q α)
    rΓσ : r  Probe  Γ  σ 
    rΓσ = cond₁  Γ  t uc p  , 
 _∙_ {n} {Γ} {σ} {τ} M N  = f , cts-f
 where
  f : U  Γ   U  τ 
  f xs = π₀ (π₀  M  xs) (π₀  N  xs)
  cts-f : continuous { Γ } { τ } f
  cts-f p  = π₁  M  p  ((π₀  N )  p) (π₁  N  p ) id Lemma[id-UC]

\end{code}

HAω formulas:

\begin{code}

infix  9 _⌸_
infixl 8 _⍓_
infixr 7 _⍈_
infixr 6 Ā_‣_
infixr 6 Ē_‣_

data HAω : {n : }  Cxt n  Set where
 _⌸_  : {n : }{Γ : Cxt n}{σ : Ty}  Tm Γ σ  Tm Γ σ  HAω Γ
 _⍓_  : {n : }{Γ : Cxt n}  HAω Γ  HAω Γ  HAω Γ
 _⍌_  : {n : }{Γ : Cxt n}  HAω Γ  HAω Γ  HAω Γ
 _⍈_  : {n : }{Γ : Cxt n}  HAω Γ  HAω Γ  HAω Γ
 Ā_‣_ : {n : }{Γ : Cxt n}  (σ : Ty)  HAω (Γ  σ)  HAω Γ
 Ē_‣_ : {n : }{Γ : Cxt n}  (σ : Ty)  HAω (Γ  σ)  HAω Γ

\end{code}

Translations of formulas to T types:

\begin{code}

∣_∣ : {n : }{Γ : Cxt n}  HAω Γ  Ty
 M  N    = 
 φ  ψ    =  φ    ψ 
 φ  ψ    =  φ    ψ 
 φ  ψ    =  φ    ψ 
 Ā σ  φ  = σ   φ 
 Ē σ  φ  = σ   φ 

\end{code}

Realizability relation:

\begin{code}

infix 50 _is-realized-by_

_is-realized-by_ : {n : }{Γ : Cxt n}  (φ : HAω Γ)  U  Γ  × U   φ    Set
(M  N)   is-realized-by (ρ , )     = π₀  M  ρ  π₀  N  ρ
(φ  ψ)   is-realized-by (ρ , x , y) = φ is-realized-by (ρ , x)  ψ is-realized-by (ρ , y)
(φ  ψ)   is-realized-by (ρ , in₀ x) = φ is-realized-by (ρ , x)
(φ  ψ)   is-realized-by (ρ , in₁ y) = ψ is-realized-by (ρ , y)
(φ  ψ)   is-realized-by (ρ , f)     = ∀(x : U   φ  )  φ is-realized-by (ρ , x)  ψ is-realized-by (ρ , π₀ f x)
(Ā σ  φ) is-realized-by (ρ , f)     = ∀(x : U  σ )  φ is-realized-by ((ρ , x) , π₀ f x)
(Ē σ  φ) is-realized-by (ρ , x , y) = φ is-realized-by ((ρ , x) , y)

_is-realizable : {n : }{Γ : Cxt n}  HAω Γ  Set
_is-realizable {n} {Γ} φ =  \(w : U  Γ  × U   φ  )  φ is-realized-by w

\end{code}

Auxiliaries to express UC in HAω:

EQ returns ⊤ iff the two input booleans are equal. And eq is the set
interpretation of EQ.

\begin{code}

EQ : {n : }{Γ : Cxt n}  Tm Γ   Tm Γ   Tm Γ 
EQ B₀ B₁ = IF  (IF      B₁)  B₁  B₀

eq :     
eq b₀ b₁ = if (if   b₁) b₁ b₀

Lemma[eq] : (b₀ b₁ : )  eq b₀ b₁    b₀  b₁
Lemma[eq]   refl = refl
Lemma[eq]   ()
Lemma[eq]   ()
Lemma[eq]   refl = refl

\end{code}

MIN returns the minimal one of the two input booleans. And min is the set
interpretation of MIN.

\begin{code}

MIN : {n : }{Γ : Cxt n}  Tm Γ   Tm Γ   Tm Γ 
MIN B₀ B₁ = IF    B₁  B₀

min :     
min b₀ b₁ = if  b₁ b₀

Lemma[min] : (b₀ b₁ : )  min b₀ b₁    b₀    b₁  
Lemma[min]   ()
Lemma[min]   ()
Lemma[min]   ()
Lemma[min]   refl = ∧-intro refl refl

\end{code}

To realize UC, we work with the context

  Γ  ≡  F : (Ⓝ ⇨ ②) ⇨ Ⓝ , n : Ⓝ , A : Ⓝ ⇨ ② , B : Ⓝ ⇨ ②

Here are the projection functions for the context Γ.

\begin{code}

ν₀ : {n : }{Γ : Cxt (succ n)} 
     Tm Γ (Γ  zero )
ν₀ = VAR zero
ν₁ : {n : }{Γ : Cxt (succ (succ n))} 
     Tm Γ (Γ  succ zero )
ν₁ = VAR (succ zero)
ν₂ : {n : }{Γ : Cxt (succ (succ (succ n)))} 
     Tm Γ (Γ  succ (succ zero) )
ν₂ = VAR (succ (succ zero))
ν₃ : {n : }{Γ : Cxt (succ (succ (succ (succ n))))} 
     Tm Γ (Γ  succ (succ (succ zero)) )
ν₃ = VAR (succ (succ (succ zero)))
ν₄ : {n : }{Γ : Cxt (succ (succ (succ (succ (succ n)))))} 
     Tm Γ (Γ  succ (succ (succ (succ zero))) )
ν₄ = VAR (succ (succ (succ (succ zero))))
ν₅ : {n : }{Γ : Cxt (succ (succ (succ (succ (succ (succ n))))))} 
     Tm Γ (Γ  succ (succ (succ (succ (succ zero)))) )
ν₅ = VAR (succ (succ (succ (succ (succ zero)))))

Γ : Cxt 4
Γ = ε  ((  )  )    (  )  (  )

F : Tm Γ ((  )  )
F = ν₃

N : Tm Γ 
N = ν₂

A B : Tm Γ (  )
A = ν₁
B = ν₀

\end{code}

A' and B' are the weakening of sequences A and B in context Γ.

\begin{code}

A' B' : Tm (Γ    ) (  )
A' = ν₃
B' = ν₂

\end{code}

The "agree with" function for the two sequences A and B.
If they are equal up to the first N positions, then A≣[N]B is ⊤.

\begin{code}

A≣[N]B : Tm Γ 
A≣[N]B = REC    (LAM (LAM (MIN (EQ (A'  ν₁) (B'  ν₁)) ν₀)))  N

\end{code}

The axiom of uniform continuity and its realizer:

\begin{code}

Axiom[Uniform-Continuity] : HAω ε
Axiom[Uniform-Continuity] =
   Ā (  )        Ē       Ā         Ā          A≣[N]B      F  A  F  B
-- ∀ f : (Ⓝ ⇨ ②) ⇨ Ⓝ . ∃ n : Ⓝ . ∀ α : Ⓝ ⇨ ② . ∀ β : Ⓝ ⇨ ② .   α ≣[n] β   →    f α ≡ f β


Theorem : Axiom[Uniform-Continuity] is-realizable
Theorem = ∃-intro ( , e) prf
 where
  e : U (((ℕSpace  ₂Space)  ℕSpace)  (ℕSpace  ((ℕSpace  ₂Space)  (ℕSpace  ₂Space)  ⒈Space  ⒈Space)))
  e = g , cts-g
   where
    g : Map (ℕSpace  ₂Space) ℕSpace   × Map (ℕSpace  ₂Space) ((ℕSpace  ₂Space)  ⒈Space  ⒈Space)
    g f = π₀ fan f , g₀ , cts-g₀
     where
      g₀ : Map ℕSpace ₂Space  Map (ℕSpace  ₂Space) (⒈Space  ⒈Space)
      g₀ α = g₁ , cts-g₁
       where
        g₁ : Map ℕSpace ₂Space  Map ⒈Space ⒈Space
        g₁ β =  _  ) ,  _ _  )
        cts-g₁ : continuous {ℕSpace  ₂Space} {⒈Space  ⒈Space} g₁
        cts-g₁ _ _ _ _ _ _ = 
      cts-g₀ : continuous {ℕSpace  ₂Space} {(ℕSpace  ₂Space)  ⒈Space  ⒈Space} g₀
      cts-g₀ _ _ _ _ _ _ _ _ _ _ = 
    cts-g : continuous {(ℕSpace  ₂Space)  ℕSpace}
                       {ℕSpace  ((ℕSpace  ₂Space)  (ℕSpace  ₂Space)  ⒈Space  ⒈Space)} g
    cts-g p pP = π₁ fan p pP ,  _ _ _ _ _ _ _ _ _ _ _ _  )
  prf : Axiom[Uniform-Continuity] is-realized-by ( , e)
  prf f = prf'
   where
    n : 
    n = π₀ (π₀ e f)
    prf' : ∀(α β : Map ℕSpace ₂Space) 
           ∀(x : )  (A≣[N]B  ) is-realized-by ((((( , f) , n) , α) , β) , x) 
           π₀ f α  π₀ f β
   -- i.e. π₀ ⟦ F ∙ A ⟧ ((((⋆ , f) , n) , α) , β) ≡ π₀ ⟦ F ∙ B ⟧ ((((⋆ , f) , n) , α) , β)
    prf' α β  EN = fan-behaviour f α β en
     where
      ρ : U  Γ 
      ρ = (((( , f) , n) , α) , β)
      g :     
      g n b = π₀ (π₀ (π₀  LAM (LAM (MIN (EQ (A'  ν₁) (B'  ν₁)) ν₀))  ρ) n) b
      lemma : (k : )  rec  g k    π₀ α ≣[ k ] π₀ β
      lemma 0        refl = ≣[zero]
      lemma (succ k) esk  = ≣[succ] IH claim₁
       where
        ek : rec  g k  
        ek = ∧-elim₁ (Lemma[min] (eq (π₀ α k) (π₀ β k)) (rec  g k)  esk)
        IH : π₀ α ≣[ k ] π₀ β
        IH = lemma k ek
        claim₀ : eq (π₀ α k) (π₀ β k)  
        claim₀ = ∧-elim₀ (Lemma[min] (eq (π₀ α k) (π₀ β k)) (rec  g k) esk)
        claim₁ : π₀ α k  π₀ β k
        claim₁ = Lemma[eq] (π₀ α k) (π₀ β k) claim₀
      en : π₀ α ≣[ n ] π₀ β
      en = lemma n EN

\end{code}