tricu

A lazy functional language based on Tree Calculus
Log | Files | Refs | README | LICENSE

streams.tri (1242B)


      1 !import base as B
      2 !import io as IO
      3 !import handlers as H
      4 !export main
      5 !export handled
      6 !export child
      7 !export diagnostic
      8 !export closeDenied
      9 !export transferDenied
     10 !export broken
     11 
     12 -- Run stored exports with --io --no-result: stdout belongs to the pipeline.
     13 stream = do IO.require
     14   input <- IO.stdin
     15   output <- IO.stdout
     16   IO.require (IO.copyStream input output) (_ : IO.pure t)
     17 main = IO.io stream
     18 handled = IO.io <| H.runIO (H.liftIO stream)
     19 -- Root admission closes on return, but the attached child may first borrow
     20 -- inherited streams afterwards. Parent completion waits for the entire copy.
     21 child = IO.io <| IO.require (IO.spawn 0 0 [] (_ : stream)) (_ : IO.pure t)
     22 diagnostic = IO.io <| IO.require IO.stderr
     23   (output : IO.require (IO.writeAll output [0 255 13 10]) (_ : IO.pure t))
     24 closeDenied = IO.io <| IO.require IO.stdout IO.close
     25 transferDenied = IO.io <| IO.require IO.stdout
     26   (output : IO.spawn 0 0 [output] (_ : IO.fail "must not start"))
     27 broken = IO.io <| IO.require IO.stdout (output : IO.bind (IO.writeAll output [97])
     28   (reply : case reply of
     29     (t, (_, t)) = IO.require IO.stderr
     30       (diagnostic : IO.require (IO.writeAll diagnostic [102 97 105 108 101 100]) (_ : IO.pure t))
     31     _ = IO.fail "Expected broken pipe"))