Skip to content

Instantly share code, notes, and snippets.

@forked-from-1kasper
Created July 13, 2019 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forked-from-1kasper/082962db6b6c920034020789086db912 to your computer and use it in GitHub Desktop.
Save forked-from-1kasper/082962db6b6c920034020789086db912 to your computer and use it in GitHub Desktop.
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