Home / Learn / 3 Text and conditions
3.3 Conditions: Bool
Page 4 of 10
3.3.1 What a condition is
A condition is a question whose answer is yes or no. Is the order over 100.00? Is the address in the country? Does the SKU start with LAMP-? Una's type for such answers is Bool, and it has exactly two values: true for yes, and false for no.
Think of a checklist at the packing station: each line is a question, and each gets a tick or a cross, nothing in between. A program asks such questions all the time, and decides what to do from the answers: which price to print, which note to add, whether a parcel ships for free.
You have seen Bool values already: == gave them in section 3.1, and starts_with in section 3.2. This section shows how to write conditions, how to join them, and the rules the checker holds them to.
3.3.2 Why a condition has its own type
In some languages a number can stand for yes or no: 0 means no, and any other number means yes. Then a slip, such as writing the amount where the rule belongs, still runs, and quietly answers the wrong question. In Una a condition must be a Bool. Here the rule for free shipping forgets its comparison:
--- Whether an order ships for free: 10000 cents of goods or more, to a domestic address.
pub ships_free(cents: Int, domestic: Bool) -> Bool = cents and domestic[U2001] Error: an operand of `and` must be `Bool`, found `Int`
╭─[ src/shipping.una:4:54 ]
│
4 │ pub ships_free(cents: Int, domestic: Bool) -> Bool = cents and domestic
│ ──┬──
│ ╰──── an operand of `and` must be `Bool`, found `Int`and joins two conditions, as this section shows, and cents is an Int, an amount. The rule meant cents >= 10_000, an amount of at least 10000 cents, and the checker says so before the program runs.
3.3.3 How to write conditions
This module tries each form:
module stock
--- Whether a shelf holds any units.
pub in_stock(units: Int) -> Bool = units > 0
--- Prints what comparisons and the operators on `Bool` give.
pub main() -> Unit
uses @console: Console[print]
=
let units = 3
@console.print("{units == 3} {units != 3} {units < 3} {units <= 3} {units > 3} {units >= 3}")
@console.print("{true and true} {true and false} {false and false}")
@console.print("{true or false} {false or false}")
@console.print("{not true} {not false}")
@console.print("{in_stock(units)} {in_stock(0)} {not in_stock(0)}")
let few = units < 5
@console.print("few left: {few}")
@console.print("{units > 1 and units < 5} {units < 1 or units > 5}")true false false true false true
true false false
true false
false true
true false true
few left: true
true false3.3.3.1 true and false
The two values of Bool are written true and false, in small letters. In an interpolation they print as those words.
3.3.3.2 Comparisons
A comparison compares two values and gives a Bool:
| Operator | True when |
|---|---|
== |
the two values are equal |
!= |
they are not equal |
< |
the left one is smaller |
<= |
the left one is smaller, or equal |
> |
the left one is larger |
>= |
the left one is larger, or equal |
With units equal to 3, the first line of output gives each comparison with 3 in turn: equal, not unequal, not smaller, smaller or equal, not larger, larger or equal.
3.3.3.3 and, or and not
Three operators join conditions into larger ones:
a and bistruewhen bothaandbaretrue;a or bistruewhen at least one of them istrue;not aistruewhenaisfalse, andfalsewhenaistrue.
units > 1 and units < 5 asks whether units lies between the two: 3 does, so it is true. units < 1 or units > 5 asks whether it lies outside them, and gives false. Una has one more such operator, implies, used mostly in the contracts of chapter 5.
3.3.3.4 A function that answers a question
in_stock(units: Int) -> Bool = units > 0 is a function whose result is a Bool. A call of it, in_stock(0), is a condition like any other, and not in_stock(0) asks the opposite question. Naming a condition this way, after the question it answers, is how a program keeps a rule in one place.
3.3.3.5 Naming a condition with let
let few = units < 5 names a condition, as let names any value (section 2.1). few is a Bool, and you can use it wherever a condition goes.
3.3.3.6 The order of operations
When a condition mixes operators, Una works it out in this order: arithmetic first (*, /, mod, then + and -), then comparisons, then not, then and, then or. So not units > 5 means not (units > 5), and a or b and c means a or (b and c). Parentheses make any order explicit, and a mixed rule is easier to read with them, even where the order would give the same answer.
3.3.4 Comparing texts
== and != work on two values of any one type, texts included. < and the other orderings work on Int, on the fixed-width types, and on Text. Texts are ordered character by character, by each character's Unicode number, which is not quite the order of a dictionary:
--- Prints how texts compare and order.
pub main() -> Unit
uses @console: Console[print]
=
let lamp = "LAMP-01"
let rug = "RUG-07"
@console.print("{lamp < rug} {rug < lamp}")
let small = "lamp-01"
@console.print("{lamp == small} {lamp < small}")
let ten = "10"
let nine = "9"
@console.print("{ten < nine} {10 < 9}")
let short = "LAMP"
let empty = ""
@console.print("{short < lamp} {empty < short}")true false
false true
true false
true true"LAMP-01" < "RUG-07"istrue: the first characters differ, andLcomes beforeR."LAMP-01" < "lamp-01"istrue: the capital lettersAtoZcome before the small lettersatoz."10" < "9"istrue, though 10 is larger than 9: as texts,1comes before9, and the rest does not matter. Compare amounts asInt, never as texts.- A text that is the start of another comes first, so
"LAMP" < "LAMP-01", and the empty text comes before every other text.
3.3.5 Store rules that give a Bool
The rules of a store are full of conditions, so they make good functions. The module shipping holds three:
module shipping
--- Whether an order ships for free: 10000 cents of goods or more, to a domestic address.
pub ships_free(cents: Int, domestic: Bool) -> Bool = cents >= 10_000 and domestic
--- Whether the carrier must get a signature: an order over 50000 cents, or a gift over 20000.
pub needs_signature(cents: Int, gift: Bool) -> Bool = cents > 50_000 or (gift and cents > 20_000)
--- Whether a typed SKU looks like one of the store's: a dash, and four characters at least.
pub looks_like_sku(typed: Text) -> Bool = typed.trim.len >= 4 and typed.contains_text(part: "-")
--- Prints the shipping rules for a few orders, and checks two typed SKUs.
pub main() -> Unit
uses @console: Console[print]
=
let home = ships_free(12_000, domestic: true)
let abroad = ships_free(12_000, domestic: false)
@console.print("12000 cents: free at home {home}, free abroad {abroad}")
let gift = needs_signature(30_000, gift: true)
let plain = needs_signature(30_000, gift: false)
@console.print("30000 cents: a signature as a gift {gift}, otherwise {plain}")
let pays = not ships_free(2_500, domestic: true)
@console.print("2500 cents pays for shipping: {pays}")
let typed = " lamp-01"
let word = "lamp"
@console.print("{looks_like_sku(typed)} {looks_like_sku(word)}")12000 cents: free at home true, free abroad false
30000 cents: a signature as a gift true, otherwise false
2500 cents pays for shipping: true
true falsecents >= 10_000 and domestic. Both parts must hold.domesticis already aBool, so it needs no comparison: writedomestic, notdomestic == true.cents > 50_000 or (gift and cents > 20_000).andcomes beforeoranyway, but the parentheses make the rule readable at a glance.typed.trim.len >= 4 and typed.contains_text(part: "-"). A condition on a text, built from the functions of section 3.2.not ships_free(2_500, domestic: true).notturns the answer of a call around: a small order pays for shipping.
Each rule lives in one function, with a name that says which question it answers. When the threshold for free shipping changes, one line changes.
3.3.6 How conditions behave
3.3.6.1 Comparisons do not chain
In school, 1 <= percent <= 90 means that percent lies between 1 and 90. In Una it does not check:
--- Whether a discount is one the store runs: from 1 to 90 percent off.
pub fair_discount(percent: Int) -> Bool = 1 <= percent <= 90[U0101] Error: comparisons cannot be chained; join them with `and`, as in `a < b and b < c`The message gives the repair: two comparisons joined with and, percent >= 1 and percent <= 90, which no reader can misread.
3.3.6.2 Only values of one type compare
== needs two values of the same type. Comparing an Int with a Text, such as units == code, is U2001, "== needs operands of one type", never a quiet false. The orderings work on numbers and texts only: true < false is U2001 too, since a Bool has no order.
3.3.6.3 The right side runs only when it is needed
a and b works out a first. When a is false, the answer is false whatever b is, so b is not worked out at all. In the same way, a or b skips b when a is true. Una relies on this: on the right side of and, the checker knows that the left side holds. Here a division by units is safe, because it only runs when units > 0:
module packs
--- Whether each unit of a pack costs less than 500 cents. An empty pack has no unit, and is
--- not cheap.
pub cheap_each(cents: Int, units: Int) -> Bool = units > 0 and cents / units < 500
--- Prints the answer for three packs.
pub main() -> Unit
uses @console: Console[print]
=
@console.print("{cheap_each(900, units: 6)} {cheap_each(900, units: 1)} {cheap_each(900, units: 0)}")true false falseFor the empty pack, units > 0 is false, so the division never runs, and the answer is false. Swap the two sides, and the division comes first, with nothing to say that units is not zero:
[U5001] Error: division obligation `units != 0` is refuted
╭─[ src/packs.una:5:50 ]
│
5 │ pub cheap_each(cents: Int, units: Int) -> Bool = cents / units < 500 and units > 0
│ ──────┬──────
│ ╰──────── division obligation `units != 0` is refuted
│ Note 2: counterexample: units = 0This is the division obligation of section 2.2: the checker must prove that the divisor is not zero, and the counterexample is units = 0. Order the parts of a condition so that each one is safe given the ones before it.
3.3.7 When to use a Bool
Which to use:
| You want to… | Use |
|---|---|
| Answer a yes-or-no question | a Bool, true or false |
| Name a rule of the store | a function that gives a Bool, named after the question: ships_free |
| Require two things at once | and |
| Accept either of two things | or |
| Ask the opposite question | not |
| Test a range | two comparisons joined with and, never a chain |
| Guard a step that needs the first part to hold | and, with the guarding part on the left |
3.3.8 Check yourself: conditions
units is 3 and gift is true:
--- Prints three conditions on the same two values.
pub main() -> Unit
uses @console: Console[print]
=
let units = 3
let gift = true
@console.print("{not units > 5}")
@console.print("{not gift or units > 5}")
@console.print("{units < 2 or units < 5 and gift}")What do the three lines print?
Show the answer
true
false
truenot comes after comparisons, so not units > 5 is not (units > 5): 3 > 5 is false, and not false is true. In the second line not applies to gift alone: not gift is false, units > 5 is false, and false or false is false. In the third, and comes before or: units < 5 and gift is true, so the whole is true, whatever units < 2 gives.