mirage / functoria

A DSL to invoke otherworldly functors
ISC License
63 stars 21 forks source link

code generation: fewer bindings, do not evaluate configurables multiple times, run initialization once #166

Closed hannesm closed 5 years ago

hannesm commented 5 years ago

When I look into a (mirage-)generated main.ml, I find the following code:

let mirage1 = lazy (
  let __noop1 = Lazy.force noop1 in
  let __noop1 = Lazy.force noop1 in
  let __key1 = Lazy.force key1 in
  let __mirage_logs1 = Lazy.force mirage_logs1 in
  let __f11 = Lazy.force f11 in
  __noop1 >>= fun _noop1 ->
  __noop1 >>= fun _noop1 ->
  __key1 >>= fun _key1 ->
  __mirage_logs1 >>= fun _mirage_logs1 ->
  __f11 >>= fun _f11 ->
  Lwt.return_unit
  )

let () =
  let t =
  Lazy.force noop1 >>= fun _ ->
    Lazy.force noop1 >>= fun _ ->
    Lazy.force key1 >>= fun _ ->
    Lazy.force mirage_logs1 >>= fun _ ->
    Lazy.force mirage1
  in run t

Which IMHO has two issues fixed in separate commits in this PR:

This patch generates instead the following code:

[@@@ocaml.warning "-27"]

let mirage1 = lazy (
  Lazy.force noop1 >>= fun noop1' ->
  Lazy.force key1 >>= fun key1' ->
  Lazy.force mirage_logs1 >>= fun mirage_logs1' ->
  Lazy.force f11 >>= fun f11' ->
  Lwt.return_unit
  )

let () =
  run (Lazy.force mirage1)

Please let me know what you think (esp. @Drup, but obviously other input welcome) /cc @linse

Drup commented 5 years ago

Nothing is "evaluated twice". >>= doesn't evaluate its left hand side, it simply waits for it. If if was already evaluated, then nothing further happens.

As for the rest, your patch is incorrect, in that it doesn't execute the dependencies concurrently. Please read this thread: https://github.com/mirage/functoria/pull/104