\begin{code}
module TContMonad where
open import Preliminaries
open import T hiding (Num)
\end{code}\begin{code}
data TΩ (Γ : Cxt) : Ty → Set where
Var : {σ : Ty} → ∥ σ ∈ Γ ∥ → TΩ Γ σ
Lam : {σ τ : Ty} → TΩ (Γ ₊ σ) τ → TΩ Γ (σ ⇾ τ)
_·_ : {σ τ : Ty} → TΩ Γ (σ ⇾ τ) → TΩ Γ σ → TΩ Γ τ
Zero : TΩ Γ ι
Succ : TΩ Γ (ι ⇾ ι)
Rec : {σ : Ty} → TΩ Γ (σ ⇾ (ι ⇾ σ ⇾ σ) ⇾ ι ⇾ σ)
Ω : TΩ Γ (ι ⇾ ι)
\end{code}\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} → TΩ Γ ρ → [ Γ ]ˣ → ℕᴺ → 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}\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}\begin{code}
val : TΩ ε ι → ℕᴺ → ℕ
val t α = pr₁ ([ t ] ⋆ α ε)
maxᴸ : List ℕ → ℕ
maxᴸ ε = 0
maxᴸ (i :: u) = max i (maxᴸ u)
# : List ℕ → ℕ
# ε = 0
# (i :: u) = succ (maxᴸ (i :: u))
mod : TΩ ε ι → ℕᴺ → ℕ
mod t α = # (pr₂ ([ t ] ⋆ α ε))
\end{code}\begin{code}
Num : ℕ → {Γ : Cxt} → TΩ Γ ι
Num 0 = Zero
Num (succ k) = Succ · Num k
t₀ : TΩ ε ι
t₀ = Num 4
result₀ : mod t₀ 0ʷ ≡ 0
× mod t₀ δ ≡ 0
× mod t₀ succ ≡ 0
× mod t₀ (12 ✶ 67 ✶ 8 ✶ 99 ✶ 3 ✶ 0ʷ) ≡ 0
result₀ = refl , refl , refl , refl
t₅ : 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₅ 0ʷ ≡ 18
× mod t₅ δ ≡ 18
× mod t₅ succ ≡ 58
× mod t₅ (12 ✶ 67 ✶ 8 ✶ 99 ✶ 3 ✶ 0ʷ) ≡ 68
result₅ = refl , refl , refl , refl
\end{code}