summaryrefslogtreecommitdiff
path: root/hamlet.go
blob: 8cdc5788704767b3f2e1435922df53e0692220f5 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package hamlet

import "reflect"

type Assertion struct {
	truth bool
}

func (a Assertion) Assert() bool {
	return a.truth
}

func Present(v any) Assertion {
	return Assertion{!reflect.ValueOf(v).IsZero()}
}

func Absent(v any) Assertion {
	return Assertion{reflect.ValueOf(v).IsZero()}
}

// Not computes ~a, given assertion a.
func Not(a Assertion) Assertion {
	negation := !a.truth

	return Assertion{negation}
}

// Or computes a v b, given assertions a and b.
func Or(a Assertion, b Assertion) Assertion {
	av := a.truth
	bv := b.truth

	return Assertion{av || bv}
}

// And computes a ^ b, given assertions a and b.
func And(a Assertion, b Assertion) Assertion {
	av := a.truth
	bv := b.truth

	return Assertion{av && bv}
}

// If computes a → b, given assertions a and b.
func If(a Assertion, b Assertion) Assertion {
	return Or(Not(a), b)
}

// If computes a ↔ b, given assertions a and b.
func Iff(a Assertion, b Assertion) Assertion {
	return And(If(a, b), If(b, a))
}

// Xor computes a ⊻ b, given assertions a and b.
func Xor(a Assertion, b Assertion) Assertion {
	return Not(Iff(a, b))
}