Jupyter Notebook Viewer

In a Logic course, one exercise is to turn an English sentence like this:

Sieglinde will survive, and either her son will gain the Ring and Wotan’s plan will be fulfilled or else Valhalla will be destroyed.

Into a formal Propositional Logic statement:

P ⋀ ((Q ⋀ R) ∨ S)

along with definitions of the propositions:

P: Sieglinde will survive
Q: Sieglinde’s son will gain the Ring
R: Wotan’s plan will be fulfilled
S: Valhalla will be destroyed

For some sentences, it takes detailed knowledge to get a good translation. The following two sentences are ambiguous, with different prefered interpretations, and translating them correctly requires knowledge of eating habits:

I will eat salad or I will eat bread and I will eat butter. P ∨ (Q ⋀ R)
I will eat salad or I will eat soup and I will eat ice cream. (P ∨ Q) ⋀ R

But for many sentences, the translation process is automatic, with no special knowledge required. I will develop a program to handle these easy sentences. The program is based on the idea of a series of translation rules of the form:

Rule(‘{P} ⇒ {Q}’, ‘if {P} then {Q}’, ‘if {P}, {Q}’)

which means that the logic translation will have the form ‘P ⇒ Q’, whenever the English sentence has either the form ‘if P then Q’ or ‘if P, Q’, where P and Q can match any non-empty subsequence of characters. Whatever matches P and Q will be recursively processed by the rules. The rules are in order—top to bottom, left to right, and the first rule that matches in that order will be accepted, no matter what, so be sure you order your rules carefully. One guideline I have adhered to is to put all the rules that start with a keyword (like ‘if’ or ‘neither’) before the rules that start with a variable (like ‘{P}’); that way you avoid accidently having a keyword swallowed up inside a ‘{P}’.

Notice that given the sentence “Sieglinde will survive”, the program should make up a new propositional symbol, P, and record the fact that P refers to “Sieglinde will survive”. But the negative sentence “Sieglinde will not survive”, should be translated as ~P, where again P is “Sieglinde will survive”. So to fully specify the translation process, we need to define both rules and negations. (We do that using regular expressions, which can sometimes be confusing.)

First the function to define a rule (and some auxiliary functions):

Leave a Reply

Your email address will not be published.