Lambda.agda 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. module Lambda where
  2. open import Prelude
  3. open import Star
  4. open import Examples
  5. open import Modal
  6. -- Environments
  7. record TyAlg (ty : Set) : Set where
  8. field
  9. nat : ty
  10. _⟶_ : ty -> ty -> ty
  11. data Ty : Set where
  12. <nat> : Ty
  13. _<⟶>_ : Ty -> Ty -> Ty
  14. freeTyAlg : TyAlg Ty
  15. freeTyAlg = record { nat = <nat>; _⟶_ = _<⟶>_ }
  16. termTyAlg : TyAlg True
  17. termTyAlg = record { nat = _; _⟶_ = \_ _ -> _ }
  18. record TyArrow {ty₁ ty₂ : Set}(T₁ : TyAlg ty₁)(T₂ : TyAlg ty₂) : Set where
  19. field
  20. apply : ty₁ -> ty₂
  21. respNat : apply (TyAlg.nat T₁) == TyAlg.nat T₂
  22. resp⟶ : forall {τ₁ τ₂} ->
  23. apply (TyAlg._⟶_ T₁ τ₁ τ₂) == TyAlg._⟶_ T₂ (apply τ₁) (apply τ₂)
  24. _=Ty=>_ : {ty₁ ty₂ : Set}(T₁ : TyAlg ty₁)(T₂ : TyAlg ty₂) -> Set
  25. _=Ty=>_ = TyArrow
  26. !Ty : {ty : Set}{T : TyAlg ty} -> T =Ty=> termTyAlg
  27. !Ty = record { apply = !
  28. ; respNat = refl
  29. ; resp⟶ = refl
  30. }
  31. Ctx : Set
  32. Ctx = List Ty
  33. Var : {ty : Set} -> List ty -> ty -> Set
  34. Var Γ τ = Any (_==_ τ) Γ
  35. vzero : {τ : Ty} {Γ : Ctx} -> Var (τ • Γ) τ
  36. vzero = done refl • ε
  37. vsuc : {σ τ : Ty} {Γ : Ctx} -> Var Γ τ -> Var (σ • Γ) τ
  38. vsuc v = step • v
  39. module Term {ty : Set}(T : TyAlg ty) where
  40. private open module TT = TyAlg T
  41. data Tm : List ty -> ty -> Set where
  42. var : forall {Γ τ} -> Var Γ τ -> Tm Γ τ
  43. zz : forall {Γ} -> Tm Γ nat
  44. ss : forall {Γ} -> Tm Γ (nat ⟶ nat)
  45. ƛ : forall {Γ σ τ} -> Tm (σ • Γ) τ -> Tm Γ (σ ⟶ τ)
  46. _$_ : forall {Γ σ τ} -> Tm Γ (σ ⟶ τ) -> Tm Γ σ -> Tm Γ τ
  47. module Eval where
  48. private open module TT = Term freeTyAlg
  49. ty⟦_⟧ : Ty -> Set
  50. ty⟦ <nat> ⟧ = Nat
  51. ty⟦ σ <⟶> τ ⟧ = ty⟦ σ ⟧ -> ty⟦ τ ⟧
  52. Env : Ctx -> Set
  53. Env = All ty⟦_⟧
  54. _[_] : forall {Γ τ} -> Env Γ -> Var Γ τ -> ty⟦ τ ⟧
  55. ρ [ x ] with lookup x ρ
  56. ... | result _ refl v = v
  57. ⟦_⟧_ : forall {Γ τ} -> Tm Γ τ -> Env Γ -> ty⟦ τ ⟧
  58. ⟦ var x ⟧ ρ = ρ [ x ]
  59. ⟦ zz ⟧ ρ = zero
  60. ⟦ ss ⟧ ρ = suc
  61. ⟦ ƛ t ⟧ ρ = \x -> ⟦ t ⟧ (check x • ρ)
  62. ⟦ s $ t ⟧ ρ = (⟦ s ⟧ ρ) (⟦ t ⟧ ρ)
  63. module MoreExamples where
  64. private open module TT = TyAlg freeTyAlg
  65. private open module Tm = Term freeTyAlg
  66. open Eval
  67. tm-one : Tm ε nat
  68. tm-one = ss $ zz
  69. tm-id : Tm ε (nat ⟶ nat)
  70. tm-id = ƛ (var (done refl • ε))
  71. tm : Tm ε nat
  72. tm = tm-id $ tm-one
  73. tm-twice : Tm ε ((nat ⟶ nat) ⟶ (nat ⟶ nat))
  74. tm-twice = ƛ (ƛ (f $ (f $ x)))
  75. where Γ : Ctx
  76. Γ = nat • (nat ⟶ nat) • ε
  77. f : Tm Γ (nat ⟶ nat)
  78. f = var (vsuc vzero)
  79. x : Tm Γ nat
  80. x = var vzero
  81. sem : {τ : Ty} -> Tm ε τ -> ty⟦ τ ⟧
  82. sem e = ⟦ e ⟧ ε
  83. one : Nat
  84. one = sem tm
  85. twice : (Nat -> Nat) -> (Nat -> Nat)
  86. twice = sem tm-twice