vicramgon / logicus / LogicUS.PL.SyntaxSemantics

The module provides the elementary tools for working with propositional logic. It allows defining both formulas and sets as well as performing some basic operations on them, such as evaluations regarding interpretations, construction of truth tables, extraction of models and decision of satisfaction, tautology and logical consequence.

Definition Types


type alias PSymb =
( String, List Basics.Int )

It is used to represent the propositional symbols of the formulas. It is recommended to use lowercase alphabetic characters using "_ {...}" to indicate subscripts.

-- Some examples of propositional symbols
simpleSymbs : List PSymb
simpleSymbs =
    [ "a", "b", "p", "q", "jhon", "marie" ]

subindexedSymb : List PSymb
subindexedSymb =
    [ "a_{1}", "p_{2,3}" ]


type FormulaPL
    = Atom PSymb
    | Neg FormulaPL
    | Conj FormulaPL FormulaPL
    | Disj FormulaPL FormulaPL
    | Impl FormulaPL FormulaPL
    | Equi FormulaPL FormulaPL
    | Insat
    | Taut

It is used to define propositional formulas recursively. It allows defining atoms, negations, conjunctions, disjunctions, implications, equivalences, and unsatisfiable formulas.

-- Some examples of definition of propositional formulas
-- f1 = a → b
f1 =
    Impl (Atom "a") (Atom "b")

-- f2 = ¬(a ∧ b) ↔ (¬a ∨ ¬b)
f2 =
    Equi (Neg (Conj (Atom "a") (Atom "b"))) (Disj (Neg (Atom "a")) (Neg (Atom "b")))


type alias Literal =
FormulaPL



It corresponds to an Atom or a negation of an Atom


type alias SetPL =
List FormulaPL

It is used to define sets of propositional formulas.

-- fs = {a → b, ¬(a ∧ b) ↔ (¬a ∨ ¬b)}
fs =
    [ f1, f2 ]


type alias Interpretation =
List PSymb

It is used to give a sparse definition of an Interpretation as a list of PSymb. This definition assumed that symbols including in the list are considered True. The rest are considered False.

-- i1 = {a=0, b=1}
i1 =
    [ "b" ]

-- i2 ={a=1, b=0}
i2 =
    [ "a" ]

-- i3 = {a=1, b=1}
i3 =
    [ "a", "b" ]

Work with PL Formulas

fplIsLiteral : FormulaPL -> Basics.Bool

It checks if a formula is a literal or not.

fplIsLiteral (Atom "p\_{1}") == True
fplIsLiteral (Neg(Atom "p\_{1}")) == True
fplIsLiteral (Disj (Atom "p\_{1}") (Atom "p\_{2}")) == False

fplIsPositiveLiteral : FormulaPL -> Basics.Bool

It checks if a formula is a positive literal or not

fplIsPositiveLiteral (Atom "p\_{1}") == True

fplIsPositiveLiteral (Neg(Atom "p\_{1}")) == False

fplIsPositiveLiteral (Disj (Atom "p\_{1}") (Atom "p\_{2}")) == False

fplIsNegativeLiteral : FormulaPL -> Basics.Bool

It checks if a formula is a negative literal or not

fplIsNegativeLiteral (Atom "p\_{1}") == False

fplIsNegativeLiteral (Neg(Atom "p\_{1}")) == True

fplIsNegativeLiteral (Disj (Atom "p\_{1}") (Atom "p\_{2}")) == False

fplNegation : FormulaPL -> FormulaPL

It gives the negation of a formula.

fplNegation (Neg f1) == f1

fplNegation Insat == Taut

fplNegation f1 = Neg f1

fplSymbols : FormulaPL -> List PSymb

It calculates all the symbols that take place in a formula

fplSymbols f1 == [ "a", "b" ]

fplSymbols f2 == [ "a", "b" ]

fplFormTree : FormulaPL -> Graph FormulaPL ()

It gives a Graph.Graph with the form tree of a formula. If you want visualize it you can use formTreeHTML (defined in module Logicus.Base.Repr.SintaxSemantics.HTML) that gives a dot code of the graph. You can visualize it in a online graphviz visualizer or you can create an html file and follow the instructions (defined in Logicus.PL.Repr.Common.GraphViz).

fplFormTree f1 == Graph.Graph (Inner { left = Inner { left = Leaf { key = 0, value = { incoming = Empty, node = { id = 0, label = Impl (Atom "a") (Atom "b") }, outgoing = Inner { left = Leaf { key = 1, value = () }, prefix = { branchingBit = 2, prefixBits = 0 }, right = Leaf { key = 2, value = () }, size = 2 } } }, prefix = { branchingBit = 1, prefixBits = 0 }, right = Leaf { key = 1, value = { incoming = Leaf { key = 0, value = () }, node = { id = 1, label = Atom "a" }, outgoing = Empty } }, size = 2 }, prefix = { branchingBit = 2, prefixBits = 0 }, right = Leaf { key = 2, value = { incoming = Leaf { key = 0, value = () }, node = { id = 2, label = Atom "b" }, outgoing = Empty } }, size = 3 })

fplValuation : FormulaPL -> Interpretation -> Basics.Bool

It calculates the truth value of a formula regarding to an interpretation.

fplValuation f1 i1 == True

fplValuation f1 i2 == False

fplValuation f2 i3 == True

fplInterpretations : FormulaPL -> List Interpretation

It gives all possible interpretations of a formula.

fplInterpretations f1 == [ [], [ "b" ], [ "a" ], [ "a", "b" ] ]

fplInterpretations f2 == [ [], [ "b" ], [ "a" ], [ "a", "b" ] ]

fplModels : FormulaPL -> List Interpretation

It gives all models of a formula

fplModels f1 == [ [], [ "b" ], [ "a", "b" ] ]

fplModels f2 == [ [], [ "b" ], [ "a" ], [ "a", "b" ] ]

fplCountermodels : FormulaPL -> List Interpretation

It gives all countermodels of a formula

fplCountermodels f1 == [ [ "a" ] ]

fplCountermodels f2 == []

fplModelsCountermodels : FormulaPL -> ( List Interpretation, List Interpretation )

It gives all models and countermodels of a formula as a tuple (models, countermodels).

fplModelsCountermodels f1 == ( [ [], [ "b" ], [ "a", "b" ] ], [ [ "a" ] ] )

fplModelsCountermodels f2 == ( [ [], [ "b" ], [ "a" ], [ "a", "b" ] ], [] )

fplTruthTable : FormulaPL -> List ( Interpretation, Basics.Bool )

It gives the truth table of a formula as a List(Interpretation, Bool). You cab visualize it as a table using the function fplTruthTableHTML (defined in module Logicus.Base.Repr.SintaxSemantics.HTML) or using fplTruthTableMathString (defined in module Logicus.Base.Repr.SintaxSemantics.Latex) and follow the instructions given in the respective module.

fplTruthTable f1 == [ ( [], True ), ( [ "b" ], True ), ( [ "a" ], False ), ( [ "a", "b" ], True ) ]

flpTruthTable f2 == [ ( [], True ), ( [ "b" ], True ), ( [ "a" ], True ), ( [ "a", "b" ], True ) ]

fplSatisfiability : FormulaPL -> Basics.Bool

It gives if a formula is satisfiable (bruteforce)

fplSatisfiability f1 == True

fplSatisfiability f2 == True

fplValidity : FormulaPL -> Basics.Bool

It gives if a formula is a tautology (bruteforce)

fplValidity f1 == False

fplValidity f2 == True

fplUnsatisfiability : FormulaPL -> Basics.Bool

It gives if a formula is unsatisfiable (bruteforce)

fplUnsatisfiability f1 == False

fplUnsatisfiability f2 == False

fplUnsatisfiability Insat == True

Work with PL Sets

splSymbols : List FormulaPL -> List PSymb

It calculates all the symbols that take place in a set of formulas

fplSymbols f2 == [ "a", "b" ]

splValuation : SetPL -> Interpretation -> Basics.Bool

It calculates the truth value of a set of formulas regarding to an interpretation.

splValuation fs i1 == True

splValuation fs i2 == False

splValuation fs i3 == True

splInterpretations : SetPL -> List Interpretation

It gives all possible interpretations of a set of formulas.

splInterpretations fs == [ [], [ "b" ], [ "a" ], [ "a", "b" ] ]

splModels : SetPL -> List Interpretation

It gives all models of a set of formulas

splModels fs == [ [], [ "b" ], [ "a", "b" ] ]

splCountermodels : SetPL -> List Interpretation

It gives all countermodels of a set of formulas

splCountermodels fs == [ [ "a" ] ]

splModelsCountermodels : SetPL -> ( List Interpretation, List Interpretation )

It gives all models and countermodels of a set of formulas as a tuple (models, countermodels).

splModelsCountermodels fs == ( [ [], [ "b" ], [ "a", "b" ] ], [ [ "a" ] ] )

splTruthTable : SetPL -> List ( Interpretation, Basics.Bool )

It gives the truth table of a set of formulas as a List(Interpretation, Bool). You can visualize it as a table using the function splTruthTableHTML (defined in module Logicus.Base.Repr.SintaxSemantics.HTML) or using splTruthTableLatex (defined in module Logicus.Base.Repr.SintaxSemantics.Latex) and follow the instructions given in the respective module.

splTruthTable fs == [ ( [], True ), ( [ "b" ], True ), ( [ "a" ], False ), ( [ "a", "b" ], True ) ]

splSatisfiability : SetPL -> Basics.Bool

It gives if a set of formulas is satisfiable (bruteforce)

splSatisfiability fs == True

splSatisfiability (fs ++ [ Atom "a", Neg (Atom "b") ]) == False

splUnsatisfiability : SetPL -> Basics.Bool

It gives if a set of formulas is unsatisfiable (bruteforce)

splUnsatisfiability fs == False

splUnatisfiability (fs ++ [ Atom "a", Neg (Atom "b") ]) == True

logicalConsecuence : SetPL -> FormulaPL -> Basics.Bool

It gives if a if a formula F is consecuence of a set of formulas S checking if all model of S is also model of F. (bruteforce)

logicalConsecuence fs (Disj (Neg (Atom "a")) (Atom "b")) == True

logicalConsecuence fs Insat == False

logicalConsecuence [ Insat ] f1 == True -- You can deduce anything of an unsatifiable set.

logicalConsecuence2 : SetPL -> FormulaPL -> Basics.Bool

It gives if a if a formula F is consecuence of a set of formulas S checking if S U {F} is unsatisfiable. (bruteforce)

logicalConsecuence2 fs (Disj (Neg (Atom "a")) (Atom "b")) == True

logicalConsecuence2 fs Insat == False

logicalConsecuence2 [ Insat ] f1 == True -- You can deduce anything of an unsatifiable set.

Parsing PL Formulas

fplReadFromString : String -> ( Maybe FormulaPL, String, String )

It reads the formula from a string.It returns a tuple with may be a formula (if it can be read it), the input considerated to parse and a message of error it it is not able to performs the parsing.

The string must satisfy the following rules:

fplReadFromString "p" == ( Just (Atom "p"), "p", "" )

fplReadFromString "p_{1} & p_{2}" == ( Just (Conj (Atom "p_{1}") (Atom "p_{2}")), "(p_{1}&p_{2})", "" )

fplReadFromString "(p | q -> r)" == ( Just (Impl (Disj (Atom "p") (Atom "q")) (Atom "r")), "((p|q)->r)", "" )

fplReadFromString "p_{1,1} <-> p_{1,2}" == ( Just (Equi (Atom "p_{1,1}") (Atom "p_{1,2}")), "(p_{1,1}<->p_{1,2})", "" )

fplReadFromString "!F" == ( Just Insat, "!F", "" )

fplReadFromString "p_1" == ( Nothing, "(p_1)", "Error: [{ col = 3, problem = ExpectingSymbol ')', row = 1 }]" )

Messages are not perfect but we're working to improve it.

fplReadExtraction : ( Maybe FormulaPL, String, String ) -> FormulaPL

It extract the formula readed. If it is Nothing it returns Insat

f3 = fplReadExtraction <| fplReadFromString "(p | q -> r)"
f3 == Impl (Disj (Atom "p") (Atom "q")) (Atom "r")
f4 = fplReadExtraction <| fplReadFromString "(p | q <- r)"
f4 == Insat

fplToInputString : FormulaPL -> String

It gives the corresponding input syntax of a formula

fplToInputString f3 == "((p|q)->r)"

fplToInputString f4 == "_|_"

Parsing Interpretations

interpretationReadFromString : String -> ( Maybe Interpretation, String )

It reads an interpretation from a string that represents the list of propositional variables (following the rules given in fplReadFromString function) separated by commas and surrounded by [ at the beginning and ] at the end.

interpretationReadExtraction : ( Maybe Interpretation, String ) -> Interpretation

It extract the interpretation readed. If it is Nothing it returns an empty list (all vars taking as false).

Repesentation for PL Formulas

fplToString : FormulaPL -> String

It generates the String representation of a PL formula using unicode symbols.

fplToString f1 == "( a → b )"

fplToString f2 == "( ¬ ( a ∧ b ) ↔ ( ¬ a ∨ ¬ b ) )"

fplToMathString : FormulaPL -> String

It generates the Latex string of a PL formula. The result requires a math enviroment to be displayed.

fplToMathString f1 == "( a\\rightarrow b )"

fplToMathString f2 == "( \\neg ( a \\wedge b ) \\leftrightarrow ( \\neg a \\vee \\neg b ) )"

fplTruthTableString : FormulaPL -> String

It generates the Truth Table of a PL formula as a string using CSV format (separated by ';')

fplTruthTableString f1 == "a ; b ; ( a → b ) \nF ; F ; T \nF ; T ; T \nT ; F ; F \nT ; T ; T""
fplTruthTableString f2 == "a ; b ; ( ¬ ( a ∧ b ) ↔ ( ¬ a ∨ ¬ b ) ) \nF ; F ; T \nF ; T ; T \nT ; F ; T \nT ; T ; T"

fplTruthTableMathString : FormulaPL -> String

It generates the Latex code of a Truth Table of a PL formula. The result requires a math enviroment to be displayed.

fplTruthTableMathString f1 == "\\begin{array}{c|c|c|}\n\mathbf{a} & \\mathbf{b} & \\mathbf{( a\\rightarrow b )} \\\\ \\hline \nF & F & T \\\\ \nF & T & T \\\\ \nT & F & F \\\\ \nT & T & T \\\\ \\end{array}"

fplTruthTableMathString f2 == "\\begin{array}{c|c|c|}\n\n\\mathbf{a} & \\mathbf{b} & \\mathbf{( \\neg ( a \\wedge b )\\leftrightarrow ( \\neg a \\vee \\neg b ) )} \\\\\\hlineF & F & T \\\\ \nF & T & T \\\\ \nT & F & T \\\\ \nT & T & T \\\\ \\end{array}"

fplFormTreeToString : Graph FormulaPL () -> String

It gives the String representation of a formTree.

fplFormTreeToString <| fplFormTree f3 == "0 ( ( p ∨ q ) → r )\n1 ( p ∨ q )\n2 p\n3 q\n4 r\n#\n0 1\n0 4\n1 2\n1 3"

fplFormTreeToDOT : Graph FormulaPL () -> String

It generates the formation tree of a formula as DOT string. It requires a GraphViz interpreter to be displayed.

fplFormTreeToDOT <| fplFormTree f3 == "digraph G {\n  rankdir=TB\n  graph []\n  node [shape=plaintext, color=black]\n  edge [dir=none]\n\n  0 -> 1\n  0 -> 4\n  1 -> 2\n  1 -> 3\n\n  0 [label=\"( ( p ∨ q ) → r )\"]\n  1 [label=\"( p ∨ q )\"]\n  2 [label=\"p\"]\n  3 [label=\"q\"]\n  4 [label=\"r\"]\n}"

Representation for PL Sets

splToString : SetPL -> String

It generates the String of Set of PL formulas using unicode symbols.

splToString [ f1, f2 ] == "{( a → b ), ( ¬ ( a ∧ b ) ↔ ( ¬ a ∨ ¬ b ) )}"

splToMathString : List FormulaPL -> String

It generates the Latex string of a Set of PL formulas. The result requires a math enviroment to be displayed.

splToMathString [ f1, f2 ] == "\\lbrace ( a\\rightarrow b ), ( \\neg ( a \\wedge b )\\leftrightarrow ( \\neg a \\vee \\neg b ) )\\rbrace"

splToMathString2 : List FormulaPL -> String

It generates the Latex string of a Set of PL formulas in one line, avoiding the use of the array. The result requires a math enviroment to be displayed.

splTruthTableString : SetPL -> String

It generates the Truth Table of a set of PL formulas as a string using CSV format.

splTruthTableString [ f1, f2 ] == "a ; b ; ( a → b ) ; ( ¬ ( a ∧ b ) ↔ ( ¬ a ∨ ¬ b ) ) ; U \n0 ; 0 ; T ; T ; T \n0 ; 1 ; T ; T ; T \n1 ; 0 ; F ; T ; F \n1 ; 1 ; T ; T ; T"

splTruthTableMathString : SetPL -> String

It generates the Latex code of a Truth Table of Set of PL formulas. The result requires a math enviroment to be displayed.

splTruthTableMathString [ f1, f2 ] =
    "\\begin{array}{c|c|c|c|c|}\\mathbf{a} & \\mathbf{b} & \\mathbf{( a \\rightarrow b )} & \\mathbf{( \\neg ( a \\wedge b ) \\leftrightarrow ( \\neg a \\vee \\neg b ) )} & \\mathbf{U} \\\\ \\hlineF & F & T & T & T \\\\ F & T & T & T & T \\\\ T & F & F & T & F \\\\ T & T & T & T & T \\\\ \\end{array}"

splCompactTruthTableString : SetPL -> String

It generates the Truth Table of a set of PL formulas as a string using CSV format. It only shows the truth values of variables and the evaluation of the set.

splCompactTruthTableMathString : SetPL -> String

It generates the Latex code of a Truth Table of Set of PL formulas. It only shows the truth values of the variables and the evaluation of the set. The result requires a math enviroment to be displayed.

Representation for Interpretations

interpretationToString : Interpretation -> List PSymb -> String

It gives a interpretation as a string.

 interpretationToString ["a"] ["a", "b"] == "{a:T, b:F}"

interpretationsToString : List Interpretation -> List PSymb -> String

It gives a interpretation list as a string.

interpretationsToString [ [], [ "a" ], [ "a", "b" ] ] [ "a", "b", "c" ]
    == "{{a:F, b:F, c:F}\n{a:T, b:F, c:F}\n{a:T, b:T, c:F}}"

interpretationToMathString : Interpretation -> List PSymb -> String

It gives a interpretation as a string in latex format.

interpretationToMathString [ "a" ] [ "a", "b" ] == "\\lbrace a:T, b:F \\rbrace"

interpretationsToMathString : List Interpretation -> List PSymb -> String

It gives a interpretation list as a string in latex format.

interpretationsToMathString [ [], [ "a" ], [ "a", "b" ] ] [ "a", "b", "c" ]
    == "\\begin{array}{c}\\lbrace a:F, b:F, c:F \\rbrace \\\\ \\lbrace a:T, b:F, c:F \\rbrace \\\\ \\lbrace a:T, b:T, c:F \\rbrace \\end{array}"

Other functions

interpretationsFromSymbolsAndLiterals : List PSymb -> List Literal -> List Interpretation

It calculates the list of interpretations from a list of symbols and a list of literals taking each symbol as true if it apears as positive literal, as negative if it apears as negative literal and indiferent if it doesn't appear.

splConjunction : SetPL -> FormulaPL

It transforms a SetPL into a FormulaPL using conjuction between formulas. If Set is empty Taut is given

splDisjunction : SetPL -> FormulaPL

It transforms a SetPL into a FormulaPL using disjunction between formulas. If Set is empty Taut is given