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

module Definition.Typed.Consequences.Canonicity where

open import Definition.Untyped hiding (wk)
import Definition.Untyped as U
open import Definition.Untyped.Properties

open import Definition.Typed
open import Definition.Typed.Weakening
open import Definition.Typed.Properties
open import Definition.Typed.RedSteps

open import Definition.Typed.EqRelInstance

open import Definition.LogicalRelation
open import Definition.LogicalRelation.Irrelevance
open import Definition.LogicalRelation.ShapeView
open import Definition.LogicalRelation.Properties
open import Definition.LogicalRelation.Substitution
open import Definition.LogicalRelation.Substitution.Properties
open import Definition.LogicalRelation.Fundamental.Reducibility

open import Tools.Empty
open import Tools.Unit
open import Tools.Nat
open import Tools.Product

import Tools.PropositionalEquality as PE


-- Turns a natural number into its term representation
sucᵏ : Nat  Term
sucᵏ zero = zero
sucᵏ (suc n) = suc (sucᵏ n)

-- Helper function for canonicity for reducible natural properties
canonicity″ :  {t}
               Natural-prop ε t
                λ k  ε  t  sucᵏ k  
canonicity″ (suc (ℕₜ n₁ d n≡n prop)) =
  let a , b = canonicity″ prop
  in  suc a , suc-cong (trans (subset*Term (redₜ d)) b)
canonicity″ zero = zero , refl (zero ε)
canonicity″ (ne (neNfₜ neK ⊢k k≡k)) = ⊥-elim (noNe ⊢k neK)

-- Helper function for canonicity for specific reducible natural numbers
canonicity′ :  {t l}
              ([ℕ] : ε ⊩⟨ l ⟩ℕ )
              ε ⊩⟨ l  t   / ℕ-intr [ℕ]
               λ k  ε  t  sucᵏ k  
canonicity′ (noemb [ℕ]) (ℕₜ n d n≡n prop) =
  let a , b = canonicity″ prop
  in  a , trans (subset*Term (redₜ d)) b
canonicity′ (emb 0<1 [ℕ]) [t] = canonicity′ [ℕ] [t]

-- Canonicity of natural numbers
canonicity :  {t}  ε  t     λ k  ε  t  sucᵏ k  
canonicity ⊢t with reducibleTerm ⊢t
canonicity ⊢t | [ℕ] , [t] =
  canonicity′ (ℕ-elim [ℕ]) (irrelevanceTerm [ℕ] (ℕ-intr (ℕ-elim [ℕ])) [t])