/cpcf/with-oc/test-new.rkt

http://github.com/samth/var · Racket · 250 lines · 186 code · 29 blank · 35 comment · 70 complexity · 0dd049d068a8d2812a7c2712194c2b8b MD5 · raw file

  1. #lang racket
  2. (require redex)
  3. (require (only-in "lang-paper.rkt" sλrec [ev ev1]))
  4. (define-extended-language cr sλrec
  5. [e ....
  6. (let [x e] e)
  7. (let ([x e] [x e] ...) e)
  8. (or e e ...)
  9. (and e e ...)
  10. (begin e e ...)
  11. (cond [e e] ... [else e])])
  12. (define-metafunction cr
  13. desug : e -> e
  14. [(desug (λ x e)) (λ x (desug e))]
  15. [(desug (rec [f x] e)) (rec [f x] (desug e))]
  16. [(desug a) a]
  17. [(desug x) x]
  18. [(desug (if e ...)) (if (desug e) ...)]
  19. [(desug (and e)) (desug e)]
  20. [(desug (and e_1 e_2 ...)) (if (desug e_1) (desug (and e_2 ...)) #f)]
  21. [(desug (or e)) (desug e)]
  22. [(desug (or e_1 e_2 ...))
  23. ((λ x_tmp (if x_tmp x_tmp (desug (or e_2 ...)))) (desug e_1))
  24. (where x_tmp ,(variable-not-in (term (e_1 e_2 ...)) (term tmp)))]
  25. [(desug (let [x e_x] e)) ((λ x (desug e)) (desug e_x))]
  26. [(desug (let ([x e_x]) e)) ((λ x (desug e)) (desug e_x))]
  27. [(desug (let ([x_1 e_1] [x_2 e_2] ...) e))
  28. ((λ x_1 (desug (let ([x_2 e_2] ...) e))) (desug e_1))]
  29. [(desug (cond [else e])) (desug e)]
  30. [(desug (cond [e_1 e_2] any ...))
  31. (if (desug e_1) (desug e_2) (desug (cond any ...)))]
  32. [(desug (begin e)) (desug e)]
  33. [(desug (begin e_1 e_2 ...))
  34. ((λ x_tmp (desug (begin e_2 ...))) (desug e_1))
  35. (where x_tmp ,(variable-not-in (term (e_1 e_2 ...)) (term tmp)))]
  36. [(desug (e_0 e_1 e_2 ...)) ((desug e_0) (desug e_1) (desug e_2) ...)])
  37. (define-metafunction cr
  38. ev : e -> (EA ...)
  39. [(ev e) (ev1 (desug e))])
  40. ; f : ( num? str?) num?
  41. ; for example 2
  42. (define f
  43. (term (λ xf
  44. (if (num? xf) (add1 xf) (str-len xf)))))
  45. ; strnum? : Bool
  46. (define strnum?
  47. (term (λ x (or (str? x) (num? x)))))
  48. ; carnum? : (cons ) -> Bool
  49. (define carnum?
  50. (term (λ xcn (num? (car xcn)))))
  51. (for-each
  52. (match-lambda
  53. [`(,e → ,r)
  54. (test-equal (list->set (term (ev ,e))) (list->set r))])
  55. (term
  56. (
  57. ; example 1
  58. [(let [x •]
  59. (if (num? x) (add1 x) 0))
  60. → {0 (• num?)}]
  61. ; example 2
  62. [(let [x •]
  63. (if (or (num? x) (str? x))
  64. (,f x)
  65. "not in f's domain"))
  66. → {(• num?) "not in f's domain"}]
  67. ; example 3, somewhat equivalent, cos i don't have 'member'
  68. [(let [z •]
  69. (let [x (cons? z)]
  70. (if x (cons? z) "FAIL")))
  71. → {#t "FAIL"}]
  72. ; example 4 (already tested in 2, actually)
  73. [(let [x •]
  74. (if (or (num? x) (str? x))
  75. (,f x)
  76. 0))
  77. → {0 (• num?)}]
  78. ; example 5
  79. [(let ([x •] [y •])
  80. (if (or (num? x) (str? x)) ; assummption
  81. (if (and (num? x) (str? y))
  82. (+ x (str-len y))
  83. 0)
  84. "not tested"))
  85. → {0 (• num?) "not tested"}]
  86. ; example 6
  87. [(let ([x •] [y •])
  88. (if (or (num? x) (str? x))
  89. (if (and (num? x) (str? y))
  90. (+ x (str-len y))
  91. (str-len x))
  92. "not tested"))
  93. → {(• num?) "not tested" ERR}]
  94. ; example 7 (no need to test actually, cos and is already a macro)
  95. [(let ([x •] [y •])
  96. (if (if (num? x) (str? y) #f)
  97. (+ x (str-len y))
  98. 0))
  99. → {0 (• num?)}]
  100. ; example 8
  101. [(let [x •]
  102. (if (,strnum? x)
  103. (or (num? x) (str? x))
  104. "else"))
  105. → {#t "else"}]
  106. ; example 9 (no need for test, like and)
  107. [(let [x •]
  108. (if (let [tmp (num? x)]
  109. (if tmp tmp (str? x)))
  110. (,f x)
  111. 0))
  112. → {0 (• num?)}]
  113. ; example 10
  114. [(let [p •]
  115. (if (cons? p)
  116. (if (num? (car p))
  117. (add1 (car p))
  118. 7)
  119. "ignore"))
  120. → {7 (• num?) "ignore"}]
  121. ; example 11
  122. [(let [p (cons • •)]
  123. (if (and (num? (car p)) (num? (cdr p)))
  124. (and (num? (car p)) (num? (cdr p)))
  125. "else"))
  126. → {#t "else"}]
  127. ; example 12
  128. [(let [p (cons • •)]
  129. (if (,carnum? p)
  130. (num? (car p))
  131. "else"))
  132. → (#t "else")]
  133. ; example 13
  134. [(let ([x •] [y •])
  135. (cond
  136. [(and (num? x) (str? y)) (and (num? x) (str? y))]
  137. [(num? x) (and (num? x) (false? (str? y)))]
  138. [else #t]))
  139. → {#t}]
  140. ; example 14
  141. [(let ([input •] [extra •])
  142. (if (and (or (num? input) (str? input))
  143. (cons? extra))
  144. (cond
  145. [(and (num? input) (num? (car extra))) (+ input (car extra))]
  146. [(num? (car extra)) (+ (str-len input) (car extra))]
  147. [else 0])
  148. "ignore"))
  149. → {(• num?) 0 "ignore"}]
  150. ; information is represented in terms of farthest possible variable so it can
  151. ; be retained
  152. [(let (l (cons • •))
  153. (begin
  154. (let (x (car l))
  155. (if (num? x) "ignore" (add1 "raise error")))
  156. ; reached here, (car l) has to be num?
  157. (num? (car l))))
  158. → {#t ERR}]
  159. ; with contracts
  160. #;[(mon (flat num?) •) → {ERR (• num?)}]
  161. #;[(mon (μ list? (or/c (flat false?) (cons/c (flat num?) list?)))
  162. #f) → {#f}]
  163. #;[(mon (μ list? (or/c (flat false?) (cons/c (cons/c (flat num?) (flat num?)) list?)))
  164. (cons • #f)) → {(Cons (Cons (• num?) (• num?)) #f) ERR}]
  165. ; check for proper list (with safe counter to make sure it terminates)
  166. #;[(let (proper-list? (μ (check)
  167. (λ l
  168. (λ n
  169. (cond
  170. [(zero? n) "killed"]
  171. [else (or (false? l)
  172. (and (cons? l)
  173. ((check (cdr l)) (sub1 n))))])))))
  174. ((proper-list? •) 7))
  175. → {#t #f "killed"}]
  176. ; 'lastpair' from Wright paper (with a safe counter to make sure it terminates)
  177. #;[(let [lastpair (μ (lp)
  178. (λ s
  179. (λ n
  180. (cond
  181. [(zero? n) "killed"]
  182. [(cons? (cdr s)) ((lp (cdr s)) (sub1 n))]
  183. [else s]))))]
  184. ((lastpair (cons • •)) 5))
  185. → {"killed" (cons • •)}]
  186. ; previously, precision was lost b/c Γ threw away stuff without 'flushing'
  187. ; them into the environment that closed λ
  188. [(num? ((let [x •]
  189. (if (num? x)
  190. (λ y1 x)
  191. (λ y2 1)))
  192. •))
  193. → {#t}]
  194. ;; Chugh paper examples:
  195. ; negate, section 2.1
  196. ,@ (let* ([bool? (term (λ xb (or (true? xb) (false? xb))))]
  197. [negate (term (λ x
  198. (cond
  199. [(num? x) (+ 0 x)] ; i don't have "-"
  200. [(,bool? x) (false? x)]
  201. [else err])))])
  202. (term {[(num? (,negate ((λ z (if (num? z) z 42)) •))) → {#t}]
  203. [(,negate #t) → {#f}]}))
  204. ;; Linear-log paper examples:
  205. [(let [x (cons • •)] (car x)) → {•}]
  206. [(let [x •] (if (cons? x) (car x) #f)) → {• #f}]
  207. [(let [x •]
  208. ; i replace the latter (car x) with (cons? x)
  209. ; cos i don't know a more obvious way to reflect the
  210. ; learned info in the result
  211. (begin [cdr x] [cons? x]))
  212. → {ERR #t}]
  213. [(let ([x •]
  214. [g (λ y (+ (car y) 1))])
  215. ; i wanna strengthen the original example a bit:
  216. ; if the program survives the call to g, not only
  217. ; we know x is a pair, but also its car an int
  218. (begin [g x]
  219. [and (cons? x) (num? (car x))]))
  220. → {ERR #t}])))
  221. (test-results)