tricu

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

browser.tri (1982B)


      1 !import io as IO
      2 !import io_text as Text
      3 !import handlers as H
      4 !export direct
      5 !export handled
      6 !export roundTrip
      7 !export cancellation
      8 !export main
      9 !export binary
     10 !export invalidUTF8
     11 !export invalidScalar
     12 !export publicHandled
     13 !export publicRoundTrip
     14 !export publicCancellation
     15 !export publicBinary
     16 !export returnValue
     17 
     18 -- Bare computations exercise the private bridge; explicit envelopes exercise
     19 -- the public RuntimeSession and worker/terminal routing.
     20 direct = do IO.require
     21   writer <- IO.stdout
     22   Text.writeText writer "λ😀é\r\n"
     23   IO.pure 42
     24 handled = H.runIO (H.liftIO direct)
     25 roundTrip = do IO.require
     26   reader <- IO.stdin
     27   writer <- IO.stdout
     28   chars <- Text.readText reader 4096
     29   Text.writeText writer chars
     30   IO.pure 42
     31 cancellation = IO.scope (owner : do IO.require
     32   reader <- IO.stdin
     33   writer <- IO.stdout
     34   spawned <- IO.spawn 1 1 [] (_ : do IO.bind
     35     IO.readSome reader 8
     36     IO.fail "abandoned continuation resumed")
     37   case spawned of
     38     [task ticket] = do IO.require
     39       Text.writeText writer "ready"
     40       IO.cancel task "stop reader"
     41       outcome <- IO.join ticket
     42       case outcome of
     43         ["cancelled" _ _] = IO.pure 42
     44         _ = IO.fail "wrong child outcome"
     45     _ = IO.fail "invalid spawn result")
     46 main = IO.io direct
     47 
     48 invalidUTF8 = IO.bind (IO.decodeUTF8 0 [237 160]) (reply : case reply of
     49   (t, (["invalid-data" "utf8/decode" "invalid UTF-8 sequence at byte 0" "none"], t)) = IO.pure 42
     50   _ = IO.fail "invalid UTF8 prefix was accepted or misclassified")
     51 invalidScalar = IO.bind (IO.encodeUTF8 [55296]) (reply : case reply of
     52   (t, (["invalid-argument" "utf8/encode" "" "none"], t)) = IO.pure 42
     53   _ = IO.fail "surrogate was accepted or misclassified")
     54 
     55 binary = do IO.require
     56   writer <- IO.stdout
     57   IO.writeAll writer [92 120 102 102 255 240 159]
     58   IO.pure 42
     59 
     60 publicHandled = IO.io handled
     61 publicRoundTrip = IO.io roundTrip
     62 publicCancellation = IO.io cancellation
     63 publicBinary = IO.io binary
     64 returnValue value = IO.io (IO.pure value)