Pascal-Chapter Option

Chapter 19 : Option

Captain, we should warm up the weapons while we are still in FTL, we don’t know what awaits us on the other side.

The option type is a predefined variant type that is used to express whether there is a value of some type or none. This is especially useful when calling a partial function, that is, a function that is not defined for some inputs. In that case, the value of the option type would be None, otherwise Some (v), where v is some meaningful value of any type.

An example in arithmetic is the division operation:

function div (const a : nat; const b : nat) : option (nat) is
if b = 0n then (None: option (nat)) else Some (a/b)

Map Access and Option evaluation

Use the postfix [] operator to read a value of the map. When accessing to an element of a map (using [] operator), the returned result is the associated value or None if the given key does not exist. This is the reason why the [] operator returns an option .of the expected type (and not just the expected type).

The keyword Some can be used to create an option variable for a given value.

The keyword None can be used to create an option variable with no given value.

const middleName : option(string) = Some(“Foo”);

const middleName : option(string) = None;

Option in Pattern matching

In the previous chapters, you’ve seen how to do pattern matching using the case operator. The keyword Some can be used in a pattern matching to retrieve the value behind the option variable. The keyword None can be used in a pattern matching to verify the option variable has no value.

case <variable> of
| Some(<value_name>) -> <block_code>
| None -> <block_code>

<block_code> can be a single instruction or a block {} <value_name> is a local variable name. <value_name> which holds the option value and can be used inside the <block_code>

Here is an example of [] operator returning an option type :

type expected_type is int
type balance_type is map(nat, expected_type)
const user_balances: balance_type = map[ 1n -> 10 ];

const my_balance : option(expected_type) = user_balances[1n];
case my_balance of
Some (val) -> block { skip }
| None -> failwith (“Unknown user”)

Here is an example of pattern matching resolving an option type directly (useful when we just want to retrieve the value behind the optional) :

const my_balance2 : expected_type = case user_balances[1n] of
Some (val) -> val
| None -> (failwith (“Unknown user”) : expected_type)

Notice the cast of failwith instruction into an expected\type_

Your mission

1- Notice the weapons mapping which maps the name of each weapon to its corresponding input of power. We want to increase the power of the Main Laser but mapping returns optional results as they might not be found in the mapping. Define the constant main_laser_power as an optional int from selecting “Main Laser” from the weapons mapping.

2- Writte a pattern matching for main_laser_power. If it exists, increase the power of the “Main Laser” by 1 (use i as temporary matching variable). If it does not exist in the mapping, fail with “Weapon not found”


Type your solution above and validate your answer