/cesk.rkt

http://github.com/samth/var · Racket · 200 lines · 184 code · 15 blank · 1 comment · 6 complexity · 97105f23793f183c758c47711b494349 MD5 · raw file

  1. #lang racket
  2. (require "lang.rkt" "meta.rkt" "step.rkt" "garbage.rkt" "util.rkt")
  3. (require redex/reduction-semantics)
  4. (test-suite test cesk)
  5. (provide CESK inj CESK-trace-it CESK-run unload)
  6. (current-direct? #f)
  7. (define-metafunction λCESK
  8. inj : EXP -> ς
  9. [(inj EXP)
  10. (ev ( EXP (env)) (sto) MT)])
  11. (define-metafunction λCESK
  12. bind : σ K -> (a σ)
  13. [(bind σ K)
  14. (a σ_1)
  15. (where (a) (alloc σ (K)))
  16. (where σ_1 (extend-sto σ a (K)))])
  17. (define-metafunction λCESK
  18. ev : D σ K -> ς
  19. [(ev A σ MT) (A σ)]
  20. [(ev BLAME σ K) (BLAME σ)]
  21. [(ev V σ K) (co K V σ)]
  22. [(ev REDEX σ K) (ap REDEX σ K)]
  23. [(ev PREVAL σ K) (ev (-- PREVAL) σ K)]
  24. [(ev (clos ρ) σ K) (ev (join-contracts) σ K)]
  25. [(ev (clos (@ EXP ... LAB) ρ) σ K)
  26. (ev (@ ( EXP ρ) ... LAB) σ K)]
  27. [(ev (clos (@* EXP ... LAB) ρ) σ K)
  28. (ev (@* ( EXP ρ) ... LAB) σ K)]
  29. [(ev (clos (if EXP ...) ρ) σ K)
  30. (ev (if ( EXP ρ) ...) σ K)]
  31. [(ev (clos (let ((X EXP) ...) EXP_1) ρ) σ K)
  32. (ev (let ((X ( EXP ρ)) ...) ( EXP_1 ρ)) σ K)]
  33. [(ev (clos (begin EXP EXP_1) ρ) σ K)
  34. (ev (begin ( EXP ρ) ( EXP_1 ρ)) σ K)]
  35. [(ev (clos MODREF ρ) σ K)
  36. (ev MODREF σ K)]
  37. [(ev (@ D_1 D_2 ... LAB) σ K)
  38. (ev D_1 σ_1 (APP () (D_2 ...) LAB a))
  39. (where (a σ_1) (bind σ K))]
  40. [(ev (@* D_1 D_2 ... LAB) σ K)
  41. (ev D_1 σ_1 (APP* () (D_2 ...) LAB a))
  42. (where (a σ_1) (bind σ K))]
  43. [(ev (if D_1 D_2 D_3) σ K)
  44. (ev D_1 σ_1 (IF D_2 D_3 a))
  45. (where (a σ_1) (bind σ K))]
  46. [(ev (let () D) σ K)
  47. (ev D σ K)]
  48. [(ev (let ((X D) (X_1 D_1) ...) D_n) σ K)
  49. (ev D σ_1 (LET () X ((X_1 D_1) ...) D_n a))
  50. (where (a σ_1) (bind σ K))]
  51. [(ev (CON ρ <= LAB_1 LAB_2 V LAB_3 D) σ K)
  52. (ev D σ_1 (CHECK CON ρ LAB_1 LAB_2 V LAB_3 a))
  53. (where (a σ_1) (bind σ K))]
  54. [(ev (begin D D_1) σ K)
  55. (ev D σ_1 (BEGIN D_1 a))
  56. (where (a σ_1) (bind σ K))]
  57. [(ev (ANYCON ρ <= LAB_1 LAB_2 V LAB_3 D) σ K)
  58. (ev D σ K)])
  59. (define (ap Ms)
  60. (define r
  61. (union-reduction-relations v c c~ a d (m Ms) (m~ Ms)))
  62. (reduction-relation
  63. λCESK #:domain ς
  64. (--> (ap D_redex σ K)
  65. (gc-state (ev D_contractum σ_1 K))
  66. (where (any_0 ... (any_name (D_contractum σ_1)) any_1 ...)
  67. ,(apply-reduction-relation/tag-with-names r (term (D_redex σ))))
  68. (computed-name (term any_name)))))
  69. (define co
  70. (reduction-relation
  71. λCESK #:domain ς
  72. (--> (co (APP (V_1 ...) (D_1 D_2 ...) LAB a) V σ)
  73. (gc-state (ev D_1 σ (APP (V_1 ... V) (D_2 ...) LAB a)))
  74. co-next-@)
  75. (--> (co (APP* (V_1 ...) (D_1 D_2 ...) LAB a) V σ)
  76. (gc-state (ev D_1 σ (APP* (V_1 ... V) (D_2 ...) LAB a)))
  77. co-next-@*)
  78. (--> (co (APP (V_1 ...) () LAB a) V σ)
  79. (gc-state (ap (@ V_1 ... V LAB) σ K))
  80. (where (S_0 ... K S_1 ...)
  81. (sto-lookup σ a))
  82. co-done-@)
  83. (--> (co (APP* (V_1 ...) () LAB a) V σ)
  84. (gc-state (ap (@* V_1 ... V LAB) σ K))
  85. (where (S_0 ... K S_1 ...)
  86. (sto-lookup σ a))
  87. co-done-@*)
  88. (--> (co (IF D_1 D_2 a) V σ)
  89. (gc-state (ap (if V D_1 D_2) σ K))
  90. (where (S_0 ... K S_1 ...)
  91. (sto-lookup σ a))
  92. co-done-if)
  93. (--> (co (LET ((X_1 V_1) ...) X ((X_2 D_2) (X_3 D_3) ...) D_b a) V σ)
  94. (gc-state (ev D_2 σ (LET ((X_1 V_1) ... (X V)) X_2 ((X_3 D_3) ...) D_b a)))
  95. co-next-let)
  96. (--> (co (LET ((X_1 V_1) ...) X () D_b a) V σ)
  97. (gc-state (ap (let ((X_1 V_1) ... (X V)) D_b) σ K))
  98. (where (S_0 ... K S_1 ...)
  99. (sto-lookup σ a))
  100. co-done-let)
  101. (--> (co (BEGIN D a) V σ)
  102. (gc-state (ev D σ K))
  103. (where (S_0 ... K S_1 ...)
  104. (sto-lookup σ a))
  105. co-done-begin)
  106. (--> (co (CHECK CON ρ LAB_1 LAB_2 V_1 LAB_3 a) V σ)
  107. (gc-state (ap (CON ρ <= LAB_1 LAB_2 V_1 LAB_3 V) σ K))
  108. (where (S_0 ... K S_1 ...)
  109. (sto-lookup σ a))
  110. co-done-check)))
  111. (define-metafunction λCESK
  112. unload : ς -> (D σ)
  113. [(unload (A σ)) (gc (A σ))]
  114. [(unload (ap D σ K)) (gc ((stick D K σ) σ))]
  115. [(unload (co K V σ)) (gc ((stick V K σ) σ))])
  116. (test
  117. (test-equal (term (unload ((-- (clos 0 (env))) (sto))))
  118. (term ((-- (clos 0 (env))) (sto))))
  119. (test-equal (term (unload (ap (clos 0 (env)) (sto) MT)))
  120. (term ((clos 0 (env)) (sto))))
  121. (test-equal (term (unload (co MT (-- (clos 0 (env))) (sto))))
  122. (term ((-- (clos 0 (env))) (sto)))))
  123. (define-metafunction λCESK
  124. stick : D K σ -> D
  125. [(stick D MT σ) D]
  126. [(stick D (APP (V ...) (D_1 ...) LAB a) σ)
  127. (stick (@ V ... D D_1 ... LAB) K σ)
  128. (where (S_0 ... K S_1 ...)
  129. (sto-lookup σ a))]
  130. [(stick D (APP* (V ...) (D_1 ...) LAB a) σ)
  131. (stick (@* V ... D D_1 ... LAB) K σ)
  132. (where (S_0 ... K S_1 ...)
  133. (sto-lookup σ a))]
  134. [(stick D (IF D_1 D_2 a) σ)
  135. (stick (if D D_1 D_2) K σ)
  136. (where (S_0 ... K S_1 ...)
  137. (sto-lookup σ a))]
  138. [(stick D (LET ((X V) ...) X_1 ((X_2 D_2) ...) D_1 a) σ)
  139. (stick (let ((X V) ... (X_1 D) (X_2 D_2) ...) D_1) K σ)
  140. (where (S_0 ... K S_1 ...)
  141. (sto-lookup σ a))]
  142. [(stick D (BEGIN D_1 a) σ)
  143. (stick (begin D D_1) K σ)
  144. (where (S_0 ... K S_1 ...)
  145. (sto-lookup σ a))]
  146. [(stick D (DEM CON a) σ)
  147. (stick (dem CON D) K σ)
  148. (where (S_0 ... K S_1 ...)
  149. (sto-lookup σ a))]
  150. [(stick D (CHECK CON ρ LAB_1 LAB_2 V LAB_3 a) σ)
  151. (stick (CON ρ <= LAB_1 LAB_2 V LAB_3 D) K σ)
  152. (where (S_0 ... K S_1 ...)
  153. (sto-lookup σ a))])
  154. (test
  155. (define D (term (clos 0 (env))))
  156. (define D1 (term (clos 1 (env))))
  157. (define D2 (term (clos 2 (env))))
  158. (define V1 (term (-- (clos 1 (env)))))
  159. (define V2 (term (-- (clos 2 (env)))))
  160. (test-equal (term (stick ,D MT (sto)))
  161. D)
  162. (test-equal (term (stick ,D (APP (,V1 ,V2) (,D1 ,D2) f (loc a)) (sto [(loc a) (MT)])))
  163. (term (@ ,V1 ,V2 ,D ,D1 ,D2 f)))
  164. (test-equal (term (stick ,D (APP* (,V1 ,V2) (,D1 ,D2) f (loc a)) (sto [(loc a) (MT)])))
  165. (term (@* ,V1 ,V2 ,D ,D1 ,D2 f)))
  166. (test-equal (term (stick ,D (IF ,D1 ,D2 (loc a)) (sto [(loc a) (MT)])))
  167. (term (if ,D ,D1 ,D2)))
  168. (test-equal (term (stick ,D (LET ((x ,V1) (y ,V2)) z ((p ,D1)) ,D2 (loc a)) (sto [(loc a) (MT)])))
  169. (term (let ((x ,V1) (y ,V2) (z ,D) (p ,D1)) ,D2)))
  170. (test-equal (term (stick ,D (BEGIN ,D1 (loc a)) (sto [(loc a) (MT)])))
  171. (term (begin ,D ,D1)))
  172. (test-equal (term (stick ,D (DEM () (loc a)) (sto [(loc a) (MT)])))
  173. (term (dem () ,D)))
  174. (test-equal (term (stick ,D (CHECK () (env) f g ,V1 h (loc a)) (sto [(loc a) (MT)])))
  175. (term (() (env) <= f g ,V1 h ,D))))
  176. (define (CESK Ms)
  177. (union-reduction-relations co (ap Ms)))
  178. (define (CESK-run P)
  179. (apply-reduction-relation* (CESK (program-modules P))
  180. (term (inj ,(last P)))))
  181. (define-syntax-rule (CESK-trace-it P . rest)
  182. (traces (CESK (program-modules P))
  183. (term (inj ,(last P)))
  184. ;; #:pred (colorize (program-modules P))
  185. . rest))