├── README.md ├── lit ├── Geuvers-IntroTT-2008.pdf ├── Girard-ProofsAndTypes-1990.pdf └── Scott-TypeTheoreticAlternative-1993.pdf └── notes ├── MGS └── Jung │ └── Jung-2014-Teaching-denotational-semantics.pdf ├── OPLSS ├── 2014 │ ├── Birkedal-categorical-logic-tutorial-notes.pdf │ └── Pientka-ProofTheoryNotes.pdf ├── 2015 │ ├── Constable-OPLSS-Short-2015-2.pdf │ └── Morehouse-IntroCategoricalSemantics.pdf └── 2017 │ ├── Downen │ ├── Downen-ReviewLectures.md │ ├── README.md │ └── ReviewLectures.org │ ├── Harper-Licata │ ├── README.md │ ├── day1.pdf │ ├── day1.tar.gz │ ├── day2.pdf │ └── day3.pdf │ └── Pfenning │ ├── 01-inference.pdf │ ├── 02-propositions.pdf │ ├── 03-harmony.pdf │ ├── 04-cutelim.pdf │ ├── 05-pap.pdf │ └── README.md └── Raamsdonk-LogicalVerification-2008.pdf /README.md: -------------------------------------------------------------------------------- 1 | # TypeFunc 2 | 3 | This repository collects some links and resources for learning about type 4 | theory, functional programming, and related subjects. 5 | 6 | ## My Current Favorites (new section!) 7 | 8 | Because there are so many (perhaps too many) links to resources on this page, I've decided to occasionally short-list a few resources that were (at the time of the last commit) my current favorites... for whatever that's worth. 9 | 10 | 1. [Introduction to Univalent Foundations of Mathematics with Agda](https://www.cs.bham.ac.uk/~mhe/HoTT-UF-in-Agda-Lecture-Notes/HoTT-UF-Agda.html), [Martín Escardó](https://www.cs.bham.ac.uk/~mhe/), 2019. 11 | 2. [The Agda Universal Algebra Library](https://ualib.gitlab.io), [William DeMeo](https://williamdemeo.gitlab.io), 2021. (shameless plug). 12 | 3. [Homotopy Type Theory Electronic Seminar Talks](https://www.uwo.ca/math/faculty/kapulkin/seminars/hottest.html). 13 | 4. [The HoTT Book][], textbook from the IAS year on univalent foundations, 2013. 14 | 5. [Scala with Cats](https://underscore.io/books/scala-with-cats/), Noel Welsh and Dave Gurnell, 2020. 15 | 16 | What appeals to me about the last item is the book's promotion of program composition and category theory as two powerful organizing principles for software design and development. 17 | 18 | ## Books and Papers 19 | 20 | ### Basics/Background 21 | + [Logic and Proof](https://leanprover.github.io/logic_and_proof/) ([source](https://github.com/leanprover/logic_and_proof)), Avigad, Lewis, van Doorn. 22 | + [Type Theory and Formal Proof: An Introduction](https://www.google.com/search?kgmid=/g/1z2tr7kmy&hl=en-US&kgs=73629010424c16a3&q=Type+Theory+and+Formal+Proof:+An+Introduction&shndl=0&source=sh/x/kp/osrp&entrypoint=sh/x/kp/osrp), Geuvers and Nederpelt. 23 | + [Foundations for Programming Languages](http://www.amazon.com/dp/0262133210/), John Mitchell. 24 | + [Types and Programming Languages](https://www.cis.upenn.edu/~bcpierce/tapl/), Benjamin Pierce. 25 | + [Practical Foundations for Programming Languages](http://www.cs.cmu.edu/~rwh/pfpl.html), [Robert Harper](http://www.cs.cmu.edu/~rwh/). 26 | + [Proofs and Types](http://www.paultaylor.eu/stable/Proofs+Types.html), Girard, Lafont, Taylor. 27 | + [Computational Category Theory](http://www.cs.man.ac.uk/~david/categories/book/book.pdf), Rydeheard and Burstall. 28 | + [Categories for Types](http://www.amazon.com/Categories-Types-Cambridge-Mathematical-Textbooks/dp/0521457017/), Roy Crole. 29 | + [Category Theory in Context](http://www.math.jhu.edu/~eriehl/context.pdf), Emily Riehl. 30 | + [Type Theory and Functional Programming](https://www.cs.kent.ac.uk/people/staff/sjt/TTFP/), Simon Thompson. 31 | + [Intuitionistic Type Theory](http://www.csie.ntu.edu.tw/~b94087/ITT.pdf), Per Martin-Löf's Padova lectures. 32 | + [Two Lectures on Contructive Type Theory](https://www.cs.uoregon.edu/research/summerschool/summer15/notes/OPLSS-Short-2015-2.pdf), Robert Constable. 33 | 34 | ### Programming Books 35 | + [Type-Driven Development with Idris](https://edwinb.wordpress.com/2017/03/23/type-driven-development-with-idris/), Edwin Brady. 36 | + [Functional Programming in Scala](https://www.manning.com/books/functional-programming-in-scala), Paul Chiusano and Rúnar Bjarnason. 37 | + [Software Foundations](http://www.cis.upenn.edu/~bcpierce/sf/), Pierce, et al. 38 | + [Verified Functional Programming in Agda](http://www.amazon.com/Verified-Functional-Programming-Agda-Books/dp/1970001240/), Aaron Stump. 39 | 40 | ### More Advanced 41 | + [Domain-theoretic Foundations of Functional Programming](https://g.co/kgs/eSXtwn) 42 | + [Advanced Topics in Types and Programming Languages](https://mitpress.mit.edu/books/advanced-topics-types-and-programming-languages), Pierce ed., 2005. 43 | + [Higher-Order Computability](http://www.springer.com/us/book/9783662479919), Longley, Normann, 2015. 44 | + [The HoTT Book][], many authors, a textbook on informal type theory, 2013. 45 | + [Introduction to Homotopy Type Theory](http://www.cs.nott.ac.uk/~psztxa/ewscs-17/notes.pdf), [Thorsten Altenkirch](https://www.nottingham.ac.uk/computerscience/People/thorsten.altenkirch)'s lecture notes, 2017. 46 | + [Programming in Martin-Löf's Type Theory](http://www.cse.chalmers.se/research/group/logic/book/), Nordström, Petersson, Smith, 1990. 47 | + [Lectures on the Curry-Howard Isomorphism](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.17.7385), Sørensen, Urzyczyn, 1998. 48 | + [Synthetic Topology of data types and classical spaces](http://www.cs.bham.ac.uk/~mhe/papers/entcs87.pdf), [Martín Escardó](https://www.cs.bham.ac.uk/~mhe/), 2004. 49 | + [Topological domain theory](http://homepages.inf.ed.ac.uk/als/Research/topological-domain-theory.html), Alex Simpson, et al. 50 | 51 | --------------------------------------------------------------- 52 | 53 | ## Recorded Lectures 54 | + [Foundations of Math: Univalent Foundations & Set Theory](http://fomus.weebly.com/talks-abstracts--videos.html), ​​​Bielefeld, DE, 2016. 55 | + [IHP Semantics of proofs and certified math](https://www.youtube.com/playlist?list=PL9kd4mpdvWcCUOrG5S7KrvOFDhyqnXoUj), Institute Henri Poincare, FR, 2014. 56 | + [Homotopy Type Theory graduate seminar](http://www.cs.cmu.edu/~rwh/courses/hott/), [Robert Harper](http://www.cs.cmu.edu/~rwh/), CMU, 2013. 57 | + [Idris course at ITU](https://edwinb.wordpress.com/2013/03/15/idris-course-at-itu-slides-and-video/), by [Edwin Brady](https://edwinb.wordpress.com/), Copenhagen, DK, 2013. 58 | + [IAS Univalent Foundations Program](https://video.ias.edu/taxonomy/term/78), 2012--2013. 59 | + [The Five Stages of Accepting Constructive Math](https://video.ias.edu/members/1213/0318-AndrejBauer), [Andrej Bauer](http://www.andrej.com/), IAS, 2013. 60 | + [Introductory Coq tutorials](https://www.youtube.com/playlist?list=PL5FJyaC2WsVlcWB4we3sPe6t09Vviu3Hn), [Andrej Bauer](http://www.andrej.com/). 61 | + [Introduction to computational logic](https://www.youtube.com/playlist?list=PL5FJyaC2WsVnmxmaOixx3EWx3888BToGJ), [Chad Brown](https://www.ps.uni-saarland.de/~cebrown/) ([downloadable](https://www.ps.uni-saarland.de/courses/cl/videos.php)) 62 | 63 | ---------------------------------------------------------- 64 | 65 | ## Courses 66 | 67 | + [2021 Agda Tutorial in Advanced Functional Programming](http://www.cse.chalmers.se/~abela/AFP-2021/html/Everything.html), [Andreas Abel](http://www.cse.chalmers.se/~abela/). 68 | + [2019 Introduction to Univalent Foundations of Mathematics with Agda](https://www.cs.bham.ac.uk/~mhe/HoTT-UF-in-Agda-Lecture-Notes/HoTT-UF-Agda.html), [Martín Escardó](https://www.cs.bham.ac.uk/~mhe/). 69 | + [2019 Homotopy (type) theory](https://github.com/andrejbauer/homotopy-type-theory-course), [Andrej Bauer](http://www.andrej.com/) and Jaka Smrekar. 70 | + [2017 Type Theory](http://www2.tcs.ifi.lmu.de/~abel/eafit2017/index.html), [Andreas Abel](http://www.cse.chalmers.se/~abela/). 71 | + [2017 Mathematical Foundations of Programming](http://www.cs.nott.ac.uk/~pszvc/g54fop/), [Venanzio Capretta](http://www.duplavis.com/venanzio/index.html). 72 | + [2017 Coalgebras and Infinite Data Structures](http://www.duplavis.com/venanzio/mgs_coalgebra/index.html), [Venanzio Capretta](http://www.duplavis.com/venanzio/index.html). 73 | + [2017 Naïve Type Theory](http://www.cs.nott.ac.uk/~psztxa/mgs-17/) ([YouTube videos](https://www.youtube.com/watch?v=bNG53SA4n48)), [Thorsten Altenkirch](https://www.nottingham.ac.uk/computerscience/People/thorsten.altenkirch). 74 | + [2015 Denotational Semantics](https://www.cl.cam.ac.uk/teaching/1516/DenotSem/), [Marcelo Fiore](https://www.cl.cam.ac.uk/~mpf23/). 75 | + [2015 Introduction to Computational Logic](https://courses.ps.uni-saarland.de/icl_15/), Gert Smolka and Tobias Tebbi. 76 | + [2014 Semantics of proofs and certified mathematics](https://ihp2014.pps.univ-paris-diderot.fr/doku.php), Institute Henri Poincare. 77 | + [2014 Coalgebra](http://www.cs.ru.nl/is/education/courses/2014/coalgebra/), Sam Staton and Alexandra Silva. 78 | + [2014 Software Foundations](http://www.seas.upenn.edu/~cis500/current/index.html), Stephanie Weirich. 79 | + [2014 Programs and Proofs: mechanizing math with dependent types](http://ilyasergey.net/pnp-2014/), Ilya Sergey. 80 | + [2013 Homotopy Type Theory graduate seminar](http://www.cs.cmu.edu/~rwh/courses/hott/), [Robert Harper](http://www.cs.cmu.edu/~rwh/). 81 | + [2013 Dependently typed metaprogramming](http://www.cl.cam.ac.uk/~ok259/agda-course-13/), [Connor McBride](http://strictlypositive.org/). 82 | + [2013 Functional programming principles in Scala](https://www.coursera.org/course/progfun), Martin Odersky. 83 | + [2012 short course on category theory](http://youtu.be/ZKmodCApZwk), Steve Awodey ([textbook](http://carlossicoli.free.fr/A/Awodey_S.-Category_theory-Oxford_University_Press,_USA(2010).pdf)). 84 | + [2012 short course on topology for functional programming](http://www.cs.bham.ac.uk/~mhe/.talks/EWSCS2012/), Martin Escardo. 85 | + [2011 Introduction to Formal Reasoning](http://www.cs.nott.ac.uk/~txa/g52ifr/) [Thorsten Altenkirch](https://www.nottingham.ac.uk/computerscience/People/thorsten.altenkirch). 86 | + [2011 course on type theory and Coq](http://www.cs.ru.nl/~freek/courses/tt-2011/), Radboud University, NL. 87 | + [2011 course on verifying algorithms in Coq](http://www.di.ens.fr/~zappa/teaching/coq/ecole11/), CEA-EDF-INRIA, FR. 88 | + [2010 Categorical Logic](http://www.cl.cam.ac.uk/teaching/0910/L20/), Samuel Staton. 89 | + **Oregon Programming Languages Summer School** 90 | - [2019: probabilistic programming and security](https://www.cs.uoregon.edu/research/summerschool/summer19/) 91 | - [2018: parallelism and concurrency](https://www.cs.uoregon.edu/research/summerschool/summer18/index.php) 92 | - [2017: a spectrum of types.](https://www.cs.uoregon.edu/research/summerschool/summer17/) 93 | - [2016: types, logic, semantics, and verification.](https://www.cs.uoregon.edu/research/summerschool/summer16/) 94 | - [2015: types, logic, semantics, and verification.](https://www.cs.uoregon.edu/research/summerschool/summer15/index.php) 95 | - [2014: types, logic, semantics, and verification.](http://www.cs.uoregon.edu/research/summerschool/summer14/index.html) 96 | - [2013: types, logic, and verification.](http://www.cs.uoregon.edu/research/summerschool/summer13/) 97 | - [2012: logic, languages, compilation, and verification.](http://www.cs.uoregon.edu/research/summerschool/summer12/) ([Lectures@YouTube](http://www.youtube.com/playlist?list=PL8Ky8lYL8-Oh7awp0sqa82o7Ggt4AGhyf)) 98 | + **Midlands Graduate School** on foundations of computer science 99 | - [2019: Birmingham](http://events.cs.bham.ac.uk/mgs2019/) 100 | - [2018: Nottingham](http://www.cs.nott.ac.uk/~psznhn/MGS2018/) 101 | - [2017: Leicester](http://www.cs.le.ac.uk/events/mgs2017/) 102 | - [2016: Birmingham](http://www.cs.bham.ac.uk/~pbl/mgs2016/) 103 | - [2015: Sheffield](http://staffwww.dcs.shef.ac.uk/people/G.Struth/mgs2015/mgs.html) 104 | - [2014: Nottingham](http://www.cs.nott.ac.uk/~txa/mgs.2014/) 105 | + **CMU courses** 106 | - [2013 grad seminar on HoTT](http://www.cs.cmu.edu/~rwh/courses/hott/), [Robert Harper](http://www.cs.cmu.edu/~rwh/), with [recorded lectures][]. 107 | - [2012 grad course on linear logic](http://www.cs.cmu.edu/~fp/courses/15816-s12/), Frank Pfenning. 108 | - [2010 grad course on modal logic](http://www.cs.cmu.edu/~fp/courses/15816-s10/), Frank Pfenning and André Platzer. 109 | - [2009 undergrad course on constructive logic](http://www.cs.cmu.edu/~fp/courses/15317-f09/index.html), Frank Pfenning and Ron Garcia. 110 | 111 | ------------------------------------------------------ 112 | 113 | ## Programming Languages (in alphabetical order) 114 | + [Agda][] 115 | - General Resources 116 | * [The Agda Wiki](http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.HomePage) 117 | * [Dependent Types at Work](http://www.cse.chalmers.se/~peterd/papers/DependentTypesAtWork.pdf), introductory tutorial by Ana Bove and Peter Dybjer. 118 | * [Computer aided formal reasoning](http://www.cs.nott.ac.uk/~psztxa/g53cfr/) (course), [Thorsten Altenkirch](https://www.nottingham.ac.uk/computerscience/People/thorsten.altenkirch), 2010. 119 | * [Agda Tutorial in Advanced Functional Programming](http://www.cse.chalmers.se/~abela/AFP-2021/html/Everything.html), [Andreas Abel](http://www.cse.chalmers.se/~abela/), 2021. 120 | - Resources with a Mathematics focus 121 | * [Introduction to Univalent Foundations with Agda](https://www.cs.bham.ac.uk/~mhe/HoTT-UF-in-Agda-Lecture-Notes/HoTT-UF-Agda.html), [Martín Escardó](https://www.cs.bham.ac.uk/~mhe/), 2019. 122 | * [The Agda Universal Algebra Library](https://ualib.gitlab.io), [William DeMeo](https://williamdemeo.gitlab.io), 2021. 123 | * [YouTube: Cubical Agda and its extensions](https://youtu.be/9RFt1Q2pHE8), [Andrea Vezzosi](https://saizan.github.io/), 2021. 124 | * [Seemingly impossible constructive proofs](http://math.andrej.com/2014/05/08/seemingly-impossible-proofs/), [Martín Escardó](https://www.cs.bham.ac.uk/~mhe/), 2014. 125 | - Resources with a Programming Languages focus 126 | * [Programming Language Foundations in Agda](https://plfa.github.io/), Philip Wadler. 127 | * [Verified functional programming in Agda](http://www.amazon.com/Verified-Functional-Programming-Agda-Books/dp/1970001240/) (book), Aaron Stump. 128 | * [Dependently typed metaprogramming](http://www.cl.cam.ac.uk/~ok259/agda-course-13/) (course), [Conor McBride](http://strictlypositive.org/), 2013. 129 | 130 | + [Coq][] 131 | - [Software Foundations](http://www.cis.upenn.edu/~bcpierce/sf/) (book), Pierce, et al. 132 | - [Certified Programming with Dependent Types](http://adam.chlipala.net/cpdt/cpdt.pdf) (book), Adam Chlipala. 133 | - [YouTube: introductory Coq tutorials](https://www.youtube.com/playlist?list=PL5FJyaC2WsVlcWB4we3sPe6t09Vviu3Hn), by [Andrej Bauer](http://www.andrej.com/). 134 | - [Gentle Intro to Type Classes in Coq](http://www.labri.fr/perso/casteran/CoqArt/TypeClassesTut/typeclassestut.pdf), Casteran and Sozeau. 135 | - [Type Classes for Math in Type Theory (paper)](http://arxiv.org/abs/1102.1323v1) [(repository)](https://coq.inria.fr/cocorico/MathClasses), Spitters and van der Weegen. 136 | - [General Algebra in Coq](http://www-sop.inria.fr/lemme/Venanzio.Capretta/universal_algebra.html) [(thesis)](http://www.cs.nott.ac.uk/~pszvc/publications/Abstraction_Computation.pdf), [Venanzio Capretta](http://www.duplavis.com/venanzio/index.html). 137 | - [UniMath Coq Library for univalent foundations](https://github.com/UniMath/UniMath), Voevodsky, et al. 138 | - [Type theory and Coq](http://www.cs.ru.nl/~freek/courses/tt-2011/) (course) Radboud University, NL. 139 | - [Software Foundations in Coq](http://web.cecs.pdx.edu/~apt/coq_hints.html), tips from OPLSS. 140 | 141 | + [Haskell](http://www.haskell.org/) 142 | - [Learn You a Haskell for Great Good!](http://learnyouahaskell.com/) (book), a beginner's guide that's free to read online. 143 | - [Stephen Diehl](http://www.stephendiehl.com/posts.html) has some nice posts covering things like monads in Haskell. 144 | - [School of Haskell](https://www.fpcomplete.com/school) at fpcomplete.com, a commercial site that seems to allow free creation and sharing of online projects; 145 | offers many resources for learning Haskell. 146 | - [A monad for infinite search in finite time](http://math.andrej.com/2008/11/21/a-haskell-monad-for-infinite-search-in-finite-time/), by [Martín Escardó](https://www.cs.bham.ac.uk/~mhe/). 147 | 148 | + [Idris](https://www.idris-lang.org/) 149 | - [The Idris Tutorial](http://docs.idris-lang.org/en/latest/tutorial/index.html). 150 | - [Type-Driven Development with Idris](https://www.manning.com/books/type-driven-development-with-idris) (book), Edwin Brady. 151 | - [Kats Idris Workshop](http://www.idris-lang.org/documentation/kats-workshop-may-2016/), Dublin, IR, 2016. 152 | - [Idris Course at ITU](https://edwinb.wordpress.com/2013/03/15/idris-course-at-itu-slides-and-video/), [Edwin Brady](https://edwinb.wordpress.com/), Copenhagen, DK, 2013. 153 | 154 | 155 | + [Lean](https://leanprover.github.io/) 156 | - [Official homepage](https://leanprover.github.io/) 157 | - [Official github](https://github.com/leanprover) 158 | - [Community homepage](https://leanprover-community.github.io/) 159 | - [Community github](https://github.com/leanprover-community) 160 | - **Lean Live!** or, "Blean" (weB lean) 161 | + [official](https://leanprover.github.io/live/3.4.1/) 162 | + [lean-community](https://leanprover-community.github.io/lean-web-editor/) 163 | - **Documentation** 164 | + [Intro to Lean](https://leanprover.github.io/introduction_to_lean) 165 | + [Theorem Proving in Lean](https://leanprover.github.io/theorem_proving_in_lean) 166 | + [Programming in Lean](https://leanprover.github.io/programming_in_lean) 167 | + [Logic and Proof](https://leanprover.github.io/logic_and_proof/) ([source](https://github.com/leanprover/logic_and_proof)), CMU Undergrad Course. 168 | + [ualib.org](https://ualib.gitlab.io/) documentation for the Lean Universal Algebra Library. 169 | - [Lean zulip chatroom](https://leanprover.zulipchat.com/login/) 170 | - [Lean zulip archive](https://leanprover-community.github.io/archive/) 171 | 172 | + [Scala][] 173 | - [Functional Programming in Scala](https://www.manning.com/books/functional-programming-in-scala) (book), Paul Chiusano and Rúnar Bjarnason, 2014. 174 | - [Functional Programming Principles in Scala](https://www.coursera.org/course/progfun) (course), Martin Odersky. 175 | - [Principles of Reactive Programming](https://www.coursera.org/course/reactive) (course), Odersky, Meijer, Kuhn. 176 | - [Ninety-Nine Scala Problems](http://aperiodic.net/phil/scala/s-99/), Gold and Hett. 177 | 178 | ------------------------------------------------------ 179 | 180 | ## Some Related GitHub Repositories 181 | 182 | (alphabetical) 183 | 184 | + [agda/agda](https://github.com/agda/agda) 185 | + [atom/atom](https://github.com/atom/atom) 186 | + [coq/coq](https://github.com/coq/coq) 187 | + [databricks/learning-spark](https://github.com/databricks/learning-spark) to accompany the book "Learning Spark" 188 | + [edwinb/TypeDD-Samples](https://github.com/edwinb/TypeDD-Samples) to accompany the book "TDD in Idris" 189 | + [fpinscala/fpinscala](https://github.com/fpinscala/fpinscala) to accompany the book "FP in Scala" 190 | + [HoTT/book](https://github.com/HoTT/book) for the HoTT book (clone it to get the latest edition) 191 | + [idris-lang/Idris-dev](https://github.com/idris-lang/Idris-dev) 192 | + [java8/Java8InAction](https://github.com/java8/Java8InAction) to accompany the book "Java 8 in Action" 193 | + [scala/scala](https://github.com/scala/scala) 194 | + [spark-in-action/first-edition](https://github.com/spark-in-action/first-edition) to accompany the book "Spark in Action" 195 | + [ualib/ualib.gitlab.io](https://gitlab.com/ualib/ualib.gitlab.io) source code for [The Agda Universal Algebra Library][] 196 | 197 | ## Miscellaneous 198 | + [homotopytypetheory.org][] 199 | + [Francois Dorais' blog posts](http://logic.dorais.org/archives/1448) about doing math in HoTT. 200 | + [types-forum][] and [types-announce][] mailing lists. 201 | + [types project][] 202 | 203 | [types-forum]: http://lists.seas.upenn.edu/mailman/listinfo/types-list 204 | [types-announce]: http://lists.seas.upenn.edu/mailman/listinfo/types-announce 205 | [Experimental library of univalent formalization of mathematics]: http://arxiv.org/abs/1401.0053 206 | [types project]: http://www.cse.chalmers.se/research/group/logic/Types/index.html 207 | [Scala]: http://www.scala-lang.org/ 208 | [Agda]: http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.HomePage 209 | [Coq]: http://coq.inria.fr/ 210 | [The HoTT Book]: http://homotopytypetheory.org/book/ 211 | [recorded lectures]: http://scs.hosted.panopto.com/Panopto/Pages/Sessions/List.aspx#folderID="07756bb0-b872-4a4a-95b1-b77ad206dab3" 212 | [2011 Course on Type Theory and Coq]: http://www.cs.ru.nl/~freek/courses/tt-2011/ 213 | [Short course on type theory and programming]: http://www.cse.chalmers.se/~bengt/course/typetheory-oneweek.html 214 | [2013 course "Functional programming principles in Scala"]: https://www.coursera.org/course/progfun 215 | [2008 course "Introduction to Type Theory"]: http://www.cs.ru.nl/~herman/Uruguay2008SummerSchool.html 216 | [Notes from CMU HoTT course]: https://github.com/favonia/hott-notes 217 | [notes directory]: https://github.com/williamdemeo/TypeFunc/tree/master/notes 218 | [the main repository]: https://github.com/favonia/hott-notes 219 | [Interactive Theorem Proving for Agda Users]: http://www.cs.swan.ac.uk/~csetzer/lectures/intertheo/07/interactiveTheoremProvingForAgdaUsers.html 220 | [github.com/HoTT]: https://github.com/HoTT/book 221 | [homotopytypetheory.org]: http://homotopytypetheory.org/ 222 | [Category Theory]: http://carlossicoli.free.fr/A/Awodey_S.-Category_theory-Oxford_University_Press,_USA(2010).pdf 223 | [The Agda Universal Algebra Library]: https://ualib.gitlab.io 224 | -------------------------------------------------------------------------------- /lit/Geuvers-IntroTT-2008.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/lit/Geuvers-IntroTT-2008.pdf -------------------------------------------------------------------------------- /lit/Girard-ProofsAndTypes-1990.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/lit/Girard-ProofsAndTypes-1990.pdf -------------------------------------------------------------------------------- /lit/Scott-TypeTheoreticAlternative-1993.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/lit/Scott-TypeTheoreticAlternative-1993.pdf -------------------------------------------------------------------------------- /notes/MGS/Jung/Jung-2014-Teaching-denotational-semantics.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/MGS/Jung/Jung-2014-Teaching-denotational-semantics.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2014/Birkedal-categorical-logic-tutorial-notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2014/Birkedal-categorical-logic-tutorial-notes.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2014/Pientka-ProofTheoryNotes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2014/Pientka-ProofTheoryNotes.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2015/Constable-OPLSS-Short-2015-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2015/Constable-OPLSS-Short-2015-2.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2015/Morehouse-IntroCategoricalSemantics.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2015/Morehouse-IntroCategoricalSemantics.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Downen/Downen-ReviewLectures.md: -------------------------------------------------------------------------------- 1 | OPLSS program 2 | ⟨2017-06-23 Fri⟩ 3 | 4 | Welcome from Zena M. Ariola 5 | 6 | First year for review. 7 | 8 | **U of O people** 9 | Zach 10 | Anna 11 | 12 | **Review** 13 | Dr. Paul Downen 14 | University of Oregon 15 | 16 | ## Day 1: STLC (simply typed lambda calculus) 17 | 18 | ### Plan 19 | 1. lambda calculus 20 | 2. Products and Sums 21 | 3. Operational Semantics 22 | 23 | ### Untyped Lambda Calculus 24 | only has 3 things 25 | M, N, P ∈ Term ::= x | λx.M | M N 26 | surprisingly tricky to get right 27 | 28 | x, y, z ∈ Variable ::= ... 29 | might write λx.M as Lam(x.M) 30 | might write M N as ap(M;N) 31 | 32 | ### Bound variables 33 | BV(x) = {} 34 | BV(λx.M) = BV(M) ∪ {x} 35 | BV(M N) = B(M) ∪ BV(N) 36 | 37 | ### Free variables 38 | FV(x) = {x} 39 | FV(λx.M) = FV(M) - {x} 40 | FV(M N) = FV(M) ∪ FV(N) 41 | 42 | ### Substitution (capture avoiding substitution) 43 | M[N/x] replace occurrences of x with N 44 | x[N/x] = N (variable) 45 | y[N/x] = y (x ≠ y) (variable) 46 | (λx.M)[N/x] = λx.M (abstraction) 47 | (λy.M)[N/x] = λy.(M[N/x]) if y ∉ FV(N) (abstraction) 48 | (λy.M)[N/x] = (need renaming) if y ∈ FV(N) (abstraction) 49 | (M P)[N/x] = (M[N/x]) (P[N/x]) (application) 50 | 51 | Substitution is a partial function, because of the need for renaming. 52 | 53 | If we do multiple substitutions, order matters. 54 | (There is a commutation relation.) 55 | 56 | #### Let in terms of lambda 57 | Let x = M in N := (λx.N) M 58 | syntactic sugar 59 | 60 | #### Renaming 61 | α, β, γ are equivalence relations 62 | 63 | ##### α law (what functions are) 64 | λx.M =α λy.(M[y/x]), y ∉ FV(M) 65 | We work with terms up to α-equivalence. 66 | decidable 67 | about renaming 68 | 69 | ##### β law (what functions do) 70 | (λx.M) N =β M[N/x] 71 | the computational engine 72 | undecidable 73 | 74 | ##### η law (extensionality) 75 | M =η λx. Mx, x ∉ FV(M) 76 | extensionality is sometimes the same as "function extensionality" 77 | η rule used for reasoning, not for computation 78 | left and right sides would have different syntax trees 79 | A compiler should respect η. 80 | A compiler could use η for optimization. 81 | decidable 82 | 83 | #### function extensionality 84 | If for all inputs P, M P = N P then M = N. 85 | In the λ-calculus, function extensionality is equivalent to the η law. 86 | In other calculi, maybe not equivalent. 87 | (Function extensionality is usually stronger when they are different.) 88 | 89 | #### Böhm separability 90 | Thms about untyped lambda calculus. 91 | Alpha, beta, gamma give the "largest equivalence relation" that doesn't break. 92 | 93 | #### Simple thms 94 | M =β N implies M[P/x] =β N[P/x] (substitution respects beta) 95 | ### Compatibility rules 96 | you can apply alpha, beta, gamma in any setting 97 | ## Types 98 | types are contracts 99 | 100 | X, Y, Z ∈ TypeVariable ::= ... 101 | A, B, C ∈ Type ::= X | A → B 102 | 103 | Judgment: Γ ⊢ M : A (a 3-place thing) (⊢ is produced by \vdash) 104 | 105 | **Example:** Γ = x₁ : A₁, x₂ : A₂, x₃ : A₃, ..., xₙ : Aₙ 106 | 107 | Shadowing convention: Γ, x : A, x : B, Γ' = Γ, x : B, Γ' (rightmost thing wins) 108 | 109 | Exchange: Γ, x : A, y : B, Γ' = Γ, y : B, x : A, Γ' (x ≠ y) 110 | 111 | Inference rules: (things with long horizontal lines) 112 | 113 | ———————————————————— (Var) 114 | Γ, x : A ⊢ x : A 115 | 116 | Γ, x : A ⊢ M : B 117 | ————————————————————– (→ I) (function introduction) 118 | Γ ⊢ λx.M : A → B 119 | 120 | Γ ⊢ M : A → B Γ ⊢ N : A 121 | ————————————————————–———— (→ E) (function elimination) 122 | Γ ⊢ M N : B 123 | 124 | This is type inference. 125 | It's a bit different from type checking. 126 | (In type checking we know the goal.) 127 | 128 | We have already baked in the idea that terms have unique types. 129 | 130 | **Theorem:** 131 | If Γ, x : A ⊢ M : B and Γ ⊢ N : A 132 | then Γ ⊢ M[N/x] : B. 133 | 134 | ## Products and Sums 135 | The λ-calulus had 3 parts: 136 | 1. syntax 137 | 2. laws (alpha, beta, eta) 138 | 3. type derivations (static semantics) 139 | 140 | 141 | ### Syntax 142 | x, y, z ∈ Variable ::= ... 143 | 144 | M, N, P ∈ Term ::= x | λx.M | M N | ⟨M,N⟩ 145 | | fst M | snd M 146 | | inl M | inr M 147 | | (case M of inl x ⇒ N 148 | | inr y ⇒ P) 149 | 150 | A, B, C ∈ Type ::= X | A → B | A × B | A + B 151 | 152 | We added six terms and we added two types. 153 | 154 | Could have "fst" in the grammar instead of "fst M", 155 | but that would require polymorphism in the language. 156 | 157 | Alternative to (case M of inl x ⇒ N | inr y ⇒ P) 158 | is case(M; x.N; y.P) 159 | 160 | "case" is a new binding construct (binding x and y) 161 | 162 | ### Substitution 163 | ⟨M,P⟩[N/x] = ⟨M[N/x],P[N/x]⟩ 164 | 165 | ### Laws 166 | 167 | #### α law 168 | no alpha law for products 169 | 170 | case M of inl x ⇒ N | inr y ⇒ P 171 | =α case M of inl x' ⇒ (N[x'/x]) | inr y' ⇒ (P[y'/y]) 172 | 173 | #### β law 174 | fst ⟨M,N⟩ =β M 175 | snd ⟨M,N⟩ =β N 176 | 177 | case inl M of inl x ⇒ N 178 | | inr y ⇒ P =β= N[M/x] 179 | 180 | 181 | case inr M of inl x ⇒ N 182 | | inr y ⇒ P =β= P[M/y] 183 | 184 | #### η law 185 | 186 | M : A × B =η ⟨fst M, snd M⟩ (if M typecheks at A × B ....) 187 | 188 | M : A → B =η λx. Mx 189 | 190 | M : A + B =η case M of inl x ⇒ inl x | inr y ⇒ inr y 191 | 192 | ### Typing rules 193 | 194 | Γ ⊢ M : A Γ ⊢ N : B 195 | ————————————————————– (×I) (product introduction) 196 | Γ ⊢ ⟨M,N⟩ : A × B 197 | 198 | Γ ⊢ M : A × B 199 | ————————————– (×E₁) (product elimination 1) 200 | Γ ⊢ fst M : A 201 | 202 | Γ ⊢ M : A × B 203 | ————————————– (×E₂) (product elimination 2) 204 | Γ ⊢ snd M : B 205 | 206 | Γ ⊢ M : A 207 | ————————–———————– (+I₁) (sum introduction 1) 208 | Γ ⊢ inl M : A + B 209 | 210 | Γ ⊢ M : B 211 | ————————–———————– (+I₂) (sum introduction 2) 212 | Γ ⊢ inr M : A + B 213 | (sum elimination) 214 | Γ ⊢ M : A + B Γ,x:A ⊢ N : C Γ,y:B ⊢ P : C 215 | ————————————————————————————————————————————– (+E) 216 | Γ ⊢ case M of inl x ⇒ N | inr y ⇒ P : C 217 | 218 | With sums, we lose unique typing. 219 | We could save it by annotating. 220 | 221 | ### Example 222 | swap : A+B → B+A 223 | swap = λx. case x of inl y ⇒ inr y | inr z ⇒ inl z 224 | 225 | Type derivation tree: (writing from bottom up) 226 | 227 | ———————————————–(Var) ————————————————–(Var) 228 | x:A+B, y:A ⊢ y:A x:A+B, z:B ⊢ z:B 229 | ————————————–(Var) ——————————————————————–(+I₂) ——————————————————————–(+I₁) 230 | x:A+B ⊢ x:A+B x:A+B, y:A ⊢ inr y :B+A x:A+B, z:B ⊢ inl z :B+A 231 | ———————————————————————————————————————————————————————————–————————–(+E) 232 | x:A+B ⊢ case x of inl y ⇒ inr y | inr z ⇒ inl z : B+A 233 | ————————————————————————————————————————————————————————–(→I) 234 | ⊢ λx. case x of inl y ⇒ inr y | inr z ⇒ inl z : A+B → B+A 235 | 236 | Some discussion of "weakening" took place. 237 | 238 | **Weakening:** 239 | 240 | Γ ⊢ M:A 241 | ————————–———— weakening 242 | x:B, Γ ⊢ M:A 243 | 244 | We did not include weakening in our system, but we could prove it as a theorem. 245 | 246 | **Contraction:** 247 | 248 | Γ, x:A, y:A ⊢ M:B 249 | ————————————————–– contraction 250 | Γ, x:A ⊢ M[x/y]:B 251 | 252 | Check that it works: 253 | 254 | (λx. case x of inl y ⇒ inr y 255 | | inr z ⇒ inl z) (inr w) 256 | =ᵦ case inr w of inl y ⇒ inr y 257 | | inr z ⇒ inl z 258 | =ᵦ inl w 259 | 260 | ## Operational Semantics 261 | 262 | Curry style: define an untyped evaluator. 263 | 264 | Church style: program doesn't even have a meaning until it's been typed. 265 | 266 | We will follow Curry style here. 267 | 268 | Small-step semantics 269 | 270 | M ↦ N 271 | M ↦* N Kleane closure 272 | 273 | M ↦ N 274 | ————–– —————– 275 | M ↦* N M ↦* M 276 | 277 | M ↦* N N ↦* P 278 | ——————————————– 279 | M ↦* P 280 | 281 | ### Axioms 282 | (λx.M) N ↦ M[N/x] 283 | fst ⟨M,N⟩ ↦ M 284 | snd ⟨M,N⟩ ↦ N 285 | case inl M of inl y ⇒ N | inr z ⇒ P ↦ N[M/y] 286 | case inr M of inl y ⇒ N | inr z ⇒ P ↦ P[M/z] 287 | 288 | ### Inference rules 289 | M ↦ M' 290 | —————–—–—– 291 | M N ↦ M' N 292 | 293 | M ↦ M' 294 | —————–—–—–———– 295 | fst M ↦ fst M' 296 | 297 | M ↦ M' 298 | —————–—–—–———– 299 | snd M ↦ snd M' 300 | 301 | M ↦ M' 302 | —————–—–—–———–——————————————————————————————— 303 | case M of inl y ⇒ N 304 | | inr z ⇒ P ↦ case M' of inl y ⇒ N 305 | | inr z ⇒ P 306 | 307 | ### Evaluation context 308 | term with a hole in it 309 | 310 | E ∈ EvalCtxt ::= □ | E M | fst E | snd E 311 | | (case E of inl x ⇒ M | inr y ⇒ N) 312 | 313 | evaluation context tells where evaluation happens 314 | 315 | (\square is □) 316 | 317 | M ↦ M' 318 | ———————————– 319 | E[M] ↦ E[M'] 320 | 321 | E[M] is a Term 322 | 323 | □ [M] = M 324 | (E N)[M] = E[M] N 325 | (fst E)[M] = fst (E[M]) 326 | (snd E)[M] = snd (E[M]) 327 | (case E of inl x ⇒ N 328 | | inr y ⇒ P)[M] = case E[M] of inl x ⇒ N 329 | | inr y ⇒ P 330 | 331 | Continuations are related to evaluation contexts. 332 | 333 | #### Scott's examples 334 | (□ N)[M] = □[M] M = M N (M fills the hole) 335 | (fst □)[M] = fst (□[M]) = fst M (M fills the hole) 336 | (case □ of inl x ⇒ N 337 | | inr y ⇒ P)[M] = case M of inl x ⇒ N 338 | | inr y ⇒ P 339 | 340 | (fst (snd □))[M] = fst ((snd □)[M]) = fst (snd M) 341 | 342 | ### Example 343 | case fst ⟨inr 1, inl 5⟩ of inl x ⇒ x*2 | inr y ⇒ y+3 344 | = (case □ of ...)[fst ⟨inr 1, inl 5⟩] 345 | ↦ case inr 1 of inl x ⇒ x*2 | inr y ⇒ y+3 346 | ↦ 1 + 3 347 | 348 | ### Unique Decomposition 349 | **Theorem:** 350 | For every term M, either 351 | 1. there exists an evaluation context E and term N 352 | such that M = E[N] and N ↦ N', or 353 | 2. M is a result/value 354 | (i.e., an introduction form λx.M', ⟨M₁,M₂⟩, inl M', inr M') 355 | 3. M is stuck (M not ↦) 356 | 357 | ### Call by value 358 | We did call by name. 359 | We would set everything up differently for call by value. 360 | EvalCtxt would be defined differently, for example. 361 | 362 | Call-by-name: 363 | (λx. x∗x) (2+2) ↦ (2+2) ∗ (2+2) 364 | 365 | Call-by-value: 366 | (λx. x∗x) (2+2) ↦ (λx. x∗x) 4 ↦ 4∗4 367 | 368 | Call-by-name is dual to call-by-value. (There is a paper on this.) 369 | 370 | Call-by-need is where you memoize results, so you don't evaluate twice. 371 | 372 | 373 | ### Big-step operational semantics 374 | M ⇓ N 375 | We'll do call-by-name. 376 | 377 | (⇓ is \Downarrow) 378 | 379 | #### Axioms 380 | x ⇓ x 381 | λx.M ⇓ λx.M 382 | ⟨M,N⟩ ⇓ ⟨M,N⟩ 383 | inl M ⇓ inl M 384 | inr M ⇓ inr M 385 | #### Inference rules 386 | 387 | M ⇓ λx.M' M'[N/x] ⇓ P 388 | ——————————————————————– 389 | M N ⇓ P 390 | 391 | M ⇓ ⟨M₁,M₂⟩ M₁ ⇓ P 392 | ———————————————————— 393 | fst M ⇓ P 394 | 395 | M ⇓ ⟨M₁,M₂⟩ M₂ ⇓ P 396 | ———————————————————— 397 | snd M ⇓ P 398 | 399 | M ⇓ inl M' N₁[M'/x] ⇓ P 400 | ——————————————————————————————————————– 401 | case M of inl x ⇒ N₁ | inr y ⇒ N₂ ⇓ P 402 | 403 | M ⇓ inr M' N₂[M'/x] ⇓ P 404 | ——————————————————————————————————————– 405 | case M of inl x ⇒ N₁ | inr y ⇒ N₂ ⇓ P 406 | #### Example 407 | 408 | ——————————————————————————————– 409 | ⟨inr 1, inl 5⟩ ⇓ ⟨inr 1, inl 5⟩ 410 | —————–——————————————————–—————– 411 | fst ⟨inl1, inr 5⟩ ⇓ inr 1 1+3 ⇓ 4 412 | —————————————————————————————————————————————————————————– 413 | case fst ⟨inr 1, inl 5⟩ of inl x ⇒ x*2 | inr y ⇒ y+3 ⇓ 4 414 | ## Type Safety 415 | progress and preservation are syntactic theorems 416 | 417 | V, W ∈ Value ::= λx.M | ⟨M,N⟩ | inl M | inr M 418 | 419 | Definition: N is *stuck* when N is not a value and cannot advance. 420 | 421 | Theorem (Type Safety): If ⊢ M : A is derivable and M ↦* N 422 | then N is not stuck. 423 | 424 | Proof is based on Progess and Preservation lemmas. 425 | 426 | The simply typed lambda calculus is normalizing 427 | (all programs terminate), but the type safety theorem 428 | applies also to languages that do not terminate. 429 | 430 | ### Example of structural induction 431 | 432 | From yesterday: 433 | 434 | M ↦* N N ↦* P 435 | ——————————————– 436 | M ↦* P 437 | 438 | Definition of ⤇: 439 | 440 | M ↦ N N ⤇ P 441 | ——————————————– 442 | M ⤇ P 443 | 444 | —————– 445 | M ⤇ M 446 | 447 | Double arrow ⤇ associates to the right. 448 | 449 | **Lemma:** M ↦* P implies that either M ↦ N ↦* P or M = P. 450 | *Proof:* By induction on M ↦* P 451 | 452 | —————– refl 453 | M ↦* M 454 | 455 | M ↦ N 456 | —————– ⇒ —————————— refl 457 | M ↦* N M ↦ N ↦* N 458 | 459 | M ↦* N N ↦* P 460 | ——————————————– 461 | M ↦* P 462 | 463 | (more stuff) 464 | 465 | 466 | **Theorem:** M ↦* P iff M ⤇ P 467 | *Proof:* M ↦* P imples M ⤇ P by induction on the inference rules for M ↦* P. 468 | 469 | Suppose 470 | 471 | M ↦* N N ↦*P 472 | —————————————– 473 | M ↦* P 474 | 475 | M₁ ↦ M₂ M₂ ↦ M₃ 476 | ———————– ———————– 477 | M₁ ↦* M₂ M₂ ↦* M₃ M₃ ↦ M₄ 478 | ——————————————————— ———————– 479 | M₁ ↦* M₃ M₃ ↦* M₄ 480 | ——————————————————————————————— 481 | M₁ ↦* M₄ 482 | 483 | implies 484 | (something here) 485 | 486 | M₁ ↦ M₂ M₂ ⤇ M₄ 487 | ——————————————————————————————— 488 | M₁ ⤇ M₄ 489 | 490 | This is rule induction. 491 | 492 | ### Canonical Forms 493 | 494 | **Lemma:** 495 | 1. ⊢ V : A → B imples V = λx.M 496 | 2. ⊢ V : A × B imples V = ⟨M,N⟩ 497 | 3. ⊢ V : A + B imples either V = inl M or V = inr M 498 | 499 | ### Progress 500 | **Theorem** (Progress): 501 | If ⊢ M : A is derivable then either M is a value or there exists 502 | an N such that M ↦ N. 503 | 504 | (If M is derivable them M is not stuck.) 505 | 506 | No environment because we want to rule out (x 5). 507 | Progress is for closed terms only. 508 | 509 | *Proof:* By induction on the derivation of ⊢ M : A. 510 | There are 9 typing rules we have to check 511 | (Var, →I, →E, ×I, ×E₁, ×E₂, +I₁, +I₂, +E). 512 | 513 | __Case Var:__ impossible. 514 | 515 | What about function introduction? In this case, M is a value. 516 | All the introduction rules imply that M is a value. 517 | 518 | __Case →E:__ Need induction hypothesis and evaluation context. 519 | 520 | __Case ×E:__ Again need induction hypothesis and evaluation context. 521 | 522 | ⊢ M : A × B 523 | ———————————(×E₁) 524 | ⊢ fst M : A 525 | 526 | by IH: either M is a value or M ↦ M' 527 | If M is a value, then M = ⟨M₁,M₂⟩ 528 | fst M = fst ⟨M₁,M₂⟩ ↦ M₁, so we can take a step. 529 | If M ↦ M', fst □ is an evaluation context, so we can take step. 530 | 531 | M ↦ M' 532 | ———————–—————– 533 | fst M ↦ fst M' 534 | 535 | __Case +E:__ 536 | 537 | ⊢ M : A + B x:A ⊢ N : C y:B ⊢ P : C 538 | ———————————————————————————————————————(+E) 539 | ⊢ case M of inl x ⇒ N | inr y ⇒ P : C 540 | 541 | by IH: either M is a value or M ↦ M' 542 | If M is a value, then either M = inl M', or M = inr M'. 543 | 544 | ### Preservation 545 | **Theorem** (Preservation): 546 | If Γ ⊢ M : A is derivable and M ↦ N then Γ ⊢ N : A is derivable. 547 | 548 | **Lemma** (Typed Substitution): 549 | If Γ,x:A ⊢ M : B and Γ ⊢ N : A then Γ ⊢ M[N/x] : B. 550 | 551 | *Proof:* By induction on Γ,x:A ⊢ M : B. 552 | 553 | __Case ×I:__ 554 | 555 | Γ,x:A ⊢ M₁ : B₁ Γ,x:A ⊢ M₂ : B₂ 556 | ————————————————————–———————————–(×I) 557 | Γ,x:A ⊢ ⟨M₁,M₂⟩ : B₁ × B₂ 558 | 559 | by IH: 560 | 561 | Γ ⊢ M₁[N/x] : B₁ 562 | Γ ⊢ M₂[N/x] : B₂ 563 | 564 | ⟨M₁,M₂⟩[N/x] = ⟨M₁[N/x],M₂[N/x]⟩ 565 | 566 | There are many other cases... 567 | 568 | *Proof of main theorem:* By induction on the derivation of Γ ⊢ M : A, 569 | confirming that each evaluation step maintains type. 570 | The Var and introduction rules are not at the bottom of the derivation because they don't step. 571 | We need to look at the four elimination rules. 572 | 573 | __Case →E:__ 574 | 575 | Γ ⊢ M : A → B Γ ⊢ N : A 576 | —————————————————————–————(→E) 577 | Γ ⊢ M N : B 578 | 579 | What could M be? 580 | 581 | *Subcase 1:* If M = λx.M', then M N = (λx.M') N ↦ M'[N/x] 582 | 583 | **GOAL:** Derive Γ ⊢ M'[N/x] : B 584 | 585 | Γ, x : A ⊢ M' : B 586 | ———————————————————(→I) 587 | Γ ⊢ λx.M' : A → B 588 | 589 | By the Typed Substitution Lemma, we know Γ ⊢ M'[N/x] : B 590 | because Γ,x:A ⊢ M' : B and Γ ⊢ N : A, and we achieve the goal. 591 | 592 | *Subcase 2:* 593 | 594 | M ↦ M' 595 | —————–—–—– 596 | M N ↦ M' N 597 | 598 | __Case ×E:__ 599 | 600 | Γ ⊢ M : A × B 601 | ————————————– (×E₁) 602 | Γ ⊢ fst M : A 603 | 604 | *Subcase 1:* M = ⟨M₁,M₂⟩ 605 | 606 | Γ ⊢ M₁ : A Γ ⊢ M₂ : B 607 | ————————————————————–——(×I) 608 | Γ ⊢ ⟨M₁,M₂⟩ : A × B 609 | 610 | fst M = fst ⟨M₁,M₂⟩ ↦ M₁ 611 | 612 | We have shown that Γ ⊢ M₁ : A is derivable. 613 | 614 | *Subcase 2:* 615 | 616 | M ↦ M' 617 | —————–—–—–———– 618 | fst M ↦ fst M' 619 | 620 | __Case +E:__ 621 | 622 | Γ ⊢ M : A + B Γ,x:A ⊢ N : C Γ,y:B ⊢ P : C 623 | ————————————————————————————————————————————–(+E) 624 | Γ ⊢ case M of inl x ⇒ N | inr y ⇒ P : C 625 | 626 | *Subcase 1:* M = inl M' 627 | 628 | Γ ⊢ M' : A 629 | ————————–——————–—–(+I₁) 630 | Γ ⊢ inl M' : A + B 631 | 632 | case M of inl x ⇒ N | inr y ⇒ P 633 | = case inl M' of inl x ⇒ N | inr y ⇒ P 634 | ↦ N[M'/x] 635 | 636 | **GOAL:** Γ ⊢ N[M'/x] : C 637 | 638 | Use the Typed Substitution Lemma 639 | because Γ ⊢ M' : A and Γ,x:A ⊢ N : C. 640 | 641 | *Subcase 2:* M = inr M' 642 | 643 | *Subcase 3:* 644 | 645 | M ↦ M' 646 | —————–—–—–———–———————————————————————————— 647 | case M of inl y ⇒ N 648 | | inr z ⇒ P ↦ case M' of inl y ⇒ N 649 | | inr z ⇒ P 650 | 651 | by IH: Γ ⊢ M' : A + B 652 | 653 | ### Termination 654 | **Theorem** (Termination): 655 | For all terms M, where ⊢ M : A, there is a value V such that M ↦* V. 656 | 657 | **Lemma:** ⊢ M : A then there is no infinite reduction sequence. 658 | *Proof:* Induction on M. 659 | 660 | Case M = λx.M' 661 | 662 | (λx.M') N ↦ M'[N/x] 663 | 664 | **Problem:** term on the right need not be structural smaller, 665 | so we can't use the induction hypothesis. 666 | 667 | Need a different method... 668 | 669 | ...Logical Relations, Reducibility Candidate, Tait's Method. 670 | 671 | ## Recursion 672 | 673 | ### Primitive Recursion 674 | a type of recursion on numbers that's guaranteed to terminate 675 | 676 | We will look at System T (Gödel) 677 | 678 | A,B,C ∈ Type ::= X | A → B | Bool | Nat 679 | 680 | M, N, P ∈ Term ::= x | λx.M | M N 681 | | True | False 682 | | if M then N else P 683 | | Z | S M 684 | | (rec M of Z ⇒ N | S x with y ⇒ P) 685 | 686 | Binding tree: rec(M; N; x,y.P) 687 | 688 | V, W ∈ Value ::= λx.M | True | False | Z | S M 689 | 690 | E ∈ EvalCtxt ::= □ | E M | if E then M else N 691 | | (rec E of Z ⇒ N | S x with y ⇒ P) 692 | 693 | 694 | #### Rules for Bool 695 | 696 | (β) if True then N else P =ᵦ N 697 | if False then N else P =ᵦ P 698 | 699 | (η) M : Bool =η if M then True else False 700 | 701 | 702 | #### Rules for Nat 703 | 704 | (α) rec M of Z ⇒ N 705 | | S x with y ⇒ P 706 | =α rec M of Z ⇒ N 707 | | S x' with y' ⇒ P[x'/x,y'/y] 708 | 709 | (β) rec Z of Z ⇒ N 710 | | S x with y ⇒ P =ᵦ N 711 | 712 | rec S M of Z ⇒ N 713 | | S x with y ⇒ P 714 | =ᵦ P [M/x, (rec M of Z ⇒ N | S x with y ⇒ P)/y] 715 | 716 | (η) M : Nat =η rec M of Z ⇒ Z | S x with y ⇒ S x 717 | 718 | 719 | #### Introduction and Elimination Rules 720 | 721 | ——————————————–(Bool I₁) 722 | Γ ⊢ True : Bool 723 | 724 | 725 | ——————————————––(Bool I₂) 726 | Γ ⊢ False : Bool 727 | 728 | Γ ⊢ M : Bool Γ ⊢ N : C Γ ⊢ P : C 729 | ——————————————––———————————————————–(Bool E) 730 | Γ ⊢ if M then N else P : C 731 | 732 | 733 | ——————————–(Nat I₁) 734 | Γ ⊢ Z : Nat 735 | 736 | Γ ⊢ M : Nat 737 | ——————————–—–(Nat I₂) 738 | Γ ⊢ S M : Nat 739 | 740 | Γ ⊢ M : Nat Γ ⊢ N : C Γ,x:Nat,y:C ⊢ P : C 741 | ————————————————————————————————–—————————–—–(Nat E) 742 | Γ ⊢ rec M of Z ⇒ N | S x with y ⇒ P : C 743 | 744 | #### Examples 745 | add : Nat → Nat → Nat 746 | add = λx.λy.rec x of Z ⇒ y | S x' with w ⇒ S w 747 | 748 | pred : Nat → Nat 749 | pred = λx.rec x of Z ⇒ Z | S x' with y ⇒ x' 750 | 751 | add (S (S Z)) (S (S (S Z))) 752 | =ᵦ rec S (S Z) of Z ⇒ S (S (S Z)) | S x' with w ⇒ S w 753 | =ᵦ S (rec S Z of Z ⇒ S (S (S Z)) | S x' with w ⇒ S w) 754 | =ᵦ S (S (rec Z of Z ⇒ S (S (S Z)) | S x' with w ⇒ S w)) 755 | =ᵦ S (S (S (S (S Z)))) 756 | 757 | mult : Nat → Nat → Nat 758 | mult = λx.λy.rec x of Z ⇒ Z | S x' with y ⇒ add y w 759 | 760 | isZero : Nat → Bool 761 | isZero = λx.rec x of Z ⇒ True | S x' with y ⇒ False 762 | ### General Recursion 763 | Ω = (λx.x x) (λx.x x) 764 | ↦ (x x)[λx.x x/x] 765 | = (λx.x x) (λx.x x) 766 | 767 | Does Ω have a type? No. 768 | 769 | (more stuff) 770 | ———————————– 771 | ⊢ λx.x x : ? → ? ⊢ λx.x x : ? 772 | ——————————————————————–————————– →E 773 | ⊢ (λx.x x) (λx.x x) : ? 774 | 775 | Y = λf.((λx.f (x x)) (λx.f (x x))) (Y combinator) 776 | 777 | Y M =ᵦ (λx.M (x x)) (λx.M (x x)) 778 | =ᵦ M ((λx.M (x x)) (λx.M (x x))) 779 | =ᵦ M (Y M) 780 | 781 | Y M is a fixed point of M 782 | 783 | The Y combinator has no type. 784 | 785 | Let x = M in N := (λx.N) M 786 | 787 | Letrec x = M in N := (λx.N) (Y(λx.M)) 788 | =ᵦ (λx.N) ((λx.M)(Y(λx.M))) 789 | =ᵦ (λx.N) M[Y(λx.M)/x] 790 | := Letrec x = M[M/x] in N 791 | 792 | (β) Letrec x = M in N =ᵦ N[Letrec x = M in N/x] 793 | 794 | Letrec x = M in E[x] ↦ Letrec x = M in E[M] 795 | 796 | E ∈ EvalCtxt ::= ... | Letrec x = M in E 797 | 798 | 799 | **Example.** 800 | 801 | Letrec fact = λx. if x == 0 then 1 802 | else x ∗ (fact (x-1)) 803 | 804 | in fact 3 805 | ↦ (many steps) 806 | ↦ Letrec fact = ... in 6 807 | 808 | We could have written fact with primitive recursion. 809 | 810 | 811 | #### fix Term 812 | M, N, P ∈ Term ::= ... | fix x in M 813 | 814 | (β) fix x in M =β M[fix x in M/x] 815 | 816 | Γ,x:A ⊢ M : A 817 | —————————————————–(fix) 818 | Γ ⊢ fix x in M : A 819 | 820 | 821 | ### Recursive Types 822 | A, B, C ∈ Type ::= X | A → B | μX.A | unit | A + B 823 | 824 | M, N, P ∈ Term ::= x | λx.M | M N | fold M | unfold M 825 | | ... | () 826 | 827 | Nat = μX.(unit + X) 828 | 829 | μX.A =α μY.(A[Y/X]) 830 | 831 | Γ ⊢ M : A[μX.A/X] 832 | ————————————————–(μI) 833 | Γ ⊢ fold M : μX.A 834 | 835 | Γ ⊢ M : μX.A 836 | ————————————–——————————–(μE) 837 | Γ ⊢ unfold M : A[μX.A/X] 838 | 839 | Z = fold (inl ()) 840 | 841 | 842 | *Proof that Z : Nat:* 843 | 844 | ——————————–(unit I) 845 | ⊢ () : unit 846 | ——————————————————————————————–(+I₁) 847 | ⊢ inl () : unit + μX.(unit + X) 848 | ——————————————–———————————————–(μI) 849 | ⊢ fold (inl ()) : μX.(unit + X) 850 | 851 | S M = fold (inr M) 852 | 853 | *Proof that M : Nat implies S M : Nat:* 854 | 855 | Γ ⊢ M : μX.(unit + X) 856 | ——————————————————————————————–– +I₂ 857 | Γ ⊢ inr M : unit + μX.(unit + X) 858 | ——————————————–———————————————–– μI 859 | Γ ⊢ fold (inr M) : μX.(unit + X) 860 | 861 | ((... → A) → A) → A ≈ μX.(X → A) 862 | ≈ (μX.X → A) → A 863 | ≈ ((μX.X → A) → A) → A 864 | 865 | Now Ω has a type. (Although it has a new definition.) 866 | 867 | ω : (μX.X → A) → A 868 | ω = λx.(unfold x) x 869 | 870 | Ω : A 871 | Ω = ω (fold ω) 872 | 873 | **Exercise:** Type check ω and Ω by building a type derivation. 874 | 875 | *Solution for ω:* 876 | 877 | ————————————————————————–—(Var) 878 | x:(μX.X → A) ⊢ x:(μX.X → A) 879 | —————————————————————————————————————–(μE) ———————————————————————————(Var) 880 | x:(μX.X → A) ⊢ (unfold x):(μX.X → A) → A x:(μX.X → A) ⊢ x:(μX.X → A) 881 | ——————————————————————————————––————————————–—————————–—————————————————–(→E) 882 | x:(μX.X → A) ⊢ (unfold x) x : A 883 | ——————————————–——————————————————–(→I) 884 | ⊢ λx.(unfold x) x : (μX.X → A) → A 885 | 886 | 887 | Typed Y combinator: 888 | 889 | Y : (A → A) → A 890 | Y = λf. (λx.f (unfold x x)) (fold (λx.f (unfold x x))) 891 | 892 | **Exercise:** Show that Y does the right thing. 893 | 894 | **Exercise** (for the energetic): Type check Y by building a type derivation. 895 | 896 | ## Polymorphism 897 | ### Universal Types 898 | System F, Girard and Reynolds 899 | (System F is terminating) 900 | A, B, C ∈ Type ::= X | A → B | ∀X.A 901 | M, N, P ∈ Term ::= x | λx.M | M N | ΛX.M | M A 902 | E ∈ EvalCtxt ::= □ | E M | E A 903 | V, W ∈ Value ::= λx.M | ΛX.M 904 | 905 | (α) ∀X.A =α ∀Y.(A[Y/X]) 906 | 907 | FV(X) = {X} 908 | FV(A → B) = FV(A) ∪ FV(B) 909 | FV(ΛX.A) = FV(A) - {X} 910 | 911 | Example: 912 | map : ∀X.∀Y.(X → Y) → [X] → [Y] 913 | map = ΛX.ΛY.λf:X → Y.λxs:[X]. .... 914 | #### Typing rules 915 | We extend the notion of environment Γ. 916 | 917 | x₁:A₁, ...,xₙ:Aₙ,X₁:Type, ...,Xₘ:Type 918 | 919 | Exchange: Γ,x:A,Y:Type,Γ' = Γ,Y:Type,x:A,Γ' if Y ∉ FV(A) 920 | 921 | Γ,X:Type ⊢ M : A 922 | ——————————————–– ∀I 923 | Γ ⊢ ΛX.M : ∀X.A 924 | 925 | Γ ⊢ M : ∀X.A Γ ⊢ B : Type 926 | ——————————————––——————————— ∀E 927 | Γ ⊢ M B : A[B/X] 928 | 929 | Γ ⊢ A : Type means for all X ∈ FV(A), (X : Type) ∈ Γ 930 | 931 | Well-formedness rules for types: 932 | 933 | 934 | ————————————————– TyVar 935 | Γ,X:Type ⊢ X:Type 936 | 937 | Γ ⊢ A : Type Γ ⊢ B : Type 938 | ——————————————————————————— 939 | Γ ⊢ A → B : Type 940 | 941 | Γ,X:Type ⊢ A : Type 942 | ——————————————————— 943 | Γ ⊢ ∀X.A : Type 944 | #### Dynamic Semantics 945 | (α) ΛX.M =α ΛY.(M[Y/X]) 946 | 947 | (β) (ΛX.M) A =β M[A/X] 948 | 949 | (η) M =η ΛX.(M X), if X ∉ FV(M) and M : ∀X.A 950 | 951 | Lemma (Substitution): 952 | If Γ,X:Type,Γ' ⊢ M : A and Γ ⊢ B : Type are derivable, 953 | then Γ,Γ'[B/x] ⊢ M[B/X] : A[B/X] is derivable. 954 | #### Example 955 | id : ∀Y.Y → Y 956 | id = ΛY.λx.x (note x:Y) 957 | ### Existential Types 958 | A, B, C ∈ Type ::= X | A → B | ∀X.A | ∃X.A 959 | M, N, P ∈ Term ::= x | λx.M | M N | ΛX.M | M A | ⟨B,M⟩ | open M as ⟨X,y⟩ ⇒ N 960 | #### Typing rules 961 | 962 | Γ ⊢ B : Type Γ ⊢ M : A[B/X] 963 | ———————————————–————————————– ∃I (existential introduction) 964 | Γ ⊢ ⟨B,M⟩ : ∃X.A 965 | 966 | Γ ⊢ M : ∃X.A Γ,X:Type,y:A ⊢ N : C 967 | ———————————————–————————————–—————— ∃E , X ∉ FV(C) (existential elimination) 968 | Γ ⊢ open M as ⟨X,y⟩ ⇒ N : C 969 | #### Dynamic Semantics 970 | (α) open M as ⟨X,y⟩ ⇒ N =α open M as ⟨Y,x⟩ ⇒ (N[Y/X,x/y]) 971 | 972 | With type annotations: 973 | open M as ⟨X,y:A⟩ ⇒ N =α open M as ⟨Y,x:A[Y/X]⟩ ⇒ (N[Y/X,x/y]) 974 | 975 | (β) open ⟨B,M⟩ as ⟨X,y⟩ ⇒ N =β N[B/X,M/y] 976 | 977 | (η) M =η open M as ⟨X,y⟩ ⇒ ⟨X,y⟩ , if M : ∃X.A 978 | 979 | 980 | ——————————————————————– TyVar ————————————————————————– (Var) 981 | Γ,X:Type,y:A ⊢ X : Type Γ,X:Type,y:A ⊢ y : A[X/X] 982 | ——————————————————————————–————–——–—————————————————————– ∃I 983 | Γ ⊢ M : ∃X.A Γ,X:Type,y:A ⊢ ⟨X,y⟩ : ∃X.A 984 | —————————————————————————————————————————– ∃E 985 | Γ ⊢ open M as ⟨X,y⟩ ⇒ ⟨X,y⟩ : ∃X.A 986 | #### Example 987 | IntSet where 988 | Set : Type 989 | empty : Set 990 | single : Int → Set 991 | union : Set → Set → Set 992 | member : Int → Set → Bool 993 | 994 | IntSet = ∃Z.(Z × (Int → Z) × (Z → Z → Z) × (Int → Z → Bool)) 995 | ### Encodings 996 | #### Untyped encodings 997 | Church encodings 998 | ##### Booleans 999 | IfThenElse = λx.λt.λe.x t e 1000 | 1001 | True = λt.λe.t 1002 | False = λt.λe.e 1003 | 1004 | IFThenElse True M N =β True M N =β M 1005 | IFThenElse False M N =β False M N =β N 1006 | 1007 | And = λx.λy.IfThenElse x y False 1008 | Or = λx.λy.IfThenElse x True y 1009 | Not = λx.IfThenElse x False True 1010 | ##### Sums 1011 | Case = λi.λl.λr.i l r 1012 | Inl = λx.λl.λr.l x 1013 | Inr = λx.λl.λr.r x 1014 | 1015 | Case (Inl M) N P =β (Inl M) N P =β N M 1016 | Case (Inr M) N P =β (Inr M) N P =β P M 1017 | ##### Products 1018 | Pair = λx.λy.λp.p x y 1019 | Fst = λx.λy.x 1020 | Snd = λx.λy.y 1021 | 1022 | Pair M N Fst =β Fst M N =β M 1023 | Pair M N Snd =β Snd M N =β N 1024 | ##### Natural numbers 1025 | Iter = λn.λz.λs.n z s 1026 | Zero = λz.λs.z 1027 | Suc = λn.λz.λs.s (n z s) 1028 | 1029 | One = Suc Zero =η λz.λs.s z 1030 | Two = Suc One =η λz.λs.s (s z) 1031 | Three = Suc Two =η λz.λs.s (s (s z)) 1032 | ##### Lists 1033 | Fold = λl.λn.λc.l n c 1034 | Nil = λn.λc.n 1035 | Cons = λx.λl.λn.λc.c x (l n c) 1036 | #### Typed encodings 1037 | ##### Booleans 1038 | Bool = ∀Z.Z → Z → Z 1039 | 1040 | IfThenElse : ∀Z.Bool → Z → Z → Z 1041 | IfThenElse = ΛZ.λx:Bool.λt:Z.λe:Z.x Z t e 1042 | 1043 | True : Bool 1044 | True = ΛZ.λt:Z.λe:Z.t 1045 | False : Bool 1046 | False = ΛZ.λt:Z.λe:Z.e 1047 | ##### Sums 1048 | Sum A B = ∀Z.(A → Z) → (B → Z) → Z 1049 | 1050 | If we had type functions in our language, we could write 1051 | Sum = λX.λY.∀Z.(X → Z) → (Y → Z) → Z 1052 | 1053 | Case : ∀X.∀Y.∀Z.Sum X Y → (X → Z) → (Y → Z) → Z 1054 | Case = ΛX.ΛY.ΛZ.λi:Sum X Y.λl:X → Z.λr:Y → Z.i Z l r 1055 | 1056 | Inl : ∀X.∀Y.X → Sum X Y 1057 | Inl = ΛX.ΛY.λx:X.ΛZ.λl:X → Z.λr:Y → Z.l x 1058 | Inr : ∀X.∀Y.Y → Sum X Y 1059 | Inr = ΛX.ΛY.λy:Y.ΛZ.λl:X → Z.λr:Y → Z.r y 1060 | ##### Products 1061 | Prod A B = ∀Z.(A → B → Z) → Z 1062 | 1063 | Fst : ∀X.∀Y.X → Y → X 1064 | Fst = ΛX.ΛY.λx:X.λy:Y.x 1065 | Snd : ∀X.∀Y.X → Y → Y 1066 | Snd = ΛX.ΛY.λx:X.λy:Y.y 1067 | 1068 | Pair : ∀X.∀Y.X → Y → Prod X Y 1069 | Pair = ΛX.ΛY.λx:X.λy:Y.ΛZ.λp:X → Y → Z.p x y 1070 | ##### Natural numbers 1071 | Nat = ∀Z.Z → (Z → Z) → Z 1072 | 1073 | Iter : ∀Z.Nat → Z → (Z → Z) → Z 1074 | Iter = ΛZ.λn:Nat.λz:Z.λs:Z → Z.n Z z s 1075 | 1076 | Zero : Nat 1077 | Zero = ΛZ.λz:Z.λs:Z → Z.z 1078 | 1079 | Suc : Nat → Nat 1080 | Suc = λn:Nat.ΛZ.λz:Z.λs:Z → Z.s (n Z z s) 1081 | ##### Lists 1082 | List A = ∀Z.Z → (A → Z → Z) → Z 1083 | 1084 | Fold : ∀X.∀Z.List X → Z → (X → Z → Z) → Z 1085 | Fold = ΛX.ΛZ.λl:List X.λn:Z.λc:X → Z → Z.l Z n c 1086 | 1087 | Nil : ∀X.List X 1088 | Nil = ΛX.ΛZ.λn:Z.λc:X → Z → Z.n 1089 | 1090 | Cons : ∀X.X → List X → List X 1091 | Cons = ΛX.λx:X.λl:List X.ΛZ.λn:Z.λc:X → Z → Z.c x (l Z n c) 1092 | ##### Encoding existential types 1093 | We really want type functions, but we don't have them. 1094 | 1095 | Exist X.A = ∀Z.(∀X.A → Z) → Z 1096 | 1097 | Pack_X.A : ∀X.A → Exist X.A 1098 | Pack_X.A = ΛX.λy:A.ΛZ.λf:∀X.A → Z.f X y 1099 | 1100 | Open_X.A : ∀Z.Exist X.A → (∀X.A → Z) → Z 1101 | Open_X.A = ΛZ.λp:Exist X.A.λf:∀X.A → Z.p Z f 1102 | 1103 | Reference: Girard, Proofs and Types 1104 | -------------------------------------------------------------------------------- /notes/OPLSS/2017/Downen/README.md: -------------------------------------------------------------------------------- 1 | OPLSS program 2 | ⟨2017-06-23 Fri⟩ 3 | 4 | Welcome from Zena M. Ariola 5 | 6 | First year for review. 7 | 8 | **U of O people** 9 | Zach 10 | Anna 11 | 12 | **Review** 13 | Dr. Paul Downen 14 | University of Oregon 15 | 16 | ## Day 1: STLC (simply typed lambda calculus) 17 | 18 | ### Plan 19 | 1. lambda calculus 20 | 2. Products and Sums 21 | 3. Operational Semantics 22 | 23 | ### Untyped Lambda Calculus 24 | only has 3 things 25 | M, N, P ∈ Term ::= x | λx.M | M N 26 | surprisingly tricky to get right 27 | 28 | x, y, z ∈ Variable ::= ... 29 | might write λx.M as Lam(x.M) 30 | might write M N as ap(M;N) 31 | 32 | ### Bound variables 33 | BV(x) = {} 34 | BV(λx.M) = BV(M) ∪ {x} 35 | BV(M N) = B(M) ∪ BV(N) 36 | 37 | ### Free variables 38 | FV(x) = {x} 39 | FV(λx.M) = FV(M) - {x} 40 | FV(M N) = FV(M) ∪ FV(N) 41 | 42 | ### Substitution (capture avoiding substitution) 43 | M[N/x] replace occurrences of x with N 44 | x[N/x] = N (variable) 45 | y[N/x] = y (x ≠ y) (variable) 46 | (λx.M)[N/x] = λx.M (abstraction) 47 | (λy.M)[N/x] = λy.(M[N/x]) if y ∉ FV(N) (abstraction) 48 | (λy.M)[N/x] = (need renaming) if y ∈ FV(N) (abstraction) 49 | (M P)[N/x] = (M[N/x]) (P[N/x]) (application) 50 | 51 | Substitution is a partial function, because of the need for renaming. 52 | 53 | If we do multiple substitutions, order matters. 54 | (There is a commutation relation.) 55 | 56 | #### Let in terms of lambda 57 | Let x = M in N := (λx.N) M 58 | syntactic sugar 59 | 60 | #### Renaming 61 | α, β, γ are equivalence relations 62 | 63 | ##### α law (what functions are) 64 | λx.M =α λy.(M[y/x]), y ∉ FV(M) 65 | We work with terms up to α-equivalence. 66 | decidable 67 | about renaming 68 | 69 | ##### β law (what functions do) 70 | (λx.M) N =β M[N/x] 71 | the computational engine 72 | undecidable 73 | 74 | ##### η law (extensionality) 75 | M =η λx. Mx, x ∉ FV(M) 76 | extensionality is sometimes the same as "function extensionality" 77 | η rule used for reasoning, not for computation 78 | left and right sides would have different syntax trees 79 | A compiler should respect η. 80 | A compiler could use η for optimization. 81 | decidable 82 | 83 | #### function extensionality 84 | If for all inputs P, M P = N P then M = N. 85 | In the λ-calculus, function extensionality is equivalent to the η law. 86 | In other calculi, maybe not equivalent. 87 | (Function extensionality is usually stronger when they are different.) 88 | 89 | #### Böhm separability 90 | Thms about untyped lambda calculus. 91 | Alpha, beta, gamma give the "largest equivalence relation" that doesn't break. 92 | 93 | #### Simple thms 94 | M =β N implies M[P/x] =β N[P/x] (substitution respects beta) 95 | ### Compatibility rules 96 | you can apply alpha, beta, gamma in any setting 97 | ## Types 98 | types are contracts 99 | 100 | X, Y, Z ∈ TypeVariable ::= ... 101 | A, B, C ∈ Type ::= X | A → B 102 | 103 | Judgment: Γ ⊢ M : A (a 3-place thing) (⊢ is produced by \vdash) 104 | 105 | **Example:** Γ = x₁ : A₁, x₂ : A₂, x₃ : A₃, ..., xₙ : Aₙ 106 | 107 | Shadowing convention: Γ, x : A, x : B, Γ' = Γ, x : B, Γ' (rightmost thing wins) 108 | 109 | Exchange: Γ, x : A, y : B, Γ' = Γ, y : B, x : A, Γ' (x ≠ y) 110 | 111 | Inference rules: (things with long horizontal lines) 112 | 113 | ———————————————————— (Var) 114 | Γ, x : A ⊢ x : A 115 | 116 | Γ, x : A ⊢ M : B 117 | ————————————————————– (→ I) (function introduction) 118 | Γ ⊢ λx.M : A → B 119 | 120 | Γ ⊢ M : A → B Γ ⊢ N : A 121 | ————————————————————–———— (→ E) (function elimination) 122 | Γ ⊢ M N : B 123 | 124 | This is type inference. 125 | It's a bit different from type checking. 126 | (In type checking we know the goal.) 127 | 128 | We have already baked in the idea that terms have unique types. 129 | 130 | **Theorem:** 131 | If Γ, x : A ⊢ M : B and Γ ⊢ N : A 132 | then Γ ⊢ M[N/x] : B. 133 | 134 | ## Products and Sums 135 | The λ-calulus had 3 parts: 136 | 1. syntax 137 | 2. laws (alpha, beta, eta) 138 | 3. type derivations (static semantics) 139 | 140 | 141 | ### Syntax 142 | x, y, z ∈ Variable ::= ... 143 | 144 | M, N, P ∈ Term ::= x | λx.M | M N | ⟨M,N⟩ 145 | | fst M | snd M 146 | | inl M | inr M 147 | | (case M of inl x ⇒ N 148 | | inr y ⇒ P) 149 | 150 | A, B, C ∈ Type ::= X | A → B | A × B | A + B 151 | 152 | We added six terms and we added two types. 153 | 154 | Could have "fst" in the grammar instead of "fst M", 155 | but that would require polymorphism in the language. 156 | 157 | Alternative to (case M of inl x ⇒ N | inr y ⇒ P) 158 | is case(M; x.N; y.P) 159 | 160 | "case" is a new binding construct (binding x and y) 161 | 162 | ### Substitution 163 | ⟨M,P⟩[N/x] = ⟨M[N/x],P[N/x]⟩ 164 | 165 | ### Laws 166 | 167 | #### α law 168 | no alpha law for products 169 | 170 | case M of inl x ⇒ N | inr y ⇒ P 171 | =α case M of inl x' ⇒ (N[x'/x]) | inr y' ⇒ (P[y'/y]) 172 | 173 | #### β law 174 | fst ⟨M,N⟩ =β M 175 | snd ⟨M,N⟩ =β N 176 | 177 | case inl M of inl x ⇒ N 178 | | inr y ⇒ P =β= N[M/x] 179 | 180 | 181 | case inr M of inl x ⇒ N 182 | | inr y ⇒ P =β= P[M/y] 183 | 184 | #### η law 185 | 186 | M : A × B =η ⟨fst M, snd M⟩ (if M typecheks at A × B ....) 187 | 188 | M : A → B =η λx. Mx 189 | 190 | M : A + B =η case M of inl x ⇒ inl x | inr y ⇒ inr y 191 | 192 | ### Typing rules 193 | 194 | Γ ⊢ M : A Γ ⊢ N : B 195 | ————————————————————– (×I) (product introduction) 196 | Γ ⊢ ⟨M,N⟩ : A × B 197 | 198 | Γ ⊢ M : A × B 199 | ————————————– (×E₁) (product elimination 1) 200 | Γ ⊢ fst M : A 201 | 202 | Γ ⊢ M : A × B 203 | ————————————– (×E₂) (product elimination 2) 204 | Γ ⊢ snd M : B 205 | 206 | Γ ⊢ M : A 207 | ————————–———————– (+I₁) (sum introduction 1) 208 | Γ ⊢ inl M : A + B 209 | 210 | Γ ⊢ M : B 211 | ————————–———————– (+I₂) (sum introduction 2) 212 | Γ ⊢ inr M : A + B 213 | (sum elimination) 214 | Γ ⊢ M : A + B Γ,x:A ⊢ N : C Γ,y:B ⊢ P : C 215 | ————————————————————————————————————————————– (+E) 216 | Γ ⊢ case M of inl x ⇒ N | inr y ⇒ P : C 217 | 218 | With sums, we lose unique typing. 219 | We could save it by annotating. 220 | 221 | ### Example 222 | swap : A+B → B+A 223 | swap = λx. case x of inl y ⇒ inr y | inr z ⇒ inl z 224 | 225 | Type derivation tree: (writing from bottom up) 226 | 227 | ———————————————–(Var) ————————————————–(Var) 228 | x:A+B, y:A ⊢ y:A x:A+B, z:B ⊢ z:B 229 | ————————————–(Var) ——————————————————————–(+I₂) ——————————————————————–(+I₁) 230 | x:A+B ⊢ x:A+B x:A+B, y:A ⊢ inr y :B+A x:A+B, z:B ⊢ inl z :B+A 231 | ———————————————————————————————————————————————————————————–————————–(+E) 232 | x:A+B ⊢ case x of inl y ⇒ inr y | inr z ⇒ inl z : B+A 233 | ————————————————————————————————————————————————————————–(→I) 234 | ⊢ λx. case x of inl y ⇒ inr y | inr z ⇒ inl z : A+B → B+A 235 | 236 | Some discussion of "weakening" took place. 237 | 238 | **Weakening:** 239 | 240 | Γ ⊢ M:A 241 | ————————–———— weakening 242 | x:B, Γ ⊢ M:A 243 | 244 | We did not include weakening in our system, but we could prove it as a theorem. 245 | 246 | **Contraction:** 247 | 248 | Γ, x:A, y:A ⊢ M:B 249 | ————————————————–– contraction 250 | Γ, x:A ⊢ M[x/y]:B 251 | 252 | Check that it works: 253 | 254 | (λx. case x of inl y ⇒ inr y 255 | | inr z ⇒ inl z) (inr w) 256 | =ᵦ case inr w of inl y ⇒ inr y 257 | | inr z ⇒ inl z 258 | =ᵦ inl w 259 | 260 | ## Operational Semantics 261 | 262 | Curry style: define an untyped evaluator. 263 | 264 | Church style: program doesn't even have a meaning until it's been typed. 265 | 266 | We will follow Curry style here. 267 | 268 | Small-step semantics 269 | 270 | M ↦ N 271 | M ↦* N Kleane closure 272 | 273 | M ↦ N 274 | ————–– —————– 275 | M ↦* N M ↦* M 276 | 277 | M ↦* N N ↦* P 278 | ——————————————– 279 | M ↦* P 280 | 281 | ### Axioms 282 | (λx.M) N ↦ M[N/x] 283 | fst ⟨M,N⟩ ↦ M 284 | snd ⟨M,N⟩ ↦ N 285 | case inl M of inl y ⇒ N | inr z ⇒ P ↦ N[M/y] 286 | case inr M of inl y ⇒ N | inr z ⇒ P ↦ P[M/z] 287 | 288 | ### Inference rules 289 | M ↦ M' 290 | —————–—–—– 291 | M N ↦ M' N 292 | 293 | M ↦ M' 294 | —————–—–—–———– 295 | fst M ↦ fst M' 296 | 297 | M ↦ M' 298 | —————–—–—–———– 299 | snd M ↦ snd M' 300 | 301 | M ↦ M' 302 | —————–—–—–———–——————————————————————————————— 303 | case M of inl y ⇒ N 304 | | inr z ⇒ P ↦ case M' of inl y ⇒ N 305 | | inr z ⇒ P 306 | 307 | ### Evaluation context 308 | term with a hole in it 309 | 310 | E ∈ EvalCtxt ::= □ | E M | fst E | snd E 311 | | (case E of inl x ⇒ M | inr y ⇒ N) 312 | 313 | evaluation context tells where evaluation happens 314 | 315 | (\square is □) 316 | 317 | M ↦ M' 318 | ———————————– 319 | E[M] ↦ E[M'] 320 | 321 | E[M] is a Term 322 | 323 | □ [M] = M 324 | (E N)[M] = E[M] N 325 | (fst E)[M] = fst (E[M]) 326 | (snd E)[M] = snd (E[M]) 327 | (case E of inl x ⇒ N 328 | | inr y ⇒ P)[M] = case E[M] of inl x ⇒ N 329 | | inr y ⇒ P 330 | 331 | Continuations are related to evaluation contexts. 332 | 333 | #### Scott's examples 334 | (□ N)[M] = □[M] M = M N (M fills the hole) 335 | (fst □)[M] = fst (□[M]) = fst M (M fills the hole) 336 | (case □ of inl x ⇒ N 337 | | inr y ⇒ P)[M] = case M of inl x ⇒ N 338 | | inr y ⇒ P 339 | 340 | (fst (snd □))[M] = fst ((snd □)[M]) = fst (snd M) 341 | 342 | ### Example 343 | case fst ⟨inr 1, inl 5⟩ of inl x ⇒ x*2 | inr y ⇒ y+3 344 | = (case □ of ...)[fst ⟨inr 1, inl 5⟩] 345 | ↦ case inr 1 of inl x ⇒ x*2 | inr y ⇒ y+3 346 | ↦ 1 + 3 347 | 348 | ### Unique Decomposition 349 | **Theorem:** 350 | For every term M, either 351 | 1. there exists an evaluation context E and term N 352 | such that M = E[N] and N ↦ N', or 353 | 2. M is a result/value 354 | (i.e., an introduction form λx.M', ⟨M₁,M₂⟩, inl M', inr M') 355 | 3. M is stuck (M not ↦) 356 | 357 | ### Call by value 358 | We did call by name. 359 | We would set everything up differently for call by value. 360 | EvalCtxt would be defined differently, for example. 361 | 362 | Call-by-name: 363 | (λx. x∗x) (2+2) ↦ (2+2) ∗ (2+2) 364 | 365 | Call-by-value: 366 | (λx. x∗x) (2+2) ↦ (λx. x∗x) 4 ↦ 4∗4 367 | 368 | Call-by-name is dual to call-by-value. (There is a paper on this.) 369 | 370 | Call-by-need is where you memoize results, so you don't evaluate twice. 371 | 372 | 373 | ### Big-step operational semantics 374 | M ⇓ N 375 | We'll do call-by-name. 376 | 377 | (⇓ is \Downarrow) 378 | 379 | #### Axioms 380 | x ⇓ x 381 | λx.M ⇓ λx.M 382 | ⟨M,N⟩ ⇓ ⟨M,N⟩ 383 | inl M ⇓ inl M 384 | inr M ⇓ inr M 385 | #### Inference rules 386 | 387 | M ⇓ λx.M' M'[N/x] ⇓ P 388 | ——————————————————————– 389 | M N ⇓ P 390 | 391 | M ⇓ ⟨M₁,M₂⟩ M₁ ⇓ P 392 | ———————————————————— 393 | fst M ⇓ P 394 | 395 | M ⇓ ⟨M₁,M₂⟩ M₂ ⇓ P 396 | ———————————————————— 397 | snd M ⇓ P 398 | 399 | M ⇓ inl M' N₁[M'/x] ⇓ P 400 | ——————————————————————————————————————– 401 | case M of inl x ⇒ N₁ | inr y ⇒ N₂ ⇓ P 402 | 403 | M ⇓ inr M' N₂[M'/x] ⇓ P 404 | ——————————————————————————————————————– 405 | case M of inl x ⇒ N₁ | inr y ⇒ N₂ ⇓ P 406 | #### Example 407 | 408 | ——————————————————————————————– 409 | ⟨inr 1, inl 5⟩ ⇓ ⟨inr 1, inl 5⟩ 410 | —————–——————————————————–—————– 411 | fst ⟨inl1, inr 5⟩ ⇓ inr 1 1+3 ⇓ 4 412 | —————————————————————————————————————————————————————————– 413 | case fst ⟨inr 1, inl 5⟩ of inl x ⇒ x*2 | inr y ⇒ y+3 ⇓ 4 414 | ## Type Safety 415 | progress and preservation are syntactic theorems 416 | 417 | V, W ∈ Value ::= λx.M | ⟨M,N⟩ | inl M | inr M 418 | 419 | Definition: N is *stuck* when N is not a value and cannot advance. 420 | 421 | Theorem (Type Safety): If ⊢ M : A is derivable and M ↦* N 422 | then N is not stuck. 423 | 424 | Proof is based on Progess and Preservation lemmas. 425 | 426 | The simply typed lambda calculus is normalizing 427 | (all programs terminate), but the type safety theorem 428 | applies also to languages that do not terminate. 429 | 430 | ### Example of structural induction 431 | 432 | From yesterday: 433 | 434 | M ↦* N N ↦* P 435 | ——————————————– 436 | M ↦* P 437 | 438 | Definition of ⤇: 439 | 440 | M ↦ N N ⤇ P 441 | ——————————————– 442 | M ⤇ P 443 | 444 | —————– 445 | M ⤇ M 446 | 447 | Double arrow ⤇ associates to the right. 448 | 449 | **Lemma:** M ↦* P implies that either M ↦ N ↦* P or M = P. 450 | *Proof:* By induction on M ↦* P 451 | 452 | —————– refl 453 | M ↦* M 454 | 455 | M ↦ N 456 | —————– ⇒ —————————— refl 457 | M ↦* N M ↦ N ↦* N 458 | 459 | M ↦* N N ↦* P 460 | ——————————————– 461 | M ↦* P 462 | 463 | (more stuff) 464 | 465 | 466 | **Theorem:** M ↦* P iff M ⤇ P 467 | *Proof:* M ↦* P imples M ⤇ P by induction on the inference rules for M ↦* P. 468 | 469 | Suppose 470 | 471 | M ↦* N N ↦*P 472 | —————————————– 473 | M ↦* P 474 | 475 | M₁ ↦ M₂ M₂ ↦ M₃ 476 | ———————– ———————– 477 | M₁ ↦* M₂ M₂ ↦* M₃ M₃ ↦ M₄ 478 | ——————————————————— ———————– 479 | M₁ ↦* M₃ M₃ ↦* M₄ 480 | ——————————————————————————————— 481 | M₁ ↦* M₄ 482 | 483 | implies 484 | (something here) 485 | 486 | M₁ ↦ M₂ M₂ ⤇ M₄ 487 | ——————————————————————————————— 488 | M₁ ⤇ M₄ 489 | 490 | This is rule induction. 491 | 492 | ### Canonical Forms 493 | 494 | **Lemma:** 495 | 1. ⊢ V : A → B imples V = λx.M 496 | 2. ⊢ V : A × B imples V = ⟨M,N⟩ 497 | 3. ⊢ V : A + B imples either V = inl M or V = inr M 498 | 499 | ### Progress 500 | **Theorem** (Progress): 501 | If ⊢ M : A is derivable then either M is a value or there exists 502 | an N such that M ↦ N. 503 | 504 | (If M is derivable them M is not stuck.) 505 | 506 | No environment because we want to rule out (x 5). 507 | Progress is for closed terms only. 508 | 509 | *Proof:* By induction on the derivation of ⊢ M : A. 510 | There are 9 typing rules we have to check 511 | (Var, →I, →E, ×I, ×E₁, ×E₂, +I₁, +I₂, +E). 512 | 513 | __Case Var:__ impossible. 514 | 515 | What about function introduction? In this case, M is a value. 516 | All the introduction rules imply that M is a value. 517 | 518 | __Case →E:__ Need induction hypothesis and evaluation context. 519 | 520 | __Case ×E:__ Again need induction hypothesis and evaluation context. 521 | 522 | ⊢ M : A × B 523 | ———————————(×E₁) 524 | ⊢ fst M : A 525 | 526 | by IH: either M is a value or M ↦ M' 527 | If M is a value, then M = ⟨M₁,M₂⟩ 528 | fst M = fst ⟨M₁,M₂⟩ ↦ M₁, so we can take a step. 529 | If M ↦ M', fst □ is an evaluation context, so we can take step. 530 | 531 | M ↦ M' 532 | ———————–—————– 533 | fst M ↦ fst M' 534 | 535 | __Case +E:__ 536 | 537 | ⊢ M : A + B x:A ⊢ N : C y:B ⊢ P : C 538 | ———————————————————————————————————————(+E) 539 | ⊢ case M of inl x ⇒ N | inr y ⇒ P : C 540 | 541 | by IH: either M is a value or M ↦ M' 542 | If M is a value, then either M = inl M', or M = inr M'. 543 | 544 | ### Preservation 545 | **Theorem** (Preservation): 546 | If Γ ⊢ M : A is derivable and M ↦ N then Γ ⊢ N : A is derivable. 547 | 548 | **Lemma** (Typed Substitution): 549 | If Γ,x:A ⊢ M : B and Γ ⊢ N : A then Γ ⊢ M[N/x] : B. 550 | 551 | *Proof:* By induction on Γ,x:A ⊢ M : B. 552 | 553 | __Case ×I:__ 554 | 555 | Γ,x:A ⊢ M₁ : B₁ Γ,x:A ⊢ M₂ : B₂ 556 | ————————————————————–———————————–(×I) 557 | Γ,x:A ⊢ ⟨M₁,M₂⟩ : B₁ × B₂ 558 | 559 | by IH: 560 | 561 | Γ ⊢ M₁[N/x] : B₁ 562 | Γ ⊢ M₂[N/x] : B₂ 563 | 564 | ⟨M₁,M₂⟩[N/x] = ⟨M₁[N/x],M₂[N/x]⟩ 565 | 566 | There are many other cases... 567 | 568 | *Proof of main theorem:* By induction on the derivation of Γ ⊢ M : A, 569 | confirming that each evaluation step maintains type. 570 | The Var and introduction rules are not at the bottom of the derivation because they don't step. 571 | We need to look at the four elimination rules. 572 | 573 | __Case →E:__ 574 | 575 | Γ ⊢ M : A → B Γ ⊢ N : A 576 | —————————————————————–————(→E) 577 | Γ ⊢ M N : B 578 | 579 | What could M be? 580 | 581 | *Subcase 1:* If M = λx.M', then M N = (λx.M') N ↦ M'[N/x] 582 | 583 | **GOAL:** Derive Γ ⊢ M'[N/x] : B 584 | 585 | Γ, x : A ⊢ M' : B 586 | ———————————————————(→I) 587 | Γ ⊢ λx.M' : A → B 588 | 589 | By the Typed Substitution Lemma, we know Γ ⊢ M'[N/x] : B 590 | because Γ,x:A ⊢ M' : B and Γ ⊢ N : A, and we achieve the goal. 591 | 592 | *Subcase 2:* 593 | 594 | M ↦ M' 595 | —————–—–—– 596 | M N ↦ M' N 597 | 598 | __Case ×E:__ 599 | 600 | Γ ⊢ M : A × B 601 | ————————————– (×E₁) 602 | Γ ⊢ fst M : A 603 | 604 | *Subcase 1:* M = ⟨M₁,M₂⟩ 605 | 606 | Γ ⊢ M₁ : A Γ ⊢ M₂ : B 607 | ————————————————————–——(×I) 608 | Γ ⊢ ⟨M₁,M₂⟩ : A × B 609 | 610 | fst M = fst ⟨M₁,M₂⟩ ↦ M₁ 611 | 612 | We have shown that Γ ⊢ M₁ : A is derivable. 613 | 614 | *Subcase 2:* 615 | 616 | M ↦ M' 617 | —————–—–—–———– 618 | fst M ↦ fst M' 619 | 620 | __Case +E:__ 621 | 622 | Γ ⊢ M : A + B Γ,x:A ⊢ N : C Γ,y:B ⊢ P : C 623 | ————————————————————————————————————————————–(+E) 624 | Γ ⊢ case M of inl x ⇒ N | inr y ⇒ P : C 625 | 626 | *Subcase 1:* M = inl M' 627 | 628 | Γ ⊢ M' : A 629 | ————————–——————–—–(+I₁) 630 | Γ ⊢ inl M' : A + B 631 | 632 | case M of inl x ⇒ N | inr y ⇒ P 633 | = case inl M' of inl x ⇒ N | inr y ⇒ P 634 | ↦ N[M'/x] 635 | 636 | **GOAL:** Γ ⊢ N[M'/x] : C 637 | 638 | Use the Typed Substitution Lemma 639 | because Γ ⊢ M' : A and Γ,x:A ⊢ N : C. 640 | 641 | *Subcase 2:* M = inr M' 642 | 643 | *Subcase 3:* 644 | 645 | M ↦ M' 646 | —————–—–—–———–———————————————————————————— 647 | case M of inl y ⇒ N 648 | | inr z ⇒ P ↦ case M' of inl y ⇒ N 649 | | inr z ⇒ P 650 | 651 | by IH: Γ ⊢ M' : A + B 652 | 653 | ### Termination 654 | **Theorem** (Termination): 655 | For all terms M, where ⊢ M : A, there is a value V such that M ↦* V. 656 | 657 | **Lemma:** ⊢ M : A then there is no infinite reduction sequence. 658 | *Proof:* Induction on M. 659 | 660 | Case M = λx.M' 661 | 662 | (λx.M') N ↦ M'[N/x] 663 | 664 | **Problem:** term on the right need not be structural smaller, 665 | so we can't use the induction hypothesis. 666 | 667 | Need a different method... 668 | 669 | ...Logical Relations, Reducibility Candidate, Tait's Method. 670 | 671 | ## Recursion 672 | 673 | ### Primitive Recursion 674 | a type of recursion on numbers that's guaranteed to terminate 675 | 676 | We will look at System T (Gödel) 677 | 678 | A,B,C ∈ Type ::= X | A → B | Bool | Nat 679 | 680 | M, N, P ∈ Term ::= x | λx.M | M N 681 | | True | False 682 | | if M then N else P 683 | | Z | S M 684 | | (rec M of Z ⇒ N | S x with y ⇒ P) 685 | 686 | Binding tree: rec(M; N; x,y.P) 687 | 688 | V, W ∈ Value ::= λx.M | True | False | Z | S M 689 | 690 | E ∈ EvalCtxt ::= □ | E M | if E then M else N 691 | | (rec E of Z ⇒ N | S x with y ⇒ P) 692 | 693 | 694 | #### Rules for Bool 695 | 696 | (β) if True then N else P =ᵦ N 697 | if False then N else P =ᵦ P 698 | 699 | (η) M : Bool =η if M then True else False 700 | 701 | 702 | #### Rules for Nat 703 | 704 | (α) rec M of Z ⇒ N 705 | | S x with y ⇒ P 706 | =α rec M of Z ⇒ N 707 | | S x' with y' ⇒ P[x'/x,y'/y] 708 | 709 | (β) rec Z of Z ⇒ N 710 | | S x with y ⇒ P =ᵦ N 711 | 712 | rec S M of Z ⇒ N 713 | | S x with y ⇒ P 714 | =ᵦ P [M/x, (rec M of Z ⇒ N | S x with y ⇒ P)/y] 715 | 716 | (η) M : Nat =η rec M of Z ⇒ Z | S x with y ⇒ S x 717 | 718 | 719 | #### Introduction and Elimination Rules 720 | 721 | ——————————————–(Bool I₁) 722 | Γ ⊢ True : Bool 723 | 724 | 725 | ——————————————––(Bool I₂) 726 | Γ ⊢ False : Bool 727 | 728 | Γ ⊢ M : Bool Γ ⊢ N : C Γ ⊢ P : C 729 | ——————————————––———————————————————–(Bool E) 730 | Γ ⊢ if M then N else P : C 731 | 732 | 733 | ——————————–(Nat I₁) 734 | Γ ⊢ Z : Nat 735 | 736 | Γ ⊢ M : Nat 737 | ——————————–—–(Nat I₂) 738 | Γ ⊢ S M : Nat 739 | 740 | Γ ⊢ M : Nat Γ ⊢ N : C Γ,x:Nat,y:C ⊢ P : C 741 | ————————————————————————————————–—————————–—–(Nat E) 742 | Γ ⊢ rec M of Z ⇒ N | S x with y ⇒ P : C 743 | 744 | #### Examples 745 | add : Nat → Nat → Nat 746 | add = λx.λy.rec x of Z ⇒ y | S x' with w ⇒ S w 747 | 748 | pred : Nat → Nat 749 | pred = λx.rec x of Z ⇒ Z | S x' with y ⇒ x' 750 | 751 | add (S (S Z)) (S (S (S Z))) 752 | =ᵦ rec S (S Z) of Z ⇒ S (S (S Z)) | S x' with w ⇒ S w 753 | =ᵦ S (rec S Z of Z ⇒ S (S (S Z)) | S x' with w ⇒ S w) 754 | =ᵦ S (S (rec Z of Z ⇒ S (S (S Z)) | S x' with w ⇒ S w)) 755 | =ᵦ S (S (S (S (S Z)))) 756 | 757 | mult : Nat → Nat → Nat 758 | mult = λx.λy.rec x of Z ⇒ Z | S x' with y ⇒ add y w 759 | 760 | isZero : Nat → Bool 761 | isZero = λx.rec x of Z ⇒ True | S x' with y ⇒ False 762 | ### General Recursion 763 | Ω = (λx.x x) (λx.x x) 764 | ↦ (x x)[λx.x x/x] 765 | = (λx.x x) (λx.x x) 766 | 767 | Does Ω have a type? No. 768 | 769 | (more stuff) 770 | ———————————– 771 | ⊢ λx.x x : ? → ? ⊢ λx.x x : ? 772 | ——————————————————————–————————– →E 773 | ⊢ (λx.x x) (λx.x x) : ? 774 | 775 | Y = λf.((λx.f (x x)) (λx.f (x x))) (Y combinator) 776 | 777 | Y M =ᵦ (λx.M (x x)) (λx.M (x x)) 778 | =ᵦ M ((λx.M (x x)) (λx.M (x x))) 779 | =ᵦ M (Y M) 780 | 781 | Y M is a fixed point of M 782 | 783 | The Y combinator has no type. 784 | 785 | Let x = M in N := (λx.N) M 786 | 787 | Letrec x = M in N := (λx.N) (Y(λx.M)) 788 | =ᵦ (λx.N) ((λx.M)(Y(λx.M))) 789 | =ᵦ (λx.N) M[Y(λx.M)/x] 790 | := Letrec x = M[M/x] in N 791 | 792 | (β) Letrec x = M in N =ᵦ N[Letrec x = M in N/x] 793 | 794 | Letrec x = M in E[x] ↦ Letrec x = M in E[M] 795 | 796 | E ∈ EvalCtxt ::= ... | Letrec x = M in E 797 | 798 | 799 | **Example.** 800 | 801 | Letrec fact = λx. if x == 0 then 1 802 | else x ∗ (fact (x-1)) 803 | 804 | in fact 3 805 | ↦ (many steps) 806 | ↦ Letrec fact = ... in 6 807 | 808 | We could have written fact with primitive recursion. 809 | 810 | 811 | #### fix Term 812 | M, N, P ∈ Term ::= ... | fix x in M 813 | 814 | (β) fix x in M =β M[fix x in M/x] 815 | 816 | Γ,x:A ⊢ M : A 817 | —————————————————–(fix) 818 | Γ ⊢ fix x in M : A 819 | 820 | 821 | ### Recursive Types 822 | A, B, C ∈ Type ::= X | A → B | μX.A | unit | A + B 823 | 824 | M, N, P ∈ Term ::= x | λx.M | M N | fold M | unfold M 825 | | ... | () 826 | 827 | Nat = μX.(unit + X) 828 | 829 | μX.A =α μY.(A[Y/X]) 830 | 831 | Γ ⊢ M : A[μX.A/X] 832 | ————————————————–(μI) 833 | Γ ⊢ fold M : μX.A 834 | 835 | Γ ⊢ M : μX.A 836 | ————————————–——————————–(μE) 837 | Γ ⊢ unfold M : A[μX.A/X] 838 | 839 | Z = fold (inl ()) 840 | 841 | 842 | *Proof that Z : Nat:* 843 | 844 | ——————————–(unit I) 845 | ⊢ () : unit 846 | ——————————————————————————————–(+I₁) 847 | ⊢ inl () : unit + μX.(unit + X) 848 | ——————————————–———————————————–(μI) 849 | ⊢ fold (inl ()) : μX.(unit + X) 850 | 851 | S M = fold (inr M) 852 | 853 | *Proof that M : Nat implies S M : Nat:* 854 | 855 | Γ ⊢ M : μX.(unit + X) 856 | ——————————————————————————————–– +I₂ 857 | Γ ⊢ inr M : unit + μX.(unit + X) 858 | ——————————————–———————————————–– μI 859 | Γ ⊢ fold (inr M) : μX.(unit + X) 860 | 861 | ((... → A) → A) → A ≈ μX.(X → A) 862 | ≈ (μX.X → A) → A 863 | ≈ ((μX.X → A) → A) → A 864 | 865 | Now Ω has a type. (Although it has a new definition.) 866 | 867 | ω : (μX.X → A) → A 868 | ω = λx.(unfold x) x 869 | 870 | Ω : A 871 | Ω = ω (fold ω) 872 | 873 | **Exercise:** Type check ω and Ω by building a type derivation. 874 | 875 | *Solution for ω:* 876 | 877 | ————————————————————————–—(Var) 878 | x:(μX.X → A) ⊢ x:(μX.X → A) 879 | —————————————————————————————————————–(μE) ———————————————————————————(Var) 880 | x:(μX.X → A) ⊢ (unfold x):(μX.X → A) → A x:(μX.X → A) ⊢ x:(μX.X → A) 881 | ——————————————————————————————––————————————–—————————–—————————————————–(→E) 882 | x:(μX.X → A) ⊢ (unfold x) x : A 883 | ——————————————–——————————————————–(→I) 884 | ⊢ λx.(unfold x) x : (μX.X → A) → A 885 | 886 | 887 | Typed Y combinator: 888 | 889 | Y : (A → A) → A 890 | Y = λf. (λx.f (unfold x x)) (fold (λx.f (unfold x x))) 891 | 892 | **Exercise:** Show that Y does the right thing. 893 | 894 | **Exercise** (for the energetic): Type check Y by building a type derivation. 895 | 896 | ## Polymorphism 897 | ### Universal Types 898 | System F, Girard and Reynolds 899 | (System F is terminating) 900 | A, B, C ∈ Type ::= X | A → B | ∀X.A 901 | M, N, P ∈ Term ::= x | λx.M | M N | ΛX.M | M A 902 | E ∈ EvalCtxt ::= □ | E M | E A 903 | V, W ∈ Value ::= λx.M | ΛX.M 904 | 905 | (α) ∀X.A =α ∀Y.(A[Y/X]) 906 | 907 | FV(X) = {X} 908 | FV(A → B) = FV(A) ∪ FV(B) 909 | FV(ΛX.A) = FV(A) - {X} 910 | 911 | Example: 912 | map : ∀X.∀Y.(X → Y) → [X] → [Y] 913 | map = ΛX.ΛY.λf:X → Y.λxs:[X]. .... 914 | #### Typing rules 915 | We extend the notion of environment Γ. 916 | 917 | x₁:A₁, ...,xₙ:Aₙ,X₁:Type, ...,Xₘ:Type 918 | 919 | Exchange: Γ,x:A,Y:Type,Γ' = Γ,Y:Type,x:A,Γ' if Y ∉ FV(A) 920 | 921 | Γ,X:Type ⊢ M : A 922 | ——————————————–– ∀I 923 | Γ ⊢ ΛX.M : ∀X.A 924 | 925 | Γ ⊢ M : ∀X.A Γ ⊢ B : Type 926 | ——————————————––——————————— ∀E 927 | Γ ⊢ M B : A[B/X] 928 | 929 | Γ ⊢ A : Type means for all X ∈ FV(A), (X : Type) ∈ Γ 930 | 931 | Well-formedness rules for types: 932 | 933 | 934 | ————————————————– TyVar 935 | Γ,X:Type ⊢ X:Type 936 | 937 | Γ ⊢ A : Type Γ ⊢ B : Type 938 | ——————————————————————————— 939 | Γ ⊢ A → B : Type 940 | 941 | Γ,X:Type ⊢ A : Type 942 | ——————————————————— 943 | Γ ⊢ ∀X.A : Type 944 | #### Dynamic Semantics 945 | (α) ΛX.M =α ΛY.(M[Y/X]) 946 | 947 | (β) (ΛX.M) A =β M[A/X] 948 | 949 | (η) M =η ΛX.(M X), if X ∉ FV(M) and M : ∀X.A 950 | 951 | Lemma (Substitution): 952 | If Γ,X:Type,Γ' ⊢ M : A and Γ ⊢ B : Type are derivable, 953 | then Γ,Γ'[B/x] ⊢ M[B/X] : A[B/X] is derivable. 954 | #### Example 955 | id : ∀Y.Y → Y 956 | id = ΛY.λx.x (note x:Y) 957 | ### Existential Types 958 | A, B, C ∈ Type ::= X | A → B | ∀X.A | ∃X.A 959 | M, N, P ∈ Term ::= x | λx.M | M N | ΛX.M | M A | ⟨B,M⟩ | open M as ⟨X,y⟩ ⇒ N 960 | #### Typing rules 961 | 962 | Γ ⊢ B : Type Γ ⊢ M : A[B/X] 963 | ———————————————–————————————– ∃I (existential introduction) 964 | Γ ⊢ ⟨B,M⟩ : ∃X.A 965 | 966 | Γ ⊢ M : ∃X.A Γ,X:Type,y:A ⊢ N : C 967 | ———————————————–————————————–—————— ∃E , X ∉ FV(C) (existential elimination) 968 | Γ ⊢ open M as ⟨X,y⟩ ⇒ N : C 969 | #### Dynamic Semantics 970 | (α) open M as ⟨X,y⟩ ⇒ N =α open M as ⟨Y,x⟩ ⇒ (N[Y/X,x/y]) 971 | 972 | With type annotations: 973 | open M as ⟨X,y:A⟩ ⇒ N =α open M as ⟨Y,x:A[Y/X]⟩ ⇒ (N[Y/X,x/y]) 974 | 975 | (β) open ⟨B,M⟩ as ⟨X,y⟩ ⇒ N =β N[B/X,M/y] 976 | 977 | (η) M =η open M as ⟨X,y⟩ ⇒ ⟨X,y⟩ , if M : ∃X.A 978 | 979 | 980 | ——————————————————————– TyVar ————————————————————————– (Var) 981 | Γ,X:Type,y:A ⊢ X : Type Γ,X:Type,y:A ⊢ y : A[X/X] 982 | ——————————————————————————–————–——–—————————————————————– ∃I 983 | Γ ⊢ M : ∃X.A Γ,X:Type,y:A ⊢ ⟨X,y⟩ : ∃X.A 984 | —————————————————————————————————————————– ∃E 985 | Γ ⊢ open M as ⟨X,y⟩ ⇒ ⟨X,y⟩ : ∃X.A 986 | #### Example 987 | IntSet where 988 | Set : Type 989 | empty : Set 990 | single : Int → Set 991 | union : Set → Set → Set 992 | member : Int → Set → Bool 993 | 994 | IntSet = ∃Z.(Z × (Int → Z) × (Z → Z → Z) × (Int → Z → Bool)) 995 | ### Encodings 996 | #### Untyped encodings 997 | Church encodings 998 | ##### Booleans 999 | IfThenElse = λx.λt.λe.x t e 1000 | 1001 | True = λt.λe.t 1002 | False = λt.λe.e 1003 | 1004 | IFThenElse True M N =β True M N =β M 1005 | IFThenElse False M N =β False M N =β N 1006 | 1007 | And = λx.λy.IfThenElse x y False 1008 | Or = λx.λy.IfThenElse x True y 1009 | Not = λx.IfThenElse x False True 1010 | ##### Sums 1011 | Case = λi.λl.λr.i l r 1012 | Inl = λx.λl.λr.l x 1013 | Inr = λx.λl.λr.r x 1014 | 1015 | Case (Inl M) N P =β (Inl M) N P =β N M 1016 | Case (Inr M) N P =β (Inr M) N P =β P M 1017 | ##### Products 1018 | Pair = λx.λy.λp.p x y 1019 | Fst = λx.λy.x 1020 | Snd = λx.λy.y 1021 | 1022 | Pair M N Fst =β Fst M N =β M 1023 | Pair M N Snd =β Snd M N =β N 1024 | ##### Natural numbers 1025 | Iter = λn.λz.λs.n z s 1026 | Zero = λz.λs.z 1027 | Suc = λn.λz.λs.s (n z s) 1028 | 1029 | One = Suc Zero =η λz.λs.s z 1030 | Two = Suc One =η λz.λs.s (s z) 1031 | Three = Suc Two =η λz.λs.s (s (s z)) 1032 | ##### Lists 1033 | Fold = λl.λn.λc.l n c 1034 | Nil = λn.λc.n 1035 | Cons = λx.λl.λn.λc.c x (l n c) 1036 | #### Typed encodings 1037 | ##### Booleans 1038 | Bool = ∀Z.Z → Z → Z 1039 | 1040 | IfThenElse : ∀Z.Bool → Z → Z → Z 1041 | IfThenElse = ΛZ.λx:Bool.λt:Z.λe:Z.x Z t e 1042 | 1043 | True : Bool 1044 | True = ΛZ.λt:Z.λe:Z.t 1045 | False : Bool 1046 | False = ΛZ.λt:Z.λe:Z.e 1047 | ##### Sums 1048 | Sum A B = ∀Z.(A → Z) → (B → Z) → Z 1049 | 1050 | If we had type functions in our language, we could write 1051 | Sum = λX.λY.∀Z.(X → Z) → (Y → Z) → Z 1052 | 1053 | Case : ∀X.∀Y.∀Z.Sum X Y → (X → Z) → (Y → Z) → Z 1054 | Case = ΛX.ΛY.ΛZ.λi:Sum X Y.λl:X → Z.λr:Y → Z.i Z l r 1055 | 1056 | Inl : ∀X.∀Y.X → Sum X Y 1057 | Inl = ΛX.ΛY.λx:X.ΛZ.λl:X → Z.λr:Y → Z.l x 1058 | Inr : ∀X.∀Y.Y → Sum X Y 1059 | Inr = ΛX.ΛY.λy:Y.ΛZ.λl:X → Z.λr:Y → Z.r y 1060 | ##### Products 1061 | Prod A B = ∀Z.(A → B → Z) → Z 1062 | 1063 | Fst : ∀X.∀Y.X → Y → X 1064 | Fst = ΛX.ΛY.λx:X.λy:Y.x 1065 | Snd : ∀X.∀Y.X → Y → Y 1066 | Snd = ΛX.ΛY.λx:X.λy:Y.y 1067 | 1068 | Pair : ∀X.∀Y.X → Y → Prod X Y 1069 | Pair = ΛX.ΛY.λx:X.λy:Y.ΛZ.λp:X → Y → Z.p x y 1070 | ##### Natural numbers 1071 | Nat = ∀Z.Z → (Z → Z) → Z 1072 | 1073 | Iter : ∀Z.Nat → Z → (Z → Z) → Z 1074 | Iter = ΛZ.λn:Nat.λz:Z.λs:Z → Z.n Z z s 1075 | 1076 | Zero : Nat 1077 | Zero = ΛZ.λz:Z.λs:Z → Z.z 1078 | 1079 | Suc : Nat → Nat 1080 | Suc = λn:Nat.ΛZ.λz:Z.λs:Z → Z.s (n Z z s) 1081 | ##### Lists 1082 | List A = ∀Z.Z → (A → Z → Z) → Z 1083 | 1084 | Fold : ∀X.∀Z.List X → Z → (X → Z → Z) → Z 1085 | Fold = ΛX.ΛZ.λl:List X.λn:Z.λc:X → Z → Z.l Z n c 1086 | 1087 | Nil : ∀X.List X 1088 | Nil = ΛX.ΛZ.λn:Z.λc:X → Z → Z.n 1089 | 1090 | Cons : ∀X.X → List X → List X 1091 | Cons = ΛX.λx:X.λl:List X.ΛZ.λn:Z.λc:X → Z → Z.c x (l Z n c) 1092 | ##### Encoding existential types 1093 | We really want type functions, but we don't have them. 1094 | 1095 | Exist X.A = ∀Z.(∀X.A → Z) → Z 1096 | 1097 | Pack_X.A : ∀X.A → Exist X.A 1098 | Pack_X.A = ΛX.λy:A.ΛZ.λf:∀X.A → Z.f X y 1099 | 1100 | Open_X.A : ∀Z.Exist X.A → (∀X.A → Z) → Z 1101 | Open_X.A = ΛZ.λp:Exist X.A.λf:∀X.A → Z.p Z f 1102 | 1103 | Reference: Girard, Proofs and Types 1104 | -------------------------------------------------------------------------------- /notes/OPLSS/2017/Downen/ReviewLectures.org: -------------------------------------------------------------------------------- 1 | OPLSS program 2 | <2017-06-23 Fri> 3 | * Welcome from Zena M. Ariola 4 | First year for review. 5 | ** U of O people 6 | Zach 7 | Anna 8 | * Review 9 | Dr. Paul Downen 10 | University of Oregon 11 | ** Plan 12 | *** Day 1: STLC (simply typed lambda calculus) 13 | **** lambda calculus 14 | **** Products and Sums 15 | **** Operational Semantics 16 | ** Untyped Lambda Calculus 17 | only has 3 things 18 | M, N, P ∈ Term ::= x | λx.M | M N 19 | surprisingly tricky to get right 20 | 21 | x, y, z ∈ Variable ::= ... 22 | might write λx.M as Lam(x.M) 23 | might write M N as ap(M;N) 24 | *** Bound variables 25 | BV(x) = {} 26 | BV(λx.M) = BV(M) ∪ {x} 27 | BV(M N) = B(M) ∪ BV(N) 28 | *** Free variables 29 | FV(x) = {x} 30 | FV(λx.M) = FV(M) - {x} 31 | FV(M N) = FV(M) ∪ FV(N) 32 | *** Substitution (capture avoiding substitution) 33 | M[N/x] replace occurrences of x with N 34 | x[N/x] = N (variable) 35 | y[N/x] = y (x ≠ y) (variable) 36 | (λx.M)[N/x] = λx.M (abstraction) 37 | (λy.M)[N/x] = λy.(M[N/x]) if y ∉ FV(N) (abstraction) 38 | (λy.M)[N/x] = (need renaming) if y ∈ FV(N) (abstraction) 39 | (M P)[N/x] = (M[N/x]) (P[N/x]) (application) 40 | 41 | Substitution is a partial function, because of the need for renaming. 42 | 43 | If we do multiple substitutions, order matters. 44 | (There is a commutation relation.) 45 | **** Let in terms of lambda 46 | Let x = M in N := (λx.N) M 47 | syntactic sugar 48 | **** Renaming 49 | α, β, γ are equivalence relations 50 | ***** α law (what functions are) 51 | λx.M =_α λy.(M[y/x]), y ∉ FV(M) 52 | We work with terms up to α-equivalence. 53 | decidable 54 | about renaming 55 | ***** β law (what functions do) 56 | (λx.M) N =_β M[N/x] 57 | the computational engine 58 | undecidable 59 | ***** η law (extensionality) 60 | M =_η λx.Mx, x ∉ FV(M) 61 | extensionality is sometimes the same as "function extensionality" 62 | η rule used for reasoning, not for computation 63 | left and right sides would have different syntax trees 64 | A compiler should respect η. 65 | A compiler could use η for optimization. 66 | decidable 67 | ****** function extensionality 68 | If for all inputs P, M P = N P 69 | then M = N. 70 | In the lambda calculus, function extensionality is equivalent to the η law. 71 | In other calculi, maybe not equivalent. 72 | (Function extensionality is usually stronger when they are different.) 73 | ***** Böhm separability 74 | Thms about untyped lambda calculus. 75 | Alpha, beta, gamma give the "largest equivalence relation" that doesn't break. 76 | ***** Simple thms 77 | M =_β N implies M[P/x] =_β N[P/x] (substitution respects beta) 78 | *** Compatibility rules 79 | you can apply alpha, beta, gamma in any setting 80 | ** Types 81 | types are contracts 82 | 83 | X, Y, Z ∈ TypeVariable ::= ... 84 | A, B, C ∈ Type ::= X | A -> B 85 | 86 | Judgment: Γ ⊢ M : A (a 3-place thing) (⊢ is produced by \vdash) 87 | 88 | Example: Γ = x₁ : A₁, x₂ : A₂, x₃ : A₃, ..., xₙ : Aₙ 89 | 90 | Shadowing convention: Γ, x : A, x : B, Γ' = Γ, x : B, Γ' (rightmost thing wins) 91 | 92 | Exchange: Γ, x : A, y : B, Γ' = Γ, y : B, x : A, Γ' (x ≠ y) 93 | 94 | 95 | Inference rules: (things with long horizontal lines) 96 | 97 | 98 | ———————————————————— Var 99 | Γ, x : A ⊢ x : A 100 | 101 | Γ, x : A ⊢ M : B 102 | —————————————————————————– ->I (function introduction) 103 | Γ ⊢ λx.M : A -> B 104 | 105 | Γ ⊢ M : A -> B Γ ⊢ N : A 106 | —————————————————————–———— ->E (function elimination) 107 | Γ ⊢ M N : B 108 | 109 | This is type inference. 110 | It's a bit different from type checking. 111 | (In type checking we know the goal.) 112 | 113 | We have already baked in the idea that terms have unique types. 114 | 115 | Theorem: 116 | If Γ, x : A ⊢ M : B and Γ ⊢ N : A 117 | then Γ ⊢ M[N/x] : B . 118 | ** Products and Sums 119 | lambda calculus had 3 parts: 120 | syntax 121 | laws (alpha, beta, eta) 122 | type derivations (static semantics) 123 | *** Syntax 124 | x, y, z ∈ Variable ::= ... 125 | M, N, P ∈ Term ::= x | λx.M | M N | | fst M | snd M 126 | | inl M | inr M | (case M of inl x => N | inr y => P) 127 | A, B, C ∈ Type ::= X | A -> B | A × B | A + B 128 | 129 | We added six terms and we added two types. 130 | 131 | Could have "fst" in the grammar instead of "fst M", 132 | but that would require polymorphism in the language. 133 | 134 | Alternative to (case M of inl x => N | inr y => P) 135 | is case(M; x.N; y.P) 136 | 137 | "case" is a new binding construct (binding x and y) 138 | *** Substitution 139 | [N/x] = 140 | *** Laws 141 | **** α law 142 | no alpha law for products 143 | 144 | case M of inl x => N | inr y => P 145 | =_α 146 | case M of inl x' => (N[x'/x]) | inr y' => (P[y'/y]) 147 | **** β law 148 | fst =_β M 149 | snd =_β N 150 | 151 | case inl M of inl x => N | inr y => P 152 | =_β 153 | N[M/x] 154 | 155 | case inr M of inl x => N | inr y => P 156 | =_β 157 | P[M/y] 158 | **** η law 159 | M : A × B =_η (if M typecheks at A × B ....) 160 | M : A -> B =_η λx.Mx 161 | M : A + B =_η case M of inl x => inl x | inr y => inr y 162 | *** Typing rules 163 | 164 | Γ ⊢ M : A Γ ⊢ N : B 165 | ————————————————————– ×I (product introduction) 166 | Γ ⊢ : A × B 167 | 168 | Γ ⊢ M : A × B 169 | ————————————– ×E₁ (product elimination 1) 170 | Γ ⊢ fst M : A 171 | 172 | Γ ⊢ M : A × B 173 | ————————————– ×E₂ (product elimination 2) 174 | Γ ⊢ snd M : B 175 | 176 | Γ ⊢ M : A 177 | ————————–———————– +I₁ (sum introduction 1) 178 | Γ ⊢ inl M : A + B 179 | 180 | Γ ⊢ M : B 181 | ————————–———————– +I₂ (sum introduction 2) 182 | Γ ⊢ inr M : A + B 183 | 184 | Γ ⊢ M : A + B Γ,x:A ⊢ N : C Γ,y:B ⊢ P : C 185 | ————————————————————————————————————————————– +E (sum elimination) 186 | Γ ⊢ case M of inl x => N | inr y => P : C 187 | 188 | With sums, we lose unique typing. 189 | We could save it by annotating. 190 | *** Example 191 | swap : A+B -> B+A 192 | swap = λx. case x of inl y => inr y | inr z => inl z 193 | 194 | Type derivation tree: writing from bottom up 195 | 196 | 197 | ————————————————– Var ————————————————– Var 198 | x:A+B,y:A ⊢ y : A x:A+B,z:B ⊢ z : B 199 | ————————————————– Var ——————————————————————– +I₂ ——————————————————————– +I₁ 200 | x : A+B ⊢ x : A+B x:A+B,y:A ⊢ inr y : B+A x:A+B,z:B ⊢ inl z : B+A 201 | ———————————————————————————————————————————————————————————–————————– +E 202 | x : A+B ⊢ case x of inl y => inr y | inr z => inl z : B+A 203 | ———————————————————————————————————————————————————————————– ->I 204 | ⊢ λx. case x of inl y => inr y | inr z => inl z : A+B -> B+A 205 | 206 | Some discussion of "weakening" took place. 207 | 208 | Weakening: 209 | 210 | Γ ⊢ M : A 211 | ————————–———— weakening 212 | x:B,Γ ⊢ M : A 213 | 214 | We did not include weakening in our system, but we could 215 | prove it as a theorem. 216 | 217 | Contraction: 218 | 219 | Γ,x:A,y:A ⊢ M : B 220 | ————————————————–– contraction 221 | Γ,x:A ⊢ M[x/y] : B 222 | 223 | Check that it works: 224 | 225 | (λx. case x of inl y => inr y | inr z => inl z) (inr w) 226 | =_β 227 | case inr w of inl y => inr y | inr z => inl z 228 | =_β 229 | inl w 230 | ** Operational Semantics 231 | Curry style: define an untyped evaluator. 232 | Church style: program doesn't even have a meaning until it's been typed. 233 | 234 | We will follow Curry style here. 235 | 236 | Small-step semantics 237 | 238 | M ↦ N 239 | M ↦* N Kleane closure 240 | 241 | M ↦ N 242 | ————–– —————– 243 | M ↦* N M ↦* M 244 | 245 | M ↦* N N ↦* P 246 | ——————————————– 247 | M ↦* P 248 | 249 | *** Axioms 250 | (λx.M) N ↦ M[N/x] 251 | fst ↦ M 252 | snd ↦ N 253 | case inl M of inl y => N | inr z => P ↦ N[M/y] 254 | case inr M of inl y => N | inr z => P ↦ P[M/z] 255 | *** Inference rules 256 | 257 | M ↦ M' 258 | —————–—–—– 259 | M N ↦ M' N 260 | 261 | M ↦ M' 262 | —————–—–—–———– 263 | fst M ↦ fst M' 264 | 265 | M ↦ M' 266 | —————–—–—–———– 267 | snd M ↦ snd M' 268 | 269 | M ↦ M' 270 | —————–—–—–———–———————————————————————————————————————————————————————– 271 | case M of inl y => N | inr z => P ↦ case M' of inl y => N | inr z => P 272 | 273 | *** Evaluation context 274 | term with a hole in it 275 | E ∈ EvalCtxt ::= □ | E M | fst E | snd E | (case E of inl x => M | inr y => N) 276 | 277 | evaluation context tells where evaluation happens 278 | 279 | (\square is □) 280 | 281 | M ↦ M' 282 | ———————————– 283 | E[M] ↦ E[M'] 284 | 285 | E[M] is a Term 286 | 287 | □ [M] = M 288 | (E N)[M] = E[M] N 289 | (fst E)[M] = fst (E[M]) 290 | (snd E)[M] = snd (E[M]) 291 | (case E of inl x => N | inr y => P)[M] = case E[M] of inl x => N | inr y => P 292 | 293 | Continuations are related to evaluation contexts. 294 | **** My examples 295 | (□ N)[M] = □[M] M = M N (M fills the hole) 296 | (fst □)[M] = fst (□[M]) = fst M (M fills the hole) 297 | (case □ of inl x => N | inr y => P)[M] = case M of inl x => N | inr y => P 298 | 299 | (fst (snd □))[M] = fst ((snd □)[M]) = fst (snd M) 300 | *** Example 301 | case fst of inl x => x*2 | inr y => y+3 302 | = (case □ of ...)[fst ] 303 | ↦ case inr 1 of inl x => x*2 | inr y => y+3 304 | ↦ 1 + 3 305 | *** Unique Decomposition 306 | Theorem: 307 | For every term M, either 308 | 1) there exists an evaluation context E and term N 309 | such that M = E[N] and N ↦ N', or 310 | 2) M is a result/value 311 | (an introduction form 312 | λx.M' 313 | 314 | inl M' 315 | inr M') 316 | 3) M is stuck (M not ↦) 317 | *** Call by value 318 | We did call by name. 319 | We would set everything up differently for call by value. 320 | EvalCtxt would be defined differently, for example. 321 | 322 | Call-by-name: 323 | (λx.x*x) (2+2) ↦ (2+2) * (2+2) 324 | 325 | Call-by-value: 326 | (λx.x*x) (2+2) ↦ (λx.x*x) 4 ↦ 4*4 327 | 328 | Call-by-name is dual to call-by-value. 329 | (There is a paper on this.) 330 | 331 | Call-by-need is where you memoize results 332 | so you don't evaluate twice. 333 | *** Big-step operational semantics 334 | M ⇓ N (⇓ is \Downarrow) 335 | We'll do call-by-name. 336 | **** Axioms 337 | x ⇓ x 338 | λx.M ⇓ λx.M 339 | 340 | inl M ⇓ inl M 341 | inr M ⇓ inr M 342 | **** Inference rules 343 | 344 | M ⇓ λx.M' M'[N/x] ⇓ P 345 | ——————————————————————– 346 | M N ⇓ P 347 | 348 | M ⇓ M₁ ⇓ P 349 | ———————————————————— 350 | fst M ⇓ P 351 | 352 | M ⇓ M₂ ⇓ P 353 | ———————————————————— 354 | snd M ⇓ P 355 | 356 | M ⇓ inl M' N₁[M'/x] ⇓ P 357 | ——————————————————————————————————————– 358 | case M of inl x => N₁ | inr y => N₂ ⇓ P 359 | 360 | M ⇓ inr M' N₂[M'/x] ⇓ P 361 | ——————————————————————————————————————– 362 | case M of inl x => N₁ | inr y => N₂ ⇓ P 363 | **** Example 364 | 365 | ——————————————————————————————– 366 | 367 | —————–——————————————————–—————– 368 | fst ⇓ inr 1 1+3 ⇓ 4 369 | —————————————————————————————————————————————————————————– 370 | case fst of inl x => x*2 | inr y => y+3 ⇓ 4 371 | ** Type Safety 372 | progress and preservation are syntactic theorems 373 | 374 | V, W ∈ Value ::= λx.M | | inl M | inr M 375 | 376 | Definition: N is *stuck* when N is not a value and cannot advance. 377 | 378 | Theorem (Type Safety): If ⊢ M : A is derivable and M ↦* N 379 | then N is not stuck. 380 | 381 | Proof is based on Progess and Preservation lemmas. 382 | 383 | The simply typed lambda calculus is normalizing 384 | (all programs terminate), but the type safety theorem 385 | applies also to languages that do not terminate. 386 | *** Example of structural induction 387 | 388 | From yesterday: 389 | 390 | M ↦* N N ↦* P 391 | ——————————————– 392 | M ↦* P 393 | 394 | Definition of ↦>: 395 | 396 | M ↦ N N ↦> P 397 | ——————————————– 398 | M ↦> P 399 | 400 | 401 | —————– 402 | M ↦> M 403 | 404 | Double arrow ↦> associates to the right. 405 | 406 | Lemma: M ↦* P implies that either 407 | M ↦ N ↦* P or M = P 408 | Proof: By induction on M ↦* P 409 | 410 | 411 | —————– refl 412 | M ↦* M 413 | 414 | M ↦ N 415 | —————– => —————————— refl 416 | M ↦* N M ↦ N ↦* N 417 | 418 | M ↦* N N ↦* P 419 | ——————————————– 420 | M ↦* P 421 | 422 | (more stuff) 423 | 424 | Theorem: M ↦* P iff M ↦> P 425 | Proof: M ↦* P imples M ↦> P 426 | by induction on the inference rules 427 | for M ↦* P. 428 | 429 | Suppose 430 | 431 | M ↦* N N ↦*P 432 | —————————————– 433 | M ↦* P 434 | 435 | 436 | M₁ ↦ M₂ M₂ ↦ M₃ 437 | ———————– ———————– 438 | M₁ ↦* M₂ M₂ ↦* M₃ M₃ ↦ M₄ 439 | ——————————————————— ———————– 440 | M₁ ↦* M₃ M₃ ↦* M₄ 441 | ——————————————————————————————— 442 | M₁ ↦* M₄ 443 | 444 | implies 445 | 446 | (something here) 447 | M₁ ↦ M₂ M₂ ↦> M₄ 448 | ——————————————————————————————— 449 | M₁ ↦> M₄ 450 | 451 | This is rule induction. 452 | *** Canonical Forms 453 | Lemma: 454 | 1) ⊢ V : A -> B imples V = λx.M 455 | 2) ⊢ V : A × B imples V = 456 | 3) ⊢ V : A + B imples either V = inl M or V = inr M 457 | *** Progress 458 | Theorem (Progress): 459 | If ⊢ M : A is derivable 460 | then either M is a value 461 | or there exists an N such that M ↦ N. 462 | 463 | (If M is derivable them M is not stuck.) 464 | 465 | No environment because we want to rule out (x 5). 466 | Progress is for closed terms only. 467 | 468 | Proof: By induction on the derivation of ⊢ M : A. 469 | There are 9 typing rules we have to check 470 | (Var, ->I, ->E, ×I, ×E₁, ×E₂, +I₁, +I₂, +E). 471 | Start with an easy case: the Var rule. Var is impossible. 472 | What about function introduction? In this case, M is a value. 473 | All the introduction rules imply that M is a value. 474 | Case ->E: Need induction hypothesis and evaluation context. 475 | Case ×E: Again need induction hypothesis and evaluation context. 476 | 477 | ⊢ M : A × B 478 | ——————————— ×E₁ 479 | ⊢ fst M : A 480 | 481 | by IH: either M is a value or M ↦ M' 482 | If M is a value, then M = 483 | fst M = fst ↦ M₁, so we can take a step. 484 | If M ↦ M', fst □ is an evaluation context, so we can take step. 485 | 486 | M ↦ M' 487 | ———————–—————– 488 | fst M ↦ fst M' 489 | 490 | Case +E: 491 | 492 | ⊢ M : A + B x:A ⊢ N : C y:B ⊢ P : C 493 | ——————————————————————————————————————— +E 494 | ⊢ case M of inl x => N | inr y => P : C 495 | 496 | by IH: either M is a value or M ↦ M' 497 | If M is a value, then either 498 | M = inl M', or 499 | M = inr M' 500 | *** Preservation 501 | Theorem (Preservation): 502 | If Γ ⊢ M : A is derivable 503 | and M ↦ N then Γ ⊢ N : A is derivable. 504 | 505 | Lemma (Typed Substitution): 506 | If Γ,x:A ⊢ M : B and Γ ⊢ N : A 507 | then Γ ⊢ M[N/x] : B. 508 | Proof: By induction on Γ,x:A ⊢ M : B. 509 | Case ×I: 510 | 511 | Γ,x:A ⊢ M₁ : B₁ Γ,x:A ⊢ M₂ : B₂ 512 | ————————————————————–———————————– ×I 513 | Γ,x:A ⊢ : B₁ × B₂ 514 | 515 | by IH: Γ ⊢ M₁[N/x] : B₁ 516 | Γ ⊢ M₂[N/x] : B₂ 517 | 518 | [N/x] = 519 | There are many other cases. 520 | 521 | Proof of main theorem: By induction on the derivation of Γ ⊢ M : A, 522 | confirming that each evaluation step maintains type. 523 | The Var and introduction rules are not at the bottom 524 | of the derivation because they don't step. 525 | We need to look at the four elimination rules. 526 | Case ->E: 527 | 528 | Γ ⊢ M : A -> B Γ ⊢ N : A 529 | —————————————————————–———— ->E 530 | Γ ⊢ M N : B 531 | 532 | What could M be? 533 | Subcase 1: 534 | If M = λx.M', then M N = (λx.M') N ↦ M'[N/x] 535 | Goal: Derive Γ ⊢ M'[N/x] : B 536 | 537 | Γ, x : A ⊢ M' : B 538 | ——————————————————— ->I 539 | Γ ⊢ λx.M' : A -> B 540 | 541 | By the Typed Substitution Lemma, 542 | we know Γ ⊢ M'[N/x] : B 543 | because Γ,x:A ⊢ M' : B and Γ ⊢ N : A, 544 | and we achieve the goal. 545 | Subcase 2: 546 | 547 | M ↦ M' 548 | —————–—–—– 549 | M N ↦ M' N 550 | 551 | Case ×E: 552 | 553 | Γ ⊢ M : A × B 554 | ————————————– ×E₁ 555 | Γ ⊢ fst M : A 556 | 557 | Subcase 1: M = 558 | 559 | Γ ⊢ M₁ : A Γ ⊢ M₂ : B 560 | ————————————————————–—— ×I 561 | Γ ⊢ : A × B 562 | 563 | fst M = fst ↦ M₁ 564 | We have shown that Γ ⊢ M₁ : A is derivable. 565 | 566 | Subcase 2: 567 | 568 | M ↦ M' 569 | —————–—–—–———– 570 | fst M ↦ fst M' 571 | 572 | Case +E: 573 | 574 | Γ ⊢ M : A + B Γ,x:A ⊢ N : C Γ,y:B ⊢ P : C 575 | ————————————————————————————————————————————– +E 576 | Γ ⊢ case M of inl x => N | inr y => P : C 577 | 578 | Subcase 1: M = inl M' 579 | 580 | Γ ⊢ M' : A 581 | ————————–——————–—– +I₁ 582 | Γ ⊢ inl M' : A + B 583 | 584 | case M of inl x => N | inr y => P 585 | = case inl M' of inl x => N | inr y => P 586 | ↦ N[M'/x] 587 | 588 | Goal: Γ ⊢ N[M'/x] : C 589 | 590 | Use the Typed Substitution Lemma 591 | because Γ ⊢ M' : A and Γ,x:A ⊢ N : C. 592 | 593 | Subcase 2: M = inr M' 594 | 595 | Subcase 3: 596 | 597 | M ↦ M' 598 | —————–—–—–———–———————————————————————————————————————————————————————– 599 | case M of inl y => N | inr z => P ↦ case M' of inl y => N | inr z => P 600 | 601 | by IH: Γ ⊢ M' : A + B 602 | *** Termination 603 | Theorem (Termination): 604 | For all terms M, where ⊢ M : A, 605 | there is a value V such that M ↦* V. 606 | 607 | Lemma: ⊢ M : A then there is no infinite reduction sequence. 608 | Proof: Induction on M. 609 | Case M = λx.M' 610 | (λx.M') N ↦ M'[N/x] 611 | 612 | Problem: term on the right need not be structural smaller, 613 | so we can't use the induction hypothesis. 614 | 615 | Need a different method. 616 | Logical Relations, Reducibility Candidate, Tait's Method. 617 | ** Recursion 618 | *** Primitive Recursion 619 | a type of recursion on numbers that's guaranteed to terminate 620 | We will look at System T (Goodall) 621 | 622 | A,B,C ∈ Type ::= X | A -> B | Bool | Nat 623 | 624 | M, N, P ∈ Term ::= x | λx.M | M N 625 | | True | False 626 | | if M then N else P 627 | | Z | S M 628 | | (rec M of Z => N | S x with y => P) 629 | 630 | Binding tree: rec(M; N; x,y.P) 631 | 632 | V, W ∈ Value ::= λx.M | True | False | Z | S M 633 | 634 | E ∈ EvalCtxt ::= □ | E M | if E then M else N | (rec E of Z => N | S x with y => P) 635 | **** Rules for Bool 636 | 637 | (β) if True then N else P =_β N 638 | if False then N else P =_β P 639 | 640 | (η) M : Bool =_η if M then True else False 641 | **** Rules for Nat 642 | 643 | (α) rec M of Z => N | S x with y => P =_α rec M of Z => N | S x' with y' => P[x'/x,y'/y] 644 | 645 | (β) rec Z of Z => N | S x with y => P =_β N 646 | rec S M of Z => N | S x with y => P =_β P[M/x,(rec M of Z => N | S x with y => P)/y] 647 | 648 | (η) M : Nat =_η rec M of Z => Z | S x with y => S x 649 | **** Introduction and Elimination Rules 650 | 651 | 652 | ——————————————– Bool I₁ 653 | Γ ⊢ True : Bool 654 | 655 | 656 | ——————————————–– Bool I₂ 657 | Γ ⊢ False : Bool 658 | 659 | Γ ⊢ M : Bool Γ ⊢ N : C Γ ⊢ P : C 660 | ——————————————––———————————————————– Bool E 661 | Γ ⊢ if M then N else P : C 662 | 663 | 664 | ——————————– Nat I₁ 665 | Γ ⊢ Z : Nat 666 | 667 | Γ ⊢ M : Nat 668 | ——————————–—– Nat I₂ 669 | Γ ⊢ S M : Nat 670 | 671 | Γ ⊢ M : Nat Γ ⊢ N : C Γ,x:Nat,y:C ⊢ P : C 672 | ————————————————————————————————–—————————–—– Nat E 673 | Γ ⊢ rec M of Z => N | S x with y => P : C 674 | **** Examples 675 | add : Nat -> Nat -> Nat 676 | add = λx.λy.rec x of Z => y | S x' with w => S w 677 | 678 | pred : Nat -> Nat 679 | pred = λx.rec x of Z => Z | S x' with y => x' 680 | 681 | add (S (S Z)) (S (S (S Z))) 682 | =β rec S (S Z) of Z => S (S (S Z)) | S x' with w => S w 683 | =β S (rec S Z of Z => S (S (S Z)) | S x' with w => S w) 684 | =β S (S (rec Z of Z => S (S (S Z)) | S x' with w => S w)) 685 | =β S (S (S (S (S Z)))) 686 | 687 | mult : Nat -> Nat -> Nat 688 | mult = λx.λy.rec x of Z => Z | S x' with y => add y w 689 | 690 | isZero : Nat -> Bool 691 | isZero = λx.rec x of Z => True | S x' with y => False 692 | *** General Recursion 693 | Ω = (λx.x x) (λx.x x) 694 | ↦ (x x)[λx.x x/x] 695 | = (λx.x x) (λx.x x) 696 | 697 | Does Ω have a type? No. 698 | 699 | (more stuff) 700 | ———————————– 701 | ⊢ λx.x x : ? -> ? ⊢ λx.x x : ? 702 | ——————————————————————–————————– ->E 703 | ⊢ (λx.x x) (λx.x x) : ? 704 | 705 | Y = λf.((λx.f (x x)) (λx.f (x x))) (Y combinator) 706 | 707 | Y M =β (λx.M (x x)) (λx.M (x x)) 708 | =β M ((λx.M (x x)) (λx.M (x x))) 709 | =β M (Y M) 710 | 711 | Y M is a fixed point of M 712 | 713 | Y combinator has no type. 714 | 715 | Let x = M in N := (λx.N) M 716 | Letrec x = M in N := (λx.N) (Y(λx.M)) 717 | =β (λx.N) ((λx.M)(Y(λx.M))) 718 | =β (λx.N) M[Y(λx.M)/x] 719 | := Letrec x = M[M/x] in N 720 | 721 | (β) Letrec x = M in N =β N[Letrec x = M in N/x] 722 | 723 | Letrec x = M in E[x] 724 | ↦ Letrec x = M in E[M] 725 | 726 | E ∈ EvalCtxt ::= ... | Letrec x = M in E 727 | **** Example 728 | Letrec fact = λx. 729 | if x == 0 then 1 730 | else x * (fact (x-1)) 731 | in fact 3 732 | ↦ (many steps) 733 | ↦ Letrec fact = ... in 6 734 | 735 | We could have written fact with primitive recursion. 736 | **** fix Term 737 | M, N, P ∈ Term ::= ... | fix x in M 738 | 739 | (β) fix x in M =β M[fix x in M/x] 740 | 741 | Γ,x:A ⊢ M : A 742 | —————————————————– fix 743 | Γ ⊢ fix x in M : A 744 | *** Recursive Types 745 | A, B, C ∈ Type ::= X | A -> B | μX.A | unit | A + B 746 | 747 | M, N, P ∈ Term ::= x | λx.M | M N | fold M | unfold M | ... | () 748 | 749 | Nat = μX.(unit + X) 750 | 751 | μX.A =α μY.(A[Y/X]) 752 | 753 | Γ ⊢ M : A[μX.A/X] 754 | ————————————————– μI 755 | Γ ⊢ fold M : μX.A 756 | 757 | Γ ⊢ M : μX.A 758 | ————————————–——————————– μE 759 | Γ ⊢ unfold M : A[μX.A/X] 760 | 761 | Z = fold (inl ()) 762 | 763 | Proof that Z : Nat: 764 | 765 | 766 | ——————————– unit I 767 | ⊢ () : unit 768 | ——————————————————————————————– +I₁ 769 | ⊢ inl () : unit + μX.(unit + X) 770 | ——————————————–———————————————– μI 771 | ⊢ fold (inl ()) : μX.(unit + X) 772 | 773 | 774 | S M = fold (inr M) 775 | 776 | Proof that M : Nat implies S M : Nat: 777 | 778 | Γ ⊢ M : μX.(unit + X) 779 | ——————————————————————————————–– +I₂ 780 | Γ ⊢ inr M : unit + μX.(unit + X) 781 | ——————————————–———————————————–– μI 782 | Γ ⊢ fold (inr M) : μX.(unit + X) 783 | 784 | ((... -> A) -> A) -> A ≈ μX.(X -> A) 785 | ≈ (μX.X -> A) -> A 786 | ≈ ((μX.X -> A) -> A) -> A 787 | 788 | Now Ω has a type. (Although it has a new definition.) 789 | 790 | ω : (μX.X -> A) -> A 791 | ω = λx.(unfold x) x 792 | 793 | Ω : A 794 | Ω = ω (fold ω) 795 | 796 | Exercise: Type check ω and Ω by building a type derivation. 797 | 798 | Solution for ω: 799 | 800 | 801 | ————————————————————————————–—– Var 802 | x:(μX.X -> A) ⊢ x : (μX.X -> A) 803 | ————————————————————————————————————————————– μE ————————————————————————————– Var 804 | x:(μX.X -> A) ⊢ (unfold x) : (μX.X -> A) -> A x:(μX.X -> A) ⊢ x:(μX.X -> A) 805 | ——————————————————————————————––———————————————————–—————————–—————————————————– ->E 806 | x:(μX.X -> A) ⊢ (unfold x) x : A 807 | ——————————————–————————————————————– ->I 808 | ⊢ λx.(unfold x) x : (μX.X -> A) -> A 809 | 810 | 811 | Typed Y combinator: 812 | 813 | Y : (A -> A) -> A 814 | Y = λf. (λx.f (unfold x x)) (fold (λx.f (unfold x x))) 815 | 816 | Exercise: Show that Y does the right thing. 817 | Exercise for the energetic: Type check Y by building a type derivation. 818 | ** Polymorphism 819 | *** Universal Types 820 | System F, Girard and Reynolds 821 | (System F is terminating) 822 | A, B, C ∈ Type ::= X | A -> B | ∀X.A 823 | M, N, P ∈ Term ::= x | λx.M | M N | ΛX.M | M A 824 | E ∈ EvalCtxt ::= □ | E M | E A 825 | V, W ∈ Value ::= λx.M | ΛX.M 826 | 827 | (α) ∀X.A =α ∀Y.(A[Y/X]) 828 | 829 | FV(X) = {X} 830 | FV(A -> B) = FV(A) ∪ FV(B) 831 | FV(ΛX.A) = FV(A) - {X} 832 | 833 | Example: 834 | map : ∀X.∀Y.(X -> Y) -> [X] -> [Y] 835 | map = ΛX.ΛY.λf:X -> Y.λxs:[X]. .... 836 | **** Typing rules 837 | We extend the notion of environment Γ. 838 | 839 | x₁:A₁, ...,xₙ:Aₙ,X₁:Type, ...,Xₘ:Type 840 | 841 | Exchange: Γ,x:A,Y:Type,Γ' = Γ,Y:Type,x:A,Γ' if Y ∉ FV(A) 842 | 843 | Γ,X:Type ⊢ M : A 844 | ——————————————–– ∀I 845 | Γ ⊢ ΛX.M : ∀X.A 846 | 847 | Γ ⊢ M : ∀X.A Γ ⊢ B : Type 848 | ——————————————––——————————— ∀E 849 | Γ ⊢ M B : A[B/X] 850 | 851 | Γ ⊢ A : Type means for all X ∈ FV(A), (X : Type) ∈ Γ 852 | 853 | Well-formedness rules for types: 854 | 855 | 856 | ————————————————– TyVar 857 | Γ,X:Type ⊢ X:Type 858 | 859 | Γ ⊢ A : Type Γ ⊢ B : Type 860 | ——————————————————————————— 861 | Γ ⊢ A -> B : Type 862 | 863 | Γ,X:Type ⊢ A : Type 864 | ——————————————————— 865 | Γ ⊢ ∀X.A : Type 866 | **** Dynamic Semantics 867 | (α) ΛX.M =α ΛY.(M[Y/X]) 868 | 869 | (β) (ΛX.M) A =β M[A/X] 870 | 871 | (η) M =η ΛX.(M X), if X ∉ FV(M) and M : ∀X.A 872 | 873 | Lemma (Substitution): 874 | If Γ,X:Type,Γ' ⊢ M : A and Γ ⊢ B : Type are derivable, 875 | then Γ,Γ'[B/x] ⊢ M[B/X] : A[B/X] is derivable. 876 | **** Example 877 | id : ∀Y.Y -> Y 878 | id = ΛY.λx.x (note x:Y) 879 | *** Existential Types 880 | A, B, C ∈ Type ::= X | A -> B | ∀X.A | ∃X.A 881 | M, N, P ∈ Term ::= x | λx.M | M N | ΛX.M | M A | | open M as => N 882 | **** Typing rules 883 | 884 | Γ ⊢ B : Type Γ ⊢ M : A[B/X] 885 | ———————————————–————————————– ∃I (existential introduction) 886 | Γ ⊢ : ∃X.A 887 | 888 | Γ ⊢ M : ∃X.A Γ,X:Type,y:A ⊢ N : C 889 | ———————————————–————————————–—————— ∃E , X ∉ FV(C) (existential elimination) 890 | Γ ⊢ open M as => N : C 891 | **** Dynamic Semantics 892 | (α) open M as => N =α open M as => (N[Y/X,x/y]) 893 | 894 | With type annotations: 895 | open M as => N =α open M as => (N[Y/X,x/y]) 896 | 897 | (β) open as => N =β N[B/X,M/y] 898 | 899 | (η) M =η open M as => , if M : ∃X.A 900 | 901 | 902 | ——————————————————————– TyVar ————————————————————————– Var 903 | Γ,X:Type,y:A ⊢ X : Type Γ,X:Type,y:A ⊢ y : A[X/X] 904 | ——————————————————————————–————–——–—————————————————————– ∃I 905 | Γ ⊢ M : ∃X.A Γ,X:Type,y:A ⊢ : ∃X.A 906 | —————————————————————————————————————————– ∃E 907 | Γ ⊢ open M as => : ∃X.A 908 | **** Example 909 | IntSet where 910 | Set : Type 911 | empty : Set 912 | single : Int -> Set 913 | union : Set -> Set -> Set 914 | member : Int -> Set -> Bool 915 | 916 | IntSet = ∃Z.(Z × (Int -> Z) × (Z -> Z -> Z) × (Int -> Z -> Bool)) 917 | *** Encodings 918 | **** Untyped encodings 919 | Church encodings 920 | ***** Booleans 921 | IfThenElse = λx.λt.λe.x t e 922 | 923 | True = λt.λe.t 924 | False = λt.λe.e 925 | 926 | IFThenElse True M N =β True M N =β M 927 | IFThenElse False M N =β False M N =β N 928 | 929 | And = λx.λy.IfThenElse x y False 930 | Or = λx.λy.IfThenElse x True y 931 | Not = λx.IfThenElse x False True 932 | ***** Sums 933 | Case = λi.λl.λr.i l r 934 | Inl = λx.λl.λr.l x 935 | Inr = λx.λl.λr.r x 936 | 937 | Case (Inl M) N P =β (Inl M) N P =β N M 938 | Case (Inr M) N P =β (Inr M) N P =β P M 939 | ***** Products 940 | Pair = λx.λy.λp.p x y 941 | Fst = λx.λy.x 942 | Snd = λx.λy.y 943 | 944 | Pair M N Fst =β Fst M N =β M 945 | Pair M N Snd =β Snd M N =β N 946 | ***** Natural numbers 947 | Iter = λn.λz.λs.n z s 948 | Zero = λz.λs.z 949 | Suc = λn.λz.λs.s (n z s) 950 | 951 | One = Suc Zero =η λz.λs.s z 952 | Two = Suc One =η λz.λs.s (s z) 953 | Three = Suc Two =η λz.λs.s (s (s z)) 954 | ***** Lists 955 | Fold = λl.λn.λc.l n c 956 | Nil = λn.λc.n 957 | Cons = λx.λl.λn.λc.c x (l n c) 958 | **** Typed encodings 959 | ***** Booleans 960 | Bool = ∀Z.Z -> Z -> Z 961 | 962 | IfThenElse : ∀Z.Bool -> Z -> Z -> Z 963 | IfThenElse = ΛZ.λx:Bool.λt:Z.λe:Z.x Z t e 964 | 965 | True : Bool 966 | True = ΛZ.λt:Z.λe:Z.t 967 | False : Bool 968 | False = ΛZ.λt:Z.λe:Z.e 969 | ***** Sums 970 | Sum A B = ∀Z.(A -> Z) -> (B -> Z) -> Z 971 | 972 | If we had type functions in our language, we could write 973 | Sum = λX.λY.∀Z.(X -> Z) -> (Y -> Z) -> Z 974 | 975 | Case : ∀X.∀Y.∀Z.Sum X Y -> (X -> Z) -> (Y -> Z) -> Z 976 | Case = ΛX.ΛY.ΛZ.λi:Sum X Y.λl:X -> Z.λr:Y -> Z.i Z l r 977 | 978 | Inl : ∀X.∀Y.X -> Sum X Y 979 | Inl = ΛX.ΛY.λx:X.ΛZ.λl:X -> Z.λr:Y -> Z.l x 980 | Inr : ∀X.∀Y.Y -> Sum X Y 981 | Inr = ΛX.ΛY.λy:Y.ΛZ.λl:X -> Z.λr:Y -> Z.r y 982 | ***** Products 983 | Prod A B = ∀Z.(A -> B -> Z) -> Z 984 | 985 | Fst : ∀X.∀Y.X -> Y -> X 986 | Fst = ΛX.ΛY.λx:X.λy:Y.x 987 | Snd : ∀X.∀Y.X -> Y -> Y 988 | Snd = ΛX.ΛY.λx:X.λy:Y.y 989 | 990 | Pair : ∀X.∀Y.X -> Y -> Prod X Y 991 | Pair = ΛX.ΛY.λx:X.λy:Y.ΛZ.λp:X -> Y -> Z.p x y 992 | ***** Natural numbers 993 | Nat = ∀Z.Z -> (Z -> Z) -> Z 994 | 995 | Iter : ∀Z.Nat -> Z -> (Z -> Z) -> Z 996 | Iter = ΛZ.λn:Nat.λz:Z.λs:Z -> Z.n Z z s 997 | 998 | Zero : Nat 999 | Zero = ΛZ.λz:Z.λs:Z -> Z.z 1000 | 1001 | Suc : Nat -> Nat 1002 | Suc = λn:Nat.ΛZ.λz:Z.λs:Z -> Z.s (n Z z s) 1003 | ***** Lists 1004 | List A = ∀Z.Z -> (A -> Z -> Z) -> Z 1005 | 1006 | Fold : ∀X.∀Z.List X -> Z -> (X -> Z -> Z) -> Z 1007 | Fold = ΛX.ΛZ.λl:List X.λn:Z.λc:X -> Z -> Z.l Z n c 1008 | 1009 | Nil : ∀X.List X 1010 | Nil = ΛX.ΛZ.λn:Z.λc:X -> Z -> Z.n 1011 | 1012 | Cons : ∀X.X -> List X -> List X 1013 | Cons = ΛX.λx:X.λl:List X.ΛZ.λn:Z.λc:X -> Z -> Z.c x (l Z n c) 1014 | ***** Encoding existential types 1015 | We really want type functions, but we don't have them. 1016 | 1017 | Exist X.A = ∀Z.(∀X.A -> Z) -> Z 1018 | 1019 | Pack_X.A : ∀X.A -> Exist X.A 1020 | Pack_X.A = ΛX.λy:A.ΛZ.λf:∀X.A -> Z.f X y 1021 | 1022 | Open_X.A : ∀Z.Exist X.A -> (∀X.A -> Z) -> Z 1023 | Open_X.A = ΛZ.λp:Exist X.A.λf:∀X.A -> Z.p Z f 1024 | 1025 | Reference: Girard, Proofs and Types 1026 | -------------------------------------------------------------------------------- /notes/OPLSS/2017/Harper-Licata/README.md: -------------------------------------------------------------------------------- 1 | # Harper-Licata 2 | 3 | ## Programming Languages Background 4 | 5 | **Instructor:** Professor Bob Harper (CMU) and Dr. Dan Licata (Wesleyan) 6 | 7 | **Videos:** 8 | [YouTube Playlist](https://www.youtube.com/playlist?list=PL5FJyaC2WsVmmQcFmwnOFtt6b-zxdQVgH), 9 | [Original Videos](https://www.cs.uoregon.edu/research/summerschool/summer17/topics.php) (direct from OPLSS website) 10 | 11 | 12 | -------------------------------------------------------------------------------- /notes/OPLSS/2017/Harper-Licata/day1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Harper-Licata/day1.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Harper-Licata/day1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Harper-Licata/day1.tar.gz -------------------------------------------------------------------------------- /notes/OPLSS/2017/Harper-Licata/day2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Harper-Licata/day2.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Harper-Licata/day3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Harper-Licata/day3.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Pfenning/01-inference.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Pfenning/01-inference.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Pfenning/02-propositions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Pfenning/02-propositions.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Pfenning/03-harmony.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Pfenning/03-harmony.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Pfenning/04-cutelim.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Pfenning/04-cutelim.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Pfenning/05-pap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/OPLSS/2017/Pfenning/05-pap.pdf -------------------------------------------------------------------------------- /notes/OPLSS/2017/Pfenning/README.md: -------------------------------------------------------------------------------- 1 | # Pfenning 2 | 3 | ## Substructural Logics 4 | 5 | **Instructor:** Professor Frank Pfenning, CMU 6 | 7 | ### Day 1 8 | **What was discussed:** a subset of 9 | + [Deductive Inference](http://www.cs.cmu.edu/~fp/courses/15816-f16/lectures/01-inference.pdf) (15-816 Fall 2016, Lecture 1) 10 | + [From Rules to Propositions](http://www.cs.cmu.edu/~fp/courses/15816-f16/lectures/02-propositions.pdf) (15-816 Fall 2016, Lecture 2) 11 | 12 | **Recommended exercises** to check your understanding, do 13 | + L1.3, L1.7, L2.5 14 | 15 | 16 | ### Day 2 17 | **What was discussed:** a subset of 18 | + [Harmony](http://www.cs.cmu.edu/~fp/courses/15816-f16/lectures/03-harmony.pdf) (15-816, Fall 2016, Lec 3) 19 | + [Cut Elimination](http://www.cs.cmu.edu/~fp/courses/15816-f16/lectures/04-cutelim.pdf) (15-816, Fall 2016, Lec 4), not discussed here 20 | + [Ordered Proofs as Concurrent Programs](http://www.cs.cmu.edu/~fp/courses/15816-f16/lectures/05-pap.pdf) (15-816, Fall 2016, Lec 5) 21 | 22 | **Recommended exercises:** to check your understanding, do 23 | + L3.2, L3.3, L3.4 (one of these) 24 | + L3.5 25 | + L5.3 26 | -------------------------------------------------------------------------------- /notes/Raamsdonk-LogicalVerification-2008.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamdemeo/TypeFunc/75f82cf01e92f0cf7a898e09faeb2740ed3eada5/notes/Raamsdonk-LogicalVerification-2008.pdf --------------------------------------------------------------------------------