-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrit.go
681 lines (617 loc) · 17.2 KB
/
trit.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package trit
import (
"encoding/json"
"errors"
)
// ErrUnknownValue is returned when we try to convert Unknown to bool.
var ErrUnknownValue = errors.New("cannot convert Unknown to bool")
// Trit represents a trinary digit, which can take on three distinct
// states: False, Unknown, or True. This type is a fundamental unit of
// trinary or ternary logic systems, including trinary computers and
// balanced ternary systems.
type Trit int8
const (
// False represents the Trit state equivalent to 'false' in binary
// logic. It is represented by a negative value (-1) in the underlying
// int8 type.
//
// *Any negative numbers are also considered False.
False Trit = -1
// Unknown represents an unknown or indeterminate Trit state. It is
// beneficial in scenarios where a "maybe" state is required.
// Unknown is represented by a zero value in the underlying int8 type.
Unknown Trit = 0
// True represents the Trit state equivalent to 'true' in binary
// logic. It is represented by a positive value (1) in the underlying
// int8 type.
//
// *Any positive numbers are also considered True.
True Trit = 1
)
// Default is a method that checks if the value of the Trit is Unknown.
// If it is, it sets the Trit to the given Trit argument.
//
// Example usage:
//
// t := trit.Unknown
// t.Default(trit.True)
// fmt.Println(t.String()) // Output: True
func (t *Trit) Default(trit Trit) Trit {
if t.Val() == Unknown {
*t = trit
}
return *t
}
// TrueIfUnknown is a method that checks if the value of the Trit is Unknown.
// If it is, it sets the Trit to True.
// It then returns the updated Trit.
//
// Example usage:
//
// t := trit.Unknown
// t.TrueIfUnknown()
// fmt.Println(t.String()) // Output: True
func (t *Trit) TrueIfUnknown() Trit {
if t.Val() == Unknown {
*t = True
}
return *t
}
// FalseIfUnknown is a method that checks if the value of the Trit is Unknown.
// If it is, it sets the Trit to False.
// It then returns the updated Trit.
//
// Example usage:
//
// t := trit.Unknown
// t.FalseIfUnknown()
// fmt.Println(t.String()) // Output: False
func (t *Trit) FalseIfUnknown() Trit {
if t.Val() == Unknown {
*t = False
}
return *t
}
// Clean is a method that checks if the value of the Trit is Unknown.
// If it is, it resets the Trit to Unknown.
// It then returns the updated Trit.
//
// Example usage:
//
// t := trit.True
// t.Clean()
// fmt.Println(t.String()) // Output: Unknown
func (t *Trit) Clean() Trit {
if t.Val() == Unknown {
*t = Unknown
}
return *t
}
// IsFalse returns true if the Trit value represents a False state, which
// is any negative number in the underlying int8 type.
//
// Example usage:
//
// t := trit.Trit(-2)
// fmt.Println(t.IsFalse()) // Output: true
func (t Trit) IsFalse() bool {
if int8(t) < 0 {
return true
}
return false
}
// IsUnknown returns true if the Trit value represents a Unknown state, which
// is zero in the underlying int8 type.
//
// Example usage:
//
// t := trit.Trit(0)
// fmt.Println(t.IsUnknown()) // Output: true
func (t Trit) IsUnknown() bool {
if int8(t) == 0 {
return true
}
return false
}
// IsTrue returns true if the Trit value represents a True state, which
// is any positive number in the underlying int8 type.
//
// Example usage:
//
// t := trit.Trit(2)
// fmt.Println(t.IsTrue()) // Output: true
func (t Trit) IsTrue() bool {
if int8(t) > 0 {
return true
}
return false
}
// Set assigns a Trit value based on the given integer. Negative values
// are interpreted as False, zero as Unknown, and positive values as True.
//
// Example usage:
//
// var t trit.Trit
// t.Set(-2)
// fmt.Println(t.String()) // Output: False
func (t *Trit) Set(v int) Trit {
switch {
case v < 0:
*t = False
case v > 0:
*t = True
default:
*t = Unknown
}
return *t
}
// Val returns the normalized Trit value if a Trit was not properly
// initialized using the predefined constants (False, Unknown, True).
// If a Trit was set using an int8 value other than -1, 0, or 1, Val
// maps it to the closest Trit value.
//
// Example usage:
//
// t := trit.Trit(7)
// fmt.Println(t.Val().String()) // Output: True
func (t Trit) Val() Trit {
if t.IsFalse() {
return False
}
if t.IsTrue() {
return True
}
return Unknown
}
// Norm normalizes the Trit value. If a Trit was set using an int8
// value other than -1, 0, or 1, Norm maps it to the closest Trit value.
//
// Example usage:
//
// t := trit.Trit(7)
// t.Norm()
// fmt.Println(t.Int()) // Output: 1
func (t *Trit) Norm() Trit {
*t = t.Val()
return *t
}
// Int returns the underlying int8 value of a Trit in the form of an int.
// It first normalizes the Trit value and then returns it as an int.
//
// Example usage:
//
// t := trit.Trit(7)
// fmt.Println(t.Int()) // Output: 1
func (t Trit) Int() int {
return int(t.Val())
}
// String returns a string representation of a Trit value.
// The possible return values are "False", "Unknown", and "True".
//
// Example usage:
//
// t := trit.True
// fmt.Println(t.String()) // Output: True
func (t Trit) String() string {
switch t.Val() {
case False:
return "False"
case True:
return "True"
}
return "Unknown"
}
// Not performs a logical NOT operation on a Trit value and returns the result.
// This function applies the following rules based on the truth table for NOT:
// - Not(False) => True
// - Not(Unknown) => Unknown
// - Not(True) => False
//
// Example usage:
//
// t := trit.True
// result := t.Not()
// fmt.Println(result.String()) // Output: False
func (t Trit) Not() Trit {
switch t.Val() {
case False:
return True
case True:
return False
}
return Unknown
}
// Ma performs a logical MA (Modus Ponens Absorption) operation on a Trit
// value and returns the result. This function applies the following rules
// based on the truth table for MA:
// - Ma(False) => False
// - Ma(Unknown) => True
// - Ma(True) => True
//
// Example usage:
//
// a := trit.True
// result := a.Ma()
// fmt.Println(result.String()) // Output: True
func (t Trit) Ma() Trit {
if t.Val() == False {
return False
}
return True
}
// La performs a logical LA (Law of Absorption) operation on a Trit value
// and returns the result. This function applies the following rules based
// on the truth table for LA:
// - La(False) => False
// - La(Unknown) => False
// - La(True) => True
//
// Example usage:
//
// a := trit.True
// result := a.La()
// fmt.Println(result.String()) // Output: True
func (t Trit) La() Trit {
if t.Val() == True {
return True
}
return False
}
// Ia performs a logical IA (Implication Absorption) operation on a Trit
// values and returns the result. This function applies the following
// rules based on the truth table for IA:
// - Ia(False) => False
// - Ia(Unknown) => True
// - Ia(True) => False
//
// Example usage:
//
// a := trit.True
// result := a.Ia()
// fmt.Println(result.String()) // Output: False
func (t Trit) Ia() Trit {
if t.Val() == Unknown {
return True
}
return False
}
// And performs a logical AND operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for AND:
// - And(False, False) => False
// - And(False, Unknown) => False
// - And(False, True) => False
// - And(Unknown, False) => False
// - And(Unknown, Unknown) => Unknown
// - And(Unknown, True) => Unknown
// - And(True, False) => False
// - And(True, Unknown) => Unknown
// - And(True, True) => True
//
// Example usage:
//
// a := trit.True
// b := trit.Unknown
// result := a.And(b)
// fmt.Println(result.String()) // Output: Unknown
func (t Trit) And(trit Trit) Trit {
if t.Val() == False || trit.Val() == False {
return False
}
if t.Val() == Unknown || trit.Val() == Unknown {
return Unknown
}
return True
}
// Or performs a logical OR operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for OR:
// - Or(False, False) => False
// - Or(False, Unknown) => Unknown
// - Or(False, True) => True
// - Or(Unknown, False) => Unknown
// - Or(Unknown, Unknown) => Unknown
// - Or(Unknown, True) => True
// - Or(True, False) => True
// - Or(True, Unknown) => True
// - Or(True, True) => True
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Or(b)
// fmt.Println(result.String()) // Output: True
func (t Trit) Or(trit Trit) Trit {
if t.Val() == True || trit.Val() == True {
return True
}
if t.Val() == Unknown || trit.Val() == Unknown {
return Unknown
}
return False
}
// Xor performs a logical XOR operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for XOR:
// - Xor(False, False) => False
// - Xor(False, Unknown) => Unknown
// - Xor(False, True) => True
// - Xor(Unknown, False) => Unknown
// - Xor(Unknown, Unknown) => Unknown
// - Xor(Unknown, True) => Unknown
// - Xor(True, False) => True
// - Xor(True, Unknown) => Unknown
// - Xor(True, True) => False
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Xor(b)
// fmt.Println(result.String()) // Output: True
func (t Trit) Xor(trit Trit) Trit {
// Check first, because Xor(Unknown, Unknown) should be Unknown.
if t.Val() == Unknown || trit.Val() == Unknown {
return Unknown
}
// Pay attention, Unknown == Unknown != False
if t.Val() == trit.Val() {
return False
}
return True
}
// Nand performs a logical NAND operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for NAND:
// - Nand(False, False) => True
// - Nand(False, Unknown) => True
// - Nand(False, True) => True
// - Nand(Unknown, False) => True
// - Nand(Unknown, Unknown) => Unknown
// - Nand(Unknown, True) => Unknown
// - Nand(True, False) => True
// - Nand(True, Unknown) => Unknown
// - Nand(True, True) => False
//
// Example usage:
//
// a := trit.True
// b := trit.Unknown
// result := a.Nand(b)
// fmt.Println(result.String()) // Output: True
func (t Trit) Nand(trit Trit) Trit {
return t.And(trit).Not()
}
// Nor performs a logical NOR operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for NOR:
// - Nor(False, False) => True
// - Nor(False, Unknown) => Unknown
// - Nor(False, True) => False
// - Nor(Unknown, False) => Unknown
// - Nor(Unknown, Unknown) => Unknown
// - Nor(Unknown, True) => False
// - Nor(True, False) => False
// - Nor(True, Unknown) => False
// - Nor(True, True) => False
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Nor(b)
// fmt.Println(result.String()) // Output: False
func (t Trit) Nor(trit Trit) Trit {
return t.Or(trit).Not()
}
// Nxor performs a logical XNOR operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for XNOR:
// - Nxor(False, False) => True
// - Nxor(False, Unknown) => Unknown
// - Nxor(False, True) => False
// - Nxor(Unknown, False) => Unknown
// - Nxor(Unknown, Unknown) => Unknown
// - Nxor(Unknown, True) => Unknown
// - Nxor(True, False) => False
// - Nxor(True, Unknown) => Unknown
// - Nxor(True, True) => True
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Nxor(b)
// fmt.Println(result.String()) // Output: False
func (t Trit) Nxor(trit Trit) Trit {
return t.Xor(trit).Not()
}
// Min performs a logical MIN operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for MIN:
// - Min(False, False) => False
// - Min(False, Unknown) => False
// - Min(False, True) => False
// - Min(Unknown, False) => False
// - Min(Unknown, Unknown) => Unknown
// - Min(Unknown, True) => Unknown
// - Min(True, False) => False
// - Min(True, Unknown) => Unknown
// - Min(True, True) => True
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Min(b)
// fmt.Println(result.String()) // Output: False
func (t Trit) Min(trit Trit) Trit {
return t.And(trit)
}
// Max performs a logical MAX operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for MAX:
// - Max(False, False) => False
// - Max(False, Unknown) => Unknown
// - Max(False, True) => True
// - Max(Unknown, False) => Unknown
// - Max(Unknown, Unknown) => Unknown
// - Max(Unknown, True) => True
// - Max(True, False) => True
// - Max(True, Unknown) => True
// - Max(True, True) => True
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Max(b)
// fmt.Println(result.String()) // Output: True
func (t Trit) Max(trit Trit) Trit {
return t.Or(trit)
}
// Imp performs a logical IMP operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for IMP:
// - Imp(False, False) => True
// - Imp(False, Unknown) => True
// - Imp(False, True) => True
// - Imp(Unknown, False) => Unknown
// - Imp(Unknown, Unknown) => True
// - Imp(Unknown, True) => True
// - Imp(True, False) => False
// - Imp(True, Unknown) => Unknown
// - Imp(True, True) => True
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Imp(b)
// fmt.Println(result.String()) // Output: False
func (t Trit) Imp(trit Trit) Trit {
if t.Val() == Unknown && trit.Val() == Unknown {
return True
} else if t.Val() == False || trit.Val() == True {
return True
} else if t.Val() == Unknown || trit.Val() == Unknown {
return Unknown
}
return False
}
// Nimp performs a logical NIMP operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for NIMP:
// - Nimp(False, False) => False
// - Nimp(False, Unknown) => False
// - Nimp(False, True) => False
// - Nimp(Unknown, False) => Unknown
// - Nimp(Unknown, Unknown) => False
// - Nimp(Unknown, True) => False
// - Nimp(True, False) => True
// - Nimp(True, Unknown) => Unknown
// - Nimp(True, True) => False
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Nimp(b)
// fmt.Println(result.String()) // Output: True
func (t Trit) Nimp(trit Trit) Trit {
return t.Imp(trit).Not()
}
// Eq performs a logical EQ operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for EQ:
// - Eq(False, False) => True
// - Eq(False, Unknown) => Unknown
// - Eq(False, True) => False
// - Eq(Unknown, False) => Unknown
// - Eq(Unknown, Unknown) => Unknown
// - Eq(Unknown, True) => Unknown
// - Eq(True, False) => False
// - Eq(True, Unknown) => Unknown
// - Eq(True, True) => True
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Eq(b)
// fmt.Println(result.String()) // Output: False
//
// Equivalence (EQ): Also known as "if and only if" or "iff".
// It's True if both Trit are the same (either both True or both False),
// and False if they are different.
func (t Trit) Eq(trit Trit) Trit {
if t.Val() == Unknown || trit.Val() == Unknown {
return Unknown
} else if t.Val() == trit.Val() {
return True
}
return False
}
// Neq performs a logical NEQ operation between two Trit values and returns
// the result. This function applies the following rules based on the truth
// table for NEQ:
// - Neq(False, False) => False
// - Neq(False, Unknown) => Unknown
// - Neq(False, True) => True
// - Neq(Unknown, False) => Unknown
// - Neq(Unknown, Unknown) => Unknown
// - Neq(Unknown, True) => Unknown
// - Neq(True, False) => True
// - Neq(True, Unknown) => Unknown
// - Neq(True, True) => False
//
// Example usage:
//
// a := trit.True
// b := trit.False
// result := a.Neq(b)
// fmt.Println(result.String()) // Output: True
func (t Trit) Neq(trit Trit) Trit {
return t.Eq(trit).Not()
}
// CanBeBool checks if the value can be converted to bool.
// Returns true only if the value is True or False.
func (t Trit) CanBeBool() bool {
return !t.IsUnknown()
}
// ToBool converts Trit to bool.
// Returns (false, ErrUnknownValue) if the value is Unknown.
func (t Trit) ToBool() (bool, error) {
if !t.CanBeBool() {
return false, ErrUnknownValue
}
return t.IsTrue(), nil
}
// MarshalJSON implements the json.Marshaler interface.
// Unknown is converted to null, True to true, False to false.
func (t Trit) MarshalJSON() ([]byte, error) {
if t.IsUnknown() {
return []byte("null"), nil
}
return json.Marshal(t.IsTrue())
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// null is converted to Unknown, true to True, false to False.
func (t *Trit) UnmarshalJSON(data []byte) error {
// Перевіряємо на null
if string(data) == "null" {
*t = Unknown
return nil
}
var b bool
if err := json.Unmarshal(data, &b); err != nil {
return err
}
if b {
*t = True
} else {
*t = False
}
return nil
}