summaryrefslogtreecommitdiff
path: root/hamlet.go
blob: 3315433f7b9d596cb7d3b916eb56c68874699502 (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
package hamlet

import "reflect"

type Assertion struct {
	value bool
}

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

// New returns a new Assertion. An assertion is true iff its
// underlying value is non-zero in the Go sense.
func New(v any) Assertion {
	return Assertion{!reflect.ValueOf(v).IsZero()}
}

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

	return Assertion{negation}
}

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

	return Assertion{av || bv}
}

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

	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))
}