aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/scriptum.lux
blob: 04570a9bb5ea63e8cf3c7781c1fad030399cdd85 (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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
(.module:
  [lux #*
   [control
    [pipe (#+ when>)]
    ["." monad (#+ do)]
    ["ex" exception (#+ exception:)]
    [security
     ["!" capability]]]
   [cli (#+ program:)]
   [data
    ["." maybe]
    ["." product]
    ["." error (#+ Error)]
    [format
     ["md" markdown (#+ Markdown Span Block)]]
    ["." text ("#/." equivalence)
     format
     ["." encoding]]
    [collection
     ["." sequence (#+ Sequence) ("#/." functor)]
     ["." list ("#/." functor fold)]]]
   ["." function]
   ["." type]
   ["." macro]
   ["." io (#+ IO io)]
   [world
    ["." file (#+ File)]]]
  ## This was added to make sure that all tested modules are picked up
  ## and their documentation is generated.
  [test/lux (#+)])

(def: name-options "abcdefghijklmnopqrstuvwxyz")
(def: name-options-count (text.size name-options))

(def: (parameter-type-name id)
  (-> Nat Text)
  (case (text.nth id ..name-options)
    (#.Some char)
    (text.from-code char)

    #.None
    (format (parameter-type-name (n// name-options-count id))
            (parameter-type-name (n/% name-options-count id)))))

(def: type-var-names
  (Sequence Text)
  (|> 0 (sequence.iterate inc) (sequence/map parameter-type-name)))

(do-template [<name> <partition>]
  [(def: (<name> id)
     (-> Nat Bit)
     (<partition> id))]

  [type-func? n/even?]
  [type-arg? n/odd?]
  )

(def: (arg-id level id)
  (-> Nat Nat Nat)
  (n/- (n// 2 id) level))

(def: (parameter->name [type-func-name type-function-arguments] level id)
  (-> [Text (List Text)] Nat Nat Text)
  (if (type-arg? id)
    (let [arg-id (..arg-id level id)]
      (case (list.nth arg-id type-function-arguments)
        (#.Some found)
        found

        _
        (|> type-var-names
            (sequence.filter (function (_ var-name)
                               (not (list.member? text.equivalence type-function-arguments var-name))))
            (sequence.nth arg-id))))      
    type-func-name))

(def: (level->args offset level)
  (-> Nat Nat (List Text))
  (if (n/= 0 level)
    (list)
    (|> level
        dec
        (list.n/range 0)
        (list/map (|>> (n/+ (inc offset)) parameter-type-name)))))

(def: (prefix-lines prefix lines)
  (-> Text Text Text)
  (|> lines
      (text.split-all-with text.new-line)
      (list/map (|>> (format prefix)))
      (text.join-with text.new-line)))

(def: (pprint-type-definition level type-func-info tags module signature? recursive-type? type)
  (-> Nat [Text (List Text)] (List Name) Text Bit Bit Type Text)
  (case tags
    (^ (list [_ single-tag]))
    (if signature?
      (format "(: " (pprint-type-definition level type-func-info #.None module signature? recursive-type? type) text.new-line "   " single-tag ")")
      (format "{#" single-tag " " (pprint-type-definition level type-func-info #.None module signature? recursive-type? type) "}"))

    _
    (case [recursive-type? type]
      [_ (#.Primitive name params)]
      (case params
        #.Nil
        (format "(primitive " (%t name) ")")

        _
        (format "(primitive " (%t name) " " (|> params (list/map (pprint-type-definition level type-func-info #.None module signature? recursive-type?)) (text.join-with " ")) ")"))

      [_ (#.Sum _)]
      (let [members (type.flatten-variant type)]
        (case tags
          #.Nil
          (format "(| "
                  (|> members
                      (list/map (pprint-type-definition level type-func-info #.None module signature? recursive-type?))
                      (text.join-with " "))
                  ")")

          _
          (|> members
              (list.zip2 tags)
              (list/map (function (_ [[_ t-name] type])
                          (case type
                            (#.Product _)
                            (let [types (type.flatten-tuple type)]
                              (format "(#" t-name " "
                                      (|> types
                                          (list/map (pprint-type-definition level type-func-info #.None module signature? recursive-type?))
                                          (text.join-with " "))
                                      ")"))

                            _
                            (format "(#" t-name " " (pprint-type-definition level type-func-info #.None module signature? recursive-type? type) ")"))))
              (text.join-with text.new-line))))

      [_ (#.Product _)]
      (let [members (type.flatten-tuple type)]
        (case tags
          #.Nil
          (format "[" (|> members (list/map (pprint-type-definition level type-func-info #.None module signature? recursive-type?)) (text.join-with " ")) "]")

          _
          (let [member-docs (|> members
                                (list.zip2 tags)
                                (list/map (function (_ [[_ t-name] type])
                                            (if signature?
                                              (format "(: " (pprint-type-definition level type-func-info #.None module signature? recursive-type? type) text.new-line "   "  t-name ")")
                                              (format "#" t-name " " (pprint-type-definition level type-func-info #.None module signature? recursive-type? type)))))
                                (text.join-with (format text.new-line " ")))]
            (if signature?
              member-docs
              (format "{" member-docs "}")))))

      [_ (#.Function input output)]
      (let [[ins out] (type.flatten-function type)]
        (format  "(-> " (|> ins (list/map (pprint-type-definition level type-func-info #.None module signature? recursive-type?)) (text.join-with " "))
                 " "
                 (pprint-type-definition level type-func-info #.None module signature? recursive-type? out)
                 ")"))

      [_ (#.Parameter idx)]
      (parameter->name type-func-info level idx)

      (^template [<tag> <pre> <post>]
        [_ (<tag> id)]
        (format <pre> (%n id) <post>))
      ([#.Var "⌈v:" "⌋"]
       [#.Ex  "⟨e:" "⟩"])

      (^template [<tag> <name> <flatten>]
        [_ (<tag> _)]
        (let [[level' body] (<flatten> type)
              args (level->args level level')
              body-doc (pprint-type-definition (n/+ level level') type-func-info tags module signature? recursive-type? body)]
          (format "(" <name> " " "[" (text.join-with " " args) "]"
                  (case tags
                    #.Nil
                    (format " " body-doc)

                    _
                    (format text.new-line (prefix-lines "  " body-doc)))
                  ")")))
      ([#.UnivQ "All" type.flatten-univ-q]
       [#.ExQ   "Ex"  type.flatten-ex-q])

      [true (#.Apply (#.Parameter 1) (#.Parameter 0))]
      (product.left type-func-info)

      [_ (#.Apply param fun)]
      (let [[type-func type-arguments] (type.flatten-application type)]
        (format  "(" (pprint-type-definition level type-func-info tags module signature? recursive-type? type-func) " " (|> type-arguments (list/map (pprint-type-definition level type-func-info #.None module signature? recursive-type?)) (text.join-with " ")) ")"))

      [_ (#.Named [_module _name] type)]
      (if (text/= module _module)
        _name
        (%name [_module _name]))
      )))

(def: (pprint-type level type-func-name module type)
  (-> Nat Text Text Type Text)
  (case type
    (#.Primitive name params)
    (case params
      #.Nil
      (format "(primitive " (%t name) ")")

      _
      (format "(primitive " (%t name) " " (|> params (list/map (pprint-type level type-func-name module)) (list.interpose " ") (text.join-with "")) ")"))

    (#.Sum _)
    (let [members (type.flatten-variant type)]
      (format "(| " (|> members (list/map (pprint-type level type-func-name module)) (list.interpose " ") (text.join-with "")) ")"))

    (#.Product _)
    (let [members (type.flatten-tuple type)]
      (format "[" (|> members (list/map (pprint-type level type-func-name module)) (list.interpose " ") (text.join-with "")) "]"))

    (#.Function input output)
    (let [[ins out] (type.flatten-function type)]
      (format  "(-> "
               (|> ins (list/map (pprint-type level type-func-name module)) (list.interpose " ") (text.join-with ""))
               " "
               (pprint-type level type-func-name module out)
               ")"))

    (#.Parameter idx)
    (parameter->name [type-func-name (list)] level idx)

    (^template [<tag> <pre> <post>]
      (<tag> id)
      (format <pre> (%n id) <post>))
    ([#.Var "⌈" "⌋"]
     [#.Ex  "⟨" "⟩"])

    (^template [<tag> <name> <flatten>]
      (<tag> _)
      (let [[level' body] (<flatten> type)
            args (level->args level level')
            body-doc (pprint-type (n/+ level level') type-func-name module body)]
        (format "(" <name> " " "[" (|> args (list.interpose " ") (text.join-with "")) "]"
                (format " " body-doc)
                ")")))
    ([#.UnivQ "All" type.flatten-univ-q]
     [#.ExQ   "Ex"  type.flatten-ex-q])

    (#.Apply param fun)
    (let [[type-func type-arguments] (type.flatten-application type)]
      (format  "(" (pprint-type level type-func-name module type-func) " " (|> type-arguments (list/map (pprint-type level type-func-name module)) (list.interpose " ") (text.join-with "")) ")"))

    (#.Named [_module _name] type)
    (if (text/= module _module)
      _name
      (%name [_module _name]))
    ))

(type: (Mutation a)
  (-> a a))

(type: Value [Text Code Type])

(type: Organization
  {#types (List Value)
   #macros (List [Text Code])
   #structures (List Value)
   #values (List Value)})

(def: (lux-module? module-name)
  (-> Text Bit)
  (or (text/= "lux" module-name)
      (text.starts-with? "lux/" module-name)))

(def: (add-definition [name [def-type def-annotations def-value]] organization)
  (-> [Text Definition] Organization Organization)
  (cond (macro.type? def-annotations)
        (update@ #types
                 (: (Mutation (List Value))
                    (|>> (#.Cons [name def-annotations (:coerce Type def-value)])))
                 organization)

        (macro.macro? def-annotations)
        (update@ #macros
                 (: (Mutation (List [Text Code]))
                    (|>> (#.Cons [name def-annotations])))
                 organization)

        (macro.structure? def-annotations)
        (update@ #structures
                 (: (Mutation (List Value))
                    (|>> (#.Cons [name def-annotations def-type])))
                 organization)

        ## else
        (update@ #values
                 (: (Mutation (List Value))
                    (|>> (#.Cons [name def-annotations def-type])))
                 organization)))

(def: name-sort
  (All [r] (-> [Text r] [Text r] Bit))
  (let [text/< (:: text.order <)]
    (function (_ [n1 _] [n2 _])
      (text/< n1 n2))))

(def: (organize-definitions defs)
  (-> (List [Text Definition]) Organization)
  (let [init {#types (list)
              #macros (list)
              #structures (list)
              #values (list)}]
    (|> (list/fold add-definition init defs)
        (update@ #types (list.sort name-sort))
        (update@ #macros (list.sort name-sort))
        (update@ #structures (list.sort name-sort))
        (update@ #values (list.sort name-sort)))))

(def: (unravel-type-func level type)
  (-> Nat Type Type)
  (if (n/> 0 level)
    (case type
      (#.UnivQ _env _type)
      (unravel-type-func (dec level) _type)

      _
      type)
    type))

(def: (unrecurse-type type)
  (-> Type Type)
  (case type
    (#.Apply _ (#.UnivQ _env _type))
    _type

    _
    type))

(exception: #export (anonymous-type-definition {type Type})
  (ex.report ["Type" (%type type)]))

(def: (document-type module type def-annotations)
  (-> Text Type Code (Meta (Markdown Block)))
  (case type
    (#.Named type-name type)
    (do macro.monad
      [tags (macro.tags-of type-name)
       #let [[_ _name] type-name
             recursive-type? (macro.recursive-type? def-annotations)
             type-arguments (macro.type-arguments def-annotations)
             signature? (macro.signature? def-annotations)
             usage (case type-arguments
                     #.Nil
                     _name

                     _
                     (format "(" (text.join-with " " (list& _name type-arguments)) ")"))
             nesting (list.size type-arguments)]]
      (wrap (md.code (format (if signature? "(signature: " "(type: ")
                             (if recursive-type? "#rec " "")
                             usage text.new-line
                             (|> type
                                 (unravel-type-func nesting)
                                 (when> recursive-type? [unrecurse-type])
                                 (pprint-type-definition (dec nesting) [_name type-arguments] (maybe.default (list) tags) module signature? recursive-type?)
                                 (text.split-all-with text.new-line)
                                 (list/map (|>> (format "  ")))
                                 (text.join-with text.new-line))
                             ")"))))

    _
    (macro.fail (ex.construct anonymous-type-definition type))))

(def: (document-types module types)
  (-> Text (List Value) (Meta (Markdown Block)))
  (do macro.monad
    [type-docs (monad.map @
                          (: (-> Value (Meta (Markdown Block)))
                             (function (_ [name def-annotations type])
                               (do macro.monad
                                 [#let [?doc (macro.get-documentation def-annotations)]
                                  type-code (document-type module type def-annotations)]
                                 (wrap ($_ md.then
                                           (md.heading/3 name)
                                           (case ?doc
                                             (#.Some doc)
                                             (md.paragraph (md.text doc))

                                             _
                                             md.empty)
                                           type-code)))))
                          types)]
    (wrap (list/fold (function.flip md.then)
                     (md.heading/2 "Types")
                     type-docs))))

(def: (document-macros module-name names)
  (-> Text (List [Text Code]) (Markdown Block))
  (|> names
      (list/map (: (-> [Text Code] (Markdown Block))
                   (function (_ [name def-annotations])
                     ($_ md.then
                         (md.heading/3 name)
                         (<| (: (Markdown Block))
                             (maybe.default md.empty)
                             (do maybe.monad
                               [documentation (macro.get-documentation def-annotations)]
                               (wrap (md.code documentation))))))))
      (list/fold (function.flip md.then)
                 (md.heading/2 "Macros"))))

(do-template [<singular> <plural> <header>]
  [(def: (<singular> module type)
     (-> Text Type (Markdown Block))
     (md.code (pprint-type (dec 0) "?" module type)))

   (def: (<plural> module values)
     (-> Text (List Value) (Markdown Block))
     (|> values
         (list/map (function (_ [name def-annotations value-type])
                     (let [?doc (macro.get-documentation def-annotations)
                           usage (case (macro.function-arguments def-annotations)
                                   #.Nil
                                   name

                                   args
                                   (format "(" (text.join-with " " (list& name args)) ")"))]
                       ($_ md.then
                           (md.heading/3 usage)
                           (case ?doc
                             (#.Some doc)
                             (md.code doc)

                             _
                             md.empty)
                           (<singular> module value-type)))))
         (list/fold (function.flip md.then)
                    (md.heading/2 <header>))))]

  [document-structure document-structures "Structures"]
  [document-value     document-values     "Values"]
  )

(def: (enclose-lines pre+post block)
  (-> [Text Text] Text Text)
  (|> block
      (text.split-all-with text.new-line)
      (list/map (text.enclose pre+post))
      (text.join-with text.new-line)))

(def: (document-module [[module-name module] organization])
  (-> [[Text Module] Organization] (Meta [Text (Markdown Block)]))
  (do macro.monad
    [#let [(^slots [#types #macros #structures #values]) organization
           annotations (|> module
                           (get@ #.module-annotations)
                           (maybe.default (' {}))
                           macro.get-documentation)
           description (case annotations
                         (#.Some doc-text)
                         (md.quote (md.paragraph (md.text doc-text)))

                         #.None
                         md.empty)
           empty-block (: (Markdown Block) md.empty)]
     types-documentation (if (list.empty? types)
                           (wrap empty-block)
                           (document-types module-name types))
     #let [documentation ($_ md.then
                             types-documentation
                             (if (list.empty? macros) empty-block (document-macros module-name macros))
                             (if (list.empty? structures) empty-block (document-structures module-name structures))
                             (if (list.empty? values) empty-block (document-values module-name values)))]]
    (wrap [module-name
           ($_ md.then
               (md.heading/1 module-name)
               description
               documentation)])))

(exception: #export (io-error {error Text})
  error)

(def: (save-documentation! [module-name documentation])
  (-> [Text (Markdown Block)] (IO Any))
  (let [path (format (text.replace-all "/" "_" module-name) ".md")]
    (do io.monad
      [outcome (do (error.with-error io.monad)
                 [target (: (IO (Error (File IO)))
                            (file.get-file io.monad file.system path))]
                 (!.use (:: target over-write) (encoding.to-utf8 (md.markdown documentation))))]
      (case outcome
        (#error.Failure error)
        (wrap (log! (ex.construct io-error error)))

        (#error.Success _)
        (wrap [])))))

(macro: (gen-documentation! _)
  (do macro.monad
    [all-modules macro.modules
     #let [lux-modules (|> all-modules
                           (list.filter (function.compose lux-module? product.left))
                           (list.sort name-sort))]
     lux-exports (monad.map @ (function.compose macro.exports product.left)
                            lux-modules)
     module-documentation (|> (list/map organize-definitions lux-exports)
                              (list.zip2 lux-modules)
                              (monad.map @ document-module))
     #let [_ (io.run (monad.map io.monad save-documentation! module-documentation))]]
    (wrap (list))))

(gen-documentation!)

(program: args
  (io (log! "Done!")))