├── .lvimrc ├── tex ├── reference.bib ├── preambles │ ├── slide.tex │ ├── note.tex │ └── basic.tex ├── banner.pdf ├── lecture1_note.tex ├── lecture2_note.tex ├── lecture3_note.tex ├── lecture1_slide.tex ├── lecture2_slide.tex ├── lecture3_slide.tex ├── ref.bib ├── beamercolorthemeowl.sty ├── lecture2.tex ├── lecture1.tex └── lecture3.tex ├── .gitignore ├── pdf └── .gitignore ├── README.md ├── Makefile └── LICENSE /.lvimrc: -------------------------------------------------------------------------------- 1 | let b:build_dir = "./output" 2 | -------------------------------------------------------------------------------- /tex/reference.bib: -------------------------------------------------------------------------------- 1 | ../../../../reference.bib -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | tex/output 2 | *.pdf 3 | tex/exam.tex 4 | tex/exercise-sol.tex 5 | -------------------------------------------------------------------------------- /tex/preambles/slide.tex: -------------------------------------------------------------------------------- 1 | \documentclass[t,11pt,aspectratio=1610]{beamer} 2 | -------------------------------------------------------------------------------- /tex/banner.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/L-TChen/Type-Theory/HEAD/tex/banner.pdf -------------------------------------------------------------------------------- /tex/lecture1_note.tex: -------------------------------------------------------------------------------- 1 | \input{preambles/note}\input{preambles/basic}\input{lecture1} 2 | -------------------------------------------------------------------------------- /tex/lecture2_note.tex: -------------------------------------------------------------------------------- 1 | \input{preambles/note}\input{preambles/basic}\input{lecture2} 2 | -------------------------------------------------------------------------------- /tex/lecture3_note.tex: -------------------------------------------------------------------------------- 1 | \input{preambles/note}\input{preambles/basic}\input{lecture3} 2 | -------------------------------------------------------------------------------- /pdf/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /tex/lecture1_slide.tex: -------------------------------------------------------------------------------- 1 | %! TeX program = xelatex 2 | \input{preambles/slide}\input{preambles/basic}\input{lecture1} 3 | -------------------------------------------------------------------------------- /tex/lecture2_slide.tex: -------------------------------------------------------------------------------- 1 | %! TeX program = xelatex 2 | \input{preambles/slide}\input{preambles/basic}\input{lecture2} 3 | -------------------------------------------------------------------------------- /tex/lecture3_slide.tex: -------------------------------------------------------------------------------- 1 | %! TEX program = xelatex 2 | 3 | \input{preambles/slide}\input{preambles/basic}\input{lecture3} 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction to Lambda Calculus and Types 2 | 3 | ## Requirement 4 | 5 | * XeLaTeX 6 | * latexmk 7 | * Fira Sans Font (optional) 8 | 9 | ## Compilation 10 | 11 | To compile every lecture note, do 12 | 13 | ``make`` 14 | 15 | or to compile specific lecture note, do 16 | 17 | ``make lecture{0,1,2,3}`` 18 | 19 | -------------------------------------------------------------------------------- /tex/preambles/note.tex: -------------------------------------------------------------------------------- 1 | %\documentclass[10pt,a4paper,envcountsect]{article} 2 | %\usepackage{beamerarticle} 3 | %\renewcommand{\vfill}{} 4 | \documentclass[10pt,a4paper,handout]{beamer} 5 | \usepackage{handoutWithNotes} 6 | \pgfpagesuselayout{2 on 1 with notes}[a4paper,landscape,border shrink=8mm] 7 | \setbeamercolor{background canvas}{bg=white} 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | outdir = output 2 | 3 | tex = latexmk -xelatex -outdir=$(outdir) 4 | 5 | lectures = lecture1 lecture2 lecture3 6 | 7 | all: $(lectures) 8 | 9 | $(lectures): 10 | cd tex/ && $(tex) $@_slide 11 | cp tex/$(outdir)/$@_slide.pdf pdf 12 | 13 | $(lectures:=_note): 14 | cd tex/ && $(tex) $@ 15 | cp tex/$(outdir)/$@.pdf pdf 16 | 17 | note: $(lectures:=_note) 18 | pdfjam pdf/*_note.pdf --angle '90' --outfile pdf/plt.pdf 19 | rm pdf/*_note.pdf 20 | 21 | clean: 22 | cd tex && latexmk -outdir=$(outdir) -C 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2018, Liang-Ting Chen 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /tex/ref.bib: -------------------------------------------------------------------------------- 1 | 2 | 3 | @article{Bohm1985, 4 | author = {B{\"o}hm, Corrado and Berarducci, Alessandro}, 5 | journal = {Theoretical Computer Science}, 6 | pages = {135--154}, 7 | title = {Automatic synthesis of typed {$\Lambda$}-programs on 8 | term algebras}, 9 | volume = {39}, 10 | year = {1985}, 11 | doi = {10.1016/0304-3975(85)90135-5}, 12 | } 13 | 14 | @inproceedings{Koopman2014, 15 | address = {New York, NY, USA}, 16 | author = {Koopman, Pieter and Plasmeijer, Rinus and Jansen, Jan Martin}, 17 | booktitle = {Implementation and Application of Functional Languages (IFL)}, 18 | publisher = {Association for Computing Machinery}, 19 | title = {Church encoding of data types considered harmful for 20 | implementations: Functional pearl}, 21 | year = {2014}, 22 | doi = {10.1145/2746325.2746330}, 23 | isbn = {9781450332842}, 24 | } 25 | 26 | @inproceedings{Firsov2018, 27 | author = {Firsov, Denis and Richard, Blair and Stump, Aaron}, 28 | booktitle = {Interactive Theorem Proving (ITP)}, 29 | editor = {Avigad, Jeremy and Mahboubi, Assia}, 30 | pages = {235--252}, 31 | publisher = {Springer, Cham}, 32 | series = {Lecture Notes in Computer Science}, 33 | title = {Efficient {Mendler}-style lambda-encodings in 34 | {Cedille}}, 35 | volume = {10895}, 36 | year = {2018}, 37 | doi = {10.1007/978-3-319-94821-8_14}, 38 | } 39 | 40 | @inproceedings{Reynolds1984, 41 | author = {Reynolds, John C.}, 42 | booktitle = {Semantics of Data Types (SDT)}, 43 | editor = {Kahn, Gilles and MacQueen, David B. and 44 | Plotkin, Gordon}, 45 | pages = {145--156}, 46 | series = {Lecture Notes in Computer Science}, 47 | title = {Polymorphism is not set-theoretic}, 48 | volume = {173}, 49 | year = {1984}, 50 | doi = {10.1007/3-540-13346-1_7}, 51 | } 52 | 53 | @incollection{Pitts1987, 54 | author = {Pitts, A. M.}, 55 | booktitle = {Category Theory and Computer Science}, 56 | editor = {Pitt, David H. and Poign{\'e}, Axel and 57 | Rydeheard, David E.}, 58 | pages = {12--39}, 59 | publisher = {Springer, Berlin, Heidelberg}, 60 | series = {Lecture Notes in Computer Science}, 61 | title = {Polymorphism is set theoretic, constructively}, 62 | volume = {283}, 63 | year = {1987}, 64 | doi = {10.1007/3-540-18508-9_18}, 65 | isbn = {978-3-540-48006-8}, 66 | } 67 | 68 | @article{Leivant1991, 69 | author = {Leivant, Daniel}, 70 | journal = {Information and Computation}, 71 | number = {1}, 72 | pages = {93--113}, 73 | title = {Finitely stratified polymorphism}, 74 | volume = {93}, 75 | year = {1991}, 76 | doi = {10.1016/0890-5401(91)90053-5}, 77 | } 78 | 79 | @book{Girard1989, 80 | author = {Girard, Jean-Yves and Lafont, Yves and Taylor, Paul}, 81 | number = {7}, 82 | publisher = {Cambridge University Press}, 83 | series = {Cambridge Tracts in Theoretical Computer Science}, 84 | title = {Proofs and Types}, 85 | year = {1989}, 86 | } 87 | 88 | @inproceedings{Wadler1989, 89 | address = {New York, NY, USA}, 90 | author = {Wadler, Philip}, 91 | booktitle = {4th International Conference on Functional 92 | Programming Languages and Computer Architecture 93 | (FPCA)}, 94 | pages = {347--359}, 95 | publisher = {ACM Press}, 96 | title = {Theorems for free!}, 97 | year = {1989}, 98 | doi = {10.1145/99370.99404}, 99 | isbn = {0-89791-328-0}, 100 | } 101 | 102 | -------------------------------------------------------------------------------- /tex/preambles/basic.tex: -------------------------------------------------------------------------------- 1 | \usetheme[block=fill,titleformat=smallcaps]{metropolis} 2 | 3 | \author[L.-T. Chen]{陳亮廷 Chen, Liang-Ting} 4 | \institute{Institute of Information Science\\Academia Sinica} 5 | \date[FLOLAC'24]{Formosan Summer School on Logic, Language, and Computation 2024} 6 | 7 | %\setbeamercolor{frame}{fg=black} 8 | 9 | \newcounter{saveenumi} 10 | \newcommand{\seti}{\setcounter{saveenumi}{\value{enumi}}} 11 | \newcommand{\conti}{\setcounter{enumi}{\value{saveenumi}}} 12 | \mode{\resetcounteronoverlays{saveenumi}} 13 | 14 | \usepackage[british]{babel} 15 | \mode
{\usepackage[a4paper]{geometry}} 16 | \usepackage{mathtools} 17 | \usepackage[warnings-off={mathtools-colon,mathtools-overbracket}]{unicode-math} 18 | 19 | \setmathfont{NewCMMath-Regular.otf} 20 | %\usepackage{newcomputermodern} 21 | 22 | \usepackage{bussproofs} 23 | \EnableBpAbbreviations 24 | \usepackage{mathpartir} 25 | \usepackage{multicol} 26 | \usepackage{manfnt} 27 | \usepackage{centernot} 28 | \usepackage{xspace} 29 | 30 | \usepackage{graphicx} 31 | \usepackage[all,cmtip]{xy} 32 | \mode
{\usepackage{hyperref}} 33 | \usepackage{tikz} 34 | \usetikzlibrary{trees,decorations.pathreplacing,fit} 35 | \usepackage{microtype} 36 | 37 | \setbeamertemplate{theorems}[numbered] 38 | \theoremstyle{plain} 39 | \newtheorem*{theorem*}{Theorem} 40 | \newtheorem{proposition}[theorem]{Proposition} 41 | \newtheorem{convention}[theorem]{Convention} 42 | \newtheorem*{convention*}{Convention} 43 | \newtheorem*{quiz*}{Quiz} 44 | \newtheorem{remark}{Remark}[section] 45 | \newtheorem*{remark*}{Remark} 46 | \theoremstyle{definition} 47 | \newtheorem*{exercise*}{Exercise} 48 | 49 | \newenvironment<>{idea}[1]{\begin{block}{Idea}#1}{\end{block}} 50 | 51 | \usepackage{fontspec} 52 | \usepackage{xeCJK} 53 | 54 | %\setmainfont{Fira Sans} 55 | \setsansfont[BoldFont={Fira Sans Light}]{Fira Sans} 56 | \setmonofont{Fira Code} 57 | \setCJKmainfont{Noto Serif TC} 58 | \setCJKsansfont{Noto Sans TC} 59 | \setCJKmonofont{Noto Sans TC} 60 | 61 | \newcommand{\FV}{\mathbf{FV}} 62 | \newcommand{\Var}{\mathbf{Var}} 63 | \newcommand{\BV}{\mathbf{BV}} 64 | \newcommand{\term}{\mathtt{Term}} 65 | \newcommand{\type}{\mathtt{Type}} 66 | \newcommand{\reason}[1]{\{\text{\,#1\,}\}} 67 | \renewcommand{\o}{\circ} 68 | \newcommand{\mnd}[1]{{\overleftarrow{#1}}} 69 | \newcommand{\cmnd}[1]{{\overrightarrow{#1}}} 70 | \makeatletter 71 | \newcommand*{\defeq}{\mathrel{\rlap{% 72 | \raisebox{0.3ex}{$\m@th\cdot$}}% 73 | \raisebox{-0.3ex}{$\m@th\cdot$}}% 74 | =} 75 | \makeatother 76 | 77 | \newcommand{\op}{\mathrm{op}} 78 | \newcommand{\subst}[2]{[{#1}/{#2}]} 79 | 80 | \newcommand{\Hask}{\textsc{Haskell}\xspace} 81 | \newcommand{\Python}{\textsc{Python}} 82 | \newcommand{\OCaml}{\textsc{OCaml}} 83 | 84 | \newcommand{\leftexp}[2]{{\vphantom{#2}}^{#1}{#2}} 85 | \newcommand{\set}[2]{\{\,#1\mid#2\,\}} 86 | \newcommand{\sem}[1]{[\![#1]\!]} 87 | \newcommand{\id}{\mathit{id}} 88 | 89 | \newcommand{\M}{\mathsf{M}} 90 | \newcommand{\N}{\mathsf{N}} 91 | \newcommand{\V}{\mathsf{V}} 92 | \newcommand{\T}{\mathbb{T}} 93 | 94 | % Macros for lambda calculus 95 | \DeclareRobustCommand\longtwoheadrightarrow 96 | {\relbar\joinrel\twoheadrightarrow} 97 | 98 | \newcommand{\reduce}{\longtwoheadrightarrow_{\beta}} 99 | \newcommand{\parreduce}{\Longrightarrow_{\beta}} 100 | \newcommand{\onereduce}{\longrightarrow_{\beta}} 101 | \newcommand{\nat}{\mathtt{nat}} 102 | \newcommand{\bool}{\mathtt{bool}} 103 | \newcommand{\List}{\mathtt{list}} 104 | \newcommand{\Bool}{\mathbb{B}} 105 | 106 | \newcommand{\bc}{\mathbf{c}} 107 | \newcommand{\add}{\mathtt{add}} 108 | \newcommand{\mul}{\mathtt{mul}} 109 | \newcommand{\fix}{\mathtt{fix}} 110 | \newcommand{\natrec}{\mathtt{natrec}} 111 | \newcommand{\fold}{\mathtt{fold}} 112 | \newcommand{\suc}{\mathtt{suc}} 113 | \newcommand{\pred}{\mathtt{pred}} 114 | \newcommand{\ifz}{\mathtt{ifz}} 115 | \newcommand{\cond}{\mathtt{cond}} 116 | \newcommand{\zero}{\mathtt{zero}} 117 | \newcommand{\true}{\mathtt{true}} 118 | \newcommand{\false}{\mathtt{false}} 119 | \newcommand{\neutral}{\mathtt{Neutral}} 120 | \newcommand{\normal}{\mathtt{Normal}} 121 | 122 | \newcommand{\btt}{\mathsf{t}} 123 | \newcommand{\bff}{\mathsf{f}} 124 | 125 | \newcommand{\PCF}{{\textbf{PCF}}} 126 | 127 | \newcommand{\notimplies}{\centernot\implies} 128 | \newcommand{\notonereduce}{\centernot\onereduce} 129 | 130 | \newcommand {\textplus}{\nolinebreak\hspace{-.05em}\raisebox{.4ex}{\tiny\bf +}} 131 | 132 | \DeclarePairedDelimiter\Brackets{\lBrack}{\rBrack} 133 | \DeclarePairedDelimiter\Parens{\lParen}{\rParen} 134 | 135 | \makeatletter 136 | %%% Lambda abstractions. 137 | % Each variable being abstracted over is a separate argument. If 138 | % there is more than one such argument, they *must* be enclosed in 139 | % braces. Arguments can be untyped, as in \lam{x}{y}, or typed with a 140 | % colon, as in \lam{x:A}{y:B}. In the latter case, the colons are 141 | % automatically noticed and (with current implementation) the space 142 | % around the colon is reduced. You can even give more than one variable 143 | % the same type, as in \lam{x,y:A}. 144 | \def\lam#1{{\lambda}\@lamarg#1:\@endlamarg\@ifnextchar\bgroup{.\,\lam}{.\,}} 145 | \def\@lamarg#1:#2\@endlamarg{\if\relax\detokenize{#2}\relax #1\else\@lamvar{\@lameatcolon#2},#1\@endlamvar\fi} 146 | \def\@lamvar#1,#2\@endlamvar{(#2\,{:}\,#1)} 147 | % \def\@lamvar#1,#2{{#2}^{#1}\@ifnextchar,{.\,{\lambda}\@lamvar{#1}}{\let\@endlamvar\relax}} 148 | \def\@lameatcolon#1:{#1} 149 | \let\lamt\lam 150 | % This version silently eats any typing annotation. 151 | \def\lamu#1{{\lambda}\@lamuarg#1:\@endlamuarg\@ifnextchar\bgroup{.\,\lamu}{.\,}} 152 | \def\@lamuarg#1:#2\@endlamuarg{#1} 153 | \makeatother 154 | -------------------------------------------------------------------------------- /tex/beamercolorthemeowl.sty: -------------------------------------------------------------------------------- 1 | %% 2 | %% This is file `beamercolorthemeowl.sty', 3 | %% generated with the docstrip utility. 4 | %% 5 | %% The original source files were: 6 | %% 7 | %% beamercolorthemeowl.dtx (with options: `package') 8 | %% --------------------------------------------------------------------------- 9 | %% Copyright 2016 Ross Churchley. Contributions to this package are welcome at 10 | %% 11 | %% https://github.com/rchurchley/beamercolortheme-owl 12 | %% 13 | %% This work may be distributed and/or modified under the conditions of 14 | %% the LaTeX Project Public License, either version 1.3 of this license 15 | %% or (at your option) any later version. The latest version of this 16 | %% license is in: 17 | %% 18 | %% http://www.latex-project.org/lppl.txt 19 | %% 20 | %% and version 1.3 or later is part of all distributions of LaTeX version 21 | %% 2005/12/01 or later. 22 | %% --------------------------------------------------------------------------- 23 | \NeedsTeXFormat{LaTeX2e}[2005/12/01] 24 | \ProvidesPackage{beamercolorthemeowl} 25 | [2016/03/15 v0.1.1 A visible colour theme for Beamer presentations] 26 | \RequirePackage{etoolbox} 27 | \newtoggle{snowy} 28 | \newtoggle{cautious} 29 | \@ifclassloaded{beamer}{ 30 | \DeclareOptionBeamer{snowy}{\toggletrue{snowy}} 31 | \DeclareOptionBeamer{cautious}{\toggletrue{cautious}} 32 | \ProcessOptionsBeamer 33 | }{} 34 | \@ifclassloaded{beamer}{ 35 | \iftoggle{snowy}{ 36 | \setbeamercolor{normal text}{fg=black, bg=white} 37 | }{ 38 | \setbeamercolor{normal text}{fg=white, bg=black} 39 | } 40 | }{} 41 | \RequirePackage{xcolor} 42 | \definecolor{OwlRed}{RGB}{ 255, 92, 168} 43 | \definecolor{OwlGreen}{RGB}{ 90, 168, 0} 44 | \definecolor{OwlBlue}{RGB}{ 0, 152, 233} 45 | \definecolor{OwlYellow}{RGB}{ 242, 147, 24} 46 | \colorlet{OwlViolet}{OwlRed!50!OwlBlue} 47 | \colorlet{OwlBrown}{OwlRed!50!OwlGreen} 48 | \colorlet{OwlOrange}{OwlRed!50!OwlYellow} 49 | \colorlet{OwlCyan}{OwlGreen!50!OwlBlue} 50 | \iftoggle{cautious}{}{ 51 | \colorlet{red}{OwlRed} 52 | \colorlet{green}{OwlGreen} 53 | \colorlet{blue}{OwlBlue} 54 | \colorlet{yellow}{OwlYellow} 55 | \colorlet{violet}{OwlViolet} 56 | \colorlet{brown}{OwlBrown} 57 | \colorlet{orange}{OwlOrange} 58 | \colorlet{cyan}{OwlCyan} 59 | } 60 | \@ifclassloaded{beamer}{ 61 | \setbeamercolor*{alerted text}{ 62 | fg=OwlRed 63 | } 64 | \setbeamercolor*{example text}{ 65 | fg=OwlGreen 66 | } 67 | \setbeamercolor*{title}{ 68 | use=normal text, 69 | fg=normal text.fg 70 | } 71 | \setbeamercolor*{title in sidebar}{ 72 | use=normal text, 73 | fg=normal text.fg 74 | } 75 | \setbeamercolor*{titlelike}{ 76 | use=normal text, 77 | parent=normal text.fg 78 | } 79 | \setbeamercolor*{author}{ 80 | use=normal text, 81 | parent=normal text.fg 82 | } 83 | \setbeamercolor*{date}{ 84 | use=normal text, 85 | parent=normal text.fg 86 | } 87 | \setbeamercolor*{institute}{ 88 | use=normal text, 89 | parent=normal text.fg 90 | } 91 | \setbeamercolor*{structure}{ 92 | use=normal text, 93 | fg=normal text.fg!50!normal text.bg 94 | } 95 | \setbeamercolor*{palette primary}{ 96 | use=normal text, 97 | fg=normal text.fg!90!normal text.bg, 98 | bg=normal text.bg!90!normal text.fg 99 | } 100 | \setbeamercolor*{palette secondary}{ 101 | use=normal text, 102 | fg=alerted text.fg!75!normal text.bg, 103 | bg=normal text.bg!90!normal text.fg 104 | } 105 | \setbeamercolor*{palette tertiary}{ 106 | use=normal text, 107 | fg=example text.fg!75!normal text.bg, 108 | bg=normal text.bg!90!normal text.fg 109 | } 110 | \setbeamercolor*{palette quaternary}{ 111 | use=normal text, 112 | fg=normal text.fg!75!normal text.bg, 113 | bg=normal text.bg!90!normal text.fg 114 | } 115 | \setbeamercolor*{sidebar}{ 116 | use=normal text, 117 | fg=normal text.fg!80!normal text.bg, 118 | bg=normal text.bg!80!normal text.fg 119 | } 120 | \setbeamercolor*{palette sidebar primary}{ 121 | use=palette primary, 122 | parent=palette primary 123 | } 124 | \setbeamercolor*{palette sidebar secondary}{ 125 | use=palette primary, 126 | parent=palette primary 127 | } 128 | \setbeamercolor*{palette sidebar tertiary}{ 129 | use=palette quaternary, 130 | parent=palette quaternary 131 | } 132 | \setbeamercolor*{palette sidebar quaternary}{ 133 | use=palette quaternary, 134 | parent=palette quaternary 135 | } 136 | \setbeamercolor*{frametitle}{ 137 | use=palette primary, 138 | parent=palette primary 139 | } 140 | \setbeamercolor*{block title}{ 141 | use=normal text, 142 | fg=normal text.fg, 143 | bg= 144 | } 145 | \setbeamercolor{block body}{ 146 | bg= 147 | } 148 | \setbeamercolor*{block title alerted}{% 149 | use={block title, alerted text}, 150 | fg=alerted text.fg, 151 | bg=block title.bg 152 | } 153 | \setbeamercolor*{block title example}{% 154 | use={block title, example text}, 155 | fg=example text.fg, 156 | bg=block title.bg 157 | } 158 | \setbeamercolor*{block body alerted}{ 159 | use=block body, 160 | parent=block body 161 | } 162 | \setbeamercolor*{block body example}{ 163 | use=block body, 164 | parent=block body 165 | } 166 | \def\beamer@themerounded@shadow{false} 167 | \setbeamertemplate{navigation symbols}{} 168 | }{} 169 | \endinput 170 | %% 171 | %% End of file `beamercolorthemeowl.sty'. 172 | -------------------------------------------------------------------------------- /tex/lecture2.tex: -------------------------------------------------------------------------------- 1 | %! TEX program = xelatex 2 | %! TEX root = lecture2_slide.tex 3 | 4 | \title{\texorpdfstring{$\lambda$}{λ}-Calculus} 5 | \subtitle{Simple Types and their Extensions} 6 | \begin{document} 7 | 8 | \bgroup 9 | \usebackgroundtemplate{\includegraphics[width=\paperwidth]{banner.pdf}} 10 | \begin{frame}[plain,noframenumbering] 11 | \maketitle 12 | \end{frame} 13 | \egroup 14 | 15 | %\section{Properties of \texorpdfstring{$\lambda$}{λ}-Calculus} 16 | % 17 | %\begin{frame} 18 | % \begin{example} 19 | % Suppose $t$ is a term and $y \not\in \FV(t)$. 20 | % Then, consider 21 | % \[ 22 | % (\lambda y.\, t)\, ((\lambda x.\, x\,x)(\lambda x.\, x\, x)) 23 | % \] 24 | % \end{example} 25 | % \mode{\vfill} 26 | % Observations: 27 | % \begin{itemize} 28 | % \item Some evaluation may diverge while some may converge. 29 | % \item Full $\beta$-reduction lacks for determinacy. 30 | % \end{itemize} 31 | % Question: 32 | % \begin{itemize} 33 | % \item Does every path give the same evaluation? 34 | % \end{itemize} 35 | %\end{frame} 36 | % 37 | %\begin{frame}{Confluence} 38 | %\begin{theorem}[Church-Rosser] 39 | % Given $u_1$ and $u_2$ with $t \reduce u_1$ and $t\reduce u_2$, there is $v$ 40 | % such that $u_1 \reduce v$ and $u_2 \reduce v$, pictorially, 41 | % \[ 42 | % \xymatrix{ 43 | % & t \ar@{->>}[rd]^{\beta} \ar@{->>}[ld]_{\beta} \\ 44 | % u_1 \ar@{->>}[rd]_{\beta} & & u_2 \ar@{->>}[ld]^{\beta} \\ 45 | % & v 46 | % } 47 | % \] 48 | %\end{theorem} 49 | % 50 | %No matter which way we choose we can always find a \alert{confluent} 51 | %term. 52 | % 53 | %\end{frame} 54 | % 55 | %\begin{frame}{Normal form} 56 | % \begin{definition} 57 | % $M$ is \emph{in normal form} if 58 | % there is no $N$ such that $M \onereduce N$, abbreviated to $M \notonereduce$. 59 | % 60 | % \end{definition} 61 | % \begin{lemma} 62 | % Suppose that $M$ is in normal form. Then 63 | % $M \reduce N$ implies $M =_\alpha N$. 64 | % \label{lem:normal-no-reduction}% 65 | % \end{lemma} 66 | % \begin{proof} 67 | % By induction on the derivation $d$ of $M \reduce N$. 68 | % \begin{enumerate} 69 | % \item If $d$ is given by (0-step), then 70 | % $M \reduce N$ where $M =_\alpha N$ by definition. 71 | % 72 | % \item If $d$ is given by (n+1-step), then 73 | % $M \onereduce M'$ and $M' \reduce N$ are derivable for some $M'$. By 74 | % assumption $M \onereduce N$ is not derivable for any $N$, so by 75 | % contradiction the statement follows. 76 | % \end{enumerate} 77 | % \end{proof} 78 | % 79 | %\end{frame} 80 | % 81 | %\begin{frame}{Corollaries of confluence} 82 | % \begin{corollary}[Uniqueness of normal forms] 83 | % Let $M$ be a term with $M \reduce N_1$ and $M\reduce N_2$ where 84 | % $N_i$'s are in normal form. Then, $N_1 =_\alpha N_2$. 85 | % \label{coro:uniqueness-normal}% 86 | % \end{corollary} 87 | % 88 | % 89 | % \begin{corollary}[Computationally equal terms have a confluent term] 90 | % If $M =_\beta N$, then there exists $L$ satisfying 91 | % \[ 92 | % \xymatrix{ 93 | % M \ar@{->>}[rd]_{\beta} \ar@{}[rr]|{=_\beta} & & N \ar@{->>}[ld]^{\beta} \\ 94 | % & L 95 | % } 96 | % \] 97 | % \label{coro:computational-meanting-confluence} 98 | % \end{corollary} 99 | % \begin{proof}[Proof sketch] 100 | % By induction on the derivation of $M =_\beta N$. 101 | % \end{proof} 102 | % 103 | %%\begin{corollary} 104 | %% Let $=_\beta$ denote the congruence closure of $\onereduce$. 105 | %% \begin{enumerate} 106 | %% \item If $M =_\beta N$, then there exists $L$ such that 107 | %% \[ 108 | %% M \reduce L 109 | %% \; _{\beta*}\longleftarrow N 110 | %% \] 111 | %% \item If in addition $N$ is in normal form, then 112 | %% $M \reduce N$. 113 | %% \end{enumerate} 114 | %%\end{corollary} 115 | %%\begin{block}{Homework} 116 | %% Show this corollary. 117 | %%\end{block} 118 | %\end{frame} 119 | 120 | 121 | \section{Simply Typed \texorpdfstring{$\lambda$}{λ}-Calculus: Introduction} 122 | 123 | \begin{frame}{Adding types to a language} 124 | While $\lambda$-calculus is expressive and computationally powerful, it is rather painful to write programs inside $\lambda$-calculus. 125 | 126 | Function can be applied to an arbitrary term which can represent a Boolean value, a number, or even a function, so as a programming language it is not easy to see the \alert{intention} of a program. 127 | 128 | Therefore, we will consider a formal definition of a \alert{typing judgement} 129 | \[ 130 | \Gamma \vdash t : A 131 | \] 132 | which specifies the type $A$ of a term $t$ under a list of free (typed) variables, allowing us to \emph{restrict the formation} of a valid term by typing. 133 | \end{frame} 134 | 135 | \section{Simply Typed \texorpdfstring{$\lambda$}{λ}-Calculus: Statics} 136 | 137 | %\begin{frame}{Typing judgement} 138 | % A \alert{typing judgement} is of the form 139 | % \[ 140 | % \Gamma \vdash t : A 141 | % \] 142 | % saying the \emph{term $t$ is of type $\sigma$ under the context $\Gamma$} 143 | % where 144 | % \begin{description} 145 | % \item[$\Gamma$] is a list of variables $x_i : A_i$ available in $t$ 146 | % \item[$A$] is the type of $t$. 147 | % \end{description} 148 | % 149 | % For example, the typing judgement 150 | % \[ 151 | % x_1:A_1, x_2: A_2 \vdash x_1 : A_1 152 | % \] 153 | % means `the term $x_1$ is of type $A_1$ under the context $x_1:A_1, x_2:A_2$'. 154 | %\end{frame} 155 | 156 | 157 | 158 | \begin{frame}{Higher-order function type} 159 | Assume $\mathbb{V}$ is a set of type variables different from variables in untyped $\lambda$-terms. 160 | (And suppress its existence from now on.) 161 | 162 | \begin{definition} 163 | The judgement $A : \type$ is defined inductively as follows. 164 | \begin{multicols}{2} 165 | \begin{prooftree} 166 | \AXC{\vphantom{$X$}} 167 | \RightLabel{if $X \in \mathbb{V}$} 168 | \UIC{$X : \type$} 169 | \end{prooftree} 170 | \begin{prooftree} 171 | \AXC{$A : \type$} 172 | \AXC{$B : \type$} 173 | \BIC{$A \to B : \type$} 174 | \end{prooftree} 175 | \end{multicols} 176 | where $A \to B$ represents a function type from $A$ to $B$. 177 | \end{definition} 178 | We say that $A$ is a type if $A : \type$ is derivable. 179 | 180 | \end{frame} 181 | 182 | \begin{frame} 183 | The function type is \alert{higher-order}, because 184 | \begin{enumerate} 185 | \item functions can be arguments of another function; 186 | \item functions can be the result of a computation. 187 | \end{enumerate} 188 | For example, 189 | \begin{description} 190 | \item[$\;(A_1 \to A_2) \to B$] a function type whose argument is of type $A_1 \to A_2$; 191 | \item[$\;A_1 \to (A_2 \to B)$] a function whose return type is $A_2 \to B$. 192 | \end{description} 193 | \vfill 194 | Following the convention of function application, we introduce the convention for the function type: 195 | \begin{block}{Convention} 196 | \[ 197 | A_1 \to A_2 \to \dots A_n \quad \defeq\quad A_1 \to 198 | (A_2 \to ( \dots \to (A_{n-1} \to A_n)\dots)) 199 | \] 200 | \end{block} 201 | \end{frame} 202 | 203 | \begin{frame}{Context} 204 | 205 | \begin{definition} 206 | A \emph{typing context}~$\Gamma$ 207 | is a sequence 208 | \[ 209 | \Gamma \equiv x_1 : A_1,\; x_2 : A_2,\; \ldots,\; x_n : A_n 210 | \] 211 | of \alert{\emph{distinct variables}} $x_i$ of type $A_i$. 212 | \end{definition} 213 | 214 | \begin{definition} 215 | The membership judgement $\Gamma \ni (x : A)$ is defined inductively: 216 | 217 | \begin{multicols}{2} 218 | \begin{prooftree} 219 | \AXC{$\vphantom{\Gamma}$} 220 | \UIC{$\Gamma, x : A \ni x : A$} 221 | \end{prooftree} 222 | \begin{prooftree} 223 | \AXC{$\Gamma \ni x : A$} 224 | \UIC{$\Gamma, y : B \ni x : A$} 225 | \end{prooftree} 226 | \end{multicols} 227 | We say that $x$ of type $A$ occurs in $\Gamma$ if $\Gamma \ni (x : A)$ if derivable. 228 | \end{definition} 229 | \end{frame} 230 | 231 | %\begin{frame}{Hypothetical judgement} 232 | % A \emph{hypothetical judgement} (or, an inference rule) is of the form 233 | % \begin{prooftree} 234 | % \AXC{$J_1$} 235 | % \AXC{$J_2$} 236 | % \AXC{$\dots$} 237 | % \AXC{$J_n$} 238 | % \RightLabel{(rule)} 239 | % \QuaternaryInfC{$J$} 240 | % \end{prooftree} 241 | % consisting of 242 | % \begin{description} 243 | % \item[hypothesis] a set of judgements $J_i$, for $1 \leq i \leq n$ 244 | % \item[name] the name for the reason why $J_i$'s imply $J$ 245 | % \item[conclusion] a single judgement $J$ 246 | % \end{description} 247 | % Examples include 248 | % \begin{enumerate} 249 | % \item The formation of $\lambda$-terms (var), (app), and (abs). 250 | % \item The formation of $\alpha$-equivalence $=_\alpha$. 251 | % \item The formation of simple types $\mathbb{T}$. 252 | % \end{enumerate} 253 | %\end{frame} 254 | 255 | \begin{frame}{Typing rule -- Curry-style typing system} 256 | The implicit typing system for simply typed $\lambda$-calculus is defined by the following typing rules, i.e.\ 257 | inference rules with its conclusion a typing judgement: 258 | 259 | \begin{prooftree} 260 | \AXC{} 261 | \RightLabel{(var)\quad if $\Gamma \ni (x : A)$\quad } 262 | \UIC{$\Gamma \vdash_i x : A$} 263 | \end{prooftree} 264 | \begin{prooftree} 265 | \AXC{$\Gamma, x : A \vdash_i t : B$} 266 | \RightLabel{(abs)} 267 | \UIC{$\Gamma \vdash_i \lambda x.\; t : A \to B$} 268 | \end{prooftree} 269 | \begin{prooftree} 270 | \AXC{$\Gamma \vdash_i t : A \to B$} 271 | \AXC{$\Gamma \vdash_i u : A$} 272 | \RightLabel{(app)} 273 | \BIC{$\Gamma \vdash_i t\;u : B$} 274 | \end{prooftree} 275 | We say that $t$ is a \alert{closed term} if $\vdash t : A$ is derivable. 276 | 277 | N.B.\ Whether a term $t$ has a typing derivation is a \emph{property} of $t$. 278 | \end{frame} 279 | 280 | %\begin{frame}{Derivation} 281 | % Given a set $\mathcal{R}$ of hypothetical judgements, a \emph{derivation} of 282 | % \begin{prooftree} 283 | % \AXC{$J_1$} 284 | % \AXC{$J_2$} 285 | % \AXC{$\dots$} 286 | % \AXC{$J_n$} 287 | % \QuaternaryInfC{$J$} 288 | % \end{prooftree} 289 | % is a tree of instances of rules in $\mathcal{R}$ composed with $J_i$'s as the top-level hypothesises. 290 | % 291 | % A judgement $J$ is \emph{derivable} (with assumptions $J_i$'s) if there is derivation whose root is $J$ (with assumptions $J_i$'s). 292 | % 293 | % \begin{example} 294 | % The fact that 295 | % \[ 296 | % \lambda x\,y.\, y 297 | % \] 298 | % is a $\lambda$-term means the judgement $\lambda x\,y.\, y$ is derivable 299 | % from the formation rules of $\lambda$-terms without any assumption. 300 | % \end{example} 301 | %\end{frame} 302 | 303 | \begin{frame}{Syntax-directedness} 304 | A typing system is \emph{syntax-directed} if it has \emph{exactly} one typing rule 305 | for each term construct. 306 | \vfill 307 | By being syntax-directed, every typing derivation can be inverted: 308 | \begin{lemma}[Typing inversion] 309 | Suppose that $\Gamma \vdash_i t : A$ is derivable. 310 | Then, 311 | \begin{description} 312 | \item[$t \equiv x$] implies $x : A$ occurs in $\Gamma$. 313 | \item[$t \equiv \lambda x.\, t'$] implies $A = B \to C$ and $\Gamma, x: B \vdash_i u' : C$. 314 | \item[$t \equiv u\;v$] implies there is some $B$ such that $\Gamma \vdash_i u : B \to A$ and $\Gamma \vdash_i v : B$. 315 | \end{description} 316 | \end{lemma} 317 | This lemma is particularly useful when constructing a typing derivation by hand. 318 | \end{frame} 319 | 320 | \begin{frame}{Typing derivation} 321 | % The judgement $\vdash_i \lam{x}x : A \to A$, for any type $A$, has a derivation 322 | % \begin{prooftree} 323 | % \AXC{} 324 | % \RightLabel{(var)} 325 | % \UIC{$x : A \vdash_i x : A$} 326 | % \RightLabel{(abs)} 327 | % \UIC{$\vdash_i \lambda x.\; x : (A \to A)$} 328 | % \end{prooftree} 329 | 330 | For any types $A$ and $B$, the judgement $ \vdash_i \lambda x\,y.\, x : A \to B \to A$ 331 | has a derivation 332 | \begin{prooftree} 333 | \AXC{} 334 | \RightLabel{(var)} 335 | \UIC{$x : A, y: B \vdash_i x : A$} 336 | \RightLabel{(abs)} 337 | \UIC{$x : A \vdash_i \lambda y.\, x : B \to A$} 338 | \RightLabel{(abs)} 339 | \UIC{$\vdash_i \lambda x\,y.\, x : A \to B \to A$} 340 | \end{prooftree} 341 | Therefore, $\lam{x\,y}x$ is a program of type $A \to B \to A$. 342 | 343 | \end{frame} 344 | 345 | \begin{frame}{Exercise} 346 | Derive the typing judgement 347 | \[ 348 | \vdash_i \lambda f\,g\,x.\, f\,x\, (g\,x) : (A \to B \to C) \to 349 | (A\to B) \to A \to C 350 | \] 351 | for every types $A, B$ and $C$. 352 | \end{frame} 353 | 354 | %\begin{frame}{Explicit typing: Typed terms} 355 | %\begin{definition}[Typed terms] 356 | % The formation $M \; \term_{V}^{\to}$ of typed terms is defined by 357 | % \begin{prooftree} 358 | % \AXC{$x \in V$} 359 | % \UIC{$x \quad \term_{V}^{\to}$} 360 | % \end{prooftree} 361 | % \begin{prooftree} 362 | % \AXC{$M \quad \term_{V}^{\to}$} 363 | % \AXC{$N \quad \term_{V}^{\to}$} 364 | % \BIC{$M\, N \quad \term_{V}^{\to}$} 365 | % \end{prooftree} 366 | % \begin{prooftree} 367 | % \AXC{$M \quad \term_{V}^{\to}$} 368 | % \AXC{$x \in V$} 369 | % \AXC{$\tau \quad \type$} 370 | % \TIC{$\lambda {\color{red}x : \tau}.\; M \quad \term_{V}^{\to}$} 371 | % \end{prooftree} 372 | %\end{definition} 373 | %\end{frame} 374 | % 375 | %\begin{frame}{Explicit typing: Typing rules} 376 | %\begin{definition}[Typing Rules] 377 | % Typing derivations on \emph{typed terms} are defined by 378 | % \begin{prooftree} 379 | % \AXC{$\Gamma \ni (x : \sigma)$} 380 | % \RightLabel{(var)} 381 | % \UIC{$\Gamma \vdash_e x : \sigma$} 382 | % \end{prooftree} 383 | % \begin{prooftree} 384 | % \AXC{$\Gamma \vdash_e M : \sigma \to \tau$} 385 | % \AXC{$\Gamma \vdash_e N : \sigma$} 386 | % \RightLabel{(app)} 387 | % \BIC{$\Gamma \vdash_e M\;N : \tau$} 388 | % \end{prooftree} 389 | % \begin{prooftree} 390 | % \AXC{$\Gamma, {\color{red}x : \sigma} \vdash_e M : \tau$} 391 | % \RightLabel{(abs)} 392 | % \UIC{$\Gamma \vdash_e \lambda {\color{red}x : \sigma} .\; M : \sigma \to \tau$} 393 | % \end{prooftree} 394 | %\end{definition} 395 | % 396 | %\end{frame} 397 | %\begin{frame}{Explicit typing: Unicity} 398 | % 399 | %\begin{proposition}\label{prop:unicity} 400 | % For every typed term $M$, context~$\Gamma$, and types $\sigma_i$, 401 | % \[ 402 | % \Gamma \vdash_e M : \sigma_1 403 | % \quad\text{and}\quad 404 | % \Gamma \vdash_e M : \sigma_2 405 | % \implies 406 | % \sigma_1 = \sigma_2 407 | % \] 408 | %\end{proposition} 409 | %\begin{proof}[Proof sketch] 410 | % Use the inversion lemma and the structural induction on $M$. 411 | % 412 | % E.g., suppose that $M$ is of the form 413 | % \[ 414 | % L\;M' 415 | % \] 416 | % By inversion there are $\tau_i$ such that $\Gamma \vdash_e L: \tau_i \to 417 | % \sigma_i$ and $\Gamma \vdash_e M': \tau_i$. By induction hypothesis, $\tau_1 \to 418 | % \sigma_1 = \tau_2 \to \sigma_2$, so $\sigma_1 = \sigma_2$. 419 | %\end{proof} 420 | %\end{frame} 421 | % 422 | % 423 | % 424 | %\begin{frame}{Type erasure} 425 | %An \emph{erasing map} $|-|\colon \term_{V}^{\to} \to \term_{V}$ is defined by 426 | % \begin{align*} 427 | % |x| & = x \\ 428 | % |M\; N| & = |M|\;|N| \\ 429 | % |\lambda x:\sigma.\, M| & = \lambda x.\, |M| 430 | % \end{align*} 431 | % \begin{example} 432 | % \begin{enumerate} 433 | % \item $|\lambda (f: \sigma \to \tau)\,(x: \sigma).\, f\;x| = \lambda f\, x.\, f\;x$ 434 | % \item $|(\lambda (x: \sigma)\,(y: \tau). y)\;z| = (\lambda x\,y.\, y)\; z$ 435 | % \end{enumerate} 436 | % \end{example} 437 | % 438 | %$|-|$ is an translation from $\term_{V}^{\to}$ to $\term_{V}$. 439 | %Does $|-|$ respect the behaviour of $\term_{\term_V^{\to}}$? 440 | %\end{frame} 441 | %\begin{frame}{From typed terms to untyped and back} 442 | %\begin{proposition} 443 | % Let $M$ and $N$ be typed $\lambda$-terms in~$\term_{\lambda_V^{\to}}$. Then, 444 | % \begin{align*} 445 | % \Gamma \vdash_e M : \sigma & \text{ implies } \Gamma \vdash_i |M| : 446 | % \sigma \\ 447 | % M \reduce N & \text{ implies } |M| \reduce |N| 448 | % \end{align*} 449 | %\end{proposition} 450 | % 451 | %\begin{proposition} 452 | % Let $M$ and $N$ be $\lambda$-terms in~$\term_V$. Then, 453 | % \begin{enumerate} 454 | % \item If $\Gamma \vdash_i M : \sigma$, then there is $M' : \term_V^{\to}$ with 455 | % $|M'| = M 456 | % \quad\text{and}\quad 457 | % \Gamma \vdash_e M' : \sigma$ 458 | % \item If $M \reduce N$ and $M = |M'|$ for some $M' : \term_V^{\to}$, 459 | % then there exists $N'$ with $|N'| = N$ and $M' \reduce N'$. 460 | % \end{enumerate} 461 | %\end{proposition} 462 | %\end{frame} 463 | 464 | \begin{frame}{Type inference and checking} 465 | Can we answer the following questions algorithmically? 466 | \begin{description} 467 | \item[Type inference] Given a context $\Gamma$ and a term $t$, is there a type $?$ 468 | such that the typing judgement $\Gamma \vdash t : ?$ is derivable? 469 | \item[Type checking] Given a context $\Gamma$, a type $A$, and a term $t$, is the typing judgement $\Gamma \vdash t : A$ derivable? 470 | \end{description} 471 | 472 | Typability is reducible to type checking problem of 473 | \[ 474 | x_0: A \vdash \mathtt{fst} \;x_0\;t : A 475 | \] 476 | 477 | \begin{theorem} 478 | Type checking is \emph{decidable} in simply typed $\lambda$-calculus. 479 | \end{theorem} 480 | 481 | \end{frame} 482 | 483 | \section{Programming in Simply Typed \texorpdfstring{$\lambda$}{λ}-Calculus} 484 | 485 | \begin{frame}[allowframebreaks]{Church encodings of natural numbers} 486 | The type of natural numbers is of the form 487 | \[ 488 | \nat_A \defeq (A \to A) \to A \to A 489 | \] 490 | for every type $A$. 491 | 492 | \begin{description} 493 | \item[Church numerals] 494 | \begin{align*} 495 | & \bc_n \defeq \lambda f\,x.\, 496 | f^n x \\ 497 | \vdash{} & \bc_n : \nat_A 498 | \end{align*} 499 | \item[Successor] 500 | \begin{align*} 501 | & \suc \defeq \lambda n \,f\,x\,.\;f\;(n\;f\;x) \\ 502 | \vdash{} & \suc : \nat_A \to \nat_A 503 | \end{align*} 504 | \item[Addition] 505 | \begin{align*} 506 | & \add \defeq \lambda n\,m\,f\,x.\; (m\;f)\;(n\;f\;x) \\ 507 | \vdash{} & \add : \nat_A \to \nat_A \to \nat_A 508 | \end{align*} 509 | \item[Muliplication] 510 | \begin{align*} 511 | & \mul \defeq \lambda n\,m\,f\,x.\, (m\;(n\;f))\;x\\ 512 | \vdash{} & \mul : \nat_A \to \nat_A \to \nat_A 513 | \end{align*} 514 | \item[Conditional] 515 | \begin{align*} 516 | & \ifz \defeq \lambda n\,x\,y.\, n\;(\lambda z.\, x)\;y\\ 517 | \vdash {} & \ifz : ? 518 | \end{align*} 519 | \end{description} 520 | 521 | \end{frame} 522 | \begin{frame}{Church encodings of boolean values} 523 | We can also define the type of Boolean values 524 | for each type variable as 525 | \[ 526 | \bool_A \defeq A \to A \to A 527 | \] 528 | \begin{description} 529 | \item[Boolean values] 530 | \[ 531 | \true \defeq \lambda x\,y.\,x 532 | \quad\text{and}\quad 533 | \false \defeq \lambda x\,y.\,y 534 | \] 535 | \item[Conditional] 536 | \begin{align*} 537 | & \cond \defeq \lambda b\,x\,y.\, b\,x\,y\\ 538 | \vdash {} & \cond : \bool_{A} \to A \to A \to A 539 | \end{align*} 540 | \end{description} 541 | \end{frame} 542 | 543 | \begin{frame}{Exercise} 544 | \begin{enumerate} 545 | \item Define conjunction $\mathtt{and}$, disjunction $\mathtt{or}$, and 546 | negation $\mathtt{not}$ in simply typed $\lambda$-calculus. 547 | 548 | \item Prove that $\mathtt{and}$, $\mathtt{or}$, and $\mathtt{not}$ are well-typed. 549 | \end{enumerate} 550 | 551 | \end{frame} 552 | 553 | 554 | \section{Properties of Simply Typed \texorpdfstring{$\lambda$}{λ}-Calculus} 555 | 556 | 557 | \begin{frame}[c]{Type safety = Preservation + Progress} 558 | 559 | \begin{quote} 560 | ``Well-typed programs cannot `go wrong'.''\\ 561 | \hfill ---(Milner, 1978) 562 | \end{quote} 563 | 564 | \begin{description} 565 | \item[Preservation] If $\Gamma \vdash t : A$ is derivable and $t \onereduce u$, then $\Gamma \vdash u : A$. 566 | \item[Progress] If $\Gamma \vdash t : A$ is derivable, then either $t$ is in \emph{normal form} or there is $u$ with $t \onereduce u$. 567 | \end{description} 568 | \vfill 569 | By combing the above two properties, we can extend the progress theorem to $\reduce$: if $\Gamma \vdash t : A$ then $t \reduce u$ for some $\Gamma \vdash u : A$ which is either reducible or in normal form. 570 | \end{frame} 571 | 572 | \begin{frame}{Converse of Preservation} 573 | The converse of preservation might not hold. 574 | 575 | \begin{lemma}[Typability of subterms] 576 | Let $t$ be a term with $\Gamma \vdash t : A$ derivable. Then, for every 577 | subterm $t'$ of~$t$ there exists $\Gamma'$ such that 578 | \[ 579 | \Gamma' \vdash t' : A'. 580 | \] 581 | \end{lemma} 582 | %\begin{proof} 583 | % By induction on $\Gamma \vdash t : A$. 584 | %\end{proof} 585 | Recall that 586 | \begin{enumerate} 587 | \item $\mathbf{K}_1 = \lambda x\,y.\, x$ 588 | \item $\Omega = (\lambda x.\, x\,x)\,(\lambda x.\, x\,x)$ 589 | \end{enumerate} 590 | and $\mathbf{K}_1\;(\lam{x}x)\;\Omega \reduce \mathbf{I}$. 591 | 592 | $\Omega$ is not typable, so $\mathbf{K}_1\,\mathbf{I}\,\Omega$ is not typable. 593 | \end{frame} 594 | 595 | \begin{frame}{Preservation Theorem} 596 | \begin{description} 597 | \item[Weakening] If $\Gamma \vdash t : A$ and $x \not\in \Gamma$, then 598 | $\Gamma, x : B \vdash t : A$. 599 | \item[Substitution] If $\Gamma, x : A \vdash t : B$ and $\Gamma \vdash u : A$ then $\Gamma \vdash t\subst{u}{x} : B$. 600 | \end{description} 601 | 602 | \begin{corollary}[Variable renaming] 603 | If $\Gamma, x : A \vdash t : B$ and $y \not\in \mathrm{dom}(\Gamma)$, then $ \Gamma, y : A \vdash t\subst{y}{x} : B$ 604 | where $\mathrm{dom}(\Gamma)$ 605 | denotes the set of variables which occur in $\Gamma$. 606 | \end{corollary} 607 | 608 | \begin{theorem} 609 | For any $t$ and $u$ if $\Gamma \vdash t : A$ is derivable and $t \onereduce u$, then $\Gamma \vdash u : A$. 610 | \end{theorem} 611 | \begin{proof}[Proof sketch] 612 | By induction on both the derivation of $\Gamma \vdash t : A$ and $t \onereduce u$. 613 | \end{proof} 614 | N.B. The only non-trivial case is $\Gamma \vdash (\lam{x}t)\; u : B$ which needs the above results. 615 | % \begin{proof} 616 | % $y$ is not in $\Gamma$, so 617 | % $\Gamma, y : A, x : A \vdash M$ 618 | % by weakening and by definition $\Gamma, y : A \vdash y : A$. 619 | % Thus, by substitution, we have 620 | % \[ 621 | % \Gamma, y : A \vdash M\subst{x}{y} : \sigma 622 | % \] 623 | % \end{proof} 624 | \end{frame} 625 | 626 | 627 | \begin{frame}{Proof of Preservation Theorem} 628 | \begin{proof} 629 | By induction on both the derivation of $\Gamma \vdash t : A$ and $t \onereduce u$. 630 | 631 | \begin{enumerate} 632 | \item Suppose $\Gamma \vdash x : A$. 633 | However, $x \not\onereduce u$ for any $u$. 634 | Therefore, it is vacuously true that $\Gamma \vdash u : A$. 635 | \item Suppose $\Gamma \vdash \lambda x.\, t : A \to B$ and $\lambda x.\, t \onereduce u$. 636 | Then, $u$ must be $\lambda x.\, u'$ for some $u'$; $\Gamma, x : A \vdash t \colon B$ and $t \onereduce u'$ must be derivable. 637 | By induction hypothesis, $\Gamma, x : A \vdash u'$ is derivable, so is $\Gamma \vdash \lambda x.\, u'\colon A \to B$. 638 | \item Suppose $\Gamma \vdash t\; u$. 639 | Then ... 640 | \item ... 641 | \end{enumerate} 642 | \end{proof} 643 | \end{frame} 644 | 645 | \begin{frame}{Progress: First attempt} 646 | \begin{theorem} 647 | If $\Gamma \vdash t : A$ is derivable, then $t$ is in normal form or there is $u$ with $t \onereduce u$. 648 | \end{theorem} 649 | To prove the theorem, we would like to use induction on $\Gamma \vdash t : A$ again. 650 | 651 | However, the fact that $t$ is in normal form does not tell us much what $t$ is. 652 | Can we characterise $t$ syntactically? 653 | \end{frame} 654 | 655 | \begin{frame}{Normal form} 656 | \begin{definition} 657 | Define judgements $\texttt{Neutral}\;t$ and $\texttt{Normal}\;u$ mutually by 658 | \begin{multicols}{2} 659 | \begin{prooftree} 660 | \AXC{$\vphantom{\Gamma}$} 661 | \UIC{$\texttt{Neutral}\;x$} 662 | \end{prooftree} 663 | \begin{prooftree} 664 | \AXC{$\neutral\;t$} 665 | \AXC{$\normal\;u$} 666 | \BIC{$\neutral\;t\;u$} 667 | \end{prooftree} 668 | \columnbreak 669 | \begin{prooftree} 670 | \AXC{$\neutral\;t$} 671 | \UIC{$\normal\;t$} 672 | \end{prooftree} 673 | \begin{prooftree} 674 | \AXC{$\normal\;u$} 675 | \UIC{$\normal\;\lambda x.\,u$} 676 | \end{prooftree} 677 | \end{multicols} 678 | \end{definition} 679 | \textbf{Idea.} $\neutral\;u$ and $\normal\;t$ are derivable iff 680 | \[ 681 | t \equiv x\;u_1 \cdots u_n 682 | \quad\text{and}\quad 683 | u \equiv \lam{x_1 \cdots x_n}x\;u_1 \cdots u_m 684 | \] 685 | where $\beta$-redex cannot exist in $u$ if $u$ is normal. 686 | \end{frame} 687 | 688 | \begin{frame}{Soundness and completeness of the inductive characterisation} 689 | A term $t$ has no $\beta$-reduction if and only if $t$ is normal: 690 | \begin{lemma} 691 | \begin{description} 692 | \item[Soundness] If $\normal\;t$ (resp.\ $\neutral\;t$) is derivable, then $t$ is in normal form. 693 | \item[Completeness] 694 | If $t$ is in normal form, then $\normal\;t$ is derivable. 695 | \end{description} 696 | \end{lemma} 697 | \begin{proof}[Proof sketch] 698 | \begin{description} 699 | \item[Soundness] By mutual induction on the derivation of 700 | $\normal\;t$ and $\neutral\;t$. 701 | \item[Completeness] By induction on the formation of $t$. 702 | \end{description} 703 | \end{proof} 704 | 705 | 706 | \end{frame} 707 | 708 | \begin{frame}{Progress} 709 | \begin{theorem} 710 | If $\Gamma \vdash t : A$ is derivable, then $\normal\;t$ or there is $u$ with $t \onereduce u$. 711 | \end{theorem} 712 | \begin{proof}[Proof sketch] 713 | By induction on the derivation of $\Gamma \vdash t : A$. 714 | \end{proof} 715 | 716 | The statement is trivial in classical logic, as a direct consequence of the Law of Excluded Middle. 717 | 718 | Yet, the progress theorem can be proved constructively without LEM. 719 | What is the \emph{computational meaning} of this theorem? 720 | \end{frame} 721 | 722 | 723 | \begin{frame}{Weak normalisation} 724 | \begin{definition} 725 | $t$ is \emph{weakly normalising} denoted by $t\downarrow$ if 726 | \begin{multicols}{2} 727 | \begin{prooftree} 728 | \AXC{$\normal\; t$} 729 | \UIC{$t \downarrow$} 730 | \end{prooftree} 731 | \columnbreak 732 | \begin{prooftree} 733 | \AXC{$t \onereduce u$} 734 | \AXC{$u \downarrow$} 735 | \BIC{$t \downarrow$} 736 | \end{prooftree} 737 | \end{multicols} 738 | \end{definition} 739 | That is, $t$ is weakly normalising if there is a sequence 740 | \[ 741 | t \onereduce t_1 \onereduce t_2 \onereduce \dots u \notonereduce 742 | \] 743 | \begin{theorem}[Weak normalisation] 744 | Every term $t$ with $\Gamma \vdash t : A$ is weakly normalising. 745 | \end{theorem} 746 | \end{frame} 747 | 748 | \begin{frame}{Strong normalisation} 749 | \begin{definition} 750 | $t$ is \emph{strongly normalising} denoted by $t \Downarrow$ if 751 | \begin{prooftree} 752 | \AXC{$\forall u.\, (t \onereduce u \implies u \Downarrow)$} 753 | \UIC{$t \Downarrow$} 754 | \end{prooftree} 755 | 756 | \end{definition} 757 | Intuitively, \emph{strong normalisation} says every sequence 758 | \[ 759 | t \onereduce t_1 \onereduce t_2 \cdots 760 | \] 761 | terminates, but the definition builds the sequence backwards. 762 | 763 | \begin{theorem} 764 | Every term $t$ with $\Gamma \vdash t : A$ is strongly normalising. 765 | \end{theorem} 766 | \end{frame} 767 | 768 | %\begin{frame}{Typed Conversion} 769 | % 770 | % 771 | %\end{frame} 772 | 773 | %\begin{frame}{Definability} 774 | %%Within simply typed lambda calculus, we wonder how many computable functions can 775 | %%be defined, as it excludes non-terminating $\lambda$-terms. 776 | % A function $f\colon \mathbb{N}^k \to \mathbb{N}$ is called 777 | % \alert{\emph{$\lambda_\to$-definable}} if there is a $\lambda$-term $F$ of 778 | % type $\nat \to \nat \to \dots \nat \to \nat$ such that 779 | % \[ 780 | % F\,\bc_{n_1}\ldots\bc_{n_k} \reduce \bc_{f(n_1, \dots, n_k)} 781 | % \] 782 | % for every sequence $(n_1, n_2, \ldots, n_k) \in \mathbb{N}^k$. 783 | % Diagrammatically, 784 | % 785 | %\[ 786 | % \xymatrix{ 787 | % (n_1, n_2, \ldots, n_k) \ar@{|->}[rr] \ar@{|->}[d]_{(\bc_{-})^k} & & f(n_1, 788 | % n_2, \ldots, n_k) \ar@{|->}[d]^{\bc_{-}}\\ 789 | % (\bc_{n_1}, \bc_{n_2}, \ldots, \bc_{n_k}) \ar@{|->}[rr] & & 790 | % F\;\bc_{n_1}\; \bc_{n_2}\; \ldots\;\bc_{n_k} 791 | % = \bc_{f(n_1, n_2, \ldots, n_k)} 792 | % } 793 | %\] 794 | %\end{frame} 795 | %\begin{frame}{The limit of $\lambda_\to$} 796 | %\begin{theorem} 797 | % The $\lambda_\to$-definable functions are the class of functions 798 | % of the form $f\colon \mathbb{N}^k \to \mathbb{N}$ closed under 799 | % compositions 800 | % which contains 801 | % \begin{itemize} 802 | % \item the constant functions, 803 | % \item projections, 804 | % \item additions, 805 | % \item multiplications, 806 | % \item and the conditional 807 | % \[ 808 | % \mathrm{ifz}(n_0, n_1, n_2) = 809 | % \begin{cases} 810 | % n_1 & \text{if } n_0 = 0\\ 811 | % n_2 & \text{otherwise.} 812 | % \end{cases} 813 | % \] 814 | % \end{itemize} 815 | %\end{theorem} 816 | %\end{frame} 817 | \section{Extensions to Simply Typed \texorpdfstring{$\lambda$}{λ}-Calculus} 818 | 819 | 820 | \begin{frame}{General Recursion: Static} 821 | Self-applicative term cannot be typed in simply typed $\lambda$-calculus. 822 | E.g., 823 | \[ 824 | \lambda x.\, x\,x 825 | \] 826 | cannot be typed, since $A \to A$ is not equal to $A$. 827 | Hence, the $Y$-combinator in untyped $\lambda$-calculus cannot be typed. 828 | 829 | A construct is introduced explicitly for general recursion: 830 | 831 | Let $\Lambda_{\fix}(V)$ be the set of terms defined with an additional construct: 832 | \begin{description} 833 | \item[fixpoint] $\fix\,f.\, t$ is a term in $\Lambda_{\fix}(V)$, if $t \in \Lambda_{\fix}(V)$ and $f \in V$ 834 | \end{description} 835 | 836 | An additional typing rule is added to simply typed $\lambda$-calculus: 837 | \begin{prooftree} 838 | \AXC{$\Gamma, f : A \vdash_{i} t : A$} 839 | \UIC{$\Gamma \vdash_{i} \fix\,f.\,t : A$} 840 | \end{prooftree} 841 | \end{frame} 842 | 843 | \begin{frame}{General Recursion: Dynamic} 844 | $\beta$-reduction for the general recursion $\fix$ is extended with the relation 845 | \begin{align*} 846 | \fix\,x.\,t & \onereduce t\subst{\fix\,x.\,t}{x} 847 | \end{align*} 848 | \vfill 849 | A term which never terminates can be defined easily. 850 | \begin{align*} 851 | & \fix\,x.\,x & \onereduce x\subst{\fix\,x.\,x}{x} \\ 852 | \equiv{} & \fix\,x.\,x & \onereduce x\subst{\fix\,x.\,x}{x} \\ 853 | \equiv{} & \fix\,x.\,x & \onereduce x\subst{\fix\,x.\,x}{x} \\ 854 | \equiv{} & \dots 855 | \end{align*} 856 | \vfill 857 | Other notions such as $=_\alpha, \reduce$, and $\FV$ are extended similarly. 858 | \end{frame} 859 | 860 | %\subsection{System T} 861 | \begin{frame}{Natural numbers: Static} 862 | While Church numerals can have multiple types $\nat_A$, for any $A$, we extend the calculus with a single type of natural numbers instead. 863 | 864 | Let $\Lambda_{\fix, \N}(V)$ be the set of terms defined with additional constructs: 865 | \begin{itemize} 866 | \item $\zero$ is a term in $\Lambda_{\fix, \N}(V)$ 867 | \item $\suc(t)$ is a term in $\Lambda_{\fix, \N}(V)$ if $t$ is 868 | \item $\ifz(t; x.\,u; v)$ is a term in $\Lambda_{\fix, \N}(V)$ if $t, u, v \in \Lambda_{\fix, \N}(V)$ and $x \in V$ 869 | \end{itemize} 870 | with additional typing rules 871 | \begin{mathpar} 872 | \inferrule{ }{\Gamma \vdash \zero : \mathbb{N}} \and 873 | \inferrule{\Gamma \vdash t : \mathbb{N}}{\Gamma \vdash \suc(t) : \mathbb{N}} \and 874 | \inferrule{\Gamma \vdash v : \mathbb{N} \\ \Gamma \vdash t : A \\ \Gamma, x : \mathbb{N} \vdash u : A}{\Gamma \vdash \ifz(t; x.\,u; v) : A} 875 | \end{mathpar} 876 | % \begin{multicols}{3} 877 | % \begin{prooftree} 878 | % \AXC{$\vphantom{\Gamma}$} 879 | % \UIC{$\Gamma \vdash \zero : \mathbb{N}$} 880 | % \end{prooftree} 881 | % \begin{prooftree} 882 | % \AXC{$\Gamma \vdash t : \mathbb{N}$} 883 | % \UIC{$\Gamma \vdash \suc(t) : \mathbb{N}$} 884 | % \end{prooftree} 885 | % \begin{prooftree} 886 | % \AXC{$\Gamma \vdash v : \mathbb{N}$} 887 | % \AXC{$\Gamma \vdash t : A$} 888 | % \AXC{$\Gamma, x : \mathbb{N} \vdash u : A$} 889 | % \TrinaryInfC{$\Gamma \vdash \ifz(t; x.\,u; v) : A$} 890 | % \end{prooftree} 891 | % \end{multicols} 892 | 893 | The third rule is akin to pattern matching on natural numbers. 894 | \end{frame} 895 | 896 | \begin{frame}{Natural numbers: Dynamic} 897 | $\beta$-reduction for natural numbers is extended with two rules: 898 | \begin{align*} 899 | \ifz(t; x.\, u;\zero) & \onereduce t \\ 900 | \ifz(t; x.\, u;\suc(n)) & \onereduce u\subst{n}{x} 901 | \end{align*} 902 | \end{frame} 903 | 904 | \begin{frame}{Natural numbers: Exercise} 905 | Define the predecessor of natural numbers as a program 906 | \[ 907 | \pred : \mathbb{N} \to \mathbb{N}. 908 | \] 909 | 910 | Evaluate the following terms to their normal forms. 911 | \begin{enumerate} 912 | \item $\pred\;\zero$ 913 | \item $\pred\;(\suc\;(\suc\;(\suc\;\zero)))$ 914 | \end{enumerate} 915 | \end{frame} 916 | 917 | \begin{frame}{Boolean values: Exercise} 918 | Extend simply typed $\lambda$-calculus $\Lambda_{\fix, \N}(V)$ further with a type of Boolean values. 919 | \begin{enumerate} 920 | \item What term constructs are needed? 921 | \item What typing rules should be added? 922 | \item How $\beta$-reduction should be updated? 923 | \item Define Boolean operations, i.e. conjunction, disjunction, and negation, in this extension. 924 | \end{enumerate} 925 | 926 | \end{frame} 927 | %include numerals in the generation of syntax: 928 | % \begin{multicols}{2} 929 | % \begin{prooftree} 930 | % \AXC{$x \in V$} 931 | % \UIC{$x \in \Lambda_{\T, \nat}$} 932 | % \end{prooftree} 933 | % \begin{prooftree} 934 | % \AXC{\phantom{$n \in \mathbb{N}$}} 935 | % \UIC{$\zero\in \Lambda_{\T, \nat}$} 936 | % \end{prooftree} 937 | % \begin{prooftree} 938 | % \AXC{$M \in \Lambda_{\T, \nat}$} 939 | % \UIC{$\suc\;M\in \Lambda_{\T, \nat}$} 940 | % \end{prooftree} 941 | % \begin{prooftree} 942 | % \AXC{$M \in \Lambda_{\T,\nat}$} 943 | % \AXC{$N \in \Lambda_{\T,\nat}$} 944 | % \BIC{$(M\, N) \in \Lambda_{\T, \nat}$} 945 | % \end{prooftree} 946 | % \begin{prooftree} 947 | % \AXC{$M \in \Lambda_{\T, \nat}$} 948 | % \AXC{$x \in V$} 949 | % \AXC{$\tau \in \mathbb{T}$} 950 | % \TIC{$\lambda (x:\tau).\; M \in \Lambda_{\T, \nat}$} 951 | % \end{prooftree} 952 | % \end{multicols} 953 | %\noindent and let $\mathbb{G} \defeq \{\nat\}$ with an addition set of typing 954 | %rules: 955 | %\begin{multicols}{2} 956 | %\begin{prooftree} 957 | % \AXC{\phantom{$\Gamma$}} 958 | % \UIC{$\Gamma \vdash \zero : \nat$} 959 | %\end{prooftree} 960 | %\begin{prooftree} 961 | % \AXC{$\Gamma \vdash M : \nat$} 962 | % \UIC{$\Gamma \vdash \suc\:M : \nat$} 963 | %\end{prooftree} 964 | %\end{multicols} 965 | %\noindent In this way, we can derive that every $\underline{n} \defeq 966 | %\suc^n\;\zero$ has the type of $\nat$: 967 | %\[ 968 | % \Gamma \vdash \underline{n} : \nat 969 | %\] 970 | % 971 | %However, it still remains to add primitive operations such as addition, 972 | %multiplication, division, and so on with a proper set of reduction rules. As we 973 | %have seen that recursions on natural numbers in untyped lambda calculus can be 974 | %defined with the fixpoint operator. Something similar can be done: 975 | %\begin{definition} 976 | % In addition to typing rules introduced so far, we add the following 977 | % \begin{prooftree} 978 | % \AXC{$\Gamma \vdash M : \nat$} 979 | % \AXC{$\Gamma \vdash M_0 : \sigma$} 980 | % \AXC{$\Gamma \vdash F : \nat \to (\sigma \to \sigma)$} 981 | % \TIC{$\Gamma \vdash \fold\;M_0\;F : \nat \to \sigma $} 982 | % \end{prooftree} 983 | % with additional reductions 984 | % \[ 985 | % \fold\;M_0\;F\;\zero 986 | % \xrightarrow{\beta} 987 | % M_0 988 | % \quad\text{and}\quad 989 | % \fold\;M_0\;F\;(\suc\;M) 990 | % \xrightarrow{\beta} 991 | % F\;M\;(\fold\;F\;M). 992 | % \] 993 | %\end{definition} 994 | %\begin{definition} 995 | % \todo[inline]{define addition, multiplication using $\fold$} 996 | %\end{definition} 997 | %\begin{frame}{Remark} 998 | %With the decidability of type checking, Preservation Theorem, Progress Theorem 999 | %(a well-typed term is either a ``value'' or a reducible term), and the strong 1000 | %normalisation, we actually have exhibited a decidable evaluator of simply typed 1001 | %lambda calculus that always reduce a well-typed term of type~$\sigma$ to a value 1002 | %of type~$\sigma$. 1003 | %\end{frame} 1004 | 1005 | \begin{frame}{Homework} 1006 | \begin{enumerate} 1007 | \item (5\%) Show the Progress Theorem. 1008 | \item (2.5\%) Show that if $t$ is in normal form then $\normal\;t$ is derivable. 1009 | \item (2.5\%) Extend $\Lambda_{\fix, \N}(V)$ further with product types $A \times B$, for any $A$ and $B$ where additional constructs should include pairs $(t, u)$ and a construct to pattern match on a pair. 1010 | 1011 | \end{enumerate} 1012 | 1013 | \end{frame} 1014 | 1015 | %\begin{frame}{References} 1016 | % 1017 | %\bibliographystyle{amsalpha} 1018 | %\bibliography{library} 1019 | % 1020 | %\end{frame} 1021 | 1022 | \appendix 1023 | 1024 | \end{document} 1025 | -------------------------------------------------------------------------------- /tex/lecture1.tex: -------------------------------------------------------------------------------- 1 | %! TeX program = xelatex 2 | %! TEX root = lecture1_slide.tex 3 | 4 | \title{\texorpdfstring{$\lambda$}{λ}-Calculus} 5 | \subtitle{Untyped \texorpdfstring{$\lambda$}{λ}-Calculus} 6 | \begin{document} 7 | 8 | \bgroup 9 | \usebackgroundtemplate{\includegraphics[width=\paperwidth]{banner.pdf}} 10 | \begin{frame}[plain,noframenumbering] 11 | \maketitle 12 | \end{frame} 13 | \egroup 14 | 15 | \begin{frame}[fragile]{評分標準} 16 | \begin{description} 17 | \item[期限] 七月十號 下午 4 點 20 18 | \item[分數比例] 19 | \begin{description} 20 | \item[作業] (22.5\%) 21 | \item[考試] (100\%) 22 | \end{description} 23 | \item[Email] \url{liang.ting.chen.tw@gmail.com} 24 | \end{description} 25 | 26 | \textbf{作業繳交請遵循以下規定} 27 | 28 | \begin{enumerate} 29 | \item 請用 A4 紙(其餘規格尺寸不收),寫上姓名與學號 30 | \item 請清楚標示作業次、題號並且力求字跡清楚 31 | \item 作業可現場親交給我,或用 PDF 檔案(如翻拍請用掃描軟體後製)後寄到我 email 信箱,格式如下: 32 | \begin{description} 33 | \item[主旨] \texttt{[FLOLAC] Lambda HW\%x\%} 34 | \item[附件] \texttt{Lambda-HW\{1, 2, 3\} - 學號 - 姓名.pdf} 35 | \item[內文] (可空白) 36 | \end{description} 37 | \end{enumerate} 38 | \end{frame} 39 | 40 | \section{Untyped \texorpdfstring{$\lambda$}{λ}-Calculus: Introduction} 41 | \begin{frame}[fragile]{The idea of anonymous functions} 42 | Anonymous functions can be defined in many languages, e.g., 43 | \begin{description} 44 | \item[\Hask] 45 | \verb|\x f -> f x| 46 | \item[\OCaml] 47 | \verb|fun x f -> f x| 48 | \end{description} 49 | 50 | This type of expression is inspired by the \alert{$\lambda$-notation} introduced by Alan Turing's supervisor, Alonzo Church, who was seeking a foundation for mathematics. 51 | 52 | \vfill 53 | 54 | In $\lambda$-notation 55 | \[ 56 | \lam{x}e 57 | \] 58 | means `a function that maps the argument $x$ to expression $e$' where $x$ may appear in $e$. 59 | E.g., the above examples can be expressed as 60 | \[ 61 | \lam{x\,f}f\;x 62 | \] 63 | \end{frame} 64 | 65 | \begin{frame}{An example of $\lambda$-notation} 66 | The idea of function application in $\lambda$-notation is straightforward. 67 | 68 | For example, in high school we may say a function $f(x) \defeq x^2$ with the variable $x$ and write 69 | \[ 70 | f(3) = 3^2 = 9 71 | \] 72 | 73 | In $\lambda$-notation, we write 74 | \[ 75 | (\lambda x.\, x^2)\;3 = x^2 \subst{3}{x} = 3^2 = 9 76 | \] 77 | where $x^2\subst{3}{x}$ means `the \alert{substitution} of $3$ for $x$ in the expression $x^2$'. 78 | \end{frame} 79 | 80 | \begin{frame}{What is $\lambda$-calculus} 81 | $\lambda$-calculus is a \emph{language of functions in $\lambda$-notation} consisting of three constructs: 82 | \begin{description} 83 | \item[abstraction] 84 | functions can be introduced $\lam{x} t$ 85 | 86 | \item[application] 87 | functions can be applied to an argument $t\;u$ 88 | 89 | \item[variable] 90 | variables are terms 91 | \end{description} 92 | where a \emph{term} means a minimal unit of expression. 93 | 94 | That is, every term in $\lambda$-calculus is in one and only one of the above forms. 95 | 96 | $\lambda$-calculus can be understood as a programming language \emph{without} any built-in data types and suffices to define every computable function. 97 | \end{frame} 98 | 99 | \begin{frame}{Why should we study $\lambda$-calculus?} 100 | $\lambda$-calculus itself is a fruitful subject but it is also useful: 101 | \begin{itemize} 102 | \item it serves as a prototype of programming languages which can be reasoned about \alert{mathematically} and \alert{rigorously}; 103 | \item the methodology we develop to understand $\lambda$-calculus can be used to study and design other programming languages. 104 | \end{itemize} 105 | \vfill 106 | The common practice in PL research is to start with a variant of typed $\lambda$-calculus and a \emph{language feature} in question and investigate properties of this prototype language. 107 | 108 | \vfill 109 | Moreover, $\lambda$-calculus has a strong connection with \emph{logic} and \emph{mathematics} which is a topic for another day. 110 | \end{frame} 111 | 112 | \begin{frame}{What to expect next?} 113 | For $\lambda$-calculus, we will consider following topics in programming language in a style of mathematical formalism. 114 | \begin{enumerate} 115 | \item How programs can be identified \emph{up to variable renaming}? 116 | E.g., $\lambda x.\, x$ should be `equal' to $\lambda y.\, y$. 117 | \item How do programs \emph{compute}? 118 | E.g., the application $(\lambda x.\, x)\;3$ of the identity to $3$ should compute to $3$. 119 | \item How programs can be identified \emph{computationally}? 120 | E.g., 121 | \[ 122 | (\lambda x.\, x)\;3 \quad\text{and}\quad (\lambda y.\, 3)\;10 123 | \] 124 | should be `computationally equal' as they should compute to the same term (but not each other). 125 | \item How to write programs in $\lambda$-calculus? 126 | \end{enumerate} 127 | \end{frame} 128 | 129 | \section{Untyped \texorpdfstring{$\lambda$}{λ}-Calculus: Statics} 130 | 131 | \begin{frame}{Syntax of $\lambda$-calculus} 132 | To define the language of $\lambda$-calculus, we need a primitive notion of \emph{variables} first. 133 | Let us fix a \emph{countably infinite} set $V$ for variables. 134 | 135 | The set $\Lambda(V)$ of $\lambda$-terms over $V$ is defined \emph{inductively} as 136 | \begin{description} 137 | \item[variable] $x \in \Lambda(V)$ if $x$ is in $V$ 138 | \item[application] $t@u \in \Lambda(V)$ if $t, u \in \Lambda(V)$ 139 | \item[abstraction] $\lam{x}t$ if $x \in V$ and $t \in \Lambda(V)$ 140 | \end{description} 141 | 142 | \vfill 143 | Each construct can be represented as a node in a tree, i.e.\ 144 | \begin{center} 145 | \begin{minipage}[t]{.32\textwidth} 146 | \centering 147 | \begin{tikzpicture}[ 148 | level distance=0.75cm, 149 | level 1/.style={sibling distance=1cm}, 150 | ] 151 | 152 | \node {$x$}; 153 | \end{tikzpicture} 154 | \end{minipage} 155 | \begin{minipage}[t]{.32\textwidth} 156 | \centering 157 | \begin{tikzpicture}[ 158 | level distance=0.75cm, 159 | level 1/.style={sibling distance=1cm}, 160 | ] 161 | 162 | \node {$@$} 163 | child { node {$t$} } 164 | child { node {$u$} }; 165 | \end{tikzpicture} 166 | \end{minipage} 167 | \begin{minipage}[t]{.32\textwidth} 168 | \centering 169 | \begin{tikzpicture}[ 170 | level distance=0.75cm, 171 | level 1/.style={sibling distance=2cm}, 172 | ] 173 | 174 | \node {$\lambda x$} 175 | child { node {$t$} }; 176 | \end{tikzpicture} 177 | \end{minipage} 178 | \end{center} 179 | for a variable $x$, an application $t@u$, and an abstraction $\lam{x}t$. 180 | \end{frame} 181 | 182 | 183 | \begin{frame}{Representing a term as an abstract syntax tree} 184 | The expression $\lam{x}(\lam{y}((x@y)@z))$ can be represented as 185 | \begin{center} 186 | \begin{tikzpicture}[ 187 | level distance=1cm, 188 | level 1/.style={sibling distance=8cm}, 189 | level 2/.style={sibling distance=5cm}, 190 | level 3/.style={sibling distance=4cm} 191 | ] 192 | 193 | \node {$\lambda x$} 194 | child {node {$\lambda y$} 195 | child {node {$@$} 196 | child {node {$@$} 197 | child {node {$x$}} 198 | child {node {$y$}} 199 | } 200 | child {node {$z$}} 201 | } 202 | }; 203 | \end{tikzpicture} 204 | \end{center} 205 | \begin{alertblock}{Important} 206 | Brackets `(' and `)' are not part of a term but are used for grouping a subterm. 207 | \end{alertblock} 208 | \end{frame} 209 | 210 | \begin{frame}{Formal justification} 211 | The validity of the expression can be justified by its very definition: 212 | \[ 213 | \lam{x}(\lam{y}((x@y)@z)) 214 | \] 215 | \vskip -8pt 216 | \begin{enumerate} 217 | \item $x$, $y$, and $z$ are in $V$, so $x$, $y$, $z$ are terms; 218 | \item $x$ and $y$ are terms, so $x@y$ is a term; 219 | \item $(x@y)@z$ is a term since $x@y$ is a term and $z$ is a term; 220 | \item {\small $\lam{y}((x@y)@z)$ is a term since $(x@y)@z$ is a term and $y$ is a variable;} 221 | \item {\footnotesize $\lam{y}((x@y)@z)$ is a term and $x$ is a variable, so $\lam{x}(\lam{y}((x@y)@z))$ is a term.} 222 | \end{enumerate} 223 | 224 | \begin{convention*} 225 | $@$ is omitted if a term is written as a sequence of symbols, so we write 226 | \[ 227 | t\;u 228 | \qquad\text{instead of}\qquad 229 | t@u 230 | \] 231 | \end{convention*} 232 | \end{frame} 233 | 234 | \begin{frame}{The need for some conventions} 235 | For arithmetic expressions, we typically write 236 | \[ 237 | 3 * 4 + 7 *2 \qquad\text{to mean}\qquad (3 * 4) + (7 + 2) 238 | \] 239 | by the precedence convention. 240 | 241 | We'd also like to have some conventions to omit brackets without any ambiguity. 242 | E.g., one should be able to write 243 | \[ 244 | \lam{x y}x\;y\;z \qquad\text{to mean}\qquad \lam{x}(\lam{y}((x\;y)\;z)) 245 | \] 246 | because 247 | \begin{enumerate} 248 | \item multiple abstractions means a function with multiple arguments; 249 | \item applying a function to multiple arguments can be achieved by applying a function to an argument to get another function for the next argument; 250 | \item applications occur more often than abstractions in a body. 251 | \end{enumerate} 252 | \end{frame} 253 | 254 | \begin{frame}{Conventions} 255 | \begin{description} 256 | \item[Consecutive abstractions] 257 | \[ 258 | \lambda x_1\,x_2\,\ldots x_n.\, M \equiv \lambda x_1.\,(\lambda x_2.\,(\ldots (\lambda x_n.\, M)\ldots)) 259 | \] 260 | \item[Consecutive applications] 261 | \[ 262 | M_1\;M_2\; M_3\; \dots\;M_n \equiv (\dots((M_1\;M_2)\;M_3) \dots )\; M_n 263 | \] 264 | \item[Function body extends as far right as possible] 265 | $\lam{x}M\;N \quad\text{means}\quad \lambda x.\,(M\; N)$ instead of $(\lambda x.\,M)\; N$. 266 | \end{description} 267 | \begin{enumerate} 268 | \item $(x\;y)\;z \equiv x\;y\;z$ 269 | \item $\lambda s.\,(\lambda z.\, (s \;z)) \equiv \lambda s\,z.\, s\;z$ 270 | \item $\lambda a.\,(\lambda b.\, (a\;(\lambda c.\, a\; b))) \equiv \lambda a\,b.\, a\;(\lambda c.\, a\;b)$ 271 | \item $(\lambda x.\, x)\;(\lambda y.\, y) \equiv (\lambda x.\, x)\;\lambda y.\, y$ 272 | \end{enumerate} 273 | \end{frame} 274 | \begin{frame}{More Example} 275 | 276 | \begin{exercise*} 277 | Draw the corresponding abstract syntax tree for each of the following terms: 278 | \begin{enumerate} 279 | \item $x\;(y\;z)$ 280 | \item $x\;y\;z$ 281 | \item $\lam{sz}s\;z$ 282 | \item $(\lambda x.\, x)\;(\lambda y.\, y)$ 283 | \item $\lam{a b}a\;(\lam{c}a\;b)$ 284 | \end{enumerate} 285 | \end{exercise*} 286 | \end{frame} 287 | 288 | 289 | 290 | \begin{frame}{Variable binding} 291 | Let's discuss an important notion of syntax: \emph{variable binding}. 292 | 293 | In the expression $f({\color{red}{x}}) = {\color{red}x}^2$, the variable $x$ in the expression $x^2$ is \alert{bound} to $x$ of $f$ and the \alert{meaning} of $f(x)$ is the same as $f({\color{red}y}) = {\color{red}y}^2$. 294 | 295 | Similarly, following expressions exhibit the variable binding in various forms: 296 | \begin{enumerate} 297 | \item $\sum_{\alert{x} = 0}^{n} \alert{x}$ 298 | \item $\int_{0}^{1} e^{\alert{y}}\; \mathrm{d}\alert{y}$ 299 | \item $f({\color{red}x}, {\color{blue}y}) = {\color{red}x}^2 + {\color{blue}y}^2$ 300 | \item \dots 301 | \item $\lambda {\color{red}y}.\,(\lambda {\color{blue}x}.\, {\color{red}y})$ means a function that takes an argument $y$ returns a constant function at $y$ 302 | \item $\lambda {\color{red}x}.\,(\lambda {\color{blue}y}.\, {\color{blue}y})$ means a constant function that always returns the identity 303 | \end{enumerate} 304 | \end{frame} 305 | 306 | \begin{frame}{Binding structure in an abstract syntax tree} 307 | The binding structure can be visualised in an abstract syntax tree: 308 | \begin{center} 309 | \begin{tikzpicture}[ 310 | level distance=1.3cm, 311 | level 1/.style={sibling distance=8cm}, 312 | level 2/.style={sibling distance=5cm}, 313 | level 3/.style={sibling distance=4cm} 314 | ] 315 | 316 | \node (root) {$\lambda x$} 317 | child {node (lamy) {$\lambda y$} 318 | child {node {$@$} 319 | child {node {$@$} 320 | child {node (x) {$x$}} 321 | child {node (y) {$y$}} 322 | } 323 | child {node {$z$}} 324 | } 325 | }; 326 | 327 | % Draw the arc from x to root 328 | \draw[red, ->, thick, dashed, bend left=60] (x) to (root); 329 | \draw[red, ->, thick, dashed, bend left=40] (y) to (lamy); 330 | 331 | \end{tikzpicture} 332 | \end{center} 333 | \end{frame} 334 | 335 | \begin{frame}{$\alpha$-equivalence: renaming of bound variables} 336 | It is common sense that renaming variables of a program should not alter its meaning: the point of having a name for a variable to look for where it applies to. 337 | 338 | \vfill 339 | Intuitively, two terms $t$ and $u$ are \alert{$\alpha$-equivalent}, written as 340 | \[ 341 | t =_\alpha u 342 | \] 343 | if $t$ and $u$ have the same binding structure, \emph{regardless of their variable names}, in their abstract syntax trees. 344 | \vfill 345 | \begin{alertblock}{Quest} 346 | How to define $\alpha$-equivalence formally? 347 | \end{alertblock} 348 | \end{frame} 349 | 350 | \begin{frame}{First solution: de Bruijn representation} 351 | \begin{idea} 352 | We discard names completely and use indices $i'$ to represent variable bindings. 353 | \end{idea} 354 | 355 | The index $i'$ points to the $i$-th innermost $\lambda$-node from the variable: 356 | \begin{center} 357 | \begin{minipage}{.4\textwidth} 358 | \begin{tikzpicture}[ 359 | level distance=0.75cm, 360 | level 1/.style={sibling distance=6cm}, 361 | level 2/.style={sibling distance=4cm}, 362 | level 3/.style={sibling distance=2cm} 363 | ] 364 | \node (root) {$\lambda x$} 365 | child {node (lamy) {$\lambda y$} 366 | child {node {$@$} 367 | child {node {$@$} 368 | child {node (x) {$x$}} 369 | child {node (y) {$y$}} 370 | } 371 | child {node {$z$}} 372 | } 373 | }; 374 | 375 | % Draw the arc from x to root 376 | \draw[red, ->, thick, dashed, bend left=60] (x) to (root); 377 | \draw[red, ->, thick, dashed, bend left=40] (y) to (lamy); 378 | \end{tikzpicture} 379 | \end{minipage} 380 | becomes 381 | \begin{minipage}{.4\textwidth} 382 | \begin{tikzpicture}[ 383 | level distance=0.75cm, 384 | level 1/.style={sibling distance=6cm}, 385 | level 2/.style={sibling distance=4cm}, 386 | level 3/.style={sibling distance=2cm} 387 | ] 388 | \node (root) {$\lambda$} 389 | child {node (lamy) {$\lambda$} 390 | child {node {$@$} 391 | child {node {$@$} 392 | child {node (x) {$1'$}} 393 | child {node (y) {$0'$}} 394 | } 395 | child {node {$3'$}} 396 | } 397 | }; 398 | 399 | % Draw the arc from x to root 400 | \draw[red, ->, thick, dashed, bend left=60] (x) to (root); 401 | \draw[red, ->, thick, dashed, bend left=40] (y) to (lamy); 402 | \end{tikzpicture} 403 | \end{minipage} 404 | \end{center} 405 | This representation is invented by a Dutch mathematician, N. G. de Bruijn, while implementing a system for formalising mathematics. 406 | \end{frame} 407 | 408 | \begin{frame}{Pros and cons of De Bruijn representation} 409 | 410 | \begin{description} 411 | \item[Good] This representation does solve many problems: 412 | \begin{enumerate} 413 | \item $\alpha$-equivalence coincides with syntactic equality, i.e. 414 | \[ 415 | t =_\alpha u \iff t = u. 416 | \] 417 | \item Machine-readable. 418 | \item No variable renaming is involved. 419 | \end{enumerate} 420 | \item[Bad] `Don't throw the baby out with the bathwater'. 421 | \end{description} 422 | \end{frame} 423 | 424 | \begin{frame}{Second solution: Capture-avoidance renaming} 425 | \begin{idea} 426 | Using the nominal representation, we define $t =_\alpha u$ if variables $t$ and $u$ can be renamed `suitably' to exactly the same term. 427 | \end{idea} 428 | With naive renaming, a renamed variable might be \alert{captured} by some $\lambda$, breaking the binding structure. 429 | For example, $y$ can be renamed to anything but $x$ in 430 | \[ 431 | \lambda x.\,(\lambda y.\, x \; y) 432 | \] 433 | to preserve the same binding structure. 434 | 435 | Hence, variable renaming has to be constrained to variables that do not \alert{occur} in the term to avoid changing the binding structure. 436 | \begin{alertblock}{Quest} 437 | How to define the \alert{occurrence} of a variable and variable \alert{renaming}? 438 | \end{alertblock} 439 | \end{frame} 440 | 441 | \begin{frame}{Structural recursion over $\lambda$-terms} 442 | To define a function from $\lambda$-terms, we may use the '\alert{fold}': 443 | 444 | \begin{theorem*} 445 | Given a target set $X$ and functions 446 | \begin{align*} 447 | f_1 & \colon V \to X \\ 448 | f_2 & \colon X \times X \to X \\ 449 | f_3 & \colon V \times X \to X 450 | \end{align*} 451 | there exists a unique $\hat{f}\colon \Lambda(V) \to X$ such that 452 | \begin{align*} 453 | \hat{f}\,x & = f_1\,x & \hat{f}(t\;u) & = f_2(\hat{f}\,t, \hat{f}\,u) & \hat{f}(\lambda x.\, t) & = f_3(x, \hat{f}\,t) 454 | \end{align*} 455 | \end{theorem*} 456 | \end{frame} 457 | 458 | %\begin{frame}{Exercise: Height} 459 | % 460 | %The \emph{height} of a term is given informally as follows: 461 | %\begin{enumerate} 462 | % \item the height of a variable is 1; 463 | % \item the height of an application is the maximum of the heights of its subterms plus 1; 464 | % \item the height of an abstraction is the height of its body plus 1. 465 | %\end{enumerate} 466 | % 467 | %\begin{exercise*} 468 | % Define the height function $h\colon \Lambda(V) \to \mathbb{N}$ by structural recursion. 469 | %\end{exercise*} 470 | %\end{frame} 471 | 472 | \begin{frame}{Variable occurrences} 473 | We define the set $\Var(t)$ of variables in a term $t$ by structural recursion with the target set $\mathcal{P}V$ and 474 | \begin{align*} 475 | \Var_1(x) & = \{x\} \\ 476 | \Var_2(S_1, S_2) & = S_1 \cup S_2 \\ 477 | \Var_3(x, S) & = \{x\} \cup S 478 | \end{align*} 479 | That is, $\Var$ is a function from $\Lambda(V)$ to $\mathcal{P}V$ such that 480 | \begin{align*} 481 | \Var(x) & = \{x\} \\ 482 | \Var(t\;u) & = \Var(t) \cup \Var(u) \\ 483 | \Var(\lam{x} t) & = \{x\} \cup \Var(t) 484 | \end{align*} 485 | \vfill 486 | We say $x$ occurs in $t$ if $x \in \Var(t)$, i.e.\ $x$ appear in $t$ somewhere. 487 | \end{frame} 488 | 489 | 490 | \begin{frame}{Variable permutation} 491 | A \emph{transposition} $(x\, y)$ is a function that swaps $x$ and $y$ but fixes everything else, i.e.\ 492 | \[ 493 | (x\, y)\;z = 494 | \begin{cases} 495 | y & \text{$z$ = $x$} \\ 496 | x & \text{$z$ = $y$} \\ 497 | z & \text{otherwise} 498 | \end{cases} 499 | \] 500 | \vfill 501 | The \emph{variable permutation} by a transportation $\pi = (y\,z)$ is defined by 502 | \begin{align*} 503 | \pi\cdot x & = \pi\;x \\ 504 | \pi\cdot(t\;u) & = (\pi\cdot t)\; (\pi\cdot u) \\ 505 | \pi\cdot(\lambda x.\, t) & = \lambda (\pi\;x).\, (\pi\cdot t) 506 | \end{align*} 507 | \vfill 508 | E.g., 509 | \[ 510 | (z\,y) \cdot \lambda x.\,(\lambda y.\, y \; y) = \lambda x.\,(\lambda z.\, z \; z) 511 | \] 512 | \end{frame} 513 | 514 | \begin{frame}{Renaming of bound variables} 515 | Now we are ready to formulate what we mean by $\alpha$-equivalence 516 | 517 | \begin{definition}[$\alpha$-equivalence] 518 | $\alpha$-equivalence is a relation $t =_\alpha u$ between terms $t$ and $u$ defined inductively as 519 | \begin{multicols}{2} 520 | \begin{prooftree} 521 | \AXC{\vphantom{$M_1$}} 522 | \RightLabel{if $x \in V$} 523 | \UIC{$x =_\alpha x$} 524 | \end{prooftree} 525 | \begin{prooftree} 526 | \AXC{$t_1 \mathrel{=_\alpha} t_2$} 527 | \AXC{$u_1 \mathrel{=_\alpha} u_2$} 528 | \BIC{$t_1\;u_1 \mathrel{=_\alpha} t_2\;u_2$} 529 | \end{prooftree} 530 | \end{multicols} 531 | \begin{prooftree} 532 | \AXC{$(z\;x)\cdot t \mathrel{=_\alpha} (z\;y)\cdot u$} 533 | \RightLabel{if $z \not\in \Var(t, u)$} 534 | \UIC{$\lam{x}t \mathrel{=_\alpha} \lam{y}u $} 535 | \end{prooftree} 536 | \end{definition} 537 | The third case is the interesting one: $\lam{x}t$ and $\lam{y}u$ are equal up to renaming bound variables if the variables $x$ and $y$ can be swapped with a variable $z$ that does not exist in $t$ and $u$. 538 | \end{frame} 539 | 540 | \begin{frame}{An example of $\alpha$-equivalent terms} 541 | 542 | \begin{example} 543 | Show that $(\lambda y.\, y)\;z =_\alpha (\lambda x.\,x)\;z$. 544 | \end{example} 545 | 546 | \begin{proof} 547 | By definition 548 | \begin{prooftree} 549 | \AXC{} 550 | \UIC{$(y\;y)\cdot y \mathrel{=_\alpha} (y\; x)\cdot x$} 551 | \UIC{$\lambda y.\, y \mathrel{=_\alpha} \lambda x.\, x$} 552 | \AXC{} 553 | \UIC{$z \mathrel{=_\alpha} z$} 554 | \BIC{$(\lambda y.\, y)\;z \mathrel{=_\alpha} (\lambda x.\, x)\;z$} 555 | \end{prooftree} 556 | where $(y\;y)\cdot y = () \cdot y = y$ and $(y\;x)\cdot x = y$, so it follows that $(\lambda y.\, y)\;z \mathrel{=_\alpha} (\lambda x.\, x)\;z$. 557 | \end{proof} 558 | \end{frame} 559 | 560 | 561 | \begin{frame}{$\alpha$-equivalence is an equivalence} 562 | $\alpha$-equivalence satisfies the following properties 563 | \begin{description} 564 | \item[reflexivity] $t =_\alpha t$ for any term $t$; 565 | \item[symmetry] $u =_\alpha t$ if $t =_\alpha u$; 566 | \item[transitivity] $t =_\alpha v$ if $t =_\alpha u$ and $u =_\alpha v$. 567 | \end{description} 568 | Easy to prove reflexivity and symmetry (try it!) but tricky to prove transitivity. 569 | \vfill 570 | We are mainly in interested in \alert{terms up to $\alpha$-equivalence}, as the name of a bound variable does not matter. 571 | Hence, we consider $\lambda$-terms \emph{modulo} $\alpha$-equivalence, i.e.\ 572 | \[ 573 | [t]_{\alpha} = \{\, u \in \Lambda(V) \mid t =_\alpha u \,\} 574 | \] 575 | as well as the (quotient) set: 576 | \[ 577 | \Lambda(V)/{=_\alpha} \defeq \{\, [t]_{\alpha} \mid t \in \Lambda(V) \,\}. 578 | \] 579 | \end{frame} 580 | 581 | \begin{frame}{Exercise} 582 | Which of the following pairs are $\alpha$-equivalent? If so, prove it. 583 | \begin{enumerate} 584 | \item $x$ and $y$ if $x \neq y$ 585 | \item $\lambda x\,y.\, y$ and $\lambda z\,y.\, y$ 586 | \item $\lambda x\,y.\, x$ and $\lambda y\,x.\, y$ 587 | \item $\lambda x\,y.\, x$ and $\lambda x\,y.\, y$ 588 | \end{enumerate} 589 | \vfill 590 | \begin{block}{Challenge} 591 | Is it true that $\alpha$-equivalent terms have the same de Bruijn representation? 592 | 593 | Can you come up with a strategy to prove your conjecture? 594 | \end{block} 595 | 596 | \end{frame} 597 | 598 | 599 | 600 | \section{Untyped \texorpdfstring{$\lambda$}{λ}-Calculus: Dynamics} 601 | 602 | \begin{frame}{Evaluation, informally} 603 | The \alert{evaluation} of $\lambda$-calculus is of this form 604 | \[ 605 | \fbox{$\cdots\underbrace{(\lam{x}t)\,u}_{\beta\text{-redex}} \cdots$} \longrightarrow_{\beta 1} 606 | \fbox{$\cdots \underbrace{t\;\subst{u}{x}}_{\text{substitution of $N$ for $x$ in $M$}} \cdots$} 607 | \] 608 | 609 | \mode{\vfill} 610 | In $\lambda$-calculus, defining substitution is subtle: 611 | 612 | \begin{quote} 613 | Variable $x$ in $u$ may be captured by an abstraction $\lam{x}t$, if the substitution $\subst{u}{x}(\lam{x}t)$ is naively carried out. 614 | \end{quote} 615 | How to evaluate the following terms? 616 | Remember that we shall not discriminate $\alpha$-variants. 617 | \begin{enumerate} 618 | \item $(\lambda x.x)\,z$ 619 | \item $(\lambda x\, y.\,y)\,x$ 620 | \item $(\lambda x\, y.\,y)\,(x\;y)$ 621 | \end{enumerate} 622 | \end{frame} 623 | 624 | \begin{frame}{Free variables} 625 | A notion of the \emph{scope} of a variable is needed to know which variable is available in scope to be substituted. 626 | 627 | We use the notion of \emph{free variable}: a variable $y$ is \alert {free} if $y \in \FV(t)$ where $\FV(t)$ is defined by 628 | \begin{align*} 629 | \FV(x) & = \{x\} \\ 630 | \FV(t\;u) & = \FV(t) \cup \FV(u) \\ 631 | \FV(\lambda x.\, t) & = \FV(t) - \{x\} 632 | \end{align*} 633 | 634 | A variable $y$ is \alert{bound} in $t$ if it occurs in $t$ but is not free. 635 | \vfill 636 | \begin{proposition} 637 | $\FV$ respects $\alpha$-equivalence, i.e.\ if $t =_\alpha u$, then $\FV(t) = \FV(u)$. 638 | \end{proposition} 639 | \end{frame} 640 | 641 | \begin{frame}{Free variables: Exercise} 642 | Compute the set $\FV(t)$ of free variables for each subtree $t$ of the following abstract syntax tree: 643 | \vfill 644 | \begin{center} 645 | \begin{tikzpicture}[ 646 | level distance=1.2cm, 647 | level 1/.style={sibling distance=6cm}, 648 | level 2/.style={sibling distance=6cm}, 649 | level 3/.style={sibling distance=4cm} 650 | ] 651 | \node (root) {$\lambda x$} 652 | child {node (lamy) {$\lambda y$} 653 | child {node (app1) {$@$} 654 | child {node (app2) {$@$} 655 | child {node (x) {$x$}} 656 | child {node (y) {$y$}} 657 | } 658 | child {node (z) {$z$}} 659 | } 660 | }; 661 | 662 | % Boxes around each subterm 663 | \node[draw=red, dashed, fit=(root) (lamy) (app1) (app2) (x) (y) (z) ] {}; 664 | \node[draw=red, dashed, fit=(lamy) (app1) (app2) (x) (y) (z) ] {}; 665 | \node[draw=red, dashed, fit=(app1) (app2) (x) (y) (z) ] {}; 666 | \node[draw=red, dashed, fit=(z) ] {}; 667 | \node[draw=red, dashed, fit=(app2) (x) (y) ] {}; 668 | \node[draw=red, dashed, fit=(x)] {}; 669 | \node[draw=red, dashed, fit=(y)] {}; 670 | \end{tikzpicture} 671 | \end{center} 672 | \end{frame} 673 | 674 | \begin{frame}{Capture-avoiding substitution} 675 | Given a term $t$ and a variable $x$, the \alert{capture-avoiding substitution} 676 | \[ 677 | \_\subst{t}{x} \colon \Lambda \to \Lambda 678 | \] 679 | of $t$ for $x$ is defined on terms by 680 | \begin{align*} 681 | y\subst{t}{x} & = 682 | \begin{cases*} 683 | t & \text{if $x = y$} \\ 684 | y & \text{otherwise} 685 | \end{cases*} \\ 686 | (t_1\;t_2) \subst{t}{x} & = (t_1\subst{t}{x})\;(t_2\subst{t}{x}) \\ 687 | (\lam{y}u) \subst{t}{x} & = 688 | \begin{cases*} 689 | \lam{y}(u\subst{t}{x}) & \text{if $x \neq y$ and $y \notin \FV(t)$} \\ 690 | ? & \text{otherwise} 691 | \end{cases*} 692 | \end{align*} 693 | If the clause $?$ is reached, then \emph{rename} the bound variable $y$ to some variable \alert{fresh} for $x$ and $t$, i.e.\ some $z$ such that $z \neq y$ and $z \notin \FV(t)$, before proceeding. 694 | 695 | \end{frame} 696 | 697 | %\begin{frame}{$\alpha$-Structural recursion} 698 | %\end{frame} 699 | % 700 | %\begin{frame}{Substitution} 701 | %\end{frame} 702 | 703 | 704 | %\begin{frame}{$\eta$-conversion} 705 | % Pointy style is \emph{assumed} to be equivalent to point-free style. 706 | % \begin{description} 707 | % \item[$\eta$-reduction] 708 | % $\lambda x.\,(M\,x) \longrightarrow_\eta M$ 709 | % \item[$\eta$-expansion] 710 | % $M \longrightarrow_\eta \lambda x.\,(M\,x) $ 711 | % where $x$ is fresh for $M$. 712 | % \end{description} 713 | % \mode{\vfill} 714 | % $\eta$-equivalence is a form of \emph{extensionality} limited to 715 | % $\lambda$-terms. 716 | % \[ 717 | % f = g \iff \forall x.\, f(x) = g(x) 718 | % \] 719 | %\end{frame} 720 | 721 | \begin{frame}{Single-step $\beta$-reduction} 722 | 723 | A \alert{$\beta$-redex} is a term of the form $(\lam{x}t)\;u$ where computation can be performed upon and the application can be reduced to $t\subst{u}{x}$. 724 | 725 | \begin{definition} 726 | The \emph{one-step (full) $\beta$-reduction} is a relation between terms defined inductively by 727 | following rules: 728 | \begin{multicols}{2} 729 | \begin{prooftree} 730 | \AXC{\vphantom{$M\subst{N}{x}$ is defined}} 731 | \UIC{$(\lam{x}t)\;u \onereduce t\subst{u}{x}$} 732 | \end{prooftree} 733 | \begin{prooftree} 734 | \AXC{$t_1 \onereduce t_2$} 735 | \UIC{$\lam{x} t_1 \onereduce \lam{x} t_2$} 736 | \end{prooftree} 737 | \begin{prooftree} 738 | \AXC{$t_1 \onereduce t_2$} 739 | \UIC{$t_1\;u \onereduce t_2\;u$} 740 | \end{prooftree} 741 | \begin{prooftree} 742 | \AXC{$u_1\onereduce u_2$} 743 | \UIC{$t\;u_1 \onereduce t\;u_2$} 744 | \end{prooftree} 745 | \end{multicols} 746 | \end{definition} 747 | For example, 748 | $(\boxed{(\lam{x y}x)\; t})\;u \onereduce \boxed{(\lam{y}t)\;u} \onereduce t\subst{u}{y}$. 749 | \end{frame} 750 | \begin{frame}{Exercise} 751 | Write down a sequence of $\beta$-reductions and \emph{circle} all $\beta$-redexes while reducing a term: 752 | \begin{enumerate} 753 | \item $(\lam{x}x)\;z$ 754 | \item $\left((\lam{x}x)\;y\right)\;\left((\lam{z}z)\;x\right)$ 755 | \item $\lam{n\,x\,y}n\;(\lam{z}y)\;x$ 756 | \item $\left(\lam{n\,x\,y}n\;(\lam{z}y)\;x\right)\;\lam{f\,x}x$ 757 | \end{enumerate} 758 | \end{frame} 759 | 760 | 761 | \begin{frame}{Multi-step full $\beta$-reduction} 762 | It is convenient to represents a sequence 763 | of $\beta$-reductions 764 | \[ 765 | t \onereduce t_1 \onereduce \dots \onereduce u 766 | \] 767 | by a single relation $t \reduce u$. 768 | 769 | \begin{definition} 770 | The \emph{multi-step (full) $\beta$-reduction} is a relation defined inductively by 771 | \begin{prooftree} 772 | \AXC{$\vphantom{t_1}$} 773 | \RightLabel{($0$-step)} 774 | \UIC{$t \reduce t$} 775 | \end{prooftree} 776 | \begin{prooftree} 777 | \AXC{$t \onereduce u$} 778 | \AXC{$u \reduce v$} 779 | \RightLabel{($n+1$-step)} 780 | \BIC{$t \reduce v$} 781 | \end{prooftree} 782 | 783 | \end{definition} 784 | \end{frame} 785 | 786 | \begin{frame}{$t \reduce u$ is transitive} 787 | \begin{lemma} 788 | For every derivations of $t \reduce u$ and $u \reduce v$, there 789 | is a derivation of $t \reduce v$. 790 | \end{lemma} 791 | We often say ``if $t \reduce u$ and $u \reduce v$ then $t \reduce v$'' instead. 792 | \begin{proof} 793 | By induction on the derivation $d$ of $t \reduce u$: 794 | \begin{enumerate} 795 | \item If $d$ is given by (0-step), then $t =_\alpha u$. 796 | \item If $d$ is given by (n+1-step), i.e.\ there is $u'$ s.t.\ 797 | $t \onereduce u'$ and $u' \reduce u$. 798 | 799 | By induction hypothesis, 800 | every derivation $u' \reduce u$ gives rise to a derivation of $u' \reduce v$, so by (n+1-step) $t \reduce v$. 801 | \end{enumerate} 802 | \end{proof} 803 | \end{frame} 804 | 805 | \begin{frame}{$\beta$-equality} 806 | The reduction relation $t \onereduce u$ is \alert{directed}, i.e.\ $t \onereduce u$ does not imply $u \onereduce t$. 807 | We may consider a notion of \alert{undirected equality} based on $\beta$-reduction, while arguing the computational equality: 808 | \begin{definition} 809 | We say that $t$ and $u$ are $\beta$-equal, written $t =_\beta u$, if 810 | \begin{mathpar} 811 | \inferrule{t \onereduce u}{t =_\beta u}(\beta) \and 812 | \inferrule{ }{t =_\beta t}(\text{refl}) \and 813 | \inferrule{t =_\beta u}{u =_\beta t}(\text{sym}) \and 814 | \inferrule{t =_\beta u \\ u =_\beta v}{t =_\beta v} (\text{trans}) 815 | \end{mathpar} 816 | \end{definition} 817 | It is clear that $t\reduce u$ implies $t =_\beta u$ (why?). 818 | How about the converse? 819 | \end{frame} 820 | 821 | \begin{frame}{Summary} 822 | SUMMARISE HERE ALL THE RELATIONS WE HAVE SEEN SO FAR. 823 | \end{frame} 824 | 825 | \section{Programming in \texorpdfstring{$\lambda$}{λ}-Calculus} 826 | \begin{frame}{Church encoding of boolean values} 827 | 828 | Boolean and conditional can be encoded as combinators. 829 | 830 | \begin{description} 831 | \item[Boolean] 832 | \begin{align*} 833 | & \mathtt{True} && \defeq && \lambda x\,y.\, x \\ 834 | & \mathtt{False} && \defeq && \lambda x\,y.\, y 835 | \end{align*} 836 | 837 | \item[Conditional] 838 | \begin{align*} 839 | \mathtt{if} & \defeq \lambda b\;x\;y.\;b\,x\,y \\ 840 | \mathtt{if} \;\mathtt{True} \;M\;N &\reduce M \\ 841 | \mathtt{if} \;\mathtt{False}\;M\;N &\reduce N 842 | \end{align*} 843 | for any two $\lambda$-terms $M$ and~$N$. 844 | \end{description} 845 | 846 | \end{frame} 847 | \begin{frame}[allowframebreaks]{Church Encoding of natural numbers} 848 | 849 | Natural numbers as well as arithmetic operations can be encoded in untyped 850 | lambda calculus. 851 | \begin{description} 852 | \item[Church numerals] 853 | \begin{align*} 854 | &\bc_0 &&\defeq\;&& \lambda f\,x.\, x \\ 855 | &\bc_{1} &&\defeq\;&& \lambda f\,x.\, f\,x \\ 856 | &\bc_{2} &&\defeq\;&& \lambda f\,x.\, f\,(f\,x) \\ 857 | &\bc_{n+1} &&\defeq\;&& \lambda f\,x.\, f^{n+1}\,(x) 858 | \end{align*} 859 | where $f^1(x) \defeq f\;x$ and $f^{n+1}(x) \defeq f\;(f^n(x))$. 860 | \item[Successor] 861 | \begin{align*} 862 | & \mathtt{succ} && \defeq\; && \lambda n.\,\lambda f\,x.\, f\,(n\,f\,x) \\ 863 | & \mathtt{succ}\;\bc_n && \reduce\; && \bc_{n+1} 864 | \end{align*} 865 | for any natural number~$n \in \mathbb{N}$. 866 | \item[Addition] 867 | \begin{align*} 868 | & \mathtt{add} && \defeq\; && \lambda n\,m.\,\lambda f\,x.\; 869 | n\;f\;(m\;f\;x) \\ & \mathtt{add}\;\bc_{n}\;\bc_{m}\; 870 | && \reduce\; && \bc_{n + m} 871 | \end{align*} 872 | 873 | \item[Conditional] 874 | \begin{align*} 875 | & \mathtt{ifz} && \defeq \; \lambda n\;x\;y.\;n\;(\lambda z.\;y)\;x 876 | \\ 877 | & \mathtt{ifz}\;\bc_0\;M\;N && \reduce \; M \\ 878 | & \mathtt{ifz}\;\bc_{n+1}\;M\;N && \reduce \; N 879 | \end{align*} 880 | \end{description} 881 | \end{frame} 882 | 883 | \begin{frame}{Exercise} 884 | \begin{enumerate} 885 | \item Define Boolean operations $\mathtt{not}$, $\mathtt{and}$, and $\mathtt{or}$. 886 | \item Evaluate $\mathtt{succ}\;\bc_0$ and $\mathtt{add}\;\bc_{1}\;\bc_{2}$. 887 | \item Define the multiplication $\mathtt{mult}$ over Church numerals. 888 | 889 | \end{enumerate} 890 | \end{frame} 891 | \begin{frame}{General Recursion via self-reference} 892 | 893 | The summation $\sum_{i = 0}^{n} i$ for $n \in \mathbb{N}$ is typically 894 | described by self-reference as 895 | \[ 896 | \mathit{sum}(n) = 897 | \begin{cases} 898 | 0 & \text{if } n = 0 \\ 899 | n + \mathit{sum}(n - 1) & \text{otherwise}. 900 | \end{cases} 901 | \] 902 | This \alert{cannot} be done in $\lambda$-calculus directly. (Why?) 903 | 904 | Note that unfolding $\mathit{sum}$ as many times as it requires gives 905 | \[ 906 | \mathit{sum}(n) = 907 | \begin{cases} 908 | 0 & \text{if } n = 0 \\ 909 | 1 + \mathit{sum}(0) & n = 1 \\ 910 | \cdots \\ 911 | n + \mathit{sum}(n - 1) & \text{otherwise}. 912 | \end{cases} 913 | \] 914 | 915 | 916 | %We can avoid self-reference by considering a functional 917 | %$G\colon (\mathbb{N} \to \mathbb{N}) \to (\mathbb{N} \to \mathbb{N})$ 918 | %defined by 919 | %\begin{equation} \label{eq:summation} 920 | % (G(f))(n) \defeq 921 | % \begin{cases} 922 | % 0 & \text{if } n = 0 \\ 923 | % n + f(n - 1) & \text{otherwise}. 924 | % \end{cases} 925 | %\end{equation} 926 | %Then, we can show that if there exists $s\colon \mathbb{N} \to \mathbb{N}$ such that $G(s) = s$ then $s = \mathit{sum}$ by induction. 927 | \end{frame} 928 | 929 | \begin{frame}{Curry's paradoxical combinator} 930 | The \emph{Y combinator} is defined as a term 931 | \[ 932 | \mathbf{Y} \defeq \lambda f.\, (\lambda x.\, f\,(x\,x))\,(\lambda 933 | x.\,f\,(x\,x)). 934 | \] 935 | \begin{proposition} 936 | $\mathbf{Y}$ is a fixed-point operator, i.e.\ 937 | \begin{align*} 938 | \mathbf{Y}F 939 | & \onereduce {\color{red} (\lambda x.\,F\,(x\,x))\,(\lambda x.\, F\,(x\,x))} \\ 940 | & \onereduce F\,({\color{red}(\lambda x.\,F\,(x\,x))\,(\lambda x.\, F\,(x\,x))}) \\ 941 | \end{align*} 942 | for every $\lambda$-term~$F$. In particular, $\mathbf{Y}F =_\beta F(\mathbf{Y}F)$. 943 | \end{proposition} 944 | Intuitively, $\mathbf{Y}F$ defines recursion where $F$ describes each iteration. 945 | \end{frame} 946 | 947 | 948 | \begin{frame}{Summation via $\mathtt{Y}$} 949 | We encode the following recursion 950 | \[ 951 | \mathit{sum}(n) = 952 | \begin{cases} 953 | 0 & \text{if } n = 0 \\ 954 | n + \mathit{sum}(n - 1) & \text{otherwise}. 955 | \end{cases} 956 | \] 957 | by defining each iteration $G$ with an additional function $f$ so that $\mathtt{sum} \defeq \mathbf{Y}G$: 958 | \[ 959 | G \defeq \lambda f\,n.\, \mathtt{ifz}\;n\;\bc_0\; (\add\;n\;(f\;(\mathtt{pred}\;n))) 960 | \] 961 | For example, letting $G' \defeq ((\lambda x.\, G\;(x\;x))\;(\lambda x.\,G\;(x\;x)))$, we have 962 | \begin{align*} 963 | \mathtt{sum}\;\bc_1 964 | &\onereduce G'\;\bc_1 \\ 965 | &\onereduce G\;G'\;\bc_1 \\ 966 | &\onereduce (\lambda n.\, \mathtt{ifz}\;n\;\bc_0\; 967 | (\add\;n\;(G'\;(\mathtt{pred}\;n))))\;\bc_1 \\ 968 | &\onereduce \mathtt{ifz}\;\bc_1\;\bc_0\; 969 | (\add\;\bc_1\;(G'\;(\mathtt{pred}\;\bc_1))) \\ 970 | &\onereduce \dots 971 | \end{align*} 972 | \end{frame} 973 | 974 | %\begin{frame}{Turing's fixed-point combinator} 975 | % Recall that $\mathbf{Y}G =_\beta G(\mathbf{Y}\;G)$ but $\mathbf{Y}G \reduce 976 | % G(\mathbf{Y}\;G)$ does not hold. Here is a fixed-point operator such that 977 | % $\Theta F \reduce F(\Theta F)$. 978 | %\begin{proposition} 979 | % Define 980 | % \[ 981 | % \Theta \defeq 982 | % (\lambda x\,f.\,f\,(x\,x\,f))\; 983 | % (\lambda x\,f.\,f\,(x\,x\,f)) 984 | % \] 985 | % Then, 986 | % \[ 987 | % \Theta F \reduce F (\Theta F) 988 | % \] 989 | %\end{proposition} 990 | %Try Turing's fixed-point combinator with $G$ to define $\sum_{i=0}^n i$. 991 | %\begin{align*} 992 | % G \defeq{} & 993 | % \lambda f\,n.\, 994 | % \mathtt{ifz}\;n\;\bc_0\; 995 | % (\add\;n\;(f\,(\mathtt{pred}\;n))) \\ 996 | % \mathtt{sum} \defeq\; & \Theta\,G 997 | %\end{align*} 998 | %\end{frame} 999 | 1000 | \begin{frame}{Exercise} 1001 | \begin{enumerate} 1002 | \item Evaluate $\mathtt{sum}\;\bc_1$ to its normal form in detail. 1003 | \item Define the factorial $n!$ with Church numerals. 1004 | \end{enumerate} 1005 | \end{frame} 1006 | 1007 | \begin{frame}{Homework} 1008 | \begin{theorem}[Church-Rosser] 1009 | Given $u_1$ and $u_2$ with $t \reduce u_1$ and $t\reduce u_2$, there is $v$ 1010 | such that $u_1 \reduce v$ and $u_2 \reduce v$. 1011 | \[ 1012 | \xymatrix@-4.8pt{ 1013 | & t \ar@{->>}[rd]^{\beta} \ar@{->>}[ld]_{\beta} \\ 1014 | u_1 \ar@{->>}[rd]_{\beta} & & u_2 \ar@{->>}[ld]^{\beta} \\ 1015 | & v 1016 | } 1017 | \] 1018 | \end{theorem} 1019 | \begin{enumerate} 1020 | \item (2.5\%) Show that $t \reduce u$ implies $t =_\beta u$. 1021 | \item (2.5\%) Show that if $t =_\beta u$ then there is a \emph{confluent} term $v$ of $t$ and $u$, i.e.\ $t \reduce v$ and $u \reduce v$. 1022 | \end{enumerate} 1023 | \end{frame} 1024 | 1025 | \appendix 1026 | \section{Appendix: Evaluation strategy} 1027 | \begin{frame}[allowframebreaks]{Evaluation strategies} 1028 | An evaluation strategy is a procedure of selecting $\beta$-redexes 1029 | to reduce. It is a subset $\longrightarrow_{\mathrm{ev}}$ of the full 1030 | $\beta$-reduction $\onereduce$. 1031 | 1032 | \begin{description} 1033 | \item[Innermost $\beta$-redex] does not contain any $\beta$-redex. 1034 | \item[Outermost $\beta$-redex] is not contained in any other $\beta$-redex. 1035 | \end{description} 1036 | 1037 | \begin{description} 1038 | \item[the leftmost-outermost (\emph{normal order}) strategy] reduces the leftmost outermost 1039 | $\beta$-redex in a term first. For example, 1040 | \begin{align*} 1041 | & 1042 | \underline{(\lambda x.\, (\lambda y.\, y)\,x)}\quad 1043 | \underline{(\lambda x.\, (\lambda y.\, y\,y)\,x)} 1044 | \\ 1045 | \onereduce & 1046 | \underline{(\lambda y.\, y)}\quad 1047 | \underline{(\lambda x.\, (\lambda y.\, y\,y)\, x)} \\ 1048 | \onereduce & 1049 | \lambda x.\, \underline{(\lambda y.\, y\,y)}\quad 1050 | \underline{\vphantom{(\lambda}x} \\ 1051 | \onereduce & (\lambda x.\, x\,x) \\ 1052 | \notonereduce 1053 | \end{align*} 1054 | \item[the leftmost-innermost strategy] reduces the leftmost innermost 1055 | $\beta$-redex in a term first. For example, 1056 | \begin{align*} 1057 | & (\lambda x.\, \underline{(\lambda y.\, 1058 | y)}\;\;\underline{\vphantom{(\lambda} x})\, 1059 | (\lambda x.\, (\lambda y.\, y\, y)\,x) \\ 1060 | \onereduce & (\lambda x.\,x)\; 1061 | (\lambda x.\, \underline{(\lambda y.\, y\, 1062 | y)}\;\;\underline{\vphantom{(\lambda}x}) \\ 1063 | \onereduce & \underline{(\lambda x.\,x)}\;\;\underline{(\lambda x.\, x\,x)} \\ 1064 | \onereduce & (\lambda x.\, x\, x) \\ 1065 | \notonereduce 1066 | \end{align*} 1067 | \item[the rightmost-innermost/outermost strategy] 1068 | are defined similarly where terms are reduced from right to left 1069 | instead. 1070 | \end{description} 1071 | \end{frame} 1072 | 1073 | \begin{frame}{CBV versus CBN} 1074 | \begin{description} 1075 | \item[Call-by-value strategy] 1076 | rightmost-outermost but not under any abstraction 1077 | \item[Call-by-name strategy] 1078 | leftmost-outermost but not under any abstraction 1079 | \end{description} 1080 | 1081 | \mode{\vfill} 1082 | \begin{proposition}[Determinacy] 1083 | Each of evaluation strategies is deterministic, i.e.\ 1084 | if $M \onereduce N_1$ and $M \onereduce N_2$ then $N_1 = N_2$. 1085 | \end{proposition} 1086 | \end{frame} 1087 | 1088 | %\begin{frame}{Exercise} 1089 | % Define following terms 1090 | %\begin{align*} 1091 | % \Omega &\defeq (\lambda x.\, x\,x)\;(\lambda x.\,x\,x) \\ 1092 | % \mathbf{K}_1 & \defeq \lambda x\,y.\;x 1093 | %\end{align*} 1094 | %Evaluate 1095 | %\[ 1096 | % \mathbf{K}_1\;z\;\Omega 1097 | %\] 1098 | %using the call-by-value and the call-by-name strategy respectively. 1099 | %\end{frame} 1100 | 1101 | \begin{frame}{Normalisation} 1102 | \begin{definition} 1103 | \begin{enumerate} 1104 | \item $M$ is in \emph{normal form} if $M \notonereduce N$ for any $N$. 1105 | \item $M$ is \emph{weakly normalising} if $M \reduce N$ for some $N$ in 1106 | normal form. 1107 | \end{enumerate} 1108 | \end{definition} 1109 | % 1110 | \begin{enumerate} 1111 | \item $\Omega$ is not weakly normalising. 1112 | \item $\mathbf{K}_1$ is normal and thus weakly normalising. 1113 | \item $\mathbf{K}_1\;z\; \Omega$ is weakly normalising. 1114 | \end{enumerate} 1115 | 1116 | \begin{theorem} 1117 | The normal order strategy reduces every weakly 1118 | normalising term to a normal form. 1119 | \end{theorem} 1120 | 1121 | \end{frame} 1122 | %\begin{frame}{Remark} 1123 | % The definition of capture-avoiding substation is widely adopted, it is still 1124 | % ill-defined. Recursion is always a total function. Advanced 1125 | % mathematics~\cite{Pitts2013} is needed to resolve this issue. 1126 | % 1127 | % \mode{\vfill} 1128 | % Issues with named variables may be lifted by using nameless representation of 1129 | % terms. For example, in de Bruijin's representation every term 1130 | % has a canonical form, see \cite[Chapter 6]{Pierce2002}. 1131 | % 1132 | % \mode{\vfill} 1133 | % In fact, every computable function on natural numbers is definable in terms of 1134 | % terms. For interested readers, see~\cite[Chapter 3]{Barendregt1984} 1135 | % for further detail. Therefore, $\lambda$-calculus is Turing-complete. 1136 | %\end{frame} 1137 | 1138 | 1139 | 1140 | \section{Appendix: Takahashi's Proof of Confluence} 1141 | 1142 | \begin{frame}{Takahashi's Proof of Confluence} 1143 | Proving the Church-Rosser property (or \alert{confluence}) can be quite tricky. 1144 | This section presents a straightforward strategy based on a notion of complete development, which unfolds as many $\beta$-redexes as possible \emph{statically}. 1145 | 1146 | The \emph{complete development} $M^*$ of a $\lambda$-term $M$ is defined by 1147 | \begin{align*} 1148 | x^* & = x \\ 1149 | (\lambda x.\, M)^* & = \lambda x.\, M^* \\ 1150 | \left((\lambda x.\, M)\;N\right)^* & = M^*\subst{N^*}{x} \\ 1151 | (M\;N)^* & = M^*\;N^* && \text{ if $M \not\equiv \lambda x.\,M'$ } 1152 | \end{align*} 1153 | \end{frame} 1154 | \begin{frame}{Confluence: Parallel reduction} 1155 | Let $M \parreduce N$ denote the \emph{parallel reduction} defined by 1156 | \begin{multicols}{2} 1157 | \begin{prooftree} 1158 | \AXC{$\vphantom{M\reduce M}$} 1159 | \UIC{$x \parreduce x$} 1160 | \end{prooftree} 1161 | \begin{prooftree} 1162 | \AXC{$M \parreduce N$} 1163 | \UIC{$\lambda x.\, M \parreduce \lambda x.\, N$} 1164 | \end{prooftree} 1165 | \columnbreak 1166 | \begin{prooftree} 1167 | \AXC{$M \parreduce M'$} 1168 | \AXC{$N \parreduce N'$} 1169 | \BIC{$M\;N \parreduce M'\;N'$} 1170 | \end{prooftree} 1171 | \begin{prooftree} 1172 | \AXC{$M \parreduce M'$} 1173 | \AXC{$N \parreduce N'$} 1174 | \BIC{$(\lambda x.\, M)\;N \parreduce M'\subst{N'}{x}$} 1175 | \end{prooftree} 1176 | \end{multicols} 1177 | For example, 1178 | \[ 1179 | \underline{(\lambda x.\, (\lambda y.\, y)\;x)}\;\underline{((\lambda x.\, x)\;\false)} 1180 | \parreduce 1181 | \false 1182 | \] 1183 | because $(\lambda y.\,y)\;x \parreduce x$ and $(\lambda x.\,x)\;\false \parreduce \false$. 1184 | \end{frame} 1185 | 1186 | \begin{frame}{Confluence: Properties of parallel reduction} 1187 | \begin{lemma} 1188 | \begin{enumerate} 1189 | \item $M \parreduce M$ holds for any term~$M$, 1190 | \item $M \onereduce N$ implies $M \parreduce N$, and 1191 | \item $M \parreduce N$ implies $M \reduce N$. 1192 | \end{enumerate} 1193 | In particular, $M \parreduce^* N$ if and only if $M \reduce N$. 1194 | \end{lemma} 1195 | \begin{lemma}[Substitution respects parallel reduction] 1196 | $M \parreduce M'$ and $N \parreduce N'$ imply $M\subst{N}{x} \parreduce 1197 | M'\subst{N'}{x}$. 1198 | \end{lemma} 1199 | \begin{theorem}[Triangle property] 1200 | If $M \parreduce N$, then $N \parreduce M^*$. 1201 | \end{theorem} 1202 | \begin{proof}[Proof sketch] 1203 | By induction on $M \parreduce N$. 1204 | \end{proof} 1205 | \end{frame} 1206 | 1207 | \begin{frame}{Strip Lemma} 1208 | \begin{theorem} 1209 | If $L \parreduce^* M_1$ and $L \parreduce M_2$, then there exists $N$ 1210 | satisfying that $M_1 \parreduce N$ and $M_2 \parreduce^* N$, i.e.\ 1211 | \[ 1212 | \xymatrix{ 1213 | & L \ar@{=>}[rd] \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\ 1214 | M_1 \ar@{=>}[rd] & & M_2 \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\ 1215 | & N 1216 | } 1217 | \] 1218 | \end{theorem} 1219 | \begin{proof}[Proof sketch] 1220 | By induction on $L \parreduce^* M_1$. 1221 | \end{proof} 1222 | \end{frame} 1223 | 1224 | \begin{frame}{Confluence} 1225 | \begin{theorem} 1226 | If $L \parreduce^* M_1$ and $L \parreduce^* M_2$, then there exists $N$ such that $M_1 \parreduce^* N$ and $M_2 \parreduce^* N$. 1227 | \[ 1228 | \xymatrix{ 1229 | & L \ar@{=>}[rd]^(.8){*}_(.8){\beta} \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\ 1230 | M_1 \ar@{=>}[rd]^(.8){*}_(.8){\beta} & & M_2 \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\ 1231 | & N 1232 | } 1233 | \] 1234 | \end{theorem} 1235 | \begin{corollary} 1236 | The confluence of $\reduce$ holds. 1237 | \end{corollary} 1238 | 1239 | \end{frame} 1240 | 1241 | %\begin{frame}[allowframebreaks]{References} 1242 | % \bibliographystyle{amsalpha} 1243 | % \bibliography{library} 1244 | %\end{frame} 1245 | 1246 | \end{document} 1247 | -------------------------------------------------------------------------------- /tex/lecture3.tex: -------------------------------------------------------------------------------- 1 | %! TEX program = xelatex 2 | %! TEX root = lecture3_slide.tex 3 | 4 | \usepackage{listings} 5 | \title{\texorpdfstring{$\lambda$}{λ}-Calculus} 6 | \subtitle{Parametric Polymorphism} 7 | \begin{document} 8 | 9 | %{\usebackgroundtemplate{\includegraphics[width=\paperwidth]{banner.pdf}} 10 | %\begin{frame}\maketitle\end{frame}} 11 | 12 | \bgroup 13 | \usebackgroundtemplate{\includegraphics[width=\paperwidth]{banner.pdf}} 14 | \begin{frame}[plain,noframenumbering] 15 | \maketitle 16 | \end{frame} 17 | \egroup 18 | 19 | %\begin{frame}{\textbf{T}: Types and terms} 20 | % \begin{definition}[Types] 21 | % \begin{multicols}{2} 22 | % \begin{prooftree} 23 | % \AXC{$B \in \mathbb{V}$} 24 | % \RightLabel{(tvar)} 25 | % \UIC{$B : \type$} 26 | % \end{prooftree} 27 | % \begin{prooftree} 28 | % \AXC{$\vphantom{B}$} 29 | % \RightLabel{(nat)} 30 | % \UIC{$\mathbb{N} : \type$} 31 | % \end{prooftree} 32 | % \begin{prooftree} 33 | % \AXC{$\sigma : \type$} 34 | % \AXC{$\tau : \type$} 35 | % \RightLabel{(fun)} 36 | % \BIC{$\sigma \to \tau : \type$} 37 | % \end{prooftree} 38 | % \end{multicols} 39 | % \end{definition} 40 | % \begin{definition}[Terms] 41 | % Additional term formation rules are added to $\lambda_\to$ as follows. 42 | % \begin{multicols}{2} 43 | % \begin{prooftree} 44 | % \AXC{$\vphantom{M}$} 45 | % \UIC{$\zero : \term_{\textbf{T}}$} 46 | % \end{prooftree} 47 | % \begin{prooftree} 48 | % \AXC{$M$} 49 | % \UIC{$\suc\;M : \term_{\textbf{T}}$} 50 | % \end{prooftree} 51 | % \end{multicols} 52 | % \begin{prooftree} 53 | % \AXC{$L : \term_{\textbf{T}}$} 54 | % \AXC{$M : \term_{\textbf{T}}$} 55 | % \AXC{$N : \term_{\textbf{T}}$} 56 | % \AXC{$x \in V$} 57 | % \AXC{$y \in V$} 58 | % \QuinaryInfC{$\mathtt{rec}(M; x.\,y.\,N)\; L : \term_{\textbf{T}}$} 59 | % \end{prooftree} 60 | % \end{definition} 61 | %\end{frame} 62 | % 63 | %\begin{frame}{\textbf{T}: Typing rules} 64 | % \begin{definition} 65 | % Additional term typing rules are added to $\lambda_\to$ as follows. 66 | % \begin{multicols}{2} 67 | % \begin{prooftree} 68 | % \AXC{$\vphantom{\Gamma}$} 69 | % \UIC{$\Gamma \vdash \zero : \mathbb{N}$} 70 | % \end{prooftree} 71 | % \begin{prooftree} 72 | % \AXC{$\Gamma \vdash M : \mathbb{N}$} 73 | % \UIC{$\Gamma \vdash \suc\;M : \mathbb{N}$} 74 | % \end{prooftree} 75 | % \end{multicols} 76 | % \begin{prooftree} 77 | % \AXC{$\Gamma \vdash L : \mathbb{N}$} 78 | % \AXC{$\Gamma \vdash M : \tau$} 79 | % \AXC{$\Gamma, x : \mathbb{N}, y : \tau \vdash N : \tau$} 80 | % \TrinaryInfC{$\Gamma \vdash \mathtt{rec}(M; x.\,y.\,N)\;L : \tau$} 81 | % \end{prooftree} 82 | % \end{definition} 83 | % \begin{itemize} 84 | % \item Substitution for \textbf{T} is defined similarly. 85 | % \item Substitution respects typing judgements, i.e.\ 86 | % $\Gamma \vdash N : \tau$ and $\Gamma, x : \tau \vdash M : \sigma$, then 87 | % $\Gamma \vdash M\subst{N}{x} : \sigma$. 88 | % \end{itemize} 89 | %\end{frame} 90 | % 91 | %\begin{frame}{\textbf{T}: Dynamics} 92 | % $\beta$-conversion for \textbf{T} is extended with two rules 93 | % \begin{align*} 94 | % \mathtt{rec}(M, x.\,y.\, N)\;\zero & \longrightarrow_\beta M \\ 95 | % \mathtt{rec}(M, x.\,y.\, N)\;\suc\;L & \longrightarrow_\beta N\subst{L , \mathtt{rec}(M; x.\,y.\,N)\;L}{x, y} 96 | % \end{align*} 97 | % Similarly, a $\beta$-reduction $\onereduce$ extends $\longrightarrow_\beta$ to all parts of a term and $\reduce$ indicates finitely many $\beta$-reductions. 98 | % 99 | % \mode{\vfill} 100 | % \begin{theorem} 101 | % \textbf{T} enjoys the strong and weak normalisation properties as well as type safety. 102 | % \end{theorem} 103 | %\end{frame} 104 | % 105 | %\begin{frame}{Example: Addition and summation} 106 | % $\mathtt{add} : \mathbb{N} \to \mathbb{N} \to \mathbb{N}$ can be defined in \textbf{T} as 107 | % \[ 108 | % \lambda n.\,\lambda m.\,\mathtt{rec}\;(m; x.\,y.\, \suc\;y)\;n\;m 109 | % \] 110 | % 111 | % $\mathtt{sum} : \mathbb{N} \to \mathbb{N}$ can be defined in \textbf{T} as 112 | % \[ 113 | % \lambda n.\, \mathtt{rec}\;(\zero; x.\,y.\, \add\;(\suc\;x)\;y)\;n 114 | % \] 115 | %\mode{\vfill} 116 | % \begin{block}{Exercise} 117 | % Evaluate $\mathtt{sum}\;(\suc\;\zero)$. 118 | % \end{block} 119 | %\end{frame} 120 | % 121 | 122 | %\section{\PCF --- System of Recursive Functions} 123 | % 124 | %\begin{frame}{\textbf{PCF}: $\lambda_\to$ with naturals and general recursion} 125 | %% \textbf{T} does not include all computable functions, since all terms terminate eventually. 126 | %% Programming language in reality allows us to do \emph{general recursion} 127 | %% including \emph{infinite loops}. 128 | % 129 | %% \textbf{PCF} has the same class of types as \textbf{T}. 130 | % 131 | % \begin{definition}[Terms] 132 | % Additional term formation rules are added to $\lambda_\to$ as follows. 133 | % \begin{multicols}{2} 134 | % \begin{prooftree} 135 | % \AXC{$\vphantom{M}$} 136 | % \UIC{$\zero : \term_{\PCF}$} 137 | % \end{prooftree} 138 | % \begin{prooftree} 139 | % \AXC{$M : \term_{\PCF}$} 140 | % \UIC{$\suc\;M : \term_{\PCF}$} 141 | % \end{prooftree} 142 | % \end{multicols} 143 | % \begin{prooftree} 144 | % \color{red} 145 | % \AXC{$L : \term_{\PCF}$} 146 | % \AXC{$M : \term_{\PCF}$} 147 | % \AXC{$N : \term_{\PCF}$} 148 | % \AXC{$x \in V$} 149 | % \QuaternaryInfC{$\ifz(M; x.\,N)\; L$} 150 | % \end{prooftree} 151 | % \begin{prooftree} 152 | % \color{red} 153 | % \AXC{$M : \term_{\PCF}$} 154 | % \AXC{$x \in V$} 155 | % \BIC{$\fix\,x.\,M : \term_{\PCF}$} 156 | % \end{prooftree} 157 | % \end{definition} 158 | %\end{frame} 159 | % 160 | %\begin{frame}{\textbf{PCF}: Typing rules} 161 | % \begin{definition} 162 | % Additional term typing rules are added to $\lambda_\to$ as follows. 163 | % \begin{multicols}{2} 164 | % \begin{prooftree} 165 | % \AXC{$\vphantom{\Gamma}$} 166 | % \UIC{$\Gamma \vdash \zero : \mathbb{N}$} 167 | % \end{prooftree} 168 | % \begin{prooftree} 169 | % \AXC{$\Gamma \vdash M : \mathbb{N}$} 170 | % \UIC{$\Gamma \vdash \suc\;M : \mathbb{N}$} 171 | % \end{prooftree} 172 | % \end{multicols} 173 | % \begin{prooftree} 174 | % \color{red} 175 | % \AXC{$\Gamma \vdash L : \mathbb{N}$} 176 | % \AXC{$\Gamma \vdash M : \tau$} 177 | % \AXC{$\Gamma, x : \mathbb{N} \vdash N : \tau$} 178 | % \TrinaryInfC{$\Gamma \vdash \ifz(M; x.\,N)\;L : \tau$} 179 | % \end{prooftree} 180 | % \begin{prooftree} 181 | % \color{red} 182 | % \AXC{$\Gamma, x : \tau \vdash M : \tau$} 183 | % \UIC{$\Gamma \vdash \fix\,x.\,M : \tau$} 184 | % \end{prooftree} 185 | % \end{definition} 186 | % \begin{itemize} 187 | % \item Substitution for \textbf{PCF} is defined similarly. 188 | % \item Substitution respects typing judgements, i.e.\ 189 | % $\Gamma \vdash N : \tau$ and $\Gamma, x : \tau \vdash M : \sigma$, then 190 | % $\Gamma \vdash M\subst{N}{x} : \sigma$. 191 | % \end{itemize} 192 | %\end{frame} 193 | % 194 | %\begin{frame}{\textbf{PCF}: Dynamics} 195 | % $\beta$-conversion for \textbf{PCF} is extended with three rules 196 | % \begin{align*} 197 | % \fix\,x.\,M & \longrightarrow_\beta M\subst{\fix\,x.\,M}{x} \\ 198 | % \ifz(M; x.\, N)\;\zero & \longrightarrow_\beta M \\ 199 | % \ifz(M; x.\, N)\;(\suc\;L) & \longrightarrow_\beta N\subst{L}{x} 200 | % \end{align*} 201 | % Similarly, a $\beta$-reduction $\onereduce$ extends $\longrightarrow_\beta$ to all parts of a term and $\reduce$ indicates finitely many $\beta$-reductions. 202 | % 203 | % \mode{\vfill} 204 | % \begin{theorem} 205 | % \textbf{PCF} enjoys type safety. 206 | % \end{theorem} 207 | %\end{frame} 208 | % 209 | %\begin{frame}{Example: Predecessor and negation} 210 | % \begin{align*} 211 | % \pred & \defeq \lambda n : \mathbb{N}.\,\ifz(\zero; x.\, x)\;n & : \mathbb{N} \to \mathbb{N} \\ 212 | % \mathtt{not} & \defeq \lambda n : \mathbb{N}.\, \ifz(\suc\;\zero; x.\, \zero)\; n & : \mathbb{N} \to \mathbb{N} 213 | % \end{align*} 214 | % \begin{block}{Exercise} 215 | % Evaluate the following terms to their normal forms. 216 | % \begin{enumerate} 217 | % \item $\pred\;\zero$ 218 | % \item $\pred\;(\suc\;(\suc\;(\suc\;\zero)))$ 219 | % \item $\mathtt{not}\;(\suc\;(\suc\;\zero))$ 220 | % \end{enumerate} 221 | % 222 | % \end{block} 223 | %\end{frame} 224 | 225 | \section{Polymorphic \texorpdfstring{$\lambda$}{λ}-Calculus: Static} 226 | \begin{frame}{Polymorphic types} 227 | Given a set $\mathbb{V}$ of type variables, the judgement $A : \type$ is defined by 228 | defined by 229 | \begin{prooftree} 230 | \AXC{} 231 | \RightLabel{(tvar), if $X \in \mathbb{V}$} 232 | \UIC{$X : \type$} 233 | \end{prooftree} 234 | \begin{prooftree} 235 | \AXC{$A : \type$} 236 | \AXC{$B : \type$} 237 | \RightLabel{(fun)} 238 | \BIC{$A \to B : \type$} 239 | \end{prooftree} 240 | \begin{prooftree} 241 | \AXC{$A : \type$} 242 | \AXC{$X \in \mathbb{V}$} 243 | \RightLabel{(universal)} 244 | \BIC{$\forall X.\, A : \type$} 245 | \end{prooftree} 246 | where $X$ may or may not occur in~$A$. 247 | 248 | The polymorphic type $\forall X.\, A$ provides a universal type for every type $B$ by instantiating $X$ for $B$, i.e.\ $A\subst{B}{x}$. 249 | \end{frame} 250 | 251 | \begin{frame}{Examples} 252 | For example, the polymorphic type allows us to express terms that should work on arbitrary types, such as 253 | \begin{itemize} 254 | \item $\mathtt{id} : \forall X.\, X \to X$ 255 | 256 | \item $\mathtt{proj}_1 : \forall X.\,\forall Y.\, X \to Y \to X$ 257 | 258 | \item $\mathtt{proj}_2 : \forall X.\,\forall Y.\, X \to Y \to Y$ 259 | 260 | \item $\mathtt{length} : \forall X.\, \List\;X \to \nat$ 261 | 262 | \item $\mathtt{singleton} : \forall X. X \to \List(X)$ 263 | \end{itemize} 264 | 265 | \end{frame} 266 | 267 | \begin{frame}{Free and bound variables, again} 268 | \begin{definition} 269 | The \emph{free variable} $\FV(A)$ of $A$ is defined inductively by 270 | \begin{align*} 271 | \FV(X) & = X \\ 272 | \FV(A \to B) & = \FV(A) \cup \FV(B) \\ 273 | \FV(\forall X.\, A) & = \FV(A) - \{X\} 274 | \end{align*} 275 | \end{definition} 276 | The function extends to contexts: 277 | $\FV(\Gamma) = \set{ X \in \mathbb{V}}{ \exists (x : A) \in \Gamma \wedge X \in \FV(A) }$. 278 | \begin{exercise*} 279 | 280 | \begin{enumerate} 281 | \item $\FV(\forall X.\, (X \to X) \to X \to X)$ 282 | \item $\FV(x : X_1, y : X_2, z : \forall X.\,X)$ 283 | \end{enumerate} 284 | \end{exercise*} 285 | \end{frame} 286 | 287 | \begin{frame}{Capture-avoiding substitution for type} 288 | Permutation of type variables and $\alpha$-equivalence between types are defined similarly. 289 | In particular, the substitution is defined to avoid any capture of free type variables: 290 | \begin{definition} 291 | The \emph{capture-avoiding substitution} $\_\subst{A}{X}$ of a type $A$ for a type variable~$X$ is defined by 292 | \begin{align*} 293 | X\subst{A}{X} & = A \\ 294 | Y\subst{A}{X} & = Y && \text{if $X \neq Y$}\\ 295 | (B \to C)\subst{A}{X} & = (B\subst{A}{X}) \to (C\subst{A}{X}) \\ 296 | (\forall Y.B)\subst{A}{X} & = \forall Y. B\subst{A}{X} && 297 | \text{if $Y \neq X, Y \not\in \FV(A)$} 298 | \end{align*} 299 | \end{definition} 300 | \end{frame} 301 | 302 | \begin{frame}{Typed terms} 303 | Terms in polymorphic $\lambda$-calculus are extended with types. 304 | We define the set of terms from scratch here: 305 | \begin{definition} 306 | The set $\Lambda_{\forall}(V, \mathbb{V})$ of terms in polymorphic $\lambda$-calculus is defined inductively: 307 | \begin{description} 308 | \item[variable] $x \in \Lambda_{\forall}(V, \mathbb{V})$ if $x$ is in $V$ 309 | \item[application] $t@u \in \Lambda_{\forall}(V, \mathbb{V})$ if $t, u \in \Lambda_{\forall}(V, \mathbb{V})$ 310 | \item[abstraction] $\lam{x:A}t$ if $x \in V$, $A$ is a type, and $t \in \Lambda_{\forall}(V, \mathbb{V})$ 311 | \item[type abstraction] $\lam{X}t$ is in $\Lambda_{\forall}(V, \mathbb{V})$ if $X$ is in $\mathbb{V}$ and $t$ is in $\Lambda_{\forall}(V, \mathbb{V})$ 312 | \item[type application] $t\;A$ is in $\Lambda_{\forall}(V, \mathbb{V})$ if $t$ is in $\Lambda_{\forall}(V, \mathbb{V})$ and $A$ is a type. 313 | \end{description} 314 | \end{definition} 315 | 316 | N.B.\ $\lam{x:A}t$ includes the type of $x$ as part of term. 317 | We have additionally a \emph{substitution $t\subst{A}{X}$ of a type $A$ for a type variable $X$} in $t$. 318 | \end{frame} 319 | 320 | \begin{frame}{Typing judgement: Overview} 321 | Polymorphic $\lambda$-calculus has two kinds of typing judgements. 322 | \begin{itemize} 323 | \item $\Delta \vdash A$ stands for a type $A$ under the type context $\Delta$; 324 | \item $\Delta; \Gamma \vdash t : A$ stands for a term $t$ of type $A$ under the context $\Gamma$ and the type context~$\Delta$ 325 | \end{itemize} 326 | where a \emph{type context} is a sequence of type variable $X_1, X_2, \dots, X_n$. 327 | 328 | The new context $\Delta$ is used to keep track of type variables available within the term, as they may be introduced by type abstraction. 329 | 330 | \end{frame} 331 | 332 | \begin{frame}{Type formation} 333 | The judgement $\Delta \vdash A$ is constructed inductively by following rules. 334 | \begin{multicols}{2} 335 | \begin{prooftree} 336 | \AXC{} 337 | \RightLabel{if $\Delta \ni X$} 338 | \UIC{$\Delta \vdash X$} 339 | \end{prooftree} 340 | \columnbreak 341 | \begin{prooftree} 342 | \AXC{$\Delta \vdash X$} 343 | \AXC{$\Delta \vdash Y$} 344 | \BIC{$\Delta \vdash X \to Y$} 345 | \end{prooftree} 346 | \end{multicols} 347 | \begin{prooftree} 348 | \AXC{$\Delta, X \vdash A$} 349 | \UIC{$\Delta \vdash \forall X.\, A$} 350 | \end{prooftree} 351 | 352 | \begin{block}{Exercise} 353 | Derive the judgement 354 | \[ 355 | X \vdash X \to X 356 | \] 357 | \end{block} 358 | \end{frame} 359 | 360 | \begin{frame}{Typing rules} 361 | The judgement $\Delta; \Gamma \vdash t : A$ is defined inductively by following rules. 362 | \begin{multicols}{2} 363 | % \small 364 | \begin{prooftree} 365 | \AXC{} 366 | \RightLabel{if $\Gamma \ni x : A$} 367 | \UIC{$\Delta ; \Gamma \vdash x : A$} 368 | \end{prooftree} 369 | \begin{prooftree} 370 | \AXC{$\Delta; \Gamma \vdash t : A \to B$} 371 | \AXC{$\Delta; \Gamma \vdash u : A$} 372 | \BIC{$\Delta; \Gamma \vdash t\;u : B$} 373 | \end{prooftree} 374 | \begin{prooftree} 375 | \AXC{{\color{red}$\Delta \vdash A$}} 376 | \AXC{$\Delta; \Gamma, x : A \vdash t : B$} 377 | \BIC{$\Delta; \Gamma \vdash \lam{x : A} t : A \to B$} 378 | \end{prooftree} 379 | \color{red} 380 | \begin{prooftree} 381 | \AXC{$\Delta, X; \Gamma \vdash t : A$} 382 | \RightLabel{($\forall$-intro)} 383 | \UIC{$\Delta; \Gamma \vdash \lam{X}t : \forall X.\, A$} 384 | \end{prooftree} 385 | \begin{prooftree} 386 | \AXC{$\Delta; \Gamma \vdash t : \forall X.\, A$} 387 | \AXC{$\Delta \vdash B$} 388 | \RightLabel{($\forall$-elim)} 389 | \BIC{$\Delta; \Gamma \vdash t\;B : A\subst{B}{x}$} 390 | \end{prooftree} 391 | \end{multicols} 392 | 393 | \begin{theorem}[Type safety] 394 | Suppose $\Delta; \Gamma \vdash t : A$. Then, 395 | \vskip -1pt 396 | \begin{enumerate} 397 | \item $t \onereduce u$ implies $\Delta; \Gamma \vdash u : A$; 398 | \item $t$ is in normal form or there exists $u$ such that $t \onereduce u$ 399 | \end{enumerate} 400 | \end{theorem} 401 | \end{frame} 402 | 403 | \begin{frame}{Undecidability of type inference} 404 | \begin{theorem}[Wells, 1999] 405 | It is undecidable whether, given a closed term $t$ of the untyped 406 | $lambda$-calculus, there is a well-typed term $t'$ in polymorphic $\lambda$-calculus such that 407 | $|t'| = t$. 408 | \end{theorem} 409 | 410 | Two ways to retain decidable type inference: 411 | \begin{enumerate} 412 | \item Limit the expressiveness so that type inference remains decidable. 413 | For example, \emph{Hindley-Milner type system} adapted by Haskell 98, Standard ML, etc.\ supports only a limited form of polymorphism but type inference is decidable. 414 | \item Adopt \emph{partial} type inference so that type annotations can be used for, e.g. top-level definitions and 415 | local definitions. 416 | \end{enumerate} 417 | 418 | Check out \emph{bidirectional type synthesis}. 419 | % \begin{description} 420 | % \item[Arbitrary Rank Polymorphism] $\forall$ can appear 421 | % anywhere {\small (GHC with \texttt{-XRankNType})}. 422 | % \item[Rank-1 Polymorphism] 423 | % $\forall$ only appear in the outermost position. 424 | % \end{description} 425 | \end{frame} 426 | 427 | \begin{frame}{Typing derivation} 428 | 429 | The typing judgement ${}\vdash \lam{X} \lam{x : X} x : \forall X.\,X \to X$ is derivable 430 | \vfill 431 | \begin{prooftree} 432 | \LARGE 433 | \AXC{} 434 | \UIC{$X \vdash X$} 435 | \AXC{} 436 | \UIC{$X; x : X \vdash x : X $} 437 | \BIC{$X ; \cdot \vdash \lam{x : X} x : X \to X$} 438 | \UIC{$\vdash \lam{X}\lam{x : X} x : \forall X. X \to X$} 439 | \end{prooftree} 440 | \vfill 441 | 442 | \begin{convention} 443 | $\vdash t : A$ stands for $\cdot ; \cdot \vdash t : \tau$ where both contexts are empty. 444 | \end{convention} 445 | \end{frame} 446 | 447 | \begin{frame}{Exercise} 448 | Derive the following judgements: 449 | \begin{enumerate} 450 | \item $\vdash (\lam{X\,Y}\lam{x : X}{y : Y} x) : \forall X.\,\forall Y.\, X \to Y \to X$ 451 | \item $\vdash\lam{X}\lam{f : X \to X}{x : X} f\;(f\;x) : \forall X.\;(X\to X) \to X\to X$ 452 | \end{enumerate} 453 | Hint.\, polymorphic $\lambda$-calculus F is syntax-directed, so the type inversion holds. 454 | 455 | \end{frame} 456 | %\begin{frame}{Self application} 457 | %Self-application is not typable in simply typed $\lambda$-calculus. 458 | % \[ 459 | % \lambda (x : t).\, x\; x 460 | % \] 461 | % However, self-application is possible in System F. 462 | % \[ 463 | % \lambda (x : \forall t. t\to t).\, x\;(\forall t. t\to t)\;x 464 | % \] 465 | %\mode{\vfill} 466 | % \begin{block}{Exercise} 467 | % Instantiate the first $t$ with the type $\forall t.\, t \to t$. 468 | % \end{block} 469 | %\end{frame} 470 | 471 | \section{Polymorphic \texorpdfstring{$\lambda$}{λ}-Calculus: Dynamics and Programming} 472 | 473 | \begin{frame}{Reduction} 474 | 475 | $\beta$-reduction for polymorphic $\lambda$-calculus has two rules apart from other structural rules: 476 | \[ 477 | (\lam{x : A} t)\,u \onereduce t\subst{u}{x} 478 | \quad\text{and}\quad 479 | \color{red} (\lam{X}t)\;A \onereduce t \subst{A}{X} 480 | \] 481 | 482 | For example, 483 | \[ 484 | (\lam{X}\lam{x : X} x)\;A\; t 485 | \onereduce 486 | (\lam{x : X} x)\subst{A}{X}\;t 487 | \equiv 488 | (\lambda x : A.\, x)\;t 489 | \onereduce 490 | t 491 | \] 492 | 493 | Similarly, $\beta$-reduction extends to subterms of a given term, introducing relations $\onereduce$ and $\reduce$ in the same way. 494 | \end{frame} 495 | 496 | %\begin{frame}{System F: Evaluation} 497 | % 498 | %The full $\beta$-reduction is a relation on $\lambda$-terms defined by 499 | %\begin{multicols}{2} 500 | % \begin{prooftree} 501 | % \AXC{$M_1 \longrightarrow_{\beta} M_2$} 502 | % \UIC{$M_1 \onereduce M_2$} 503 | % \end{prooftree} 504 | % \begin{prooftree} 505 | % \AXC{$M_1 \onereduce M_2$} 506 | % \UIC{$\lambda (x:\tau).\, M_1 \onereduce \lambda (x:\tau).\, M_2$} 507 | % \end{prooftree} 508 | % \begin{prooftree} 509 | % \AXC{{\color{red}$M_1 \onereduce M_2$}} 510 | % \UIC{{\color{red}$\Lambda t.\, M_1 \onereduce \Lambda t.\, M_2$}} 511 | % \end{prooftree} 512 | % \begin{prooftree} 513 | % \AXC{$M_1 \onereduce M_2$} 514 | % \UIC{$M_1\,N \onereduce M_2\,N$} 515 | % \end{prooftree} 516 | % \begin{prooftree} 517 | % \AXC{$N_1\onereduce N_2$} 518 | % \UIC{$M\,N_1 \onereduce M\,N_2$} 519 | % \end{prooftree} 520 | % \begin{prooftree} 521 | % \AXC{{\color{red}$M_1 \onereduce M_2$}} 522 | % \UIC{{\color{red}$M_1\,\tau \onereduce M_2\,\tau$}} 523 | % \end{prooftree} 524 | %\end{multicols} 525 | %If $M \onereduce N$, then $M$ and $N$ \alert{denotes} the same value. So, 526 | %\[ 527 | % M =_\beta N 528 | %\] 529 | %where $=_\beta$ is the congruence and equivalence closure of 530 | %$\longrightarrow_\beta$. 531 | %\end{frame} 532 | 533 | \begin{frame}{Empty type} 534 | \begin{definition} 535 | The \emph{empty type} is defined by 536 | \[ 537 | \bot \defeq \forall X.\, X 538 | \] 539 | \end{definition} 540 | \vfill 541 | No closed term $t$ has this type! (Why?) 542 | \begin{exercise*} 543 | Suppose that $\vdash t : \forall X.\, X$. 544 | Can we derive a contradiction? 545 | \end{exercise*} 546 | \end{frame} 547 | 548 | \begin{frame}{Sum type} 549 | 550 | \begin{definition} 551 | The \emph{sum type} is defined by 552 | \[ 553 | A + B \defeq \forall X. (A \to X) \to (B \to X) \to X 554 | \] 555 | \end{definition} 556 | It has two injection functions: the first injection is defined by 557 | \begin{align*} 558 | \mathtt{left}_{A + B} & \defeq \lam{x : A}\lam{X}\lam{f : A \to X}{g : B\to X} f\;x \\ 559 | \mathtt{right}_{A + B} & \defeq \lam{y : B}\lam{X}\lam{f : A \to X}{g : B \to X} g\;y 560 | \end{align*} 561 | 562 | %With the sum type, we can implement the usual construct \texttt{either}: 563 | %\[ 564 | % \mathtt{either} : \forall\,t.\;(\sigma \to t) \to (\tau \to t) 565 | % \to (\sigma + \tau) \to t 566 | %\] 567 | %which corresponds to the Haskell function 568 | %{\small 569 | %\begin{semiverbatim} 570 | % either :: (a -> c) -> (b -> c) -> Either a b -> c 571 | % 572 | % either f g (Left a) = f a 573 | % 574 | % either f g (Right b) = g b 575 | %\end{semiverbatim}} 576 | 577 | \mode{\vfill} 578 | \begin{block}{Exercise} 579 | Define 580 | \[ 581 | \mathtt{either} : \forall X.\, (A \to X) \to (B \to X) \to A + B \to X 582 | \] 583 | \end{block} 584 | \end{frame} 585 | 586 | \begin{frame}{Product type} 587 | \begin{definition}[Product Type] 588 | The product type is defined by 589 | \[ 590 | A \times B \defeq \forall X.\, (A \to B \to X) \to X 591 | \] 592 | \end{definition} 593 | The pairing function is defined by 594 | \begin{align*} 595 | \left< \_, \_\right>_{A, B} \defeq \lam{x : A}{y : B}\lam{X}\lam{f : A \to B \to X} f\;x\;y 596 | \end{align*} 597 | \vskip -1pt 598 | \begin{block}{Exercise} 599 | Define projections 600 | \[ 601 | \mathtt{proj}_1 : A \times B \to A 602 | \quad\text{and}\quad 603 | \mathtt{proj}_2 : A\times B \to B 604 | \] 605 | \end{block} 606 | \end{frame} 607 | \begin{frame}[allowframebreaks]{Natural numbers} 608 | The type of Church numerals is defined by 609 | \[ 610 | \nat \defeq \forall X.\, (X\to X)\to X\to X 611 | \] 612 | \begin{description} 613 | \item[Church numerals] 614 | \begin{align*} 615 | \bc_n & : \nat \\ 616 | \bc_n & \defeq \lam{X}\lam{f:X \to X}{x:X} f^n\;x 617 | \end{align*} 618 | \item[Successor] 619 | \begin{align*} 620 | \suc & : \nat \to \nat \\ 621 | \suc & \defeq \lam{n : \nat}{X}{f : X \to X}{x:X}f\;(n\;X\;f\;x) 622 | \end{align*} 623 | \item[Addition] 624 | \begin{align*} 625 | \add & : \nat \to \nat \to \nat \\ 626 | \add & \defeq \lam{n : \nat}{m:\nat} \lam{X}{f:X\to X}{x: X}\\ 627 | & (m\;X\;f)\;(n\;X\;f\;x) 628 | \end{align*} 629 | \item[Multiplication] 630 | \begin{align*} 631 | \mul & : \nat\to \nat \to\nat\\ 632 | \mul & \defeq\,? 633 | \end{align*} 634 | \item[Conditional] 635 | \begin{align*} 636 | \ifz & : \forall X.\,\nat \to X \to X \to X \\ 637 | \ifz & \defeq\,? 638 | \end{align*} 639 | \end{description} 640 | Polymorphic $\lambda$-calculus allows us to define \emph{recursor} like \texttt{fold} in Haskell. 641 | \begin{align*} 642 | \mathtt{fold}_{\nat} & : \forall X.\, (X \to X) \to X \to \nat \to X \\ 643 | \mathtt{fold}_{\nat} & \defeq \lam{X}{f : X \to X}{e_0 : X}{n : \nat} n\;X\;f\;e_0 644 | \end{align*} 645 | 646 | \begin{block}{Exercise} 647 | Define $\add$ and $\mul$ using $\mathtt{fold}_{\nat}$ and justify your answer. 648 | 649 | \begin{enumerate} 650 | \item $\add'\defeq \;? : \nat\to\nat\to\nat$ 651 | \item $\mul'\defeq \;? : \nat\to\nat\to\nat$ 652 | \end{enumerate} 653 | \end{block} 654 | \end{frame} 655 | 656 | \begin{frame}{Lists} 657 | \begin{definition} 658 | For any type $A$, the type of lists over $A$ is 659 | \[ 660 | \List(A) \defeq \forall X.\, X \to (A \to X \to X) \to X 661 | \] 662 | \end{definition} 663 | with list constructors: 664 | \[ 665 | \mathtt{nil}_A \defeq \lam{X}{h : X}{f : A \to X \to X} h 666 | \] 667 | and $\mathtt{cons}_A$ of type $A \to \List(A) \to \List(A)$ defined as 668 | \[ 669 | \lam{x : A}{xs : \List(A)}{X}{h : X}{f : A \to X \to X}f\,x\,(xs\; X\; h\; f) 670 | \] 671 | 672 | 673 | \end{frame} 674 | 675 | %\begin{frame}[allowframebreaks]{Existential Type} 676 | % Abstraction can be described by an \emph{existential type}. 677 | % \[ 678 | % \exists t.\, \tau 679 | % \] 680 | % where $\tau$ is a type in which $t$ may appear as a free variable. 681 | % A term $(\sigma, M)$ of type $\exists t.\, \tau$ consists of a \emph{type} 682 | % $\sigma$ and a term $M$ of type $\tau\subst{t}{\sigma}$. 683 | % 684 | % 685 | % \begin{example} 686 | % A \emph{stack} is a data structure with two operations: 687 | % \begin{itemize} 688 | % \item \texttt{push} 689 | % \item \texttt{pop} 690 | % \end{itemize} 691 | % Any implementation satisfying $\mathtt{pop} \circ (\mathtt{push}\, n\, st) = 692 | % (n, st)$ can be seen as a stack. 693 | % \end{example} 694 | % 695 | % \begin{example} 696 | % The type of stacks over natural numbers can be defined by 697 | % \[ 698 | % \exists t. (t \times \nat \to t) \times (t \to 1 + \nat \times t) 699 | % \] 700 | % \begin{enumerate} 701 | % \item The first component is the \text{push} function. 702 | % \item The second component is the \text{pop} function. 703 | % \end{enumerate} 704 | % An instance of a stack is an element $(\sigma, M)$ of the above type 705 | % \begin{enumerate} 706 | % \item $\sigma$ is a type 707 | % \item $\mathtt{proj}_1\, M \colon \sigma \times \nat \to \sigma$ 708 | % \item $\mathtt{proj}_2\, M \colon \sigma \to 1 + \nat \times 709 | % \sigma$. 710 | % \end{enumerate} 711 | % \end{example} 712 | %\end{frame} 713 | % 714 | %%\begin{frame}[fragile]{Encoding of Existential Type in Haskell} 715 | %% A limited form of existential type can be defined by 716 | %% \begin{semiverbatim} 717 | %% data Exists t where 718 | %% Ex :: t a -> Exists t 719 | %% \end{semiverbatim} 720 | %% and another version for typeclass 721 | %% \begin{semiverbatim} 722 | %% data Exists' c where 723 | %% Ex' :: c a => a -> Exists' c 724 | %% \end{semiverbatim} 725 | %% in GHC with extensions \texttt{GADTs} and \texttt{ConstraintKinds}. 726 | %% 727 | %%\mode{\vfill} 728 | %% \begin{block}{Exercise} 729 | %% Define the top type $\top$ which contains every typable term. 730 | %% \end{block} 731 | %%\end{frame} 732 | % 733 | %\begin{frame}{Encoding of Existential Type in System F} 734 | % The existential type can also be encoded in System F as 735 | % \[ 736 | % \exists t.\, \tau \defeq \forall u.\; (\forall t.\; \tau \to u) \to u 737 | % \] 738 | %% using continuation passing style.\footnote{Recall that $\exists x.\,\varphi 739 | %% = (\forall x.\, (\varphi \to \bot)) \to \bot$ classically. } 740 | %\mode{\vfill} 741 | % A witeness $(\sigma, M)$ of $\exists t.\, \tau$ is encoded as 742 | % \[ 743 | % \Lambda u.\,\lambda (f : \forall t.\, \tau \to u).\, f\;\sigma\;M 744 | % \] 745 | % 746 | % \begin{block}{Exercise} 747 | % Check that the above terms do make sense. How to use a term of some existential 748 | % type? 749 | % \end{block} 750 | % 751 | %\end{frame} 752 | 753 | \begin{frame}{Impredicative encodings of inductive types} 754 | \emph{Inductive types} can be defined in polymorphic $\lambda$-calculus~\cite{Bohm1985}, including the empty type, the types of sums, natural numbers, and lists. 755 | 756 | The Church encoding shows the expressiveness of polymorphic $\lambda$-calculus but is not efficient~\cite{Koopman2014}. 757 | Other styles of encoding have been proposed~\cite{Firsov2018} to improve the efficiency and the size and used in implementations. 758 | \end{frame} 759 | 760 | %\begin{frame}{Type erasure} 761 | %\begin{definition} 762 | % The \emph{erasing map} is a function defined by 763 | % \begin{align*} 764 | % |x| & = x \\ 765 | % |\lam{x : A} t| & = \lam{x} |t| \\ 766 | % |t\;u| & = |t|\;|u| \\ 767 | % |\lam{X} t| & = |t| \\ 768 | % |t\;A| & = |t| 769 | % \end{align*} 770 | %\end{definition} 771 | % 772 | %\begin{proposition} 773 | % Within polymorphic $\lambda$-calculus, if ${}\vdash t : A$ and $|t| \onereduce u'$, then there exists a well-typed term~$u$ with 774 | % ${}\vdash u : A$ and $|u| = u'$. 775 | %\end{proposition} 776 | %Well-typed terms can be evaluated with types erased safely. 777 | %\end{frame} 778 | 779 | \section{Reasoning with Types} 780 | 781 | \begin{frame}{What can types tell?} 782 | The \emph{type discipline} of a language does not only check if a program makes sense but also enforce safety properties such as \emph{type safety} and \emph{strong normalisation}. 783 | 784 | In fact, types can be used to tell what functions are \emph{definable} or what equations a term should satisfy with respect to a given type. 785 | 786 | What terms can be defined for the following types? 787 | \begin{enumerate} 788 | \item $\forall X. X$ 789 | \item $\forall X.\, X \to X$ 790 | \item $\forall X Y.\, X \to Y \to X$ 791 | \item $\forall X.\, X \to \nat$ 792 | \end{enumerate} 793 | 794 | Let's start with functions definable in simply typed $\lambda$-calculus first. 795 | \end{frame} 796 | 797 | \begin{frame}[allowframebreaks]{$\lambda$-Definability in simply typed $\lambda$-calculus} 798 | \begin{idea} 799 | Each term $\Gamma \vdash t : A$ can be interpreted as a \emph{set-theoretic} function $f$ to $\Brackets{A}$, a designated interpretation of $A$, from $\Brackets{\Gamma} = \prod_{x : A \in \Gamma} \Brackets{A}$. 800 | \end{idea} 801 | 802 | In detail, we assign a set $O_X$ to each $X \in \mathbb{V}$ and then extend the interpretation to all types: 803 | \begin{align*} 804 | \Brackets{X} & = O_X \\ 805 | \Brackets{A \to B} & = \Brackets{A} \to \Brackets{B} 806 | \end{align*} 807 | as well as contexts $\Gamma$: 808 | \begin{align*} 809 | \Brackets{\cdot} & = \{*\} \\ 810 | \Brackets{\Gamma, x : A} & = \Brackets{\Gamma} \times \Brackets{A}. 811 | \end{align*} 812 | 813 | Each term $\Gamma \vdash t : A$ is interpreted as a set-theoretic function 814 | \[ 815 | \Brackets{t}\colon \Brackets{\Gamma} \to \Brackets{A} 816 | \] 817 | defined inductively (modulo $\alpha$-equivalence) by 818 | \begin{align*} 819 | \Brackets{\Gamma \vdash x_i : A}(\rho) & = \rho(i) \\ 820 | \Brackets{\Gamma \vdash t\; u : B}(\rho) & = \Brackets{\Gamma \vdash t : A \to B}(\rho)\left(\Brackets{\Gamma \vdash u : A}(\rho)\right) \\ 821 | \Brackets{\Gamma \vdash \lam{x}t : A \to B}(\rho) & = \left( v \mapsto \Brackets{\Gamma, x : A \vdash t}(\rho , v) \right) 822 | \end{align*} 823 | where $\rho \in \Brackets{\Gamma}$ is called an \emph{environment}. 824 | 825 | N.B.\ For $\Brackets{\cdot \vdash t : A}(*)$ we simply write $\Brackets{t}$. 826 | \begin{definition} 827 | A set-theoretic function $f\colon X \to Y$ is \alert{$\lambda$-definable} w.r.t.\ some interpretation if there is a closed term $t : A \to B$ such that $f = \Brackets{t}$. 828 | \end{definition} 829 | \end{frame} 830 | 831 | \begin{frame}{Quiz time} 832 | Suppose that there is only one type variable $X$ and $O_X = \{ \btt, \bff \}$. 833 | 834 | Which of the following functions $f\colon O_X \to O_X$ are $\lambda$-definable? 835 | \begin{enumerate} 836 | \item the identity function $f(x) = x$ 837 | \item the constant function $f(x) = \btt$ 838 | \item the constant function $f(x) = \bff$ 839 | \item the negation function $f(\btt) = \bff$ and $f(\bff) = \btt$ 840 | \end{enumerate} 841 | \end{frame} 842 | 843 | \begin{frame}{Logical relation} 844 | \begin{idea} 845 | If $v_1$ and $v_2$ are related, $\Brackets{t}(v_1)$ and $\Brackets{t}(v_2)$ should also be related. 846 | \end{idea} 847 | A family $\{R^A \subseteq \Brackets{A} \times \Brackets{A} \}_{A : \type}$ of binary relations is \alert{logical} if 848 | \[ 849 | R^{A \to B}(f_1, f_2) \quad\text{iff}\quad \forall x_1 x_2.\, R^{A}(x_1, x_2) \implies R^{B}(f_1(x_1), f_2(x_2)). 850 | \] 851 | 852 | N.B.\ A logical relation is determined by $R^X$ for type variables $X$. 853 | \begin{exercise*} 854 | What is $R^{X \to X}$, if \dots 855 | \begin{enumerate} 856 | \item $R^X = \emptyset$? 857 | \item $R^X = O_X \times O_X$? 858 | \item $R^X = \{(\btt, \bff)\}$? 859 | \end{enumerate} 860 | \end{exercise*} 861 | \end{frame} 862 | 863 | \begin{frame}{The fundamental theorem of logical relations} 864 | \begin{theorem}[Fundamental Theorem of Logical Relations]\label{thm:fundamental_thm} 865 | Let $\{R^A\}_{A : \type}$ be a logical relation. 866 | Then, 867 | \[ 868 | R^A(\Brackets{\Gamma \vdash t : A}(\rho_1), \Brackets{\Gamma \vdash t : A}(\rho_2)) 869 | \] 870 | for every $\Gamma \vdash t : A$ and environments $\rho_1, \rho_2 \in \Brackets{\Gamma}$ satisfying $R^{A_i}(\rho_1(i), \rho_2(i))$ for every $x_i : A_i \in \Gamma$. 871 | \end{theorem} 872 | \begin{proof}[Proof sketch] 873 | By induction on the typing derivation of $\Gamma \vdash t : A$. 874 | % \footnotesize 875 | % 876 | % For example, consider $\Gamma \vdash t\;u : B$. 877 | % Then, we have $\Gamma \vdash t : A \to B$ and $\Gamma \vdash u : A$ for some $A$. 878 | % By the induction hypothesis, 879 | % \[ 880 | % (\Brackets{\Gamma \vdash u : A}(\rho_1), \Brackets{\Gamma \vdash u : A}(\rho_2)) 881 | % \;\text{and}\; 882 | % (\Brackets{\vdash t : A \to B}(\rho_1), \Brackets{\vdash t : A \to B}(\rho_2)) 883 | % \] 884 | % are related by $R^A$ and $R^{A \to B}$, respectively. 885 | % By definition, $\Brackets{\vdash t : A \to B}(\rho_1)(\Brackets{\Gamma \vdash u : A}(\rho_1))$ and $\Brackets{\vdash t : A \to B}(\rho_2)(\Brackets{\Gamma \vdash u : A}(\rho_2))$ are related by $R^B$. 886 | % Hence, \eqref{eq:fundamental_thm} holds for $\Gamma \vdash t\;u : B$. 887 | \end{proof} 888 | In particular, $R^A(\Brackets{t}, \Brackets{t})$ for any closed term $t$ of type $A$. 889 | \end{frame} 890 | 891 | \begin{frame}{Quiz, Revisited} 892 | Consider $O_X = \{\btt, \bff\}$ and the logical relation $\{R^A\}_{A}$ determined by 893 | \[ 894 | R^{X} = \{(\bff, \btt)\}. 895 | \] 896 | 897 | \begin{enumerate} 898 | \item Suppose that the constant function $f(x) = \btt$ is $\lambda$-definable, then $R^{X \to X}(\Brackets{t}, \Brackets{t})$ by the fundamental theorem. 899 | By definition of being logical $R^{X}(\Brackets{t}(\bff), \Brackets{t}(\btt))$, i.e.\ $R^{X}(\btt, \btt)$---a contradiction. 900 | That is, $f(x) = \btt$ is not $\lambda$-definable. 901 | \end{enumerate} 902 | \begin{exercise*} 903 | \begin{enumerate} 904 | \item Show that the constant function $f(x) = \bff$ is not $\lambda$-definable. 905 | \item Show that the negation function $\neg$ is not $\lambda$-definable. 906 | \end{enumerate} 907 | \end{exercise*} 908 | \end{frame} 909 | 910 | \begin{frame}{No set-theoretic model for polymorphic $\lambda$-calculus} 911 | We would like to apply the same approach of arguing $\lambda$-definability to polymorphic $\lambda$-calculus, but it is apparently circular: 912 | \begin{enumerate} 913 | \item the universal quantification $\forall X. A$ is \alert{impredicative} and 914 | \item $\Brackets{\forall X. A}$ should depend on $\Brackets{A \subst{B}{X}}$ for any $B : \type$, 915 | \item including $B = \forall X. A$. 916 | \end{enumerate} 917 | 918 | In fact, there is no set-theoretic interpretation for polymorphic $\lambda$-calculus \cite{Reynolds1984} in classical set theory, due to the \emph{cardinality issue}. 919 | 920 | Thus, we have to consider \emph{other models} rather than sets, some constructive set theory~\cite{Pitts1987}, or a weaker but predicative version of parametric polymorphism~\cite{Leivant1991}. 921 | \end{frame} 922 | 923 | \begin{frame}{Parametricity in polymorphic $\lambda$-calculus} 924 | Following Girard's \emph{reducibility candidate}~\cite{Girard1989}, assume a set $\mathcal{U}$ of \alert{relation candidates} in some model. 925 | A family of $\{ R_{\Phi}^{A} \}_{\Delta \vdash A}$ is logical if 926 | \begin{align*} 927 | R^{X}_{\Phi}(x_1, x_2) & \quad\text{iff} \quad\Phi(X)(x_1, x_2) \\ 928 | R^{A \to B}_{\Phi}(f_1, f_2) & \quad \text{iff} \quad \forall x_1 x_2.\, R^{A}_{\Phi}(x_1, x_2) \implies R^{B}_{\Phi}(f_1(x_1), f_2(x_2)) \\ 929 | R^{\forall X.\, A}_{\Phi}(x_1, x_2) & \quad\text{iff}\quad \alert{\forall U \in \mathcal{U}.\, R^{A}_{\Phi; X \mapsto U}(x_1, x_2)} 930 | \end{align*} 931 | where $\Phi\colon \Delta \to \mathcal{U}$ is a map and $\Phi; X \mapsto U$ means a map s.t.\ $Y$ is mapped to $U$ if $Y = X$ or $\Phi(Y)$ otherwise. 932 | 933 | If $\Delta$ is empty, then the subscript $\Phi$ in $R^A_{\Phi}$ is omitted, i.e.\ $R^A$ instead. 934 | \begin{theorem} 935 | The fundamental theorem holds for logical relations i.e.\ $R^A(\Brackets{t}, \Brackets{t})$ holds for any closed term $t$ of type $A$ in polymorphic $\lambda$-calculus. 936 | \end{theorem} 937 | 938 | \end{frame} 939 | 940 | \begin{frame}{Examples: $\forall X.\, X$} 941 | The type $\forall X.\, X$ is not inhabited. 942 | 943 | Suppose that $\vdash t : \forall X.\, X$. 944 | Then, by the fundamental theorem, 945 | \[ 946 | R^{\forall X.\, X}(\Brackets{t}, \Brackets{t}). 947 | \] 948 | By definition, $R^{\forall X.\, X}(\Brackets{t}, \Brackets{t})$ if and only if 949 | \[ 950 | \forall U \in \mathcal{U}.\, R^X_{X \mapsto U}(\Brackets{t}, \Brackets{t}) 951 | \quad\text{or equivalently,}\quad 952 | \forall U \in \mathcal{U}.\, U(\Brackets{t}, \Brackets{t}) 953 | \] 954 | Choosing $U$ to be the empty relation $\emptyset$, 955 | \[ 956 | (\Brackets{t}, \Brackets{t}) \in \emptyset, 957 | \] 958 | a contradiction. 959 | Hence, there is \emph{no} closed term of type $\forall X.\, X$. 960 | \end{frame} 961 | 962 | \begin{frame}{Theorems for Free} 963 | Consider the case that $R^X$ is instantiated as $\{\,(x, f(x)) \mid x \in A \,\}$ of some $f\colon A \to B$ and apply the fundamental theorem to derive, e.g., 964 | \begin{itemize} 965 | \item the following equation for any $t : \forall X.\, \List(X) \to \List(X)$: 966 | \[ 967 | \xymatrix{ 968 | \Brackets{\List(A)} \ar[r]^{\Brackets{t}_A} \ar[d]_{\mathtt{map}\;f} & \Brackets{\List(A)} \ar[d]^{\mathtt{map}\;f} \\ 969 | \Brackets{\List(B)} \ar[r]_{\Brackets{t}_B }& \Brackets{\List(B)} 970 | } 971 | \] 972 | \end{itemize} 973 | N.B.\ The equation is derived in the working model, not necessarily implying $=_{\beta}$ between $\lambda$-terms. 974 | 975 | The fundamental theorem is well known for this specialised form, dubbed as \emph{free theorems}~\cite{Wadler1989}. 976 | \end{frame} 977 | 978 | %\section{Nameless Representation} 979 | % 980 | %\begin{frame}{Capture-avoiding but ill-defined substitution} 981 | % The definition of capture-avoiding substitution is not well-defined. 982 | % Recall that 983 | % \begin{align*} 984 | % x\subst{L}{x} & = L \\ 985 | % y\subst{L}{x} & = y && \text{if $x \neq y$} \\ 986 | % (M\, N)\subst{L}{x} & = M\subst{L}{x}\; N\subst{L}{x} \\ 987 | % (\lambda x.\, M)\subst{L}{x} & = \lambda x.\, M \\ 988 | % (\lambda y.\, M)\subst{L}{x} & = \lambda y.\, M\subst{L}{x} &&\text{if $x \neq y$ and $y \not\in \FV(L)$} 989 | % \end{align*} 990 | % The function $\_\subst{L}{x}\colon \term_V \to \term_V$ is not total, so it is \alert{not} an instance of \emph{structural recursion} (i.e.\ $\texttt{fold}$). 991 | % In what sense, is the above well-defined? 992 | % 993 | % \begin{enumerate} 994 | % \item Use \emph{nominal technique} and the notion of $\alpha$-structure recursion/induction. 995 | % It requires some elements of group theory. 996 | % \item Use \emph{nameless} representation. 997 | % \end{enumerate} 998 | % 999 | %\end{frame} 1000 | % 1001 | %\begin{frame}[allowframebreaks]{Well-Scoped de Bruijn index representation} 1002 | % An index $i$ starting from $0$ is used as a variable to represent the $i$-th enclosing $\lambda$ (binder) `from the inside out'. 1003 | % For example, a term with named variables 1004 | % \[ 1005 | % \lambda a.\,\lambda b.\, (\lambda c.\, c) \;(\lambda c.\, a\;b) 1006 | % \] 1007 | % becomes 1008 | % \[ 1009 | % {\color{blue}\lambda}\, {\color{red}\lambda}\, (\lambda\,0) \;(\lambda\,{\color{blue} 2}\;{\color{red}1}) 1010 | % \] 1011 | % Hint.\ It may be easier to think of a term in its tree representation. 1012 | % \begin{definition}[de Bruijn representation with a local scope] 1013 | % The term formation \fbox{$t\quad \term_n$} is defined inductively for $n \in \mathbb{N}$ by 1014 | % \begin{minipage}{.45\textwidth} 1015 | % \begin{prooftree} 1016 | % \AXC{$0 \leq i < n$} 1017 | % \UIC{$i \quad \term_{n}$} 1018 | % \end{prooftree} 1019 | % \end{minipage} 1020 | % \begin{minipage}{.45\textwidth} 1021 | % \begin{prooftree} 1022 | % \AXC{$t \quad \term_{n + 1}$} 1023 | % \UIC{$\lambda\, t \quad \term_n$} 1024 | % \end{prooftree} 1025 | % \end{minipage} 1026 | % \begin{prooftree} 1027 | % \AXC{$t \quad \term_{n}$} 1028 | % \AXC{$u \quad \term_{n}$} 1029 | % \BIC{$t\;u \quad \term_{n}$} 1030 | % \end{prooftree} 1031 | % \fbox{$t\quad \term_n$} means $t$ has at most $n$ many free variables. 1032 | % \end{definition} 1033 | % 1034 | %\end{frame} 1035 | % 1036 | %\begin{frame}{Exercise} 1037 | % Translate the following terms to its de Bruijn index representation. 1038 | % \begin{enumerate} 1039 | % \item $\lambda x.\, x$ 1040 | % \item $\lambda s.\,\lambda z.\, s \;z$ 1041 | % \item $\lambda a.\,\lambda b.\, a\;(\lambda c.\, a\; b)$ 1042 | % \item $(\lambda x.\, x)\;(\lambda y.\, y)$ 1043 | % \item $\lambda x.\, y$ 1044 | % \item $x\;y\;z$ 1045 | % \end{enumerate} 1046 | %\end{frame} 1047 | % 1048 | %\begin{frame}{Substitution, revisited} 1049 | % How to reformulate $\beta$-reduction for terms in de Bruijn representation? 1050 | % Consider 1051 | % \[ 1052 | % \left({\color{blue}\lambda}\, {\color{red}\lambda}\,(\lambda\,0) \;(\lambda\,{\color{blue} 2}\;{\color{red}1})\right)\;t 1053 | % \longrightarrow_{\beta} 1054 | % \left({\color{red}\lambda}\, \;(\lambda\,0) \;(\lambda\,{\color{blue} 2}\;{\color{red}1})\right)\subst{t}{\color{blue}0} 1055 | % \] 1056 | % 1057 | % The de Bruijn index increments under a binder so $\subst{t}{i}$ should be $\subst{t'}{i+1}$ where $t'$ is the result of incrementing every index in $t$, e.g., 1058 | % \begin{align*} 1059 | % \left({\color{red}\lambda}\, (\lambda\,0) \;(\lambda\,{\color{blue} 2}\;{\color{red}1})\right)\subst{t}{\color{blue}0} 1060 | % & = 1061 | % {\color{red}\lambda}\, (\lambda\,0)\subst{t'}{\color{blue}1} \quad(\lambda\,{\color{blue} 2}\;{\color{red}1})\subst{t'}{1} \\ 1062 | % & = 1063 | % {\color{red}\lambda}\, (\lambda\,0\subst{t''}{\color{blue}2}) \quad(\lambda\,\left({\color{blue} 2}\;{\color{red}1}\right)\subst{t''}{2}) \\ 1064 | % & = 1065 | % {\color{red}\lambda}\, (\lambda\,0) \quad(\lambda\, {\color{blue} 2}\subst{t''}{2}\;{\color{red}1}\subst{t''}{2}) \\ 1066 | % & = 1067 | % {\color{red}\lambda}\, (\lambda\,0) \quad(\lambda\, t''\;{\color{red}1}) 1068 | % \end{align*} 1069 | % 1070 | %\end{frame} 1071 | % 1072 | % 1073 | %\begin{frame}{Simultaneous variable renaming} 1074 | % \begin{definition} 1075 | % A \emph{(variable) renaming} is a function $\rho$ between $\mathbb{Z}_n$ and $\mathbb{Z}_m$. 1076 | % \end{definition} 1077 | % Every renaming $\rho\colon \mathbb{Z}_n \to \mathbb{Z}_m$ extends to an action on terms: 1078 | % \begin{align*} 1079 | % \left< \rho \right> i & = \rho(i) \\ 1080 | % \left< \rho \right> \left(t\;u\right) & = \left<\rho\right>t \; \left<\rho\right>u \\ 1081 | % \left< \rho \right> \lambda\,t & = \lambda\left< \rho' \right> t 1082 | % \end{align*} 1083 | % where $\rho' \colon \mathbb{Z}_{n+1} \to \mathbb{Z}_{m+1}$ is defined as 1084 | % \begin{align*} 1085 | % \rho'(0) & = 0 \\ 1086 | % \rho'(1 + i) & = 1 + \rho(i) 1087 | % \end{align*} 1088 | % to avoid changing bound variables. 1089 | % 1090 | % In particular, $\mathit{wk}\colon \term_{n} \to \term_{n+1}$ derived by $i \mapsto i+1 \in \mathbb{Z}_{n+1}$ increments every index of a free variable by $1$. 1091 | %\end{frame} 1092 | % 1093 | %\begin{frame}{Simultaneous substitution} 1094 | % \begin{definition} 1095 | % A \emph{(simultaneous) substitution} is a function $\sigma$ from $\mathbb{Z}_n$ to $\term_m$. 1096 | % \end{definition} 1097 | % Every substitution extends to an action terms: 1098 | % \begin{align*} 1099 | % \left< \sigma \right> i & = \sigma(i) \\ 1100 | % \left< \sigma \right> \left(t\;u\right) & = \left<\sigma\right>t \; \left<\sigma\right>u \\ 1101 | % \left< \sigma \right> \lambda\,t & = \lambda\left< \sigma' \right> t 1102 | % \end{align*} 1103 | % where $\sigma' \colon \mathbb{Z}_{n+1} \to \term_{m+1}$ is defined as 1104 | % \begin{align*} 1105 | % \sigma'(0) & = 0 \\ 1106 | % \sigma'(1 + i) & = \mathit{wk}\left(\sigma(i)\right) 1107 | % \end{align*} 1108 | %\end{frame} 1109 | % 1110 | %\begin{frame}{Single substitution} 1111 | % \begin{definition} 1112 | % A \emph{single substitution} for $t$ is a simultaneous substitution given by 1113 | % \begin{align*} 1114 | % \sigma \colon \mathbb{Z}_{1+n} & \to \mathbb{Z}_{n} \\ 1115 | % \sigma(0) & = t \\ 1116 | % \sigma(1+i) & = i 1117 | % \end{align*} 1118 | % \end{definition} 1119 | %\end{frame} 1120 | % 1121 | %\begin{frame}{Exercise} 1122 | % \begin{enumerate} 1123 | % \item Adopt $\alpha$-equivalence to the de Bruijn representation. 1124 | % \item Adopt $\beta$-equivalence to the de Bruijn representation. 1125 | % \item Apply the new definition of substitution to compute $\mathtt{not}\;\mathtt{True}$. 1126 | % \item Adopt the definitions of renaming and substitution to the de Bruijn level representation. 1127 | % N.B.\ we may also count the $i$-th enclosing binder `from the outside in' using the same definition, called \emph{the de Bruijn level}. 1128 | % \end{enumerate} 1129 | %\end{frame} 1130 | 1131 | %\begin{frame} 1132 | % $M \sim N : \mathcal{R}_{\sigma, \tau}$ is defined as follows. 1133 | % 1134 | % \begin{enumerate} 1135 | % \item $M \sim M : \mathcal{R}_{\mathbb{N}, \mathbb{N}}$ for every $M : 1136 | % \mathbb{N}$. 1137 | % 1138 | % \item For any $\mathcal{R}_{\tau, \sigma}$, 1139 | % \[ 1140 | % M_i \sim N_i : \mathcal{R}_{\sigma, \tau} \; \text{for any $M_i : \sigma$ and $N_i : \tau$} 1141 | % \iff 1142 | % [ M_1, \dots, M_k ] \sim [ N_1, \dots, N_k ] : \mathcal{R}_{\List\;\sigma, \List\;\tau} 1143 | % \] 1144 | % 1145 | % \item For any $\mathcal{R}_{\sigma, \sigma'}$ and $\mathcal{S}_{\tau, \tau'}$, 1146 | % $M \sim M' : \mathcal{R}_{\sigma, \sigma'} \implies L\; M 1147 | % \sim L'\;M' : \mathcal{S}_{\tau, \tau'}$ for any $M$ and $M'$ 1148 | % if and only if 1149 | % $L \sim L' : \mathcal{R}\to\mathcal{S}_{\sigma \to \tau, \sigma' \to \tau'}$ 1150 | % 1151 | % \item Assume $\mathcal{F}$ which sends $\mathcal{R}_{\sigma, \tau}$ to 1152 | % $\mathcal{F}\mathcal{R}_{F\sigma, F\tau}$. Then 1153 | % $f\;\sigma \sim g\;\tau : \mathcal{F}\mathcal{R}$ for all $\mathcal{R}_{\sigma, \tau}$ 1154 | % if and only if $f \sim g : \forall \mathcal{X}_{t, u}.\, {\mathcal{F}\mathcal{X}}_{Ft, Fu}$ 1155 | % \end{enumerate} 1156 | %\end{frame} 1157 | 1158 | %\begin{frame}{System $F_\omega$} 1159 | % Recall that for $\sigma, \tau \in \type$ the sum type of $\sigma$ and $\tau$ 1160 | % is 1161 | % \[ 1162 | % \sigma + \tau \defeq \forall t. (\sigma \to t) \to (\tau \to t) \to t 1163 | % \] 1164 | % Can we internalise this \alert{type construction}? 1165 | % \vfill 1166 | % \alert{\emph{Kinds}} are like \emph{classes} in set theory: 1167 | % \begin{enumerate} 1168 | % \item $*$ : the kind of types 1169 | % \item $* \Rightarrow *$ : the kind of type operators, e.g., 1170 | % $a \mapsto [a]$ 1171 | % \item \ldots 1172 | % \end{enumerate} 1173 | % System $F_\omega$ is an extension of System $F$ with type-level functions and 1174 | % kinds. See \cite{Pierce2002} for further detail. 1175 | %\end{frame} 1176 | % 1177 | %\begin{frame}[allowframebreaks]{Impredicativity} 1178 | % A definition is \alert{\emph{impredicative}} if it has a quantifier whose 1179 | % domain includes itself (which is being defined). 1180 | % \begin{block}{Russell's Paradox} 1181 | % \[ 1182 | % R \defeq \set{x}{ x \not\in x } 1183 | % \] 1184 | % \end{block} 1185 | % Recall the self-application ... 1186 | % \[ 1187 | % (\Lambda t.\,\lambda (x: t). x)\;{\color{red}(\forall t.\,t \to t)}\; 1188 | % (\Lambda t.\, \lambda (x: t). x) 1189 | % \] 1190 | % which is actually impredicative! 1191 | % This form of polymorphism is called \alert{\emph{impredicative polymorphism}}. 1192 | % \begin{block}{Girard's Paradox} 1193 | % An encoding of Russell's paradox in extensions of 1194 | % System F. 1195 | % \end{block} 1196 | % 1197 | % Martin-L\"of's type theory (on which Agda is based) was inconsistent, as it 1198 | % included an axiom\footnote{It is only a simplified story...}: 1199 | % \[ 1200 | % \mathsf{Set} : \mathsf{Set} 1201 | % \] 1202 | % 1203 | % \begin{block}{Inconsistency of Haskell} 1204 | % Impredicativity $+$ Injectivity $+$ Type Case Analysis 1205 | % $=$ Inconsistency (Russell paradox) 1206 | % \footnote{\url{http://okmij.org/ftp/Haskell/impredicativity-bites.html}} 1207 | % 1208 | % \end{block} 1209 | %\end{frame} 1210 | 1211 | \begin{frame}{Homework} 1212 | \begin{enumerate} 1213 | \item (2.5\%) Define $\mathtt{length}_\sigma : \List\;\sigma \to \nat$ calculating the length of a list in polymorphic $\lambda$-calculus. 1214 | \item (5\%) Prove Theorem~\ref{thm:fundamental_thm}. 1215 | \end{enumerate} 1216 | 1217 | \end{frame} 1218 | 1219 | \begin{frame}{Acknowledgement} 1220 | I would like to thank 薛盛安, 曾顯恩, 蔡子濬 (Gene Tsai), 游書泓, and many other students at FLOLAC for their efforts in reading and correcting the material carefully. 1221 | \end{frame} 1222 | 1223 | \begin{frame}[allowframebreaks]{References} 1224 | \setbeamerfont{bibliography item}{size=\footnotesize} 1225 | \setbeamerfont{bibliography entry author}{size=\footnotesize} 1226 | \setbeamerfont{bibliography entry title}{size=\footnotesize} 1227 | \setbeamerfont{bibliography entry location}{size=\footnotesize} 1228 | \setbeamerfont{bibliography entry note}{size=\footnotesize} 1229 | \bibliographystyle{apalike} 1230 | \bibliography{ref} 1231 | \end{frame} 1232 | 1233 | 1234 | \end{document} 1235 | 1236 | --------------------------------------------------------------------------------