hs-bindgen-runtime
Safe HaskellSafe-Inferred
LanguageGHC2021

HsBindgen.Runtime.CBool

Description

C boolean semantics

In C, boolean types are numeric, where value 0 is considered false and any other value is considered true. They are implemented differently across different C projects:

  • Since C23, there is a bool type and predefined constants true and false.
  • Since C99, standard header stdbool.h defines (implementation-dependent) type _Bool and macros true and false. This header is deprecated since C23.
  • Before C99, users defined boolean types and values, following the common conventions. Some projects still define their own boolean type, perhaps for compatibility with old C standards. Common implementations include the following, where bool may be spelled differently (such as Bool or BOOL):

    • An enum type:

      typedef enum { false, true } bool;
      
    • A typedef and separate enum values:

      typedef int bool;
      enum { false, true };
      
    • A typedef and macro values:

      typedef int bool;
      #define true 1
      #define false 0
      
    • A macro alias and macro values:

      #define bool int
      #define true 1
      #define false 0
      

This module provides an API that is compatible with bindings generated for any of these possible implementations. Note that the implementation only requires Eq and Num instances. Conversion follows C23 semantics: only value 0 is considered false, and any other value is considered true.

Intended for qualified import.

import HsBindgen.Runtime.CBool qualified as CBool
Synopsis

Values

true :: Num b => b Source #

Standard true value: 1

false :: Num b => b Source #

Standard false value: 0

Predicates

isTrue :: (Eq b, Num b) => b -> Bool Source #

True if the value is not 0

isFalse :: (Eq b, Num b) => b -> Bool Source #

True if the value is 0

Conversion

fromBool :: Num b => Bool -> b Source #

Convert from Bool to true or false

toBool :: (Eq b, Num b) => b -> Bool Source #

Convert to Bool using isTrue

Operations

(&&) :: (Eq b, Num b) => b -> b -> b Source #

Boolean and, lazy in the second argument

This function returns one of the standard values.

(||) :: (Eq b, Num b) => b -> b -> b Source #

Boolean or, lazy in the second argument

This function returns one of the standard values.

not :: (Eq b, Num b) => b -> b Source #

Boolean not

This function returns one of the standard values.

bool :: (Eq b, Num b) => a -> a -> b -> a Source #

Boolean case analysis, implemented using isTrue

See bool for details.

if_ :: (Eq b, Num b) => b -> a -> a -> a Source #

Boolean case analysis, implemented using isTrue

when :: (Eq b, Num b, Applicative f) => b -> f () -> f () Source #

Execute an applicative expression when the condition isTrue

unless :: (Eq b, Num b, Applicative f) => b -> f () -> f () Source #

Execute an applicative expression when the condition ifFalse