-
-
Save forked-from-1kasper/082962db6b6c920034020789086db912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import system.io | |
instance : monad_except io.error io := | |
{ throw := λ α s, monad_io.fail io_core io.error α s, | |
catch := λ α x f, monad_io.catch io.error io.error α x f } | |
def process_error : io.error → io unit | |
| (io.error.other s) := io.put_str_ln s | |
| (io.error.sys n) := io.put_str_ln $ sformat! "System error #{n}" | |
def print_file : io unit := | |
catch (io.put_str "> " >> io.get_line >>= | |
io.fs.read_file >>= | |
io.put_str_ln ∘ buffer.to_string) process_error | |
def main : io unit := io.forever print_file | |
/- | |
$ cat test.txt | |
cat: test.txt: No such file or directory | |
$ lean --run io-monad.lean | |
> test.txt | |
failed to open file 'test.txt' | |
> | |
-/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment