============================
= =
= Example: bar recursion =
= =
============================
Chuangjie Xu, July 2019
Here we present only the construction of general-bar-recursion
functionals via the translation. The correctness proof is available at
http://cj-xu.github.io/agda/BRCT/BRCT.html
References.
β‘ Paulo Oliva and Silvia Steila. A direct proof of Schwichtenberg's
bar recursion closure theorem. The Journal of Symbolic Logic 83 (1)
(2018) 70β83.
\begin{code}
{-# OPTIONS --without-K --safe #-}
open import Preliminaries
open import T
module BarRecursion (Ο : Set) where
\end{code}
β Preliminaries
\begin{code}
data π : Set where
π π : π
minΒ² : π β π β π
minΒ² π b = π
minΒ² π b = b
Le : β β β β π
Le n 0 = π
Le 0 (succ m) = π
Le (succ n) (succ m) = Le n m
data _* (A : Set) : Set where
β¨β© : A *
_,_ : A β A * β A *
β£_β£ : {A : Set} β A * β β
β£ β¨β© β£ = 0
β£ _ , u β£ = succ β£ u β£
_βΆ_ : {A : Set} β A * β A β A *
β¨β© βΆ y = y , β¨β©
(x , xs) βΆ y = x , (xs βΆ y)
_^ : β * β βα΄Ί
(β¨β© ^) i = 0
((n , ns) ^) 0 = n
((n , ns) ^) (succ i) = (ns ^) i
β€-dec : (n m : β) β (n > m) + (n β€ m)
β€-dec 0 m = inr β€zero
β€-dec (succ n) 0 = inl (β€succ β€zero)
β€-dec (succ n) (succ m) with β€-dec n m
β€-dec (succ n) (succ m) | inl r = inl (β€succ r)
β€-dec (succ n) (succ m) | inr r = inr (β€succ r)
_-_ : β β β β β
0 - m = 0
succ n - 0 = succ n
succ n - succ m = n - m
\end{code}
β A nucleus for general-bar-recursion functionals
\begin{code}
βα΅ : Set
βα΅ = (βα΄Ί β β) Γ (β * β π) Γ ((β * β Ο) β (β * β (β β Ο) β Ο) β β * β Ο)
V : βα΅ β βα΄Ί β β
V = prβ
S : βα΅ β β * β π
S = prβ β prβ
B : βα΅ β (β * β Ο) β (β * β (β β Ο) β Ο) β β * β Ο
B = prβ β prβ
Ξ· : β β βα΅
Ξ· n = (Ξ» Ξ± β n) , (Ξ» s β π) , (Ξ» G H β G)
ΞΊ : (β β βα΅) β βα΅ β βα΅
ΞΊ g x = (Ξ» Ξ± β V (g (V x Ξ±)) Ξ±)
, (Ξ» s β minΒ² (S x s) (S (g (V x (s ^))) s))
, (Ξ» G H β B x (Ξ» s β B (g (V x (s ^))) G H s) H)
βͺ_β«ΚΈ : Ty β Set
βͺ ΞΉ β«ΚΈ = βα΅
βͺ Ο βΎ Ο β«ΚΈ = βͺ Ο β«ΚΈ β βͺ Ο β«ΚΈ
βͺ_β«Λ£ : Cxt β Set
βͺ Ξ΅ β«Λ£ = π
βͺ Ξ β Ο β«Λ£ = βͺ Ξ β«Λ£ Γ βͺ Ο β«ΚΈ
_β_βα΅ : {Ξ : Cxt} {Ο : Ty} β βͺ Ξ β«Λ£ β β₯ Ο β Ξ β₯ β βͺ Ο β«ΚΈ
(_ , a) β zero βα΅ = a
(Ξ³ , _) β succ i βα΅ = Ξ³ β i βα΅
KE : {Ο : Ty} β (β β βͺ Ο β«ΚΈ) β βα΅ β βͺ Ο β«ΚΈ
KE {ΞΉ} g = ΞΊ g
KE {Ο βΎ Ο} g = Ξ» x a β KE (Ξ» z β g z a) x
βͺ_β«α΅ : {Ξ : Cxt} {Ο : Ty} β T Ξ Ο β βͺ Ξ β«Λ£ β βͺ Ο β«ΚΈ
βͺ Var i β«α΅ Ξ³ = Ξ³ β i βα΅
βͺ Lam t β«α΅ Ξ³ = Ξ» a β βͺ t β«α΅ (Ξ³ , a)
βͺ f Β· a β«α΅ Ξ³ = βͺ f β«α΅ Ξ³ (βͺ a β«α΅ Ξ³)
βͺ Zero β«α΅ _ = Ξ· 0
βͺ Succ β«α΅ _ = ΞΊ (Ξ· β succ)
βͺ Rec β«α΅ _ = Ξ» a f β KE (rec a (f β Ξ·))
_α΅ : {Ο : Ty} β T Ξ΅ Ο β βͺ Ο β«ΚΈ
t α΅ = βͺ t β«α΅ β
Ξ¨ : β β (β * β Ο) β (β * β (β β Ο) β Ο) β β * β Ο
Ξ¨ k G H s with β€-dec β£ s β£ k
Ξ¨ k G H s | inl |s|>k = G s
Ξ¨ k G H s | inr |s|β€k = Ο G H (succ k - β£ s β£) s
where
Ο : (β * β Ο) β (β * β (β β Ο) β Ο) β β β β * β Ο
Ο G H 0 = G
Ο G H (succ n) = Ξ» s β H s (Ξ» x β Ο G H n (s βΆ x))
Ξ© : βα΅ β βα΅
Ξ© = ΞΊ (Ξ» n β (Ξ» Ξ± β Ξ± n) , (Ξ» s β Le n β£ s β£) , Ξ¨ n)
π : T Ξ΅ ((ΞΉ βΎ ΞΉ) βΎ ΞΉ)
β β * β π
π Y = S ((Y α΅) Ξ©)
π΅ : T Ξ΅ ((ΞΉ βΎ ΞΉ) βΎ ΞΉ)
β (β * β Ο) β (β * β (β β Ο) β Ο) β β * β Ο
π΅ Y = B ((Y α΅) Ξ©)
\end{code}