comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Seeking for papers about tagged types vs access to subprograms
Date: Sun, 12 May 2013 11:10:01 +0300
Date: 2013-05-12T11:10:01+03:00	[thread overview]
Message-ID: <av912pFfve6U1@mid.individual.net> (raw)
In-Reply-To: <op.wwykzzi9ule2fv@cardamome>

On 13-05-12 09:44 , Yannick Duchêne (Hibou57) wrote:
> Le Sat, 11 May 2013 23:06:25 +0200, Niklas Holsti
> <niklas.holsti@tidorum.invalid> a écrit:
> 
>>    type File_State is (Is_Closed, Is_Open);
>>
>>    type File_Object (State : File_State := Is_Closed) is
>>    record
>>       case State is
>>       when Is_Closed => null;
>>       when Is_Open   => Handle : System.IO.File_Handle;
>>       end case;
>>    end record;
>>
>>    subtype Closed_File is File_Object (State => Is_Closed);
>>    subtype Open_File   is File_Object (State => Is_Open  );
>>
>> By using subtypes on formal parameters, we can indicate that the
>> available operations on a File_Object depend on the actual subtype
>> (i.e. the state), except for one deficiency, on which more below.
>>
>> First, reading and writing is possible only for open files:
>>
>>    procedure Read  (File : in Open_File; ...)
>>    procedure Write (File : in Open_File; ...)
>>
>> Second, a Closed file can be Opened, and an Open file can be Closed:
>>
>>    procedure Open  (File : in out Closed_File);
>>    procedure Close (File : in out Open_File  );
>>
>> The problem here is that these operations should change the subtype of
>> the "in out" parameter: Open changes the File from Closed_File to
>> Open_File, and Close changes it from Open_File to Closed_File.
> 
> Additionally to the idea you suggested, that may also suggest to use a
> function instead of a procedure with in/out parameter, with a new
> declaration for each of the important state changes which would become
> clearly visible (still what if this already was a function already
> returning something… Ada functions can return only one single element).

You mean like this:

   function Open  (File : in Closed_File) return Open_File;
   function Close (File : in Open_File  ) return Closed_File;

I agree that this is a possible approach. This would work, but at the
cost of writing the parameter/object name twice in each call:

   File : File_Object;
   ...
   File := Open (File);
   File := Close (File);

But this code seems to cause a problem:

   File1, File2 : File_Object;
   ...
   File1 := Open (File1);
   File2 := Close (File1);

Now File2 is a clone of File1, and the underlying system file is closed,
but File1 still considers it open. (This may of course be acceptable to
the specific underlying system and not lead to an error, but in general
I believe that such side-effects could be problematic.)

The "function" approach thus tends to clone objects, and to lose the
"object identity".

If we want to preserve object identity under that approach, I think we
need a "linear type system"
(http://en.wikipedia.org/wiki/Linear_type_system) in which each variable
can be used exactly once. However, every use (e.g. as a parameter to a
function) can produce a new incarnation of the object (the function
return value), which can be assigned to a new variable, so the object
lives on. However, this would be a BIG change to the language.

> There is now the question about compilers: are compilers clever enough
> to statically catch subtype miss‑match in such a use cases and give
> warnings (I believe no),

I think that if this issue of subtype matching comes to be considered
important, compiler writers could give it more attention, and
conservative (but incomplete) static-analysis solutions could be
implemented with current technology.

> and are compilers clever enough to optimize out
> multiple object declaration into a single one, changing its subtype (I
> believe no, too),

Those problems are solved by a linear type system. But I agree with you
that it is too much to expect that the compiler would automatically
implement an Ada-like program using linear typing behind the scenes.

> Another issue with subtype, is that it requires the `File_Object` full
> definition to be public, or else, there is no way to define a subtype of
> it as the example do (no opaque type).

Yes, that could be a problem. The language might allow the declaration
of subtypes of opaque types, with opaque constraints. That does not seem
too hard to implement if the type and subtypes are fully defined in the
private part of a package, but it seems more difficult if the full type
declaration is deferred (through an access type) to the package body.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .

  reply	other threads:[~2013-05-12  8:10 UTC|newest]

Thread overview: 202+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-28  5:14 Seeking for papers about tagged types vs access to subprograms Yannick Duchêne (Hibou57)
2013-04-30  0:31 ` Yannick Duchêne (Hibou57)
2013-04-30  0:41   ` Shark8
2013-04-30  1:00     ` Yannick Duchêne (Hibou57)
2013-05-02  1:18       ` Randy Brukardt
2013-05-02  2:29         ` Jeffrey Carter
2013-05-06 10:00   ` Yannick Duchêne (Hibou57)
2013-05-06 10:18     ` Dmitry A. Kazakov
2013-05-06 10:55       ` Yannick Duchêne (Hibou57)
2013-05-06 12:11         ` Dmitry A. Kazakov
2013-05-06 13:16           ` Yannick Duchêne (Hibou57)
2013-05-06 14:16             ` Dmitry A. Kazakov
2013-05-06 15:15               ` Yannick Duchêne (Hibou57)
2013-05-06 18:55                 ` Dmitry A. Kazakov
2013-05-06 20:05                   ` Adam Beneschan
2013-05-07  7:30                     ` Dmitry A. Kazakov
2013-05-10  4:33                     ` Yannick Duchêne (Hibou57)
2013-05-07 20:35                   ` Jacob Sparre Andersen news
2013-05-07 20:44                     ` Yannick Duchêne (Hibou57)
2013-05-07 22:32                     ` Dennis Lee Bieber
2013-05-07 23:10                       ` Adam Beneschan
2013-05-08  0:18                     ` Shark8
2013-05-10  4:34                       ` Yannick Duchêne (Hibou57)
2013-05-10  8:27                         ` Simon Wright
2013-05-10 18:08                         ` Niklas Holsti
2013-05-08  7:38                     ` Dmitry A. Kazakov
2013-05-08  7:59                       ` Yannick Duchêne (Hibou57)
2013-05-08  8:23                         ` Dmitry A. Kazakov
2013-05-08  9:39                           ` Yannick Duchêne (Hibou57)
2013-05-08  9:51                             ` Yannick Duchêne (Hibou57)
2013-05-08 10:23                             ` Dmitry A. Kazakov
2013-05-08 11:08                               ` Yannick Duchêne (Hibou57)
2013-05-08 15:29                                 ` Dmitry A. Kazakov
2013-05-08 16:13                                   ` Yannick Duchêne (Hibou57)
2013-05-08 18:17                                     ` Dmitry A. Kazakov
2013-05-10  4:35                                       ` Yannick Duchêne (Hibou57)
2013-05-08 20:27                                   ` Randy Brukardt
2013-05-09  7:33                                     ` Dmitry A. Kazakov
2013-05-09 22:19                                       ` Randy Brukardt
2013-05-10  3:29                                         ` Yannick Duchêne (Hibou57)
2013-05-10  4:16                                           ` Yannick Duchêne (Hibou57)
2013-05-10  8:42                                           ` Dmitry A. Kazakov
2013-05-10 11:18                                             ` Yannick Duchêne (Hibou57)
2013-05-10 12:15                                               ` Dmitry A. Kazakov
2013-05-10 12:40                                                 ` Yannick Duchêne (Hibou57)
2013-05-10 12:59                                                   ` Yannick Duchêne (Hibou57)
2013-05-10 13:54                                                   ` Dmitry A. Kazakov
2013-05-10 14:01                                                     ` Yannick Duchêne (Hibou57)
2013-05-10 14:27                                                       ` Dmitry A. Kazakov
2013-05-10 15:20                                                         ` Yannick Duchêne (Hibou57)
2013-05-11  7:22                                                         ` Georg Bauhaus
2013-05-10 18:04                                           ` Niklas Holsti
2013-05-10 19:33                                             ` Yannick Duchêne (Hibou57)
2013-05-11  0:18                                             ` Randy Brukardt
2013-05-11  7:14                                               ` Yannick Duchêne (Hibou57)
2013-05-11 21:06                                               ` Niklas Holsti
2013-05-11 23:19                                                 ` Shark8
2013-05-12  6:09                                                   ` Niklas Holsti
2013-05-14  2:02                                                     ` Randy Brukardt
2013-05-12  6:44                                                 ` Yannick Duchêne (Hibou57)
2013-05-12  8:10                                                   ` Niklas Holsti [this message]
2013-05-12  8:49                                                     ` Yannick Duchêne (Hibou57)
2013-05-12 18:56                                                     ` Jeffrey Carter
2013-05-12 22:15                                                       ` Robert A Duff
2013-05-13  0:26                                                         ` Jeffrey Carter
2013-05-13  7:03                                                         ` Yannick Duchêne (Hibou57)
2013-05-13 13:15                                                           ` Robert A Duff
2013-05-13 17:30                                                             ` Jeffrey Carter
2013-05-13 18:01                                                               ` J-P. Rosen
2013-05-13 18:39                                                                 ` Bill Findlay
2013-05-13 18:57                                                                 ` Jeffrey Carter
2013-05-13 19:13                                                                 ` Robert A Duff
2013-05-13 20:38                                                                   ` J-P. Rosen
2013-05-14  7:26                                                                     ` Dmitry A. Kazakov
2013-05-14 20:00                                                                       ` Robert A Duff
2013-05-15 10:10                                                                         ` Dmitry A. Kazakov
2013-05-14 19:56                                                                     ` Robert A Duff
2013-05-15  4:24                                                                       ` Yannick Duchêne (Hibou57)
2013-05-15  9:28                                                                         ` Dmitry A. Kazakov
2013-05-15 11:31                                                                           ` Peter C. Chapin
2013-05-15 12:32                                                                             ` Yannick Duchêne (Hibou57)
2013-05-15 19:59                                                                               ` Peter C. Chapin
2013-05-15 20:56                                                                                 ` Dmitry A. Kazakov
2013-05-15 12:46                                                                             ` Dmitry A. Kazakov
2013-05-15 18:15                                                                             ` Jeffrey Carter
2013-05-15 19:18                                                                               ` Eryndlia Mavourneen
2013-05-15 19:57                                                                                 ` Dmitry A. Kazakov
2013-05-15 20:37                                                                                   ` Yannick Duchêne (Hibou57)
2013-05-15 20:48                                                                                     ` Dmitry A. Kazakov
2013-05-16 12:45                                                                                       ` Eryndlia Mavourneen
2013-05-16 17:16                                                                                         ` Jeffrey Carter
2013-05-16  9:41                                                                                 ` G.B.
2013-05-16 12:35                                                                                   ` J-P. Rosen
2013-05-15 14:21                                                                       ` J-P. Rosen
2013-05-14  2:14                                                         ` Randy Brukardt
2013-05-14 19:35                                                           ` Robert A Duff
2013-05-15  4:11                                                             ` Yannick Duchêne (Hibou57)
2013-05-16 23:36                                                             ` Randy Brukardt
2013-05-13  5:21                                                       ` Niklas Holsti
2013-05-13  7:22                                                         ` Dmitry A. Kazakov
2013-05-13  8:23                                                           ` Yannick Duchêne (Hibou57)
2013-05-13 19:20                                                           ` Niklas Holsti
2013-05-14  8:14                                                             ` Dmitry A. Kazakov
2013-05-10  3:47                                         ` Yannick Duchêne (Hibou57)
2013-05-11  0:22                                           ` Randy Brukardt
2013-05-11  7:22                                             ` Yannick Duchêne (Hibou57)
2013-05-10  3:59                                         ` Yannick Duchêne (Hibou57)
2013-05-10  4:03                                         ` Yannick Duchêne (Hibou57)
2013-05-10  7:48                                         ` Dmitry A. Kazakov
2013-05-10  8:12                                           ` Yannick Duchêne (Hibou57)
2013-05-10 15:11                                             ` Yannick Duchêne (Hibou57)
2013-05-11  0:42                                           ` Randy Brukardt
2013-05-11  6:37                                             ` Dmitry A. Kazakov
2013-05-11  7:06                                               ` Georg Bauhaus
2013-05-11  7:42                                                 ` Dmitry A. Kazakov
2013-05-11  8:14                                                   ` Yannick Duchêne (Hibou57)
2013-05-14  2:29                                                   ` Randy Brukardt
2013-05-14  7:44                                                     ` Dmitry A. Kazakov
2013-05-14 11:34                                                       ` Yannick Duchêne (Hibou57)
2013-05-14 12:16                                                         ` Dmitry A. Kazakov
2013-05-14 13:13                                                           ` Yannick Duchêne (Hibou57)
2013-05-14 18:41                                                           ` Randy Brukardt
2013-05-15 11:20                                                           ` Peter C. Chapin
2013-05-15 13:00                                                             ` Dmitry A. Kazakov
2013-05-15 21:12                                                               ` Peter C. Chapin
2013-05-15 22:08                                                                 ` Dmitry A. Kazakov
2013-05-16 11:31                                                                   ` Peter C. Chapin
2013-05-16 11:56                                                                     ` Yannick Duchêne (Hibou57)
2013-05-16 12:20                                                                     ` Dmitry A. Kazakov
2013-05-16 13:10                                                                       ` Peter C. Chapin
2013-05-16 13:54                                                                         ` Dmitry A. Kazakov
2013-05-16 17:15                                                                           ` G.B.
2013-05-16 18:09                                                                             ` Peter C. Chapin
2013-05-16 19:16                                                                               ` Dmitry A. Kazakov
2013-05-16 21:59                                                                                 ` Georg Bauhaus
2013-05-17 19:57                                                                                   ` Dmitry A. Kazakov
2013-05-16 21:20                                                                               ` Niklas Holsti
2013-05-16 23:20                                                                                 ` Peter C. Chapin
2013-05-17  5:25                                                                                   ` Niklas Holsti
2013-05-17  7:53                                                                                   ` Georg Bauhaus
2013-05-16 13:09                                                                     ` Eryndlia Mavourneen
2013-05-11  7:58                                               ` Yannick Duchêne (Hibou57)
2013-05-11  9:08                                                 ` Dmitry A. Kazakov
2013-05-11 18:14                                                 ` Niklas Holsti
2013-05-11  8:03                                               ` Yannick Duchêne (Hibou57)
2013-05-11  9:16                                                 ` Dmitry A. Kazakov
2013-05-11 11:49                                                   ` Georg Bauhaus
2013-05-11 12:25                                                     ` Dmitry A. Kazakov
2013-05-11 22:51                                                   ` Robert A Duff
2013-05-12  6:02                                                     ` Dmitry A. Kazakov
2013-05-12  6:25                                                       ` Yannick Duchêne (Hibou57)
2013-05-12  7:14                                                         ` Dmitry A. Kazakov
2013-05-12  7:37                                                           ` Simon Wright
2013-05-12  7:59                                                             ` Dmitry A. Kazakov
2013-05-12  8:21                                                           ` Yannick Duchêne (Hibou57)
2013-05-12  9:25                                                             ` Dmitry A. Kazakov
2013-05-12  9:32                                                               ` Yannick Duchêne (Hibou57)
2013-05-12 10:07                                                                 ` Dmitry A. Kazakov
2013-05-11  7:32                                             ` Yannick Duchêne (Hibou57)
2013-05-11  7:46                                             ` Yannick Duchêne (Hibou57)
2013-05-14 12:46                                         ` Weaker typing as a part of the way to stronger typing? (Was: Seeking for papers about tagged types vs access to subprograms) Jacob Sparre Andersen
2013-05-14 19:08                                           ` Randy Brukardt
2013-05-10 16:02                                       ` Seeking for papers about tagged types vs access to subprograms Yannick Duchêne (Hibou57)
2013-05-08 20:12                       ` Randy Brukardt
2013-05-09  7:50                         ` Dmitry A. Kazakov
2013-05-09 21:43                           ` Randy Brukardt
2013-05-10  4:39                             ` Yannick Duchêne (Hibou57)
2013-05-10  7:49                             ` Dmitry A. Kazakov
2013-05-11  0:09                               ` Randy Brukardt
2013-05-11  6:40                                 ` Dmitry A. Kazakov
2013-05-14  3:01                                   ` Randy Brukardt
2013-05-14  8:32                                     ` Dmitry A. Kazakov
2013-05-14 19:02                                       ` Randy Brukardt
2013-05-15  4:43                                         ` Yannick Duchêne (Hibou57)
2013-05-16 23:27                                           ` Randy Brukardt
2013-05-15  9:14                                       ` G.B.
2013-05-15 12:08                                         ` Dmitry A. Kazakov
2013-05-15 14:43                                           ` G.B.
2013-05-15 15:02                                             ` Dmitry A. Kazakov
2013-05-14 19:21                                     ` Robert A Duff
2013-05-10  4:29                   ` Yannick Duchêne (Hibou57)
2013-05-07  1:14             ` Randy Brukardt
2013-05-07  2:42               ` Yannick Duchêne (Hibou57)
2013-05-07  1:09     ` Randy Brukardt
2013-05-07  7:41       ` Dmitry A. Kazakov
2013-05-07 20:27         ` Jacob Sparre Andersen news
2013-05-07 20:40           ` Yannick Duchêne (Hibou57)
2013-05-08  7:57           ` Dmitry A. Kazakov
2013-05-08 20:37             ` Randy Brukardt
2013-05-09  8:04               ` Dmitry A. Kazakov
2013-05-09 21:33                 ` Randy Brukardt
2013-05-10  7:15                   ` Dmitry A. Kazakov
2013-05-11  1:00                     ` Randy Brukardt
2013-05-11  7:08                       ` Yannick Duchêne (Hibou57)
2013-05-11  7:12                       ` Dmitry A. Kazakov
2013-05-14  2:52                         ` Randy Brukardt
2013-05-11  5:31                     ` Simon Wright
2013-05-11  7:22                       ` Dmitry A. Kazakov
2013-05-02  1:09 ` Randy Brukardt
2013-05-02  6:56   ` Dmitry A. Kazakov
2013-05-02 21:49     ` Randy Brukardt
2013-05-03  6:49       ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox