├── .gitignore ├── Control └── JS │ └── Async.idr ├── Inigo.dhall ├── Inigo.ipkg ├── LICENSE ├── Makefile ├── support-src └── async.ts ├── support └── async.js ├── test.idr └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | # idris 2 | build 3 | -------------------------------------------------------------------------------- /Control/JS/Async.idr: -------------------------------------------------------------------------------- 1 | module Control.JS.Async 2 | 3 | export 4 | data Async : Type -> Type where 5 | Pure : a -> Async a 6 | LiftIO : PrimIO a -> Async a 7 | Asyn : ((a -> PrimIO ()) -> PrimIO ()) -> Async a 8 | Par : Async (a -> b) -> Async a -> Async b 9 | Bind : Async a -> (a -> Async b) -> Async b 10 | 11 | export 12 | Functor Async where 13 | map f (Pure x) = Pure $ f x 14 | map f (LiftIO g) = LiftIO $ \w => let MkIORes x w' = g w in MkIORes (f x) w' 15 | map f (Asyn g) = Asyn $ \k, w => g (\x, w' => k (f x) w') w 16 | map f (Par mf mx) = Par (map (f .) mf) mx 17 | map f (Bind mx g) = Bind mx $ \x => map f $ g x 18 | 19 | export 20 | Applicative Async where 21 | pure = Pure 22 | mf <*> mx = Bind mf $ \f => map f mx 23 | 24 | export 25 | [Parallel] Applicative Async where 26 | pure = Pure 27 | (<*>) = Par 28 | 29 | export 30 | Monad Async where 31 | (>>=) = Bind 32 | 33 | export 34 | HasIO Async where 35 | liftIO act = LiftIO (toPrim act) 36 | 37 | export %inline 38 | parallel : Traversable t => t (Async a) -> Async (t a) 39 | parallel = sequence @{Parallel} 40 | 41 | export %inline 42 | parallelMap : Traversable t => (a -> Async b) -> t a -> Async (t b) 43 | parallelMap = traverse @{%search} @{Parallel} 44 | 45 | export %inline 46 | prim__async : ((a -> PrimIO ()) -> PrimIO ()) -> Async a 47 | prim__async = Asyn 48 | 49 | export %inline 50 | async : ((a -> IO ()) -> IO ()) -> Async a 51 | async f = prim__async (\k, w => toPrim (f (\x => fromPrim (k x))) w) 52 | 53 | data Promise : Type -> Type where [external] 54 | 55 | %foreign "javascript:support:pure,async" 56 | promise__pure : {0 a : _} -> a -> PrimIO (Promise a) 57 | 58 | %foreign "javascript:support:liftIO,async" 59 | promise__liftIO : {0 a : _} -> PrimIO a -> PrimIO (Promise a) 60 | 61 | %foreign "javascript:support:async,async" 62 | promise__async : {0 a : _} -> ((a -> PrimIO ()) -> PrimIO ()) -> PrimIO (Promise a) 63 | 64 | %foreign "javascript:support:par_app,async" 65 | promise__par_app : {0 a, b : _} -> Promise (a -> b) -> Promise a -> PrimIO (Promise b) 66 | 67 | %foreign "javascript:support:bind,async" 68 | promise__bind :{0 a, b : _} -> Promise a -> (a -> PrimIO (Promise b)) -> PrimIO (Promise b) 69 | 70 | export 71 | toPromise : Async a -> IO (Promise a) 72 | toPromise (Pure x) = fromPrim $ promise__pure x 73 | toPromise (LiftIO f) = fromPrim $ promise__liftIO f 74 | toPromise (Asyn f) = fromPrim $ promise__async f 75 | toPromise (Par x y) = fromPrim $ promise__par_app !(toPromise x) !(toPromise y) 76 | toPromise (Bind x f) = fromPrim $ promise__bind !(toPromise x) (\x => toPrim $ toPromise (f x)) 77 | 78 | export 79 | launch : Async a -> IO () 80 | launch = ignore . toPromise 81 | -------------------------------------------------------------------------------- /Inigo.dhall: -------------------------------------------------------------------------------- 1 | { ns = "Control" 2 | , package = "AsyncJS" 3 | , version = "0.0.1" 4 | , sourcedir = "." 5 | , description = None Text 6 | , executable = Some "AsyncJS" 7 | , modules = ["Control.JS.Async"] : List Text 8 | , readme = None Text 9 | , license = None Text 10 | , link = None Text 11 | , main = None Text 12 | , depends = [] : List Text 13 | , deps = [] : List Text 14 | , devDeps = [] : List Text 15 | , localDeps = [] : List Text 16 | , gitDeps = [] : List { url : Text, commit : Text, subDirs : List Text } 17 | } 18 | -------------------------------------------------------------------------------- /Inigo.ipkg: -------------------------------------------------------------------------------- 1 | package AsyncJS 2 | 3 | modules = Control.JS.Async 4 | 5 | 6 | sourcedir = "." 7 | builddir = "build" 8 | 9 | version = 0.0.1 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2021 Zoe Stafford 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 5 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 | 7 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | 9 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | IDRIS2_LIBDIR = $(shell idris2 --libdir) 3 | 4 | .PHONY: install-support 5 | install-support: 6 | install support/async.js $(IDRIS2_LIBDIR)/support/js/async.js 7 | 8 | .PHONY: install 9 | install: 10 | idris2 --install Inigo.ipkg 11 | 12 | .PHONY: install-with-src 13 | install-with-src: 14 | idris2 --install Inigo.ipkg 15 | -------------------------------------------------------------------------------- /support-src/async.ts: -------------------------------------------------------------------------------- 1 | interface world {} 2 | type unit = void; 3 | 4 | // promise__pure : {0 a : _} -> a -> PrimIO (Promise a) 5 | function async_pure(_ty: undefined, x: T, _w: world): Promise { 6 | return new Promise((res, _rej) => res(x)); 7 | } 8 | 9 | // promise__liftIO : {0 a : _} -> PrimIO a -> PrimIO (Promise a) 10 | function async_liftIO( 11 | _ty: undefined, 12 | act: (w: world) => T, 13 | w: world 14 | ): Promise { 15 | return new Promise((res, _rej) => res(act(w))); 16 | } 17 | 18 | // promise__async : {0 a : _} -> ((a -> PrimIO ()) -> PrimIO ()) -> PrimIO (Promise a) 19 | function async_async( 20 | _ty: undefined, 21 | f: (k: (x: T) => (w: world) => unit) => (w: world) => unit, 22 | w: world 23 | ): Promise { 24 | return new Promise((res, _rej) => f((x) => (_w) => res(x))(w)); 25 | } 26 | 27 | // promise__par_app : {0 a, b : _} -> Promise (a -> b) -> Promise a -> PrimIO (Promise b) 28 | function async_par_app( 29 | _ty1: undefined, 30 | _ty2: undefined, 31 | fun: Promise<(x: A) => B>, 32 | arg: Promise 33 | ): Promise { 34 | return Promise.all([fun, arg]).then(([fun, arg]) => fun(arg)); 35 | } 36 | 37 | // promise__bind :{0 a, b : _} -> Promise a -> (a -> PrimIO (Promise b)) -> PrimIO (Promise b) 38 | function async_bind( 39 | _ty1: undefined, 40 | _ty2: undefined, 41 | x: Promise, 42 | f: (x: A) => (w: world) => Promise, 43 | w: world 44 | ): Promise { 45 | return x.then((x) => f(x)(w)); 46 | } 47 | -------------------------------------------------------------------------------- /support/async.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | function async_pure(_ty, x, _w) { 3 | return new Promise(function (res, _rej) { return res(x); }); 4 | } 5 | function async_liftIO(_ty, act, w) { 6 | return new Promise(function (res, _rej) { return res(act(w)); }); 7 | } 8 | function async_async(_ty, f, w) { 9 | return new Promise(function (res, _rej) { return f(function (x) { return function (_w) { return res(x); }; })(w); }); 10 | } 11 | function async_par_app(_ty1, _ty2, fun, arg) { 12 | return Promise.all([fun, arg]).then(function (_a) { 13 | var fun = _a[0], arg = _a[1]; 14 | return fun(arg); 15 | }); 16 | } 17 | function async_bind(_ty1, _ty2, x, f, w) { 18 | return x.then(function (x) { return f(x)(w); }); 19 | } 20 | -------------------------------------------------------------------------------- /test.idr: -------------------------------------------------------------------------------- 1 | import Control.JS.Async 2 | 3 | %foreign "javascript:lambda:(act, time, w) => setTimeout(() => act(w), time)" 4 | prim__setTimeout : PrimIO () -> Int -> PrimIO () 5 | 6 | wait : Int -> Async () 7 | wait time = prim__async $ \k => prim__setTimeout (k ()) time 8 | 9 | main : IO () 10 | main = launch $ parallel 11 | [ wait 3000 *> putStrLn "world" 12 | , wait 1000 *> putStrLn "hello" 13 | ] 14 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": ["support-src/*"], 3 | "compilerOptions": { 4 | "lib": ["es2015"], 5 | "outFile": "support/async.js", 6 | "noImplicitAny": true, 7 | "removeComments": true, 8 | "strict": true 9 | } 10 | } 11 | --------------------------------------------------------------------------------