blob: 87034ab0dd9ce3c41ffa56c27147f27537bcd7c8 (
plain) (
blame)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
#lang racket
(require (except-in rackunit check))
(require "../stlc-dll.rkt")
(define-test-suite let-set-inc-case
(check-exn
exn:fail?
(λ () (infer '
(let (id : (Nat → 1 Nat)) (λ x x)
(let (r : (Ref (Nat → 1 Nat))) (new id)
(let (f : (Nat → 3 Nat)) (λ x ((! r) x))
(set r f
(f 0))))))))
(check-eq?
(infer '
(let (id : (Nat → 1 Nat)) (λ x x)
(let (r : (Ref (Nat → 1 Nat))) (new id)
(let (f : (Nat → 3 Nat)) (λ x ((! r) x))
(f 0)))))
'Nat)
(check-eq?
(check '
(let (id : (Nat → 1 Nat)) (λ x x)
(let (r : (Ref (Nat → 1 Nat))) (new id)
(let (f : (Nat → 3 Nat)) (λ x ((! r) x))
(f 0))))
'Nat)
#t)
(check-eq? (interpret '(if #t 1 0)) 1)
(check-eq? (interpret '(type Natural Nat ((λ (x : Natural) (inc x)) 1))) 2)
(check-eq? (infer '(type Natural Nat ((λ (x : Natural) (inc x)) 1))) 'Nat)
(check-true (check '(type Natural Nat ((λ (x : Natural) (inc x)) 1)) 'Nat))
(check-equal?
(infer
'(case ((inr sole) : (Nat ⊕ Unit))
(x ⇒ 0) (x ⇒ 1))) 'Nat)
(check-true
(check
'(case ((inr sole) : (Nat ⊕ Unit))
(x ⇒ x)
(x ⇒ 1)) 'Nat))
(check-equal?
(interpret
'((λ p1 (car (unfold p1)))
(fold
(pair 413
(pair (inl sole)
(inl sole))))))
413))
;; initial implementation of doubly linked lists:
;; (type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit)))))
(define-test-suite dll-no-empty-lists
(check-equal?
(interpret '(type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))
(let get
(λ x (car (unfold x)))
(let my_list
(fold
(pair 413
(pair (inl sole)
(inl sole))))
(get my_list)))))
413)
(check-equal?
(interpret '(type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))
(let prev
(λ x
(case (car (cdr (unfold x)))
(x ⇒ (inl (! x)))
(x ⇒ (inr sole))))
(let my_list
(fold
(pair 413
(pair (inl (new sole))
(inl (new sole)))))
(prev my_list)))))
'(inl sole))
(check-equal?
(interpret '(type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))
(let next
(λ x
(case (cdr (cdr (unfold x)))
(x ⇒ (inl (! x)))
(x ⇒ (inr sole))))
(let my_list
(fold
(pair 413
(pair (inr (new sole))
(inr (new sole)))))
(next my_list)))))
'(inr sole))
(check-true
(equiv-type
(infer '(type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))
(λ (self : DoublyLinkedList)
(case (cdr (cdr (unfold self)))
(x ⇒ ((inl (! x)) : (DoublyLinkedList ⊕ Unit)))
(x ⇒ ((inr sole) : (DoublyLinkedList ⊕ Unit)))))))
'(DoublyLinkedList → 1 (DoublyLinkedList ⊕ Unit))
#hash((DoublyLinkedList . (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))))))
(check-true
(check
'(type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))
(λ (self : DoublyLinkedList)
(case (cdr (cdr (unfold self)))
(x ⇒ ((inl (! x)) : (DoublyLinkedList ⊕ Unit)))
(x ⇒ ((inr sole) : (DoublyLinkedList ⊕ Unit))))))
'(DoublyLinkedList → 1 (DoublyLinkedList ⊕ Unit))))
(check-equal?
(infer '(type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))
(let (get : (DoublyLinkedList → 1 Nat))
(λ self (car (unfold self)))
(let (prev : (DoublyLinkedList → 1 (DoublyLinkedList ⊕ Unit)))
(λ self
(case (car (cdr (unfold self)))
(x ⇒ (inl (! x)))
(x ⇒ (inr sole))))
(let (next : (DoublyLinkedList → 1 (DoublyLinkedList ⊕ Unit)))
(λ self
(case (cdr (cdr (unfold self)))
(x ⇒ (inl (! x)))
(x ⇒ (inr sole))))
(let (my_list : DoublyLinkedList)
(fold
(pair 413
(pair ((inr sole) : ((Ref DoublyLinkedList) ⊕ Unit))
((inr sole) : ((Ref DoublyLinkedList) ⊕ Unit)))))
(prev my_list)))))))
'(DoublyLinkedList ⊕ Unit))
(check-true
(check '(type DoublyLinkedList (μ Self (Nat × (((Ref Self) ⊕ Unit) × ((Ref Self) ⊕ Unit))))
(let (get : (DoublyLinkedList → 1 Nat))
(λ self (car (unfold self)))
(let (prev : (DoublyLinkedList → 1 (DoublyLinkedList ⊕ Unit)))
(λ self
(case (car (cdr (unfold self)))
(x ⇒ (inl (! x)))
(x ⇒ (inr sole))))
(let (next : (DoublyLinkedList → 1 (DoublyLinkedList ⊕ Unit)))
(λ self
(case (cdr (cdr (unfold self)))
(x ⇒ (inl (! x)))
(x ⇒ (inr sole))))
(let (my_list : DoublyLinkedList)
(fold
(pair 413
(pair ((inr sole) : ((Ref DoublyLinkedList) ⊕ Unit))
((inr sole) : ((Ref DoublyLinkedList) ⊕ Unit)))))
(prev my_list))))))
'(DoublyLinkedList ⊕ Unit))))
;; new implementation of doubly linked lists:
;; (type DoublyLinkedList (μ Self ((Nat × ((Ref Self) × (Ref Self))) ⊕ Unit)))
(define-test-suite dll-with-empty-lists
(check-equal?
(interpret
'(let next
(λ self
(case (unfold self)
(some ⇒ (! (cdr (cdr some))))
(none ⇒ (fold (inr sole)))))
(let my_list
(fold
(inl
(pair 413
(pair (new (inr sole))
(new (inr sole))))))
(next my_list))))
'(inr sole))
(check-equal?
(infer '(type DoublyLinkedList (μ Self ((Nat × ((Ref Self) × (Ref Self))) ⊕ Unit))
(λ (self : DoublyLinkedList)
(case (unfold self)
(some ⇒ ((! (cdr (cdr some))) : DoublyLinkedList))
(none ⇒ ((fold (inr sole)) : DoublyLinkedList))))))
'((μ Self ((Nat × ((Ref Self) × (Ref Self))) ⊕ Unit)) → 1 (μ Self ((Nat × ((Ref Self) × (Ref Self))) ⊕ Unit))))
(check-true
(equiv-type
(infer '(type DoublyLinkedList (μ Self ((Nat × ((Ref Self) × (Ref Self))) ⊕ Unit))
(λ (self : DoublyLinkedList)
(case (unfold self)
(some ⇒ (! (cdr (cdr some))))
(none ⇒ ((fold (inr sole)) : DoublyLinkedList))))))
'(DoublyLinkedList → 1 DoublyLinkedList)
#hash((DoublyLinkedList . (μ Self ((Nat × ((Ref Self) × (Ref Self))) ⊕ Unit))))))
(check-true
(check '(type DoublyLinkedList (μ Self ((Nat × ((Ref Self) × (Ref Self))) ⊕ Unit))
(let (get : (DoublyLinkedList → 1 (Nat ⊕ Unit)))
(λ self
(case (unfold self)
(some ⇒ (inl (car some)))
(none ⇒ (inr sole))))
(let (prev : (DoublyLinkedList → 1 DoublyLinkedList))
(λ self
(case (unfold self)
(some ⇒ (! (car (cdr some))))
(none ⇒ ((fold (inr sole)) : DoublyLinkedList))))
(let (next : (DoublyLinkedList → 1 DoublyLinkedList))
(λ self
(case (unfold self)
(some ⇒ (! (cdr (cdr some))))
(none ⇒ ((fold (inr sole)) : DoublyLinkedList))))
(let (my_list : DoublyLinkedList)
(fold (inl
(pair 413
(pair (new ((fold (inr sole)) : DoublyLinkedList))
(new ((fold (inr sole)) : DoublyLinkedList))))))
(prev my_list))))))
'DoublyLinkedList)))
|