Source file BuildRebuilders.ml

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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
(** An OCaml embedding of rebuilders described in "Build systems à la carte:
    Theory and practice".

    This embedding is not necessary for MlFront thunks; we can implement thunks
    without using a generalized framework! However, this embedding may aid
    modifications to thunk implementations.

    Confer
    {{:https://www.cambridge.org/core/services/aop-cambridge-core/content/view/097CE52C750E69BD16B78C318754C7A4/S0956796820000088a.pdf/build-systems-a-la-carte-theory-and-practice.pdf}5
     Rebuilders}.

    {v
Andrey Mokhov, Neil Mitchell, and Simon Peyton Jones. 2018. Build Systems à la Carte. Proc. ACM Program. Lang. 2, ICFP, Article 79 (September 2018) 
doi:10.1017/S0956796820000088
    v} *)

(** The type of synchronous rebuilder build system components. *)
module type REBUILDER = sig
  type vc
  (** The type of applicative/monad-constrained build task value. *)

  type ir
  (** The type of persistent build information carried by a state monad.

      The state can be combined with persistent build information from a
      {!SCHEDULER} to make the full persistent build information. *)

  type k
  (** The type of build task key. *)

  type v
  (** The type of build task value. *)

  type o
  (** The type of build task origin. *)

  type tracked
  (** The type of what is being tracked for a build task value. *)

  type lifted_vc
  type task = k * ((o -> k -> vc) -> vc)
  type rebuilt_vc = ir -> v * ir * tracked
  type rebuilt_task = k * ((o -> k -> rebuilt_vc) -> rebuilt_vc)

  type t = ir -> o -> v -> task -> rebuilt_task
  (** The type of rebuilders which take an information recaller [ir], an origin
      [o], a value [v], and a task [task] to produce a rebuilt task. *)
end

let rebuilder_async_cls =
  BuildConstraints.StateMessage.new_class_exn
    "MlFront_Thunk.BuildRebuilders.CtRebuilderAsync"

(** The type of asynchronous rebuilder build system components. *)
module type REBUILDER_ASYNC = sig
  type vc
  (** The type of applicative/monad-constrained build task value. *)

  type ir
  (** The type of persistent build information carried by a state monad.

      The state can be combined with persistent build information from a
      {!SCHEDULER} to make the full persistent build information. *)

  type k
  (** The type of build task key. *)

  type v
  (** The type of build task value. *)

  type o
  (** The type of build task origin. *)

  type state

  type tracked
  (** The type of what is being tracked for a build task value. *)

  type 'a promise
  type journal
  type journal_entry_id
  type journal_entry_value
  type task = k * ((o -> k -> vc) -> vc)
  type ('k, 'v) key_dependent_hash

  type rebuilt_vc =
    (state * journal ->
    (v
    * BuildConstraints.StateMessage.t list
    * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
    promise)
    promise

  type rebuilt_task = k * ((o -> k -> rebuilt_vc) -> rebuilt_vc)

  type t = ir -> o -> v -> task -> rebuilt_task
  (** The type of rebuilders which take an information recaller [ir], an origin
      [o], a value [v], and a task [task] to produce a rebuilt task. *)

  type BuildConstraints.StateMessage.obj +=
    | PostBuildValueAndDependencyHashes of
        (k * (k, v) key_dependent_hash option) list * v
end

(** Functions common to async and sync constructive trace rebuilders. *)
module CtRebuilderUtils
    (K : BuildSystems.ABSTRACT_TYPE)
    (V : BuildSystems.ABSTRACT_TYPE)
    (CT : BuildSystems.CLOUD_PERSISTENT_HASH with type k = K.t and type v = V.t) =
struct
  (** [is_elem k v list_of_v] is [true] if and only if the key [k] dependent
      value [v] is a member of the value list [list_of_v].

      Membership is considered using the hash of the value. This is the source
      of the {!BuildSystems.CLOUD_PERSISTENT_HASH} technical requirement that
      the hash {b must be a secure hash} so that hash equality means value
      equality. *)
  let is_elem (k : K.t) (v : V.t) (vl : V.t list) =
    match CT.maybe_cloud_persistent_hash k v with
    | None -> false
    | Some h ->
        List.exists
          (fun v' ->
            match CT.maybe_cloud_persistent_hash k v' with
            | None -> false
            | Some h' -> CT.strong_hash_equal h h')
          vl
end

(** Constructive trace rebuilder.

    [CT] is the functor parameter for the constructive trace store. Its hash
    function {b must be a "secure hash" like SHA-256} in that two values can be
    considered equal if their (secure) hashes are equal.
    {b Do not use conventional hashes} like OCaml's {!Stdlib.Hashtbl.hash} or a
    MD5 hash which do not imply that hash equality is the same as value
    equality.*)
module CtRebuilder
    (IR : BuildSystems.ABSTRACT_TYPE)
    (K : BuildSystems.ABSTRACT_TYPE)
    (V : BuildSystems.ABSTRACT_TYPE)
    (O : BuildSystems.ORIGIN_WITH_DEPTH with type k = K.t)
    (C :
      BuildConstraints.MONAD_STATE_WRITER
        with type state = IR.t
         and type output = (K.t * V.t) list)
    (CT :
      BuildTraces.CONSTRUCTIVE_TRACE_STORE_SYNC
        with type k = K.t
         and type v = V.t
         and type ir = IR.t
         and module C = C)
    (Tasks :
      BuildSystems.TASKS
        with type k = K.t
         and type vc = V.t C.t
         and type lifted_vc = V.t C.t
         and type o = O.t) : sig
  include
    REBUILDER
      with type k = K.t
       and type ir = IR.t
       and type v = V.t
       and type vc = V.t C.t
       and type o = O.t
       and type tracked = (K.t * V.t) list

  val rebuilder :
    ir ->
    o ->
    v ->
    k * ((o -> k -> v C.t) -> v C.t) ->
    k * ((o -> k -> rebuilt_vc) -> rebuilt_vc)
end = struct
  open CtRebuilderUtils (K) (V) (CT)

  type k = K.t
  type ir = IR.t
  type v = V.t
  type vc = V.t C.t
  type lifted_vc = vc
  type o = O.t
  type tracked = (K.t * V.t) list
  type task = k * ((o -> k -> vc) -> vc)
  type rebuilt_vc = ir -> v * ir * tracked
  type rebuilt_task = k * ((o -> k -> rebuilt_vc) -> rebuilt_vc)
  type t = ir -> o -> v -> task -> rebuilt_task

  module Tracked = BuildSystems.TrackOnStateWriter (K) (V) (O) (C)

  let rebuilder : t =
   fun (ir : ir) (origin : O.t) (v : V.t)
       ((k, _f_compute) as task : Tasks.task) ->
    (* make rebuilder *)
    ( k,
      fun (fetch : o -> k -> rebuilt_vc) : rebuilt_vc ->
        let open BuildConstraints.MonadLetSyntax (C) in
        let* cached_values =
          let tracked_fetch_hash dep_k : v C.t = fetch origin dep_k in
          CT.construct_ct k
            (fun dep_k ->
              C.map
                (fun dep_v -> CT.maybe_cloud_persistent_hash dep_k dep_v)
                (tracked_fetch_hash dep_k))
            ir
        in
        match cached_values with
        | _ when is_elem k v cached_values -> C.pure v
        | cached_value :: _ -> C.pure cached_value
        | [] ->
            let new_value, deps =
              Tracked.track ir (module Tasks) origin task fetch
            in
            let ir_modification =
              CT.record_ct k v
                (List.map
                   (fun (dep_k, dep_v) ->
                     (dep_k, CT.maybe_cloud_persistent_hash dep_k dep_v))
                   deps)
            in
            let* () = C.modify ir_modification in
            C.return new_value )
end

(** Can listen and react to rebuilder events. *)
module type REBUILDER_EVENTS = sig
  type k
  type v
  type uc
  type o

  val on_dependency_discovered : depth:int -> key:k -> depends_upon_key:k -> uc
  (** [on_dependency_discovered ~depth ~key ~depends_upon_key] is the event that
      the build task [key] has discovered a dependency on the build task
      [depends_upon_key].

      The [depth] is how deep in the dependency chain the discovery is. A depth
      of [0] means that [key] is being built directly by the user (or top-level
      build system).

      Returns a unit continuation. *)

  val before_rebuild_key : candidate_values:v list -> origin:o -> k -> uc
  (** [before_rebuild_key ~candidate_values ~origin k] is the event that the key
      [k] is out-of-date and about to be built so it is up-to-date.

      The [candidate_values] are values that were considered for [k] from the
      constructive trace store, but none matched the values available in the
      value store.

      [origin] is the origin of the build task.

      Returns a unit continuation. *)
end

(** Asynchronous constructive trace rebuilder.

    [CT] is the functor paramter for the constructive trace store. Its hash
    function {b must be a "secure hash" like SHA-256} in that two values can be
    considered equal if their (secure) hashes are equal.
    {b Do not use conventional hashes} like OCaml's {!Stdlib.Hashtbl.hash} or a
    MD5 hash which do not imply that hash equality is the same as value
    equality.*)
module CtRebuilderAsync
    (IR : BuildSystems.ABSTRACT_TYPE)
    (K : sig
      type t

      val show : t -> string [@@warning "-unused-value-declaration"]
    end)
    (O : BuildSystems.ORIGIN_WITH_DEPTH with type k = K.t)
    (V : sig
      type t

      val with_origin : t -> O.t -> t
      val show : t -> string [@@warning "-unused-value-declaration"]
    end)
    (P : BuildConstraints.PROMISE_IMPL)
    (C :
      BuildConstraints.MONAD_STATE_WRITER_ASYNC
        with type state = IR.t
         and type 'a promise = 'a P.promise)
    (RE :
      REBUILDER_EVENTS
        with type k = K.t
         and type v = V.t
         and type uc = unit C.t
         and type o = O.t)
    (CT :
      BuildTraces.CONSTRUCTIVE_TRACE_STORE_ASYNC
        with type k = K.t
         and type v = V.t
         and type ir = IR.t
         and module C = C)
    (Tasks :
      BuildSystems.TASKS
        with type k = K.t
         and type vc = V.t C.t
         and type lifted_vc = V.t C.t
         and type o = O.t) : sig
  include
    REBUILDER_ASYNC
      with type k = K.t
       and type ir = IR.t
       and type v = V.t
       and type vc = V.t C.t
       and type o = O.t
       and type state = C.state
       and type tracked = (K.t * (K.t, V.t) CT.key_dependent_hash) list * V.t
       and type 'a promise = 'a P.promise
       and type journal = C.journal
       and type journal_entry_id = C.journal_entry_id
       and type journal_entry_value = C.journal_entry_value

  val rebuilder :
    ir ->
    o ->
    v ->
    k * ((o -> k -> v C.t) -> v C.t) ->
    k * ((o -> k -> rebuilt_vc) -> rebuilt_vc)
end = struct
  open CtRebuilderUtils (K) (V) (CT)

  type BuildConstraints.StateMessage.obj +=
    | PostBuildValueAndDependencyHashes of
        (K.t * (K.t, V.t) CT.key_dependent_hash option) list * V.t

  type k = K.t
  type ir = IR.t
  type v = V.t
  type vc = V.t C.t
  type o = O.t
  type state = C.state
  type tracked = (K.t * (K.t, V.t) CT.key_dependent_hash) list * V.t
  type 'a promise = 'a P.promise
  type journal = C.journal
  type journal_entry_id = C.journal_entry_id
  type journal_entry_value = C.journal_entry_value
  type task = k * ((o -> k -> vc) -> vc)
  type ('k, 'v) key_dependent_hash = (k, v) CT.key_dependent_hash

  type rebuilt_vc =
    (C.state * C.journal ->
    (v
    * BuildConstraints.StateMessage.t list
    * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
    promise)
    promise

  type rebuilt_task = k * ((o -> k -> rebuilt_vc) -> rebuilt_vc)
  type t = ir -> o -> v -> task -> rebuilt_task

  module M = struct
    type ('key, 'value) t = BuildConstraints.StateMessage.t

    let dependency_discovered ~depth ~key ~depends_upon_key =
      RE.on_dependency_discovered ~depth ~key ~depends_upon_key

    let message_of_build_result ~deps ~result =
      let dep_hashes =
        List.map (fun (k, v) -> (k, CT.maybe_cloud_persistent_hash k v)) deps
      in
      BuildConstraints.StateMessage.create rebuilder_async_cls
        (PostBuildValueAndDependencyHashes (dep_hashes, result))
  end

  module Tracked = BuildSystems.TrackOnStateWriterAsync (K) (V) (O) (P) (C) (M)

  let rebuilder : t =
   fun (ir : ir) (o : O.t) (v : V.t) ((k, f_compute) : Tasks.task) ->
    ( k,
      fun (fetch : o -> k -> rebuilt_vc) : rebuilt_vc ->
        let open BuildConstraints.MonadLetSyntax (C) in
        (* Printf.eprintf "rebuilder k=%s child_o=%s before construct_ct\n"
          (K.show k) (O.show child_o); *)
        let* cached_values =
          let tracked_fetch_hash (dep_k : k) : v C.t = fetch o dep_k in
          CT.construct_ct k
            (fun dep_k ->
              C.map
                (fun dep_v -> CT.maybe_cloud_persistent_hash dep_k dep_v)
                (tracked_fetch_hash dep_k))
            ir
        in
        (* Printf.eprintf
          "rebuilder k=%s child_o=%s cached_values(construct_ct)=%d \
           maybe_cloud_persistent_hashes:%s\n"
          (K.show k) (O.show child_o)
          (List.length cached_values)
          (String.concat "|"
             (List.map
                (fun v ->
                  match CT.maybe_cloud_persistent_hash k v with
                  | None -> "none"
                  | Some h -> CT.strong_hash_show h)
                cached_values)); *)
        let ( let*+ ) = P.bind_promise in
        match cached_values with
        | _ when is_elem k v cached_values -> C.pure v
        | cached_value :: _ -> C.pure cached_value
        | [] ->
            let* () =
              (* Printf.eprintf
                "rebuilder before_rebuild_key |1 k=%s | origin=%s\n" (K.show k)
                (O.show o); *)
              RE.before_rebuild_key ~candidate_values:cached_values ~origin:o k
            in
            let*+ msg =
              Tracked.track ir
                (module Tasks)
                o (k, f_compute)
                (fun o' k_dep -> fetch o' k_dep)
            in
            let deps, new_value =
              match
                BuildConstraints.StateMessage.downcast msg rebuilder_async_cls
              with
              | Some (PostBuildValueAndDependencyHashes (dep_hashes, result)) ->
                  (dep_hashes, result)
              | _ ->
                  Printf.ksprintf failwith
                    "Expected PostBuildValueAndDependencyHashes, not %s"
                    (BuildConstraints.StateMessage.show msg)
            in
            (* rewrite value to include the now accurate origin *)
            let new_value = V.with_origin new_value o in
            (* Printf.eprintf "rebuilder k=%s after track new_value=%s\n"
              (K.show k) (V.show new_value); *)
            (* Printf.eprintf "post_record_ct k=%s\n" (K.show k); *)
            let* () = CT.post_record_ct k new_value deps ir in
            C.return new_value )
end