-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter7-2-drop-last-k-vec.rkt
96 lines (83 loc) · 1.96 KB
/
chapter7-2-drop-last-k-vec.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
86
87
88
89
90
91
92
93
94
95
96
#lang pie
;; Define a function called drop-last-k that takes an argument of type (Vec E ?) and
;; evaluates to a value of type (Vec E ?), the result of dropping the last k elements
;; from the first argument.
;;
;; NB: The type of the function should guarantee that we can't drop more elements
;; than there are in the first argument.
(claim + (-> Nat Nat Nat))
(define +
(lambda (i j)
(rec-Nat i
j
(lambda (i-1 sum-i-1)
(add1 sum-i-1)
))))
{claim mot-drop-last-k
(Pi [(E U) (k Nat) (n Nat)] U)
}
{define mot-drop-last-k
{lambda [E k n]
(-> (Vec E (+ n k)) (Vec E n))
}
}
{claim base-drop-last-k
(Pi [(E U) (k Nat)]
(mot-drop-last-k E k 0)
)
}
{define base-drop-last-k
{lambda [E k]
{lambda [es]
vecnil
}
}
}
{claim step-drop-last-k
(Pi [(E U) (k Nat) (n-1 Nat)]
(-> (mot-drop-last-k E k n-1)
(mot-drop-last-k E k (add1 n-1))
)
)
}
{define step-drop-last-k
{lambda [E k n-1]
{lambda [drop-last-k-1]
{lambda [es]
(vec::
(head es)
(drop-last-k-1 (tail es))
)
}
}
}
}
{claim drop-last-k
(Pi [(E U) (k Nat) (n Nat)]
(mot-drop-last-k E k n)
)
}
{define drop-last-k
{lambda [E k n]
(ind-Nat n
(mot-drop-last-k E k)
(base-drop-last-k E k)
(step-drop-last-k E k)
)
}
}
(check-same (Vec Atom 3)
(vec:: 'a (vec:: 'b (vec:: 'c vecnil)))
(drop-last-k Atom 2 3
(vec:: 'a (vec:: 'b (vec:: 'c (vec:: 'd (vec:: 'e vecnil))))))
)
(check-same (Vec Atom 0)
vecnil
(drop-last-k Atom 5 0
(vec:: 'a (vec:: 'b (vec:: 'c (vec:: 'd (vec:: 'e vecnil))))))
)
(check-same (Vec Atom 5)
(vec:: 'a (vec:: 'b (vec:: 'c (vec:: 'd (vec:: 'e vecnil)))))
(drop-last-k Atom 0 5
(vec:: 'a (vec:: 'b (vec:: 'c (vec:: 'd (vec:: 'e vecnil))))))
)