ISWIM.agda 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. -- A Typed version of a subset of Landin's ISWIM from "The Next 700 Programming
  2. -- Languages"
  3. module ISWIM where
  4. data Nat : Set where
  5. zero : Nat
  6. suc : Nat -> Nat
  7. _+_ : Nat -> Nat -> Nat
  8. zero + m = m
  9. suc n + m = suc (n + m)
  10. {-# BUILTIN NATURAL Nat #-}
  11. {-# BUILTIN NATPLUS _+_ #-}
  12. data Bool : Set where
  13. true : Bool
  14. false : Bool
  15. module Syntax where
  16. infixl 100 _∙_
  17. infixl 80 _WHERE_ _PP_
  18. infixr 60 _─→_
  19. infixl 40 _,_
  20. data Type : Set where
  21. nat : Type
  22. bool : Type
  23. _─→_ : Type -> Type -> Type
  24. data Context : Set where
  25. ε : Context
  26. _,_ : Context -> Type -> Context
  27. data Var : Context -> Type -> Set where
  28. vz : {Γ : Context}{τ : Type} -> Var (Γ , τ) τ
  29. vs : {Γ : Context}{σ τ : Type} -> Var Γ τ -> Var (Γ , σ) τ
  30. data Expr (Γ : Context) : Type -> Set where
  31. var : {τ : Type} -> Var Γ τ -> Expr Γ τ
  32. litNat : Nat -> Expr Γ nat
  33. litBool : Bool -> Expr Γ bool
  34. plus : Expr Γ (nat ─→ nat ─→ nat)
  35. if : {τ : Type} -> Expr Γ (bool ─→ τ ─→ τ ─→ τ)
  36. _∙_ : {σ τ : Type} -> Expr Γ (σ ─→ τ) -> Expr Γ σ -> Expr Γ τ
  37. _WHERE_ : {σ τ ρ : Type} -> Expr (Γ , σ ─→ τ) ρ -> Expr (Γ , σ) τ -> Expr Γ ρ
  38. _PP_ : {σ τ ρ : Type} -> Expr (Γ , σ ─→ τ) ρ -> Expr (Γ , σ) ρ -> Expr Γ ρ
  39. -- ƛ x. e = f where f x = e
  40. ƛ : {Γ : Context}{σ τ : Type} -> Expr (Γ , σ) τ -> Expr Γ (σ ─→ τ)
  41. ƛ e = var vz WHERE e
  42. module Cont (R : Set) where
  43. C : Set -> Set
  44. C a = (a -> R) -> R
  45. callcc : {a : Set} -> (({b : Set} -> a -> C b) -> C a) -> C a
  46. callcc {a} g = \k -> g (\x _ -> k x) k
  47. return : {a : Set} -> a -> C a
  48. return x = \k -> k x
  49. infixr 10 _>>=_
  50. _>>=_ : {a b : Set} -> C a -> (a -> C b) -> C b
  51. (m >>= k) ret = m \x -> k x ret
  52. module Semantics (R : Set) where
  53. open module C = Cont R
  54. open Syntax
  55. infix 60 _!_
  56. infixl 40 _||_
  57. ⟦_⟧type : Type -> Set
  58. ⟦_⟧type' : Type -> Set
  59. ⟦ nat ⟧type' = Nat
  60. ⟦ bool ⟧type' = Bool
  61. ⟦ σ ─→ τ ⟧type' = ⟦ σ ⟧type' -> ⟦ τ ⟧type
  62. ⟦ τ ⟧type = C ⟦ τ ⟧type'
  63. data ⟦_⟧ctx : Context -> Set where
  64. ★ : ⟦ ε ⟧ctx
  65. _||_ : {Γ : Context}{τ : Type} -> ⟦ Γ ⟧ctx -> ⟦ τ ⟧type' -> ⟦ Γ , τ ⟧ctx
  66. _!_ : {Γ : Context}{τ : Type} -> ⟦ Γ ⟧ctx -> Var Γ τ -> ⟦ τ ⟧type'
  67. ★ ! ()
  68. (ρ || v) ! vz = v
  69. (ρ || v) ! vs x = ρ ! x
  70. ⟦_⟧ : {Γ : Context}{τ : Type} -> Expr Γ τ -> ⟦ Γ ⟧ctx -> ⟦ τ ⟧type
  71. ⟦ var x ⟧ ρ = return (ρ ! x)
  72. ⟦ litNat n ⟧ ρ = return n
  73. ⟦ litBool b ⟧ ρ = return b
  74. ⟦ plus ⟧ ρ = return \n -> return \m -> return (n + m)
  75. ⟦ f ∙ e ⟧ ρ = ⟦ e ⟧ ρ >>= \v ->
  76. ⟦ f ⟧ ρ >>= \w ->
  77. w v
  78. ⟦ e WHERE f ⟧ ρ = ⟦ e ⟧ (ρ || (\x -> ⟦ f ⟧ (ρ || x)))
  79. ⟦ e PP f ⟧ ρ = callcc \k ->
  80. let throw = \x -> ⟦ f ⟧ (ρ || x) >>= k
  81. in ⟦ e ⟧ (ρ || throw)
  82. ⟦ if ⟧ ρ = return \x -> return \y -> return \z -> return (iff x y z)
  83. where
  84. iff : {A : Set} -> Bool -> A -> A -> A
  85. iff true x y = x
  86. iff false x y = y
  87. module Test where
  88. open Syntax
  89. open module C = Cont Nat
  90. open module S = Semantics Nat
  91. run : Expr ε nat -> Nat
  92. run e = ⟦ e ⟧ ★ \x -> x
  93. -- 1 + 1
  94. two : Expr ε nat
  95. two = plus ∙ litNat 1 ∙ litNat 1
  96. -- f 1 + f 2 where f x = x
  97. three : Expr ε nat
  98. three = plus ∙ (var vz ∙ litNat 1) ∙ (var vz ∙ litNat 2) WHERE var vz
  99. -- 1 + f 1 where pp f x = x
  100. one : Expr ε nat
  101. one = plus ∙ litNat 1 ∙ (var vz ∙ litNat 1) PP var vz
  102. open Test
  103. data _==_ {a : Set}(x : a) : a -> Set where
  104. refl : x == x
  105. twoOK : run two == 2
  106. twoOK = refl
  107. threeOK : run three == 3
  108. threeOK = refl
  109. oneOK : run one == 1
  110. oneOK = refl
  111. open Cont
  112. open Syntax
  113. open Semantics