Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

bool.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature bool
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------

# bool -- Standard Fuzion type 'bool'
#
# We need to apologize to George Boole for crippling his name a bit,
# just to safe us from typing one more letter.  But at least we stop
# here and do not use boo, bo or similar.
#
# bool is a choice type that can either be true_ of false_.
#
# Note that 'true_' and 'false_' themselves are not of type 'bool'.
# Routines 'true' and 'false' are the preferred way to get a constant
# value of type 'bool'.
#
#
public bool : choice false_ true_, property.equatable, property.hashable, java_primitive is

  # not
  public prefix ! bool =>
    if bool.this
      false
    else
      true


  # or with lazy evaluation
  #
  public infix || (other Lazy bool) bool =>
    if bool.this
      true
    else
      other


  # and with lazy evaluation
  #
  public infix && (other Lazy bool) bool =>
    if bool.this
      other
    else
      false


  # or with eager evaluation
  #
  public infix |   (other bool) bool =>
    if bool.this
      true
    else
      other


  # and with eager evaluation
  #
  public infix &   (other bool) bool =>
    if bool.this
      other
    else
      false


  # equality check implementation for inherited property.equatable
  #
  public fixed redef type.equality(a, b bool) bool =>
    if a
      b
    else
      !b


  # create hash code for this instance
  #
  # This should satisfy the following condition:
  #
  #   (T.equality a b) : (T.hash_code a = T.hash_code b)
  #
  public fixed redef type.hash_code(a bool) u64 =>
    if a
      0
    else
      1


  # xor, either or
  #
  # true if exatly one of the two
  # bools is true
  #
  public infix ^   (other bool) bool =>
    if bool.this
      !other
    else
      other


  # implies
  #
  public infix : (other Lazy bool) bool =>
    if bool.this
      other
    else
      true


  # ternary ? : -- NYI: This will be replaced by a more powerful match syntax
  #
  public ternary ? : (T type, a, b Lazy T) T =>
    if bool.this
      a
    else
      b


  # create an option from a lazily evaluated value depending on this bool
  #
  public as_option(# value that will be evaluated and wrapped in an option
                   # iff bool.this holds.
                   v Lazy T) option T
  =>
    if bool.this
      v
    else
      nil


  # as human readable string
  #
  public redef as_string String =>
    if bool.this
      "true"
    else
      "false"


  # monoid of bool with infix & operation.  Will be true iff all elements are
  # true.
  #
  public type.all Monoid bool =>
    _ : Monoid bool is
      public redef infix ? (a, b bool) bool => a & b
      public redef e bool => true


  # monoid of bool with infix | operation.  Will be false iff all elements are
  # false.
  #
  public type.any Monoid bool =>
    _ : Monoid bool is
      public redef infix ? (a, b bool) bool => a | b
      public redef e bool => false


  # monoid of bool with infix ^ operation.  Will be true iff an odd number of
  # elements is true. This gives the even parity.
  #
  public type.parity Monoid bool =>
    _ : Monoid bool is
      public redef infix ? (a, b bool) bool => a ^ b
      public redef e bool => false


# boolean value "false"
#
# Note that this value is of unit type >>false_<<, not of type >>bool<<, i.e.,
# if it is used for type inference as in
#
#     my_boolean := false_
#
# you will get a variable of type >>false_<<, it will not be possible to assign
# >>true_<< to it.  You can use >>false<< as an alternative to get type >>bool<<.
#
#
module:public false_ is


# boolean value "true"
#
# Note that this value is of unit type >>true_<<, not of type >>bool<<, i.e.,
# if it is used for type inference as in
#
#     my_boolean := true_
#
# you will get a variable of type >>true_<<, it will not be possible to assign
# >>false_<< to it.  You can use >>true<< as an alternative to get type >>bool<<.
#
#
module:public true_ is


# boolean value "false" as a constant of type >>bool<<.
#
#
public false bool => false_


# boolean value "true" as a constant of type >>bool<<.
#
#
public true bool => true_


# takes an arbitrary amount of bool arguments and returns true if and only if
# all of them are true
#
public all_of (B type..., values B...) bool =>
  values.typed_foldf true T,e,v->
    if T : bool
      v & e
    else
      compile_time_panic


# takes an arbitrary amount of bool arguments and returns true if and only if
# any of them is true
#
public any_of (B type..., values B...) bool =>
  values.typed_foldf false T,e,v->
    if T : bool
      v | e
    else
      compile_time_panic

last changed: 2026-03-13