Conditional construct for a kleenean data type - Software Engineering Stack Exchange most recent 30 from softwareengineering.stackexchange.com 2019-11-18T02:35:12Z https://softwareengineering.stackexchange.com/feeds/question/199065 https://creativecommons.org/licenses/by-sa/4.0/rdf https://softwareengineering.stackexchange.com/q/199065 2 Conditional construct for a kleenean data type Morwenn https://softwareengineering.stackexchange.com/users/81338 2013-05-22T17:50:55Z 2013-07-22T21:35:25Z <p>I was thinking of an hypothetical programming language with a <code>kleenean</code> data type which would implement Kleene's <a href="http://en.wikipedia.org/wiki/Three-valued_logic#Kleene_logic" rel="nofollow">three-valued logic</a>. To sum up, it's an extension of the boolean data type with the three constants <code>true</code>, <code>false</code> and <code>unknown</code> where <code>unknown</code> means that the value is either <code>true</code> or <code>false</code>, but we don't know which.</p> <p>The truth tables for a kleenean type are well-known and the logic is quite easy to understand. However, I was wondering how one would design a conditional construct to take in account this <code>unknown</code> value.</p> <p>A basic <code>if-then-else</code> conditional construct is almost always as follows:</p> <pre><code>if (boolean condition) then condition is true, do something else condition is false, do some other thing end </code></pre> <p>However, I have troubles seeing what a kleenean <code>if</code> construct would look like. How could we interpret the <code>unknown</code> constant? Technically speaking, it could satisfy the <code>true</code> condition as well as the <code>false</code> condition since it is one of these two. However, we can't have it match any of those since it could be the other, it is not really <code>true</code> nor <code>false</code>.</p> <p>Is there a well-known way to implement such a construct?</p> <p><strong>EDIT:</strong> To specify a little bit, I would prefere something different than the way <code>boost::tribool</code> works, or from a simple <code>switch</code> as if was an enum. Answers about quantum superposition and semantics are welcome.</p> https://softwareengineering.stackexchange.com/questions/199065/-/199072#199072 -1 Answer by Euphoric for Conditional construct for a kleenean data type Euphoric https://softwareengineering.stackexchange.com/users/56655 2013-05-22T18:30:44Z 2013-05-22T18:30:44Z <p>From what you said, <code>unknown</code> represents represents <code>true</code> or <code>false</code> "at the same time". You shouldn't take it as another value, but as something special or exceptional.</p> <p>If <code>unknown</code> is passed into <code>if</code> it becomes non deterministic which of the two code paths it should take. I think it is best to assume that passing <code>unknown</code> into an <code>if</code> should result in error.</p> <p>But you should have way to detect if value is properly defined. Something like </p> <pre><code>if defined (boolean condition) then condition is defined true or false, do something else condition is unknown, do some other thing end </code></pre> <p>and there should be negated version of this, because you cannot use normal logic to invert the code paths to remove one of the branches for code clarity.</p> https://softwareengineering.stackexchange.com/questions/199065/-/199097#199097 2 Answer by Donal Fellows for Conditional construct for a kleenean data type Donal Fellows https://softwareengineering.stackexchange.com/users/9673 2013-05-22T21:59:40Z 2013-05-22T21:59:40Z <blockquote> <p>However, I have troubles seeing what a kleenean <code>if</code> construct would look like. </p> </blockquote> <p>It looks <em>exactly</em> like an ordinary <code>if</code>. However, the semantics are slightly different. Instead of saying “if the condition is true, then this otherwise that”, it actually goes something like “if the condition is <em>known to be</em> true, then this otherwise that” (or perhaps “proven” instead of “known”, depending on which modality you prefer to use your kleenean logic to represent). This then means that you may want to have an operator for testing “is unknown/unproven” so that you can check for the third state, though that is not strictly necessary:</p> <pre><code>let boolvar = boolean condition if (boolvar) then # The true case... elseif (not boolvar) then # The false case... else # The unproven case... fi </code></pre> <p>(Assuming that <code>not unknown</code> is <code>unknown</code>, of course.)</p> https://softwareengineering.stackexchange.com/questions/199065/-/199119#199119 -1 Answer by lorus for Conditional construct for a kleenean data type lorus https://softwareengineering.stackexchange.com/users/53528 2013-05-23T04:41:21Z 2013-05-23T04:41:21Z <p>I've implemented this approach in the language I develop (then I removed this feature and switched to the boolean logic, as considered three-state logic not really useful).</p> <p>A three-state logical could be converted to boolean transparently with the following rule:</p> <blockquote> <p>The boolean value of the logical is <code>true</code> only when the logical value is <code>true</code>.</p> </blockquote> <p>The <code>unknown</code> and <code>false</code> logical values correspond to the <code>false</code> boolean.</p> <p>To be able to work with three-state logicals, a set of unary operators could be used:</p> <ul> <li><code>++</code> (<em>is</em>) - convert a logical operand to boolean value. Just in a more verbose way than implicit conversion.</li> <li><code>--</code> (<em>not</em>) - result is <code>true</code> only when operand is <code>false</code>.</li> <li><code>+-</code> (<em>known</em>) - result is <code>true</code> only when operand is either <code>true</code> or <code>false</code>.</li> <li><code>-+</code> (<em>unknown</em>) - result is <code>true</code> only when operand is <code>unknown</code>.</li> </ul> <p>This way conditionals could be implemented to work with boolean conditions, as they use to. A logical could be used as boolean condition either transparently or explicitly. Different combinations of logical operators could be used to achieve the same goal, whatever suits best:</p> <pre><code>if (condition) { // condition is true } else if (--condition) { // condition is false } else { // condition is unknown } if (-+condition) { // condition is unknown } else if (condition) { // condition is true } else { // condition is false } </code></pre>