StackLanguage.agda 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. {-
  2. A simple stack language example. Illustrating that one
  3. wants dependent elimination of inductive families.
  4. -}
  5. module StackLanguage where
  6. open import Lib.Nat
  7. open import Lib.Vec
  8. open import Lib.Id
  9. data Prog (A : Set) : Nat -> Set where
  10. init : Prog A 0
  11. push : forall {n} -> A -> Prog A n -> Prog A (suc n)
  12. pop : forall {n} -> Prog A (suc n) -> Prog A n
  13. ⟦_⟧ : forall {A n} -> Prog A n -> Vec A n
  14. ⟦ init ⟧ = ε
  15. ⟦ push x p ⟧ = x ► ⟦ p ⟧
  16. ⟦ pop p ⟧ with ⟦ p ⟧
  17. ... | x ► xs = xs
  18. reify : forall {A n} -> Vec A n -> Prog A n
  19. reify ε = init
  20. reify (x ► xs) = push x (reify xs)
  21. normalise : forall {A n} -> Prog A n -> Prog A n
  22. normalise p = reify ⟦ p ⟧
  23. _≅_ : forall {A n} -> Prog A n -> Prog A n -> Set
  24. p₁ ≅ p₂ = ⟦ p₁ ⟧ == ⟦ p₂ ⟧
  25. sound : forall {A n} -> (p : Prog A n) -> normalise p ≅ p
  26. sound init = refl
  27. sound (push x p) = cong (_►_ x) (sound p)
  28. sound (pop p) with ⟦ p ⟧ | sound p
  29. ... | x ► xs | ih with ⟦ reify xs ⟧
  30. sound (pop p) | x ► xs | refl | .xs = refl