Fundamentals of Programming Languages I Introduction and Logics - - PowerPoint PPT Presentation

fundamentals of programming languages i
SMART_READER_LITE
LIVE PREVIEW

Fundamentals of Programming Languages I Introduction and Logics - - PowerPoint PPT Presentation

Fundamentals of Programming Languages I Introduction and Logics Guoqiang Li School of Software, Shanghai Jiao Tong University Instructor and Teaching Assistants Guoqiang LI Homepage: http://basics.sjtu.edu.cn/liguoqiang Course


slide-1
SLIDE 1

Fundamentals of Programming Languages I

Introduction and Logics Guoqiang Li

School of Software, Shanghai Jiao Tong University

slide-2
SLIDE 2

Instructor and Teaching Assistants

  • Guoqiang LI
  • Homepage: http://basics.sjtu.edu.cn/˜liguoqiang
  • Course page:

http://basics.sjtu.edu.cn/˜liguoqiang/teaching/Prog17/index.htm

  • Email: li.g@outlook.com
  • Office: Rm. 1212, Building of Software
  • Phone: 3420-4167
  • TA:
  • Yuwei WANG: wangyuwei95 (AT) qq (DOT) com
  • Office hour: Tue. 14:00-17:00 @ Software Building 3203
slide-3
SLIDE 3

What does the lecture aim for?

slide-4
SLIDE 4

Similar Lectures I

Fundamentals of Programming Languages by University of Colorado Boulder http://www.cs.colorado.edu/˜bec/courses/csci5535-f13/

  • 2010 Spring Programming semantics
  • 2013 Fall Programming analysis and verification
slide-5
SLIDE 5

Similar Lectures II

Principles of Programming Languages by University of Oxford http://www.cs.ox.ac.uk/teaching/courses/2017-2018/principles/ Foundations of Programming Languages by CMU www.cs.cmu.edu/˜rjsimmon/15312-s14/schedule.html Theory of Programming Languages by ECNU basics.sjtu.edu.cn/˜yuxin/teaching/Semantics/sem.html Programming Semantics

slide-6
SLIDE 6

Similar Lectures III

Fundamentals of Programming Analysis by MIT

  • cw.mit.edu/courses/electrical-engineering-and-computer-science/6-

820-fundamentals-of-program-analysis-fall-2015/lecture-notes/ Principles of Programming Languages by Boston University http://www.cs.bu.edu/˜hwxi/academic/courses/CS520/Fall15 Programming Analysis and Verification

slide-7
SLIDE 7

Similar Lectures IV

Theory of Programming Languages by CMU www.cs.cmu.edu/ aldrich/courses/15-819O-13sp Introduction to Programming Languages Theory by Standford https://courseware.stanford.edu/pg/courses/lectures/261141 Theory of Programming Languages by SJTU http://basics.sjtu.edu.cn/˜xiaojuan/tapl2016/index.html Types and Functional Programming Languages

slide-8
SLIDE 8

Fundamental Requirements

  • Program Verification and Analysis
  • Propositional logic, predicate logic etc.
  • Automata theory, DFA, NFA, PDS, PN etc.
  • Algorithm.
  • Program Semantics
  • Set theory.
  • Algebra theory, group, ring, domain etc.
  • category theory, maybe...
  • Types and Programming Languages
  • Logic
  • Computability theory
  • Lambda calculus theory...
slide-9
SLIDE 9

Fundamental of Fundamental

Several theories in theoretical computer science are given, which is a minimal requirement and self-contained in this lecture. All of three directions are taught, which only include very fundamental part, if time permitted. As simple as possible, although it is very theoretical.

slide-10
SLIDE 10

Lecture Agenda

  • Introduction and logic basics (1 lecture)
  • Formal basics (3 lectures)
  • Model checking
  • Finite and Büchi automata
  • LTL model checking
  • Programming verification (2 or 3 lectures)
  • Abstract interpretation
  • Pushdown automata and interprocedural programs
  • Petri Net and concurrent programs
  • Exercise I. (1 lecture)
  • Programming semantics (2 lectures)
  • Denotational semantics
  • Operational semantics
  • Axiomatic semantics
  • Basic functional programming (3 lectures)
  • Lambda calculus
  • Simple types
  • Functional programming
  • Exercise II. (1 lecture)
slide-11
SLIDE 11

References

No particular textbook that can cover all the parts. Here are three Reference books:

Edmund M. Clarke Jr., Orna Grumberg, Doron A. Peled. Model Checking. MIT Press, 1999 Glynn Winskel. Formal Semantics of Programming Languages: An Introduction. MIT Press, 1993 Benjamin C. Pierce. Types and Programming Languages. MIT Press, 2002

+ Several famous papers + Lecture notes shared in the course webpage.

slide-12
SLIDE 12

Scoring Policy

  • 10% Attendance.
  • 20% Homework.
  • Four assignments.
  • Each one is 5pts.
  • Work out individually.
  • Each assignment will be evaluated by A, B, C, D, F (Excellent(5),

Good(5), Fair(4), Delay(3), Fail(0))

  • 70% Final exam.
  • Maybe replaced by report, if the condition is satisfied!
slide-13
SLIDE 13

Any Questions?

slide-14
SLIDE 14

Logic Basics

slide-15
SLIDE 15

Brief Historical Notes on Logic

slide-16
SLIDE 16

Historical View

  • Philosophical Logic
  • 500 BC to 19th Century
  • Symbolic Logic
  • Mid to late 19th Century
  • Mathematical Logic
  • Late 19th to mid 20th Century
  • Logic in Computer Science
slide-17
SLIDE 17

Philosophical Logic

500 B.C - 19th Century Logic dealt with arguments in the natural language used by humans. Example:

  • All men are mortal.
  • Socrates is a man.
  • Therefore, Socrates is mortal.
slide-18
SLIDE 18

Philosophical Logic

Natural languages are very ambiguous.

  • Eric does not believe that Mary can pass any test.
  • does not believe that she can pass some test, or
  • does not believe that she can pass all tests
  • I only borrowed your car.
  • And not ‘borrowed and used’, or
  • And not ‘car and coat’
  • Tom hates Jim and he likes Mary.
  • Tom likes Mary, or
  • Jim likes Mary

It led to many paradoxes.

  • “This sentence is a lie.”(The Liar’s Paradox)
slide-19
SLIDE 19

Sophism

…Sophism generally refers to a particularly confusing, illogical and/or insincere argument used by someone to make a point, or, perhaps, not to make a point. Sophistry refers to […] rhetoric that is designed to appeal to the listener on grounds other than the strict logical cogency of the statements being made.

slide-20
SLIDE 20

The Sophist’s Paradox

A Sophist is sued for his tuition by the school that educated him. He argues that he must win, since, if he loses, the school didn’t educate him well enough, and doesn’t deserve the money. The school argues that he must lose, since, if he wins, he was educated well enough, and therefore should pay for it.

slide-21
SLIDE 21

Logic in Computer Science

Logic has a profound impact on computer science. Some examples:

  • Propositional logic - the foundation of computers and circuitry
  • Databases - query languages
  • Programming languages (e.g. prolog)
  • Design Validation and verification
  • AI (e.g. inference systems)
slide-22
SLIDE 22

Logic in Computer Science

Propositional Logic First Order Logic Higher Order Logic Temporal Logic …

slide-23
SLIDE 23

Propositional Logic: Syntax

slide-24
SLIDE 24

Propositional Logic

A proposition: a sentence that can be either true or false. Propositions:

  • x is greater than y
  • Noam wrote this letter
slide-25
SLIDE 25

Propositional Logic: Syntax

The symbols of the language:

  • Propositional symbols (Prop): A, B, C, . . .
  • Connectives:
  • ∧ and
  • ∨ or
  • ¬ not
  • → implies
  • ↔ equivalent to
  • ⊕ xor (different than)
  • ⊥, ⊤ False, True
  • Parenthesis: (, ).

Q1: How many different binary symbols can we define? Q2: What is the minimal number of such symbols?

slide-26
SLIDE 26

Formulas

Grammar of well-formed propositional formulas Formula := prop | ¬(Formula) | (Formula ◦ Formula) where prop ∈ Prop and ◦ is one of the binary relations.

slide-27
SLIDE 27

Formulas

Examples of well-formed formulas:

  • (¬A)
  • (¬(¬A))
  • (A ∧ (B ∧ C))
  • (A → (B → C))

Correct expressions of Propositional Logic are full of unnecessary parenthesis.

slide-28
SLIDE 28

Formulas: Abbreviations

We write A ◦ B ◦ C ◦ . . . in place of (A ◦ (B ◦ (C ◦ . . .))) Thus, we write A ∧ B ∧ C, A → B → C, . . . in place of (A ∧ (B ∧ C)), (A → (B → C)), . . .

slide-29
SLIDE 29

Formulas: Abbreviations

We omit parenthesis whenever we may restore them through operator precedence: ¬ binds more strictly than ∧, ∨, and ∧, ∨ bind more strictly than →, ↔. Thus, we write:

  • ¬¬A for (¬(¬A)),
  • ¬A ∧ B for ((¬A) ∧ B)
  • A ∧ B → C for ((A ∧ B) → C)
slide-30
SLIDE 30

Propositional Logic: Semantics

slide-31
SLIDE 31

Propositional Logic: Semantics

Truth tables define the semantics (=meaning) of the operators Convention: 0 = false, 1 = true A B A ∧ B A ∨ B A → B 1 1 1 1 1 1 1 1 1 1 1

slide-32
SLIDE 32

Propositional Logic: Semantics

Truth tables define the semantics (=meaning) of the operators Convention: 0 = false, 1 = true A B ¬A A ↔ B A ⊕ B 1 1 1 1 1 1 1 1 1 1

slide-33
SLIDE 33

Back to Q1

Q1: How many binary operators can we define that have different semantic definition? A: 16

slide-34
SLIDE 34

Satisfiability and Validity

slide-35
SLIDE 35

Assignments

Definition: A truth-values assignment, α, is an element of 2Prop (i.e., α ∈ 2Prop). In other words, α is a subset of the variables that are assigned true. Equivalently, we can see α as a mapping from variables to truth values: α : Prop → {0, 1} Example: α = {A → 0, B → 1, . . .}

slide-36
SLIDE 36

Satisfaction Relation (| =): Intuition

An assignment can either satisfy or not satisfy a given formula. α | = φ means

  • α satisfies φ or
  • φ holds at α or
  • α is a model of φ

We will first see an example. Then we will define these notions formally.

slide-37
SLIDE 37

Example

Let φ = (A ∨ (B → C)) Let α = {A → 0, B → 0, C → 1} Q: Does α satisfy φ (α | = φ?) A: (0 ∨ (0 → 1)) = (0 ∨ 1) = 1 Hence, α | = φ. Let us now formalize an evaluation process.

slide-38
SLIDE 38

Satisfaction Relation (| =): Formalities

| = is a relation: | =⊆ (2Prop × Formula) Examples:

  • ({A}, A ∨ B): the assignment α = {A} satisfies A ∨ B
  • ({A, B}, A ∧ B)

Alternatively: | =⊆ ({0, 1}Prop × Formula) Examples:

  • (01, A ∨ B): the assignment α = {A → 0, B → 1} satisfies A ∨ B
  • (11, A ∧ B)
slide-39
SLIDE 39

Satisfaction Relation (| =): Formalities

| = is defined recursively:

  • α |

= A if α(A) = true

  • α |

= ¬ϕ if α | = ϕ

  • α |

= ϕ1 ∧ ϕ2 if α | = ϕ1 and α | = ϕ2

  • α |

= ϕ1 ∨ ϕ2 if α | = ϕ1 or α | = ϕ2

  • α |

= ϕ1 → ϕ2 if α | = ϕ1 implies α | = ϕ2

  • α |

= ϕ1 ↔ ϕ2 if α | = ϕ1 iff α | = ϕ2

slide-40
SLIDE 40

From Definition to an Evaluation Algorithm

Truth Evaluation Problem: Given ϕ ∈ Formula and α ∈ 2AP(ϕ), does α | = ϕ? Eval(ϕ, α) if ϕ ≡ A then return α(A); if ϕ ≡ ¬φ then return ¬ Eval (φ, α); if ϕ ≡ ψ ◦ φ then return Eval (ψ, α) ◦ Eval (φ, α); Eval uses polynomial time and space.

slide-41
SLIDE 41

Nothing More Than What We Already Know

Recall the Example:

  • Let φ = (A ∨ (B → C))
  • Let α = {A → 0, B → 0, C → 1}

Eval(φ, α) = Eval(A, α) ∨ Eval(B → C, α) = 0 ∨ Eval(B, α) → Eval(C, α) = 0 ∨ (0 → 1) = 0 ∨ 1 = 1 Hence, α | = φ.

slide-42
SLIDE 42

Extending Truth Table

p q (p → (q → p)) (p ∧ ¬p) (p ∨ ¬q) 1 1 1 1 1 1 1 1 1 1 1

slide-43
SLIDE 43

Extending Truth Table

p q r (p → (q → ¬r) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

slide-44
SLIDE 44

Set of Assignment

Intuition: a formula specifies a set of truth assignments. Function models: models : Formula → 22Prop (a formula → set of satisfying assignments) Recursive definition:

  • models(A) = {α|α(A) = 1}, A ∈ Prop
  • models(¬ϕ) = 2Prop − models(ϕ)
  • models(ϕ1 ∧ ϕ2) = models(ϕ1) ∩ models(ϕ2)
  • models(ϕ1 ∨ ϕ2) = models(ϕ1) ∪ models(ϕ2)
  • models(ϕ1 → ϕ2) = (2Prop − models(ϕ1) ∪ models(ϕ2)
slide-45
SLIDE 45

Example

models(A ∨ B) = {{10}, {01}, {11}} This is compatible with the recursive definition: models(A ∨ B) = models(A) ∪ models(B) = {{10}, {11}} ∪ {{01}, {11}} = {{10}, {01}, {11}}

slide-46
SLIDE 46

Theorem

Let ϕ ∈ Formula and α ∈ 2Prop, then the following statements are equivalent:

  • α |

= ϕ

  • α ∈ models(ϕ)
slide-47
SLIDE 47

Projected Assignment

AP(ϕ): the Atomic Propositions in ϕ. Clearly AP(ϕ) ⊆ Prop. Let α1, α2 ∈ 2Prop, ∈ Formula. Lemma: if α1 |AP(ϕ)= α2 |AP(ϕ), then α1 | = ϕ iff α2 | = ϕ Corollary: α | = ϕ iff α |AP(ϕ)| = ϕ We will assume, for simplicity, that Prop = AP(ϕ).

slide-48
SLIDE 48

Extension of | = to Assignment Sets

Let ϕ ∈ Formula Let T be a set of assignments, i.e., T ⊆ 22Prop

  • Definition. T |

= ϕ if T ⊆ models(ϕ) i.e., | =⊆ 22Prop × Formula

slide-49
SLIDE 49

Extension of | = to Formulas

| =⊆ 2Formula × 2Formula

  • Definition. Let Γ1, Γ2 be prop. formulas.

Γ1 | = Γ2 iff models(Γ1) ⊆ models(Γ2) iff for all α ∈ 2Prop if α | = Γ1 then α | = Γ2 Examples: x1 ∧ x2 | = x1 ∨ x2 x1 ∧ x2 | = x2 ∨ x3

slide-50
SLIDE 50

Classification of Formulas

A formula ϕ is called valid if models(ϕ) = 2Prop. (also called a tautology). A formula ϕ is called satisfiable if models(ϕ) = ∅. A formula ϕ is called unsatisfiable if models(ϕ) = ∅ (also called a contradiction).

slide-51
SLIDE 51

Characteristics of Formulas

A formula ϕ is valid iff ¬ϕ is unsatisfiable. ϕ is satisfiable iff ¬ϕ is not valid.

slide-52
SLIDE 52

Characteristics of Formulas

We can write | = ϕ when ϕ is valid. | = ϕ when ϕ is not valid. | = ¬ϕ when ϕ is satisfiable. | = ¬ϕ when ϕ is unsatisfiable

slide-53
SLIDE 53

Examples

(p ∧ q) → (p ∨ q) is valid (p ∨ q) → p is satisfiable (p ∧ q) ∧ ¬p is unsatisfiable

slide-54
SLIDE 54

Equivalences

| = A ∧ 1 ↔ A | = A ∧ 0 ↔ 0 | = ¬¬A ↔ A | = A ∧ (B ∨ C) ↔ (A ∧ B) ∨ (A ∧ C) | = ¬(A ∧ B) ↔ (¬A ∨ ¬B) | = ¬(A ∨ B) ↔ (¬A ∧ ¬B)

slide-55
SLIDE 55

Minimal Set of Binary Operators

Recall the question: what is the minimal set of operators necessary? A: Through such equivalences all Boolean operators can be written with a single operator (⊕). Indeed, typically industrial circuits only use one type of logical gate. We’ll see how two are enough: ¬ and ∧

  • Or: |

= (A ∨ B) ↔ ¬(¬A ∧ ¬B)

  • Implies: |

= (A → B) ↔ (¬A ∨ B)

  • Equivalence: |

= (A ↔ B) ↔ (A → B) ∧ (B → A)

slide-56
SLIDE 56

Decision Problem

The decision problem: Given a propositional formula φ, is φ satisfiable? An algorithm that always terminates with a correct answer to this problem is called a decision procedure for propositional logic.

slide-57
SLIDE 57

Normal Forms

slide-58
SLIDE 58

Definitions

A literal is either an atom or a negation of an atom. Letφ = ¬(A ∨ ¬B). Then:

  • Atoms: AP(φ) = {A, B}
  • Literals: lit(φ) = {A, ¬B}

Equivalent formulas can have different literals

  • φ = ¬(A ∨ ¬B) = ¬A ∧ B
  • Now lit(φ) = {¬A, B}
slide-59
SLIDE 59

Definitions

A term is a conjunction of literals

  • Example: (A ∧ ¬B ∧ C)

A clause is a disjunction of literals

  • Example: (A ∨ ¬B ∨ C)
slide-60
SLIDE 60

Negation Normal Form (NNF)

A formula is said to be in Negation Normal Form (NNF) if it only contains ¬, ∧, ∨ connectives and only atoms can be negated. Examples:

  • ¬(A ∨ ¬B) is not in NNF
  • ¬A ∧ B is in NNF
slide-61
SLIDE 61

Coverting to NNF

Every formula can be converted to NNF in linear time:

  • Eliminate all connectives other than ∧, ∨, ¬
  • Use De Morgan and double-negation rules to push negations to

the right Example: ¬(A → ¬B)

  • Eliminate →: ¬(¬A ∨ ¬B)
  • Push negation using De Morgan: (¬¬A ∧ ¬¬B)
  • Use Double negation rule: (A ∧ B)
slide-62
SLIDE 62

Disjunctive Normal Form (DNF)

A formula is said to be in Disjunctive Normal Form (DNF) if it is a disjunction of terms. In other words, it is a formula of the form

  • i

(

  • j

li,j) where li,j is the j-th literal in the i-th term. Examples

  • (A ∧ ¬B ∧ C) ∨ (∧A ∧ D) ∨ (B) is in DNF.

DNF is a special case of NNF.

slide-63
SLIDE 63

Coverting to DNF

Every formula can be converted to DNF in exponential time and space:

  • Convert to NNF
  • Distribute disjunctions following the rule:

| = A ∧ (B ∨ C) ↔ ((A ∧ B) ∨ (A ∧ C)) Example: (A ∨ B) ∧ (¬C ∨ D)

  • ((A ∨ B) ∧ (¬C)) ∨ ((A ∨ B) ∧ D)
  • (A ∧ ¬C) ∨ (B ∧ ¬C) ∨ (A ∧ D) ∨ (B ∧ D)

Q:How many clauses would the DNF have had we started from a conjunction of n clauses?

slide-64
SLIDE 64

Satisfiability of DNF

Is the following DNF formula satisfiable? (x1 ∧ x2 ∧ ¬x1) ∨ (x2 ∧ x1) ∨ (x2 ∧ ¬x3 ∧ x3) What is the complexity of satisfiability of DNF formulas?

slide-65
SLIDE 65

Conjunctive Normal Form (CNF)

A formula is said to be in Conjunctive Normal Form (CNF) if it is a conjunction of clauses. In other words, it is a formula of the form

  • i

(

  • j

li,j) where li,j is the j-th literal in the i-th term. Examples

  • (A ∨ ¬B ∨ C) ∧ (¬A ∨ D) ∧ (B) is in CNF

CNF is a special case of NNF.

slide-66
SLIDE 66

Coverting to CNF

Every formula can be converted to CNF:

  • in exponential time and space with the same set of atoms
  • in linear time and space if new variables are added.
  • In this case the original and converted formulas are

“equi-satisfiable”.

  • This technique is called Tseitin’s encoding.
slide-67
SLIDE 67

Converting to CNF: the Exponential Way

CNF(φ){ case

  • φ is a literal: return φ
  • φ is ϕ1 ∧ ϕ2: return CNF(ϕ1) ∧ CNF(ϕ2)
  • φ is ϕ1 ∨ ϕ2: return Dist(CNF(ϕ1), CNF(ϕ2))

} Dist(ϕ1, ϕ2){ case

  • ϕ1 is ψ11 ∧ ψ12: return Dist(ψ11, ϕ2) ∧ Dist(ψ12, ϕ2)
  • ϕ2 is ψ21 ∧ ψ22: return Dist(ϕ1, ψ21) ∧ Dist(ϕ1, ψ22)

}

slide-68
SLIDE 68

Converting to CNF: the Exponential Way

Consider the formula φ = (x1 ∧ y1) ∨ (x2 ∧ y2) CNF(φ) = (x1 ∨ x2) ∧ (x1 ∨ y2) ∧ (y1 ∨ x2) ∧ (y1 ∨ y2) Now consider: φn = (x1 ∧ y1) ∨ (x2 ∧ y2) ∨ . . . ∨ (xn ∧ yn) Q: How many clauses CNF(φn) returns? A: 2n

slide-69
SLIDE 69

Tseitin’s Encoding

Consider the formula (A → (B ∧ C)) The parse tree: Associate a new auxiliary variable with each gate. Add constraints that define these new variables. Finally, enforce the root node.

slide-70
SLIDE 70

Tseitin’s Encoding

(a1 ↔ (A → a2)) ∧ (a2 ↔ (B ∧ C)) ∧ (a1) Each such constraint has a CNF representation with 3 or 4 clauses. First: (a1 ∨ A) ∧ (a1 ∨ ¬a2) ∧ (¬a1 ∨ A ∨ a2) Second: (¬a2 ∨ B) ∧ (¬a2 ∨ C) ∧ (a2 ∨ ¬B ∨ ¬C)

slide-71
SLIDE 71

Tseitin’s Encoding

φn = (x1 ∧ y1) ∨ (x2 ∧ y2) ∨ . . . ∨ (xn ∧ yn) With Tseitin’s encoding we need:

  • n auxiliary variables a1, . . . , an.
  • Each adds 3 constraints.
  • Top clause: (a1 ∨ . . . ∨ an)

Hence, we have

  • 3n + 1 clauses, instead of 2n.
  • 3n variables rather than 2n.
slide-72
SLIDE 72

SAT Problem and SAT Solver

SAT problem is: Given a Boolean formula in CNF, asking whether there exists an assignment to each variable so that the value of the formula is true. It is a NPC problem, which means that there is only exponential algorithm so far. A SAT solver is a tool that solves the SAT problem. However, SAT solver is to be said as the ”most successful formal tools, which can handle 100,000 variables with millions of clauses in less than one sec.