Propositional-Logic Typing - Developing a Logic System for ...

30
Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design Propositional-Logic Typing Developing a Logic System for Type Checking Florian Schr¨ ogendorfer, fl[email protected]

Transcript of Propositional-Logic Typing - Developing a Logic System for ...

Page 1: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Propositional-Logic TypingDeveloping a Logic System for Type Checking

Florian Schrogendorfer, [email protected]

Page 2: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Propositional-Logic Typing

based on correspondence between types and propositions

uses propositional calculus for type checking programs

forthcoming development based on intuitionistic type theory

we start by defining a simple logic consisting of:

a syntax definition for building well-formed formulas (wffs)

a set of axioms and inference rules for proving wffs

a notion of proof

Page 3: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

A Simple Formal Language

defines sentences of the logic, called well-formed formulas (wffs)

Alphabet

propositional symbols: P,Q,R, . . .

connectives: ⇒,∧

auxiliary symbols: ⊢, (, )

Grammar

Sequent S ∶∶= Γ ⊢ φ ...these are the wffs

Context Γ ∶∶= φ1, φ2, . . . , φn n ≥ 0

Proposition φ ∶∶= P ∣ (φ1 ∧ φ2) ∣ (φ1 ⇒ φ2)

read Γ ⊢ φ as from assumption Γ, infer conclusion φ

Page 4: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Natural Deduction

sequents are the basic units of proof

deductive system given by:

axioms for defining true propositions within the system

inference rules to transform true propositions

therefore:

each connective op defined by its set of inference rules

propositions containing op built by introduction rules op I

elimination rules op E remove connectives from propositions

Definition: Theorem

A proved sequent of the form ⊢ φ is called a theorem in the logic.

Page 5: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Axiom and Inference Rules

Axiom

Γ ⊢ φ if φ ∈ Γ where Γ = φ1, φ2, . . . , φn

Introduction Rules

∧I ∶Γ ⊢ φ1 Γ ⊢ φ2

Γ ⊢ φ1 ∧ φ2⇒ I ∶

Γ, φ1 ⊢ φ2

Γ ⊢ φ1 ⇒ φ2

Elimination Rules

∧E1 ∶Γ ⊢ φ1 ∧ φ2

Γ ⊢ φ1∧E2 ∶

Γ ⊢ φ1 ∧ φ2

Γ ⊢ φ2

⇒ E ∶Γ ⊢ φ1 ⇒ φ2 Γ ⊢ φ1

Γ ⊢ φ2

Page 6: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Sequent-Proof Trees

proofs interpreted by the notion of sequent-proof trees

Sequent-Proof Tree

A sequent-proof tree is a tree whose

root is a sequent, Γ ⊢ φ

leaves are axioms

internal nodes are consequents of inference rules

following hypothetical trees can be built from the defined connectives

Γ ⊢ φ1 ∧ φ2

Γ ⊢ φ2

∧I

Γ ⊢ φ1

Γ ⊢ φ1

Γ ⊢ φ1 ∧ φ2

∧E1

Γ ⊢ φ2

Γ ⊢ φ1 ∧ φ2

∧E2

Γ ⊢ φ1 ⇒ φ2

Γ, φ1 ⊢ φ2

⇒ I

Γ ⊢ φ2

Γ ⊢ φ1

⇒ E

Γ ⊢ φ1 ⇒ φ2

Page 7: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Sequent-Proof Trees - Example

(Q ∧ R)⇒ S ,P ∧Q ⊢ R ⇒ S

(Q ∧ R)⇒ S ,P ∧Q,R ⊢ S

(Q ∧ R)⇒ S ,P ∧Q,R ⊢ (Q ∧ R)⇒ S

⇒ E

(Q ∧ R)⇒ S ,P ∧Q,R ⊢ Q ∧ R

(Q ∧ R)⇒ S ,P ∧Q,R ⊢ Q

(Q ∧ R)⇒ S ,P ∧Q,R ⊢ P ∧Q

∧E2

(Q ∧ R)⇒ S ,P ∧Q,R ⊢ R

∧I

⇒ I

Page 8: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Natural-Deduction Trees

Natural-Deduction Tree

A natural-deduction tree is a sequent-proof tree with context informationomitted from its sequents.

variant of a proof tree convenient for proving a sequent

simpler representation of a proof situation (easy to generate)

see assumptions as propositions whose proof is forthcoming

can therefore be pasted together, root φ1 to leaf φ2 iff φ1 = φ2

must be verified as well-formed (by attaching context information)

Page 9: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Natural-Deduction Trees - Example

R ⇒ S

S

(Q ∧ R)⇒ S

⇒ E

Q ∧ R

Q

P ∧Q

∧E2

R

∧I

⇒ I

Page 10: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Linearizing Natural-Deduction Trees

Linearization of Natural-Deduction Tree

The linearized trees produced by inference rules then are as follows:

Let t1 and t2 be linearized trees proving φ1, resp. φ2

x the hypothetical tree for a local assumption.

∧ I ∶⇔ (∧ I t1 t2)

∧E1 ∶⇔ (∧E1 t1)

∧E2 ∶⇔ (∧E2 t2)

⇒ I ∶⇔ (⇒ I (x ∈ φ1) t2)

⇒ E ∶⇔ (⇒ E t1 t2)

Linearization of the previous Tree

(⇒ I (x ∈ R) (⇒ E ((Q ∧ R)⇒ S) (∧ I (∧E2 (P ∧Q)) x)))

Page 11: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Heyting Interpretation

Heyting Interpretation

Interpret natural-deduction trees as expressions in lambda calculus.

proof of φ1 ∧ φ2 is a pair of a proofs for φ1 resp. φ2

proof of φ1 ⇒ φ2 is a function mapping proofs of φ1 into proofs of φ2

transformation to lambda calculus by purely syntactic reformatting:

(∧ I t1 t2)↦ (t1, t2) ...an ordered pair

(∧E1 t)↦ fst t ...an indexing operation on a pair

(∧E2 t)↦ snd t ...an indexing operation on a pair

(⇒ I (x ∈ φ) e)↦ λx ∈ φ.e ...a lambda abstraction

(⇒ E (x ∈ φ) e)↦ t1 ⋅ t2 ...the application of lambda abstraction

Lambda Expression corresponding to the previous Tree

λ x ∈ R. ((Q ∧ R)⇒ S) ⋅ (snd(P ∧Q), x)

Page 12: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Viewing Programs as Proofs

propositional logic vs. lambda calculus

language of natural-deduction trees ⇒ typed lambda calculus

logical connective ⇒ corresponds to function-type constructor →

logical connective ∧ corresponds to product-type constructor ×

Curry-Howard Isomorphism

A proof of Γ ⊢ φ is a program of type φ within type assignment Γ.

Justifies use of following synonyms:

proposition ⇔ type

proof ⇔ program

natural-deduction tree ⇔ expression

Page 13: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Typing Rules

Judgement

A judgement is a sequent of the form Γ ⊢ p ∈ φ, where Γ is a list of itemsxi ∈ φ, such that no identifier xi appears twice in Γ.

Read the judgment Γ ⊢ p ∈ φ as:

within context Γ, p is a proof of φ, or

within type assignment Γ, p is a program of type φ

identifiers xi represent hypothetical proof trees for local assumptions

correspondence to typing judgments in the simply typed lambda calculus

viewed as typing rules, the inference rules built from judgements attachcontext and typing information to natural-deduction trees

Page 14: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Rewritten Inference Rules

Axiom

Γ ⊢ x ∈ φ if (x ∈ φ) ∈ Γ

Introduction Rules

∧I ∶Γ ⊢ t1 ∈ φ1 Γ ⊢ t2 ∈ φ2

Γ ⊢ (t1, t2) ∈ φ1 ∧ φ2⇒ I ∶

Γ, x ∈ φ1 ⊢ t ∈ φ2

Γ ⊢ λx ∈ φ1.t ∈ φ1 ⇒ φ2

Elimination Rules

∧E1 ∶Γ ⊢ t ∈ φ1 ∧ φ2

Γ ⊢ fst t ∈ φ1∧E2 ∶

Γ ⊢ t ∈ φ1 ∧ φ2

Γ ⊢ snd t ∈ φ2

⇒ E ∶Γ ⊢ t1 ∈ φ1 ⇒ φ2 Γ ⊢ t2 ∈ φ1

Γ ⊢ t1 ⋅ t2 ∈ φ2

Page 15: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Judgment-Proof Tree - Example

a1 ∈ (Q ∧ R)⇒ S, a2 ∈ P ∧Q ⊢ λx ∈ R.a1 ⋅ (snd a2, x) ∈ R ⇒ S

a1 ∈ (Q ∧ R)⇒ S , a2 ∈ P ∧Q, x ∈ R ⊢ a1 ⋅ (snd a2, x) ∈ S

⊢ a1 ∈ Q ∧ R ⇒ S◻

⇒ E

a1 ∈ (Q ∧ R)⇒ S, a2 ∈ P ∧Q, x ∈ R ⊢ (snd a2, x) ∈ Q ∧ R

a1 ∈ (Q ∧ R)⇒ S , a2 ∈ P ∧Q, x ∈ R ⊢ snd a2 ∈ Q

a1 ∈ (Q ∧ R)⇒ S, a2 ∈ P ∧Q, x ∈ R ⊢ a2 ∈ P ∧Q

∧E2

a1 ∈ (Q ∧ R)⇒ S , a2 ∈ P ∧Q, x ∈ R ⊢ x ∈ R

∧I

⇒ I

Page 16: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Introducing Primitive Types

previous chapters suggested that basic programming languages contain:

a core set of primitive types

extended by lambda abstraction, records etc.

in our core logic, data types are represented by propositions

axioms and inference rules define the data types

introducing primitive types bool and nat by defining their set of rules

see expr. like 3 + 2 as natural-deduction trees proving proposition nat

Page 17: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Introducing Primitive Types (contd.)

Boolean Primitives

bool I1 ∶ Γ ⊢ false ∈ bool bool I2 ∶ Γ ⊢ true ∈ bool

bool E ∶Γ ⊢ t1 ∈ bool Γ ⊢ t2 ∈ φ Γ ⊢ t3 ∈ φ

Γ ⊢ if t1 t2 t3 ∈ φ

Natural Numbers

nat In ∶ Γ ⊢ n ∈ nat, for n ≥ 0

nat E1 ∶Γ ⊢ n1 ∈ nat Γ ⊢ n2 ∈ nat

Γ ⊢ n1 + n2 ∈ nat

nat E2 ∶Γ ⊢ n1 ∈ nat Γ ⊢ n2 ∈ nat

Γ ⊢ n1 = n2 ∈ bool

Page 18: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Propositions vs. Types

An Example of the Extended Language

⊢ λx ∈ nat.x + 1 ∈ nat ⇒ nat

Relation between the Concepts

thus, following terms can be seen as equivalent:

constructing a function of type nat → nat

proving the proposition nat ⇒ nat

however, there are infinitely many ways of proving a theorem

analogously, there exist infinitely many programs of type nat → nat

under this insight, decidability becomes an issue

Page 19: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Decidability

1 For a natural-deduction tree and a context what proposition doesthe tree proof?

yes: the algorithm checks if the natural-deduction tree iswell-formed and calculates the proposition using defined setinference rulesin a programming language such a tree checker is referred toas type checker

2 Is a given proposition φ a theorem?

yes: a trivial algorithm would compute φ’s truth table andcheck if is a tautologyaddressed by work on automated theorem proving

Page 20: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Computation Rules

we saw that programs are evaluated by computation (rewriting) rules

computation rules in our setting are proof simplification rules

Proof Simplification Rule

A tree containing the application of an introduction rule op I , followed byan application of an elimination rule op E can be simplified withoutaffecting the sequent the tree proves, assuming an unchanged context.

Example: Proof Tree Simplification

φ1

φ1 ∧ φ2

φ2

e2

∧I

φ1

e1

∧E1

φ1φ1

e1⇒

(∧E1 (∧I e1 e2))→ e1

fst(e1, e2)→ e1

Page 21: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Computation Rules (Contd.)

simplification rules interpreted as computation rules in lambda calculus

as expected under the Heyting interpretation

Computation Rules in our current Logic

∧ ∶ fst(t1, t2)⇒ t1 as the indexing rule for pairs

snd(t1, t2)⇒ t2

⇒∶ (λ x ∈ φ1. t1) ⋅ t2 ⇒ [t2/x]t1 as the β-reduction

bool ∶ if true t1 t2 ⇒ t1if false t1 t2 ⇒ t2

nat ∶ n1 + n2 ⇒ n3

n = n⇒ truen1 = n2 ⇒ false where n1 and n2 are different numerals.

Page 22: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Extending the Logic

a standard propositional logic also contains disjunction and negation

introduction of the disjunction ∨ follows the usual pattern

negation is introduced as a logic constant: the nullary connective ⊥

Extended Grammar

φ ∶∶= ⋯ ∣ (φ1 ∨ φ2) ∣ ⊥

Inference Rules

∨I1 ∶Γ ⊢ φ1

Γ ⊢ φ1 ∨ φ2∨ I2 ∶

Γ ⊢ φ2

Γ ⊢ φ1 ∨ φ2

∨E ∶Γ ⊢ φ1 ∨ φ2 Γ, φ1 ⊢ φ3 Γ, φ2 ⊢ φ3

Γ ⊢ φ3⊥ E ∶

Γ ⊢⊥

Γ ⊢ φ

Page 23: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Extending the Logic (Contd.)

Heyting Interpretation and Linearization

Heyting interpretation of disjunction and falsehood:

proof of φ1 ∨φ2 is a proof of φ1, labeled inl or a proof of φ2 labeled by inr

proofs of ⊥ do not exist

Lambda expressions of linearized trees for the new connectives:

(∨ Iφ1∨φ21 t)↦ inlφ1∨φ2t

(∨ Iφ1∨φ22 t)↦ inrφ1∨φ2t

(∨E t1 (x ∈ φ1) t2 (y ∈ φ2) t3)↦ cases t1 of isl(x ∈ φ1). t2 8 isr(y ∈ φ2). t3

(⊥ Eφ t)↦ abortφ t

Computation Rules

∨ ∶ cases (inl t1) of isl (x ∈ φ1). t2 8 isr (y ∈ φ2). t3 ⇒ [t1/x]t2cases (inr t1) of isl (x ∈ φ1). t2 8 isr (y ∈ φ2). t3 ⇒ [t1/y]t3

Page 24: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Negation and Falsehood

Negation is introduced in terms of falsehood.

read ¬φ as an abbreviation for φ⇒⊥

we’re only concerned with consistent intuitionistic logics

omitting an introduction rule for falsehood keeps logic consistent

Consistent Propositional Logic

A logic is consistent if it is impossible to proof ⊢⊥, or equivalently ⊢ φ aswell as ⊢ ¬φ for any proposition φ.

Consistent Context

A context Γ is consistent if it is impossible to proof Γ ⊢⊥.

therefore negation in our system is reduced to falsehood

Page 25: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Negation and Falsehood - Examples

(P ⇒ ¬P)⇒ ¬P

¬P

P⇒ E

¬P

P⇒ E

P ⇒ ¬P

⇒ I

⇒ I

Reasoning about Negation (using rules for ⇒ I and ⇒ E )

observe: applying ⇒ I on a proposition ¬φ derives following rule

Γ, φ ⊢⊥

Γ ⊢ φ⇒⊥

corresponds to a weak version of proof by contradiction under context Γ

Page 26: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Summary: Programming-Languages as Logic Systems

recall that the terms program and proof are synonymous

structuring constructs defined by rules for logical connectives

values created by introduction rules, operators by elimination rules

computation rules define operators semantics on values of a type

core data types are the primitive propositions

previous examples showed development of basic functional language

but also imperative languages may be defined this way

Page 27: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

The Core Imperative Language

previously defined rules for core data types (bool , nat) still hold

introduction of connectives follows typing rules of the core language

Judgement Rules for Storing Numerals

natloc I ∶ Γ ⊢ n ≥ 0 ∈ natloc store I1 ∶ Γ ⊢ nil ∈ store

store I2 ∶Γ ⊢ l ∈ natloc Γ ⊢ n ∈ nat Γ ⊢ s ∈ store

Γ ⊢ update l n s ∈ store

store E ∶Γ ⊢ l ∈ natloc Γ ⊢ s ∈ store

Γ ⊢ lookup l s ∈ nat

Computation Rules for Storing Numerals

store ∶ lookup l nil ⇒ 0lookup l(update l n s)⇒ nlookup l(update m n s)⇒ lookup l s if l ≠ m

Page 28: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

The Core Imperative Language (Contd.)

Rules for Expressions of Type natexp

natexp I1 ∶Γ ⊢ N ∈ nat

Γ ⊢ N ∈ natexpnatexp I2 ∶

Γ ⊢ E1 ∈ natexp Γ ⊢ E2 ∈ natexp

Γ ⊢ E1 + E2 ∈ natexp

natexp E ∶Γ ⊢ E ∈ natexp Γ ⊢ s ∈ store

Γ ⊢ (E s) ∈ nat

Rules for Commands of Type comm

comm I1 ∶Γ ⊢ L ∈ natloc Γ ⊢ E ∈ natexp

Γ ⊢ L ∶= E ∈ comm

comm I2 ∶Γ ⊢ C1 ∈ comm Γ ⊢ C2 ∈ comm

Γ ⊢ C1; C2 ∈ comm

commE ∶Γ ⊢ C ∈ comm Γ ⊢ s ∈ store

Γ ⊢ (C s) ∈ store

Page 29: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

The Core Imperative Language (Contd.)

computation rules are built from the rewriting rules of lambda calculus

usual pattern of operators on canonical exp. created by elimination rule

Computation Rules

comm ∶ (L ∶= E s)⇒ update L(E s)s(C1; C2)⇒ (C1(C2 s))(if E then C1 else C2 fi s)⇒ if (E s) (C1 s) (C2 s)(while E do C od s)⇒ if (E s)(while E do C od (C s)) s

natexp ∶ (N s)⇒ N(@L s)⇒ lookup L s(E1 + E2)⇒ (E1 s) + (E2 s)(¬E s)⇒ if (E s)false true

Page 30: Propositional-Logic Typing - Developing a Logic System for ...

Introduction The Propositional Calculus Proofs as Programs Disjunction and Falsehood Programming-Language Design

Le Fin

Thanks for your attention!