======================
 =                    =
 =  §0  Mini library  =
 =                    =
 ======================

    Chuangjie Xu

A small library for the Agda development of the paper

  A Gentzen-style monadic translation of Gödel's System T

by Chuangjie Xu.

\begin{code}

{-# OPTIONS --without-K --safe #-}

module Preliminaries where

\end{code}

■ Identity types

\begin{code}

infix 2 _≡_

data _≡_ {} {A : Set } (a : A) : A  Set  where
 refl : a  a

ap :  { ℓ'} {A : Set } {B : Set ℓ'} (f : A  B) {x y : A}  x  y  f x  f y
ap f refl = refl

ap² :  { ℓ' ℓ''} {A : Set } {B : Set ℓ'} {C : Set ℓ''}
     (f : A  B  C) {x y : A} {u v : B}  x  y  u  v  f x u  f y v
ap² f refl refl = refl

sym :  {} {A : Set } {x y : A}  x  y  y  x
sym refl = refl

trans :  {} {A : Set } {x y z : A}
       x  y  y  z  x  z
trans refl p = p

transport :  { ℓ'} {A : Set } (P : A  Set ℓ') {x y : A}
           x  y  P x  P y
transport P refl p = p

infix  1 begin_
infix  3 _∎
infixr 2 _≡⟨_⟩_

begin_ :  {} {A : Set } {x y : A}  x  y  x  y
begin x≡y = x≡y

_≡⟨_⟩_ :  {} {A : Set } (x {y z} : A)  x  y  y  z  x  z
_ ≡⟨ x≡y  y≡z = trans x≡y y≡z

_∎ :  {} {A : Set } (x : A)  x  x
_∎ _ = refl

\end{code}

■ More operations and types

\begin{code}

--
-- function composition
--
_∘_ : {A : Set} {B : A  Set} {C : {a : A}  B a  Set}
     ({a : A} (b : B a)  C b)  (f : (a : A)  B a)  (a : A)  C (f a)
f  g = λ a  f (g a)

--
-- (dependent) pairs
--
record Σ {A : Set} (B : A  Set) : Set where
 constructor _,_
 field
  pr₁ : A
  pr₂ : B pr₁

open Σ public

infixr 1 _×_ _,_

_×_ : Set  Set  Set
A × B = Σ \(_ : A)  B

--
-- coproduct
--
data _+_ (A B : Set) : Set where
 inl : A  A + B
 inr : B  A + B

case : ∀{} {A B : Set} {C : Set }
      (A  C)  (B  C)  A + B  C
case f g (inl a) = f a
case f g (inr b) = g b

--
-- the empty type
--
data 𝟘 : Set where

𝟘-elim : {A : Set}  𝟘  A
𝟘-elim ()

¬ : Set  Set
¬ A = A  𝟘

--
-- the unit type
--
data 𝟙 : Set where
  : 𝟙

\end{code}

■ Natural numbers

\begin{code}

data  : Set where
 zero : 
 suc  :   

{-# BUILTIN NATURAL  #-}

rec : {A : Set}  A  (  A  A)    A
rec a f  0      = a
rec a f (suc n) = f n (rec a f n)

ind :  {} {P :   Set }  P 0  (∀ n  P n  P (suc n))   n  P n
ind p f  0      = p
ind p f (suc n) = f n (ind p f n)

not-zero-is-suc : {n : }  ¬ (n  0)  Σ \m  suc m  n
not-zero-is-suc {0}     f = 𝟘-elim (f refl)
not-zero-is-suc {suc n} f = n , refl

infix 5 _≤_ _<_ _≥_ _>_

data _≤_ :     Set where
 ≤zero : {n : }  0  n
 ≤suc  : {n m : }  n  m  suc n  suc m

_<_ _≥_ _>_ :     Set
n < m = suc n  m
n  m = m  n
n > m = m < n

≤refl : {n : }  n  n
≤refl {0}     = ≤zero
≤refl {suc n} = ≤suc ≤refl

≤refl' : {n m : }  n  m  n  m
≤refl' refl = ≤refl

≤trans : {n m l : }  n  m  m  l  n  l
≤trans  ≤zero    _       = ≤zero
≤trans (≤suc r) (≤suc s) = ≤suc (≤trans r s)

≤-cases : {n m : }  n  m  (n  m) + (n < m)
≤-cases {0}     {0}      ≤zero   = inl refl
≤-cases {0}     {suc m}  ≤zero   = inr (≤suc ≤zero)
≤-cases {suc n} {suc m} (≤suc r) with ≤-cases r
... | inl refl = inl refl
... | inr s    = inr (≤suc s)

≤suc' : {n m : }  n  m  n  suc m
≤suc'  ≤zero   = ≤zero
≤suc' (≤suc r) = ≤suc (≤suc' r)

\end{code}

■ Infinite sequences

\begin{code}

_ᴺ : Set  Set
A  =   A

ℕᴺ : Set
ℕᴺ =  

 : ℕᴺ
 = λ i  0

infixr 30 _✶_

_✶_ : {A : Set}  A  A   A 
a  u = rec a  i _  u i)
-- (a ✶ u)  0      = a
-- (a ✶ u) (suc i) = u i

head : {A : Set}  A   A
head α = α 0

tail : {A : Set}  A   A 
tail α i = α (suc i)

data _≡[_]_ {A : Set} : A     A   Set where
 ≡[zero] : {α β : A }  α ≡[ 0 ] β
 ≡[suc]  : {α β : A } {n : }  head α  head β  tail α ≡[ n ] tail β  α ≡[ suc n ] β

≡[≤] : {A : Set} {α β : A } {n m : }
      α ≡[ n ] β  m  n  α ≡[ m ] β
≡[≤]  en            ≤zero   = ≡[zero]
≡[≤] (≡[suc] e en) (≤suc r) = ≡[suc] e (≡[≤] en r)

≡[]-< : {A : Set} {α β : A } {n i : }
       α ≡[ n ] β  suc i  n  α i  β i
≡[]-< (≡[suc] e en) (≤suc ≤zero)    = e
≡[]-< (≡[suc] e en) (≤suc (≤suc r)) = ≡[]-< en (≤suc r)

≡[]-refl : {n : } {A : Set} {α : A }
          α ≡[ n ] α
≡[]-refl {0}     = ≡[zero]
≡[]-refl {suc n} = ≡[suc] refl ≡[]-refl

\end{code}