You can use a stream of chars for the content of a given file. Or use a stream of blocks of chars for the content of a given file, where each block is of size, say, 4096 bytes. For the start, using a stream of chars is a good choice. Please stay away from the urge to write "efficient" code at this stage.
By using streams, you can readily do stream-processing in parallel: http://ats-lang.sourceforge.net/EXAMPLE/EFFECTIVATS/StreamPar/. This is something that you would not easily get when coding in C-style. Of course, one could implement a parallel version of 'tail' but very few people would give it a try due to the high programming cost. Economically speaking, the very goal of ATS is to greatly reduce programming cost and increase programming productivity. My hope is to use ATS3 to give concrete demonstration convincingly. On Tue, Mar 12, 2019 at 6:19 PM Shimin Guo <smguo2...@gmail.com> wrote: > > > On Tue, Mar 12, 2019 at 12:23 PM Hongwei Xi <gmh...@gmail.com> wrote: > >> Here is a quick fix: >> extern >> praxi >> clear{a:t0ype}(x: INV(a)): void >> >> fun open_close(): void = >> let >> val fptr = fopen("hello.dats", file_mode_r) >> val p = ptrcast(fptr) >> in >> if p = 0 >> then >> let >> prval () = FILEptr_free_null(fptr) >> in >> end >> else >> let >> val (pf|rc) = fclose(fptr) >> val () = assertloc(rc = 0) // fclose is expected to succeed >> prval None_v() = pf >> prval () = clear(fptr) >> in >> end >> end >> >> ###### >> >> The above shows a C-style of dealing with IO in ATS. What I like to >> recommend >> is to deal with IO via the use of linear streams: >> >> https://github.com/ats-lang/ATS-CodeBook/tree/master/RECIPE/ReadFromSTDIN >> > I'm trying to implement tail in ATS as a practice. It seems the streams > interface may not be a good fit in this case. Is there a better way to do > it than the C-style way? (read the last N bytes into a buffer, count the > number of new lines from the end) > >> >> >> On Tue, Mar 12, 2019 at 2:17 PM Shimin Guo <smguo2...@gmail.com> wrote: >> >>> I wrote the following code >>> >>> #include "share/atspre_define.hats" >>> #include "share/atspre_staload.hats" >>> >>> staload "libats/libc/SATS/stdio.sats" >>> >>> fun open_close(): void = >>> let >>> val fptr = fopen("hello.dats", file_mode_r) >>> val p = ptrcast(fptr) >>> in >>> if p = 0 >>> then >>> let >>> prval () = FILEptr_free_null(fptr) >>> in >>> end >>> else >>> let >>> val (pf|rc) = fclose(fptr) >>> val () = assertloc(rc = 0) // fclose is expected to succeed >>> prval None_v() = pf >>> in >>> end >>> end >>> >>> >>> >>> and got the following compiler error: >>> >>> >>> /home/sguo/ats/file.dats: 242(line=13, offs=7) -- 307(line=16, offs=10): >>> error(3): the dynamic variable [fptr$4718(-1)] is consumed but it should be >>> retained with the type [S2Eapp(S2Ecst(ptr_addr_type); >>> S2Evar(l$8617$8618(14269)))] instead. >>> patsopt(TRANS3): there are [1] errors in total. >>> exit(ATS): uncaught exception: >>> _2home_2hwxi_2Research_2ATS_2dPostiats_2src_2pats_error_2esats__FatalErrorExn(1025) >>> >>> >>> >>> Why does it say fptr should be retained? >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "ats-lang-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to ats-lang-users+unsubscr...@googlegroups.com. >>> To post to this group, send email to ats-lang-users@googlegroups.com. >>> Visit this group at https://groups.google.com/group/ats-lang-users. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/ats-lang-users/8246dd5f-fb2f-497e-add4-5c808768ed50%40googlegroups.com >>> <https://groups.google.com/d/msgid/ats-lang-users/8246dd5f-fb2f-497e-add4-5c808768ed50%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "ats-lang-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to ats-lang-users+unsubscr...@googlegroups.com. >> To post to this group, send email to ats-lang-users@googlegroups.com. >> Visit this group at https://groups.google.com/group/ats-lang-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLrozrnEK_FiFB5ZyFU%2BEYU%3DZ0nYjZOvbcB5%2BsKcseeSsg%40mail.gmail.com >> <https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLrozrnEK_FiFB5ZyFU%2BEYU%3DZ0nYjZOvbcB5%2BsKcseeSsg%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- > You received this message because you are subscribed to the Google Groups > "ats-lang-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to ats-lang-users+unsubscr...@googlegroups.com. > To post to this group, send email to ats-lang-users@googlegroups.com. > Visit this group at https://groups.google.com/group/ats-lang-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ats-lang-users/CAA6Z%2B7BbLK9Fvg0gNN%2B6oCEJMY_E6D610AG8Pf8vG5QZSaekwA%40mail.gmail.com > <https://groups.google.com/d/msgid/ats-lang-users/CAA6Z%2B7BbLK9Fvg0gNN%2B6oCEJMY_E6D610AG8Pf8vG5QZSaekwA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "ats-lang-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-users+unsubscr...@googlegroups.com. To post to this group, send email to ats-lang-users@googlegroups.com. Visit this group at https://groups.google.com/group/ats-lang-users. To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLqd1eRrUa4LJmno-uz-9B-pqgo%3DZr6NBAqDiv_HvJRWHw%40mail.gmail.com.