Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

18
Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal

Transcript of Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Page 1: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Predicate Logic for software Engineers

Sagnik Bhattacharya

Siddharth Dalal

Page 2: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Why logic in software engineering?

• Engineers need mathematical tools.

• Theoretical, yet quantitative study of software.

• Parnas says so….

From a drop of water a logician could infer the possibility of an Atlantic or a Niagara without having seen or heard of one or the other. - Sir Arthur Conan Doyle

Page 3: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Problems with existing logic

• Logicians are conservative?

• Lacks precise meaning for logical expression. (* values)

• Complexity of expressions.

• Assumption of total functions.

There can never be surprises in logic.

Page 4: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Parnas Proposes….

• Get rid of *(maybe) values.

• Use partial functions.

• Make use of bounded quantification optional.

'Contrariwise', continued Tweedledee, 'if it was so, it might be, and if it were so, it would be; but as it isn't, it ain't. That's logic!' - Lewis Carroll - Alice in Wonderland

Page 5: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Basics

• Predicate – A predicate is a feature which we can use to make a statement about something.

“Peter is tall”• The property that an animal is a dog can be

expressed by the predicate:

dog(x)

This predicate holds for all animals x which are dogs. For other animals, the predicate is false

Logic is the art of going wrong with confidence.

Page 6: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Propositional Logic

• A proposition  is a language construct which can be assigned a truth value

• More complex propositions can be formed by applying the logical operators (¬, , , etc..)

• Propositional logic formula– Eg. ¬((a b) c)

Against logic there is no armor like ignorance.

Page 7: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Predicate Logic

• Predicate logic  is a more powerful formalism which is obtained by extending propositional logic with

1. “Predicates”  which describe properties or relations of objects in a certain domain ;

2. “variables”  ranging over arbitrary domains;

3. the possibility to quantify over “variables”.

Logic: The art of thinking and reasoning in strict accordance with the limitations and incapacities of the human misunderstanding.

Page 8: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Quantifiers

• We distinguish two quantifiers:– universal quantifier: – existential quantifier:

The grand aim of all science is to cover the greatest number of empirical facts by logical deduction from the smallest number of hypotheses or axioms.

Page 9: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Quantifiers

• We distinguish two quantifiers:– universal quantifier: – existential quantifier:

• Example:– There exists an integer x divisible by 5.

x | divisible_by(x,5)– All integers y are divisible by 1.

y | divisible_by(y,1)

The grand aim of all science is to cover the greatest number of empirical facts by logical deduction from the smallest number of hypotheses or axioms.

Page 10: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Tuples

• U = {true, false}

• Simple n-tuple – Ordered list of n members of U

Eg. <true, false, true, true>

• n-tuple– Ordered list of n simple tuples– Eg. <<true, false>, true, <false, true>>

Logic works, metaphysics contemplates.

Page 11: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Exercise

• <<true, false, <true, false>>, true, <true, <false, true>>>

– 3-tuple (not simple)

• “true”, “<true>”, “<<true>>”

– Equivalent simple 1-tuple!!

The fact that logic cannot satisfy us awakens an almost insatiable hunger for the irrational.

Page 12: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Relations and Functions

• Difference?– Function can’t be one-to-many.

• Partial vs. Total Functions– Partial functions not defined for all values in

domaine.g. Division over integer domain not defined for 0

The want of logic annoys. Too much logic bores. Life eludes logic, and everything that logic alone constructs remains artificial and forced.

Page 13: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Predicates Revisited

• Predicate – function whose range contains no member other than true and false

• Characteristic Predicate (R)– X=set of simple tuples

– Sk=set of all simple k-tuples

– S=S1S2 …….. Su

• u=length of largest tuple in X

– R is a predicate whose domain is S and for every b X the value of R is true

There can never be surprises in logic.

Page 14: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Primitive Expressions

• Primitive Expression is a string of the form R(V)– R is a characteristic predicate– V is a list of comma separated terms (called

arguments)

e.g. divisible_by(x, 5)

Logic is like the sword--those who appeal to it, shall perish by it.

Page 15: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Predicate Expressions

• All primitive expressions are predicate expressions.

• If P, Q: predicate expressions, x: variable, then following are also predicate expressions: (x , P) – (P)– (P) (Q) – (P) (Q)– ¬(P)

• There are no other predicate expressions.– What about (x , P) ???

Logic is neither a science or an art, but a dodge.

Page 16: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Use in Documentation

B[j]=x true

true false

(i, B[i]=x) ¬(i, B[i]=x)

j|

present=

Logic merely enables one to be wrong with authority.

Page 17: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Use in Documentation

• What next after simple search?– Palindrome

• (m, (i, 0<=i<n B[m+i]=N[m+n-1-i]))

– A7E??

• Where in Documentation?

• What about non-inspection operations?

Logic, like whiskey, loses its beneficial effect when taken in too large quantities.

Page 18: Predicate Logic for software Engineers Sagnik Bhattacharya Siddharth Dalal.

Conclusion

• Keep it Simple– Was the use of this type of logic useful?– What about more complicated programs?– Was it more precise?– Was it easier to understand?

You can only find truth with logic if you have already found truth without it.