bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: range types, ptrdiff_t, signed integers for internals


From: Bruno Haible
Subject: Re: range types, ptrdiff_t, signed integers for internals
Date: Thu, 12 Dec 2019 09:50:20 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-166-generic; KDE/5.18.0; x86_64; ; )

Hi Paul,

> >      It's quite possible that clang will have range types, 5 years from now.
> 
> I've been wanting, off and on, to get range types into the C world since 
> the 1980s. It hasn't happened yet.

Several programming languages have range types:
  - Ada [1]
  - Haskell [2]
  - Common Lisp [3]

Note that range types are also related to the 'for' loop of the programming
language. In particular, there is the question what the value of the iteration
variable is, after the loop has terminated. [4]

GCC and clang support the notion on an expression so far, not on a type. [5]

> "u" connotes "unsigned"; but "nonnegative but signed" is what is wanted 
> here, and it is not the same thing.

I would distinguish the range type [0 .. PTRDIFF_MAX] (which is an abstract
concept) from the actual implementation (through the typedef). It could
be represented by a signed type or an unsigned type; either is fine.

> Some of the ptrdiff_t values in dfa.c can be negative.

That's exactly why it is useful to distinguish the variables that are
nonnegative from those that can attain negative values.

> Some can be -1 or nonnegative. Will we need more types for these other
> possibilities?

For the purposes mentioned in the other mail (understanding the code, etc.)
I would say that it is good enough to use the signed type (ptrdiff_t) when
the range includes negative values. When C compilers actually support ranges,
in the future, we can then optionally start using more precise range types
like [-1 .. PTRDIFF_MAX].

> Also, where would we put these new typedefs? dfa.h doesn't seem like the 
> right place.

That's up to you. It could well be a separate .h file that is a module of
its own (like verify.h and intprops.h are).

Bruno

[1] https://en.wikibooks.org/wiki/Ada_Programming/Types/range
[2] 
https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1340006.3.7
[3] 
http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/syscla_integer.html
[4] https://www.cliki.net/Issue%20LOOP-FINALLY-VARIABLES
[5] 
https://stackoverflow.com/questions/40447195/can-i-hint-the-optimizer-by-giving-the-range-of-an-integer




reply via email to

[Prev in Thread] Current Thread [Next in Thread]