type-var.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ; Copyright (c) 1993-2008 by Richard Kelsey. See file COPYING.
  2. ; Type variables - what a mess
  3. (define-record-type uvar :uvar
  4. (really-make-uvar prefix depth id tuple-okay?
  5. place source binding temp) ; all initialized to #F
  6. uvar?
  7. (prefix uvar-prefix) ; a name for debugging
  8. (depth uvar-depth set-uvar-depth!) ; lexical depth of the uvar
  9. (id uvar-id) ; a number
  10. ; true if this can be unified with a tuple, set when merged
  11. (tuple-okay? uvar-tuple-okay? set-uvar-tuple-okay?!)
  12. (place uvar-place set-uvar-place!) ; used in producing type schemes
  13. (source uvar-source set-uvar-source!)
  14. ; to let the user know where this came from
  15. (binding uvar-binding set-uvar-binding!); known value of this uvar
  16. (temp uvar-temp set-uvar-temp!)) ; useful field
  17. (define-record-discloser :uvar
  18. (lambda (uvar)
  19. (list 'uvar
  20. (uvar-prefix uvar)
  21. (uvar-depth uvar)
  22. (uvar-id uvar)
  23. (uvar-binding uvar))))
  24. (define (make-uvar prefix depth . maybe-id)
  25. (really-make-uvar prefix
  26. depth
  27. (if (null? maybe-id)
  28. (unique-id)
  29. (car maybe-id))
  30. #f ; tuple-okay?
  31. #f #f #f #f)) ; place source binding temp
  32. (define (make-tuple-uvar prefix depth . maybe-id)
  33. (really-make-uvar prefix
  34. depth
  35. (if (null? maybe-id)
  36. (unique-id)
  37. (car maybe-id))
  38. #t ; tuple-okay?
  39. #f #f #f #f)) ; place source binding temp
  40. ; Could this safely short-circuit the chains?
  41. (define (maybe-follow-uvar type)
  42. (cond ((and (uvar? type)
  43. (uvar-binding type))
  44. => maybe-follow-uvar)
  45. (else type)))
  46. ; Substitute VALUE for UVAR, if this will not introduce a circularity.
  47. ; or cause other problems. Returns an error-printing thunk if there is
  48. ; a problem.
  49. (define (bind-uvar! uvar value)
  50. (cond ((uvar? value)
  51. (bind-uvar-to-uvar! uvar value)
  52. #f)
  53. (else
  54. (bind-uvar-to-type! uvar value))))
  55. (define (bind-uvar-to-uvar! uvar0 uvar1)
  56. (minimize-type-depth! uvar1 (uvar-depth uvar0))
  57. (set-uvar-binding! uvar0 uvar1)
  58. (if (and (uvar-tuple-okay? uvar1)
  59. (not (uvar-tuple-okay? uvar0)))
  60. (set-uvar-tuple-okay?! uvar1 #f)))
  61. (define (bind-uvar-to-type! uvar type)
  62. (let ((errors '()))
  63. (if (uvar-in-type? uvar type)
  64. (set! errors (cons circularity-error errors)))
  65. (if (and (tuple-type? type)
  66. (not (uvar-tuple-okay? uvar)))
  67. (set! errors (cons (tuple-error type) errors)))
  68. (cond ((null? errors) ; whew!
  69. (minimize-type-depth! type (uvar-depth uvar))
  70. (set-uvar-binding! uvar type)
  71. #f)
  72. (else
  73. (lambda ()
  74. (format #t "unifying ")
  75. (display-type uvar (current-output-port))
  76. (format #t " == ")
  77. (display-type type (current-output-port))
  78. (format #t "~% would cause the following problem~A:"
  79. (if (null? (cdr errors)) "" "s"))
  80. (for-each (lambda (x) (x)) errors))))))
  81. (define (circularity-error)
  82. (format #t "~% creation of a circular type"))
  83. (define (tuple-error type)
  84. (lambda ()
  85. (if (null? (tuple-type-types type))
  86. (format #t "~% returning no values where one is expected")
  87. (format #t "~% returning ~D values where one is expected"
  88. (length (tuple-type-types type))))))
  89. ; Check that UVAR does not occur in EXP.
  90. (define (uvar-in-type? uvar exp)
  91. (let label ((exp exp))
  92. (cond ((or (base-type? exp)
  93. (record-type? exp))
  94. #f)
  95. ((uvar? exp)
  96. (if (uvar-binding exp)
  97. (label (uvar-binding exp))
  98. (eq? exp uvar)))
  99. ((other-type? exp)
  100. (every? label (other-type-subtypes exp)))
  101. (else
  102. (identity (bug "funny type ~S" exp))))))
  103. ; Make the depths of any uvars in TYPE be no greater than DEPTH.
  104. (define (minimize-type-depth! type depth)
  105. (let label ((type type))
  106. (cond ((other-type? type)
  107. (for-each label (other-type-subtypes type)))
  108. ((uvar? type)
  109. (cond ((uvar-binding type)
  110. => label)
  111. ((< depth (uvar-depth type))
  112. (set-uvar-depth! type depth)))))))
  113. ; Set the depth of all uvars in TYPE to be -1 so that it will not be made
  114. ; polymorphic at any level.
  115. (define (make-nonpolymorphic! type)
  116. (cond ((uvar? type)
  117. (set-uvar-depth! type -1))
  118. ((other-type? type)
  119. (for-each make-nonpolymorphic! (other-type-subtypes type)))
  120. ;((type-scheme? type)
  121. ; (make-nonpolymorphic! (type-scheme-type type)))
  122. ))
  123. ;------------------------------------------------------------
  124. ; Micro utilities
  125. (define *unique-id-counter* 0)
  126. (define (unique-id)
  127. (set! *unique-id-counter* (+ *unique-id-counter* 1))
  128. *unique-id-counter*)
  129. (define (reset-type-vars!)
  130. (set! *unique-id-counter* 0))