Skip to content

Commit

Permalink
Add Equivalence method
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed Jun 29, 2023
1 parent eb352eb commit 8c5c42d
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 74 deletions.
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ The truth table for the three-valued logic system is shown here. It's a mathemat
NXOR - Logical not XOR

IMP - Implication in Lukasevich's Logic
EQ - If and only if
MIN - Minimum
NIMP - NOT IMP
NEQ - NOT EQ
MAX - Maximum
A | NA A | MA A | LA A | IA
Expand Down Expand Up @@ -145,17 +149,30 @@ The truth table for the three-valued logic system is shown here. It's a mathemat
T | U | U T | U | F T | U | U
T | T | F T | T | F T | T | T
A | B | IMP A | B | MIN A | B | MAX
A | B | IMP A | B | EQ A | B | MIN
---+---+------ ---+---+------ ---+---+------
F | F | T F | F | F F | F | F
F | U | T F | U | F F | U | U
F | T | T F | T | F F | T | T
U | F | U U | F | F U | F | U
F | F | T F | F | T F | F | F
F | U | T F | U | U F | U | F
F | T | T F | T | F F | T | F
U | F | U U | F | U U | F | F
U | U | T U | U | U U | U | U
U | T | T U | T | U U | T | T
T | F | F T | F | F T | F | T
T | U | U T | U | U T | U | T
U | T | T U | T | U U | T | U
T | F | F T | F | F T | F | F
T | U | U T | U | U T | U | U
T | T | T T | T | T T | T | T
A | B | NIMP A | B | NEQ A | B | MAX
---+---+------ ---+---+------ ---+---+------
F | F | F F | F | F F | F | F
F | U | F F | U | U F | U | U
F | T | F F | T | T F | T | T
U | F | U U | F | U U | F | U
U | U | F U | U | U U | U | U
U | T | F U | T | U U | T | T
T | F | T T | F | T T | F | T
T | U | U T | U | U T | U | T
T | T | F T | T | F T | T | T
```
## Explanation
Expand Down
40 changes: 40 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,43 @@ func Max[T, U Logicable](a T, b U) Trit {
tb := logicToTrit(b)
return ta.Max(tb)
}

// Imp performs a logical IMP operation between two Trit-Like values
// and returns the result as Trit.
//
// See Trit.Imp() for more information.
func Imp[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Imp(tb)
}

// Nimp performs a logical NIMP operation between two Trit-Like values
// and returns the result as Trit.
//
// See Trit.Nimp() for more information.
func Nimp[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Nimp(tb)
}

// Eq performs a logical EQ operation between two Trit-Like values
// and returns the result as Trit.
//
// See Trit.Eq() for more information.
func Eq[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Eq(tb)
}

// Neq performs a logical NEQ operation between two Trit-Like values
// and returns the result as Trit.
//
// See Trit.Neq() for more information.
func Neq[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Neq(tb)
}
89 changes: 88 additions & 1 deletion fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,97 @@ func TestImp(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.a.Imp(test.b)
result := Imp(test.a, test.b)
if result != test.out {
t.Errorf("Imp did not return %v for (%v, %v)", test.out, test.a, test.b)
}
})
}
}

// TestNimp tests the Nimp function.
func TestNimp(t *testing.T) {
tests := []struct {
name string
a Trit
b Trit
out Trit
}{
{"Nimp should return False for (False, False)", False, False, False},
{"Nimp should return False for (False, Unknown)", False, Unknown, False},
{"Nimp should return False for (False, True)", False, True, False},
{"Nimp should return Unknown for (Unknown, False)", Unknown, False, Unknown},
{"Nimp should return False for (Unknown, Unknown)", Unknown, Unknown, False},
{"Nimp should return False for (Unknown, True)", Unknown, True, False},
{"Nimp should return True for (True, False)", True, False, True},
{"Nimp should return Unknown for (True, Unknown)", True, Unknown, Unknown},
{"Nimp should return False for (True, True)", True, True, False},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := Nimp(test.a, test.b)
if result != test.out {
t.Errorf("Nimp did not return %v for (%v, %v)", test.out, test.a, test.b)
}
})
}
}

// TestEq tests the Eq function.
func TestEq(t *testing.T) {
tests := []struct {
name string
a Trit
b Trit
out Trit
}{
{"Eq should return True for (False, False)", False, False, True},
{"Eq should return Unknown for (False, Unknown)", False, Unknown, Unknown},
{"Eq should return False for (False, True)", False, True, False},
{"Eq should return Unknown for (Unknown, False)", Unknown, False, Unknown},
{"Eq should return Unknown for (Unknown, Unknown)", Unknown, Unknown, Unknown},
{"Eq should return Unknown for (Unknown, True)", Unknown, True, Unknown},
{"Eq should return False for (True, False)", True, False, False},
{"Eq should return Unknown for (True, Unknown)", True, Unknown, Unknown},
{"Eq should return True for (True, True)", True, True, True},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := Eq(test.a, test.b)
if result != test.out {
t.Errorf("Eq did not return %v for (%v, %v)", test.out, test.a, test.b)
}
})
}
}

// TestNeq tests the Neq function.
func TestNeq(t *testing.T) {
tests := []struct {
name string
a Trit
b Trit
out Trit
}{
{"Neq should return False for (False, False)", False, False, False},
{"Neq should return Unknown for (False, Unknown)", False, Unknown, Unknown},
{"Neq should return True for (False, True)", False, True, True},
{"Neq should return Unknown for (Unknown, False)", Unknown, False, Unknown},
{"Neq should return Unknown for (Unknown, Unknown)", Unknown, Unknown, Unknown},
{"Neq should return Unknown for (Unknown, True)", Unknown, True, Unknown},
{"Neq should return True for (True, False)", True, False, True},
{"Neq should return Unknown for (True, Unknown)", True, Unknown, Unknown},
{"Neq should return False for (True, True)", True, True, False},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := Neq(test.a, test.b)
if result != test.out {
t.Errorf("Neq did not return %v for (%v, %v)", test.out, test.a, test.b)
}
})
}
}
Loading

0 comments on commit 8c5c42d

Please sign in to comment.