Theorem Proving in Classical Logic
Total Page:16
File Type:pdf, Size:1020Kb
MEng Individual Project Imperial College London Department of Computing Theorem Proving in Classical Logic Supervisor: Dr. Steffen van Bakel Author: David Davies Second Marker: Dr. Nicolas Wu June 16, 2021 Abstract It is well known that functional programming and logic are deeply intertwined. This has led to many systems capable of expressing both propositional and first order logic, that also operate as well-typed programs. What currently ties popular theorem provers together is their basis in intuitionistic logic, where one cannot prove the law of the excluded middle, ‘A A’ – that any proposition is either true or false. In classical logic this notion is provable, and the_: corresponding programs turn out to be those with control operators. In this report, we explore and expand upon the research about calculi that correspond with classical logic; and the problems that occur for those relating to first order logic. To see how these calculi behave in practice, we develop and implement functional languages for propositional and first order logic, expressing classical calculi in the setting of a theorem prover, much like Agda and Coq. In the first order language, users are able to define inductive data and record types; importantly, they are able to write computable programs that have a correspondence with classical propositions. Acknowledgements I would like to thank Steffen van Bakel, my supervisor, for his support throughout this project and helping find a topic of study based on my interests, for which I am incredibly grateful. His insight and advice have been invaluable. I would also like to thank my second marker, Nicolas Wu, for introducing me to the world of dependent types, and suggesting useful resources that have aided me greatly during this report. Finally, I’d like to thank my friends and family for supporting me throughout my time at Imperial College. Contents 1 Introduction 1 1.1 Report Outline........................................1 1.2 Contributions........................................2 2 Logic 3 2.1 Propositional Logic.....................................3 2.2 First Order Logic......................................4 2.3 Natural Deduction......................................4 2.3.1 Logical Subsystems.................................4 2.3.2 Conjunction and Disjunction...........................6 2.3.3 First Order Natural Deduction..........................7 2.4 Sequent Calculus......................................8 2.4.1 Structural Rules...................................8 2.4.2 LK and LJ......................................8 3 λ-Calculus 10 3.1 The Untyped λ-Calculus.................................. 10 3.2 The Simply Typed λ-Calculus............................... 11 3.3 Principal Types....................................... 12 3.3.1 Unification...................................... 12 3.3.2 Principal Type.................................... 13 3.4 The Curry-Howard Isomorphism............................. 13 3.5 Proof-Term Syntax for Intuitionistic Propositional Logic................ 14 3.5.1 Inhabiting the Logic with Syntax......................... 14 + 3.5.2 STLC × for IPL................................... 15 4 λµ-Calculus 16 4.1 Evaluation Contexts..................................... 16 4.2 λµ-Terms........................................... 16 4.3 Type Assignments for the λµ-Calculus.......................... 18 4.3.1 Types and Contexts................................. 18 4.3.2 Type Assignments.................................. 18 4.4 A Proof-Term Syntax for Classical Propositional Logic................. 19 4.4.1 Translating Derivations............................... 20 4.4.2 Towards a Complete Logic............................. 21 4.5 Sums and Products..................................... 22 4.6 λµ-Calculus as a Natural Deduction System....................... 23 5 Dependent Types 25 5.1 Intuitionistic Type Theory................................. 25 5.2 The Problem of Control................................... 28 5.3 Avoiding the Degeneracy.................................. 28 5.3.1 dPA! ......................................... 28 5.3.2 Reductions...................................... 30 5.3.3 Type Assignments.................................. 30 5.4 Using dPA! .......................................... 31 5.5 Inductive Families...................................... 32 5.5.1 Defining Inductive Families............................ 32 vi 6 Simple Types with Name Polymorphism in λµ 35 6.1 Typing Algorithms for λµ-calculus............................ 35 6.1.1 Towards a Principal Pairing Algorithm...................... 35 6.1.2 Principal Pairing Definitions............................ 36 6.2 A Theorem Prover for Classical Propositional Logic................... 38 6.2.1 Syntax........................................ 38 6.2.2 Type Assignments for λµN ............................. 38 7 ECCλµ: Dependently Typed λµ-calculus 42 7.1 Some formalisms...................................... 42 7.2 Type System......................................... 42 7.2.1 Syntax........................................ 43 7.2.2 Reductions...................................... 44 7.2.3 Type Assignments.................................. 44 7.2.4 Properties...................................... 46 7.3 Dependent Algebraic Data Types............................. 47 7.3.1 Inductive Families.................................. 48 7.3.2 Inductive Records.................................. 50 7.3.3 Reductions for (Co)Inductive Types........................ 51 7.4 Typing Algorithm...................................... 52 7.4.1 Weak Head Normal Form............................. 52 7.4.2 Bidirectional Algorithm.............................. 53 7.4.3 nef Rules....................................... 54 8 Implementation 56 8.1 Variables and Representing Terms............................. 58 8.1.1 De Bruijn Indices.................................. 58 8.1.2 Unbound – Locally Nameless........................... 58 8.1.3 Using Unbound................................... 59 8.1.4 Structural Substitution............................... 59 8.2 Syntax............................................. 60 8.2.1 Parsing........................................ 60 8.2.2 User Syntax..................................... 60 8.2.3 AST and Parser Errors............................... 61 8.3 Type Checking Monad................................... 61 8.4 Simply Typed Theorem Prover............................... 62 8.4.1 Name Polymorhpism and Type Instantiation.................. 62 8.4.2 Expressing Logic.................................. 63 8.4.3 Diagram....................................... 63 8.5 Dependently Typed Theorem Prover........................... 63 8.5.1 Evaluation...................................... 63 8.5.2 (Co)Data....................................... 63 8.6 REPL............................................. 64 9 Evaluation 66 9.1 λµN .............................................. 66 9.1.1 Theory........................................ 66 9.1.2 Implementation................................... 66 9.2 ECCλµ ............................................. 66 9.2.1 Theory........................................ 66 9.2.2 Implementation................................... 68 10 Ethical Discussion 69 vii 11 Conclusion 70 11.1 Review............................................ 70 11.2 Future Work......................................... 70 11.2.1 Simple Types..................................... 70 11.2.2 Dependent Types.................................. 71 A λµN 73 A.1 Proofs............................................. 73 A.2 Implementation....................................... 82 A.2.1 Natural Deduction................................. 82 B ECCλµ 84 B.1 Proofs............................................. 84 B.2 Type Systems......................................... 93 B.3 Bidirectional Algorithms for ECCλµ ............................ 94 B.4 Derivations.......................................... 97 B.5 Implementation....................................... 98 B.5.1 Syntax........................................ 98 References 98 viii 1| Introduction Proof assistants are gaining in popularity. From helping to teach students how to write mathe- matical proofs1 to the large scale projects like checking compiler correctness [13], they are seeing more use in the computing world. Proof assistants are programming languages that correspond with a formal logic, and come in different shapes and forms; Coq [72] has a particular focus on the theorem proving aspect where proofs can be written with intuitive tactics, whereas the language Agda [59] is first and foremost a functional programming language, like Haskell. Under the hood, theorem provers ensure proof correctness by a strong type system. The mainstream languages are all based on so-called intuitionistic type theory [46]. They all capitalise on the astonishing fact that functions in a functional programming language correspond to proofs in intuitionistic logic; and the types of these functions correspond to the propositions that are derived by said proofs. Under this correspondence, type-checking a function is the same oper- ation as checking a proof of a proposition [77]. Intuitionism inescapably ties these languages to the fact that they are unable to prove a simple logical notion; that any proposition is either true or false. This is known as the law of the excluded middle ((lem)), and it characterises the logic we call classical logic [23]. Intuitionistic logic rejects this notion, and instead is based on the idea that