===========================================================
  =                                                         =
  =  Appendix I. Continuity via a monadic translation of T  =
  =                                                         =
  ===========================================================

     Chuangjie Xu, 2018


Coquand and Jaber [CJ12] extend dependent type theory with a new
constant f for a generic element, and then decorate its operational
semantics with forcing information to keep track of approximation
information about f as the computations proceed.  Their algorithm
(restricted to system T) to extract continuity information can be
given by a monadic translation from T (extended with a generic
element) to itself, which is an instance of the monadic translation
introduced in [Pow18].  Instead of a syntactic translation, we have a
monadic interpretation into Agda.  We provide only examples of
computing moduli of continuity but no formal proofs of continuity.


Reference.

[CJ12]  T. Coquand and G. Jaber.  A computational interpretation of
        forcing in type theory.  In Epistemology versus Ontology,
        volume 27, pages 203–213.  Springer Netherlands, 2012.

[Pow18] T. Powell.  A functional interpretation with state.  Accepted
        for Proceedings of Logic in Computer Science (LICS 2018), 2018.

\begin{code}

module TContMonad where

open import Preliminaries
open import T hiding (Num)

\end{code}

■ Extension of system T

We firstly extend system T with a generic element Ω.

\begin{code}

data  (Γ : Cxt) : Ty  Set where
 Var  : {σ : Ty}   σ  Γ    Γ σ
 Lam  : {σ τ : Ty}   (Γ  σ) τ   Γ (σ  τ)
 _·_  : {σ τ : Ty}   Γ (σ  τ)   Γ σ   Γ τ
 Zero :  Γ ι
 Succ :  Γ (ι  ι)
 Rec  : {σ : Ty}   Γ (σ  (ι  σ  σ)  ι  σ)
     :  Γ (ι  ι)

\end{code}

■ A monadic interpretation of TΩ

\begin{code}

module MonadicModel (M : Set  Set)
                    (η : {A : Set}  A  M A)
                    (_*_ : {A B : Set}  M (A  M B)  M A  M B)
                    ([Ω] : ℕᴺ    M )where

 [_]ʸ : Ty  Set
 [ ι  = 
 [ σ  τ  = [ σ   M [ τ 

 recᴹ : {ρ : Ty}  [ ρ   [ ι  ρ  ρ     M [ ρ 
 recᴹ b g  0       = η b
 recᴹ b g (succ n) = g n * recᴹ b g n

 [_]ˣ : Cxt  Set
 [ ε      = 𝟙
 [ Γ  σ  = [ Γ  × [ σ 

 varᴹ : {Γ : Cxt} {σ : Ty}  [ Γ    σ  Γ   [ σ 
 varᴹ (γ , x)  zero    = x
 varᴹ (γ , x) (succ i) = varᴹ γ i

 [_] : {Γ : Cxt} {ρ : Ty}   Γ ρ  [ Γ   ℕᴺ  M [ ρ 
 [ Var i ] ρ _ = η (varᴹ ρ i)
 [ Lam t ] ρ α = η  x  [ t ] (ρ , x) α)
 [ f · a ] ρ α = [ f ] ρ α * [ a ] ρ α
 [ Zero ]  _ _ = η 0
 [ Succ ]  _ _ = η  n  η (succ n))
 [ Rec ]   _ _ = η  b  η  g  η  n  recᴹ b g n)))
 [  ]     _ α = η ([Ω] α)

\end{code}

■ A combination of the state monad and the list monad

\begin{code}

data List (A : Set) : Set where
 ε    : List A
 _::_ : A  List A  List A

M : Set  Set
M A = List   A × List 

η : {A : Set}  A  M A
η a = λ u  (a , u)

_*_ : {A B : Set}  M (A  M B)  M A  M B
f * a = λ u  pr₁ (f u) (pr₁ (a (pr₂ (f u)))) (pr₂ (a (pr₂ (f u))))

[Ω] : ℕᴺ    M 
[Ω] α n u = (α n , n :: u)

open MonadicModel M η _*_ [Ω]

\end{code}

■ Moduli of continuity

Given a closed term t : ι in TΩ, we have its value "val t" and the
modulus of continuity "mod t" obtained as follows.

\begin{code}

val :  ε ι  ℕᴺ  
val t α = pr₁ ([ t ]  α ε)

maxᴸ : List   
maxᴸ  ε       = 0
maxᴸ (i :: u) = max i (maxᴸ u)

# : List   
#  ε       = 0
# (i :: u) = succ (maxᴸ (i :: u))

mod :  ε ι  ℕᴺ  
mod t α = # (pr₂ ([ t ]  α ε))

\end{code}

■ Computation experiments

\begin{code}

Num :   {Γ : Cxt}   Γ ι
Num  0       = Zero
Num (succ k) = Succ · Num k

t₀ :  ε ι
t₀ = Num 4

result₀ :  mod t₀   0
        ×  mod t₀ δ  0
        ×  mod t₀ succ  0
        ×  mod t₀ (12  67  8  99  3  )  0
result₀ = refl , refl , refl , refl


t₅ :  ε ι
t₅ = Rec · ( · (Rec · ( · (Rec · ( · Num 17) · Lam  · ( · Num 17)))
                     · Lam 
                     · (Rec · (Rec · ( · ( · Num 2)) · Lam Succ · ( · Num 3))
                            · Lam 
                            · (Rec · ( · Num 1) · Lam 
                                   · (Rec · ( · Num 2) · Lam Succ · ( · Num 3))))))
         · (Lam )
         · ( · Num 2)

result₅ :  mod t₅   18
        ×  mod t₅ δ  18
        ×  mod t₅ succ  58
        ×  mod t₅ (12  67  8  99  3  )  68
result₅ = refl , refl , refl , refl

\end{code}