forked from cpennington/h4sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.hs
424 lines (358 loc) · 14.4 KB
/
Command.hs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
--
-- Copyright (c) 2005 Don Stewart - http://www.cse.unsw.edu.au/~dons
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 2 of
-- the License, or (at your option) any later version.
--
-- This program 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
-- General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
--
-- The functions we know how to generate binaries for
module Command where
data Command =
Command Name -- haskell function to use
Name -- binary name (for, e.g. !!)
Type
Mode
[String]
data Mode = List | Packed
name :: Command -> Name
name (Command _ f _ _ _) = f
type Name = String
data Type = Pair Type Type
| To Type Type -- ->
| S -- String
| LS -- [String]
| LI -- [Int]
| I -- Int
| B -- Bool
| MaybeT Type
deriving (Show, Eq)
------------------------------------------------------------------------
commands :: [Command]
commands =
[ Command "tail" "tl"
(LS `To` LS)
Packed
["tail :: [a] -> [a]",
"Extract the elements after the head of a list, which must be non-empty."]
, Command "init" "init"
(LS `To` LS)
Packed
["init :: [a] -> [a]"
,"Return all the elements of a list except the last one."
,"The list must be finite and non-empty."]
, Command "reverse" "reverse"
(LS `To` LS)
Packed
["reverse :: [a] -> [a]"
,"reverse returns the elements of the list in reverse order."
,"The list must be finite."]
, Command "nub" "nub"
(LS `To` LS)
Packed
["nub :: (Eq a) => [a] -> [a]"
,"The 'nub' function removes duplicate elements from a list."
,"In particular, it keeps only the first occurrence of each element."]
, Command "sort" "srt"
(LS `To` LS)
Packed
["sort :: (Ord a) => [a] -> [a]"
,"The 'sort' function implements a stable sorting algorithm."]
, Command "id" "i"
(S `To` S)
Packed
["id :: a -> a"
,"Identity function."]
, Command "cycle" "cycle"
(LS `To` LS)
List
["cycle :: [a] -> [a]"
,"'cycle' ties a finite list into a circular one, or equivalently,"
,"the infinite repetition of the original list. It is the identity"
,"on infinite lists."]
, Command "transpose" "transpose"
(LS `To` LS)
List
["transpose :: [[a]] -> [[a]]"
,"The 'transpose' function transposes the rows and columns of its argument."
,"For example,"
,""
,"> transpose [[1,2,3],[4,5,6]] == [[1,4],[2,5],[3,6]]"]
------------------------------------------------------------------------
, Command "concat" "concat"
(LS `To` S)
List
["concat :: [[a]] -> [a]"
,"Concatenate a list of lists."]
, Command "unwords" "unwords"
(LS `To` S)
List
["unwords :: [String] -> String"
,"'unwords' is an inverse operation to 'words'."
,"It joins words with separating spaces."]
, Command "head" "hd"
(LS `To` S)
Packed
["head :: [a] -> a"
,"Extract the first element of a list, which must be non-empty."]
, Command "last" "last"
(LS `To` S)
Packed
["last :: [a] -> a"
,"Extract the last element of a list, which must be finite and non-empty."]
, Command "minimum" "min"
(LS `To` S)
Packed
["minimum :: (Ord a) => [a] -> a"
,"'minimum' returns the minimum value from a list,"
,"which must be non-empty, finite, and of an ordered type."]
, Command "maximum" "max"
(LS `To` S)
Packed
["maximum :: (Ord a) => [a] -> a"
,"'maximum' returns the maximum value from a list,"
,"which must be non-empty, finite, and of an ordered type."]
------------------------------------------------------------------------
, Command "length" "length" -- count '\n' is faster
(LS `To` I)
Packed
["length :: [a] -> Int"
,"length returns the length of a finite list as an Int."]
------------------------------------------------------------------------
, Command "(:[])" "show"
(S `To` LS)
Packed
["show :: (Show a) => a -> String"
,"Convert a value to a readable 'String'."]
, Command "repeat" "rpt"
(S `To` LS)
List
["repeat :: a -> [a]"
,"repeat x is an infinite list, with x the value of every element."]
, Command "words" "words"
(S `To` LS)
List
["words :: String -> [String]"
,"'words' breaks a string up into a list of words, which were delimited"
,"by white space."]
, Command "map (concat . intersperse \" \") . group" "group"
(LS `To` LS)
List
["group :: Eq a => [a] -> [[a]]"
,"The 'group' function takes a list and returns a list of lists such"
,"that the concatenation of the result is equal to the argument. Moreover,"
,"each sublist in the result contains only equal elements. For example,"
,""
,"> group \"Mississippi\" = [\"M\",\"i\",\"ss\",\"i\",\"ss\",\"i\",\"pp\",\"i\"]"
,""
,"It is a special case of 'groupBy', which allows the programmer to supply"
,"their own equality test."]
------------------------------------------------------------------------
, Command "take" "take"
(I `To` (LS `To` LS))
List
["take :: Int -> [a] -> [a]"
,"take n, applied to a list xs, returns the prefix of xs"
,"of length n, or xs itself if n > length xs."]
, Command "drop" "drop"
(I `To` (LS `To` LS))
List
["drop :: Int -> [a] -> [a]"
,"drop n xs returns the suffix of xs"
,"after the first n elements, or [] if n > length xs."]
------------------------------------------------------------------------
, Command "map" "map"
((S `To` S) `To` (LS `To` LS))
List -- list is still faster
["map :: (a -> b) -> [a] -> [b]"
,"map f xs is the list obtained by applying f to each element of xs, i.e.,"
,""
,"> map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn]"
,"> map f [x1, x2, ...] == [f x1, f x2, ...]"]
------------------------------------------------------------------------
, Command "filter" "filter"
((S `To` B) `To` (LS `To` LS))
List -- list version if faster
["filter :: (a -> Bool) -> [a] -> [a]"
,"filter, applied to a predicate and a list, returns the list of"
,"those elements that satisfy the predicate."]
, Command "takeWhile" "takew"
((S `To` B) `To` (LS `To` LS))
List
["takeWhile :: (a -> Bool) -> [a] -> [a]"
,"'takeWhile', applied to a predicate p and a list xs, returns the"
,"longest prefix (possibly empty) of xs of elements that satisfy p."]
, Command "dropWhile" "dropw"
((S `To` B) `To` (LS `To` LS))
List
["dropWhile :: (a -> Bool) -> [a] -> [a]"
,"'dropWhile' p xs returns the suffix remaining after 'takeWhile' p xs."]
------------------------------------------------------------------------
, Command "intersperse" "intersperse"
(S `To` (LS `To` LS))
Packed
["intersperse :: a -> [a] -> [a]"
,"The 'intersperse' function takes an element and a list and"
,"`intersperses\' that element between the elements of the list."
,"For example,"
,""
,"> intersperse ',' \"abcde\" == \"a,b,c,d,e\""]
, Command "delete" "delete"
(S `To` (LS `To` LS))
Packed
["delete :: (Eq a) => a -> [a] -> [a]"
,"'delete' x removes the first occurrence of x from its list argument."
,"For example,"
,""
,"> delete 'a' \"banana\" == \"bnana\""]
, Command "insert" "insert"
(S `To` (LS `To` LS))
Packed
["insert :: Ord a => a -> [a] -> [a]"
,"The 'insert' function takes an element and a list and inserts the"
,"element into the list at the last position where it is still less"
,"than or equal to the next element. In particular, if the list"
,"is sorted before the call, the result will also be sorted."]
, Command "(:)" "cons"
(S `To` (LS `To` LS))
Packed
["(:) :: a -> [a] -> [a]","List constructor"]
------------------------------------------------------------------------
, Command "elemIndices" "indices"
(S `To` (LS `To` LI))
List
["elemIndices :: Eq a => a -> [a] -> [Int]"
,"The 'elemIndices' function return the"
,"indices of all elements equal to the query element, in ascending order."]
------------------------------------------------------------------------
, Command "flip (!!)" "index"
(I `To` (LS `To` S))
Packed
["flip (!!) :: Int -> [a] -> a"
,"LS index (subscript) operator, starting from 0."]
------------------------------------------------------------------------
-- is this ok for zip?
, Command "(zipWith ((. (' ':)) . (++)))" "zp"
(LS `To` (LS `To` LS))
List -- the zipWith code has a char in it
["zip :: [a] -> [b] -> [(a,b)]"
,"'zip' takes two lists and returns a list of corresponding pairs."
,"If one input list is short, excess elements of the longer list are"
,"discarded."]
, Command "union" "union"
(LS `To` (LS `To` LS))
Packed
["union :: (Eq a) => [a] -> [a] -> [a]"
,"The 'union' function returns the list union of the two lists."
,"For example,"
,""
,"> \"dog\" `union` \"cow\" == \"dogcw\""
,""
,"Duplicates, and elements of the first list, are removed from the"
,"the second list, but if the first list contains duplicates, so will"
,"the result."]
, Command "(\\\\)" "difference"
(LS `To` (LS `To` LS))
Packed
["(\\\\) :: (Eq a) => [a] -> [a] -> [a]"
,"The '\\\\' function is list difference ((non-associative)."
,"In the result of xs '\\' ys, the first occurrence of each element of"
,"ys in turn (if any) has been removed from xs. Thus"
,""
,"> (xs ++ ys) \\ xs == ys."]
, Command "intersect" "intersect"
(LS `To` (LS `To` LS))
Packed
["intersect :: (Eq a) => [a] -> [a] -> [a]"
,"The 'intersect' function takes the list intersection of two lists."
,"For example,"
,""
,"> [1,2,3,4] `intersect` [2,4,6,8] == [2,4]"
,""
,"If the first list contains duplicates, so will the result."]
, Command "(++)" "append"
(LS `To` (LS `To` LS))
Packed
["(++) :: [a] -> [a] -> [a]"
,"Append two lists, i.e.,"
,""
,"[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn]"
,"[x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]"
,""
,"the first list is not finite, the result is the first list."]
------------------------------------------------------------------------
, Command "(\\f -> foldl f [])" "foldl"
((S `To` (S `To` S)) `To`(LS `To` S))
List
["foldl :: (a -> b -> a) -> a -> [b] -> a"
,"'foldl', applied to a binary operator, a starting value (typically"
,"the left-identity of the operator), and a list, reduces the list"
,"using the binary operator, from left to right:"
,""
,"> foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn"
,""
,"The list must be finite."]
, Command "(\\f -> foldr f [])" "foldr"
((S `To` (S `To` S)) `To`(LS `To` S))
List
["foldr :: (a -> b -> b) -> b -> [a] -> b"
,"'foldr', applied to a binary operator, a starting value (typically"
,"the right-identity of the operator), and a list, reduces the list"
,"using the binary operator, from right to left:"
,""
,"foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)"
]
------------------------------------------------------------------------
, Command "iterate" "iterate"
((S `To` S) `To` (S `To` LS))
List
["iterate :: (a -> a) -> a -> [a]"
,"'iterate' f x returns an infinite list of repeated applications"
,"of f to x:"
,""
,"> iterate f x == [x, f x, f (f x), ...]"]
------------------------------------------------------------------------
, Command "unfoldr" "unfoldr"
((S `To` (MaybeT (S `Pair` S))) `To` (S `To` LS))
List
["unfoldr :: (b -> Maybe (a, b)) -> b -> [a]"
,"The 'unfoldr' function is a `dual\' to 'foldr': while 'foldr'"
,"reduces a list to a summary value, 'unfoldr' builds a list from"
,"a seed value. The function takes the element and returns 'Nothing'"
,"if it is done producing the list or returns Just (a,b), in which"
,"case, a is a prepended to the list and b is used as the next"
,"element in a recursive call. For example,"
,""
,"> iterate f == unfoldr (\\x -> Just (x, f x))"
,""
,"In some cases, 'unfoldr' can undo a 'foldr' operation:"
,""
,"> unfoldr f' (foldr f z xs) == xs"
,""
,"if the following holds:"
,""
,"> f' (f x y) = Just (x,y)"
,"> f' z = Nothing"]
------------------------------------------------------------------------
, Command "($)" "ap"
((S `To` S) `To` (S `To` S))
List
["($) :: (a -> b) -> a -> b"
,"Function application."]
------------------------------------------------------------------------
, Command "concatMap" "concatmap"
((S `To` S) `To` (LS `To` S))
List
["concatMap :: (a -> a) -> [a] -> a"
,"Map a function over a list and concatenate the results."]
]