Beginner's question: open and close file

57 views
Skip to first unread message

Shimin Guo

unread,
Mar 12, 2019, 2:17:46 PM3/12/19
to ats-lang-users
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?

Hongwei Xi

unread,
Mar 12, 2019, 3:23:08 PM3/12/19
to ats-lan...@googlegroups.com
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:


--
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-user...@googlegroups.com.
To post to this group, send email to ats-lan...@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.

Vanessa McHale

unread,
Mar 12, 2019, 4:26:45 PM3/12/19
to ats-lan...@googlegroups.com

FWIW, I personally would use

FILEptr_is_null to check if fptr is null, rather than ptrcast and then a comparison to 0

signature.asc

Shimin Guo

unread,
Mar 12, 2019, 4:29:59 PM3/12/19
to ats-lan...@googlegroups.com
Thanks for the tip! Will look into linear streams. 

Shimin Guo

unread,
Mar 12, 2019, 4:31:31 PM3/12/19
to ats-lan...@googlegroups.com

Shimin Guo

unread,
Mar 12, 2019, 6:19:48 PM3/12/19
to ats-lan...@googlegroups.com
On Tue, Mar 12, 2019 at 12:23 PM Hongwei Xi <gmh...@gmail.com> wrote:
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) 

Hongwei Xi

unread,
Mar 12, 2019, 10:16:47 PM3/12/19
to ats-lan...@googlegroups.com
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:


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.





Reply all
Reply to author
Forward
0 new messages