mtype.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Jonathan Rees, Martin Gasbichler, Mike Sperber, Robert Ransom
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/bcomp/mtype.scm
  8. ;;;
  9. ;;; Type lattice.
  10. ;;; Sorry this is so hairy, but before it was written, type checking
  11. ;;; consumed 15% of compile time.
  12. (define-module (prescheme bcomp mtype)
  13. #:use-module (srfi srfi-9)
  14. #:use-module (prescheme scheme48)
  15. #:use-module (prescheme record-discloser)
  16. #:export (;; same-type? ;; conflicts with `same-type?' from methods
  17. subtype?
  18. meet?
  19. join-type
  20. meet-type
  21. sexp->type type->sexp rail-type->sexp
  22. syntax-type
  23. any-values-type
  24. any-arguments-type
  25. value-type value-type?
  26. error-type
  27. make-some-values-type
  28. empty-rail-type
  29. rail-type
  30. make-optional-type
  31. make-rest-type
  32. empty-rail-type?
  33. rest-type?
  34. head-type
  35. tail-type
  36. procedure-type
  37. procedure-type-domain
  38. procedure-type-codomain
  39. restrictive?
  40. procedure-type?
  41. fixed-arity-procedure-type?
  42. procedure-type-argument-types
  43. procedure-type-arity
  44. any-procedure-type
  45. proc
  46. boolean-type
  47. char-type
  48. null-type
  49. unspecific-type
  50. exact-integer-type
  51. integer-type
  52. rational-type
  53. real-type
  54. complex-type
  55. number-type
  56. exact-type
  57. inexact-type
  58. pair-type
  59. string-type
  60. symbol-type
  61. vector-type
  62. escape-type
  63. structure-type
  64. ;; Stuff moved back from syntactic - why was it moved there?
  65. variable-type
  66. variable-type?
  67. variable-value-type
  68. usual-variable-type
  69. undeclared-type
  70. compatible-types?))
  71. (define-record-type :meta-type
  72. (really-make-type mask more info)
  73. meta-type?
  74. (mask type-mask)
  75. (more type-more)
  76. (info type-info))
  77. ;; MASK is a bit set. The current bits are:
  78. ;;
  79. ;; Non values:
  80. ;; syntax
  81. ;; other static type
  82. ;; no values - indicates an optional type; the type with only this bit set
  83. ;; is the empty rail type.
  84. ;; two or more - indicates a rail-type with at least two elements
  85. ;;
  86. ;; Values:
  87. ;; exact integer
  88. ;; integer
  89. ;; exact rational
  90. ;; rational
  91. ;; exact real
  92. ;; real
  93. ;; exact complex
  94. ;; complex
  95. ;; other exact number
  96. ;; other number
  97. ;; boolean
  98. ;; pair
  99. ;; null
  100. ;; record
  101. ;; procedure
  102. ;; other
  103. ;;
  104. ;; The MORE field is only used for rail types, which are like ML's tuples.
  105. ;; If the TWO-OR-MORE? bit is set, then
  106. ;; more = (head . tail).
  107. ;; Otherwise, more = #f.
  108. ;;
  109. ;; For procedure types, the PROCEDURE bit is set and the INFO field is a three
  110. ;; element list: (domain codomain restrictive?)
  111. ;; If INFO field for the type of F is (t1 t2 #t), then
  112. ;; if x : t1 then (f x) : t2 (possible error!), else (f x) : error.
  113. ;; If INFO field for the type of F is (t1 t2 #f), then
  114. ;; there exists an x : t1 such that (f x) : t2.
  115. ;;
  116. ;; For types which do not have bits, the OTHER bit is set and the INFO field is
  117. ;; a symbol naming some type that doesn't have its own bit in the mask. The
  118. ;; other types defined in this file are:
  119. ;;
  120. ;; :char
  121. ;; :unspecific
  122. ;; :string
  123. ;; :symbol
  124. ;; :vector
  125. ;; :escape
  126. ;; :structure
  127. ;;
  128. ;; More are constructed later by using SEXP->TYPE.
  129. (define-record-discloser :meta-type
  130. (lambda (t)
  131. `(type ,(let ((m (type-mask t)))
  132. (or (table-ref mask->name-table m)
  133. m))
  134. ,(let ((more (type-more t)))
  135. (if (and (pair? more) (eq? (cdr more) t))
  136. '*
  137. more))
  138. ,(type-info t))))
  139. (define (make-type mask more info)
  140. (make-immutable!
  141. (really-make-type mask more info)))
  142. (define name->type-table (make-table))
  143. (define mask->name-table (make-table))
  144. (define (name->type x)
  145. (or (table-ref name->type-table x)
  146. (make-other-type x)))
  147. (define (set-type-name! type name)
  148. (table-set! name->type-table name type)
  149. (if (not (or (type-info type)
  150. (type-more type)))
  151. (table-set! mask->name-table (type-mask type) name)))
  152. ;; Masks
  153. ;; Top of lattice has mask = -1, bottom has mask = 0.
  154. (define *mask* 1)
  155. (define (new-type-bit)
  156. (let ((m *mask*))
  157. (set! *mask* (arithmetic-shift *mask* 1))
  158. m))
  159. (define (mask->type mask)
  160. (make-type mask #f #f))
  161. (define bottom-type (mask->type 0))
  162. (define error-type bottom-type)
  163. (define (bottom-type? t)
  164. (= (type-mask t) 0))
  165. (set-type-name! bottom-type ':error)
  166. (define (new-atomic-type)
  167. (mask->type (new-type-bit)))
  168. (define (named-atomic-type name)
  169. (let ((t (new-atomic-type)))
  170. (set-type-name! t name)
  171. t))
  172. ;; --------------------
  173. ;; Top of the lattice.
  174. (define syntax-type (named-atomic-type ':syntax))
  175. (define other-static-type (new-atomic-type))
  176. ;; --------------------
  177. ;; "Rails" are argument sequence or return value sequences.
  178. ;; Four constructors:
  179. ;; empty-rail-type
  180. ;; (rail-type t1 t2)
  181. ;; (optional-rail-type t1 t2)
  182. ;; (make-rest-type t)
  183. ;; If a type's two-or-more? bit is set, then
  184. ;; more = (head . tail).
  185. ;; Otherwise, more = #f.
  186. (define empty-rail-type (new-atomic-type))
  187. (define (rail-type t1 t2) ;;CONS analog
  188. (cond ((empty-rail-type? t2) t1)
  189. ((bottom-type? t1) t1)
  190. ((bottom-type? t2) t2)
  191. ((and (optional-type? t1)
  192. (rest-type? t2)
  193. (same-type? t1 (head-type t2)))
  194. ;; Turn (&opt t &rest t) into (&rest t)
  195. t2)
  196. ((or (optional-type? t1)
  197. (optional-type? t2))
  198. (make-type (bitwise-ior (type-mask t1) mask/two-or-more)
  199. (make-immutable! (cons t1 t2))
  200. #f))
  201. (else
  202. (make-type mask/two-or-more
  203. (make-immutable! (cons t1 t2))
  204. (type-info t1)))))
  205. (define (make-optional-type t)
  206. (if (type-more t)
  207. (warning 'make-optional-type "peculiar type in make-optional-type" t))
  208. (make-type (bitwise-ior (type-mask t) mask/no-values)
  209. #f
  210. (type-info t)))
  211. ;; A rest type is an infinite rail type with both the no-values and the
  212. ;; two-or-more bits set.
  213. (define (make-rest-type t)
  214. (if (bottom-type? t)
  215. t
  216. (let* ((z (cons (make-optional-type t) #f))
  217. (t (make-type (bitwise-ior (type-mask t) mask/&rest)
  218. z
  219. (type-info t))))
  220. (set-cdr! z t)
  221. (make-immutable! z)
  222. t)))
  223. (define (head-type t) ;;Can return an &opt type
  224. (let ((more (type-more t)))
  225. (if more
  226. (car more)
  227. t)))
  228. (define (head-type-really t) ;;Always returns a value type
  229. (let ((h (head-type t)))
  230. (if (optional-type? h)
  231. (make-type (bitwise-and (type-mask h) (bitwise-not mask/no-values))
  232. #f
  233. (type-info h))
  234. h)))
  235. (define (tail-type t)
  236. (if (empty-rail-type? t)
  237. ;; bottom-type ?
  238. (warning 'tail-type "rail-type of empty rail" t))
  239. (let ((more (type-more t)))
  240. (if more
  241. (cdr more)
  242. empty-rail-type)))
  243. (define (empty-rail-type? t)
  244. (= (bitwise-and (type-mask t) mask/one-or-more) 0))
  245. (define (rest-type? t) ;;For terminating recursions
  246. (let ((more (type-more t)))
  247. (and more
  248. (eq? (cdr more) t))))
  249. (define (optional-type? t)
  250. (> (bitwise-and (type-mask t) mask/no-values) 0))
  251. ;; The no-values type has one element, the rail of length zero.
  252. ;; The two-or-more type consists of all rails of length two
  253. ;; or more.
  254. (define mask/no-values (type-mask empty-rail-type))
  255. (define mask/two-or-more (new-type-bit))
  256. (define mask/&rest (bitwise-ior (type-mask empty-rail-type)
  257. mask/two-or-more))
  258. (table-set! mask->name-table mask/no-values ':no-values)
  259. (define value-type (mask->type (bitwise-not (- *mask* 1))))
  260. (set-type-name! value-type ':value)
  261. (define mask/value (type-mask value-type))
  262. (define (value-type? t)
  263. (let ((m (type-mask t)))
  264. (= (bitwise-and m mask/value) m)))
  265. (define any-values-type
  266. (make-rest-type value-type))
  267. (set-type-name! any-values-type ':values)
  268. (define any-arguments-type any-values-type)
  269. (define mask/one-or-more
  270. (bitwise-ior mask/value mask/two-or-more))
  271. ;; --------------------
  272. ;; Lattice operations.
  273. ;; Equivalence
  274. (define (same-type? t1 t2)
  275. (or (eq? t1 t2)
  276. (and (= (type-mask t1) (type-mask t2))
  277. (let ((more1 (type-more t1))
  278. (more2 (type-more t2)))
  279. (if more1
  280. (and more2
  281. (if (eq? (cdr more1) t1)
  282. (eq? (cdr more2) t2)
  283. (if (eq? (cdr more2) t2)
  284. #f
  285. (and (same-type? (car more1) (car more2))
  286. (same-type? (cdr more1) (cdr more2))))))
  287. (not more2)))
  288. (let ((info1 (type-info t1))
  289. (info2 (type-info t2)))
  290. (or (eq? info1 info2) ;; takes care of OTHER types
  291. (and (pair? info1) ;; check for same procedure types
  292. (pair? info2)
  293. (same-type? (car info1) (car info2))
  294. (same-type? (cadr info1) (cadr info2))
  295. (eq? (caddr info1) (caddr info2))))))))
  296. (define (subtype? t1 t2) ;*** optimize later
  297. (same-type? t1 (meet-type t1 t2)))
  298. ; (mask->type mask/procedure) represents the TOP of the procedure
  299. ; subhierarchy.
  300. (define (meet-type t1 t2)
  301. (if (same-type? t1 t2)
  302. t1
  303. (let ((m (bitwise-and (type-mask t1) (type-mask t2))))
  304. (cond ((> (bitwise-and m mask/two-or-more) 0)
  305. (meet-rail t1 t2))
  306. ((eq? (type-info t1) (type-info t2))
  307. (make-type m #f (type-info t1)))
  308. ((> (bitwise-and m mask/other) 0)
  309. (let ((i1 (other-type-info t1))
  310. (i2 (other-type-info t2)))
  311. (if (and i1 i2)
  312. (mask->type (bitwise-and m (bitwise-not mask/other)))
  313. (make-type m
  314. #f
  315. (or i1 i2)))))
  316. ((> (bitwise-and m mask/procedure) 0)
  317. (meet-procedure m t1 t2))
  318. (else
  319. (mask->type m))))))
  320. (define (other-type-info t)
  321. (let ((i (type-info t)))
  322. (if (pair? i) #f i)))
  323. ;(define (p name x) (write `(,name ,x)) (newline) x)
  324. (define (meet-rail t1 t2)
  325. (let ((t (meet-type (head-type t1)
  326. (head-type t2))))
  327. (if (and (rest-type? t1)
  328. (rest-type? t2))
  329. (make-rest-type t)
  330. (rail-type t (meet-type (tail-type t1)
  331. (tail-type t2))))))
  332. ; Start with these assumptions:
  333. ;
  334. ; . (meet? t1 t2) == (not (bottom-type? (meet-type t1 t2)))
  335. ; . (subtype? t1 t2) == (same-type? t1 (meet-type t1 t2))
  336. ; . (subtype? t1 t2) == (same-type? t2 (join-type t1 t2))
  337. ; . We signal a type error if not (intersect? have want).
  338. ; . We infer the type of a parameter by intersecting the want-types
  339. ; of all definitely-reached points of use.
  340. ;
  341. ; 1. If both types are nonrestrictive, we have to JOIN both domains
  342. ; and codomains (if we are to avoid conjunctive types).
  343. ;
  344. ; (+ (f 1) (car (f 'a))) [reconstructing type of f by computing meet of all contexts]
  345. ; => meet (proc (:integer) :number nonr) (proc (:symbol) :pair nonr)
  346. ; => (proc ((join :integer :symbol)) (join :number :pair) nonr), yes?
  347. ;
  348. ; 2. If both types are restrictive, we need to MEET both domains and
  349. ; codomains.
  350. ;
  351. ; (define (foo) 3), (export (foo (proc (:value) :value)))
  352. ; Error - disjoint domains.
  353. ;
  354. ; (define (foo) 'baz), (export (foo (proc () :number)))
  355. ; Error - disjoint codomains.
  356. ;
  357. ; 3. If one is restrictive and the other isn't then we still need to
  358. ; MEET on both sides.
  359. ;
  360. ; (with-output-to-file "foo" car)
  361. ; => meet (proc () :any nonr), (proc (:pair) :value restr)
  362. ; => Error - disjoint domains.
  363. ;
  364. ; (frob (lambda () 'a)) where (define (frob f) (+ (f) 1))
  365. ; => meet (proc () :symbol restr), (proc () :number nonr)
  366. ; => Error - disjoint codomains.
  367. ;
  368. ; Does export checking look for (intersect? want have), or for
  369. ; (subtype? want have) ? We should be able to narrow something as we
  370. ; export it, but not widen it.
  371. ;
  372. ; (define (foo . x) 3), (export (foo (proc (value) value)))
  373. ; No problem, since the domain of the first contains the domain of the second.
  374. ;
  375. ; (define (foo x . y) (+ x 3)), (export (foo (proc (value) value)))
  376. ; Dubious; the domains intersect but are incomparable. The meet
  377. ; should be (proc (number) number).
  378. ;
  379. ; (define (foo x) (numerator x)), (export (foo (proc (real) integer)))
  380. ; This is dubious, since the stated domain certainly contains values
  381. ; that will be rejected. (But then, what about divide by zero, or
  382. ; vector indexing?)
  383. ;
  384. ; (define (foo x) (numerator x)), (export (foo (proc (integer) integer)))
  385. ; This should definitely be OK.
  386. (define (meet-procedure m t1 t2)
  387. (let ((dom1 (procedure-type-domain t1))
  388. (dom2 (procedure-type-domain t2))
  389. (cod1 (procedure-type-codomain t1))
  390. (cod2 (procedure-type-codomain t2)))
  391. (cond ((or (restrictive? t1)
  392. (restrictive? t2))
  393. (let ((dom (meet-type dom1 dom2))
  394. (cod (meet-type cod1 cod2)))
  395. (if (or (bottom-type? dom)
  396. (and (bottom-type? cod)
  397. (not (bottom-type? cod1)) ;uck
  398. (not (bottom-type? cod2))))
  399. (mask->type (bitwise-and m (bitwise-not mask/procedure)))
  400. (make-procedure-type m
  401. dom
  402. cod
  403. #t))))
  404. ((and (subtype? dom2 dom1)
  405. (subtype? cod2 cod1))
  406. ;; exists x : dom1 s.t. (f x) : cod1 adds no info
  407. (make-procedure-type m dom2 cod2 #f))
  408. (else
  409. ;; Arbitrary choice.
  410. (make-procedure-type m dom1 cod1 #f)))))
  411. ; MEET? is the operation used all the time by the compiler. We want
  412. ; getting a yes answer to be as fast as possible. We could do
  413. ;
  414. ; (define (meet? t1 t2) (not (bottom-type? (meet-type t1 t2))))
  415. ;
  416. ; but that would be too slow.
  417. (define (meet? t1 t2)
  418. (or (eq? t1 t2)
  419. (let ((m (bitwise-and (type-mask t1)
  420. (type-mask t2))))
  421. (cond ((= m mask/two-or-more)
  422. (and (meet? (head-type t1)
  423. (head-type t2))
  424. (meet? (tail-type t1)
  425. (tail-type t2))))
  426. ((= m 0)
  427. #f)
  428. ((eq? (type-info t1)
  429. (type-info t2))
  430. #t)
  431. ((= m mask/other)
  432. (not (and (other-type-info t1)
  433. (other-type-info t2))))
  434. ((= m mask/procedure)
  435. (meet-procedure? t1 t2))
  436. (else
  437. #t)))))
  438. (define (meet-procedure? t1 t2)
  439. (if (or (restrictive? t1)
  440. (restrictive? t2))
  441. (and (meet? (procedure-type-domain t1)
  442. (procedure-type-domain t2))
  443. (meet? (procedure-type-codomain t1)
  444. (procedure-type-codomain t2)))
  445. #t))
  446. ; Join
  447. (define (join-type t1 t2)
  448. (if (same-type? t1 t2)
  449. t1
  450. (let ((m (bitwise-ior (type-mask t1)
  451. (type-mask t2))))
  452. (if (> (bitwise-and m mask/two-or-more) 0)
  453. (join-rail t1 t2)
  454. (let ((info1 (type-info t1))
  455. (info2 (type-info t2)))
  456. (cond ((equal? info1 info2)
  457. (make-type m #f (type-info t1)))
  458. ((> (bitwise-and m mask/other) 0)
  459. (make-type m #f #f))
  460. ((> (bitwise-and m mask/procedure) 0)
  461. (join-procedure m t1 t2))
  462. (else
  463. (assertion-violation 'join-type "This shouldn't happen" t1 t2))))))))
  464. (define (join-rail t1 t2)
  465. (let ((t (join-type (head-type t1) (head-type t2))))
  466. (if (and (rest-type? t1)
  467. (rest-type? t2))
  468. (make-rest-type t)
  469. (rail-type t
  470. (if (type-more t1)
  471. (if (type-more t2)
  472. (join-type (tail-type t1)
  473. (tail-type t2))
  474. (tail-type t1))
  475. (tail-type t2))))))
  476. ; This is pretty gross.
  477. (define (join-procedure m t1 t2)
  478. (if (procedure-type? t1)
  479. (if (procedure-type? t2)
  480. (let ((dom1 (procedure-type-domain t1))
  481. (dom2 (procedure-type-domain t2))
  482. (cod1 (procedure-type-codomain t1))
  483. (cod2 (procedure-type-codomain t2)))
  484. (make-procedure-type m
  485. (join-type dom1 dom2) ;Error when outside here
  486. (join-type cod1 cod2)
  487. (and (restrictive? t1)
  488. (restrictive? t2))))
  489. (make-type m #f (type-info t1)))
  490. (make-type m #f (type-info t2))))
  491. ; --------------------
  492. ; Value types.
  493. ; First, the ten indivisible number types.
  494. (define number-hierarchy
  495. '(:integer :rational :real :complex :number))
  496. (let loop ((names number-hierarchy)
  497. (exact bottom-type)
  498. (inexact bottom-type))
  499. (if (null? names)
  500. (begin (set-type-name! exact ':exact)
  501. (set-type-name! inexact ':inexact))
  502. (let* ((exact (join-type exact (new-atomic-type)))
  503. (inexact (join-type inexact (new-atomic-type))))
  504. (set-type-name! (join-type exact inexact)
  505. (car names))
  506. (loop (cdr names)
  507. exact
  508. inexact))))
  509. (define integer-type (name->type ':integer))
  510. (define rational-type (name->type ':rational))
  511. (define real-type (name->type ':real))
  512. (define complex-type (name->type ':complex))
  513. (define number-type (name->type ':number))
  514. (define exact-type (name->type ':exact))
  515. (define inexact-type (name->type ':inexact))
  516. (define exact-integer-type (meet-type integer-type exact-type))
  517. (set-type-name! exact-integer-type ':exact-integer)
  518. (define inexact-real-type (meet-type real-type inexact-type))
  519. (set-type-name! inexact-real-type ':inexact-real)
  520. ; Next, all the others.
  521. (define boolean-type (named-atomic-type ':boolean))
  522. (define pair-type (named-atomic-type ':pair))
  523. (define null-type (named-atomic-type ':null))
  524. (define record-type (named-atomic-type ':record))
  525. (define any-procedure-type (named-atomic-type ':procedure))
  526. ; ???
  527. ; (define procedure-nonbottom-type (new-atomic-type))
  528. ; (define procedure-bottom-type (new-atomic-type))
  529. ; (define mask/procedure (meet procedure-nonbottom-type procedure-bottom-type))
  530. ; OTHER-VALUE-TYPE is a catchall for all the other ones we don't
  531. ; anticipate (for now including string, vector, char, etc.).
  532. (define other-value-type (named-atomic-type ':other))
  533. (define mask/other (type-mask other-value-type))
  534. (define (make-other-type id)
  535. (let ((t (make-type mask/other #f id)))
  536. (set-type-name! t id)
  537. t))
  538. (define char-type (make-other-type ':char))
  539. (define unspecific-type (make-other-type ':unspecific))
  540. (define string-type (make-other-type ':string))
  541. (define symbol-type (make-other-type ':symbol))
  542. (define vector-type (make-other-type ':vector))
  543. (define escape-type (make-other-type ':escape))
  544. (define structure-type (make-other-type ':structure))
  545. ; --------------------
  546. ; Procedures.
  547. (define mask/procedure (type-mask any-procedure-type))
  548. (define (procedure-type dom cod r?)
  549. (make-procedure-type mask/procedure dom cod r?))
  550. (define (make-procedure-type m dom cod r?)
  551. (make-type m
  552. #f
  553. (if (and (not r?)
  554. (same-type? dom value-type)
  555. (same-type? cod value-type))
  556. #f ;LUB of all procedure types
  557. (list dom cod r?))))
  558. (define (procedure-type-domain t)
  559. (let ((info (type-info t)))
  560. (if (pair? info)
  561. (car info)
  562. any-values-type)))
  563. (define (procedure-type-codomain t)
  564. (let ((info (type-info t)))
  565. (if (pair? info)
  566. (cadr info)
  567. any-values-type)))
  568. (define (restrictive? t)
  569. (let ((info (type-info t)))
  570. (if (pair? info)
  571. (caddr info)
  572. #f)))
  573. ; --------------------
  574. ; Conversion to and from S-expression.
  575. (define (sexp->type x r?)
  576. (cond ((symbol? x)
  577. (name->type x))
  578. ((pair? x)
  579. (case (car x)
  580. ((some-values)
  581. (sexp->values-type (cdr x) #t r?))
  582. ((proc procedure-type)
  583. (let ((r? (if (or (null? (cdddr x))
  584. (eq? (cadddr x) r?))
  585. r?
  586. (not r?))))
  587. (procedure-type (sexp->values-type (cadr x) #t (not r?))
  588. (sexp->type (caddr x) r?)
  589. r?)))
  590. ((meet)
  591. (if (null? (cdr x))
  592. bottom-type
  593. (let ((l (map (lambda (x) (sexp->type x r?)) (cdr x))))
  594. (reduce meet-type (car l) (cdr l)))))
  595. ((join)
  596. (let ((l (map (lambda (x) (sexp->type x r?)) (cdr x))))
  597. (reduce join-type (car l) (cdr l))))
  598. ((mask->type)
  599. (mask->type (cadr x)))
  600. ((variable)
  601. (variable-type (sexp->type (cadr x) r?)))
  602. (else (assertion-violation 'sexp->type "unrecognized type" x))))
  603. (else (assertion-violation 'sexp->type "unrecognized type" x))))
  604. (define (sexp->values-type l req? r?)
  605. (cond ((null? l)
  606. empty-rail-type)
  607. ((eq? (car l) '&rest)
  608. (make-rest-type (sexp->type (cadr l) r?)))
  609. ((eq? (car l) '&opt)
  610. (sexp->values-type (cdr l) #f r?))
  611. ((eq? (car l) 'rail-type)
  612. (sexp->values-type (cdr l) req? r?))
  613. (else
  614. (let ((t (sexp->type (car l) r?)))
  615. (rail-type (if req? t (make-optional-type t))
  616. (sexp->values-type (cdr l)
  617. req?
  618. r?))))))
  619. ; Convert type to S-expression
  620. (define (type->sexp t r?)
  621. (if (variable-type? t)
  622. `(variable ,(type->sexp (variable-value-type t) r?))
  623. (if (> (bitwise-and (type-mask t) mask/&rest) 0)
  624. (if (same-type? t any-values-type)
  625. ':values
  626. `(some-values ,@(rail-type->sexp t r?)))
  627. (let ((j (disjoin-type t)))
  628. (cond ((null? j) ':error)
  629. ((null? (cdr j))
  630. (atomic-type->sexp (car j) r?))
  631. (else
  632. `(join ,@(map (lambda (t)
  633. (atomic-type->sexp t r?))
  634. j))))))))
  635. (define (atomic-type->sexp t r?)
  636. (let ((m (type-mask t)))
  637. (cond ((and (not (type-info t))
  638. (table-ref mask->name-table m)))
  639. ((= m mask/other)
  640. (or (type-info t) ':value)) ;not quite
  641. ((= m mask/procedure)
  642. (let ((r (restrictive? t)))
  643. `(proc ,(rail-type->sexp (procedure-type-domain t)
  644. (not r))
  645. ,(type->sexp (procedure-type-codomain t) r)
  646. ,@(if (eq? r r?)
  647. '()
  648. `(,r)))))
  649. ((type-info t)
  650. `(ill-formed ,(type-mask t) ,(type-info t)))
  651. ((subtype? t exact-type)
  652. `(meet :exact
  653. ,(type->sexp (mask->type (let ((m (type-mask t)))
  654. (bitwise-ior m (arithmetic-shift m 1))))
  655. #t)))
  656. ((subtype? t inexact-type)
  657. `(meet :inexact
  658. ,(type->sexp (mask->type (let ((m (type-mask t)))
  659. (bitwise-ior m (arithmetic-shift m -1))))
  660. #t)))
  661. ;; ((meet? t number-type) ...)
  662. (else
  663. `(mask->type ,(type-mask t))))))
  664. (define (rail-type->sexp t r?)
  665. (let recur ((t t) (prev-req? #t) (r? r?))
  666. (cond ((empty-rail-type? t) '())
  667. ((rest-type? t)
  668. `(&rest ,(type->sexp (head-type-really t) r?)))
  669. ((optional-type? t)
  670. (let ((tail (cons (type->sexp (head-type-really t) r?)
  671. (recur (tail-type t) #f r?))))
  672. (if prev-req?
  673. `(&opt ,@tail)
  674. tail)))
  675. (else
  676. (cons (type->sexp (head-type t) r?)
  677. (recur (tail-type t) #t r?))))))
  678. ; Decompose a type into components
  679. (define (disjoin-type t)
  680. (cond ((bottom-type? t) '())
  681. ((and (not (type-info t))
  682. (table-ref mask->name-table (type-mask t)))
  683. (list t))
  684. ((meet? t other-value-type)
  685. (cons (meet-type t other-value-type)
  686. (disjoin-rest t mask/other)))
  687. ((meet? t any-procedure-type)
  688. (cons (meet-type t any-procedure-type)
  689. (disjoin-rest t mask/procedure)))
  690. ((meet? t number-type)
  691. (cons (meet-type t number-type)
  692. (disjoin-rest t mask/number)))
  693. (else
  694. (do ((i 1 (arithmetic-shift i 1)))
  695. ((> (bitwise-and (type-mask t) i) 0)
  696. (cons (mask->type i)
  697. (disjoin-rest t i)))))))
  698. (define (disjoin-rest t mask)
  699. (disjoin-type (mask->type (bitwise-and (type-mask t)
  700. (bitwise-not mask)))))
  701. (define mask/number (type-mask number-type))
  702. ; --------------------
  703. ; obsolescent? see lambda and values reconstructors in recon.scm
  704. (define (make-some-values-type types)
  705. (if (null? types)
  706. empty-rail-type
  707. (rail-type (car types) (make-some-values-type (cdr types)))))
  708. (define-syntax proc
  709. (syntax-rules ()
  710. ((proc (?type ...) ?cod)
  711. (procedure-type (some-values ?type ...) ?cod #t))
  712. ((proc (?type ...) ?cod ?r)
  713. (procedure-type (some-values ?type ...) ?cod ?r))))
  714. (define-syntax some-values
  715. (syntax-rules (&opt &rest)
  716. ((some-values) empty-rail-type)
  717. ((some-values &opt) empty-rail-type)
  718. ((some-values ?t) ?t)
  719. ((some-values &rest ?t) (make-rest-type ?t))
  720. ((some-values &opt &rest ?t) (make-rest-type ?t))
  721. ((some-values &opt ?t1 . ?ts)
  722. (rail-type (make-optional-type ?t1)
  723. (some-values &opt . ?ts)))
  724. ((some-values ?t1 . ?ts)
  725. (rail-type ?t1 (some-values . ?ts)))))
  726. (define (procedure-type? t)
  727. (= (type-mask t) mask/procedure))
  728. (define (fixed-arity-procedure-type? t)
  729. (and (procedure-type? t)
  730. (let loop ((d (procedure-type-domain t)))
  731. (cond ((empty-rail-type? d) #t)
  732. ((optional-type? d) #f)
  733. (else (loop (tail-type d)))))))
  734. (define (procedure-type-arity t)
  735. (do ((d (procedure-type-domain t) (tail-type d))
  736. (i 0 (+ i 1)))
  737. ((empty-rail-type? d) i)
  738. (if (optional-type? d)
  739. (assertion-violation 'procedure-type-arity "this shouldn't happen" t d))))
  740. (define (procedure-type-argument-types t)
  741. (let recur ((d (procedure-type-domain t)))
  742. (cond ((empty-rail-type? d) '())
  743. ((optional-type? d)
  744. (assertion-violation 'procedure-type-argument-types "lossage" t))
  745. (else
  746. (cons (head-type d)
  747. (recur (tail-type d)))))))
  748. ;----------------
  749. ; Odd types - variable types and the undeclared type
  750. ;
  751. ; These were elsewhere (syntax.scm) and should be here. If I could understand
  752. ; the above code I could make these be `real' types.
  753. (define (variable-type type)
  754. (list 'variable type))
  755. (define (variable-type? type)
  756. (and (pair? type) (eq? (car type) 'variable)))
  757. (define variable-value-type cadr)
  758. ; Usual type for Scheme variables.
  759. (define usual-variable-type (variable-type value-type))
  760. ; cf. EXPORT macro
  761. (define undeclared-type ':undeclared)
  762. ;----------------
  763. ; Used in two places:
  764. ; 1. GET-LOCATION checks to see if the context of use (either variable
  765. ; reference or assignment) is compatible with the declared type.
  766. ; 2. CHECK-STRUCTURE checks to see if the reconstructed type is compatible
  767. ; with any type declared in the interface.
  768. (define (compatible-types? have-type want-type)
  769. (if (variable-type? want-type)
  770. (and (variable-type? have-type)
  771. (same-type? (variable-value-type have-type)
  772. (variable-value-type want-type)))
  773. (meet? (if (variable-type? have-type)
  774. (variable-value-type have-type)
  775. have-type)
  776. want-type)))