aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/static.lux
blob: 9bb38bd1d6ec4081126fa4da8d6c0ba01b77c428 (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
(.using
 [library
  [lux {"-" nat int rev}
   ["[0]" meta]
   [abstract
    [monad {"+" do}]]
   [control
    ["<>" parser
     ["<[0]>" code]]]
   [data
    [collection
     ["[0]" list ("[1]#[0]" functor)]]]
   [macro
    [syntax {"+" syntax:}]
    ["[0]" code]]
   [math
    [number {"+" hex}]
    ["[0]" random {"+" Random}]]]])

(template [<name> <type> <format>]
  [(syntax: .public (<name> [expression <code>.any])
     (# meta.monad each
        (|>> (:as <type>) <format> list)
        (meta.eval <type> expression)))]

  [nat .Nat code.nat]
  [int .Int code.int]
  [rev .Rev code.rev]
  [frac .Frac code.frac]
  [text .Text code.text]
  )

(def: pcg_32_magic_inc
  Nat
  (hex "FEDCBA9876543210"))

(with_expansions [<type> (Ex (_ a)
                           [(-> a Code)
                            a])]
  (syntax: .public (literal [format <code>.any
                             expression <code>.any])
    (do meta.monad
      [pair (meta.eval (.type <type>)
                       (` [(~ format) (~ expression)]))
       .let [[format expression] (:as <type> pair)]]
      (in (list (format expression))))))

(with_expansions [<type> (Ex (_ a)
                           [(-> a Code)
                            (List a)])]
  (syntax: .public (literals [format <code>.any
                              expression <code>.any])
    (do meta.monad
      [pair (meta.eval (.type <type>)
                       (` [(~ format) (~ expression)]))
       .let [[format expression] (:as <type> pair)]]
      (in (list#each format expression)))))

(template [<name> <random> <format>]
  [(syntax: .public (<name> [])
     (do meta.monad
       [seed meta.seed
        .let [[_ result] (random.result (random.pcg_32 [..pcg_32_magic_inc seed])
                                        <random>)]]
       (in (list (<format> result)))))]

  [random_nat random.nat code.nat]
  [random_int random.int code.int]
  [random_rev random.rev code.rev]
  [random_frac random.frac code.frac]
  )

(with_expansions [<type> (Ex (_ a)
                           [(-> a Code)
                            (Random a)])]
  (syntax: .public (random [format <code>.any
                            random <code>.any])
    (do meta.monad
      [pair (meta.eval (type <type>)
                       (` [(~ format) (~ random)]))
       .let [[format random] (:as <type> pair)]
       seed meta.seed
       .let [[_ result] (random.result (random.pcg_32 [..pcg_32_magic_inc seed])
                                       random)]]
      (in (list (format result))))))

(with_expansions [<type> (Ex (_ a)
                           [(-> a Code)
                            (Random (List a))])]
  (syntax: .public (randoms [format <code>.any
                             random <code>.any])
    (do meta.monad
      [pair (meta.eval (type <type>)
                       (` [(~ format) (~ random)]))
       .let [[format random] (:as <type> pair)]
       seed meta.seed
       .let [[_ result] (random.result (random.pcg_32 [..pcg_32_magic_inc seed])
                                       random)]]
      (in (list#each format result)))))