Files
Julius FischerandClaude Opus 5.5 d83c61261d
Typecheck / agda (push) Successful in 1m52s
Annotate λ with its domain: `λ[x⦂ A ] e
Preparation for the set-theoretic model, where a λ denotes the graph of
its body over its domain. ⊢-λ uses the annotation as the domain;
reduction also reduces it (ξ-λ₁) and β discards it, so inductive SN and
the backward closure get a side condition for it. All proofs, tests and
Examples/Issue (annotations only) are updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q7dfaHQ3dYSfqcxR7V5UrJ
2026-11-22 21:12:06 +01:00

90 lines
3.5 KiB
Agda
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
module univTypes.Tests.Nat where
-- natrec with a constant motive: plus, times and factorial, typed by
-- ⊢-natrec. Checked by typechecking this file.
open import Data.Nat using (zero; suc)
open import Data.Fin using (zero; suc)
open import Relation.Binary.PropositionalEquality using (refl)
open import univTypes.Specification
-- Constant motive λx. ℕ, and β-conversion between ℕ and P · a.
P : ∀ {n} → Term n
P = `λ[x⦂ `ℕ ] `ℕ
⊢P : ∀ {n} {Γ : Context n} → Γ ⊢ P ⦂ ∀[x⦂ `ℕ ] `Set
⊢P = ⊢-λ (⊢-Set<Setω ⊢-ℕ) ⊢-ℕ
β→ : ∀ {n} {Γ : Context n} {e a : Term n} → Γ ⊢ e ⦂ P · a → Γ ⊢ e ⦂ `ℕ
β→ = ⊢-≈ (mk-≈ `ℕ (↪~*-step (↪~-↪ β-λ) ↪~*-refl) ↪~*-refl)
β← : ∀ {n} {Γ : Context n} {e a : Term n} → Γ ⊢ e ⦂ `ℕ → Γ ⊢ e ⦂ P · a
β← = ⊢-≈ (mk-≈ `ℕ ↪~*-refl (↪~*-step (↪~-↪ β-λ) ↪~*-refl))
-- Non-dependent recursion: natrec P z (λ x ih. b) t, with b : ℕ under x, ih.
rec : ∀ {n} → Term n → Term (suc (suc n)) → Term n → Term n
rec z b t = `natrec P z (`λ[x⦂ `ℕ ] `λ[x⦂ P · ` zero ] b) t
⊢rec : ∀ {n} {Γ : Context n} {z t : Term n} {b : Term (suc (suc n))}
→ Γ ⊢ z ⦂ `ℕ
→ Γ , `ℕ , P · ` zero ⊢ b ⦂ `ℕ
→ Γ ⊢ t ⦂ `ℕ
→ Γ ⊢ rec z b t ⦂ `ℕ
⊢rec ⊢z ⊢b ⊢t =
β→ (⊢-natrec ⊢P (β← ⊢z)
(⊢-λ (⊢-Set<Setω ⊢-ℕ)
(⊢-λ (⊢-Set<Setω (⊢-· ⊢P (⊢-` zero refl))) (β← ⊢b)))
⊢t)
x ih : ∀ {n} → Term (suc (suc n))
x = ` suc zero
ih = ` zero
⊢x : ∀ {n} {Γ : Context n} → Γ , `ℕ , P · ` zero ⊢ x ⦂ `ℕ
⊢x = ⊢-` (suc zero) refl
⊢ih : ∀ {n} {Γ : Context n} → Γ , `ℕ , P · ` zero ⊢ ih ⦂ `ℕ
⊢ih = β→ (⊢-` zero refl)
ℕ→ℕ→ℕ ℕ→ℕ : ∀ {n} → Term n
ℕ→ℕ→ℕ = ∀[x⦂ `ℕ ] ∀[x⦂ `ℕ ] `ℕ
ℕ→ℕ = ∀[x⦂ `ℕ ] `ℕ
app₂ : ∀ {n} {Γ : Context n} {f a b : Term n}
→ Γ ⊢ f ⦂ ℕ→ℕ→ℕ → Γ ⊢ a ⦂ `ℕ → Γ ⊢ b ⦂ `ℕ → Γ ⊢ f · a · b ⦂ `ℕ
app₂ ⊢f ⊢a ⊢b = ⊢-· (⊢-· ⊢f ⊢a) ⊢b
lam₂ : ∀ {n} {Γ : Context n} {e : Term (suc (suc n))}
→ Γ , `ℕ , `ℕ ⊢ e ⦂ `ℕ → Γ ⊢ `λ[x⦂ `ℕ ] `λ[x⦂ `ℕ ] e ⦂ ℕ→ℕ→ℕ
lam₂ ⊢e = ⊢-λ (⊢-Set<Setω ⊢-ℕ) (⊢-λ (⊢-Set<Setω ⊢-ℕ) ⊢e)
-- plus m n = natrec n (λ x ih. suc ih) m
plus : ∀ {n} → Term n
plus = `λ[x⦂ `ℕ ] `λ[x⦂ `ℕ ] rec (` zero) (`suc ih) (` suc zero)
⊢plus : ∀ {n} {Γ : Context n} → Γ ⊢ plus ⦂ ℕ→ℕ→ℕ
⊢plus = lam₂ (⊢rec (⊢-` zero refl) (⊢-suc ⊢ih) (⊢-` (suc zero) refl))
-- times m n = natrec zero (λ x ih. plus n ih) m
times : ∀ {n} → Term n
times = `λ[x⦂ `ℕ ] `λ[x⦂ `ℕ ] rec `zero (plus · ` suc (suc zero) · ih) (` suc zero)
⊢times : ∀ {n} {Γ : Context n} → Γ ⊢ times ⦂ ℕ→ℕ→ℕ
⊢times = lam₂ (⊢rec ⊢-zero (app₂ ⊢plus (⊢-` (suc (suc zero)) refl) ⊢ih)
(⊢-` (suc zero) refl))
-- fac t = natrec 1 (λ x ih. times (suc x) ih) t
fac : ∀ {n} → Term n
fac = `λ[x⦂ `ℕ ] rec (`suc `zero) (times · `suc x · ih) (` zero)
⊢fac : ∀ {n} {Γ : Context n} → Γ ⊢ fac ⦂ ℕ→ℕ
⊢fac = ⊢-λ (⊢-Set<Setω ⊢-ℕ)
(⊢rec (⊢-suc ⊢-zero) (app₂ ⊢times (⊢-suc ⊢x) ⊢ih) (⊢-` zero refl))
-- fac 3 : ℕ
three : Term 0
three = `suc `suc `suc `zero
⊢fac3 : ∅ ⊢ fac · three ⦂ `ℕ
⊢fac3 = ⊢-· ⊢fac (⊢-suc (⊢-suc (⊢-suc ⊢-zero)))