Library zoo_parabs.algo__code

Require Import zoo.prelude.
Require Import zoo.language.typeclasses.
Require Import zoo.language.notations.
Require Import zoo.program_logic.for_.
Require Import zoo_parabs.future.
Require Import zoo_parabs.pool.
Require Import zoo_std.int.
Require Import zoo_std.mvar.
Require Import zoo.options.

Definition algoู adjust_chunk : val :=
  ๐—ณ๐˜‚๐—ป "ctx" "beg" "end_" "chunk" โ†’
    ๐—บ๐—ฎ๐˜๐—ฐ๐—ต "chunk" ๐˜„๐—ถ๐˜๐—ต
    | Some "chunk" โ†’
        "chunk"
    | None โ†’
        ๐—น๐—ฒ๐˜ "num_dom" = poolู size "ctx" + 1 ๐—ถ๐—ป
        ๐—น๐—ฒ๐˜ "num_task" = "end_" - "beg" ๐—ถ๐—ป
        ๐—ถ๐—ณ "num_dom" == 1 ๐˜๐—ต๐—ฒ๐—ป (
          "num_task"
        ) ๐—ฒ๐—น๐˜€๐—ฒ (
          intู max 1 ("num_task" ๐—พ๐˜‚๐—ผ๐˜ (8 ร— "num_dom"))
        )
    ๐—ฒ๐—ป๐—ฑ.

Definition algoู for_โ‚ : val :=
  ๐—ฟ๐—ฒ๐—ฐ "for_" "ctx" "beg" "end_" "chunk" "task" โ†’
    ๐—น๐—ฒ๐˜ "num_task" = "end_" - "beg" ๐—ถ๐—ป
    ๐—ถ๐—ณ "num_task" โ‰ค "chunk" ๐˜๐—ต๐—ฒ๐—ป (
      "task" "ctx" "beg" "num_task"
    ) ๐—ฒ๐—น๐˜€๐—ฒ (
      ๐—น๐—ฒ๐˜ "mid" = "beg" + "num_task" ๐—พ๐˜‚๐—ผ๐˜ 2 ๐—ถ๐—ป
      ๐—น๐—ฒ๐˜ "left" =
        futureู async "ctx"
          (๐—ณ๐˜‚๐—ป "ctx" โ†’ "for_" "ctx" "beg" "mid" "chunk" "task")
      ๐—ถ๐—ป
      "for_" "ctx" "mid" "end_" "chunk" "task" โฎ
      futureู wait "ctx" "left"
    ).

Definition algoู for_ : val :=
  ๐—ณ๐˜‚๐—ป "ctx" "beg" "end_" "chunk" "task" โ†’
    ๐—น๐—ฒ๐˜ "chunk" =
      algoู adjust_chunk "ctx" "beg" "end_" "chunk"
    ๐—ถ๐—ป
    algoู for_โ‚ "ctx" "beg" "end_" "chunk" "task".

Definition algoู for_each : val :=
  ๐—ณ๐˜‚๐—ป "ctx" "beg" "end_" "chunk" "task" โ†’
    algoู for_ "ctx" "beg" "end_" "chunk"
      (๐—ณ๐˜‚๐—ป "ctx" "beg" "sz" โ†’
         ๐—ณ๐—ผ๐—ฟ "i" = "beg" ๐˜๐—ผ "beg" + "sz" ๐—ฑ๐—ผ
           "task" "ctx" "i"
         ๐—ฑ๐—ผ๐—ป๐—ฒ).

Definition algoู fold_seq : val :=
  ๐—ฟ๐—ฒ๐—ฐ "fold_seq" "ctx" "beg" "end_" "body" "op" "acc" โ†’
    ๐—ถ๐—ณ "beg" == "end_" ๐˜๐—ต๐—ฒ๐—ป (
      "acc"
    ) ๐—ฒ๐—น๐˜€๐—ฒ (
      ๐—น๐—ฒ๐˜ "v" = "body" "ctx" "beg" ๐—ถ๐—ป
      ๐—น๐—ฒ๐˜ "acc" = "op" "acc" "v" ๐—ถ๐—ป
      ๐—น๐—ฒ๐˜ "beg" = "beg" + 1 ๐—ถ๐—ป
      "fold_seq" "ctx" "beg" "end_" "body" "op" "acc"
    ).

Definition algoู foldโ‚ : val :=
  ๐—ฟ๐—ฒ๐—ฐ "fold" "ctx" "beg" "end_" "chunk" "body" "op" "zero" โ†’
    ๐—น๐—ฒ๐˜ "num_task" = "end_" - "beg" ๐—ถ๐—ป
    ๐—ถ๐—ณ "num_task" โ‰ค "chunk" ๐˜๐—ต๐—ฒ๐—ป (
      algoู fold_seq "ctx" "beg" ("beg" + "num_task") "body" "op" "zero"
    ) ๐—ฒ๐—น๐˜€๐—ฒ (
      ๐—น๐—ฒ๐˜ "mid" = "beg" + "num_task" ๐—พ๐˜‚๐—ผ๐˜ 2 ๐—ถ๐—ป
      ๐—น๐—ฒ๐˜ "left" =
        futureู async "ctx"
          (๐—ณ๐˜‚๐—ป "ctx" โ†’
             "fold" "ctx" "beg" "mid" "chunk" "body" "op" "zero")
      ๐—ถ๐—ป
      ๐—น๐—ฒ๐˜ "right" =
        "fold" "ctx" "mid" "end_" "chunk" "body" "op" "zero"
      ๐—ถ๐—ป
      ๐—น๐—ฒ๐˜ "left" = futureู wait "ctx" "left" ๐—ถ๐—ป
      "op" "left" "right"
    ).

Definition algoู fold : val :=
  ๐—ณ๐˜‚๐—ป "ctx" "beg" "end_" "chunk" "body" "op" "zero" โ†’
    ๐—น๐—ฒ๐˜ "chunk" =
      algoู adjust_chunk "ctx" "beg" "end_" "chunk"
    ๐—ถ๐—ป
    algoู foldโ‚ "ctx" "beg" "end_" "chunk" "body" "op" "zero".

Definition algoู find_seq : val :=
  ๐—ฟ๐—ฒ๐—ฐ "find_seq" "ctx" "beg" "end_" "pred" "found" โ†’
    ๐—ถ๐—ณ
      "beg" != "end_" ๐—ฎ๐—ป๐—ฑ mvarู is_unset "found"
    ๐˜๐—ต๐—ฒ๐—ป (
      ๐—ถ๐—ณ "pred" "ctx" "beg" ๐˜๐—ต๐—ฒ๐—ป (
        mvarู set "found" "beg"
      ) ๐—ฒ๐—น๐˜€๐—ฒ (
        ๐—น๐—ฒ๐˜ "beg" = "beg" + 1 ๐—ถ๐—ป
        "find_seq" "ctx" "beg" "end_" "pred" "found"
      )
    ).

Definition algoู findโ‚ : val :=
  ๐—ฟ๐—ฒ๐—ฐ "find" "ctx" "beg" "end_" "chunk" "pred" "found" โ†’
    ๐—น๐—ฒ๐˜ "num_task" = "end_" - "beg" ๐—ถ๐—ป
    ๐—ถ๐—ณ "num_task" โ‰ค "chunk" ๐˜๐—ต๐—ฒ๐—ป (
      algoู find_seq "ctx" "beg" ("beg" + "num_task") "pred" "found"
    ) ๐—ฒ๐—น๐˜€๐—ฒ ๐—ถ๐—ณ mvarู is_unset "found" ๐˜๐—ต๐—ฒ๐—ป (
      ๐—น๐—ฒ๐˜ "mid" = "beg" + "num_task" ๐—พ๐˜‚๐—ผ๐˜ 2 ๐—ถ๐—ป
      ๐—น๐—ฒ๐˜ "left" =
        futureู async "ctx"
          (๐—ณ๐˜‚๐—ป "ctx" โ†’
             "find" "ctx" "beg" "mid" "chunk" "pred" "found")
      ๐—ถ๐—ป
      "find" "ctx" "mid" "end_" "chunk" "pred" "found" โฎ
      futureู wait "ctx" "left"
    ).

Definition algoู find : val :=
  ๐—ณ๐˜‚๐—ป "ctx" "beg" "end_" "chunk" "pred" โ†’
    ๐—น๐—ฒ๐˜ "chunk" =
      algoู adjust_chunk "ctx" "beg" "end_" "chunk"
    ๐—ถ๐—ป
    ๐—น๐—ฒ๐˜ "found" = mvarู create () ๐—ถ๐—ป
    algoู findโ‚ "ctx" "beg" "end_" "chunk" "pred" "found" โฎ
    mvarู try_get "found".