staload "libc/SATS/stdio.sats"
staload _ = "prelude/DATS/list_vt.dats"
viewtypedef cstream_vt = stream_vt (char)
fn* longestline_loop1 {n:nat}
(cs: cstream_vt, cur: list_vt (char, n), n: int n): List_vt (char) =
case+ !cs of
| ~stream_vt_cons (c, cs) => begin
if (c <> '\n') then begin
longestline_loop1 (cs, list_vt_cons (c, cur), n+1)
end else begin
longestline_loop2 (cs, list_vt_nil (), 0, cur, n)
end end | ~stream_vt_nil () => cur
and longestline_loop2 {m,n:nat | n <= m} (
cs: cstream_vt
, cur: list_vt (char, n), n: int n
, max: list_vt (char, m), m: int m
) : List_vt (char) = begin case+ !cs of
| ~stream_vt_cons (c, cs) => begin
if (c <> '\n') then let
val cur = list_vt_cons (c, cur)
in
if (n < m) then
longestline_loop2 (cs, cur, n + 1, max, m)
else let val () = list_vt_free (max)
in
longestline_loop1 (cs, cur, n + 1)
end end else let val () = list_vt_free (cur)
in
longestline_loop2 (cs, list_vt_nil (), 0, max, m)
end end | ~stream_vt_nil () => let
val () = list_vt_free (cur) in max
end end
implement main () = let
val (pf_stdin | p_stdin) = stdin_get ()
val cs = char_stream_vt_make_file
(file_mode_lte_r_r, pf_stdin | p_stdin)
val longestline =
longestline_loop1 {0} (cs, list_vt_nil (), 0)
val longestline = list_vt_reverse (longestline)
val () = loop (longestline) where {
fun loop {n:nat} .<n>.
(cs: list_vt (char, n)): void = case+ cs of
| ~list_vt_cons (c, cs) => (print c; loop (cs))
| ~list_vt_nil () => ()
}
val () = print_newline ()
in
end