/cpcf/in-racket/lang.rkt

http://github.com/samth/var · Racket · 223 lines · 184 code · 22 blank · 17 comment · 9 complexity · 67a6f5579c2df47db73081e3f37c36bc MD5 · raw file

  1. #lang racket
  2. (require racket/contract)
  3. (require "env.rkt")
  4. (require "nd.rkt")
  5. (provide
  6. (contract-out
  7. [struct PROG ([modules modls?] [main exp?])]
  8. [struct MODL ([name symbol?] [exports (hash/c symbol? CON?)]
  9. [bindings (hash/c symbol? exp?)])]
  10. [struct M-REF ([caller symbol?] [callee symbol?] [name symbol?])]
  11. [struct AP ([func exp?] [args (listof exp?)] [l symbol?])]
  12. [struct IF ([test exp?] [then exp?] [else exp?])]
  13. [struct MU ([x symbol?] [body exp?])]
  14. [struct MON ([lo symbol?] [l+ symbol?] [l- symbol?] [con CON?] [exp exp?])]
  15. [struct BLM ([who symbol?] [whom symbol?])]
  16. [struct PRIM ([x symbol?])]
  17. [struct PRED ([x symbol?])]
  18. [struct LAM ([xs (listof symbol?)] [body exp?] [var-arg? boolean?])]
  19. [struct OPQ ([refinements (set/c CC?)])]
  20. [struct CLO ([e exp?] [ρ env?])]
  21. [struct C-STRUCT ([tag symbol?] [fields (listof C?)])]
  22. [struct C-MON ([lo symbol?] [l+ symbol?] [l- symbol?] [con CC?] [exp VO?])]
  23. [AND (() () #:rest (listof exp?) . ->* . exp?)]
  24. [OR ((symbol?) () #:rest (listof exp?) . ->* . exp?)]
  25. [con? (any/c . -> . boolean?)]
  26. [struct FLAT/C ([exp exp?])]
  27. [struct OR/C ([c1 CON?] [c2 CON?])]
  28. [struct AND/C ([c1 CON?] [c2 CON?])]
  29. [struct STRUCT/C ([tag symbol?] [fields (listof CON?)])]
  30. [struct FUNC/C ([c1 (listof (list/c symbol? CON?))] [c2 CON?] [var-arg? boolean?])]
  31. [struct MU/C ([x symbol?] [body CON?])]
  32. [struct REF/C ([x symbol?])]
  33. [struct CC ([c CON?] [ρ env?])]
  34. [struct STRUCT-MK ([tag symbol?] [field-count integer?])]
  35. [struct STRUCT-AC ([tag symbol?] [field-count integer?] [index integer?])]
  36. [struct STRUCT-P ([tag symbol?] [field-count integer?])]
  37. [struct Π ([accs (listof STRUCT-AC?)] [x symbol?])]
  38. [struct VO ([v (or/c C? CC?)] [o π?])]
  39. [CONS val?] [CONS? val?] [CAR val?] [CDR val?]
  40. [close ((or/c exp? con?) env? . -> . (or/c C? CC?))]
  41. [modls-get-def (modls? symbol? symbol? . -> . exp?)]
  42. [modls-get-con (modls? symbol? symbol? . -> . con?)]
  43. [modls-set-def (modls? symbol? symbol? exp? . -> . modls?)]
  44. [FV (exp? . -> . (set/c symbol?))]
  45. [ (() () #:rest (listof verified?) . ->* . verified?)]
  46. [ (() () #:rest (listof verified?) . ->* . verified?)]
  47. [v¬ (verified? . -> . verified?)])
  48. verified? modls-has? modl-defines? modl-exports? C? π?
  49. base? exp? val? V? modls? ρ0)
  50. (define (set))
  51. (define ρ0 env0)
  52. ;; Program = (prog Modules Exp)
  53. (struct PROG (modules main) #:transparent)
  54. ;; Module = (modl Symbol [Hashtable Symbol Value] [Hashtable Symbol Contract])
  55. (struct MODL (name exports bindings) #:transparent)
  56. ;; Modules = [Hashtable Symbol Module]
  57. (define modls? (hash/c symbol? MODL?))
  58. ;; Module Symbol -> Boolean
  59. (define (modl-exports? m x)
  60. (hash-has-key? (MODL-exports m) x))
  61. (define (modl-defines? m x)
  62. (hash-has-key? (MODL-bindings m) x))
  63. ;; Modules Symbol -> Boolean
  64. (define modls-has? hash-has-key?)
  65. (define (modls-get-def ms m x)
  66. (hash-ref (MODL-bindings (hash-ref ms m)) x))
  67. (define (modls-get-con ms m x)
  68. (hash-ref (MODL-exports (hash-ref ms m)) x))
  69. (define (modls-set-def ms m x e)
  70. (hash-update ms m
  71. (match-lambda
  72. [(MODL name decs defs)
  73. (MODL name decs (hash-set defs x e))])))
  74. ;; Exp = .....
  75. (define (exp? x)
  76. (or (EXP? x) (symbol? x) (val? x)))
  77. (define (ans? x)
  78. (or (ANS? x) (val? x)))
  79. (struct EXP () #:transparent)
  80. (struct ANS EXP () #:transparent)
  81. (struct M-REF EXP (caller callee name) #:transparent)
  82. (struct AP EXP (func args l) #:transparent)
  83. (struct IF EXP (test then else) #:transparent)
  84. (struct MU EXP (x body) #:transparent)
  85. (struct MON EXP (lo l+ l- con exp) #:transparent)
  86. (struct BLM ANS (who whom) #:transparent)
  87. ;; Value = ...
  88. (define (val? x)
  89. (or (LAM? x) (OPQ? x) (base? x) (PRIM? x)
  90. (STRUCT-MK? x) (STRUCT-AC? x) (STRUCT-P? x)))
  91. (define (base? x)
  92. (or (number? x) (string? x) (boolean? x)))
  93. (struct LAM ANS (xs body var-arg?) #:transparent)
  94. (struct OPQ ANS (refinements) #:transparent)
  95. (define (OPQ ))
  96. ;; special operators for structs
  97. (struct STRUCT-MK ANS (tag field-count) #:transparent)
  98. (struct STRUCT-AC ANS (tag field-count index) #:transparent)
  99. (struct STRUCT-P ANS (tag field-count) #:transparent)
  100. (struct PRIM ANS (x) #:transparent)
  101. (struct PRED PRIM () #:transparent)
  102. ;; Closures
  103. (struct C () #:transparent)
  104. (struct CLO C (e ρ) #:transparent)
  105. (struct C-STRUCT (tag fields) #:transparent)
  106. (struct C-MON (lo l+ l- con exp) #:transparent)
  107. (define V?
  108. (match-lambda
  109. [(or [CLO (? val?) _]
  110. [C-STRUCT _ (? [curry andmap V?])]
  111. [C-MON _ _ _ (CC [? FUNC/C?] _) [VO (? V?) _]]) #t]
  112. [_ #f]))
  113. ;; Contracts
  114. (struct CON () #:transparent)
  115. (struct FLAT/C CON (exp) #:transparent)
  116. (struct OR/C CON (c1 c2) #:transparent)
  117. (struct AND/C CON (c1 c2) #:transparent)
  118. (struct STRUCT/C CON (tag fields) #:transparent)
  119. (struct FUNC/C CON (c1 c2 var-arg?) #:transparent)
  120. (struct MU/C CON (x body) #:transparent)
  121. (struct REF/C CON (x) #:transparent)
  122. (define con? CON?)
  123. ;; Closed Contract
  124. (struct CC (c ρ) #:transparent)
  125. (define AND
  126. (match-lambda*
  127. ['() #t]
  128. [`(,e) e]
  129. [`(,e1 ,e2 ...) (IF e1 (apply AND e2) #f)]))
  130. (define (OR m . xs)
  131. (match xs
  132. ['() #f]
  133. [`(,e) e]
  134. [`(,e1 ,e2 ...) (AP
  135. (LAM '(tmp)
  136. (IF 'tmp 'tmp (apply (curry OR m) e2))
  137. #f)
  138. (list e1)
  139. m)]))
  140. (define verified?
  141. (match-lambda
  142. [(or 'Proved 'Refuted 'Neither) #t]
  143. [_ #f]))
  144. ;; cons stuff
  145. (define CONS (STRUCT-MK 'cons 2))
  146. (define CONS? (STRUCT-P 'cons 2))
  147. (define CAR (STRUCT-AC 'cons 2 0))
  148. (define CDR (STRUCT-AC 'cons 2 1))
  149. ;; and/or operators on verification result
  150. (define ( . xs)
  151. (define v2
  152. (match-lambda*
  153. [(or `(Proved ,_) `(,_ Proved)) 'Proved]
  154. [(or `(Neither ,_) `(,_ Neither)) 'Neither]
  155. [_ 'Refuted]))
  156. (foldl v2 'Refuted xs))
  157. (define ( . xs)
  158. (define 2
  159. (match-lambda*
  160. [`(Proved Proved) 'Proved]
  161. [(or `(Refuted ,_) `(,_ Refuted)) 'Refuted]
  162. [_ 'Neither]))
  163. (foldl ∧2 'Proved xs))
  164. (define v¬
  165. (match-lambda
  166. ['Proved 'Refuted]
  167. ['Refuted 'Proved]
  168. [_ 'Neither]))
  169. ;; paths
  170. (struct Π (accs x) #:transparent)
  171. (define (π? x)
  172. (or (Π? x) (equal? x '∅)))
  173. ;; the valid thing that the run-time environment maps to
  174. (struct VO (v o) #:transparent)
  175. ;; returns expression's free variables
  176. (define FV
  177. (match-lambda
  178. [(AP f xs _) (set-union (FV f) (non-det FV xs))]
  179. [(IF e1 e2 e3) (set-union (FV e1) (FV e2) (FV e3))]
  180. [(MU x e) (set-remove (FV e) x)]
  181. [(MON _ _ _ c e) (set-union (FV-c c) (FV e))]
  182. [(LAM xs e _) (set-remove* (FV e) xs)]
  183. [(? symbol? x) {set x}]
  184. [(or [? val?] [? M-REF?] [? BLM?]) ∅]))
  185. (define FV-c
  186. (match-lambda
  187. [(FLAT/C e) (FV e)]
  188. [(or (OR/C c1 c2) (AND/C c1 c2)) (set-union (FV-c c1) (FV-c c2))]
  189. [(STRUCT/C _ cs) (non-det FV-c cs)]
  190. [(FUNC/C `((,x ,c1) ...) c2 _)
  191. (set-union (non-det FV-c c1) (set-remove* (FV-c c2) x))]
  192. [(MU/C x c) (set-remove (FV-c c) x)]
  193. [(REF/C x) {set x}]))
  194. ;; closes expression/contract with environment, discarding unused variables
  195. (define (close x ρ)
  196. (cond
  197. [(exp? x) (CLO x (env-restrict ρ (FV x)))]
  198. [(con? x) (CC x (env-restrict ρ (FV-c x)))]))
  199. (define (set-remove* s l)
  200. (foldl (λ (x s) (set-remove s x)) s l))