140 likes | 158 Views
Coq in Two Hours. A quick tutorial on reading Coq proofs. What is Coq?. https://www.seas.upenn.edu/~cis500/current/index.html. Software Engineering. Formal Verification. Foundation. Programming Languages. “More” Foundation. Math. Logic. “Application” in “Real-world”.
E N D
Coq in Two Hours AquicktutorialonreadingCoqproofs.
What is Coq? https://www.seas.upenn.edu/~cis500/current/index.html Software Engineering Formal Verification Foundation Programming Languages “More” Foundation Math Logic
“Application”in“Real-world” • Certificatedcompiler • Compcert:http://compcert.inria.fr • L2C:https://github.com/l2ctsinghua/l2c(ShengyuanWang) • CertificatedOS • CertiKOS:http://flint.cs.yale.edu/shao/papers/certikos.html(ZhongShao) • Certificatedblockchain?
Forus,… • Helptolearnlogic,types,semantics,verification,algebra,etc. • Rethinkwhatproofis. • Writeproofmorestrictlyandprecisely.
Outline • Basics • Booleans, Function Types, Numbers • Proof by simplification, rewriting and case analysis • Proof by Induction • PolymorphismList • Tactics • Logic • Relation
Basics • Coq people say “Proof.simpl. reflexivity. Qed.” is just a test! • Proofs are goal-guided. • Functions are first-class citizen. (functional) • Coq provides a programming language to model functions and data. • Recursive functions (fixpoints) must be written in a form s.t. Coq can show its termination. • Induction is the core of programming in Coq. • Q:Whodoestheproof?
Proof by Induction • Coq people saymathematicalproofsareinformal!* • Works for inductivelydefined structures. • Only necessary if destruct fails. • Choosing which variable to induct is tricky (depends on the definition). *Logiciansthink(mathematicians’)mathematicsarenotstrict.Informalproofsmaybeambiguous,e.g.the“example”in“let’sseeanexample”(metalanguage)and“synthesisbyexample”(formallanguage).
Polymorphism List • Types are first-class citizen. (dependent type) • Type arguments can be inferenced. (implicit type*) *Scala masters implicit types.
Tactics • beq_natand = are two different things. • Induction hypothesis need to be general sometimes. • apply: behave symmetrically when applying to hypotheses/goals. • inversion: reduce complicated constructors, yield contradiction. • unfold: unfold definitions. • assert: show immediate conclusions for convenience. • destruct/induction: applied on expressions.
Logic • Patterns: [A B] v.s. [A | B]. • Prove goals: split for /\ and <->, left/right for \/. • Prove existential: exists. • Propositions are first-class citizens. • Propositions and bools are different. • Coq uses Constructive Logic.
Coq’sLogicalSystemisIncomplete For those who like a challenge, here is an exercise taken from theCoq’Art book by Bertot and Casteran (p. 123). Each of thefollowing four statements, together with excluded_middle, can beconsidered as characterizing classical logic. We can’t prove anyof them in Coq, but we can consistently add any one of them as anaxiom if we wish to work in classical logic.Prove that all five propositions (these four plusexcluded_middle) are equivalent. Definitionpeirce := forall P Q: Prop,((P->Q)->P)->P.Definitiondouble_negation_elimination := forallP:Prop,~~P -> P.Definitionde_morgan_not_and_not := forall P Q:Prop,~(~P /\ ~Q) -> P\/Q.Definitionimplies_to_or := forall P Q:Prop,(P->Q) -> (~P\/Q).
Relations • Inductively defined relations tell all possible rules. • inversion: destruct on evidence. • induction: induction on evidence. • apply: use a rule.
Future We will focus on a small imperative programming language IMP… • Syntax and Semantics • Equivalence • Hoare logic • Small-Step operational semantics