|
46 | 46 | PolyDots-unsafe: |
47 | 47 | Mu? Poly? PolyDots? PolyRow? |
48 | 48 | Poly-n |
| 49 | + F-n |
| 50 | + F-bound |
| 51 | + F? |
49 | 52 | PolyDots-n |
50 | 53 | Class? Row? Row: |
51 | 54 | free-vars* |
|
84 | 87 | [PolyDots:* PolyDots:] |
85 | 88 | [PolyRow:* PolyRow:] |
86 | 89 | [Mu* make-Mu] |
| 90 | + [F* make-F] |
| 91 | + [F:* F:] |
87 | 92 | [make-Mu unsafe-make-Mu] |
88 | 93 | [Poly* make-Poly] |
89 | 94 | [PolyDots* make-PolyDots] |
|
139 | 144 |
|
140 | 145 | ;; free type variables |
141 | 146 | ;; n is a Name |
142 | | -(def-type F ([n symbol?]) |
| 147 | +(def-type F ([n symbol?] |
| 148 | + [bound (or/c #f Type?)]) |
| 149 | + #:no-provide |
143 | 150 | [#:frees |
144 | 151 | [#:vars (_) (single-free-var n)] |
145 | 152 | [#:idxs (_) empty-free-vars]] |
146 | 153 | [#:fmap (_ #:self self) self] |
147 | 154 | [#:for-each (_) (void)]) |
148 | 155 |
|
| 156 | +(define (F* n [bound #f]) |
| 157 | + (make-F n bound)) |
| 158 | + |
| 159 | + |
| 160 | +(define-match-expander F:* |
| 161 | + (lambda (stx) |
| 162 | + (syntax-case stx () |
| 163 | + [(_ n) |
| 164 | + #'(? F? (app (lambda (t) |
| 165 | + (F-n t)) |
| 166 | + n))] |
| 167 | + [(_ n b) |
| 168 | + #'(? F? (app (lambda (t) |
| 169 | + (list (F-n t) (F-bound t))) |
| 170 | + (list n b)))]))) |
| 171 | + |
| 172 | + |
149 | 173 | (define Name-table (make-free-id-table)) |
150 | 174 |
|
151 | 175 | ;; Name, an indirection of a type through the environment |
|
519 | 543 | ;; n is how many variables are bound here |
520 | 544 | ;; body is a type |
521 | 545 | (def-type Poly ([n exact-nonnegative-integer?] |
| 546 | + [bounds (hash/c exact-nonnegative-integer? |
| 547 | + Type? |
| 548 | + #:immutable #t |
| 549 | + #:flat #t)] |
522 | 550 | [body Type?]) |
523 | 551 | #:no-provide |
524 | 552 | [#:frees (f) (f body)] |
525 | | - [#:fmap (f) (make-Poly n (f body))] |
| 553 | + [#:fmap (f) (make-Poly n bounds (f body))] |
526 | 554 | [#:for-each (f) (f body)] |
527 | 555 | [#:mask (λ (t) (mask (Poly-body t)))]) |
528 | 556 |
|
|
1456 | 1484 | ;; De Bruijn indices |
1457 | 1485 | [(B: idx) (transform idx lvl cur #f)] |
1458 | 1486 | ;; Type variables |
1459 | | - [(F: var) (transform var lvl cur #f)] |
| 1487 | + [(F: var _) (transform var lvl cur #f)] |
1460 | 1488 | ;; forms w/ dotted type vars/indices |
1461 | 1489 | [(RestDots: ty d) |
1462 | 1490 | (make-RestDots (rec ty) (transform d lvl d #t))] |
|
1477 | 1505 | (make-PolyRow constraints (rec/lvl body (add1 lvl)))] |
1478 | 1506 | [(PolyDots: n body) |
1479 | 1507 | (make-PolyDots n (rec/lvl body (+ n lvl)))] |
1480 | | - [(Poly: n body) |
| 1508 | + [(Poly: n bound body) |
1481 | 1509 | (make-Poly n (rec/lvl body (+ n lvl)))] |
1482 | 1510 | [_ (Rep-fmap cur rec)]))) |
1483 | 1511 |
|
|
1618 | 1646 | (define (Mu-body* name t) |
1619 | 1647 | (match t |
1620 | 1648 | [(Mu: body) |
1621 | | - (instantiate-type body (make-F name))])) |
| 1649 | + (instantiate-type body (F* name))])) |
1622 | 1650 |
|
1623 | 1651 | ;; unfold : Mu -> Type |
1624 | 1652 | (define/cond-contract (unfold t) |
|
1638 | 1666 | ;; |
1639 | 1667 | ;; list<symbol> type #:original-names list<symbol> -> type |
1640 | 1668 | ;; |
1641 | | -(define (Poly* names body #:original-names [orig names]) |
| 1669 | +(define (Poly* names body #:bounds [bounds '#hash()] #:original-names [orig names]) |
1642 | 1670 | (if (null? names) body |
1643 | | - (let ([v (make-Poly (length names) (abstract-type body names))]) |
| 1671 | + (let* ([len (length names)] |
| 1672 | + [new-bounds (let ([max-idx (sub1 len)]) |
| 1673 | + (for/hash ([(n v) bounds]) |
| 1674 | + (values (- max-idx (index-of names n)) v)))] |
| 1675 | + [v (make-Poly len new-bounds (abstract-type body names))]) |
1644 | 1676 | (hash-set! type-var-name-table v orig) |
1645 | 1677 | v))) |
1646 | 1678 |
|
1647 | 1679 | ;; Poly 'smart' destructor |
1648 | 1680 | (define (Poly-body* names t) |
1649 | 1681 | (match t |
1650 | | - [(Poly: n body) |
| 1682 | + [(Poly: n bounds body) |
| 1683 | + (define new-bounds (for/hash ([(idx v) bounds]) |
| 1684 | + (values (list-ref names idx) v))) |
1651 | 1685 | (unless (= (length names) n) |
1652 | 1686 | (int-err "Wrong number of names: expected ~a got ~a" n (length names))) |
1653 | | - (instantiate-type body (map make-F names))])) |
| 1687 | + (eprintf "new bounds is ~a ~n" new-bounds) |
| 1688 | + (instantiate-type body |
| 1689 | + |
| 1690 | + (map (lambda (n) |
| 1691 | + (make-F n |
| 1692 | + (hash-ref new-bounds n #f) |
| 1693 | + )) names) |
| 1694 | + #; |
| 1695 | + (map F* names))])) |
1654 | 1696 |
|
1655 | 1697 | ;; PolyDots 'smart' constructor |
1656 | 1698 | (define (PolyDots* names body) |
|
1665 | 1707 | [(PolyDots: n body) |
1666 | 1708 | (unless (= (length names) n) |
1667 | 1709 | (int-err "Wrong number of names: expected ~a got ~a" n (length names))) |
1668 | | - (instantiate-type body (map make-F names))])) |
| 1710 | + (instantiate-type body (map F* names))])) |
1669 | 1711 |
|
1670 | 1712 |
|
1671 | 1713 | ;; PolyRow 'smart' constructor |
|
1683 | 1725 | (define (PolyRow-body* names t) |
1684 | 1726 | (match t |
1685 | 1727 | [(PolyRow: constraints body) |
1686 | | - (instantiate-type body (map make-F names))])) |
| 1728 | + (instantiate-type body (map names))])) |
1687 | 1729 |
|
1688 | 1730 |
|
1689 | 1731 | ;;*************************************************************** |
|
1746 | 1788 | (let* ([n (Poly-n t)] |
1747 | 1789 | [syms (build-list n (lambda _ (gensym)))]) |
1748 | 1790 | (list syms (Poly-body* syms t)))) |
1749 | | - (list nps bp)))]))) |
| 1791 | + (list nps bp)))] |
| 1792 | + [(_ nps bounds bp) |
| 1793 | + #'(? Poly? |
| 1794 | + (app (lambda (t) |
| 1795 | + (let* ([n (Poly-n t)] |
| 1796 | + [syms (build-list n (lambda _ (gensym)))] |
| 1797 | + [bounds (for/hash ([(idx v) (Poly-bounds t)]) |
| 1798 | + (values (list-ref syms idx) v))]) |
| 1799 | + (list syms bounds (Poly-body* syms t)))) |
| 1800 | + (list nps bounds bp)))]))) |
1750 | 1801 |
|
1751 | 1802 | ;; This match expander uses the names from the hashtable |
1752 | 1803 | (define-match-expander Poly-names: |
|
1939 | 1990 | [(Some: n body) |
1940 | 1991 | (unless (= (length names) n) |
1941 | 1992 | (int-err "Wrong number of names: expected ~a got ~a" n (length names))) |
1942 | | - (instantiate-type body (map make-F names))])) |
| 1993 | + (instantiate-type body (map names))])) |
1943 | 1994 |
|
1944 | 1995 |
|
1945 | 1996 | (define-match-expander Some-names: |
|
0 commit comments