aboutsummaryrefslogtreecommitdiff
path: root/source/util.lux
blob: 88b035571f34fc3b8d713b822ea289683c16bfe6 (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
(def (fail* message)
  (#Failure message))

(def (return* state value)
  (#Ok [state value]))

(def (fail message)
  (lambda [state]
     (#Failure message)))

(def (return value)
  (lambda [state]
     (#Ok [state value])))

(def (bind m-value step)
  (lambda [state]
     (let inputs (m-value state)
          (case inputs
            (#Ok [?state ?datum])
            (step ?datum ?state)
            
            _
            inputs))))

## Ideally, this is what I want...
## (exec [yolo lol
##        #let [foo (bar 1 2 3)]]
##   (meme yolo foo))

(defmacro (exec tokens)
  (case tokens
    (#Cons (#Tuple steps) (#Cons value #Nil))
    (fold (lambda [inner pair]
             (case pair
               [label computation]
               (' (bind (~ computation) (lambda [(~ label)] (~ inner))))))
          value
          (pairs steps))))

(def (try-m monad)
  (lambda [state]
     (case (monad state)
       (#Ok [?state ?datum])
       (return* ?state (#Just ?datum))
       
       (#Failure _)
       (return* state #Nothing))))

(def (repeat-m monad)
  (lambda [state]
     (case (monad state)
       (#Ok [?state ?head])
       (case ((repeat-m monad) ?state)
         (#Ok [?state* ?tail])
         (return* ?state* (#Cons ?head ?tail)))
       
       (#Failure ?message)
       (return* state #Nil))))

(def (try-all-m monads)
  (lambda [state]
     (case monads
       #Nil
       (fail* "No alternative worked!")
       (#Cons monad monads')
       (let output (monad state)
            (case output
              (#Ok _)
              output
              
              (#Failure _)
              (case monads'
                #Nil
                output
                (#Cons _ _)
                ((try-all-m monads') state))
              ))
       )))

(def (map-m f inputs)
  (case inputs
    #Nil
    (return #Nil)
    (#Cons input inputs')
    (exec [output (f input)
           outputs (map-m f inputs')]
      (return (#Cons output outputs)))))

(def (fold-m f init inputs)
  (case inputs
    #Nil              (return init)
    (#Cons x inputs') (exec [init* (f init x)]
                        (fold-m f init* inputs'))))

(def (apply-m monad call-state)
  (lambda [state]
     (let output (monad call-state)
          (case output
            (#Ok [?state ?datum])
            (#Ok [state ?datum])

            _
            output))))

(def (assert test message)
  (if test
    (return [])
    (fail message)))

(def (pass %value)
  (lambda [state]
     %value))

(def get-state
  (lambda [state]
     (return* state state)))

(def (normalize-char char)
  (case char
    #"*" "_ASTER_"
    #"+" "_PLUS_"
    #"-" "_DASH_"
    #"/" "_SLASH_"
    #"_" "_UNDERS_"
    #"%" "_PERCENT_"
    #"$" "_DOLLAR_"
    #"'" "_QUOTE_"
    #"`" "_BQUOTE_"
    #"@" "_AT_"
    #"^" "_CARET_"
    #"&" "_AMPERS_"
    #"=" "_EQ_"
    #"!" "_BANG_"
    #"?" "_QM_"
    #":" "_COLON_"
    #";" "_SCOLON_"
    #"." "_PERIOD_"
    #"," "_COMMA_"
    #"<" "_LT_"
    #">" "_GT_"
    #"~" "_TILDE_"
    ##;;#"\" "_BSLASH_"
    _    (show char)
    ))

(def (normalize-ident ident)
  (fold concat "" (map normalize-char (text->list ident))))

(def (within slot monad)
  (lambda [state]
     (let =return (monad (get slot state))
          (case =return
            (#Ok ?state ?value)
            (#Ok (put slot ?state state) ?value)
            
            _
            =return))))

(def (run-state monad state)
  (monad state))

(def (fresh-class-loader path)
  (let file (jvm/new java.io.File [String] [path])
       (let url (jvm/invokevirtual java.io.File "toURL" []
                                   file [])
            (let urls (jvm/new-array java.net.URL 1)
                 (do (jvm/aastore urls 0 url)
                   (jvm/new java.net.URLClassLoader [(Array java.net.URL)] [urls]))))
       ))