-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuples.func
More file actions
272 lines (241 loc) · 7.78 KB
/
tuples.func
File metadata and controls
272 lines (241 loc) · 7.78 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{-
This file is part of stdlib.func.
It is derived from code originally part of the TON Blockchain project.
stdlib.func is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
stdlib.func is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with stdlib.func (see the file LICENSE.LGPL). If not, see
<https://www.gnu.org/licenses/>. You should also have received a copy of
the GNU General Public License (see the file LICENSE.GPL), which is
referenced by the LGPL.
-}
{-
# Lisp-style lists
Lists can be represented as nested 2-elements tuples.
Empty list is conventionally represented as TVM `null` value (it can be obtained by calling [null()]).
For example, tuple `(1, (2, (3, null)))` represents list `[1, 2, 3]`. Elements of a list can be of different types.
-}
;;; Adds an element to the beginning of a lisp-style list.
;;;
;;; Args:
;;; head (X): Element to add to the beginning of the list.
;;; tail (tuple): Existing list to which the element will be prepended.
;;;
;;; Returns:
;;; tuple: New list with head as the first element followed by all elements from tail.
forall X -> tuple cons(X head, tuple tail) asm "CONS";
;;; Extracts the head and the tail of lisp-style list.
;;;
;;; Args:
;;; list (tuple): Lisp-style list to decompose.
;;;
;;; Returns:
;;; X: First element (head) of the list.
;;; tuple: Remaining elements (tail) of the list.
forall X -> (X, tuple) uncons(tuple list) asm "UNCONS";
;;; Extracts the tail and the head of lisp-style list.
;;;
;;; Args:
;;; list (tuple): Lisp-style list to decompose.
;;;
;;; Returns:
;;; (tuple, X):
;;; tuple: Remaining elements (tail) of the list.
;;; X: First element (head) of the list.
forall X -> (tuple, X) list_next(tuple list) asm( -> 1 0) "UNCONS";
;;; Returns the head of lisp-style list.
;;;
;;; Args:
;;; list (tuple): Lisp-style list to get head from.
;;;
;;; Returns:
;;; X: First element (head) of the list.
forall X -> X car(tuple list) asm "CAR";
;;; Returns the tail of lisp-style list.
;;;
;;; Args:
;;; list (tuple): Lisp-style list to get tail from.
;;;
;;; Returns:
;;; tuple: Remaining elements (tail) of the list without the first element.
tuple cdr(tuple list) asm "CDR";
;;; Creates tuple with zero elements.
;;;
;;; Args:
;;; None.
;;;
;;; Returns:
;;; tuple: Empty tuple with no elements.
tuple empty_tuple() asm "NIL";
;;; Returns the current length of the tuple.
;;;
;;; Args:
;;; t (tuple): Tuple whose length is to be determined.
;;;
;;; Returns:
;;; int: Number of elements in the tuple.
int tuple_length(tuple t) asm "TLEN";
;;; Appends a value to the end of a tuple.
;;;
;;; Args:
;;; t (tuple): Original tuple to which the value will be appended.
;;; value (X): Value to append to the tuple.
;;;
;;; Returns:
;;; tuple: New tuple with the value appended, but only if resulting tuple length is at most 255.
forall X -> tuple tpush(tuple t, X value) asm "TPUSH";
forall X -> (tuple, ()) ~tpush(tuple t, X value) asm "TPUSH";
;;; Creates a tuple of length one with given argument as element.
;;;
;;; Args:
;;; x (X): Element to place in the singleton tuple.
;;;
;;; Returns:
;;; [X]: Tuple containing only the given element.
forall X -> [X] single(X x) asm "SINGLE";
;;; Unpacks a tuple of length one.
;;;
;;; Args:
;;; t ([X]): Singleton tuple to unpack.
;;;
;;; Returns:
;;; X: The element contained in the tuple.
forall X -> X unsingle([X] t) asm "UNSINGLE";
;;; Creates a tuple of length two with given arguments as elements.
;;;
;;; Args:
;;; x (X): First element of the pair.
;;; y (Y): Second element of the pair.
;;;
;;; Returns:
;;; [X, Y]: Tuple containing the two given elements.
forall X, Y -> [X, Y] pair(X x, Y y) asm "PAIR";
;;; Unpacks a tuple of length two.
;;;
;;; Args:
;;; t ([X, Y]): Pair tuple to unpack.
;;;
;;; Returns:
;;; X: First element of the tuple.
;;; Y: Second element of the tuple.
forall X, Y -> (X, Y) unpair([X, Y] t) asm "UNPAIR";
;;; Creates a tuple of length three with given arguments as elements.
;;;
;;; Args:
;;; x (X): First element of the triple.
;;; y (Y): Second element of the triple.
;;; z (Z): Third element of the triple.
;;;
;;; Returns:
;;; [X, Y, Z]: Tuple containing the three given elements.
forall X, Y, Z -> [X, Y, Z] triple(X x, Y y, Z z) asm "TRIPLE";
;;; Unpacks a tuple of length three.
;;;
;;; Args:
;;; t ([X, Y, Z]): Triple tuple to unpack.
;;;
;;; Returns:
;;; X: First element of the tuple.
;;; Y: Second element of the tuple.
;;; Z: Third element of the tuple.
forall X, Y, Z -> (X, Y, Z) untriple([X, Y, Z] t) asm "UNTRIPLE";
;;; Creates a tuple of length four with given arguments as elements.
;;;
;;; Args:
;;; x (X): First element of the tuple.
;;; y (Y): Second element of the tuple.
;;; z (Z): Third element of the tuple.
;;; w (W): Fourth element of the tuple.
;;;
;;; Returns:
;;; [X, Y, Z, W]: Tuple containing the four given elements.
forall X, Y, Z, W -> [X, Y, Z, W] tuple4(X x, Y y, Z z, W w) asm "4 TUPLE";
;;; Unpacks a tuple of length four.
;;;
;;; Args:
;;; t ([X, Y, Z, W]): Four-element tuple to unpack.
;;;
;;; Returns:
;;; X: First element of the tuple.
;;; Y: Second element of the tuple.
;;; Z: Third element of the tuple.
;;; W: Fourth element of the tuple.
forall X, Y, Z, W -> (X, Y, Z, W) untuple4([X, Y, Z, W] t) asm "4 UNTUPLE";
;;; Returns the first element of a tuple.
;;;
;;; Args:
;;; t (tuple): Tuple from which to extract the first element.
;;;
;;; Returns:
;;; X: First element of the tuple.
forall X -> X first(tuple t) asm "FIRST";
;;; Returns the second element of a tuple.
;;;
;;; Args:
;;; t (tuple): Tuple from which to extract the second element.
;;;
;;; Returns:
;;; X: Second element of the tuple.
forall X -> X second(tuple t) asm "SECOND";
;;; Returns the third element of a tuple.
;;;
;;; Args:
;;; t (tuple): Tuple from which to extract the third element.
;;;
;;; Returns:
;;; X: Third element of the tuple.
forall X -> X third(tuple t) asm "THIRD";
;;; Returns the fourth element of a tuple.
;;;
;;; Args:
;;; t (tuple): Tuple from which to extract the fourth element.
;;;
;;; Returns:
;;; X: Fourth element of the tuple.
forall X -> X fourth(tuple t) asm "3 INDEX";
;;; Returns the first element of a pair tuple.
;;;
;;; Args:
;;; p ([X, Y]): Pair tuple from which to extract the first element.
;;;
;;; Returns:
;;; X: First element of the pair.
forall X, Y -> X pair_first([X, Y] p) asm "FIRST";
;;; Returns the second element of a pair tuple.
;;;
;;; Args:
;;; p ([X, Y]): Pair tuple from which to extract the second element.
;;;
;;; Returns:
;;; Y: Second element of the pair.
forall X, Y -> Y pair_second([X, Y] p) asm "SECOND";
;;; Returns the first element of a triple tuple.
;;;
;;; Args:
;;; p ([X, Y, Z]): Triple tuple from which to extract the first element.
;;;
;;; Returns:
;;; X: First element of the triple.
forall X, Y, Z -> X triple_first([X, Y, Z] p) asm "FIRST";
;;; Returns the second element of a triple tuple.
;;;
;;; Args:
;;; p ([X, Y, Z]): Triple tuple from which to extract the second element.
;;;
;;; Returns:
;;; Y: Second element of the triple.
forall X, Y, Z -> Y triple_second([X, Y, Z] p) asm "SECOND";
;;; Returns the third element of a triple tuple.
;;;
;;; Args:
;;; p ([X, Y, Z]): Triple tuple from which to extract the third element.
;;;
;;; Returns:
;;; Z: Third element of the triple.
forall X, Y, Z -> Z triple_third([X, Y, Z] p) asm "THIRD";