-
Notifications
You must be signed in to change notification settings - Fork 0
/
possibility.go
144 lines (129 loc) · 3.16 KB
/
possibility.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package analysis
// Possibility is an enumerator of possibilites.
type Possibility int
const (
// True represents a logical certanty or true.
True Possibility = iota
// Maybe represents the possibility of true or false.
Maybe
// False represents a logical certanty of false.
False
// Impossible represents a contradiction of certanties (for example
// True ∩ False)
Impossible
)
// MaybeTrue returns true iff a is True or Maybe.
func (a Possibility) MaybeTrue() bool {
return a == True || a == Maybe
}
// MaybeFalse returns true iff a is False or Maybe.
func (a Possibility) MaybeFalse() bool {
return a == False || a == Maybe
}
// Not returns the logical negation of a.
func (a Possibility) Not() Possibility {
switch a {
case False:
return True
case True:
return False
case Impossible:
return Impossible
default:
return Maybe
}
}
// And returns the logical-and of a and b.
func (a Possibility) And(b Possibility) Possibility {
switch {
case a == Impossible || b == Impossible:
return Impossible
case a == False, b == False:
return False
case a == Maybe, b == Maybe:
return Maybe
default:
return True
}
}
// Or returns the logical-or of a and b.
func (a Possibility) Or(b Possibility) Possibility {
switch {
case a == Impossible || b == Impossible:
return Impossible
case a == True, b == True:
return True
case a == Maybe, b == Maybe:
return Maybe
default:
return False
}
}
// Equals returns the possibility of a equaling b.
func (a Possibility) Equals(b Possibility) Possibility {
switch {
case a == Impossible || b == Impossible:
return Impossible
case a == Maybe, b == Maybe:
return Maybe
default:
if a == b {
return True
}
return False
}
}
// Union returns the union of possibile Possibilitys for a and b.
func (a Possibility) Union(b Possibility) Possibility {
if a == Impossible || b == Impossible {
return Impossible
}
if a.Equals(b) == True {
return a
}
return Maybe
}
// Intersect returns the intersection of possibile Possibilitys for a and b.
func (a Possibility) Intersect(b Possibility) Possibility {
if a == Impossible || b == Impossible {
return Impossible
}
if a == Maybe {
if b == Maybe {
return Maybe
}
a, b = b, a
}
// a is True or False
// b is True, False or Maybe
if b == Maybe || a == b {
return a
}
return Impossible
}
// Difference returns the possibile for v that are not found in o.
func (a Possibility) Difference(b Possibility) Possibility {
if a == Impossible || b == Impossible {
return Impossible
}
if a == Maybe {
return b.Not()
}
if a == b {
return Impossible
}
return a
}