Hello to all. Has anyone accidentally come across a library / class with at least basic ternary operations? I can realize it myself, at first I wanted to make sure that this is not a bicycle. Not googled (could google bad).

The goal is just an interest, practice, perhaps some use in projects, although it is still difficult to imagine. Something suggests that you can refuse exceptions.

Who does not know is the logic is not 1/0, but 1/0 / -1. A couple of years ago, he wrote this, but the project went to heaven for hard drives. The code was like this:

// true3=1="да", false3=-1="нет", null3=0="не знаю" $systemStatus = and3($s1->getstatus(), $s2->getstatus(), $s3->getstatus()); if ($systemStatus === true3) { echo 'all systems: ready'; } elseif ($systemStatus === false3) { echo 'all systems: idle'; } else echo 'malfunction'; 

PS: Nothing to do with the ternary operator .

  • @ Sh4dow, IMHO, faster to write yourself. =) - ling
  • @ling, that yes, but the literature is also a bit of it. All implications need to be rethought, and as I recall, she still had her own unique operations. - Sh4dow
  • @ Sh4dow, are you sure you saw this thing under php? And the logic is rather triple (three-valued) than ternary, IMHO. By topic: en.wikipedia.org/wiki/Ternary_logic rosettacode.org/wiki/Ternary_logic trinary.ru/trinary - Dex
  • one
    I do not understand another, what is the point of using this type of logic in ordinary programming without having a processor working with it? PS ternary - translated from Anglitsky is threefold, but it's up to you - Dex
  • one
    @ Sh4dow, so stop typing, start implementing, ask questions here, and the pancake is already infected ... ps here, you gave the correct link> A predicate, operation or function in mathematics - the number of their arguments, or operands> In general, predicates with n arguments is called n-ary. hence the ternary, taking three arguments. It means "arity" is not applicable to logic. By the way, right there the forum of linguists (philologists) opened, it is necessary to light up there, let them decide))) - Dex

1 answer 1

I took it myself :)

Link to the tester (there you can download the class and see the truth tables), listing the main operators for future generations:

 define('TRUE3', 1); define('NULL3', 0); define('FALSE3', -1); function l3validate(&$arg) { if ($arg !== FALSE3 and $arg !== NULL3 and $arg !== TRUE3) { $arg = NULL3; return FALSE3; } return TRUE3; } function l3not($a) { // НЕ, инверсия l3validate($a); if ($a === TRUE3) return FALSE3; if ($a === FALSE3) return TRUE3; return NULL3; } function l3and($a, $b) { // И, конъюнкция l3validate($a); l3validate($b); if ($a === TRUE3 and $b === TRUE3) return TRUE3; if ($a === FALSE3 or $b === FALSE3) return FALSE3; return NULL3; } function l3andm() { // И, несколько аргументов $result = TRUE3; foreach (func_get_args() as $arg) { l3validate($arg); if ($arg === NULL3) $result = NULL3; if ($arg === FALSE3) return FALSE3; } return $result; } function l3or($a, $b) { // ИЛИ, дизъюнкция l3validate($a); l3validate($b); if ($a === $b and $a === FALSE3) return FALSE3; if ($a === TRUE3 or $b === TRUE3) return TRUE3; return NULL3; } function l3orm() { // ИЛИ, несколько аргументов $result = FALSE3; foreach (func_get_args() as $arg) { l3validate($arg); if ($arg === NULL3) $result = NULL3; if ($arg === TRUE3) return TRUE3; } return $result; } function l3xor($a, $b) { // ЛИБО ... ЛИБО ..., строгая дизъюнкция l3validate($a); l3validate($b); if ($a === $b and $a !== NULL3) return FALSE3; if (($a === TRUE3 and $b === FALSE3) or ($a === FALSE3 and $b === TRUE3)) return TRUE3; return NULL3; } function l3imp($a, $b) { // ->, импликация l3validate($a); l3validate($b); if ($a === FALSE3 or $b === TRUE3) return TRUE3; if ($a === TRUE3 and $b === FALSE3) return FALSE3; return NULL3; } function l3bimp($a, $b) { // <-, обратная импликация l3validate($a); l3validate($b); if ($a === TRUE3 or $b === FALSE3) return TRUE3; if ($a === FALSE3 and $b === TRUE3) return FALSE3; return NULL3; } function l3eq($a, $b) { // =, эквивалентность l3validate($a); l3validate($b); if ($a === NULL3 or $b === NULL3) return NULL3; if ($a === $b) return TRUE3; return FALSE3; } 

PS: Especially for @Dex: the discussion of terms , yet yours took) But still look at the discussion, it turned out to be difficult and purely aesthetic. Even the variant "trivalent" would be true lexical%)

UPDATE: Corrected "ideologically", any "unidentified" turns into NULL3. If you want to be more tolerant, the class has a cast method that rounds to the nearest logical one.

  • Not can be easier) l3not ($ a): return - $ a; - timka_s
  • Here many functions are already on the ideology of the ternary logic, but its awareness is only a process, therefore not all) echo l3not (15); // 0 echo l3not (-250); // 0 echo l3not (array (1 => 5)); // 0 In general, I have a strong feeling that this is not “just added a symbol and counted”, but much deeper = / - Sh4dow
  • I mean that there will be a relatively simple arithmetic expression ... But l3not(15); - this is another) simpler class, and checking in the constructor)) and instead of instance methods - static ... - timka_s
  • the worse if (! in_array ($ arg, array (FALSE3, NULL3, TRUE3))) return NULL3; ?) This is in multi-functions, so it would be necessary to add everywhere. That is, what happens: you ask "tomorrow will be warm?" and they answer you either "yes" or "no" or "xs, google")). If you answer in Hebrew ( 15 ) or Chinese ( new stdClass ), this is absolutely equivalent to the answer "I do not know." - Sh4dow
  • @ Sh4dow, merci, of course. But, if you allow me, I will continue. Why did you choose such function names (prefix l3 )? Instead of following the same notation as at the beginning, when TRUE3, FALSE3, NULL3 were defined. Those. not3 , and3 ... - Dex