tricu

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

87fa3ab396fb5177d0fcd0ab5375e94d3cd11876e2edf398e2f74a08d5f767e1.tri (2260B)


      1 !import base as B
      2 !import io_v2 as IO
      3 !import io_v2_storage as Storage
      4 !import io_v2_text as Text
      5 !export *
      6 
      7 -- Compiler storage policy over IO v2, imported explicitly as compiler_io.
      8 -- Explicit application policy: resident source/metadata bytes and encoded trees
      9 -- are limited to 64 MiB. This is not a heap, expanded-tree or CPU limit.
     10 byteLimit = 67108864
     11 version = 2
     12 io = IO.io
     13 pure = IO.pure
     14 bind = IO.bind
     15 abortIO = IO.fail
     16 thenIO first second = bind first (_ : second)
     17 mapIO action f = bind action (value : pure (f value))
     18 void action = bind action (_ : pure t)
     19 onOk action next = bind action (reply : B.matchResult (_ _ : pure reply) next reply)
     20 onOk_ action next = IO.onOk action next
     21 onResult action failed succeeded = bind action (reply : B.matchResult failed succeeded reply)
     22 onResult_ action failed succeeded = onResult action (problem _ : failed problem) (value _ : succeeded value)
     23 
     24 readFile path = Storage.readBytes path byteLimit
     25 sha256Hex bytes = Storage.hashBytes byteLimit bytes
     26 readSourceFile path = IO.scope (owner : do IO.onOk
     27   reader <- IO.openReader owner path
     28   Text.readSource reader byteLimit)
     29 encodeSourceText text = Text.encodeText byteLimit text
     30 -- Compiler callers create directories in parent-first order. A missing ancestor
     31 -- is an error, not implicit recursive creation. Sync the parent entry before use.
     32 createDirectory path = do IO.onOk
     33   IO.ensureDirectory path
     34   IO.syncDirectory (Storage.parentPath path)
     35 fileExists path = bind (IO.statPath path) (reply : case reply of
     36   ((t t), (["file" _], t)) = pure (B.ok (t t) t)
     37   ((t t), (_, t)) = pure (B.ok t t)
     38   (t, (["not-found" _ _ _], t)) = pure (B.ok t t)
     39   _ = pure reply)
     40 writeBytesIfAbsent path bytes = Storage.publishBytes (Storage.parentPath path) path 0 byteLimit bytes
     41 atomicWriteBytes path bytes = Storage.publishBytes (Storage.parentPath path) path 1 byteLimit bytes
     42 hashIndexedTree tree = do IO.onOk
     43   info <- IO.hashIndexedTree byteLimit tree
     44   case info of
     45     [_ digest] = pure (B.ok digest t)
     46     _ = IO.fail "Invalid indexed hash reply"
     47 writeIndexedTreeIfAbsent path digest tree =
     48   Storage.publishTree (Storage.parentPath path) path byteLimit digest tree
     49 readIndexedTree path digest =
     50   Storage.readTree (Storage.parentPath path) path byteLimit digest