test-map.rkt (3627B)
1 #lang aful/unhygienic typed/racket 2 3 (require typed-map 4 typed/rackunit) 5 6 ;; without ann 7 (let () 8 (map (λ (x) (* x 2)) '()) 9 (map (λ (x) (* x 2)) '(1)) 10 (map (λ (x) (* x 2)) '(1 2)) 11 (map (λ (x) (* x 2)) '(1 2 3)) 12 (map + '(1 2 3) '(4 5 6)) 13 (map car '((1 2) (3 4))) 14 (map #λ(+ % 1) '(1 2 3)) 15 16 ;; Test internal definitions inside the body 17 (map (λ (x) (define y x) (+ y 1)) '(1 2 3)) 18 19 ;; used as a function (identifier macro), looses the inference abilities 20 (map map (list add1 sub1) '((1 2 3) (4 5 6))) 21 (map map 22 (ann (list car cdr) (Listof (→ (List Number) (U Number Null)))) 23 '(((1) (2) (3)) ((4) (5) (6)))) 24 25 (λ #:∀ (A) ([l : (Listof A)]) 26 (map (λ (x) x) l)) 27 28 (void)) 29 30 ;; with ann 31 (let () 32 (ann (map (λ (x) (* x 2)) '()) Null) 33 (ann (map (λ (x) (* x 2)) '(1)) (Listof Positive-Byte)) 34 (ann (map (λ (x) (* x 2)) '(1 2)) (Listof Positive-Index)) 35 (ann (map (λ (x) (* x 2)) '(1 2 3)) (Listof Positive-Index)) 36 (ann (map + '(1 2 3) '(4 5 6)) (Listof Positive-Index)) 37 (ann (map car '((1 2) (3 4))) (Listof Positive-Byte)) 38 (ann (map #λ(+ % 1) '(1 2 3)) (Listof Positive-Index)) 39 40 (ann (map (λ (x) (define y x) (+ y 1)) '(1 2 3)) (Listof Positive-Index)) 41 42 (ann (λ #:∀ (A) ([l : (Listof A)]) 43 (map (λ (x) x) l)) 44 (∀ (A) (→ (Listof A) (Listof A)))) 45 46 (void)) 47 48 ;; with check-equal? 49 (check-equal? (map (λ (x) (* x 2)) '()) '()) 50 (check-equal? (map (λ (x) (* x 2)) '(1)) '(2)) 51 (check-equal? (map (λ (x) (* x 2)) '(1 2)) '(2 4)) 52 (check-equal? (map (λ (x) (* x 2)) '(1 2 3)) '(2 4 6)) 53 (check-equal? (map + '(1 2 3) '(4 5 6)) '(5 7 9)) 54 (check-equal? (map car '((1 2) (3 4))) '(1 3)) 55 (check-equal? (map #λ(+ % 1) '(1 2 3)) '(2 3 4)) 56 57 (check-equal? (map (λ (x) (define y x) (+ y 1)) '(1 2 3)) 58 '(2 3 4)) 59 60 (check-equal? (map map (list add1 sub1) '((1 2 3) (4 5 6))) 61 '((2 3 4) (3 4 5))) 62 (check-equal? (map map 63 (ann (list car cdr) 64 (Listof (→ (List Number) (U Number Null)))) 65 '(((1) (2) (3)) ((4) (5) (6)))) 66 '((1 2 3) (() () ()))) 67 68 (check-equal? ((λ #:∀ (A) ([l : (Listof A)]) 69 (map (λ (x) x) l)) 70 '(a b c)) 71 '(a b c)) 72 73 ;; foldr: 74 75 (check-equal? (foldr (λ (x acc) (cons (add1 x) acc)) '() '(1 2 3)) 76 (map add1 '(1 2 3))) 77 (check-equal? (foldr #λ(cons (add1 %1) %2) '() '(1 2 3)) 78 (map add1 '(1 2 3))) 79 (check-equal? (foldr (λ (x acc) (cons (add1 x) acc)) '() '(1 2 3)) 80 '(2 3 4)) 81 82 ;; Test internal definitions inside the body 83 (check-equal? (foldr (λ (x acc) (define y x) (cons (add1 y) acc)) '() '(1 2 3)) 84 '(2 3 4)) 85 86 (let () 87 (ann (foldr (λ (x acc) (cons (add1 x) acc)) '() '()) Null) 88 (void)) 89 90 ;; foldl: 91 92 (check-equal? (foldl (λ (x acc) (cons (add1 x) acc)) '() '(1 2 3)) 93 '(4 3 2)) 94 (check-equal? (foldl #λ(cons (add1 %1) %2) '() '(1 2 3)) 95 '(4 3 2)) 96 97 ;; Test internal definitions inside the body 98 (check-equal? (foldl (λ (x acc) (define y x) (cons (add1 y) acc)) '() '(1 2 3)) 99 '(4 3 2)) 100 101 ;; Does not work because the inferred type changes between the first and the 102 ;; second iteration 103 #;(check-equal? (foldl (λ (x acc) (cons acc (add1 x))) '() '(1 2 3)) 104 '(((() . 4) . 3) . 2)) 105 (check-equal? (foldl (λ (x [acc : (Rec R (U Null (Pairof R Positive-Index)))]) 106 (cons acc (add1 x))) 107 '() 108 '(1 2 3)) 109 '(((() . 2) . 3) . 4)) 110 111 (let () 112 (ann (foldl (λ (x acc) (cons (add1 x) acc)) '() '()) Null) 113 (void))