aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/meta/cache/dependency/artifact.lux
blob: d6b3e055ef16eeb33360cf936bf3a7af29ebc53e (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

... https://en.wikipedia.org/wiki/Tree_shaking
(.require
 [library
  [lux (.except all)
   [abstract
    [hash (.only Hash)]
    ["[0]" monad (.only do)]]
   [data
    ["[0]" product]
    [collection
     ["[0]" list (.use "[1]#[0]" monoid mix monad)]
     ["[0]" set (.only Set)]
     ["[0]" dictionary (.only Dictionary)]
     ["[0]" sequence]]]
   [math
    [number
     ["[0]" nat]]]
   [meta
    ["[0]" symbol]
    [macro
     ["^" pattern]]
    [compiler
     ["[0]" reference (.only Constant)]
     [language
      [lux
       ["[0]" phase]
       ["[0]" synthesis (.only Path)]
       ["[0]" translation (.only Operation)]
       ["[0]" analysis
        ["[1]/[0]" complex]]]]
     [meta
      ["[0]" archive (.only Archive)
       ["[0]" artifact]
       ["[0]" registry (.only Registry)]
       ["[0]" unit]]]]]]])

(def (path_references references)
  (-> (-> synthesis.Term (List Constant))
      (-> Path (List Constant)))
  (function (again path)
    (when path
      (^.or {synthesis.#Pop}
            {synthesis.#Access _}
            {synthesis.#Bind _})
      (list)

      (^.with_template [<tag>]
        [{<tag> left right}
         (.all list#composite
               (again left)
               (again right))])
      ([synthesis.#Alt]
       [synthesis.#Seq])
      
      {synthesis.#Bit_Fork test then else}
      (when else
        {.#Some else}
        (.all list#composite
              (again then)
              (again else))
        
        {.#None}
        (again then))

      (^.with_template [<tag>]
        [{<tag> fork}
         (|> {.#Item fork}
             (list#each (|>> product.right again))
             list#conjoint)])
      ([synthesis.#I64_Fork]
       [synthesis.#F64_Fork]
       [synthesis.#Text_Fork])
      
      {synthesis.#Then then}
      (references then))))

(def (references [_ value])
  (-> synthesis.Term
      (List Constant))
  (when value
    {synthesis.#Simple value}
    (list)
    
    {synthesis.#Structure value}
    (when value
      {analysis/complex.#Variant value}
      (|> value
          (the analysis/complex.#value)
          references)
      
      {analysis/complex.#Tuple value}
      (|> value
          (list#each references)
          list#conjoint))

    {synthesis.#Reference value}
    (when value
      {reference.#Variable _}
      (list)
      
      {reference.#Constant value}
      (list value))
    
    {synthesis.#Control value}
    (when value
      {synthesis.#Branch value}
      (when value
        {synthesis.#Exec this that}
        (.all list#composite
              (references this)
              (references that))
        
        {synthesis.#Let input _ body}
        (.all list#composite
              (references input)
              (references body))
        
        {synthesis.#If test then else}
        (.all list#composite
              (references test)
              (references then)
              (references else))
        
        {synthesis.#Get _ record}
        (references record)
        
        {synthesis.#When input path}
        (.all list#composite
              (references input)
              (path_references references path)))
      
      {synthesis.#Loop value}
      (when value
        {synthesis.#Scope value}
        (let [of_inits (|> value
                           (the synthesis.#inits)
                           (list#each references))
              of_iteration (|> value
                               (the synthesis.#iteration)
                               references)]
          (list#conjoint (list.partial of_iteration of_inits)))
        
        {synthesis.#Again value}
        (|> value
            (list#each references)
            list#conjoint))
      
      {synthesis.#Function value}
      (when value
        {synthesis.#Abstraction value}
        (|> value
            (the synthesis.#body)
            references)
        
        {synthesis.#Apply function arguments}
        (|> (list.partial function arguments)
            (list#each references)
            list#conjoint)))
    
    {synthesis.#Extension [name parameters]}
    (|> parameters
        (list#each references)
        list#conjoint)))

(def .public (dependencies archive value)
  (All (_ anchor expression declaration)
    (-> Archive synthesis.Term
        (Operation anchor expression declaration (Set unit.ID))))
  (let [! phase.monad]
    (|> value
        ..references
        (set.of_list symbol.hash)
        set.list
        (monad.each ! (translation.remember archive))
        (of ! each (set.of_list unit.hash)))))

(def .public (path_dependencies archive value)
  (All (_ anchor expression declaration)
    (-> Archive Path
        (Operation anchor expression declaration (Set unit.ID))))
  (let [! phase.monad]
    (|> value
        (..path_references ..references)
        (set.of_list symbol.hash)
        set.list
        (monad.each ! (translation.remember archive))
        (of ! each (set.of_list unit.hash)))))

(def .public all
  (-> (List (Set unit.ID))
      (Set unit.ID))
  (list#mix set.union unit.none))

(def (immediate_dependencies archive)
  (-> Archive
      [(List unit.ID)
       (Dictionary unit.ID (Set unit.ID))])
  (|> archive
      archive.entries
      (list#each (function (_ [module [module_id [_module output registry]]])
                   (|> registry
                       registry.artifacts
                       sequence.list
                       (list#each (function (_ [artifact dependencies])
                                    [[module_id (the artifact.#id artifact)]
                                     (the artifact.#mandatory? artifact)
                                     dependencies])))))
      list.together
      (list#mix (function (_ [artifact_id mandatory? dependencies]
                             [mandatory_dependencies
                              all_dependencies])
                  [(if mandatory?
                     (list.partial artifact_id mandatory_dependencies)
                     mandatory_dependencies)
                   (dictionary.has artifact_id dependencies all_dependencies)])
                [(list)
                 (dictionary.empty unit.hash)])))

(def .public (necessary_dependencies archive)
  (-> Archive
      (Set unit.ID))
  (let [[mandatory immediate] (immediate_dependencies archive)]
    (loop (again [pending mandatory
                  minimum unit.none])
      (when pending
        {.#Item head tail}
        (if (set.member? minimum head)
          (again tail minimum)
          (again (when (dictionary.value head immediate)
                   {.#Some additional}
                   (list#composite (set.list additional) tail)

                   {.#None}
                   tail)
                 (set.has head minimum)))
        
        {.#End}
        minimum))))