7117. Logic Programming. - JulTob/Mathematics GitHub Wiki
Different systems of logic can be programmed in different ways. In a strong-type programming language, such as Ada, we can program a library that adapts to our needs in Logic.
We would need a library that can manage the next functionalities:
-- Logic_Operations.ads
generic
type Logic_Type is private;
package Logic_Operations is
function "not" (Right : Logic_Type) return Logic_Type;
function "and" (Left, Right : Logic_Type) return Logic_Type;
function "or" (Left, Right : Logic_Type) return Logic_Type;
function "xor" (Left, Right : Logic_Type) return Logic_Type;
function "nand" (Left, Right : Logic_Type) return Logic_Type;
function "nor" (Left, Right : Logic_Type) return Logic_Type;
function implies (Left, Right : Logic_Type) return Logic_Type;
function ">" (Left, Right : Logic_Type) return Logic_Type;
function equivalent (Left, Right : Logic_Type) return Logic_Type;
function "<>" (Left, Right : Logic_Type) return Logic_Type;
end Logic_Operations;
Boolean Logic
'Binary_Logic.ads'
-- Binary_Logic.ads
with Logic_Operations;
package Binary_Logic is new Logic_Operations(Boolean);
'Logic_Operations.adb'
-- Logic_Operations.adb
package body Logic_Operations is
function "not" (Right : Logic_Type) return Logic_Type is
begin
return not Right;
end "not";
function "and" (Left, Right : Logic_Type) return Logic_Type is
begin
return Left and Right;
end "and";
function "or" (Left, Right : Logic_Type) return Logic_Type is
begin
return Left or Right;
end "or";
function "xor" (Left, Right : Logic_Type) return Logic_Type is
begin
return Left xor Right;
end "xor";
function "nand" (Left, Right : Logic_Type) return Logic_Type is
begin
return not (Left and Right);
end "nand";
function "nor" (Left, Right : Logic_Type) return Logic_Type is
begin
return not (Left or Right);
end "nor";
function Implies (Left, Right : Logic_Type) return Logic_Type is
begin
if Left then
Return Right;
else
Return True;
end if;
end Implies;
function ">" (Left, Right : Logic_Type) return Logic_Type is
begin
return Implies(Left,Right)
end ">"
function Equivalent (Left, Right : Logic_Type) return Logic_Type is
begin
return (Left and Right) or (not Left and not Right);
end Equivalent;
function "<>" (Left, Right : Logic_Type) return Logic_Type is
begin
return Equivalent(Left,Right)
end "<>"
end Logic_Operations;
Ternary Logic
'Ternary_Logic_Type.ads'
-- Ternary_Logic_Type.ads
type Ternary_Logic_Type is (False, Unknown, True);
'Ternary_Logic_Operations.ads'
-- Ternary_Logic_Operations.ads
with Logic_Operations; -- The generic package you've previously defined
with Ternary_Logic_Type; -- The new ternary logic type definition
package Ternary_Logic_Operations is new Logic_Operations(Ternary_Logic_Type.Ternary_Logic_Type);
'Ternary_Logic_Operations.adb'
-- Ternary_Logic_Operations.adb
with Ternary_Logic_Type;
use Ternary_Logic_Type;
package body Ternary_Logic_Operations is
subtype Logic_Type is Ternary_Logic_Type;
function "not" (Right : Logic_Type) return Logic_Type is
begin
case Right is
when False => return True;
when Unknown => return Unknown;
when True => return False;
end case;
end "not";
function "and" (Left, Right : Logic_Type) return Logic_Type is
begin
if Left = False or Right = False then
return False;
elsif Left = Unknown or Right = Unknown then
return Unknown;
else
return True;
end if;
end "and";
function "or" (Left, Right : Logic_Type) return Logic_Type is
begin
if Left = True or Right = True then
return True;
elsif Left = Unknown or Right = Unknown then
return Unknown;
else
return False;
end if;
end "or";
function "xor" (Left, Right: Logic_Type) return Logic_Type is
begin
if (Left = True and Right = False) or (Left = False and Right = True) then
return True;
elsif Left = Unknown or Right = Unknown then
return Unknown;
else
return False;
end if;
end "xor";
function "nand" (Left, Right : Logic_Type) return Logic_Type is
begin
return not (Left and Right);
end "nand";
function "nor" (Left, Right : Logic_Type) return Logic_Type is
begin
return not (Left or Right);
end "nor";
function Implies (Left, Right : Logic_Type) return Logic_Type is
-- a>b | T | U | F
-- T | T | U | F
-- U | T | U | U
-- F | T | T | T
begin
if Left = False or Right = True then
Return True;
elsif Left = Unknown or Right = Unknown then
Return Unknown;
else
Return False;
end if;
end Implies;
function ">" (Left, Right : Logic_Type) return Logic_Type is
begin
return Implies(Left,Right)
end ">"
function Equivalent (Left, Right : Logic_Type) return Logic_Type is
-- a=b | T | U | F
-- T | T | U | F
-- U | U | U | U
-- F | F | U | T
begin
if Left = Unknown or Right = Unknown then
Return Unknown;
elsif Left = Right then
Return True;
else
Return False;
end if;
end Equivalent;
function "<>" (Left, Right : Logic_Type) return Logic_Type is
begin
return Equivalent(Left,Right)
end "<>"
end Logic_Operations;
Fuzzy Probability
-- Probability_Logic.ads
package Probability_Logic is
type Probability is digits 15 range 0.0 .. 1.0;
function Calculate_Posterior(Prior, Likelihood, Evidence : Probability) return Probability;
function "not" (Right : Probability) return Probability;
function "and" (Left, Right : Probability) return Probability;
function "or" (Left, Right : Probability) return Probability;
function "xor" (Left, Right : Probability) return Probability;
function "nand" (Left, Right : Probability) return Probability;
function "nor" (Left, Right : Probability) return Probability;
function implies (Left, Right : Probability) return Probability;
function ">" (Left, Right : Probability) return Probability;
function equivalent (Left, Right : Probability) return Probability;
function "<>" (Left, Right : Probability) return Probability;
end Probability_Logic;
-- Probability_Logic.adb
package body Probability_Logic is
function "not" (Right : Probability) return Probability is
begin
return 1.0 - Right;
end "not";
function "and" (Left, Right : Probability) return Probability is
begin
return Left * Right;
end "and";
function "or" (Left, Right : Probability) return Probability is
begin
return Left + Right - (Left * Right);
end "or";
function "xor" (Left, Right : Probability) return Probability is
begin
return Left + Right - 2 * (Left * Right);
end "xor";
function "nand" (Left, Right : Probability) return Probability is
begin
return not (Left and Right);
end "and";
function "nor" (Left, Right : Probability) return Probability is
begin
return not (Left or Right);
end "nor";
function implies (Left, Right : Probability) return Probability is
begin
-- Probability that Right implies Left
return not(Left) or Right
end implies;
function ">" (Left, Right : Probability) return Probability is
begin
return implies(Left, Right);
end ">";
function equivalent (Left, Right : Probability) return Probability is
begin
return (Left and Right) or ((not A) and (not B));
end equivalent;
function "<>" (Left, Right : Probability) return Probability is
begin
return equivalent(Left, Right);
end "<>";
function Calculate_Posterior(Prior, Likelihood, Evidence : Probability) return Probability is
begin
return (Likelihood * Prior) / Evidence;
end Calculate_Posterior;
end Probability_Types;