Project

General

Profile

Feature #138

Automatic conversion from bool3 to bool?

Added by John Abbott almost 12 years ago. Updated about 10 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Safety
Start date:
24 Apr 2012
Due date:
% Done:

100%

Estimated time:
2.00 h
Spent time:

Description

The BOOST equivalent of bool3 is called tribool. It offers automatic conversion to and from bool.

I have already established that auto conversion from bool gives us problems (because the C++ compiler thinks that tribool is as good a match as MachineInt for any integral value). This was evident with operator<< for OpenMath streams.

While writing the matrix solve fn I wanted to test quickly whether a ring is a PID, because if so then there is a quick and simple algm to compute the answer. But testing whether a ring is a PID is costly; however, one can do a fast check which returns a bool3 result -- essentially it recognises some easy cases, otherwise it says it doesn't know. Compare the two lines of code below:

Currently we must convert explicitly from bool3 to bool like this:

    if (IsDefinitelyTrue(IsPIDFast(R))) return SolveByHNF(M, rhs);

If the conversion were automatic, the code could be simplified to this:

    if (IsPIDFast(R)) return SolveByHNF(M, rhs);

The difference is not huge. There is a trade-off between "safety" and readability.

In this case if the auto conversion maps DefinitelyTrue to true and the other values to false then we get simpler more readable code.

On the other hand if there is auto conversion then a careless user might write code blissfully unaware that IsPIDFast returns a bool3 -- I'm not sure if this can lead to serious problems.

Opinions?


Related issues

Related to CoCoALib - Feature #152: Replace bool3 by triboolClosed2012-05-04

Related to CoCoALib - Feature #153: Shorter simpler names for some bool3 values and functionsClosed2012-05-04

History

#1 Updated by Anna Maria Bigatti almost 12 years ago

John Abbott wrote:

The BOOST equivalent of bool3 is called tribool. It offers automatic conversion to and from bool.

The difference is not huge. There is a trade-off between "safety" and readability.

On the other hand if there is auto conversion then a careless user might write code blissfully unaware that IsPIDFast returns a bool3 -- I'm not sure if this can lead to serious problems.

Opinions?

I propose a new coding convention for bool3 values and functions.
For example IsPIDFastBool3 or IsPIDBool3.
It is essential to stress that the value is a bool3 (for safety).

#2 Updated by John Abbott almost 12 years ago

  • % Done changed from 0 to 30

I have modified operator<< for OpenMath streams so that they handle explicitly machine integers (rather than MachineInt). This permits us to allow automatic conversion from bool to bool3 without problems [I have verified this by compiling].

#3 Updated by Anna Maria Bigatti about 10 years ago

  • Target version set to CoCoALib-0.99533 Easter14

#4 Updated by John Abbott about 10 years ago

  • Category set to Safety
  • Status changed from New to Feedback
  • % Done changed from 30 to 90

JAA thinks this has been resolved by #153.

Conclusion: no automatic conversion to bool, just the shorter names decided in #153.

#5 Updated by John Abbott about 10 years ago

  • Status changed from Feedback to Closed
  • Assignee set to John Abbott
  • % Done changed from 90 to 100
  • Estimated time set to 2.00 h

Also available in: Atom PDF