interactionTrees.tri (1281B)
1 !import base as B 2 !import io as IO 3 !import io_text as Text 4 !export main 5 !export asyncDemo 6 7 -- Explicit IO sequencing, scoped children and consumed join tickets. Sleep 8 -- permits other work, but promises neither exact wakeup time nor task ordering. 9 -- Only the parent writes stdout: concurrent writers need explicit coordination, 10 -- not an assumption that yield/sleep makes a multi-write message atomic. 11 line text = IO.require IO.stdout (writer : Text.writeText writer (B.append text "\n")) 12 waitFor [_ ticket] = IO.require (IO.join ticket) (outcome : case outcome of 13 ["completed" value] = IO.pure value 14 ["cancelled" reason _] = IO.cancelled reason 15 ["failed" problem cleanup] = IO.fail [problem cleanup] 16 _ = IO.fail "Invalid task outcome") 17 waitFor _ = IO.fail "Invalid spawn reply" 18 worker milliseconds message = IO.require (IO.sleepMilliseconds milliseconds) (_ : IO.pure message) 19 asyncDemo = IO.scope (_ : do IO.require 20 first <- IO.spawn 0 1 [] (_ : worker 2000 "2000ms done sleeping!") 21 second <- IO.spawn 0 1 [] (_ : worker 5000 "5000ms done sleeping!") 22 line "Parent first!" 23 IO.bind (waitFor first) (message : IO.require (line message) (_ : 24 IO.bind (waitFor second) (message : IO.require (line message) (_ : IO.pure "child2000 done"))))) 25 main = IO.io asyncDemo