recon.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Rudimentary type reconstruction, hardly worthy of the name.
  3. ; Currently, NODE-TYPE is called in two places. One is to determine
  4. ; the type of the right-hand side of a DEFINE for a variable that is
  5. ; never assigned, so uses of the variable can be checked later. The
  6. ; other is when compiling a call, to check types of arguments and
  7. ; produce warning messages.
  8. ; This is heuristic, to say the least. It's not clear what the right
  9. ; interface or formalism is for Scheme; I'm still experimenting.
  10. ; Obviously we can't do Hindley-Milner inference. Not only does
  11. ; Scheme have subtyping, but it also has dependent types up the wazoo.
  12. ; For example, the following is perfectly correct Scheme:
  13. ;
  14. ; (define (foo x y) (if (even? x) (car y) (vector-ref y 3)))
  15. (define (node-type node)
  16. (reconstruct node 'fast any-values-type))
  17. (define (reconstruct-type node env)
  18. (reconstruct node '() any-values-type))
  19. (define (reconstruct node constrained want-type)
  20. ((operator-table-ref reconstructors (node-operator-id node))
  21. node
  22. constrained
  23. want-type))
  24. (define (examine node constrained want-type)
  25. (if (pair? constrained)
  26. (reconstruct node constrained want-type)
  27. want-type))
  28. (define reconstructors
  29. (make-operator-table (lambda (node constrained want-type)
  30. (reconstruct-call (node-form node)
  31. constrained
  32. want-type))))
  33. (define (define-reconstructor name type proc)
  34. (operator-define! reconstructors name type proc))
  35. (define-reconstructor 'lambda syntax-type
  36. (lambda (node constrained want-type)
  37. (reconstruct-lambda node constrained want-type #f)))
  38. (define-reconstructor 'flat-lambda syntax-type
  39. (lambda (node constrained want-type)
  40. (reconstruct-lambda node constrained want-type #f)))
  41. (define (reconstruct-lambda node constrained want-type called?)
  42. (if (eq? constrained 'fast)
  43. any-procedure-type
  44. (let* ((form (node-form node))
  45. (want-result (careful-codomain want-type))
  46. (formals (cadr form))
  47. (alist (map (lambda (node)
  48. (cons node value-type))
  49. (normalize-formals formals)))
  50. (cod (reconstruct (last form) ; works for normal and flat
  51. (if called?
  52. (append alist constrained)
  53. alist)
  54. want-result)))
  55. (procedure-type (if (n-ary? formals)
  56. any-values-type ;lose
  57. (make-some-values-type (map cdr alist)))
  58. cod
  59. #t))))
  60. (define (careful-codomain proc-type)
  61. (if (procedure-type? proc-type)
  62. (procedure-type-codomain proc-type)
  63. any-values-type))
  64. (define-reconstructor 'name 'leaf
  65. (lambda (node constrained want-type)
  66. (if (eq? constrained 'fast)
  67. (reconstruct-name node)
  68. (let ((z (assq node constrained)))
  69. (if z
  70. (let ((type (meet-type (cdr z) want-type)))
  71. (begin (set-cdr! z type)
  72. type))
  73. (reconstruct-name node))))))
  74. (define (reconstruct-name node)
  75. (let ((probe (node-ref node 'binding)))
  76. (if (binding? probe)
  77. (let ((type (binding-type probe)))
  78. (cond ((variable-type? type)
  79. (variable-value-type type))
  80. ((subtype? type value-type)
  81. type)
  82. (else
  83. value-type)))
  84. value-type)))
  85. (define-reconstructor 'call 'internal
  86. (lambda (node constrained want-type)
  87. (let ((form (node-form node)))
  88. (cond ((proc->reconstructor (car form))
  89. => (lambda (recon)
  90. (recon (cdr form) constrained want-type)))
  91. (else
  92. (reconstruct-call form constrained want-type))))))
  93. ; See if PROC is a primop or a variable bound to a primop, and then return
  94. ; that primops reconstructor, if it has one.
  95. (define (proc->reconstructor proc)
  96. (cond ((name-node? proc)
  97. (let ((probe (node-ref proc 'binding)))
  98. (if (and probe
  99. (binding? probe)
  100. (primop? (binding-static probe)))
  101. (table-ref primop-reconstructors
  102. (binding-static probe))
  103. #f)))
  104. ((literal-node? proc)
  105. (if (primop? (node-form proc))
  106. (table-ref primop-reconstructors
  107. (node-form proc))
  108. #f))
  109. (else #f)))
  110. (define (reconstruct-call form constrained want-type)
  111. (let* ((want-op-type (procedure-type any-arguments-type
  112. want-type
  113. #f))
  114. (op-type (if (lambda-node? (car form))
  115. (reconstruct-lambda (car form)
  116. constrained
  117. want-op-type
  118. #t)
  119. (reconstruct (car form)
  120. constrained
  121. want-op-type)))
  122. (args (cdr form))
  123. (lose (lambda ()
  124. (for-each (lambda (arg)
  125. (examine arg constrained value-type))
  126. args))))
  127. (if (procedure-type? op-type)
  128. (begin (if (restrictive? op-type)
  129. (let loop ((args args)
  130. (dom (procedure-type-domain op-type)))
  131. (if (not (or (null? args)
  132. (empty-rail-type? dom)))
  133. (begin (examine (car args)
  134. constrained
  135. (head-type dom))
  136. (loop (cdr args) (tail-type dom)))))
  137. (lose))
  138. (procedure-type-codomain op-type))
  139. (begin (lose)
  140. any-values-type))))
  141. (define-reconstructor 'literal 'leaf
  142. (lambda (node constrained want-type)
  143. (constant-type (node-form node))))
  144. (define-reconstructor 'quote syntax-type
  145. (lambda (node constrained want-type)
  146. (constant-type (cadr (node-form node)))))
  147. (define-reconstructor 'unspecific #f
  148. (lambda (node constrained wnat-type)
  149. unspecific-type))
  150. (define-reconstructor 'unassigned #f
  151. (lambda (node constrained wnat-type)
  152. unspecific-type))
  153. (define-reconstructor 'if syntax-type
  154. (lambda (node constrained want-type)
  155. (let ((form (node-form node)))
  156. (examine (cadr form) constrained value-type)
  157. ;; Fork off two different constrain sets
  158. (let ((con-alist (fork-constraints constrained))
  159. (alt-alist (fork-constraints constrained)))
  160. (let ((con-type (reconstruct (caddr form) con-alist want-type))
  161. (alt-type (reconstruct (cadddr form) alt-alist want-type)))
  162. (if (pair? constrained)
  163. (for-each (lambda (c1 c2 c)
  164. (set-cdr! c (join-type (cdr c1) (cdr c2))))
  165. con-alist
  166. alt-alist
  167. constrained))
  168. (join-type con-type alt-type))))))
  169. (define (fork-constraints constrained)
  170. (if (pair? constrained)
  171. (map (lambda (x) (cons (car x) (cdr x)))
  172. constrained)
  173. constrained))-
  174. (define-reconstructor 'begin syntax-type
  175. (lambda (node constrained want-type)
  176. ;; This is unsound - there might be a throw out of some subform
  177. ;; other than the final one.
  178. (do ((forms (cdr (node-form node)) (cdr forms)))
  179. ((null? (cdr forms))
  180. (reconstruct (car forms) constrained want-type))
  181. (examine (car forms) constrained any-values-type))))
  182. (define-reconstructor 'set! syntax-type
  183. (lambda (node constrained want-type)
  184. (examine (caddr (node-form node)) constrained value-type)
  185. unspecific-type))
  186. (define-reconstructor 'letrec syntax-type
  187. (lambda (node constrained want-type)
  188. (let ((form (node-form node)))
  189. (reconstruct-letrec (cadr form) (caddr form) constrained want-type))))
  190. (define-reconstructor 'pure-letrec syntax-type
  191. (lambda (node constrained want-type)
  192. (let ((form (node-form node)))
  193. (reconstruct-letrec (cadr form) (cadddr form) constrained want-type))))
  194. (define (reconstruct-letrec specs body constrained want-type)
  195. (if (eq? constrained 'fast)
  196. (reconstruct body 'fast want-type)
  197. (let ((alist (map (lambda (spec)
  198. (cons (car spec)
  199. (reconstruct (cadr spec)
  200. constrained
  201. value-type)))
  202. specs)))
  203. (reconstruct body
  204. (append alist constrained)
  205. want-type))))
  206. (define-reconstructor 'loophole syntax-type
  207. (lambda (node constrained want-type)
  208. (let ((args (cdr (node-form node))))
  209. (examine (cadr args) constrained any-values-type)
  210. (car args))))
  211. (define (node->type node)
  212. (if (node? node)
  213. (let ((form (node-form node)))
  214. (if (pair? form)
  215. (map node->type form)
  216. (desyntaxify form)))
  217. (desyntaxify node)))
  218. (define-reconstructor 'define syntax-type
  219. (lambda (node constrained want-type)
  220. ':definition))
  221. (define-reconstructor 'lap syntax-type
  222. (lambda (node constrained want-type)
  223. any-procedure-type))
  224. ; --------------------
  225. ; Primops.
  226. ;
  227. ; Most primops just have the types assigned in comp-prim.scm.
  228. (define primop-reconstructors (make-symbol-table))
  229. (define (define-primop-reconstructor name proc)
  230. (table-set! primop-reconstructors name proc))
  231. (define-reconstructor 'primitive-procedure syntax-type
  232. (lambda (node constrained want-type)
  233. (primop-type (get-primop (cadr (node-form node))))))
  234. (define-primop-reconstructor 'values
  235. (lambda (args constrained want-type)
  236. (make-some-values-type (map (lambda (node)
  237. (meet-type
  238. (reconstruct node constrained value-type)
  239. value-type))
  240. args))))
  241. (define-primop-reconstructor 'call-with-values
  242. (lambda (args constrained want-type)
  243. (if (= (length args) 2)
  244. (let ((thunk-type (reconstruct (car args)
  245. constrained
  246. (procedure-type empty-rail-type
  247. any-values-type
  248. #f))))
  249. (careful-codomain
  250. (reconstruct (cadr args)
  251. constrained
  252. (procedure-type (careful-codomain thunk-type)
  253. any-values-type
  254. #f))))
  255. error-type)))
  256. (define (reconstruct-apply args constrained want-type)
  257. (if (not (null? args))
  258. (let ((proc-type (reconstruct (car args)
  259. constrained
  260. any-procedure-type)))
  261. (for-each (lambda (arg) (examine arg constrained value-type))
  262. (cdr args))
  263. (careful-codomain proc-type))
  264. error-type))
  265. (define-primop-reconstructor 'apply reconstruct-apply)
  266. (define-primop-reconstructor 'primitive-catch reconstruct-apply)
  267. (define (constant-type x)
  268. (cond ((number? x)
  269. (meet-type (if (exact? x) exact-type inexact-type)
  270. (cond ((integer? x) integer-type)
  271. ((rational? x) rational-type)
  272. ((real? x) real-type)
  273. ((complex? x) complex-type)
  274. (else number-type))))
  275. ((boolean? x) boolean-type)
  276. ((pair? x) pair-type)
  277. ((string? x) string-type)
  278. ((char? x) char-type)
  279. ((null? x) null-type)
  280. ((symbol? x) symbol-type)
  281. ((primop? x) (primop-type x))
  282. ((vector? x) vector-type)
  283. (else value-type)))