How to resolve the algorithm Ternary logic step by step in the Haskell programming language
How to resolve the algorithm Ternary logic step by step in the Haskell programming language
Table of Contents
Problem Statement
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.
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
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ternary logic step by step in the Haskell programming language
Haskell Code Overview
The provided Haskell code demonstrates various logical operations using a three-valued logic system where truth values are represented as False
, Maybe
, or True
.
Trit Data Type
The code defines a custom data type Trit
with three possible values: False
, Maybe
, and True
.
Logical Operations
not
: Negation. ReturnsTrue
if the input isFalse
,False
if the input isTrue
, andMaybe
otherwise.&&
: Conjunction (AND). ReturnsFalse
if either input isFalse
,Maybe
if both inputs areMaybe
, andTrue
otherwise.||
: Disjunction (OR). ReturnsTrue
if either input isTrue
,Maybe
if both inputs areMaybe
, andFalse
otherwise.=->
: Implication. ReturnsFalse
if the first input isTrue
and the second isFalse
,Maybe
if the first input isMaybe
, andTrue
otherwise.==
: Equality. ReturnsTrue
if both inputs are equal,False
if they are both different, andMaybe
otherwise.
Table Generation
The code generates truth tables for each of the logical operations by combining possible values for the inputs. It uses the inputs1
and inputs2
lists to generate input combinations for unary and binary operations, respectively.
Truth Table Formatting
The table
function formats the truth tables as strings. It adds a header row with column labels and pads the values to align them.
Main Function
The main function applies the table
function to each logical operation and prints the resulting truth tables.
Sample Input and Output
The provided inputs1
and inputs2
lists represent sample inputs for the truth tables. The output will be a set of truth tables for each logical operation, with the following format:
A | not A |
---|---|
True | False |
Maybe | Maybe |
False | True |
A | B | and A B |
---|---|---|
True | True | True |
True | Maybe | Maybe |
True | False | False |
Maybe | True | Maybe |
Maybe | Maybe | Maybe |
Maybe | False | False |
False | True | False |
False | Maybe | False |
False | False | False |
And so on for the other logical operations.
Source code in the haskell programming language
import Prelude hiding (Bool(..), not, (&&), (||), (==))
main = mapM_ (putStrLn . unlines . map unwords)
[ table "not" $ unary not
, table "and" $ binary (&&)
, table "or" $ binary (||)
, table "implies" $ binary (=->)
, table "equals" $ binary (==)
]
data Trit = False | Maybe | True deriving (Show)
False `nand` _ = True
_ `nand` False = True
True `nand` True = False
_ `nand` _ = Maybe
not a = nand a a
a && b = not $ a `nand` b
a || b = not a `nand` not b
a =-> b = a `nand` not b
a == b = (a && b) || (not a && not b)
inputs1 = [True, Maybe, False]
inputs2 = [(a,b) | a <- inputs1, b <- inputs1]
unary f = map (\a -> [a, f a]) inputs1
binary f = map (\(a,b) -> [a, b, f a b]) inputs2
table name xs = map (map pad) . (header :) $ map (map show) xs
where header = map (:[]) (take ((length $ head xs) - 1) ['A'..]) ++ [name]
pad s = s ++ replicate (5 - length s) ' '
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Clojure programming language
You may also check:How to resolve the algorithm Permutations step by step in the Tcl programming language
You may also check:How to resolve the algorithm Last letter-first letter step by step in the REXX programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Gambas programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Pharo programming language