aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/cache/dependency/artifact.lux
blob: 1ab3be9eb8cdd3b8037e228ca056a983391d7782 (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
... https://en.wikipedia.org/wiki/Tree_shaking
(.using
 [library
  [lux "*"
   [abstract
    [hash {"+" Hash}]
    ["[0]" monad {"+" do}]]
   [data
    ["[0]" product]
    [collection
     ["[0]" list ("[1]#[0]" monoid mix monad)]
     ["[0]" set {"+" Set}]
     ["[0]" dictionary {"+" Dictionary}]
     ["[0]" sequence]]]
   [macro
    ["^" pattern]]
   [math
    [number
     ["[0]" nat]]]
   [meta
    ["[0]" symbol]]
   [tool
    [compiler
     ["[0]" phase]
     ["[0]" reference {"+" Constant}]
     [language
      [lux
       ["[0]" synthesis {"+" Synthesis Path}]
       ["[0]" generation {"+" Operation}]
       ["[0]" analysis
        ["[1]/[0]" complex]]]]
     [meta
      ["[0]" archive {"+" Archive}
       ["[0]" artifact]
       ["[0]" registry {"+" Registry}]
       ["[0]" unit]]]]]]])

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

      (^.template [<tag>]
        [{<tag> left right}
         ($_ list#composite
             (again left)
             (again right))])
      ([synthesis.#Alt]
       [synthesis.#Seq])
      
      {synthesis.#Bit_Fork when then else}
      (case else
        {.#Some else}
        ($_ list#composite
            (again then)
            (again else))
        
        {.#None}
        (again then))

      (^.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 (List Constant))
  (case value
    {synthesis.#Simple value}
    (list)
    
    {synthesis.#Structure value}
    (case value
      {analysis/complex.#Variant value}
      (|> value
          (the analysis/complex.#value)
          references)
      
      {analysis/complex.#Tuple value}
      (|> value
          (list#each references)
          list#conjoint))

    {synthesis.#Reference value}
    (case value
      {reference.#Variable _}
      (list)
      
      {reference.#Constant value}
      (list value))
    
    {synthesis.#Control value}
    (case value
      {synthesis.#Branch value}
      (case value
        {synthesis.#Exec this that}
        ($_ list#composite
            (references this)
            (references that))
        
        {synthesis.#Let input _ body}
        ($_ list#composite
            (references input)
            (references body))
        
        {synthesis.#If test then else}
        ($_ list#composite
            (references test)
            (references then)
            (references else))
        
        {synthesis.#Get _ record}
        (references record)
        
        {synthesis.#Case input path}
        ($_ list#composite
            (references input)
            (path_references references path)))
      
      {synthesis.#Loop value}
      (case value
        {synthesis.#Scope value}
        (|> value
            (the synthesis.#iteration)
            references)
        
        {synthesis.#Again value}
        (|> value
            (list#each references)
            list#conjoint))
      
      {synthesis.#Function value}
      (case value
        {synthesis.#Abstraction value}
        (|> value
            (the synthesis.#body)
            references)
        
        {synthesis.#Apply function arguments}
        (|> (list& 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 directive)
    (-> Archive Synthesis (Operation anchor expression directive (Set unit.ID))))
  (let [! phase.monad]
    (|> value
        ..references
        (set.of_list symbol.hash)
        set.list
        (monad.each ! (generation.remember archive))
        (# ! each (set.of_list unit.hash)))))

(def: .public (path_dependencies archive value)
  (All (_ anchor expression directive)
    (-> Archive Path (Operation anchor expression directive (Set unit.ID))))
  (let [! phase.monad]
    (|> value
        (..path_references ..references)
        (set.of_list symbol.hash)
        set.list
        (monad.each ! (generation.remember archive))
        (# ! 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& 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])
      (case pending
        {.#Item head tail}
        (if (set.member? minimum head)
          (again tail minimum)
          (again (case (dictionary.value head immediate)
                   {.#Some additional}
                   (list#composite (set.list additional) tail)

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