-
Notifications
You must be signed in to change notification settings - Fork 11
/
chapter-14-fin.rkt
85 lines (66 loc) · 1.47 KB
/
chapter-14-fin.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#lang pie
;; Exercises on Fin from Chapter 14 of The Little Typer
(claim Maybe
(-> U
U))
(define Maybe
(λ (X)
(Either X Trivial)))
(claim nothing
(Π ([E U])
(Maybe E)))
(define nothing
(λ (_)
(right sole)))
(claim just
(Π ([E U])
(-> E
(Maybe E))))
(define just
(λ (_ e)
(left e)))
(claim Fin
(-> Nat
U))
(define Fin
(λ (n)
(iter-Nat n
Absurd
Maybe)))
(claim fzero
(Π ([n Nat])
(Fin (add1 n))))
(define fzero
(λ (n)
(nothing (Fin n))))
(claim fadd1
(Π ([n Nat])
(-> (Fin n)
(Fin (add1 n)))))
(define fadd1
(λ (n)
(λ (i-1)
(just (Fin n) i-1))))
;; Exercise 14.1
;;
;; Define a function called insert-at that inserts a Nat into
;; a Vector.
;;
;; The signature of the function should only allow calls that
;; insert the Nat at an existing position in the Vector.
(claim insert-at
(Π ([l Nat])
(-> (Fin (add1 l)) (Vec Nat l) Nat
(Vec Nat (add1 l)))))
;; Define Matrix be the type of Nat valued matricies.
(claim Matrix
(-> Nat Nat
U))
(define Matrix
(λ (rows columns)
(Vec (Vec Nat columns) rows)))
;; Exercise 14.2
;;
;; Define a function called matrix-ref that can be used to find the
;; entries of a Matrix. Use Fin types for the arguments of this function
;; that bounds check the call to the dimenstions of the matrix.