/* Ternary logic (Rosetta code) in Picat. http://rosettacode.org/wiki/Ternary_logic """ In logic, a three-valued logic (also trivalent, ternary, or trinary logic, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating true, false and some indeterminate third value. This is contrasted with the more commonly known bivalent logics (such as classical sentential or boolean logic) which provide only for true and false. Conceptual form and basic ideas were initially created by Łukasiewicz, Lewis and Sulski. These were then re-formulated by Grigore Moisil in an axiomatic algebraic form, and also extended to n-valued logics in 1945. Example Ternary Logic Operators in Truth Tables: not a ¬ True False Maybe Maybe False True a and b ∧ True Maybe False True True Maybe False Maybe Maybe Maybe False False False False False a or b ∨ True Maybe False True True True True Maybe True Maybe Maybe False True Maybe False if a then b ⊃ True Maybe False True True Maybe False Maybe True Maybe Maybe False True True True a is equivalent to b ≡ True Maybe False True True Maybe False Maybe Maybe Maybe Maybe False False Maybe True Task - Define a new type that emulates ternary logic by storing data trits. - Given all the binary logic operators of the original programming language, reimplement these operators for the new Ternary logic type trit. - Generate a sampling of results using trit variables. Kudos for actually thinking up a test case algorithm where ternary logic is intrinsically useful, optimises the test case algorithm and is preferable to binary logic. Note: Setun (Сетунь) was a balanced ternary computer developed in 1958 at Moscow State University. The device was built under the lead of Sergei Sobolev and Nikolay Brusentsov. It was the only modern ternary computer, using three-valued ternary logic """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import v3_utils. % import util. % import cp. main => go. go => (show_op1('!') ; true), nl, foreach(Op in ['/\\','\\/','->','==']) (show_op2(Op) ; nl,true) end, nl. % op('/\\'). % op('\\/'). % op('->'). % op('=='). % value(true). % value(maybe). % value(false). % Truth tables ternary(true,'!') = false. ternary(maybe,'!') = maybe. ternary(false,'!') = true. ternary(Cond,'!') = Res => C1 = cond(Cond == maybe,maybe,cond(Cond,true,false)), Res = ternary(C1,'!'). ternary(true,'/\\',A) = A. ternary(maybe,'/\\',A) = cond(A==false,false,maybe). ternary(false,'/\\',_A) = false. ternary(true,'\\/',_A) = true. ternary(maybe,'\\/',A) = cond(A==true,true, maybe). ternary(false,'\\/',A) = A. ternary(true,'->',A) = A. ternary(maybe,'->',A) = cond(A==true,true,maybe). ternary(false,'->',_) = true. ternary(true,'==',A) = A. ternary(maybe,'==',_) = maybe. ternary(false,'==',A) = ternary(A,'!'). ternary(Cond1,Op,Cond2) = Res => C1 = cond(Cond1 == maybe,maybe,cond(Cond1,true,false)), C2 = cond(Cond2 == maybe,maybe,cond(Cond2,true,false)), Res = ternary(C1,Op,C2). % List the truth tables show_op1(Op) => println(Op), println(['_' : _ in 1..11]), foreach(V1 in [true,maybe,false]) V2 = ternary(V1,Op), printf("%5w %5w \n",V1,V2) end, nl. show_op2(Op) => Vs = [true,maybe,false], printf("%2w %5w %5w %5w\n",Op,Vs[1],Vs[2],Vs[3]), println(['_' : _ in 1..25]), foreach(V1 in Vs) printf("%-5w | ", V1), foreach(V2 in Vs) C = ternary(V1,Op,V2), printf("%5w ",C) end, nl end, nl. % Some examples go2 => println(ternary(10 > 3,'->',maybe).ternary('!')), println(ternary(4 < 18,'/\\',$not membchk('a',"picat")).ternary('->',maybe).ternary('==',true)), nl.