file.tri (1529B)
1 !import prelude * 2 !import io * 3 !export * 4 -- File IO/presentation adapter: no SCP parsing, compilation or application wiring. 5 -- Bind a trusted bytes -> Result [line] invocation when publishing this code. 6 -- Runtime input is an explicit path; whole-file bytes are passed unchanged. 7 8 firstLine lines = matchList "<none>" (first _ : first) lines 9 tagResult phase result = (lazyResult 10 (problem unused : err (pair phase problem) t) 11 (value unused : ok value t) result) 12 readAndInvoke invoke path = (onResult_ (readFile path) 13 (problem : pure (err (pair "read" problem) t)) 14 (bytes : pure (tagResult "consumer" (invoke bytes)))) 15 16 -- Printable diagnostics are presentation only; arbitrary failure trees are 17 -- retained in the Result, not guessed to be byte strings. 18 printable_ self value = (triage true (_ : false) 19 (byte rest : lazyBool 20 (_ : lazyBool (_ : self rest) (_ : false) 21 (and? (not? (lt_? byte 32)) (lte_? byte 126))) 22 (_ : false) (isNat? byte)) value) 23 detail value = lazyBool (_ : value) (_ : "<non-text tree; inspect Result>") (y printable_ value) 24 25 program label invoke path = (io (onResult_ (readAndInvoke invoke path) 26 (problem : do bind 27 _ <- putStrLn (append label "-failed") 28 _ <- putStrLn (append "failure-stage=" (fst problem)) 29 _ <- putStrLn (append "failure-detail=" (detail (snd problem))) 30 pure (err problem t)) 31 (lines : do bind 32 _ <- putStrLn (append (append label "=") (showNumber (length lines))) 33 _ <- putStrLn (append "first=" (firstLine lines)) 34 pure (ok lines t))))