aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/static.lux
blob: 3f1e535ad029df0959b0cabaa71ac6214a881361 (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
(.using
 [library
  [lux {"-" nat int rev if cond}
   ["[0]" meta ("[1]#[0]" functor)]
   [abstract
    [monad {"+" do}]]
   [control
    ["<>" parser
     ["<[0]>" code]]]
   [data
    [collection
     ["[0]" list ("[1]#[0]" functor mix)]]]
   [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)))]

  [bit .Bit code.bit]
  [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)))))

(syntax: .public (seed [])
  (meta#each (|>> code.nat list) meta.seed))

(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_bit random.bit code.bit]
  [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)))))

(syntax: .public (if [test <code>.any
                      then <code>.any
                      else <code>.any])
  (do meta.monad
    [test (meta.eval .Bit test)]
    (in (list (.if (as .Bit test)
                then
                else)))))

(syntax: .public (cond [test,then/* (<>.some (<>.and <code>.any <code>.any))
                        else <code>.any])
  (in (list (list#mix (function (_ [test then] else)
                        (` (..if (~ test)
                             (~ then)
                             (~ else))))
                      else
                      (list.reversed test,then/*)))))

(syntax: .public (when [test <code>.any
                        then <code>.any])
  (do meta.monad
    [test (meta.eval .Bit test)]
    (in (.if (as .Bit test)
          (list then)
          (list)))))