alms-0.4.9: examples/ex25-io.alms
(* Typestate file IO *)
open IO
abstype in_channel' = InChannel of handle with
let open_in (s: string) = InChannel (openFile s ReadMode)
let input_char (InChannel h: in_channel') = hGetChar h
let input_line (InChannel h: in_channel') = hGetLine h
let eof_in (InChannel h: in_channel') = hIsEOF h
let close_in (InChannel h: in_channel') = hClose h
end
abstype out_channel = OutChannel of handle with
let open_out (s: string) = OutChannel (openFile s WriteMode)
let output_char (OutChannel h: out_channel) = hPutChar h
let output_string (OutChannel h: out_channel) = hPutStr h
let eof_out (OutChannel h: out_channel) = hIsEOF h
let close_out (OutChannel h: out_channel) = hClose h
end
abstype in_channel qualifier A = InChannel of in_channel' with
let a_open_in (s: string) = InChannel (open_in s)
let a_input_char (InChannel rep as ic: in_channel) =
(input_char rep, ic)
let a_input_line (InChannel rep as ic: in_channel) =
(input_line rep, ic)
let a_close_in (InChannel rep: in_channel) =
close_in rep
let a_eof_in (InChannel rep as ic: in_channel) =
if eof_in rep
then close_in rep; None[in_channel]
else Some ic
end
let cat (filename: string) =
let rec loop (ic: in_channel): unit =
match a_eof_in ic with
| None -> ()
| Some ic -> let (c, ic) = a_input_char ic in
putChar c; loop ic
in loop (a_open_in filename)