aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/booleans.pk
blob: bd81925ed255b8e8d1592850d43be633bee71f75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
## std.booleans: Booleans and related types. Mostly compiler magic.
## A stub module for documentation. Mostly compiler magic.

pub type unit = union[sole]
pub type bool = union[false, true]

# note: puck could resolve kleene.false vs bool.false...
# this is probably not worth it compared to improved type inference
# pub type Kleene = union[False, Maybe, True]

## Boolean equality
pub func ==(a: bool, b: bool): bool =
  match a
  of (true, true), (false, false): true
  of (false, true), (true, false): false

pub func !=(a: bool, b: bool): bool =
  not a == b

## Boolean negation
pub func not(a: bool): bool =
  match a
  of true: false
  of false: true

## Boolean conjunction
pub func and(a: bool, b: bool): bool =
  match (a, b)
  of (true, true): true
  of _: false

## Boolean disjunction
pub func or(a: bool, b: bool): bool =
  match (a, b)
  of (false, false): false
  of _: true

## Boolean exclusive disjunction
pub func xor(a: bool, b: bool): bool =
  match (a, b)
  of (true, true), (false, false): false
  of (true, false), (false, true): true