-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathchapter-11-ind-Vec-crib.rkt
58 lines (48 loc) · 1.17 KB
/
chapter-11-ind-Vec-crib.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#lang pie
;; Exercises on ind-Vec from Chapter 11 of The Little Typer
;; Exercise 11.1
;;
;; Use ind-Vec to define a function called unzip that takes unzips
;; a vector of pairs into a pair of vectors.
(claim unzip
(Π ([A U]
[B U]
[n Nat])
(-> (Vec (Pair A B) n)
(Pair (Vec A n) (Vec B n)))))
(claim mot-unzip
(Π ([A U]
[B U]
[n Nat])
(-> (Vec (Pair A B) n)
U)))
(define mot-unzip
(λ (A B n)
(λ (_)
(Pair (Vec A n) (Vec B n)))))
(claim base-unzip
(Π ([A U]
[B U])
(Pair (Vec A zero) (Vec B zero))))
(define base-unzip
(λ (A B)
(cons vecnil vecnil)))
(claim step-unzip
(Π ([A U]
[B U]
[k Nat]
[h (Pair A B)]
[t (Vec (Pair A B) k)])
(-> (mot-unzip A B k t)
(mot-unzip A B (add1 k) (vec:: h t)))))
(define step-unzip
(λ (A B k h t)
(λ (p)
(cons (vec:: (car h) (car p))
(vec:: (cdr h) (cdr p))))))
(define unzip
(λ (A B n v)
(ind-Vec n v
(mot-unzip A B)
(base-unzip A B)
(step-unzip A B))))