/redex/s5.rkt

http://github.com/brownplt/LambdaS5 · Racket · 476 lines · 366 code · 89 blank · 21 comment · 6 complexity · 715869ebdda64bdd827baaa0430a94bc MD5 · raw file

  1. #lang racket
  2. (require redex)
  3. (provide s5 s5)
  4. ;; This should be easy to match up with es5_eval and es5_syntax.ml
  5. (define-language s5
  6. (P (σ Σ Γ e))
  7. ;; variable store
  8. (loc (variable-prefix loc))
  9. (Σ ((loc_!_ val) ...))
  10. ;; object store
  11. (ref (variable-prefix ref))
  12. (σ ((ref_!_ obj) ...))
  13. ;; explicit closure environments
  14. (Γ ((x loc) ...))
  15. (bool #t #f)
  16. (accessor ((@g val) (@s val) (@c bool) (@e bool)))
  17. (data ((@v val) (@w bool) (@c bool) (@e bool)))
  18. (accessor-e ((@g e) (@s e) (@c e) (@e e)))
  19. (data-e ((@v e) (@w bool) (@c e) (@e e)))
  20. (attr @g @s @v @w @c @e)
  21. (obj-attr code primval proto extensible klass)
  22. (property accessor data)
  23. (property-e accessor-e data-e)
  24. (obj-attrs [(obj-attr_!_ val) ...])
  25. (obj-attrs-e [(obj-attr_!_ e) ...])
  26. (s string)
  27. (obj (obj-attrs [(s property) ...]))
  28. (prim number s #t #f undefined null)
  29. (val prim
  30. (Γ : (λ (x_!_ ...) e))
  31. ref
  32. loc)
  33. (op1 typeof surface-typeof primitive? prim->str prim->num
  34. prim->num prim->bool is-callable is-extensible
  35. prevent-extensions print get-proto get-primval get-class
  36. get-code property-names own-property-names object-to-string
  37. strlen is-array to-int32 fail? ! void floor ceil abs log
  38. ascii-ntoc ascii-cton to-lower to-upper ~ sin)
  39. (op2 + - / * % & binary-or ^ << >> >>> < <= > >= stx= abs= hasProperty
  40. hasOwnProperty string+ string< base char-at local-compare
  41. pow to-fixed isAccessor)
  42. (lbl x)
  43. (e prim
  44. x
  45. (λ (x_!_ ...) e)
  46. (object obj-attrs-e
  47. [(string property-e) ...])
  48. (get-attr attr e e)
  49. (set-attr attr e e e)
  50. (get-field e e e)
  51. (get-field2 val val val val)
  52. (set-field e e e e)
  53. (set-field2 val val val val val)
  54. (delete-field e e e)
  55. (set! x e)
  56. (e e ...)
  57. (op op1 e)
  58. (op op2 e e)
  59. (if e e e)
  60. (seq e e)
  61. (let ([x e]) e)
  62. (rec ([x e]) e)
  63. (label lbl e)
  64. (break lbl e)
  65. (catch e e)
  66. (finally e e)
  67. (throw e))
  68. ((f g x y z) variable-not-otherwise-mentioned)
  69. ;; try-catch contexts
  70. (F-property
  71. (string ((@v F) (@w bool) (@c bool) (@e bool)))
  72. (string ((@g F) (@s e) (@c bool) (@e bool)))
  73. (string ((@g val) (@s F) (@c bool) (@e bool))))
  74. (F-attrs
  75. [(obj-attr val) ... (obj-attr F) (obj-attr e) ...])
  76. (F hole
  77. (object F-attrs [(string property-e ...)])
  78. (object obj-attrs [(string property) ...
  79. F-property
  80. (string property-e) ...])
  81. (get-attr attr F e)
  82. (get-attr attr val F)
  83. (set-attr attr F e e)
  84. (set-attr attr val F e)
  85. (set-attr attr val val F)
  86. (get-field F e e)
  87. (get-field val F e)
  88. (get-field val val F)
  89. (set-field F e e e)
  90. (set-field val F e e)
  91. (set-field val val F e)
  92. (set-field val val val F)
  93. (delete-field F e e)
  94. (delete-field val F e)
  95. (delete-field val val F)
  96. (set! x F)
  97. (val ... F e ...)
  98. (op op1 F)
  99. (op op2 F e)
  100. (op op2 val F)
  101. (if F e e)
  102. (seq F e)
  103. (seq val F)
  104. (let ([x F]) e)
  105. (break lbl F)
  106. (throw F))
  107. (G-property
  108. (string ((@v G) (@w bool) (@c bool) (@e bool)))
  109. (string ((@g G) (@s e) (@c bool) (@e bool)))
  110. (string ((@g val) (@s G) (@c bool) (@e bool))))
  111. (G-attrs
  112. [(obj-attr val) ... (obj-attr G) (obj-attr e) ...])
  113. (G hole
  114. (object G-attrs [(string property-e) ...])
  115. (object obj-attrs [(string property) ...
  116. G-property
  117. (string property-e) ...])
  118. (get-attr attr G e)
  119. (get-attr attr val G)
  120. (set-attr attr G e e)
  121. (set-attr attr val G e)
  122. (set-attr attr val val G)
  123. (get-field G e e)
  124. (get-field val G e)
  125. (get-field val val G)
  126. (set-field G e e e)
  127. (set-field val G e e)
  128. (set-field val val G e)
  129. (set-field val val val G)
  130. (delete-field G e e)
  131. (delete-field val G e)
  132. (delete-field val val G)
  133. (set! x G)
  134. (val ... G e ...)
  135. (op1 op G)
  136. (op2 op G e)
  137. (op2 op val G)
  138. (if G e e)
  139. (seq G e)
  140. (seq val G)
  141. (let ([x G]) e)
  142. (label lbl G)
  143. (break lbl G)
  144. (throw G)
  145. (catch G e))
  146. ;; Top-level contexts
  147. (E-property
  148. (string ((@v E) (@w bool) (@c bool) (@e bool)))
  149. (string ((@g E) (@s e) (@c bool) (@e bool)))
  150. (string ((@g val) (@s E) (@c bool) (@e bool))))
  151. (E-attrs
  152. [(obj-attr val) ... (obj-attr E) (obj-attr e) ...])
  153. (E hole
  154. (object E-attrs [(string property-e) ...])
  155. (object obj-attrs [(string property) ...
  156. E-property
  157. (string property-e) ...])
  158. (get-attr attr E e)
  159. (get-attr attr val E)
  160. (set-attr attr E e e)
  161. (set-attr attr val E e)
  162. (set-attr attr val val E)
  163. (get-field E e e)
  164. (get-field val E e)
  165. (get-field val val E)
  166. (set-field E e e e)
  167. (set-field val E e e)
  168. (set-field val val E e)
  169. (set-field val val val E)
  170. (delete-field E e e)
  171. (delete-field val E e)
  172. (delete-field val val E)
  173. (set! x E)
  174. (val ... E e ...)
  175. (op1 op E)
  176. (op2 op E e)
  177. (op2 op val E)
  178. (if E e e)
  179. (seq E e)
  180. (seq val E)
  181. (let ([x E]) e)
  182. (label lbl E)
  183. (break lbl E)
  184. (throw E)
  185. (catch E e)
  186. (finally E e)))
  187. ;; full terms are of the form
  188. ;; (σ, Σ, Γ, e)
  189. (define s5
  190. (reduction-relation
  191. s5
  192. ;; Binding, variables, and assignment
  193. ;; ----------------------------------
  194. (--> (σ ((loc_1 val_1) ...) ((x_1 loc_2) ...)
  195. (in-hole E (let [x val] e)))
  196. (σ ((loc_1 val_1) ... (loc_new val)) ((x loc_new) (x_1 loc_2) ...)
  197. (in-hole E e))
  198. "E-Let"
  199. (fresh loc_new))
  200. (--> (σ [(loc_1 val_1) ...] [(x_1 loc_2) ...]
  201. (in-hole E (rec [f (λ (x ...) e_1)] e)))
  202. (σ [(loc_1 val_1) ... (loc ([(f loc) (x_1 loc_2) ...] : (λ (x ...) e_1)))]
  203. [(f loc) (x_1 loc_2) ...]
  204. (in-hole E e))
  205. "E-Rec")
  206. (--> (σ [(loc_1 val_1) ...] Γ
  207. (in-hole E (([(y loc_3) ...] : (λ (x ...) e)) val_2 ...)))
  208. (σ [(loc_1 val_1) ... (loc val_2) ...]
  209. [(x loc) ... (y loc_3) ...]
  210. (in-hole E e))
  211. "E-Beta"
  212. (fresh ((loc ...) (val_2 ...)))
  213. (side-condition (= (length (term (val_2 ...)))
  214. (length (term (x ...))))))
  215. (--> (σ Σ Γ (in-hole E (λ (x ...) e)))
  216. (σ Σ Γ (in-hole E (Γ : (λ (x ...) e))))
  217. "E-Γλ")
  218. (--> (σ
  219. ((loc_1 val_1) ... (loc val) (loc_2 val_2) ...)
  220. ((y loc_y) ... (x loc) (z loc_z) ...)
  221. (in-hole E (set! x val_new)))
  222. (σ
  223. ((loc_1 val_1) ... (loc val_new) (loc_2 val_2) ...)
  224. ((y loc_y) ... (x loc) (z loc_z) ...)
  225. (in-hole E val_new))
  226. "E-Set!")
  227. (--> (σ
  228. [(loc_1 val_1) ... (loc val) (loc_2 val_2) ...]
  229. [(y loc_y) ... (x loc) (z val_z) ...]
  230. (in-hole E x))
  231. (σ
  232. [(loc_1 val_1) ... (loc val) (loc_2 val_2) ...]
  233. [(y loc_y) ... (x loc) (z val_z) ...]
  234. (in-hole E val))
  235. (side-condition (not (member (term x) (term (y ...)))))
  236. "E-Ident")
  237. ;; Objects
  238. ;; -------
  239. (--> ([(ref obj) ...] Σ Γ (in-hole E (object obj-attrs [(string property) ...])))
  240. ([(ref_new (obj-attrs [(string property) ...])) (ref obj) ...] Σ Γ
  241. (in-hole E ref_new))
  242. (fresh ref_new))
  243. ;; Field Access
  244. (==> (get-field ref string val_args)
  245. (get-field2 ref ref string val_args)
  246. "E-GetField2")
  247. (--> ([(ref_first obj_first) ...
  248. (ref (obj-attrs [(string_first property_first) ...
  249. (string [(@v val) (@w bool) (@c bool) (@e bool)])
  250. (string_rest property_rest) ...]))
  251. (ref_rest obj_rest) ...]
  252. Σ Γ
  253. (in-hole E (get-field2 ref ref_this string val_args)))
  254. ([(ref_first obj_first) ...
  255. (ref (obj-attrs [(string_first property_first) ...
  256. (string [(@v val) (@w bool) (@c bool) (@e bool)])
  257. (string_rest property_rest) ...]))
  258. (ref_rest obj_rest) ...]
  259. Σ Γ
  260. (in-hole E val))
  261. "E-GetField-Found")
  262. (--> ([(ref_first obj_first) ...
  263. (ref ([(obj-attr_1 val_1) ...
  264. (proto ref_proto)
  265. (obj-attr_2 val_2) ...]
  266. [(string_first property_first) ...]))
  267. (ref_rest obj_rest) ...]
  268. Σ Γ
  269. (in-hole E (get-field2 ref ref_this string val_args)))
  270. ;; -->
  271. ([(ref_first obj_first) ...
  272. (ref ([(obj-attr_1 val_1) ...
  273. (proto ref_proto)
  274. (obj-attr_2 val_2) ...]
  275. [(string_first property_first) ...]))
  276. (ref_rest obj_rest) ...]
  277. Σ Γ
  278. (in-hole E (get-field2 ref_proto ref_this string val_args)))
  279. "E-GetField-Proto"
  280. (side-condition (not (member (term string) (term (string_first ...))))))
  281. (--> ([(ref_1 obj_1) ...
  282. (ref (obj-attrs
  283. [(string_1 property_1) ...
  284. (string [(@g val_getter) (@s val_setter) (@c bool_1) (@e bool_2)])
  285. (string_n property_n) ...]))
  286. (ref_n obj_n) ...]
  287. Σ Γ
  288. (in-hole E (get-field2 ref ref_this string val_args)))
  289. ;; -->
  290. ([(ref_1 obj_1) ...
  291. (ref (obj-attrs
  292. [(string_1 property_1) ...
  293. (string [(@g val_getter) (@s val_setter) (@c bool_1) (@e bool_2)])
  294. (string_n property_n) ...]))
  295. (ref_n obj_n) ...]
  296. Σ Γ
  297. (in-hole E (val_getter ref_this val_args)))
  298. "E-GetField-Getter")
  299. (--> ([(ref_1 obj_1) ...
  300. (ref ([(obj-attr_1 val_1) ...
  301. (proto null)
  302. (obj-attr_n val_n) ...]
  303. [(string property) ...]))
  304. (ref_n obj_n) ...]
  305. Σ Γ
  306. (in-hole E (get-field2 ref ref_this string_lookup val_args)))
  307. ;; -->
  308. ([(ref_1 obj_1) ...
  309. (ref ([(obj-attr_1 val_1) ...
  310. (proto null)
  311. (obj-attr_n val_n) ...]
  312. [(string property) ...]))
  313. (ref_n obj_n) ...]
  314. Σ Γ
  315. (in-hole E undefined))
  316. "E-GetField-Not-Found"
  317. (side-condition (not (member (term string_lookup) (term (string ...))))))
  318. ;; Field Update/Addition
  319. (==> (set-field ref string val_new val_args)
  320. (set-field2 ref ref string val_new val_args))
  321. (--> ([(ref_1 obj_1) ...
  322. (ref (obj-attrs
  323. [(string_1 property_1) ...
  324. (string [(@v val) (@w #t) (@c bool_1) (@e bool_1)])
  325. (string_n property_n) ...]))
  326. (ref_n obj_n) ...]
  327. Σ Γ
  328. (in-hole E (set-field2 ref ref_this string val_new val_args)))
  329. ;; -->
  330. ([(ref_1 obj_1) ...
  331. (ref (obj-attrs
  332. [(string_1 property_1) ...
  333. (string [(@v val_new) (@w #t) (@c bool_1) (@e bool_1)])
  334. (string_n property_n) ...]))
  335. (ref_n obj_n) ...]
  336. Σ Γ
  337. (in-hole E val_new))
  338. "E-SetField")
  339. (--> ([(ref_1 obj_1) ...
  340. (ref ([(obj-attr_1 val_1) ...
  341. (extensible #t)
  342. (obj-attr_n val_n) ...]
  343. [(string property) ...]))
  344. (ref_n obj_n) ...]
  345. Σ Γ
  346. (in-hole E (set-field2 ref ref_this string_lookup val_new val_args)))
  347. ;; -->
  348. ([(ref_1 obj_1) ...
  349. (ref ([(obj-attr_1 val_1) ... (extensible #t) (obj-attr_n val_n) ...]
  350. [(string_lookup [(@v val_new) (@w #t) (@c #t) (@e #t)])
  351. (string property) ...]))
  352. (ref_n obj_n) ...]
  353. Σ Γ
  354. (in-hole E val_new))
  355. "E-SetField-Add"
  356. (side-condition (not (member (term string_lookup) (term (string ...))))))
  357. (--> ([(ref_1 obj_1) ...
  358. (ref ([(obj-attr val) ...]
  359. [(string_1 property_1) ...
  360. (string_x [(@g val_g) (@s val_s) (@c bool_c) (@e bool_e)])
  361. (string_n property_n) ...]))
  362. (ref_n obj_n) ...]
  363. Σ Γ
  364. (in-hole E (set-field2 ref ref_this string_x val_new val_args)))
  365. ;; -->
  366. ([(ref_1 obj_1) ...
  367. (ref ([(obj-attr val) ...]
  368. [(string_1 property_1) ...
  369. (string_x [(@g val_g) (@s val_s) (@c bool_c) (@e bool_e)])
  370. (string_n property_n) ...]))
  371. (ref_n obj_n) ...]
  372. Σ Γ
  373. (in-hole E (seq (val_s ref_this val_args)
  374. val_new)))
  375. "E-SetField-Setter")
  376. ;; boring, standard stuff
  377. (==> (seq val_1 val_2) val_2 "E-Seq-Result")
  378. (==> (if #t e_1 e_2)
  379. e_1
  380. "E-If-True")
  381. (==> (if #f e_1 e_2)
  382. e_2
  383. "E-If-False")
  384. with
  385. [(--> (σ Σ Γ (in-hole E e_1)) (σ Σ Γ (in-hole E e_2)))
  386. (==> e_1 e_2)]
  387. ))