/*---------------------------------------------------------------------------
  Copyright 2021-2021, Microsoft Research, Daan Leijen.

    This is free software; you can redistribute it and/or modify it under the
  terms of the Apache License, Version 2.0. A copy of the License can be
  found in the LICENSE file at the root of this distribution.
---------------------------------------------------------------------------*/

/*
Experimental: Parallel tasks.

Note: very experimental and may not work as intended :-)
See ``test/bench/koka/binarytrees.kk`` for example usage.
*/
module std/os/taskstd/os/task

import std/num/int32std/num/int32

// A `:promise<a>` can be `await`ed for a result.
abstract struct promisestd/os/task/promise: V -> V<aa: V>
  promisestd/os/task/promise/promise: forall<a> (promise : promise<a>) -> any : anystd/core/types/any: V

noinline extern unsafe_taskstd/os/task/unsafe_task: forall<a> (work : () -> pure a) -> pure any( workwork: () -> pure $188 : () -> purestd/core/pure: E aa: V ) : purestd/core/pure: E anystd/core/types/any: V
  c "kk_task_schedule"

noinline extern unsafe_awaitstd/os/task/unsafe_await: forall<a> (p : any) -> pure a( pp: any : anystd/core/types/any: V ) : purestd/core/pure: E aa: V
  c "kk_promise_get"

extern prim-task-set-default-concurrencystd/os/task/prim-task-set-default-concurrency: (thread-count : ssize_t) -> io ()( thread-countthread-count: ssize_t : ssize_tstd/core/types/ssize_t: V  ) : iostd/core/io: E (std/core/types/unit: V)std/core/types/unit: V
  c "kk_task_set_default_concurrency"

pub fun task-set-default-concurrencystd/os/task/task-set-default-concurrency: (thread-count : int) -> io ()( thread-countthread-count: int : intstd/core/types/int: V )result: -> io () : iostd/core/io: E (std/core/types/unit: V)std/core/types/unit: V
  prim-task-set-default-concurrencystd/os/task/prim-task-set-default-concurrency: (thread-count : ssize_t) -> <alloc<global>,console,div,exn,fsys,ndet,net,read<global>,ui,write<global>> ()( thread-countthread-count: int.ssize_tstd/core/int/ssize_t: (i : int) -> <alloc<global>,console,div,exn,fsys,ndet,net,read<global>,ui,write<global>> ssize_t )


// Spark a pure computation in a separate thread of control.
pub noinline fun taskstd/os/task/task: forall<a> (work : () -> pure a) -> pure promise<a>( workwork: () -> pure $245 : () -> purestd/core/pure: E aa: V )result: -> pure promise<267> : purestd/core/pure: E promisestd/os/task/promise: V -> V<aa: V>
  Promisestd/os/task/Promise: forall<a> (promise : any) -> promise<a>( unsafe_taskstd/os/task/unsafe_task: (work : () -> pure $245) -> <div,exn> any( workwork: () -> <div,exn> $245 ) )

// Await the result of a promise.
pub fun awaitstd/os/task/await: forall<a> (p : promise<a>) -> pure a( pp: promise<$272> : promisestd/os/task/promise: V -> V<aa: V> )result: -> pure 292 : purestd/core/pure: E aa: V
  unsafe_awaitstd/os/task/unsafe_await: (p : any) -> <div,exn> $272( pp: promise<$272>.promisestd/os/task/promise/promise: (promise : promise<$272>) -> <div,exn> any )

// Await the result of a list of promises.
pub fun list/awaitstd/os/task/list/await: forall<a> (ps : list<promise<a>>) -> pure list<a>( psps: list<promise<$297>> : liststd/core/types/list: V -> V<promisestd/os/task/promise: V -> V<aa: V>> )result: -> pure list<344> : purestd/core/pure: E liststd/core/types/list: V -> V<aa: V>
  psps: list<promise<$297>>.mapstd/core/list/map: (xs : list<promise<$297>>, f : (promise<$297>) -> <div,exn> $297) -> <div,exn> list<$297>(awaitstd/os/task/await: (p : promise<$297>) -> <div,exn> $297)

// Run a list of pure computations in parallel.
pub fun parallelstd/os/task/parallel: forall<a> (xs : list<() -> pure a>) -> pure list<a>( xsxs: list<() -> pure $349> : liststd/core/types/list: V -> V<() -> purestd/core/pure: E aa: V> )result: -> pure list<426> : purestd/core/pure: E liststd/core/types/list: V -> V<aa: V>
  xsxs: list<() -> pure $349>.mapstd/core/list/map: (xs : list<() -> pure $349>, f : (() -> pure $349) -> <div,exn> promise<$349>) -> <div,exn> list<promise<$349>>( taskstd/os/task/task: (work : () -> pure $349) -> <div,exn> promise<$349> ).awaitstd/os/task/list/await: (ps : list<promise<$349>>) -> <div,exn> list<$349>


/*
noinline extern unsafe_task_n( count : ssize_t, stride : ssize_t, work : () -> pure a, combine : (a,a) -> a ) : pure any
  c "kk_task_schedule_n"

pub noinline fun taskn( count : int, stride : int, work : () -> pure a, combine : (a,a) -> total a ) : pure promise<a>
  Promise( unsafe_task_n( count.ssize_t, stride.ssize_t, work, combine ) )

*/

// ---------------------------------------------------------
// LVar's
// Note: currently unsafe in the pure effect!! needs work

abstract struct lvarstd/os/task/lvar: V -> V<aa: V>
  lvstd/os/task/lvar/lv: forall<a> (lvar : lvar<a>) -> any : anystd/core/types/any: V

noinline extern unsafe-lvarstd/os/task/unsafe-lvar: forall<a> (init : a) -> pure any( initinit: $431 : aa: V ) : purestd/core/pure: E anystd/core/types/any: V
  c "kk_lvar_alloc"

noinline extern unsafe-putstd/os/task/unsafe-put: forall<a> (lvar : any, x : a, monotonic-combine : (a, a) -> a) -> pure ()( lvarlvar: any : anystd/core/types/any: V, xx: $450 : aa: V, monotonic-combinemonotonic-combine: ($450, $450) -> $450 : (aa: V,aa: V) -> astd/core/types/total: E ) : purestd/core/pure: E (std/core/types/unit: V)std/core/types/unit: V
  c "kk_lvar_put"

noinline extern unsafe-getstd/os/task/unsafe-get: forall<a> (lvar : any, bot : a, is-gte : (a, a) -> int32) -> pure a( lvarlvar: any : anystd/core/types/any: V, botbot: $473 : aa: V, is-gteis-gte: ($473, $473) -> int32 : (aa: V,aa: V) -> int32std/core/types/int32: V ) : purestd/core/pure: E aa: V
  c "kk_lvar_get"

pub noinline fun lvarstd/os/task/lvar: forall<a> (init : a) -> pure lvar<a>( initinit: $496 : aa: V )result: -> pure lvar<516> : purestd/core/pure: E lvarstd/os/task/lvar: V -> V<aa: V>
  Lvarstd/os/task/Lvar: forall<a> (lv : any) -> lvar<a>( unsafe-lvarstd/os/task/unsafe-lvar: (init : $496) -> <div,exn> any(initinit: $496) )

pub fun putstd/os/task/put: forall<a> (lvar : lvar<a>, x : a, monotonic-combine : (a, a) -> a) -> pure ()( lvarlvar: lvar<$521> : lvarstd/os/task/lvar: V -> V<aa: V>, xx: $521 : aa: V, monotonic-combinemonotonic-combine: ($521, $521) -> $521 : (aa: V,aa: V) -> astd/core/types/total: E )result: -> pure () : purestd/core/pure: E (std/core/types/unit: V)std/core/types/unit: V
  unsafe-putstd/os/task/unsafe-put: (lvar : any, x : $521, monotonic-combine : ($521, $521) -> $521) -> <div,exn> ()( lvarlvar: lvar<$521>.lvstd/os/task/lvar/lv: (lvar : lvar<$521>) -> <div,exn> any, xx: $521, monotonic-combinemonotonic-combine: ($521, $521) -> $521 )

pub fun getstd/os/task/get: forall<a> (lvar : lvar<a>, bot : a, is-gte : (a, a) -> bool) -> pure a( lvarlvar: lvar<$550> : lvarstd/os/task/lvar: V -> V<aa: V>, botbot: $550 : aa: V, is-gteis-gte: ($550, $550) -> bool: (aa: V,aa: V) -> boolstd/core/types/bool: V )result: -> pure 583 : purestd/core/pure: E aa: V
  unsafe-getstd/os/task/unsafe-get: (lvar : any, bot : $550, is-gte : ($550, $550) -> int32) -> <div,exn> $550( lvarlvar: lvar<$550>.lvstd/os/task/lvar/lv: (lvar : lvar<$550>) -> <div,exn> any, botbot: $550, fnfn: (x : $550, y : $550) -> int32(xx: $550,yy: $550){ if (is-gteis-gte: ($550, $550) -> bool(xx: $550,yy: $550)) then onestd/num/int32/one: int32 else zerostd/num/int32/zero: int32 } )