/collects/redex/examples/cont-mark-transform/SL-semantics-test.rkt

http://github.com/agocke/racket · Racket · 376 lines · 328 code · 35 blank · 13 comment · 2 complexity · 9c8e3b27f83bf8c5f8a9c195a4d44b57 MD5 · raw file

  1. #lang racket
  2. (require "SL-syntax.rkt"
  3. "SL-semantics.rkt"
  4. "common.rkt"
  5. "test-util.rkt"
  6. redex)
  7. (test-SL-result
  8. ((λ (x) ("S" x)) ("Z"))
  9. ("S" ("Z")))
  10. (test-SL-stuck ((λ (x) ("S" x)) ("Z") ("Z")))
  11. (test-SL-result
  12. (match ("a" ("1"))
  13. [("a" x) x]
  14. [("b" y) y])
  15. ("1"))
  16. (test-SL-result
  17. (match ("b" ("1"))
  18. [("a" x) x]
  19. [("b" y) y])
  20. ("1"))
  21. (test-SL-stuck
  22. (match ("a" ("1"))
  23. [("a" x) x]
  24. [("a" y) y]))
  25. (test-SL-result
  26. (letrec ([(ref build-list)
  27. (λ (n f)
  28. (match n
  29. [("Z") ("nil")]
  30. [("S" m)
  31. ((λ (x)
  32. ((λ (xs) ("cons" x xs))
  33. ((ref build-list) m f)))
  34. (f m))]))])
  35. ((ref build-list) ("S" ("S" ("S" ("Z")))) (λ (i) ("S" i))))
  36. ("cons" ("S" ("S" ("S" ("Z"))))
  37. ("cons" ("S" ("S" ("Z")))
  38. ("cons" ("S" ("Z"))
  39. ("nil")))))
  40. (test-SL-result
  41. ((λ (clobber)
  42. ((λ (a)
  43. ((λ (b) (a))
  44. (clobber ("b"))))
  45. (clobber ("a"))))
  46. (λ (x)
  47. (letrec ([(ref y) (λ () x)])
  48. (ref y))))
  49. ("b"))
  50. (test-SL-result
  51. (letrec ([(ref x) ("S" ("Z"))])
  52. (match (ref x)
  53. [("Z") ("a")]
  54. [("S" _) ("b")]))
  55. ("b"))
  56. (test-SL-result
  57. (w-c-m ("a") ("1")
  58. ((λ (x) x)
  59. (w-c-m ("a") ("2")
  60. (c-c-m [("a")]))))
  61. ("cons"
  62. ("cons" ("cons" ("a") ("1")) ("nil"))
  63. ("cons" ("cons" ("cons" ("a") ("2")) ("nil"))
  64. ("nil"))))
  65. (test-SL-result
  66. (w-c-m ("a") ("1")
  67. (w-c-m ("b") ("2")
  68. (c-c-m [("a") ("b")])))
  69. ("cons" ("cons" ("cons" ("b") ("2"))
  70. ("cons" ("cons" ("a") ("1")) ("nil")))
  71. ("nil")))
  72. (test-SL-result
  73. (w-c-m ("a") ("1")
  74. (w-c-m ("b") ("2")
  75. (c-c-m [("b") ("a")])))
  76. ("cons" ("cons" ("cons" ("b") ("2"))
  77. ("cons" ("cons" ("a") ("1")) ("nil")))
  78. ("nil")))
  79. (test-SL-result
  80. (w-c-m ("a") ("1")
  81. (c-c-m [("b") ("a")]))
  82. ("cons" ("cons" ("cons" ("a") ("1")) ("nil"))
  83. ("nil")))
  84. (test-SL-result
  85. (w-c-m ("a") ("1")
  86. ((λ (x) x)
  87. ((λ (x) x)
  88. ((λ (x) x)
  89. (w-c-m ("a") ("2")
  90. (w-c-m ("b") ("1")
  91. (c-c-m [("a") ("b")])))))))
  92. ("cons"
  93. ("cons" ("cons" ("a") ("1")) ("nil"))
  94. ("cons" ("nil")
  95. ("cons" ("nil")
  96. ("cons"
  97. ("cons"
  98. ("cons" ("b") ("1"))
  99. ("cons" ("cons" ("a") ("2"))
  100. ("nil")))
  101. ("nil"))))))
  102. (test-SL-result
  103. (w-c-m ("a") ("1")
  104. ((λ (x) x)
  105. (c-c-m [("a")])))
  106. ("cons"
  107. ("cons" ("cons" ("a") ("1")) ("nil"))
  108. ("cons" ("nil") ("nil"))))
  109. (test-SL-result
  110. ((λ (_)
  111. ((λ (x) (x x))
  112. (λ (x) (x x))))
  113. (abort ("Z")))
  114. ("Z"))
  115. (test-SL-result
  116. ((λ (x)
  117. (match x
  118. [("Z") ("a")]
  119. [("S" _) ("b")]))
  120. (call/cc
  121. (λ (k)
  122. ((λ (_)
  123. ((λ (x) (x x))
  124. (λ (x) (x x))))
  125. (k ("Z"))))))
  126. ("a"))
  127. (test-SL-result
  128. ((λ (x) ("S" ("S" x)))
  129. (letrec ([(ref k) (κ ((λ (x) ("S" x)) hole))])
  130. ((ref k) ("Z"))))
  131. ("S" ("Z")))
  132. (test-SL-result
  133. ((λ (x)
  134. (match ("b" x)
  135. [("b" x) x]))
  136. ("a"))
  137. ("a"))
  138. (test-->>
  139. -->SL
  140. #:cycles-ok
  141. (term
  142. (
  143. /
  144. ((λ (t) (t t))
  145. (call/cc (λ (x) (call/cc x)))))))
  146. ;; fact
  147. (define fact-impl
  148. `(λ (n)
  149. ,(:if `((ref =) n ,(num 0))
  150. (:let 'marks '(c-c-m [("fact")])
  151. '(abort marks))
  152. `(w-c-m ("fact") n
  153. ,(:let 'sub1-fact
  154. (:let 'sub1 `((ref -) n ,(num 1))
  155. `((ref fact) sub1))
  156. `((ref *) n sub1-fact))))))
  157. (define fact-tr-impl
  158. `(λ (n a)
  159. ,(:if `((ref =) n ,(num 0))
  160. (:let 'marks '(c-c-m [("fact")])
  161. '(abort marks))
  162. `(w-c-m ("fact") n
  163. ,(:let 'sub1 `((ref -) n ,(num 1))
  164. (:let 'multa `((ref *) n a)
  165. `((ref fact-tr) sub1 multa)))))))
  166. (define (test-fact n)
  167. (test-SL-result
  168. ,(with-arith
  169. `(letrec ([(ref fact) ,fact-impl])
  170. ((ref fact) ,(num n))))
  171. ,(lst (append (build-list n (λ (i) (term ("cons" ("cons" ("fact") ,(num (- n i))) ("nil")))))
  172. (list (term ("nil")) ; frame computing 1 * fact(0)
  173. (term ("nil"))))))) ; frame that names c-c-m result
  174. (define (test-fact-tr n)
  175. (test-SL-result
  176. ,(with-arith
  177. `(letrec ([(ref fact-tr) ,fact-tr-impl])
  178. ((ref fact-tr) ,(num n) ,(num 1))))
  179. ,(lst (list (term ("cons" ("cons" ("fact") ,(num 1)) ("nil")))
  180. (term ("nil")))))) ; frame that names c-c-m result
  181. (for ([i (in-range 1 4)]) (test-fact i))
  182. (for ([i (in-range 1 4)]) (test-fact-tr i))
  183. ;;; Values
  184. (test-->> -->SL
  185. '(∅ / (λ (x) x))
  186. '(∅ / (λ (x) x)))
  187. (test-->> -->SL
  188. '(∅ / ("nil"))
  189. '(∅ / ("nil")))
  190. (test-->> -->SL
  191. '(∅ / ("S" ("0")))
  192. '(∅ / ("S" ("0"))))
  193. (test-->> -->SL
  194. '(∅ / (ref x))
  195. '(∅ / (ref x)))
  196. ;;; Applications
  197. (test-->> -->SL
  198. '(∅ / ((λ (x) x) ("nil")))
  199. '(∅ / ("nil")))
  200. ;;; Store applications
  201. (test-->> -->SL
  202. '((∅ [(ref x) ↦ (λ (x) ("nil"))])
  203. /
  204. ((ref x) ("0")))
  205. '((∅ [(ref x) ↦ (λ (x) ("nil"))])
  206. /
  207. ("nil")))
  208. ;;; Letrec
  209. (test-->> -->SL
  210. '(∅ / (letrec ([(ref x) (λ (x) ("nil"))])
  211. ("foo")))
  212. '((∅ [(ref x) ↦ (λ (x) ("nil"))])
  213. /
  214. ("foo")))
  215. (test-->> -->SL
  216. '(∅ / (letrec ([(ref x) (λ (x) ("nil"))])
  217. ((ref x) ("0"))))
  218. '((∅ [(ref x) ↦ (λ (x) ("nil"))])
  219. /
  220. ("nil")))
  221. ;;; match
  222. (test-->> -->SL
  223. '(∅ / (match ("S" ("0"))
  224. [("S" n) n]
  225. [("0") ("0")]))
  226. '(∅ / ("0")))
  227. (test-->> -->SL
  228. '(∅ / (match ("S" ("0"))
  229. [("0") ("0")]
  230. [("S" n) n]))
  231. '(∅ / ("0")))
  232. ; Store match
  233. (test-->> -->SL
  234. '(∅ / (letrec ([(ref x) ("S" ("0"))])
  235. (match (ref x)
  236. [("S" n) n]
  237. [("0") ("0")])))
  238. '((∅ [(ref x) ↦ ("S" ("0"))])
  239. /
  240. ("0")))
  241. ;; w-c-m
  242. (test-->> -->SL
  243. `( / (w-c-m ("k") ,(num 1) ,(num 2)))
  244. `(∅ / ,(num 2)))
  245. (test-->> -->SL
  246. `( / (w-c-m ("k") ,(num 1) (w-c-m ("k") ,(num 3) ,(num 2))))
  247. `(∅ / ,(num 2)))
  248. (test-->> -->SL
  249. `( / (w-c-m ("k") ,(num 1) ((λ (x) x) ,(num 2))))
  250. `(∅ / ,(num 2)))
  251. ;; c-c-m
  252. (test-->> -->SL
  253. `( / (c-c-m [("k")]))
  254. `(∅ / ("cons" ("nil") ("nil"))))
  255. (test-->> -->SL
  256. `( / (w-c-m ("k") ,(num 1) (c-c-m [("k")])))
  257. `(∅ / ("cons" ("cons" ("cons" ("k") ,(num 1)) ("nil")) ("nil"))))
  258. (test-->> -->SL
  259. `( / (w-c-m ("k") ,(num 1) (w-c-m ("k") ,(num 2) (c-c-m [("k")]))))
  260. `(∅ / ("cons" ("cons" ("cons" ("k") ,(num 2)) ("nil")) ("nil"))))
  261. (test-->> -->SL
  262. `( / (w-c-m ("k") ,(num 1) ((λ (x) x) (w-c-m ("k") ,(num 2) (c-c-m [("k")])))))
  263. `(∅ / ("cons" ("cons" ("cons" ("k") ,(num 1)) ("nil"))
  264. ("cons" ("cons" ("cons" ("k") ,(num 2)) ("nil"))
  265. ("nil")))))
  266. (test-->> -->SL
  267. `( / (w-c-m ("k1") ,(num 1) (c-c-m [("k1") ("k2")])))
  268. `(∅ / ("cons" ("cons" ("cons" ("k1") ,(num 1)) ("nil")) ("nil"))))
  269. (test-->> -->SL
  270. `( / (w-c-m ("k1") ,(num 1) (w-c-m ("k2") ,(num 2) (c-c-m [("k1") ("k2")]))))
  271. `(∅ / ("cons" ("cons" ("cons" ("k2") ,(num 2))
  272. ("cons" ("cons" ("k1") ,(num 1))
  273. ("nil")))
  274. ("nil"))))
  275. ;; abort
  276. (test-->> -->SL
  277. `( / (abort ,(num 2)))
  278. `(∅ / ,(num 2)))
  279. (test-->> -->SL
  280. `( / ((λ (x) x) (abort ,(num 2))))
  281. `(∅ / ,(num 2)))
  282. ;; arith
  283. (test-->> -->SL
  284. `( / ,(:let 'x (num 1) 'x))
  285. `(∅ / ,(num 1)))
  286. (test-SL-result ∅ ,(with-arith (num 1)) ,(num 1))
  287. (test-SL-result ∅ ,(with-arith `((ref +) ,(num 1) ,(num 1))) ,(num 2))
  288. (test-SL-result ,(with-arith `((ref *) ,(num 2) ,(num 2))) ,(num 4))
  289. (test-SL-result ∅ ,(with-arith `((ref =) ,(num 2) ,(num 2))) ("#t"))
  290. (test-SL-result ,(with-arith `((ref =) ,(num 2) ,(num 3))) ("#f"))
  291. (test-SL-result ∅ ,(with-arith `((ref -) ,(num 3) ,(num 2))) ,(num 1))
  292. (test-SL-result ,(with-arith (:if '("#t") (num 1) (num 2))) ,(num 1))
  293. (test-SL-result ,(with-arith (:if '("#f") (num 1) (num 2))) ,(num 2))
  294. ;; call/cc
  295. (test-->> -->SL
  296. `(∅ / (call/cc (λ (k) (k ("v")))))
  297. `( / ("v")))
  298. (test-->> -->SL
  299. `(∅ / (call/cc (λ (k)
  300. ((λ (x) ("x"))
  301. (k ("v"))))))
  302. `( / ("v")))
  303. ;; call/cc + w-c-m
  304. (test-->> -->SL
  305. `(∅ / (w-c-m ("k") ("v1")
  306. ((λ (f) (f ("unit")))
  307. (call/cc (λ (k)
  308. (w-c-m ("k") ("v2")
  309. (k (λ (x) (c-c-m [("k")])))))))))
  310. `( / ("cons" ("cons" ("cons" ("k") ("v1")) ("nil")) ("nil"))))
  311. (test-->> -->SL
  312. `(∅ / (w-c-m ("k") ("v1")
  313. ((λ (f) (f ("unit")))
  314. (call/cc (λ (k)
  315. (w-c-m ("k") ("v2")
  316. ((λ (cms)
  317. (k (λ (x) cms)))
  318. (c-c-m [("k")]))))))))
  319. `( / ("cons" ("cons" ("cons" ("k") ("v1")) ("nil"))
  320. ("cons" ("cons" ("cons" ("k") ("v2")) ("nil"))
  321. ("cons" ("nil")
  322. ("nil"))))))