[m-rev.] for review: avoid instances containing cluases in the io module

Julien Fischer jfischer at opturion.com
Sun Feb 11 02:09:27 AEDT 2024


For review by anyone.

---------------------

Avoid instances containing clauses in the io module.

The current implementation of intermodule optimization does not allow typeclass
instance definitions containing clauses to occur in .opt files. In particular,
this affects a number of the stream class instances in the io module. This diff
switches over to use 'pred(...) is predname' style of method definition
throughout the io module.

library/io.m:
     Avoid instances that contains clauses; introduce additional private
     predicates where necessary to make this possible.

Julien.

diff --git a/library/io.m b/library/io.m
index f921115..6db0fdb 100644
--- a/library/io.m
+++ b/library/io.m
@@ -2597,11 +2597,25 @@ seek_binary_input(binary_input_stream(Stream), Whence, Offset, !IO) :-
      seek_binary_2(Stream, Flag, int64.from_int(Offset), Error, !IO),
      throw_on_error(Error, "error seeking in file: ", !IO).

+:- pred stream_seek_binary_input(binary_input_stream::in, stream.whence::in,
+    int::in, io::di, io::uo) is det.
+
+stream_seek_binary_input(Stream, Whence0, OffSet, !IO) :-
+    Whence = stream_whence_to_io_whence(Whence0),
+    seek_binary_input(Stream, Whence, OffSet, !IO).
+
  seek_binary_input64(binary_input_stream(Stream), Whence, Offset, !IO) :-
      whence_to_int(Whence, Flag),
      seek_binary_2(Stream, Flag, Offset, Error, !IO),
      throw_on_error(Error, "error seeking in file: ", !IO).

+:- pred stream_seek_binary_input64(binary_input_stream::in, stream.whence::in,
+    int64::in, io::di, io::uo) is det.
+
+stream_seek_binary_input64(Stream, Whence0, OffSet, !IO) :-
+    Whence = stream_whence_to_io_whence(Whence0),
+    seek_binary_input64(Stream, Whence, OffSet, !IO).
+
  %---------------------%

  seek_binary_output(binary_output_stream(Stream), Whence, Offset, !IO) :-
@@ -2609,11 +2623,25 @@ seek_binary_output(binary_output_stream(Stream), Whence, Offset, !IO) :-
      seek_binary_2(Stream, Flag, int64.from_int(Offset), Error, !IO),
      throw_on_error(Error, "error seeking in file: ", !IO).

+:- pred stream_seek_binary_output(binary_output_stream::in, stream.whence::in,
+    int::in, io::di, io::uo) is det.
+
+stream_seek_binary_output(Stream, Whence0, OffSet, !IO) :-
+    Whence = stream_whence_to_io_whence(Whence0),
+    seek_binary_output(Stream, Whence, OffSet, !IO).
+
  seek_binary_output64(binary_output_stream(Stream), Whence, Offset, !IO) :-
      whence_to_int(Whence, Flag),
      seek_binary_2(Stream, Flag, Offset, Error, !IO),
      throw_on_error(Error, "error seeking in file: ", !IO).

+:- pred stream_seek_binary_output64(binary_output_stream::in,
+    stream.whence::in, int64::in, io::di, io::uo) is det.
+
+stream_seek_binary_output64(Stream, Whence0, Offset, !IO) :-
+    Whence = stream_whence_to_io_whence(Whence0),
+    seek_binary_output64(Stream, Whence, Offset, !IO).
+
  %---------------------%

  binary_input_stream_offset(binary_input_stream(Stream), Offset, !IO) :-
@@ -2774,6 +2802,13 @@ read_char(Result, !IO) :-
      io.input_stream(Stream, !IO),
      read_char(Stream, Result, !IO).

+:- pred stream_read_char(text_input_stream::in,
+    stream.result(char, io.error)::out, io::di, io::uo) is det.
+
+stream_read_char(Stream, Result, !IO) :-
+    read_char(Stream, Result0, !IO),
+    Result = io.result1_to_stream_result1(Result0).
+
  :- pragma inline(pred(read_char/4)).          % Inline to allow deforestation.
  read_char(Stream, Result, !IO) :-
      read_char_code(Stream, ResultCode, Error, Char, !IO),
@@ -2784,6 +2819,13 @@ read_char_unboxed(Stream, Result, Char, !IO) :-
      read_char_code(Stream, ResultCode, Error, Char, !IO),
      interpret_result_code0(ResultCode, Error, Result, !IO).

+:- pred stream_read_char_unboxed(text_input_stream::in,
+    stream.result(io.error)::out, char::out, io::di, io::uo) is det.
+
+stream_read_char_unboxed(Stream, Result, Char, !IO) :-
+    read_char_unboxed(Stream, Result0, Char, !IO),
+    Result = io.result0_to_stream_result0(Result0).
+
  %---------------------%

  putback_char(Char, !IO) :-
@@ -2811,6 +2853,13 @@ read_byte(binary_input_stream(Stream), Result, !IO) :-
      read_byte_val(text_input_stream(Stream), ResultCode, Error, Byte, !IO),
      interpret_result_code1(ResultCode, Error, Byte, Result, !IO).

+:- pred stream_read_byte(io.binary_input_stream::in,
+    stream.result(int, io.error)::out, io::di, io::uo) is det.
+
+stream_read_byte(Stream, Result, !IO) :-
+    read_byte(Stream, Result0, !IO),
+    Result = result1_to_stream_result1(Result0).
+
  read_binary_int8(Result, !IO) :-
      binary_input_stream(Stream, !IO),
      read_binary_int8(Stream, Result, !IO).
@@ -2820,11 +2869,25 @@ read_binary_int8(binary_input_stream(Stream), Result, !IO) :-
      Int8 = cast_from_int(Int), % This call cannot throw an exception.
      interpret_result_code1(ResultCode, Error, Int8, Result, !IO).

+:- pred stream_read_binary_int8(binary_input_stream::in,
+    stream.result(int8, io.error)::out, io::di, io::uo) is det.
+
+stream_read_binary_int8(Stream, Result, !IO) :-
+    read_binary_int8(Stream, Result0, !IO),
+    Result = result1_to_stream_result1(Result0).
+
  read_binary_int8_unboxed(binary_input_stream(Stream), Result, Int8, !IO) :-
      read_byte_val(text_input_stream(Stream), ResultCode, Error, Int, !IO),
      Int8 = cast_from_int(Int),
      interpret_result_code0(ResultCode, Error, Result, !IO).

+:- pred stream_read_binary_int8_unboxed(binary_input_stream::in,
+    stream.result(io.error)::out, int8::out, io::di, io::uo) is det.
+
+stream_read_binary_int8_unboxed(Stream, Result, Int8, !IO) :-
+    read_binary_int8_unboxed(Stream, Result0, Int8, !IO),
+    Result = io.result0_to_stream_result0(Result0).
+
  read_binary_uint8(Result, !IO) :-
      binary_input_stream(Stream, !IO),
      read_binary_uint8(Stream, Result, !IO).
@@ -2834,11 +2897,25 @@ read_binary_uint8(binary_input_stream(Stream), Result, !IO) :-
      UInt8 = cast_from_int(Int), % This call cannot throw an exception.
      interpret_result_code1(ResultCode, Error, UInt8, Result, !IO).

+:- pred stream_read_binary_uint8(binary_input_stream::in,
+    stream.result(uint8, io.error)::out, io::di, io::uo) is det.
+
+stream_read_binary_uint8(Stream, Result, !IO) :-
+    read_binary_uint8(Stream, Result0, !IO),
+    Result = result1_to_stream_result1(Result0).
+
  read_binary_uint8_unboxed(binary_input_stream(Stream), Result, UInt8, !IO) :-
      read_byte_val(text_input_stream(Stream), ResultCode, Error, Int, !IO),
      UInt8 = cast_from_int(Int),
      interpret_result_code0(ResultCode, Error, Result, !IO).

+:- pred stream_read_binary_uint8_unboxed(binary_input_stream::in,
+    stream.result(io.error)::out, uint8::out, io::di, io::uo) is det.
+
+stream_read_binary_uint8_unboxed(Stream, Result, UInt8, !IO) :-
+    read_binary_uint8_unboxed(Stream, Result0, UInt8, !IO),
+    Result = io.result0_to_stream_result0(Result0).
+
  %---------------------%

  putback_byte(Int, !IO) :-
@@ -3552,6 +3629,22 @@ read_line_as_string(text_input_stream(Stream), Result, !IO) :-
          Result = error(IOError)
      ).

+:- pred stream_read_line_as_string(text_input_stream::in,
+    stream.result(line, io.error)::out, io::di, io::uo) is det.
+
+stream_read_line_as_string(Stream, Result, !IO) :-
+    read_line_as_string(Stream, Result0, !IO),
+    (
+        Result0 = ok(String),
+        Result = ok(line(String))
+    ;
+        Result0 = eof,
+        Result = eof
+    ;
+        Result0 = error(Error),
+        Result = error(Error)
+    ).
+
  ignore_whitespace(Result, !IO) :-
      io.input_stream(Stream, !IO),
      ignore_whitespace(Stream, Result, !IO).
@@ -4127,6 +4220,19 @@ read_file_as_string(text_input_stream(Stream), Result, !IO) :-
          )
      ).

+:- pred stream_read_file_as_string(text_input_stream::in,
+    stream.result(text_file, io.error)::out, io::di, io::uo) is det.
+
+stream_read_file_as_string(Stream, Result, !IO) :-
+    read_file_as_string(Stream, Result0, !IO),
+    (
+        Result0 = ok(String),
+        Result = ok(text_file(String))
+    ;
+        Result0 = error(_PartialString, Error),
+        Result = error(Error)
+    ).
+
  read_file_as_string_wf(Result, !IO) :-
      io.input_stream(Stream, !IO),
      read_file_as_string_wf(Stream, Result, !IO).
@@ -5559,43 +5665,19 @@ restore_output_stream(_DummyPred, Stream, ok, !IO) :-
  :- instance stream.reader(text_input_stream, char, io, io.error)
      where
  [
-    ( get(Stream, Result, !IO) :-
-        read_char(Stream, Result0, !IO),
-        Result = io.result1_to_stream_result1(Result0)
-    )
+    pred(get/4) is stream_read_char
  ].

  :- instance stream.reader(text_input_stream, line, io, io.error)
      where
  [
-    ( get(Stream, Result, !IO) :-
-        read_line_as_string(Stream, Result0, !IO),
-        (
-            Result0 = ok(String),
-            Result = ok(line(String))
-        ;
-            Result0 = eof,
-            Result = eof
-        ;
-            Result0 = error(Error),
-            Result = error(Error)
-        )
-    )
+    pred(get/4) is stream_read_line_as_string
  ].

  :- instance stream.reader(text_input_stream, text_file, io, io.error)
      where
  [
-    ( get(Stream, Result, !IO) :-
-        read_file_as_string(Stream, Result0, !IO),
-        (
-            Result0 = ok(String),
-            Result = ok(text_file(String))
-        ;
-            Result0 = error(_PartialString, Error),
-            Result = error(Error)
-        )
-    )
+    pred(get/4) is stream_read_file_as_string
  ].

  %---------------------%
@@ -5603,10 +5685,7 @@ restore_output_stream(_DummyPred, Stream, ok, !IO) :-
  :- instance stream.unboxed_reader(text_input_stream, char, io, io.error)
      where
  [
-    ( unboxed_get(Stream, Result, Char, !IO) :-
-        read_char_unboxed(Stream, Result0, Char, !IO),
-        Result = io.result0_to_stream_result0(Result0)
-    )
+    pred(unboxed_get/5) is stream_read_char_unboxed
  ].

  %---------------------%
@@ -5633,14 +5712,8 @@ restore_output_stream(_DummyPred, Stream, ok, !IO) :-
  :- instance stream.seekable(binary_input_stream, io)
      where
  [
-    ( seek(Stream, Whence0, OffSet, !IO) :-
-        Whence = stream_whence_to_io_whence(Whence0),
-        seek_binary_input(Stream, Whence, OffSet, !IO)
-    ),
-    ( seek64(Stream, Whence0, OffSet, !IO) :-
-        Whence = stream_whence_to_io_whence(Whence0),
-        seek_binary_input64(Stream, Whence, OffSet, !IO)
-    )
+    pred(seek/5) is stream_seek_binary_input,
+    pred(seek64/5) is stream_seek_binary_input64
  ].

  %---------------------%
@@ -5648,28 +5721,19 @@ restore_output_stream(_DummyPred, Stream, ok, !IO) :-
  :- instance stream.reader(binary_input_stream, int, io, io.error)
      where
  [
-    ( get(Stream, Result, !IO) :-
-        read_byte(Stream, Result0, !IO),
-        Result = result1_to_stream_result1(Result0)
-    )
+    pred(get/4) is stream_read_byte
  ].

  :- instance stream.reader(binary_input_stream, int8, io, io.error)
      where
  [
-    ( get(Stream, Result, !IO) :-
-        read_binary_int8(Stream, Result0, !IO),
-        Result = result1_to_stream_result1(Result0)
-    )
+    pred(get/4) is stream_read_binary_int8
  ].

  :- instance stream.reader(binary_input_stream, uint8, io, io.error)
      where
  [
-    ( get(Stream, Result, !IO) :-
-        read_binary_uint8(Stream, Result0, !IO),
-        Result = result1_to_stream_result1(Result0)
-    )
+    pred(get/4) is stream_read_binary_uint8
  ].

  %---------------------%
@@ -5677,19 +5741,13 @@ restore_output_stream(_DummyPred, Stream, ok, !IO) :-
  :- instance stream.unboxed_reader(binary_input_stream, int8, io, io.error)
      where
  [
-    ( unboxed_get(Stream, Result, Int8, !IO) :-
-        read_binary_int8_unboxed(Stream, Result0, Int8, !IO),
-        Result = io.result0_to_stream_result0(Result0)
-    )
+    pred(unboxed_get/5) is stream_read_binary_int8_unboxed
  ].

  :- instance stream.unboxed_reader(binary_input_stream, uint8, io, io.error)
      where
  [
-    ( unboxed_get(Stream, Result, UInt8, !IO) :-
-        read_binary_uint8_unboxed(Stream, Result0, UInt8, !IO),
-        Result = io.result0_to_stream_result0(Result0)
-    )
+    pred(unboxed_get/5) is stream_read_binary_uint8_unboxed
  ].

  %---------------------%
@@ -5837,14 +5895,8 @@ restore_output_stream(_DummyPred, Stream, ok, !IO) :-
  :- instance stream.seekable(binary_output_stream, io)
      where
  [
-    ( seek(Stream, Whence0, OffSet, !IO) :-
-        Whence = stream_whence_to_io_whence(Whence0),
-        seek_binary_output(Stream, Whence, OffSet, !IO)
-    ),
-    ( seek64(Stream, Whence0, Offset, !IO) :-
-        Whence = stream_whence_to_io_whence(Whence0),
-        seek_binary_output64(Stream, Whence, Offset, !IO)
-    )
+    pred(seek/5) is stream_seek_binary_output,
+    pred(seek64/5) is stream_seek_binary_output64
  ].

  %---------------------%


More information about the reviews mailing list