aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/documentation/lux/control/concatenative.lux
blob: 104f593816e87f856c969f9043af6a58aece1a51 (plain)
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
(.module:
  [library
   [lux {"-" [if loop]}
    ["$" documentation {"+" [documentation:]}]
    [data
     [text {"+" [\n]}
      ["%" format {"+" [format]}]]
     [collection
      ["." list ("#\." monad)]]]
    [macro
     ["." template]]
    [math
     [number
      ["n" nat]
      ["i" int]
      ["r" rev]
      ["f" frac]]]]]
  [\\library
   ["." /]])

(documentation: /.=>
  "Concatenative function types."
  [(=> [Nat] [Nat])]
  [(All (_ a)
     (-> a (=> [] [a])))]
  [(All (_ t)
     (=> [t] []))]
  [(All (_ a b c)
     (=> [a b c] [b c a]))]
  [(All (_ ___a ___z)
     (=> {then (=> ___a ___z)
          else (=> ___a ___z)}
         ___a [Bit then else] ___z))])

(documentation: /.||>
  "A self-contained sequence of concatenative instructions."
  [(same? value
          (||> (push sample)))]
  [(||> (push 123)
        dup
        n/=)])

(documentation: /.word:
  "A named concatenative function."
  [(word: square
     (=> [Nat] [Nat])

     dup
     (apply/2 n.*))])

(documentation: /.apply
  "A generator for functions that turn arity N functions into arity N concatenative functions."
  [(: (=> [Nat] [Nat])
      ((apply 1) ++))])

(template [<arity>]
  [(with_expansions [<name> (template.identifier [/._] ["apply/" <arity>])
                     <doc> (template.text ["Lift a function of arity " <arity>
                                           " into a concatenative function of arity " <arity> "."])]
     (documentation: <name>
       <doc>))]

  [1] [2] [3] [4]
  [5] [6] [7] [8]
  )

(documentation: /.push
  "Push a value onto the stack.")

(documentation: /.drop
  "Drop/pop a value from the top of the stack.")

(documentation: /.nip
  "Drop the second-to-last value from the top of the stack.")

(documentation: /.dup
  "Duplicate the top of the stack.")

(documentation: /.swap
  "Swaps the 2 topmost stack values.")

(documentation: /.rotL
  "Rotes the 3 topmost stack values to the left.")

(documentation: /.rotR
  "Rotes the 3 topmost stack values to the right.")

(documentation: /.&&
  "Groups the 2 topmost stack values as a 2-tuple.")

(documentation: /.||L
  "Left-injects the top into sum.")

(documentation: /.||R
  "Right-injects the top into sum.")

(template [<input> <word> <func>]
  [(`` (documentation: (~~ (template.identifier [/._] [<word>]))
         (~~ (template.text [<func> " for " <input> " arithmetic."]))))]

  [Nat  n/+  n.+]
  [Nat  n/-  n.-]
  [Nat  n/*  n.*]
  [Nat  n//  n./]
  [Nat  n/%  n.%]
  [Nat  n/=  n.=]
  [Nat  n/<  n.<]
  [Nat  n/<= n.<=]
  [Nat  n/>  n.>]
  [Nat  n/>= n.>=]

  [Int  i/+  i.+]
  [Int  i/-  i.-]
  [Int  i/*  i.*]
  [Int  i//  i./]
  [Int  i/%  i.%]
  [Int  i/=  i.=]
  [Int  i/<  i.<]
  [Int  i/<= i.<=]
  [Int  i/>  i.>]
  [Int  i/>= i.>=]

  [Rev  r/+  r.+]
  [Rev  r/-  r.-]
  [Rev  r/*  r.*]
  [Rev  r//  r./]
  [Rev  r/%  r.%]
  [Rev  r/=  r.=]
  [Rev  r/<  r.<]
  [Rev  r/<= r.<=]
  [Rev  r/>  r.>]
  [Rev  r/>= r.>=]

  [Frac f/+  f.+]
  [Frac f/-  f.-]
  [Frac f/*  f.*]
  [Frac f//  f./]
  [Frac f/%  f.%]
  [Frac f/=  f.=]
  [Frac f/<  f.<]
  [Frac f/<= f.<=]
  [Frac f/>  f.>]
  [Frac f/>= f.>=]
  )

(documentation: /.if
  "If expression."
  [(same? "then"
          (||> (push true)
               (push "then")
               (push "else")
               if))])

(documentation: /.call
  "Executes an anonymous block on the stack.")

(documentation: /.loop
  "Executes a block as a loop until it yields #0 to stop.")

(documentation: /.dip
  "Executes a block on the stack, save for the topmost value.")

(documentation: /.dip/2
  "Executes a block on the stack, save for the 2 topmost values.")

(documentation: /.do
  "Do-while loop expression."
  [(n.= (++ sample)
        (||> (push sample)
             (push (push false))
             (push (|>> (push 1) n/+))
             do while))])

(documentation: /.while
  "While loop expression."
  [(n.= (n.+ distance start)
        (||> (push start)
             (push (|>> dup
                        (push start) n/-
                        (push distance) n/<))
             (push (|>> (push 1) n/+))
             while))])

(documentation: /.compose
  "Function composition."
  [(n.= (n.+ 2 sample)
        (||> (push sample)
             (push (|>> (push 1) n/+))
             (push (|>> (push 1) n/+))
             compose
             call))])

(documentation: /.partial
  "Partial application."
  [(n.= (n.+ sample sample)
        (||> (push sample)
             (push sample)
             (push n/+)
             partial
             call))])

(documentation: /.when
  "Only execute the block when #1.")

(documentation: /.?
  "Choose the top value when #0 and the second-to-top when #1.")

(.def: .public documentation
  (.List $.Module)
  ($.module /._
            ""
            [..=>
             ..||>
             ..word:
             ..apply
             ..apply/1
             ..apply/2
             ..apply/3
             ..apply/4
             ..apply/5
             ..apply/6
             ..apply/7
             ..apply/8
             ..push
             ..drop
             ..nip
             ..dup
             ..swap
             ..rotL
             ..rotR
             ..&&
             ..||L
             ..||R
             ..if
             ..call
             ..loop
             ..dip
             ..dip/2
             ..do
             ..while
             ..compose
             ..partial
             ..when
             ..?

             ..n/+
             ..n/-
             ..n/*
             ..n//
             ..n/%
             ..n/=
             ..n/<
             ..n/<=
             ..n/>
             ..n/>=
             ..i/+
             ..i/-
             ..i/*
             ..i//
             ..i/%
             ..i/=
             ..i/<
             ..i/<=
             ..i/>
             ..i/>=
             ..r/+
             ..r/-
             ..r/*
             ..r//
             ..r/%
             ..r/=
             ..r/<
             ..r/<=
             ..r/>
             ..r/>=
             ..f/+
             ..f/-
             ..f/*
             ..f//
             ..f/%
             ..f/=
             ..f/<
             ..f/<=
             ..f/>
             ..f/>=]
            []))