aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/static.lux
blob: a473f82889647a85d463b9e5246f64727beb96f0 (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
(.require
 [library
  [lux (.except nat int rev if cond)
   ["[0]" meta (.use "[1]#[0]" functor)]
   [abstract
    [monad (.only do)]]
   [control
    ["<>" parser]]
   [data
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]]]
   [macro
    [syntax (.only syntax)]
    ["[0]" code (.only)
     ["<[1]>" \\parser]]]
   [math
    [number (.only hex)]
    ["[0]" random (.only Random)]]]])

(with_template [<name> <type> <format>]
  [(def .public <name>
     (syntax (_ [expression <code>.any])
       (at 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])]
  (def .public literal
    (syntax (_ [format <code>.any
                expression <code>.any])
      (do meta.monad
        [pair (meta.eval (.type_literal <type>)
                         (` [(~ format) (~ expression)]))
         .let [[format expression] (as <type> pair)]]
        (in (list (format expression)))))))

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

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

(with_template [<name> <random> <format>]
  [(def .public <name>
     (syntax (_ [])
       (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)])]
  (def .public random
    (syntax (_ [format <code>.any
                random <code>.any])
      (do meta.monad
        [pair (meta.eval (type_literal <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))])]
  (def .public randoms
    (syntax (_ [format <code>.any
                random <code>.any])
      (do meta.monad
        [pair (meta.eval (type_literal <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))))))

(def .public if
  (syntax (_ [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))))))

(def .public cond
  (syntax (_ [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/*))))))

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