## Logic

### Logic operators

You may build propositions using the following operators:

• or (or ||). Disjunction.
• and (or &&). Conjunction.
• xor. Exclusive or.
• ~ (or !). Negation.
• -> (or then). Implication.
• iff. Equivalence.

In addition, you may use parenthesis in order to control the order of evaluations. Logic operators are seamlessly integrated with algebraic operators.

Examples:

true or false
true
true -> false
false
3<9 or 2==3
true
p=true; q=false; r=true
true
false
true
p->(q->~r)
true

### Precedence and associativity

All logic operators associate from left to right except the negation operator which associates from right to left.

 Operator Precedence Associativity ~ highest right to left or, and, xor left to right ->, iff lowest left to right

Operators or and and have lower precedence than all of the algebraic and relational operators. This means that parenthesis are usually not necessary as shown in the following lines.

3<4 and 1+4<8
true

Negation (~) has the same high priority as the unary minus. Hence, parenthesis are necessary in order to negate an algebraic expression.

Example,

~(3<5)
false
~3<5
Unary operator "~" of a "rational" is not implemented.
In line 1.

### Using functions

The most practical way to create and study propositions is to create functions which map a set of boolean values to a single boolean value.

For example, to study:

P or (~P and Q)->( P and Q )

create a function as follows:

f(p,q) = p or (~p and q) -> (p and q)
f(p,q) = p or (~p and q) -> (p and q)

Now you may evaluate the function for any combination of values:

f(true,false)
false

### Using truth tables

Function truthtable generates, evaluates and prints a truth table for a given logic function.

For example, let's assume you forgot the truth table of the -> operator. You may proceed as follows:

f(p,q)=p->q
f(p,q)=p->q
truthtable(f)
p q     p->q
T T     T
T F     F
F T     T
F F     T

A more elaborate example taken from www.philosophypages.com follows (click Logic and then click Validity).

Assume we have these premises:

P1:   A and (B and C)
P2:   (B and A) -> ( D or (~E iff F ))
P3:   ~D and ~(G or E)

and we want to prove that,

C:   ~(G or ~F)

Then, build a function with this structure:

P1 and P2 and P3 -> C

Let's call it f:

f(a,b,c,d,e,f,g)= (a and (b and c)) and ((b and a) -> (d or (~e iff f ))) and (~d and ~(g or e)) -> ~(g or ~f)
f(a,b,c,d,e,f,g)= (a and (b and c)) and ((b and a) -> (d or (~e iff f ))) and (~d and ~(g or e)) -> ~(g or ~f)

The truth table is found below. Observe it is a tautology.

truthtable(f)
a b c d e f g   (a and (b and c)) and ...
T T T T T T T   T
T T T T T T F   T
T T T T T F T   T
T T T T T F F   T
T T T T F T T   T
T T T T F T F   T
T T T T F F T   T
T T T T F F F   T
T T T F T T T   T
T T T F T T F   T
T T T F T F T   T
T T T F T F F   T
T T T F F T T   T
T T T F F T F   T
T T T F F F T   T
T T T F F F F   T
T T F T T T T   T
T T F T T T F   T
T T F T T F T   T
T T F T T F F   T
T T F T F T T   T
T T F T F T F   T
T T F T F F T   T
T T F T F F F   T
T T F F T T T   T
T T F F T T F   T
T T F F T F T   T
T T F F T F F   T
T T F F F T T   T
T T F F F T F   T
T T F F F F T   T
T T F F F F F   T
T F T T T T T   T
T F T T T T F   T
T F T T T F T   T
T F T T T F F   T
T F T T F T T   T
T F T T F T F   T
T F T T F F T   T
T F T T F F F   T
T F T F T T T   T
T F T F T T F   T
T F T F T F T   T
T F T F T F F   T
T F T F F T T   T
T F T F F T F   T
T F T F F F T   T
T F T F F F F   T
T F F T T T T   T
T F F T T T F   T
T F F T T F T   T
T F F T T F F   T
T F F T F T T   T
T F F T F T F   T
T F F T F F T   T
T F F T F F F   T
T F F F T T T   T
T F F F T T F   T
T F F F T F T   T
T F F F T F F   T
T F F F F T T   T
T F F F F T F   T
T F F F F F T   T
T F F F F F F   T
F T T T T T T   T
F T T T T T F   T
F T T T T F T   T
F T T T T F F   T
F T T T F T T   T
F T T T F T F   T
F T T T F F T   T
F T T T F F F   T
F T T F T T T   T
F T T F T T F   T
F T T F T F T   T
F T T F T F F   T
F T T F F T T   T
F T T F F T F   T
F T T F F F T   T
F T T F F F F   T
F T F T T T T   T
F T F T T T F   T
F T F T T F T   T
F T F T T F F   T
F T F T F T T   T
F T F T F T F   T
F T F T F F T   T
F T F T F F F   T
F T F F T T T   T
F T F F T T F   T
F T F F T F T   T
F T F F T F F   T
F T F F F T T   T
F T F F F T F   T
F T F F F F T   T
F T F F F F F   T
F F T T T T T   T
F F T T T T F   T
F F T T T F T   T
F F T T T F F   T
F F T T F T T   T
F F T T F T F   T
F F T T F F T   T
F F T T F F F   T
F F T F T T T   T
F F T F T T F   T
F F T F T F T   T
F F T F T F F   T
F F T F F T T   T
F F T F F T F   T
F F T F F F T   T
F F T F F F F   T
F F F T T T T   T
F F F T T T F   T
F F F T T F T   T
F F F T T F F   T
F F F T F T T   T
F F F T F T F   T
F F F T F F T   T
F F F T F F F   T
F F F F T T T   T
F F F F T T F   T
F F F F T F T   T
F F F F T F F   T
F F F F F T T   T
F F F F F T F   T
F F F F F F T   T
F F F F F F F   T