Ternary logic: Difference between revisions

(Added Easylang)
(9 intermediate revisions by 4 users not shown)
Line 933:
MAYBE EQV TRUE = MAYBE
TRUE EQV TRUE = TRUE
</pre>
 
=={{header|Bruijn}}==
Direct translations of the truth tables to lambda calculus. The operators could be golfed significantly. If you do so, please add them here!
 
For applications of Ternary logic, see bruijn's [[Balanced_ternary#Bruijn|balanced ternary]] implementation.
}</syntaxhighlight lang="bruijn">
true [[[0]]]
 
maybe [[[1]]]
 
false [[[2]]]
 
¬‣ [0 true maybe false]
 
…⋀… [[1 (0 1 1 1) (0 0 0 1) (0 0 0 0)]]
 
…⋁… [[1 (0 0 0 0) (0 1 0 0) (0 1 1 1)]]
 
…⊃… [[1 (0 true 0 1) (0 true 1 1) (0 1 1 1)]]
 
…≡… [[1 (0 true 0 1) (0 1 1 1) (0 0 0 0)]]
 
# --- result samples ---
 
:import std/List .
 
main [[inp <> "=" <> !res ++ "\n"] <++> (cross3 ops trits trits)]
!‣ [0 "false" "maybe" "true"]
…<>… [[1 ++ " " ++ 0]]
inp 0 [[~1 <> (0 [[!1 <> (0 [[!1]])]])]]
res ^(^0) ^(~0) ^(~(~0))
ops (…⋀… : "and") : ((…⋁… : "or") : ((…⊃… : "if") : {}(…≡… : "equiv")))
trits true : (maybe : {}false)
</syntaxhighlight>
 
{{out}}
<pre>
and true true = true
and true maybe = maybe
and true false = false
and maybe true = maybe
and maybe maybe = maybe
and maybe false = false
and false true = false
and false maybe = false
and false false = false
or true true = true
or true maybe = true
or true false = true
or maybe true = true
or maybe maybe = maybe
or maybe false = maybe
or false true = true
or false maybe = maybe
or false false = false
if true true = true
if true maybe = true
if true false = true
if maybe true = maybe
if maybe maybe = maybe
if maybe false = true
if false true = false
if false maybe = maybe
if false false = true
equiv true true = true
equiv true maybe = maybe
equiv true false = false
equiv maybe true = maybe
equiv maybe maybe = maybe
equiv maybe false = maybe
equiv false true = false
equiv false maybe = maybe
equiv false false = true
</pre>
 
Line 1,677 ⟶ 1,751:
return x
.
print " (AND) ( OR) (EQV) (IMP) (NOT)"
print " F ? T F ? T F ? T F ? T "
print " -------------------------------------------------"
for i = -1 to 1
o$ = " " & sym$[i] & " | "
o$ &= sym$[tand -1 i] & " " & sym$[tand 0 i] & " " & sym$[tand 1 i]
o$ &= " "
o$ &= sym$[tor -1 i] & " " & sym$[tor 0 i] & " " & sym$[tor 1 i]
o$ &= " "
o$ &= sym$[timp -1 i] & " " & sym$[timp 0 i] & " " & sym$[timp 1 i]
o$ &= " "
o$ &= sym$[timp -1 i] & " " & sym$[timp 0 i] & " " & sym$[timp 1 i]
o$ &= " " & sym$[tnot i]
print o$
.
Line 3,390 ⟶ 3,464:
=={{header|langur}}==
{{trans|Go}}
{{works with|langur|0.6.12}}
 
<syntaxhighlight lang="langur"># borrowing null for "maybe"
val .trSet = [false, null, true]
 
val .and = ffn given.a, .b: switch[and] .a, .b {
case true, null:
case null, true:
Line 3,402 ⟶ 3,474:
}
 
val .or = ffn given.a, .b: switch[and] .a, .b {
case false, null:
case null, false:
Line 3,409 ⟶ 3,481:
}
 
val .imply = ffn .a, .b: if(.a nor .b: not? .a; .b)
 
# formatting function for the result values
# replacing null with "maybe"
# using left alignment of 5 code points
val .F = ffn .r: $"\{{nn [.r, "maybe"]:-5}}"
 
writeln "a not a"
for .a in .trSet {
writeln $"\{{.a:.fn F;}} \({{not? .a:.fn F)}}"
}
 
Line 3,424 ⟶ 3,496:
for .a in .trSet {
for .b in .trSet {
writeln $"\{{.a:.fn F;}} \{{.b:.fn F;}} \{{.and(.a, .b):.fn F;}}"
}
}
Line 3,431 ⟶ 3,503:
for .a in .trSet {
for .b in .trSet {
writeln $"\{{.a:.fn F;}} \{{.b:.fn F;}} \{{.or(.a, .b):.fn F;}}"
}
}
Line 3,438 ⟶ 3,510:
for .a in .trSet {
for .b in .trSet {
writeln $"\{{.a:.fn F;}} \{{.b:.fn F;}} \{{.imply(.a, .b):.fn F;}}"
}
}
Line 3,445 ⟶ 3,517:
for .a in .trSet {
for .b in .trSet {
writeln $"\{{.a:.fn F;}} \{{.b:.fn F;}} \{{.a ==? .b:.fn F;}}"
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 6,456 ⟶ 6,529:
end func;
 
$ syntax expr: .().xor.() is -> 15;
const func trit: (in trit: aTrit1) xor (in trit: aTrit2) is
return tritImplies[succ(ord(aTrit1))][succ(ord(aTrit2))];
Line 6,463 ⟶ 6,536:
return tritImplies[succ(ord(aTrit1))][succ(ord(aTrit2))];
 
syntax expr: .(). == .() is <-> 12;
const func trit: (in trit: aTrit1) == (in trit: aTrit2) is
return tritEquiv[succ(ord(aTrit1))][succ(ord(aTrit2))];
 
const func trit: rand (in trit: low, in trit: high) is
return trit conv (rand(ord(low), ord(high)));
 
# Begin of test code
885

edits