On this page:
Boolean
False
True
||
||
||
any
&&
&&
&&
all
!
!
!
8.12

6.23 Booleans🔗ℹ

annotation

Boolean

Matches #true or #false.

annotation

False

 

annotation

True

Matches only #false and values other than #false, respectively.

> #false is_a False

#true

> #false is_a True

#false

> 42 is_a True

#true

expression

expr || expr

Produces the value of the first expr if it is non-#false, otherwise produces the value(s) of the second expr.

The second expr is evaluated in tail position with respect to the || form.

binding operator

left_bind || right_bind

Matches if either left_bind or right_bind matches. No identifiers are bound after a successful match, however. In other words, left_bind and right_bind are used only in matching mode, and implied conversions might be skipped.

> fun check_shape(v):

    match v

    | [x] || [x, y, z]: #true

    | ~else: #false

> check_shape([1])

#true

> check_shape([1, 2, 3])

#true

> check_shape([1, 2])

#false

Creates an annotation that accepts a value satisfying either left_annot or right_annot. The static information implied by the annotation is the intersection of information for left_annot and right_annot.

The annotations are checked in other. Either or both of left_annot and right_annot can be a converter annotation, in which case the conversion result of the first satisified annotation is used.

> 1 is_a (String || Int)

#true

> 1 is_a (Boolean || Int)

#true

reducer

any

A reducer that stops an iteration as soon as a non-#false value is produced for an element and returns that value, otherwise returns #false.

> for any (i: 0..10):

    i == 5 && to_string(i)

"5"

> for any (i: 0..10):

    i == 10

#false

expression

expr && expr

Produces #false if the the value of the first expr is #false, otherwise produces the value(s) of the second expr.

The second expr is evaluated in tail position with respect to the && form.

binding operator

left_bind && right_bind

Matches when both left_bind and right_bind match. All identifiers from bindings are available after the match, and static information from left_bind is propagated to right_bind (but not the other way around).

See where for a different kind of “and” binding that allows the right-hand side to refer to bindings from the left-hand side.

> class Posn(x, y)

> fun three_xs(v):

    match v

    | [_, _, _] && [Posn(x, _), ...]: [x, ...]

    | ~else: #false

> three_xs([Posn(1, 2), Posn(3, 4), Posn(5, 6)])

[1, 3, 5]

> three_xs([Posn(1, 2), Posn(3, 4)])

#false

> three_xs([Posn(1, 2), Posn(3, 4), "no"])

#false

Creates an annotation that accepts a value satisfying both left_annot and right_annot.

When left_annot and right_annot are predicate annotations, the static information implied by the annotation is the union of information for left_annot and right_annot, where information from right_annot takes precedence in cases where both supply values for the same static-information key.

If left_annot or right_annot is a converter annotation, the left_annot conversion is applied first, and its result is the input to right_annot, and the result of right_annot is the result for the for the overall annotation created by &&. When the overall annotation is used only for matching, the conversion part of right_annot is skipped, but the conversion part of left_annot must be performed.

> 1 is_a (String && Int)

#false

> Pair(1, "hello") is_a (Pair.of(Int, Any) && Pair.of(Any, String))

#true

> 1 :: (converting(fun (n): n+1) && converting(fun (n): -n))

-2

reducer

all

A reducer that stops an iteration as soon as a #false value is produced for an element and otherwise returns the result of the last iteration.

> for all (i: 0..10):

    i == 5

#false

> for all (i: 0..10):

    i < 10 && to_string(i)

"9"

operator

operator (! (v :: Any)) :: Boolean

Returns #true if v is #false, #false otherwise.

> !#false

#true

> !#true

#false

> !"false"

#false

binding operator

! bind

Matches if bind does not match. Because bind does not match, no identifiers are bound.

> fun

  | is_two_list(![x, y]): #false

  | is_two_list(_): #true

> is_two_list([1])

#false

> is_two_list([1, 2])

#true

> is_two_list([1, 2, 3])

#false

annotation

! annot

Creates an annotation that accepts a value not satisfying annot. Because annot is not satisfied, no conversion is performed.

> [1, 2, 3] is_a !List

#false

> PairList[1, 2, 3] is_a !List

#true