Booleans and Comparisons

Truth values, comparisons, and explicit conversions


Gregory S. DeLozier, PhD

Breaking Changes

New keywords: true, false, boolean, type

Rename earlier variables that use those exact names.

true_value, boolean_value, and typewriter remain identifiers.

Computing an Answer

__input = "21";
answer = number(input()) * 2;
large_enough = answer >= 40;
print(large_enough);
true

A New Literal Node

ready = true;
{"tag": "boolean", "value": True}

Vertex spelling: true. Python value: True.

Comparing Values

Expression Result
42 == 42.0 true
"dog" != "cat" true
-1.5 < .5 true
2 <= 2 true
2 > 3 false
3 >= 3 true

Ordering Strings

Expression Result
"ant" < "bee" true
"ant" < "anteater" true
"ant" <= "ant" true
"Z" < "a" true
"10" < "2" true

Compare characters, not numeric meanings.

Types Still Matter

Expression Result
true == 1 false
"1" == 1 false
"1" < 2 Type error
true + 1 Type error
number(true) + 2 3

Explicit Conversions

Expression Value
number(true) 1
string(false) "false"
boolean(-2) true
boolean(0) false
boolean(" FALSE ") false

Text Needs an Interpretation

boolean("false")          false
boolean("1")              value error
boolean(number("1"))      true

No nonempty-string truthiness.

The Type of a Value

Expression Result
type(42) "number"
type(.5) "number"
type("42") "string"
type(1 < 2) "boolean"

type(input()) still reads input.

The Comparison Tree

1 + 2 * 3 < 8

The Comparison Tree

One Comparison at a Time

expression ::= comparison
comparison ::= arithmetic_expression
               [ compare_op arithmetic_expression ]
Expression Result
1 < 2 < 3 Syntax error
(1 < 2) == true true
(1 < 2) < 3 Type error

Checking the Implementation

bash run-tests.sh

All six comparisons. Exact result types. Conversion boundaries.

Input arguments run once; comparison operands run left to right.

Practice

true == 1
number(true) == 1
boolean("0")
boolean(number("0"))

Which succeed, and what does each produce?