├── chapters ├── intro │ ├── link.sh │ ├── caller.ml │ ├── Makefile │ ├── head.ml │ ├── head-eg.v │ └── main.ml ├── param-cc.tex ├── anf.tex ├── cps.tex ├── abs-cc.tex ├── cps │ ├── cont-shuffle.v │ ├── intro.tex │ ├── related.tex │ ├── ideas.tex │ └── ecc.tex ├── source-appendix.tex ├── param-cc │ ├── intro.tex │ └── ideas.tex ├── anf │ ├── intro.tex │ ├── ideas.tex │ └── discussion.tex ├── abs-cc │ ├── intro.tex │ └── ideas.tex ├── beyond.tex ├── abs-cc-appendix.tex ├── param-cc-appendix.tex ├── cps-appendix.tex └── anf-appendix.tex ├── frontbackmatters ├── todos.tex ├── toc.tex ├── bib.tex ├── listoffigs.tex ├── PhDThesisApproval.pdf ├── abstract.tex ├── signatures.tex ├── dedication.tex ├── poem.tex ├── support.tex ├── titlepage.tex ├── conventions.tex └── acks.tex ├── .gitmodules ├── README.md ├── Makefile ├── abstract.tex ├── tech.tex ├── dissertation.tex ├── config.tex └── defs.tex /chapters/intro/link.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cat $1 $2 > $3 4 | -------------------------------------------------------------------------------- /chapters/intro/caller.ml: -------------------------------------------------------------------------------- 1 | let _ = applyer 1 (Cons (0, O, Nil)) (Cons ((fun x -> S x), O, Nil)) -------------------------------------------------------------------------------- /frontbackmatters/todos.tex: -------------------------------------------------------------------------------- 1 | \phantomsection 2 | \pdfbookmark[1]{TODOs}{TODOs} 3 | \todos 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "mttex"] 2 | path = mttex 3 | url = https://github.com/wilbowma/mttex.git 4 | -------------------------------------------------------------------------------- /frontbackmatters/toc.tex: -------------------------------------------------------------------------------- 1 | \phantomsection 2 | \pdfbookmark[1]{Contents}{Contents} 3 | \tableofcontents 4 | -------------------------------------------------------------------------------- /frontbackmatters/bib.tex: -------------------------------------------------------------------------------- 1 | \phantomsection 2 | \pdfbookmark[1]{Bibliography}{Bibliography} 3 | \bibliography{bib} 4 | -------------------------------------------------------------------------------- /frontbackmatters/listoffigs.tex: -------------------------------------------------------------------------------- 1 | \phantomsection 2 | \pdfbookmark[1]{List of Figures}{List of Figures} 3 | \listoffigures 4 | -------------------------------------------------------------------------------- /frontbackmatters/PhDThesisApproval.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wilbowma/dissertation/HEAD/frontbackmatters/PhDThesisApproval.pdf -------------------------------------------------------------------------------- /frontbackmatters/abstract.tex: -------------------------------------------------------------------------------- 1 | \pdfbookmark[1]{Abstract}{Abstract} 2 | \chapter*{Abstract} 3 | \label{chp:abstract} 4 | \input{abstract} 5 | -------------------------------------------------------------------------------- /frontbackmatters/signatures.tex: -------------------------------------------------------------------------------- 1 | \pdfbookmark[1]{Thesis Approval}{Thesis Approval} 2 | \includepdf[pages=-]{frontbackmatters/PhDThesisApproval.pdf} 3 | -------------------------------------------------------------------------------- /chapters/intro/Makefile: -------------------------------------------------------------------------------- 1 | all: test 2 | 3 | head.ml: head-eg.v 4 | coqtop -l $< -batch 5 | 6 | main.ml: head.ml caller.ml 7 | ./link.sh head.ml caller.ml main.ml 8 | 9 | test: main.ml 10 | @-ocaml main.ml 11 | 12 | clean: 13 | /bin/rm main.ml head.ml *.mli 14 | -------------------------------------------------------------------------------- /chapters/param-cc.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{param-cc} 2 | 3 | \chapter{Parametric Closure Conversion} 4 | \label{chp:param-cc} 5 | \input{chapters/param-cc/intro} 6 | \input{chapters/param-cc/ideas} 7 | \input{chapters/param-cc/coc-cc} 8 | \input{chapters/param-cc/cc} 9 | -------------------------------------------------------------------------------- /chapters/anf.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{anf} 2 | 3 | \chapter{A-normal Form} 4 | \label{chp:anf} 5 | \renewcommand{\tlang}{\anftlang} 6 | \input{chapters/anf/intro} 7 | \input{chapters/anf/ideas} 8 | \input{chapters/anf/target} 9 | \input{chapters/anf/translation} 10 | \input{chapters/anf/discussion} 11 | -------------------------------------------------------------------------------- /chapters/cps.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{cps} 2 | 3 | \chapter{Continuation-Passing Style} 4 | \label{chp:cps} 5 | \input{chapters/cps/intro} 6 | \input{chapters/cps/ideas} 7 | \input{chapters/cps/ecc} 8 | \input{chapters/cps/ecccps} 9 | \input{chapters/cps/cbn} 10 | \input{chapters/cps/cbv} 11 | \input{chapters/cps/related} 12 | -------------------------------------------------------------------------------- /chapters/abs-cc.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{abs-cc} 2 | \renewcommand{\tstepjudg}{\sstepjudg} 3 | \renewcommand{\tlang}{\abscctlang} 4 | 5 | \chapter{Abstract Closure Conversion} 6 | \label{chp:abs-cc} 7 | \input{chapters/abs-cc/intro} 8 | \input{chapters/abs-cc/ideas} 9 | \input{chapters/abs-cc/target} 10 | \input{chapters/abs-cc/cc} 11 | -------------------------------------------------------------------------------- /frontbackmatters/dedication.tex: -------------------------------------------------------------------------------- 1 | \thispagestyle{empty} 2 | \phantomsection 3 | \pdfbookmark[1]{Dedication}{Dedication} 4 | 5 | \vspace*{4cm} 6 | 7 | \begin{center} 8 | \noindent Dedicated to Daniel P. Friedman. 9 | \end{center} 10 | 11 | \bigskip 12 | 13 | \begin{center} 14 | \begin{flushright} 15 | ``No, I'm not going to grad school; I can't even imagine writing a dissertation.''\\ 16 | --- Me, responding to Dan 17 | \end{flushright} 18 | \end{center} 19 | -------------------------------------------------------------------------------- /frontbackmatters/poem.tex: -------------------------------------------------------------------------------- 1 | \pdfbookmark[1]{A Poem}{A Poem} 2 | 3 | \chapter*{One Road Should Not Be Taken} 4 | \begin{flushleft} 5 | Two roads diverged in a blue and red wood,\\ 6 | A colorful forest of derivation trees.\\ 7 | I'm no traveler; I, therefore, could---\\ 8 | and, as a scientist, because I should---\\ 9 | followed each one into its leaves.\\[6pt] 10 | 11 | In one, I found the theory fair;\\ 12 | This I suggest; the other is worse.\\ 13 | In that, the types, one's mind ensnare;\\ 14 | Though it can work, I hereby declare.\\ 15 | Really, I beg you, just use the first. 16 | \end{flushleft} 17 | -------------------------------------------------------------------------------- /frontbackmatters/support.tex: -------------------------------------------------------------------------------- 1 | \pdfbookmark[1]{Support}{Support} 2 | \begingroup 3 | \let\clearpage\relax 4 | \let\cleardoublepage\relax 5 | \let\cleardoublepage\relax 6 | \chapter*{Support} 7 | This work was funded by the National Science Foundation under grants NSF 8 | CCF-1203008, NSF CCF-1422133, and NSF CCF-1453796, and by 9 | the European Research Council under ERC Starting Grant SECOMP (715753) 10 | Any opinions, findings, and conclusions or recommendations expressed in this 11 | material are those of the author and do not necessarily reflect the views of the 12 | funding agencies. 13 | \endgroup 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Compiling with Dependent Types (sources) 2 | == 3 | These are the source files for building my dissertation. 4 | 5 | Building this requires minted, Coq, OCaml, and maybe other things. 6 | 7 | License 8 | == 9 | You may use figures and examples with acknowledgment. 10 | You may use any code freely. 11 | 12 | ## Dependencies 13 | Some (maybe not all) dependencies 14 | `tlmgr install iwona cm-super acmart todo xpatch xstring appendix framed fvextra catchfile ifnextok cleveref minted tocloft classicthesis bera mparhack titlesec mathpartir placeins chngcntr stmarysd todo titlecaps environ enumitem tikz-cd mfirstuc doi totpages was upgreek ...` 15 | -------------------------------------------------------------------------------- /chapters/intro/head.ml: -------------------------------------------------------------------------------- 1 | 2 | type __ = Obj.t 3 | let __ = let rec f _ = Obj.repr f in Obj.repr f 4 | 5 | type nat = 6 | | O 7 | | S of nat 8 | 9 | type nat0 = nat 10 | 11 | type 'a list = 12 | | Nil 13 | | Cons of 'a * nat0 * 'a list 14 | 15 | (** val head : nat0 -> 'a1 list -> __ **) 16 | 17 | let head _ = function 18 | | Nil -> Obj.magic __ 19 | | Cons (h, _, _) -> Obj.magic h 20 | 21 | (** val valid : nat0 -> nat0 **) 22 | 23 | let valid = 24 | Obj.magic head O (Cons ((fun x -> x), O, Nil)) 25 | 26 | (** val applyer : nat0 -> (nat0 -> nat0) list -> nat0 list -> nat0 **) 27 | 28 | let applyer n fs ns = 29 | Obj.magic head n fs (head n ns) 30 | -------------------------------------------------------------------------------- /chapters/intro/head-eg.v: -------------------------------------------------------------------------------- 1 | Require Extraction. 2 | Definition Nat := nat. 3 | 4 | Inductive List (A : Set) : Nat -> Set := 5 | | nil : List A 0 6 | | cons : forall (h:A) (n:Nat), List A n -> List A (1 + n). 7 | 8 | Definition head {A} {n : Nat} (v : List A (1 + n)) := 9 | match v with 10 | | cons _ h _ t => h 11 | end. 12 | 13 | (* Example invalid := head nil. *) 14 | Example valid : (Nat -> Nat) := head (cons _ (fun x => x) 0 (nil _)). 15 | Print valid. 16 | 17 | Definition applyer {n : Nat} (fs : List (Nat -> Nat) (1 + n)) 18 | (ns : List Nat (1 + n)) := 19 | (head fs) (head ns). 20 | 21 | Extraction "head.ml" head valid applyer. -------------------------------------------------------------------------------- /chapters/intro/main.ml: -------------------------------------------------------------------------------- 1 | 2 | type __ = Obj.t 3 | let __ = let rec f _ = Obj.repr f in Obj.repr f 4 | 5 | type nat = 6 | | O 7 | | S of nat 8 | 9 | type nat0 = nat 10 | 11 | type 'a list = 12 | | Nil 13 | | Cons of 'a * nat0 * 'a list 14 | 15 | (** val head : nat0 -> 'a1 list -> __ **) 16 | 17 | let head _ = function 18 | | Nil -> Obj.magic __ 19 | | Cons (h, _, _) -> Obj.magic h 20 | 21 | (** val valid : nat0 -> nat0 **) 22 | 23 | let valid = 24 | Obj.magic head O (Cons ((fun x -> x), O, Nil)) 25 | 26 | (** val applyer : nat0 -> (nat0 -> nat0) list -> nat0 list -> nat0 **) 27 | 28 | let applyer n fs ns = 29 | Obj.magic head n fs (head n ns) 30 | let _ = applyer 1 (Cons (0, O, Nil)) (Cons ((fun x -> S x), O, Nil)) -------------------------------------------------------------------------------- /chapters/cps/cont-shuffle.v: -------------------------------------------------------------------------------- 1 | Declare ML Module "paramcoq". 2 | 3 | Variable A : Prop. 4 | Variable B : Prop. 5 | 6 | Variable R_b : B -> B -> Prop. 7 | 8 | Realizer A as A_r arity 2 := (fun a1 a2 => a1 = a2). 9 | Realizer B as B_r arity 2 := R_b. 10 | 11 | Definition cpsT := forall (α:Prop), (B -> α) -> α. 12 | 13 | Parametricity cpsT. 14 | 15 | Definition id := fun (x : B) => x. 16 | 17 | Definition kT := (B -> A). 18 | 19 | Parametricity kT. 20 | 21 | (* Note that by the fundamental property, the premises are trivially 22 | satisfied, but the translation really wants an honest-to-goodness 23 | term and not a variable. *) 24 | Theorem Cont_Shuffle : forall e k, (cpsT_R e e) -> (kT_R k k) -> 25 | ((e A k) = (k (e B id))). 26 | Proof. 27 | intros e k P_e P_k. 28 | unfold cpsT_R in P_e. 29 | (* The only clever part: instantiate the relation between types 30 | A and B to be that a is related to (k b) in the A relation*) 31 | set (P := (P_e A B (fun (a:A) (b:B) => (A_r a (k b))) k id)). 32 | unfold id in P at 1. 33 | 34 | exact (P (fun b₁ b₂ H0 => (P_k b₁ b₂ H0))). 35 | Qed. -------------------------------------------------------------------------------- /chapters/source-appendix.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{srcapp} 2 | 3 | \chapter{Reference for \slang} 4 | \label{sec:source:appendix} 5 | This appendix contains the complete definitions of the semantics of \slang, in 6 | particular, its type system and evaluation semantics. 7 | All \slang figures from \fullref[]{chp:source} are reproduced and completed with 8 | elided parts here, and elided figures are presented here. 9 | 10 | Note that the observations depend on the particular pair languages and translation; 11 | Here, I give only \slang observations. 12 | In each target language, I will define the target language observation, and each 13 | translation gives the cross-language relation for observations. 14 | \begingroup 15 | \let\label\discard 16 | 17 | \FigECCSyntax[ht] 18 | \FigECCRed[ht] 19 | 20 | \endgroup 21 | 22 | \FigECCConvFull[ht] 23 | 24 | \begingroup 25 | \let\label\discard 26 | 27 | \FigECCEqv[ht] 28 | \FigECCSub[ht] 29 | 30 | \endgroup 31 | 32 | \FigECCTyping[ht] 33 | 34 | \begingroup 35 | \let\label\discard 36 | 37 | \FigECCObs[ht] 38 | \FigECCProg[ht] 39 | \FigECCEval[ht] 40 | \FigLinking[ht] 41 | 42 | \endgroup 43 | -------------------------------------------------------------------------------- /frontbackmatters/titlepage.tex: -------------------------------------------------------------------------------- 1 | \begin{titlepage} 2 | \pdfbookmark[1]{\myTitle}{titlepage} 3 | % if you want the titlepage to be centered, uncomment and fine-tune the line below (KOMA classes environment) 4 | \begin{addmargin}[-1cm]{-3cm} 5 | \begin{center} 6 | \large 7 | 8 | \hfill 9 | 10 | \vfill 11 | 12 | \begingroup 13 | \color{CTtitle}\spacedallcaps{\myTitle} \\ \bigskip 14 | \endgroup 15 | 16 | \spacedlowsmallcaps{\myName} 17 | 18 | \vfill 19 | 20 | % \mySubtitle \\ \medskip 21 | \myDegree \\ 22 | \myDepartment \\ 23 | % \myFaculty \\ 24 | \myUni \\ \bigskip 25 | 26 | \myTime%\ -- \myVersion 27 | 28 | \vfill 29 | 30 | \end{center} 31 | \end{addmargin} 32 | \end{titlepage} 33 | 34 | \thispagestyle{empty} 35 | 36 | \hfill 37 | 38 | \vfill 39 | 40 | \noindent\myName: \textit{\myTitle,} \myDegree, 41 | \textcopyright\ \myTime 42 | 43 | %\bigskip 44 | % 45 | %\noindent\spacedlowsmallcaps{Supervisors}: \\ 46 | %\myProf \\ 47 | %\myOtherProf \\ 48 | %\mySupervisor 49 | % 50 | %\medskip 51 | % 52 | %\noindent\spacedlowsmallcaps{Location}: \\ 53 | %\myLocation 54 | % 55 | %\medskip 56 | % 57 | %\noindent\spacedlowsmallcaps{Time Frame}: \\ 58 | %\myTime 59 | -------------------------------------------------------------------------------- /frontbackmatters/conventions.tex: -------------------------------------------------------------------------------- 1 | \pdfbookmark[1]{Typographical Conventions}{Typographical Conventions} 2 | \chapter*{Typographical Conventions} 3 | I use a combination of colors and fonts to make distinctions between terms in 4 | different languages clear. 5 | Often, programs from more than one language will appear in the same equation. 6 | While which language each program belongs to will be clear from the structure of 7 | the equations, I use colors and fonts to make the distinction more apparent. 8 | These colors and fonts are chosen to be distinguishable in color, in grayscale, 9 | and with various forms of color blindness. 10 | 11 | Each chapter will involve at least two languages: a source and a target 12 | language. 13 | Source languages expressions and metavariables are typeset in a \sfonttext{blue, 14 | non-bold, sans-serif font}. 15 | Target language expressions and metavariables are typeset in \tfonttext{bold, 16 | red, serif font}. 17 | These different fonts do not indicate \emph{particular} languages---they only 18 | indicate source and target. 19 | I will remind the reader which language is which each time a new language is 20 | introduced. 21 | 22 | For languages other than source and target languages, such as examples of 23 | concepts or from related work, I use a {black, non-bold, serif font}. 24 | 25 | I use emphasis to introduce a word with a \deftech{technical definition}---\ie, 26 | a specific technical use that may differ from the normal English definition. 27 | 28 | \begin{typographical} 29 | I will remind the reader of these typographical conventions in the section I 30 | first use them, or when languages change, in a typographical note like this. 31 | \end{typographical} 32 | -------------------------------------------------------------------------------- /chapters/param-cc/intro.tex: -------------------------------------------------------------------------------- 1 | \label{sec:param-cc:intro} 2 | In this chapter, I develop a \deftech{parametric closure conversion} 3 | translation, \ie, a \tech{closure conversion} translation that does not add 4 | primitive \tech{closures} to the target language but instead encodes them using 5 | \tech{existential types}. 6 | As discussed in \fullref[]{chp:abs-cc}, this translation is not suitable in all 7 | \tech{dependent type} theories, since it relies on \tech{parametricity} and 8 | \tech{impredicativity}. 9 | However, parametric \tech{closure conversion} has some advantages over \tech{abstract 10 | closure conversion} which makes studying this translation worthwhile. 11 | In particular, known optimizations for recursive \tech{closures} that avoid 12 | rebuilding the \tech{closure} every time through the loop use \tech{parametric 13 | closure conversion}~\cite{minamide1996,morrisett1998:reccc}. 14 | We can develop a \tech{parametric closure conversion} if we admit 15 | \tech{impredicativity} and \tech{parametricity}, and avoid \tech{higher 16 | universes}. 17 | 18 | I begin with a review of the problems with \tech{parametric closure conversion} 19 | discussed in \fullref[]{chp:abs-cc}. 20 | To accommodate the restrictions, the \tech{parametric closure conversion} 21 | translation is defined on \pccslang, the same source language used 22 | in \fullref[]{chp:cps}, and a restriction of \slang presented 23 | in \fullref[]{chp:source}. 24 | I then design a target language, \pcctlang, before developing the full 25 | translation, and proofs of type preservation and compiler correctness. 26 | 27 | \begin{typographical} 28 | In this chapter, I typeset the source language, \pccslang, in 29 | a \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target 30 | language, \pcctlang, in a \emph{\tfonttext{bold, red, serif font}}. 31 | \end{typographical} 32 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | export OUTPUT_DIR ?= $(abspath ./) 2 | DRAFT ?= false 3 | DEBUG ?= true 4 | 5 | ifeq ($(DEBUG), false) 6 | FASTER_FLAG = -latexoption="-interaction=batchmode" -interaction=batchmode 7 | else 8 | endif 9 | 10 | .PHONY: all clean distclean dissertation push final 11 | 12 | all: dissertation abstract.txt 13 | 14 | final: dissertation 15 | pdfopt.sh $(OUTPUT_DIR)/dissertation.pdf 16 | 17 | abstract.txt: abstract.tex 18 | detex $< > $@ 19 | 20 | push: 21 | # rsync -vz $(OUTPUT_DIR)/dissertation.pdf user@server:path/dissertation.pdf 22 | 23 | dissertation: $(OUTPUT_DIR)/dissertation.pdf 24 | 25 | mttex/%.sty: 26 | git submodule update --init mttex 27 | 28 | #ifeq (,$(wildcard ~/workspace/org/bib.bib)) 29 | #else 30 | #bib.bib: ~/workspace/org/bib.bib 31 | # if [[ ! -f bib.bib ]]; then touch bib.bib; fi 32 | # chmod +w bib.bib 33 | # cp -f ~/workspace/org/bib.bib . 34 | # chmod -w bib.bib 35 | #endif 36 | 37 | $(OUTPUT_DIR)/chapters $(OUTPUT_DIR)/frontbackmatters: 38 | mkdir -p $@ 39 | 40 | outputdir.tex: 41 | echo "\PassOptionsToPackage{outputdir=/tmp}{minted}" > $@ 42 | 43 | chapters/intro/head.ml: 44 | make -C chapters/intro/ 45 | 46 | $(OUTPUT_DIR)/%.pdf: %.tex outputdir.tex *.tex chapters/*.tex chapters/cps/cont-shuffle.v chapters/intro/head-eg.v chapters/intro/head.ml chapters/*/*.tex frontbackmatters/*.tex bib.bib mttex/mttex.sty $(OUTPUT_DIR)/chapters $(OUTPUT_DIR)/frontbackmatters 47 | ifeq ($(DRAFT), false) 48 | latexmk $(FASTER_FLAG) -latexoption="-halt-on-error -shell-escape" -auxdir=$(OUTPUT_DIR) -outdir=$(OUTPUT_DIR) -pdf $< 49 | else 50 | pdflatex -shell-escape -halt-on-error $(FASTER_FLAG) -output-directory=$(OUTPUT_DIR) $< 51 | endif 52 | 53 | clean: 54 | latexmk -auxdir=$(OUTPUT_DIR) -outdir=$(OUTPUT_DIR) -pdf -c 55 | rm -f $(OUTPUT_DIR)/*.{pyg,bbl} 56 | 57 | distclean: clean 58 | make -C chapters/intro clean 59 | latexmk -auxdir=$(OUTPUT_DIR) -outdir=$(OUTPUT_DIR) -pdf -C 60 | rm -rf $(OUTPUT_DIR)/_minted-dissertation 61 | -------------------------------------------------------------------------------- /chapters/anf/intro.tex: -------------------------------------------------------------------------------- 1 | In this chapter, I develop the first of the two front-end translations for a 2 | \tech{type-preserving} compiler for \slang: \tech{ANF} translation. 3 | The target language for this translation is \tlang, a variant of \slang 4 | syntactically restricted to \tech{ANF}. 5 | Recall from \fullref[]{chp:type-pres} that the first translation in the model 6 | compiler imposes a distinction between \tech{computation} and \tech{values}. 7 | A-normal form (\tech{ANF}) translation accomplishes this goal and can take the 8 | place of \tech{CPS} translation in the model compiler. 9 | (In fact, \tech{ANF} was introduced as the essence of 10 | \tech{CPS}~\cite{flanagan1993}.) 11 | 12 | \begin{digression} 13 | The \emph{A} in A-normal form has no further meaning. 14 | The \emph{A} comes from the set of axioms and reductions introduced by 15 | \citet{sabry1992} to reason about CPS. 16 | \citet{flanagan1993} use the \emph{A} as a label in a commutative diagram, 17 | observing that there should exist a direct translation that produces a normal 18 | form equivalent to the CPS/optimization/un-CPS process found in practice. 19 | The programs produced by this translation are in normal form with respect to 20 | the \emph{A}-reductions of \citeauthor{sabry1992}, \ie, 21 | in \emph{A}-normal form. 22 | \end{digression} 23 | 24 | I start by explaining the key problems that arise when preserving dependent 25 | types through \tech{ANF} translation and the main idea to my solution. 26 | I then move on to the formal development of the target language, the 27 | translation, and proofs of \tech{type preservation} and \tech{compiler 28 | correctness}. 29 | I conclude with a discussion of related problems, and a brief comparison 30 | of \tech{CPS} and \tech{ANF} in the context of dependent-type preservation, but 31 | leave a full discussion of \tech{CPS} until \fullref[]{chp:cps}. 32 | 33 | \begin{typographical} 34 | In this chapter, I define fixed source and target languages. 35 | I typeset the source language, \slang, in a \emph{\sfonttext{blue, non-bold, 36 | sans-serif font}}, and the target language, \tlang, in a 37 | \emph{\tfonttext{bold, red, serif font}}. 38 | \end{typographical} 39 | -------------------------------------------------------------------------------- /chapters/abs-cc/intro.tex: -------------------------------------------------------------------------------- 1 | In this chapter, I develop the second of the two front-end 2 | \tech{type-preserving} translations, a so-called \tech{abstract closure 3 | conversion} translation. 4 | \deftech*{abstract closure conversion,Abstract closure conversion}{Abstract closure conversion} 5 | produces \tech{closure converted} \tech{code} in which \tech{closures} are 6 | primitives, rather than encoded using a well-known datatype~\cite{minamide1996}. 7 | The goal of \deftech*{closure conversion,closure converted,closure 8 | converting,closure-converted}{closure conversion} is to close 9 | all \tech{computation} 10 | abstractions with respect to free variables so the definition of 11 | a \tech{computation} can be separate from its use. 12 | In \slang, the only \tech{computation} abstraction is \(\lambda\), so we will be 13 | translating all \(\lambda\) expressions into an explicit closure object. 14 | Intuitively, the abstract closure object essentially represents closed code 15 | partially applied to the local environment in which the code was defined. 16 | This is abstract compared to representations of closures as a pair of the code 17 | and environment, since a pair supports more operations (projection) than a 18 | closure. 19 | The abstract closure conversion ensures the only operation defined on closures 20 | is application. 21 | 22 | I begin by describing the key problems with type preserving closure 23 | conversion---particularly why the standard \tech{parametric closure conversion} 24 | fails---and the solutions, then develop the \tech{closure conversion} \tech{IL}, 25 | and finally prove \tech{type preservation} and \tech{compiler correctness}. 26 | As this pass is meant to follow \tech{ANF}, I also prove that \tech{ANF} is 27 | preserved. 28 | 29 | \begin{typographical} 30 | In this chapter, I typeset the source language, \slang, in 31 | a \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target 32 | language, \tlang, in a \emph{\tfonttext{bold, red, serif font}}. 33 | \end{typographical} 34 | 35 | \begin{digression} 36 | A variant of the translation presented in this chapter was independently discovered by \citet{kovacs2018:cconv}. That translation differs primarily in its use of universes ala Tarski. The author also spends some time discussing type-passing polymorphism, which I do not discuss. 37 | \end{digression} 38 | -------------------------------------------------------------------------------- /abstract.tex: -------------------------------------------------------------------------------- 1 | % To support generating a plain text version, this file can contain only plaintext, 2 | % comments, and the following macros: 3 | % - \emph 4 | Dependently typed languages have proven useful for developing large-scale fully 5 | verified software, but we do not have any guarantees after compiling that 6 | verified software. 7 | A verified program written in a dependently typed language, such as Coq, can be 8 | type checked to ensure that the program meets its specification. 9 | Similarly, type checking prevents us from importing a library and 10 | violating the specification declared by its types. 11 | Unfortunately, we cannot perform either of these checks after compiling a 12 | dependently typed program, since all current implementations erase types before 13 | compiling the program. 14 | Instead, we must trust the compiler to not introduce errors into the 15 | verified code, and, after compilation, trust the programmer to never introduce 16 | errors by linking two incompatible program components. 17 | As a result, the compiled and linked program is 18 | \emph{not verified}---we have no guarantees about what it will do. 19 | 20 | In this dissertation, I develop a theory for preserving dependent types through 21 | compilation so that we can use type checking after compilation to check that no 22 | errors are introduced by the compiler or by linking. 23 | Type-preserving compilation is a well-known technique that has been used to 24 | design compilers for non-dependently typed languages, such as ML, that 25 | statically enforce safety and security guarantees in compiled code. 26 | But there are many open challenges in scaling type preservation to dependent 27 | types. 28 | The key problems are adapting syntactic type systems to interpret low-level 29 | representations of code, and breaking the complex mutually recursive structure 30 | of dependent type systems to make proving type preservation and compiler 31 | correctness feasible. 32 | In this dissertation, I explain the concepts required to scale type preservation 33 | to dependent types, present a proof architecture and language design that 34 | support type preservation, and prove type preservation and compiler correctness 35 | for four early-stage compiler translations of a realistic dependently typed 36 | calculus. 37 | These translations include an A-normal form (ANF), a continuation-passing style 38 | (CPS), an abstract closure conversion, and a parametric closure conversion 39 | translation. 40 | -------------------------------------------------------------------------------- /chapters/beyond.tex: -------------------------------------------------------------------------------- 1 | \chapter{Beyond ``Just'' Dependency} 2 | \label{chp:beyond} 3 | \todo{How might this scale to recursion, relevance, universe polymorphism (both 4 | explicit and implicit), univalence, effects, ???} 5 | Future and related work, etc 6 | 7 | \todo{Copy pasted stub:} 8 | { 9 | \paragraph{Toward a (Full-Spectrum) Dependently Typed Assembly Language} 10 | \label{sec:future} 11 | Ultimately we want to compile to a dependently typed machine-like language in which we can safely link 12 | and then generate machine code. 13 | A typical compiler for a functional language would perform CPS translation, closure-conversion, heap 14 | allocation, and machine-code generation. 15 | We have solved one of these issues, but the others introduce further challenges. 16 | 17 | Past work shows that a standard CPS translation is not type-preserving in our 18 | setting~\cite{barthe2002}. 19 | Other CPS translations have not been studied, such as using a locally polymorphic answer type~\cite{thielecke04}. 20 | This translation has proven useful in compiler verification work already~\cite{ahmed2011}. 21 | Alternatively, we may try to target a stack-based machine language such as stack-based TAL~\cite{morrisett2002}. 22 | This avoid the problems of CPS, but complicates the target language. 23 | 24 | We have begun investigating heap allocation. 25 | This pass raises theoretical and practical questions. 26 | On the theoretical side, we must allow cycles in the heap to support recursive functions but still 27 | ensure soundness and termination. 28 | We may be able to continue adapting the guard condition to allow this. 29 | Linear types have been used to allow cycles in the heap but still guarantee strong 30 | normalization~\cite{morrisett2005}. 31 | Unfortunately, linear types and dependent types are difficult to integrate~\cite{McBride2016}. 32 | On the practical side, we must figure out how to efficiently allocate inductive data structures. 33 | Past work shows how to avoid allocating computationally irrelevant indices from inductive 34 | types~\cite{brady2003}, which we believe we can adapt to CIC. 35 | 36 | The design of a dependently typed assembly language will be challenging for similar reasons that CPS 37 | is not type-preserving. 38 | \citet{herbelin2005} show that the ability to express \verb|call/cc| with $\Sigma$ types results in an 39 | inconsistent logic. 40 | That work also shows how to recover soundness by restricting the type of \verb|call/cc|. 41 | We may need to use this design to build a sound dependently typed assembly. 42 | } 43 | -------------------------------------------------------------------------------- /tech.tex: -------------------------------------------------------------------------------- 1 | %% NB This ended up being a bad idea and so it doesn't actually do anything. 2 | 3 | 4 | % A package for defining hyper linked technical terms, and ensuring no use-before-define of technical tersm. 5 | % Comparable to acronym and glossaries, but those felt a bit heavy weight and only provided linking through an auxiliary appendix, instead of directly into the text. 6 | % https://www.sharelatex.com/learn/Glossaries 7 | 8 | % renew this at whatever granularity to get default prefixs. 9 | % use * versions of commands, like deftech* or refrule*, to circumvent 10 | \newcommand{\techprefix}{} 11 | 12 | \makeatletter 13 | 14 | % light-grey 15 | \definecolor{tech-color}{gray}{0.37} 16 | % NB: Coloring was too distracting 17 | \newcommand{\techcolor}[2]{#2} 18 | 19 | \newcommand{\@deftechStar}[3][]{% 20 | \emph{#3}% 21 | %\phantomsection{\emph{#3}}% 22 | %\let\@meow\undefined% 23 | %\foreach \@meow in {#2}{% 24 | % \expandafter\label{tech:#1:\@meow}% 25 | %}% 26 | %\let\@meow\undefined% 27 | } 28 | 29 | % by default, add a prefix. 30 | \newcommand{\@deftechNoStar}[2][]{\@deftechStar[#1]{#2,#2s}{#2}} 31 | 32 | \newcommand{\deftech}{\@ifstar\@deftechStar\@deftechNoStar} 33 | \newcommand{\deftechs}[1]{\@deftechStar{#1,#1s}{#1s}} 34 | % Can't use makefirstuc, or other iterative macros, in label generation it seems. 35 | \newcommand{\Deftech}[1]{\@deftechStar{#1,#1s}{\makefirstuc{#1}}} 36 | \newcommand{\Deftechs}[1]{\@deftechStar{#1,#1s}{\makefirstuc{#1}s}} 37 | 38 | \newcommand{\@techStar}[3][]{% 39 | %{\hypersetup{hidelinks}\hyperref[tech:#1:#2]{\techcolor{tech-color}{#3}}}% 40 | #3% 41 | } 42 | \newcommand{\@techNoStar}[2][]{\@techStar[#1]{#2}{#2}} 43 | 44 | \newcommand{\tech}{\@ifstar\@techStar\@techNoStar} 45 | \newcommand{\Tech}[1]{\@techStar{#1}{\makefirstuc{#1}}} 46 | \newcommand{\techs}[1]{\@techStar{#1}{#1s}} 47 | \newcommand{\Techs}[1]{\@techStar{#1}{\makefirstuc{#1}s}} 48 | 49 | \renewcommand{\rulename}[1]{{\scshape #1}} 50 | 51 | \LetLtxMacro{\rulelabel}{\label} 52 | % For rules in particular 53 | \newcommand{\@defruleStar}[3][\techprefix]{\phantomsection{\rulename{#3}}\expandafter\rulelabel{rule:#1:#2}} 54 | \newcommand{\@defruleNoStar}[2][\techprefix]{\@defruleStar[#1]{#2}{#2}} 55 | \newcommand{\defrule}{\@ifstar\@defruleStar\@defruleNoStar} 56 | 57 | \newcommand{\@refruleStar}[3][\techprefix]{\hyperref[rule:#1:#2]{Rule \rulename{#3}}} 58 | \newcommand{\@refruleNoStar}[2][\techprefix]{\@refruleStar[#1]{#2}{#2}} 59 | \newcommand{\refrule}{\@ifstar\@refruleStar\@refruleNoStar} 60 | 61 | \makeatother 62 | -------------------------------------------------------------------------------- /dissertation.tex: -------------------------------------------------------------------------------- 1 | \documentclass[paper=letter, twoside, openright, titlepage, headinclude, 2 | footinclude, BC0R=5mm, cleardoublepage=empty, 3 | index=totoc, fontsize=11pt, abstract=on]{scrreprt} 4 | 5 | % Template: 6 | % https://bitbucket.org/amiede/classicthesis/src/94f83724d501be794df38121c2412db6e895c98a/ClassicThesis.tex?at=master&fileviewer=file-view-default 7 | 8 | \includeonly{config,tech,defs, 9 | frontbackmatters/titlepage, 10 | %frontbackmatters/signatures, 11 | frontbackmatters/dedication, 12 | frontbackmatters/abstract, 13 | frontbackmatters/acks, 14 | frontbackmatters/support, 15 | frontbackmatters/conventions, 16 | frontbackmatters/poem, 17 | frontbackmatters/toc, 18 | chapters/intro, 19 | chapters/source, 20 | chapters/type-pres, 21 | chapters/anf, 22 | chapters/abs-cc, 23 | chapters/cps, 24 | chapters/param-cc, 25 | chapters/conclusions, 26 | chapters/source-appendix, 27 | chapters/anf-appendix, 28 | chapters/abs-cc-appendix, 29 | chapters/cps-appendix, 30 | chapters/param-cc-appendix, 31 | frontbackmatters/listoffigs, 32 | frontbackmatters/bib, 33 | } 34 | 35 | \include{config} 36 | \include{tech} 37 | \include{defs} 38 | 39 | \renewcommand{\todo}[1]{} 40 | 41 | \title{Compiling with Dependent Types} 42 | \author{William J. Bowman} 43 | \date{} 44 | 45 | \begin{document} 46 | \include{frontbackmatters/titlepage} 47 | 48 | \cleardoublepage\include{frontbackmatters/signatures} 49 | 50 | \cleardoublepage\include{frontbackmatters/dedication} 51 | \cleardoublepage\include{frontbackmatters/abstract} 52 | \cleardoublepage\include{frontbackmatters/acks} 53 | \cleardoublepage\include{frontbackmatters/support} 54 | \cleardoublepage\include{frontbackmatters/conventions} 55 | \cleardoublepage\include{frontbackmatters/poem} 56 | \cleardoublepage\include{frontbackmatters/toc} 57 | 58 | \renewcommand{\slang}{\anfslang} 59 | 60 | \cleardoublepage\include{chapters/intro} 61 | \cleardoublepage\include{chapters/source} 62 | \cleardoublepage\include{chapters/type-pres} 63 | 64 | \cleardoublepage\include{chapters/anf} 65 | \cleardoublepage\include{chapters/abs-cc} 66 | 67 | \cleardoublepage\include{chapters/cps} 68 | \cleardoublepage\include{chapters/param-cc} 69 | \cleardoublepage\include{chapters/conclusions} 70 | 71 | % Now leaving last chapter. 72 | \bookmarksetup{startatroot} 73 | 74 | \cleardoublepage\include{frontbackmatters/listoffigs} 75 | %\begin{theindex} 76 | %\end{theindex} 77 | \cleardoublepage\include{frontbackmatters/bib} 78 | 79 | \appendix 80 | \cleardoublepage\include{chapters/source-appendix} 81 | \cleardoublepage\include{chapters/anf-appendix} 82 | \cleardoublepage\include{chapters/abs-cc-appendix} 83 | \cleardoublepage\include{chapters/cps-appendix} 84 | \cleardoublepage\include{chapters/param-cc-appendix} 85 | 86 | \end{document} 87 | -------------------------------------------------------------------------------- /config.tex: -------------------------------------------------------------------------------- 1 | % Config reference 2 | % https://bitbucket.org/amiede/classicthesis/src/94f83724d501be794df38121c2412db6e895c98a/classicthesis-config.tex?at=master&fileviewer=file-view-default 3 | 4 | \newcommand{\myTitle}{Compiling with Dependent Types\xspace} 5 | \newcommand{\mySubtitle}{} 6 | \newcommand{\myDegree}{Doctor of Philosophy\xspace} 7 | \newcommand{\myName}{William J. Bowman\xspace} 8 | \newcommand{\myProf}{Amal Ahmed\xspace} 9 | \newcommand{\myDepartment}{College of Computer and Information Science\xspace} 10 | \newcommand{\myUni}{Northeastern University\xspace} 11 | \newcommand{\myLocation}{Boston, Massachusetts\xspace} 12 | \newcommand{\myTime}{2018\xspace} 13 | 14 | \PassOptionsToPackage{pdftex,pdfpagelabels}{hyperref} 15 | \usepackage[numbered]{bookmark} 16 | \usepackage{hyperref} 17 | 18 | \usepackage{amsmath,amssymb,amsthm} 19 | \newtheorem{theorem}{Theorem}[section] 20 | \newtheorem{lemma}[theorem]{Lemma} 21 | \newtheorem{corollary}[theorem]{Corollary} 22 | 23 | \usepackage{natbib} 24 | \bibliographystyle{abbrvnat} 25 | \renewcommand{\cite}{\citep} 26 | \setcitestyle{citesep={;}} 27 | 28 | % Fixing math fonts 29 | \usepackage{scrlfile} 30 | \PreventPackageFromLoading{mathpazo} 31 | \usepackage{upgreek} 32 | 33 | \PassOptionsToPackage{ 34 | eulerchapternumbers, 35 | %drafting, 36 | pdfspacing,dottedtoc, 37 | style=arsclassica 38 | }{classicthesis} 39 | \usepackage{classicthesis} 40 | 41 | % Must happen after classicthesis, to fix up colors classicthesis uses. 42 | \hypersetup{% 43 | pdftitle={\myTitle},% 44 | pdfauthor={\textcopyright\ \myName, \myUni},% 45 | linkcolor=violet,% 46 | final,% 47 | } 48 | 49 | \PassOptionsToPackage{pdftex}{graphicx} 50 | \usepackage{graphicx} 51 | 52 | \usepackage[utf8]{inputenc} 53 | \DeclareUnicodeCharacter{3B1}{\ensuremath{\alpha}} 54 | \DeclareUnicodeCharacter{2081}{\ensuremath{_1}} 55 | \DeclareUnicodeCharacter{2082}{\ensuremath{_2}} 56 | \DeclareUnicodeCharacter{21D2}{\ensuremath{\Rightarrow}} 57 | \DeclareUnicodeCharacter{2203}{\ensuremath{\exists}} 58 | \DeclareUnicodeCharacter{3A3}{\ensuremath{\Sigma}} 59 | \DeclareUnicodeCharacter{2192}{\ensuremath{\rightarrow}} 60 | \usepackage{etoolbox} 61 | \usepackage{refcount} 62 | \usepackage{totpages} 63 | \usepackage{environ} 64 | \usepackage[dvipsnames]{xcolor} 65 | \usepackage{multicol} 66 | \usepackage{xspace} 67 | \usepackage{enumitem} 68 | \usepackage{tikz-cd} 69 | \usepackage{setspace} 70 | \usepackage{alltt} 71 | \usepackage{mfirstuc} 72 | \usepackage{url} 73 | \usepackage{doi} 74 | \usepackage[title]{appendix} 75 | \usepackage{placeins} 76 | \usepackage{chngcntr} 77 | \counterwithin{figure}{chapter} 78 | 79 | % Issue with mttex 80 | \DeclareOldFontCommand{\bf}{\normalfont\bfseries}{\mathbf} 81 | \usepackage[magicref]{mttex/mttex} 82 | 83 | % A little less margin, a little more text. 84 | \areaset[current]{410pt}{700pt} 85 | 86 | % Code stuff 87 | \input{outputdir} 88 | \usepackage[chapter]{minted} 89 | 90 | % arrows 91 | \usepackage{tikz} 92 | \usetikzlibrary{calc,shapes} 93 | 94 | % include pdf 95 | \usepackage{pdfpages} 96 | -------------------------------------------------------------------------------- /chapters/abs-cc-appendix.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{refabscc} 2 | \renewcommand{\tlang}{\abscctlang} 3 | \renewcommand{\slang}{\absccslang} 4 | 5 | \newcommand{\FigcCCObs}[1][t]{ 6 | \begin{figure}[#1] 7 | \begin{bnfgrammar} 8 | \bnflabel{\abscctlang Observations} & 9 | \tv & \bnfdef & \ttruee \bnfalt \tfalsee 10 | \end{bnfgrammar} 11 | \caption{\abscctlang Observations} 12 | \label{fig:cccc:obs} 13 | \end{figure} 14 | } 15 | 16 | \newcommand{\FigCCObsRel}[1][t]{ 17 | \begin{figure}[#1] 18 | \judgshape{\sv \approx \tv} 19 | \begin{mathpar} 20 | \struee \approx \ttruee 21 | 22 | \sfalsee \approx \tfalsee 23 | \end{mathpar} 24 | \caption{Observation Relation between \slang and \tlang} 25 | \label{fig:cccc:obs} 26 | \end{figure} 27 | } 28 | 29 | \chapter{Reference for \abscctlang} 30 | \label{sec:abs-cc:appendix} 31 | This appendix contains the complete definitions for \abscctlang. 32 | All \abscctlang figures from \fullref[]{chp:abs-cc} are reproduced and completed 33 | with elided parts here, and elided figures are presented here. 34 | In particular, additional figures regarding the ANF variant of \abscctlang are 35 | included here. 36 | 37 | \begin{typographical} 38 | In this appendix, I typeset \abscctlang in a \emph{\tfonttext{bold, red, serif 39 | font}}, and typeset the model language \slang in a \emph{\sfonttext{blue, 40 | non-bold, sans-serif font}}. 41 | \end{typographical} 42 | 43 | \begingroup 44 | \let\label\discard 45 | 46 | \FigCCCCSyntax[ht] 47 | 48 | \endgroup 49 | 50 | \FigCCCCSugar[ht] 51 | \FigCCCCRedFull[ht] 52 | \FigCCCCConv[ht] 53 | 54 | \begingroup 55 | \let\label\discard 56 | 57 | \FigCCCCEqv[ht] 58 | \FigCCCCSub[ht] 59 | 60 | \endgroup 61 | 62 | \FigCCCCTypingFullOne[ht] 63 | \FigCCCCTypingFullTwo[ht] 64 | \FigCCCCWF[ht] 65 | 66 | \begingroup 67 | \let\label\discard 68 | \let\sfont\tfont 69 | \let\sfontsym\tfontsym 70 | \let\scolor\tcolor 71 | \renewcommand{\slang}{\abscctlang} 72 | 73 | \FigcCCObs 74 | \FigECCProg[ht] 75 | \FigCCCCEval[ht] 76 | \FigLinking[ht] 77 | 78 | \endgroup 79 | 80 | \FigCCCCModelFull[ht] 81 | 82 | \FloatBarrier 83 | 84 | Note that the following ANF definitions for \abscctlang exclude dependent 85 | conditionals, since the ANF translation for dependent conditionals presented in 86 | \fullref[]{chp:anf} is incomplete. 87 | 88 | \FigCCANFFull[ht] 89 | \FigCCCCAHetero[ht] 90 | \FigCCANFRed[ht] 91 | 92 | \cleardoublepage 93 | \chapter{Reference for Abstract Closure Conversion} 94 | \label{sec:abs-cc:cc:appendix} 95 | This appendix contains the complete definitions for the abstract closure 96 | conversion translation from \fullref[]{chp:abs-cc}. 97 | All figures related to the translation are reproduced and completed with 98 | elided parts here, and elided figures are presented here. 99 | 100 | \begin{typographical} 101 | In this appendix, I typeset the source language, \slang, in a 102 | \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target language, 103 | \abscctlang, in a \emph{\tfonttext{bold, red, serif font}}. 104 | \end{typographical} 105 | 106 | \FigCCObsRel[ht] 107 | 108 | \begingroup 109 | \let\label\discard 110 | 111 | \FigDFVs[ht] 112 | 113 | \endgroup 114 | 115 | \FigCCTermNotation[ht] 116 | \FigCCTermFullOne[ht] 117 | \FigCCTermFullTwo[ht] 118 | -------------------------------------------------------------------------------- /chapters/param-cc-appendix.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{refpcc} 2 | \renewcommand{\tlang}{\pcctlang} 3 | \renewcommand{\slang}{\pccslang} 4 | 5 | \chapter{Reference for \pcctlang} 6 | \label{sec:param-cc:appendix} 7 | This appendix contains the complete definitions for \pcctlang. 8 | All \pcctlang figures from \fullref[]{chp:param-cc} are reproduced and completed 9 | with elided parts here, and elided figures are presented here. 10 | These figures contained extensions with definitions required to fully formalize 11 | the compiler correctness results, including booleans with non-dependent 12 | elimination. 13 | 14 | \begin{typographical} 15 | In this appendix, I typeset \pcctlang in a \emph{\tfonttext{bold, red, serif 16 | font}}. 17 | \end{typographical} 18 | 19 | \FigCoCCCSyntaxFull[ht] 20 | \FigCoCCCSugar[ht] 21 | \FigCoCCCRedFull[ht] 22 | \FigCoCCCConv[ht] 23 | \FigCoCCCEquivFull[ht] 24 | \FigCoCCCTypingOne[ht] 25 | \FigCoCCCTypingTwo[ht] 26 | 27 | \begingroup 28 | \let\label\discard 29 | \renewcommand{\cpstlang}{\pcctlang} 30 | \renewcommand{\slang}{\pcctlang} 31 | \renewcommand{\sfont}{\pccfont} 32 | \renewcommand{\sfontsym}{\pccfontsym} 33 | \renewcommand{\scolor}{\pcccolor} 34 | 35 | \FigCPSWf[ht] 36 | \FigECCObs[ht] 37 | \FigECCProg[ht] 38 | \FigECCEval[ht] 39 | 40 | \endgroup 41 | 42 | \begingroup 43 | \let\label\discard 44 | \renewcommand{\cpsslang}{\pcctlang} 45 | \renewcommand{\sfont}{\pccfont} 46 | \renewcommand{\sfontsym}{\pccfontsym} 47 | \renewcommand{\scolor}{\pcccolor} 48 | 49 | \FigCOCLinking 50 | \endgroup 51 | 52 | \FloatBarrier 53 | 54 | \begin{listing}[h] 55 | \begin{minted}{coq} 56 | Definition translucent {A} e B := forall (x : A), (x = e) -> B. 57 | 58 | Notation "e ⇒ B" := (translucent e B) (at level 42). 59 | 60 | Definition translucent_intro {A B} {e' : A} 61 | (e : forall (x : A), B x) : e' ⇒ B e' 62 | := fun x w => match w with eq_refl => e x end. 63 | 64 | Notation "e :T: A" := (translucent_intro e : A) (at level 99). 65 | 66 | Definition translucent_elim {A B} {e' : A} (e : e' ⇒ B) : B 67 | := e e' eq_refl. 68 | 69 | Notation "e @ m" := (@translucent_elim _ _ m e) (at level 99). 70 | 71 | Inductive sigS {A:Type} (P:A -> Type) : Prop := 72 | existS : forall x:A, P x -> sigS P. 73 | 74 | Notation "'∃' x : A , P" := (sigS (A:=A) (fun x => P)) 75 | (at level 0, x at level 99) : type_scope. 76 | 77 | Definition Closure A B : Prop := 78 | ∃ α : Type , ∃ env : α , (env ⇒ (A -> B)). 79 | \end{minted} 80 | \caption{Model of \pcctlang in Coq} 81 | \end{listing} 82 | 83 | \cleardoublepage 84 | \chapter{Reference for Parametric Closure Conversion} 85 | \label{sec:param-cc:cc:appendix} 86 | This appendix contains the complete definitions for the parametric closure 87 | conversion from \cpsslang to \pcctlang. 88 | All translation figures from \fullref[]{chp:param-cc} are reproduced and completed 89 | with elided parts here, and elided figures are presented here. 90 | 91 | \begin{typographical} 92 | In this appendix, I typeset the source language, \pccslang, in a 93 | \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target language, 94 | \pcctlang, in a \emph{\tfonttext{bold, red, serif font}}. 95 | \end{typographical} 96 | 97 | \begingroup 98 | \let\label\discard 99 | \renewcommand{\cpstlang}{\pcctlang} 100 | 101 | \FigCPSObsRel[ht] 102 | 103 | \endgroup 104 | 105 | \begingroup 106 | \let\label\discard 107 | 108 | \FigDFVs[ht] 109 | 110 | \endgroup 111 | 112 | \FigCCTermOne[ht] 113 | \FigCCTermTwo[ht] 114 | -------------------------------------------------------------------------------- /chapters/cps-appendix.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{refcpssrc} 2 | \renewcommand{\tlang}{\cpstlang} 3 | \renewcommand{\slang}{\cpsslang} 4 | 5 | \chapter{Reference for \cpsslang} 6 | \label{sec:cps:appendix} 7 | This appendix contains the complete definitions for \cpsslang. 8 | All \cpsslang figures from \fullref[]{chp:cps} are reproduced and completed with 9 | elided parts here, and elided figures are presented here. 10 | These figures contained extensions with definitions required to fully formalize 11 | the compiler correctness results, including booleans with non-dependent 12 | elimination. 13 | 14 | \begin{typographical} 15 | In this appendix, I typeset \cpsslang in a \emph{\sfonttext{blue, 16 | non-bold, sans-serif font}}. 17 | \end{typographical} 18 | 19 | \begingroup 20 | \let\label\discard 21 | 22 | \FigCOCSyntax[ht] 23 | \FigECCExplicitSyntax[ht] 24 | 25 | \FigCOCRed[ht] 26 | 27 | \endgroup 28 | 29 | \FigCOCConv[ht] 30 | 31 | \begingroup 32 | \let\label\discard 33 | 34 | \FigCOCEqv[ht] 35 | 36 | \FigCoCTyping[ht] 37 | \FigCOCWF[ht] 38 | 39 | \endgroup 40 | 41 | \begingroup 42 | \let\label\discard 43 | \renewcommand{\slang}{\cpsslang} 44 | 45 | \FigECCObs[ht] 46 | \FigECCProg[ht] 47 | \FigECCEval[ht] 48 | 49 | \endgroup 50 | 51 | \FigCOCLinking 52 | 53 | \cleardoublepage 54 | \renewcommand{\techprefix}{refcps} 55 | \chapter{Reference for \cpstlang} 56 | \label{sec:cps:tlang:appendix} 57 | This appendix contains the complete definitions for \cpstlang. 58 | All \cpstlang figures from \fullref[]{chp:cps} are reproduced and completed with 59 | elided parts here, and elided figures are presented here. 60 | 61 | \begin{typographical} 62 | In this appendix, I typeset \cpstlang in a \emph{\tfonttext{bold, red, serif 63 | font}}. 64 | \end{typographical} 65 | 66 | \FigCPSSyntax[ht] 67 | \FigCPSRed[ht] 68 | \FigCPSEqv[ht] 69 | \FigCPSConv[ht] 70 | \FigCPSTyping[ht] 71 | \FigCPSWf[ht] 72 | 73 | \begingroup 74 | \let\label\discard 75 | \renewcommand{\slang}{\cpstlang} 76 | \renewcommand{\sfont}{\tfont} 77 | \renewcommand{\sfontsym}{\tfontsym} 78 | \renewcommand{\scolor}{\tcolor} 79 | 80 | \FigECCObs[ht] 81 | \endgroup 82 | 83 | \begingroup 84 | \let\label\discard 85 | 86 | \FigCPSProg[ht] 87 | \FigCPSEval[ht] 88 | 89 | \endgroup 90 | 91 | \begingroup 92 | \let\label\discard 93 | \renewcommand{\cpsslang}{\cpstlang} 94 | \renewcommand{\sfont}{\tfont} 95 | \renewcommand{\sfontsym}{\tfontsym} 96 | \renewcommand{\scolor}{\tcolor} 97 | 98 | \FigCOCLinking 99 | 100 | \endgroup 101 | 102 | \begingroup 103 | \let\label\discard 104 | 105 | \FigCCConv 106 | \FigANF 107 | 108 | \endgroup 109 | 110 | \FloatBarrier 111 | 112 | \begin{listing}[ht] 113 | \inputminted{coq}{chapters/cps/cont-shuffle.v} 114 | \caption{Coq proof of \protect\fullref[]{lem:cps:cont-shuffle}} 115 | \end{listing} 116 | 117 | \cleardoublepage 118 | \chapter{Reference for \cbnnametext} 119 | \label{sec:cps:cbn:appendix} 120 | \renewcommand{\techprefix}{refcpsN} 121 | This appendix contains the complete definitions for the CBN CPS translation from 122 | \cpsslang to \cpstlang. 123 | All translation figures from \fullref[]{chp:cps} are reproduced and completed with 124 | elided parts here, and elided figures are presented here. 125 | 126 | \begin{typographical} 127 | In this appendix, I typeset the source language, \cpsslang, in a 128 | \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target language, 129 | \cpstlang, in a \emph{\tfonttext{bold, red, serif font}}. 130 | \end{typographical} 131 | 132 | \begingroup 133 | \let\label\discard 134 | 135 | \FigCPSObsRel[ht] 136 | 137 | \FigCBNUnv[ht] 138 | \FigCBNKind[ht] 139 | 140 | \endgroup 141 | 142 | \FigCBNTypesFullOne[ht] 143 | \FigCBNTypesFullTwo[ht] 144 | \FigCBNTermsFull[ht] 145 | \FigCBNTermsPairs[ht] 146 | 147 | \begingroup 148 | \let\label\discard 149 | 150 | \FigCBNEnv[ht] 151 | \endgroup 152 | 153 | \cleardoublepage 154 | \chapter{Reference for \cbvnametext} 155 | \label{sec:cps:cbv:appendix} 156 | \renewcommand{\techprefix}{refcpsV} 157 | This appendix contains the complete definitions for the CBV CPS translation from 158 | \cpsslang to \cpstlang. 159 | All translation figures from \fullref[]{chp:cps} are reproduced and completed with 160 | elided parts here, and elided figures are presented here. 161 | 162 | \begin{typographical} 163 | In this appendix, I typeset the source language, \cpsslang, in a 164 | \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target language, 165 | \cpstlang, in a \emph{\tfonttext{bold, red, serif font}}. 166 | \end{typographical} 167 | 168 | \FigCPSObsRel[ht] 169 | \FigCBVUnv[ht] 170 | 171 | \begingroup 172 | \let\label\discard 173 | 174 | \FigCBVKinds[ht] 175 | 176 | \endgroup 177 | 178 | \FigCBVTypesFull[ht] 179 | 180 | \begingroup 181 | \let\label\discard 182 | 183 | \FigCBVTerms[ht] 184 | \FigCBVTermsPairs[ht] 185 | \FigCBVEnv[ht] 186 | 187 | \endgroup 188 | -------------------------------------------------------------------------------- /frontbackmatters/acks.tex: -------------------------------------------------------------------------------- 1 | \pdfbookmark[1]{Acknowledgments}{Acknowledgments} 2 | 3 | \begingroup 4 | \let\clearpage\relax 5 | \let\cleardoublepage\relax 6 | \let\cleardoublepage\relax 7 | \chapter*{Acknowledgments} 8 | I never thought I would get a Ph.D. 9 | In fact, I thought I would never get a Ph.D. 10 | As I completed my undergraduate degree, a Ph.D. seemed like an insurmountable 11 | challenge: I would have to produce knowledge unknown to the world, write about, 12 | talk about it, and teach it. 13 | I didn't know how to do that. 14 | I didn't want to do that. 15 | I just wanted to write fun programs and learn interesting facts. 16 | 17 | I was wrong. 18 | The following people helped me realize that. 19 | \begin{itemize} 20 | \item Dan Friedman 21 | 22 | Dan is the reason I'm here today. 23 | 24 | Dan introduced me to the beauty of programming languages, causing me to shift 25 | gears from software engineering and security to spending my free time reading 26 | about language design. 27 | When I was interested in research, Dan recommended I talk to Amal. 28 | He explicitly recommended that I not work with him, claiming that he doesn't 29 | do important research. 30 | That might be, but he does do important work. 31 | \item Amr Sabry 32 | 33 | Amr taught me how to write software, and gave me a few research problems to 34 | play with as a Master's student while I tried to figure out whether I wanted 35 | to do a Ph.D. 36 | \item R. Kent Dybvig 37 | 38 | Kent taught me that most programs aren't written by humans, and how to think 39 | like a machine. 40 | \item The Northeastern PRL, jointly and separately. 41 | 42 | Vincent St-Amour, Asumu Takikawa, Phillip Mates, Jamie Perconti, Paul 43 | Stansifer, Tony Garnock-Jones, Stephen Chang, Justin Slepak, Jonathan 44 | Schuster, Dionna Amalie Glaze, Andrew Cobb, Sam Caldwell, Ben Greenman, Leif 45 | Andersen, Daniel Patterson, Aaron Weiss, and Ben Lerner. 46 | 47 | Thank you all for hanging out at happy hours, criticizing my practice talks, 48 | and making my Ph.D. a great personal and professional experience. 49 | \item Max S. New 50 | 51 | Max, the category practitioner. 52 | He understands abstraction in ways I doubt I ever will. 53 | Many times, he has helped me understand my own work better. 54 | Max is a brilliant friend and amazing colleague. 55 | \item Cătălin Hriţcu 56 | 57 | Cătălin is a kind, generous, and skeptical person---a great scientist. 58 | He is one of the first people I met at a programming languages conference. 59 | Cătălin hosted me at Inria for a semester while I finished this work, giving 60 | me the chance to meet people who know way more about dependent types than me. 61 | This dissertation is much better because of that experience. 62 | \item Annabel Satin 63 | 64 | Annabel is an awesome person, working tirelessly in the background for the 65 | good of programming languages. 66 | Without her, our work might well grind to a halt. 67 | \item Youyou Cong 68 | 69 | Youyou visited us for a semester, and was critical to part of this 70 | dissertation. 71 | She has an encyclopedic knowledge of the literature surrounding CPS. 72 | I still sometimes bother her with my ideas. 73 | \item Neel Krishnaswami 74 | 75 | Every time I talk to Neel, I learn something awesome about semantics. 76 | Our chats have shaped my work in subtle ways. 77 | \item Ron Garcia 78 | 79 | Ron is the nicest person in the programming languages community. 80 | Talking with Ron, I feel like he is genuinely interested in what I have to 81 | say, even when all I have to say are half baked ideas. 82 | He can express his confusion, or clearly explain why I'm wrong, without 83 | making me feel stupid. 84 | \item Éric Tanter 85 | 86 | Éric has always made me feel like a colleague, and not just a graduate student. 87 | He's one of the reasons I'm sticking around in academia. 88 | \item Stephanie Weirich 89 | 90 | Stephanie has a unique perspective on dependent types. 91 | She has challenged my assumptions and design choices throughout my 92 | dissertation. 93 | One day, I'll try to put those thoughts in writing. 94 | \item Greg Morrisett 95 | 96 | Greg has been a huge help during this work. 97 | Even after starting as dean, Greg found time to chat about research. 98 | When I sent him a draft, he'd read it in mere hours and come back with cogent 99 | feedback, questions, and criticisms. 100 | \item Mitch Wand 101 | 102 | Mitch helped me learn more about writing and English during this dissertation 103 | than I learned in 22 years of school before I started. 104 | \item Matthias Felleisen 105 | 106 | Matthias has constantly pushed me to reach a higher standard for scientific 107 | rigor, precision, and clarity---more so than anyone else. 108 | He has never hesitated to (constructively) criticize my work. 109 | I've learned a ton from him, and if I'm lucky I'll learn some more. 110 | \item Amal Ahmed 111 | 112 | Amal has been a great advisor. 113 | She has absolute and infectious enthusiasm for what she does. 114 | She helped me learn about proofs and types, and about communicating complex 115 | mathematical work. 116 | She chewed me out when I ignored important things, and gave me latitude 117 | when I needed it. 118 | 119 | But mostly, Amal taught me to not fear hard problems. 120 | In 2010, as an undergrad, I asked Amal if she had any research problems. 121 | She gave me a book (Types and Programming Languages), and a secure compilation 122 | problem, and said it would take ``6 months, max''. 123 | In 2015, we finished that paper. 124 | In 2016, I had a choice: continue working on secure compilation, or switch to 125 | working on dependent types. 126 | I was more interested in dependent types, but switching topics 4 years in to a 127 | Ph.D. program seemed like a big risk, so I chose secure compilation. 128 | A few days later, I walked into her office and said ``I changed my mind. It's 129 | going to be hard, but it'll be way more interesting.''. 130 | I don't know if I could have done that without Amal. 131 | \end{itemize} 132 | 133 | \bigskip 134 | 135 | \noindent Thank you all. You have all shaped this work, either directly or indirectly through emotional and psychological support, and are part of the reason I finished this dissertation. And thank you to many more people who I've not put in this list; there are many of you who have helped me in ways I haven't been able to put into words. 136 | 137 | \hfill --- William J. Bowman 138 | \endgroup 139 | -------------------------------------------------------------------------------- /chapters/anf-appendix.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{anfapp} 2 | \renewcommand{\tlang}{\anftlang} 3 | \renewcommand{\slang}{\anfslang} 4 | 5 | \newcommand{\FigECCASyntaxE}[1][t]{ 6 | \begin{figure}[#1] 7 | \begin{bnfgrammar} 8 | \bnflabel{Universes} & 9 | \tU & \!\!\bnfdef & \tpropty \bnfalt \ttypety{i} 10 | \bnfnewline 11 | 12 | \bnflabel{Values} & \tV & \bnfdef & \tx \bnfalt \tU \bnfalt \tpity{\tx}{\tM}{\tM} \bnfalt \tfune{\tx}{\tM}{\tM} \bnfalt \tsigmaty{\tx}{\tM}{\tM} 13 | \\&& \bnfalt & \tdpaire{\tV}{\tV}{\tM} 14 | \bnfnewline 15 | 16 | \bnflabel{Computations} & \tN & \bnfdef & \tV \bnfalt \tappe{\tV}{\tV} \bnfalt \tfste{\tV} \bnfalt \tsnde{\tV} 17 | \bnfnewline 18 | 19 | \bnflabel{Configurations} & \tM & \bnfdef & \tN \bnfalt \tlete{\tx}{\tN}{\tM} 20 | \bnfnewline 21 | 22 | \bnflabel{Continuations} & \tK & \bnfdef & \hole \bnfalt \tlete{\tx}{\hole}{\tM} 23 | \bnfnewline 24 | 25 | \bnflabel{Expressions} & \te,\tA,\tB & \bnfdef & \tx 26 | \bnfalt \tU 27 | \bnfalt \tpity{\tx}{\tA}{\tB} 28 | \bnfalt \tfune{\tx}{\tA}{\te} 29 | \bnfalt \tappe{\te}{\te} 30 | \bnfalt \tsigmaty{\tx}{\tA}{\tB} 31 | \\ 32 | &&\bnfalt& \tdpaire{\teone}{\tetwo}{\tsigmaty{\tx}{\tA}{\tB}} 33 | \bnfalt \tfste{\te} 34 | \bnfalt \tsnde{\te} 35 | \bnfalt \tboolty 36 | \bnfalt \ttruee 37 | \\ && \bnfalt & 38 | \tfalsee 39 | \bnfalt \tife{\te}{\teone}{\tetwo} 40 | \bnfalt \tlete{\tx}{\te}{\te} 41 | \bnfnewline 42 | 43 | \bnflabel{Environments} & 44 | \tlenv & \bnfdef & \cdot \bnfalt \tlenv,\tx:\tA \bnfalt \tlenv,\tx=\te 45 | \end{bnfgrammar} 46 | \caption{\tlang Syntax} 47 | \end{figure} 48 | } 49 | 50 | \newcommand{\FigECCAObs}[1][t]{ 51 | \begin{figure}[#1] 52 | \begin{bnfgrammar} 53 | \bnflabel{\tlang Observations} & 54 | \tv & \bnfdef & \ttruee \bnfalt \tfalsee 55 | \end{bnfgrammar} 56 | 57 | \caption{\anftlang Observations} 58 | \label{fig:ecca:obs} 59 | \end{figure} 60 | } 61 | 62 | \newcommand{\FigANFObsRel}[1][t]{ 63 | \begin{figure}[#1] 64 | \judgshape{\sv \approx \tv} 65 | \begin{mathpar} 66 | \struee \approx \ttruee 67 | 68 | \sfalsee \approx \tfalsee 69 | \end{mathpar} 70 | \caption{Observation Relation between \slang and \anftlang} 71 | \end{figure} 72 | } 73 | 74 | \newcommand{\FigAObs}[1][t]{ 75 | \begin{figure}[#1] 76 | \begin{bnfgrammar} 77 | \bnflabel{\slang Observations} & 78 | \sv & \bnfdef & \struee \bnfalt \sfalsee 79 | \bnfnewline 80 | 81 | \bnflabel{\tlang Observations} & 82 | \tv & \bnfdef & \ttruee \bnfalt \tfalsee 83 | \end{bnfgrammar} 84 | \judgshape{\sv \approx \tv} 85 | \begin{mathpar} 86 | \struee \approx \ttruee 87 | 88 | \sfalsee \approx \tfalsee 89 | \end{mathpar} 90 | \caption{\slang and \anftlang Observations} 91 | \label{fig:ecc+:obs} 92 | \end{figure} 93 | } 94 | 95 | \newcommand{\FigECCALinking}[1][t]{ 96 | \begin{figure}[#1] 97 | \begin{bnfgrammar} 98 | \bnflabel{Closing Substitutions} & 99 | \tsubst & \defeq & \cdot \bnfalt \tsubst[\tx \mapsto \tM] 100 | \end{bnfgrammar} 101 | \judgshape{\wf{\tlenv}{\tsubst}} 102 | \begin{mathpar} 103 | \inferrule 104 | {~} 105 | {\wf{\cdot}{\cdot}} 106 | 107 | \inferrule 108 | {\wf{\tlenv}{\tsubst} \\ 109 | \ttyjudg{\cdot}{\tM}{\tA}} 110 | {\wf{\tlenv,\tx:\tA}{\tsubst[\tx \mapsto \tM]}} 111 | 112 | \inferrule 113 | {\wf{\tlenv}{\tsubst} \\ 114 | \styjudg{\tlenv}{\tM}{\tA}} 115 | {\wf{\tlenv,\tx = \tM}{\tsubst[\tx \mapsto \tsubst(\tM)]}} 116 | \end{mathpar} 117 | \judgshape{\tsubst(\tM) = \tM} 118 | \begin{mathpar} 119 | \cdot(\tM) = \tM 120 | 121 | \tsubst[\tx \mapsto \tMpr](\tM) = \tsubst(\hsubst{\tM}{\tx}{\tMpr}) 122 | \end{mathpar} 123 | \caption{\anftlang Closing Substitutions and Linking} 124 | \label{fig:ecca:link} 125 | \end{figure} 126 | } 127 | 128 | \chapter{Reference for \anftlang} 129 | \label{chp:anftlang:appendix} 130 | This appendix contains the complete definitions for \anftlang. 131 | All \anftlang figures from \fullref[]{chp:anf} are reproduced and completed with 132 | elided parts here, and elided figures are presented here. 133 | 134 | Recall that \anftlang is a syntactic restriction of \slang that supports a 135 | machine evaluation semantics. 136 | The primary difference between the figure here and in \fullref[]{chp:anf} is the 137 | syntax is extended with non-ANF expressions to formally describe type checking 138 | in \anftlang. 139 | Recall that the type system for \anftlang is the same as \slang, so the figures 140 | related to the type system are identical to those in \fullref[]{sec:source:appendix}. 141 | 142 | \begin{typographical} 143 | In this appendix, I typeset \anftlang in a \emph{\tfonttext{bold, red, serif font}}. 144 | \end{typographical} 145 | 146 | \begingroup 147 | \let\label\discard 148 | 149 | \FigECCASyntaxE[ht] 150 | 151 | \endgroup 152 | 153 | \begingroup 154 | \let\scolor\tcolor 155 | \let\sfont\tfont 156 | \let\sfontsym\tfontsym 157 | \let\label\discard 158 | \renewcommand{\slang}{\anftlang} 159 | 160 | \FigECCRed[ht] 161 | \FigECCConvFull[ht] 162 | \FigECCEqv[ht] 163 | \FigECCSub[ht] 164 | \FigECCTyping[ht] 165 | 166 | \endgroup 167 | 168 | \begingroup 169 | \renewcommand{\tA}{\tMin{A}} 170 | \renewcommand{\tB}{\tMin{B}} 171 | \let\label\discard 172 | 173 | \FigECCAHetero[ht] 174 | \FigECCAStackTypes[ht] 175 | \FigStackExports[ht] 176 | 177 | \endgroup 178 | 179 | \begingroup 180 | \let\label\discard 181 | \renewcommand{\se}{\tM} 182 | \renewcommand{\slenv}{\tlenv} 183 | \renewcommand{\slang}{\anftlang} 184 | \renewcommand{\sboolty}{\tboolty} 185 | 186 | \FigECCProg[ht] 187 | \FigECCAObs[ht] 188 | 189 | \FigECCARed[ht] 190 | \FigECCALinking[ht] 191 | \endgroup 192 | 193 | \cleardoublepage 194 | \chapter{Reference for {{ANF}} Translation} 195 | This appendix contains the complete definitions for the ANF translation from 196 | \fullref[]{chp:anf}. 197 | All figures related to the ANF translation are reproduced and completed with 198 | elided parts here, and elided figures are presented here. 199 | 200 | \begin{typographical} 201 | In this appendix, I typeset the source language, \anfslang, in a 202 | \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target language, 203 | \anftlang, in a \emph{\tfonttext{bold, red, serif font}}. 204 | \end{typographical} 205 | 206 | \begingroup 207 | \let\label\discard 208 | 209 | \FigANFObsRel[ht] 210 | \FigTrans[ht] 211 | 212 | \endgroup 213 | -------------------------------------------------------------------------------- /chapters/cps/intro.tex: -------------------------------------------------------------------------------- 1 | \label{sec:cps:intro} 2 | In this chapter, I develop a \tech{type-preserving} \tech{CPS} translation for a 3 | subset of \slang. 4 | As mentioned in \fullref[]{chp:anf}, \tech{CPS} presents many challenges and 5 | does not scale well to all features of \tech{dependency}. 6 | However, by understanding the key problems that \tech{CPS} introduces into 7 | a \tech{dependent types} system, we can develop \tech{type 8 | preserving} \tech{CPS} for some \tech{dependently typed} languages, which is 9 | surprising given past impossibility results. 10 | 11 | I start with a brief discussion of the different uses of \tech{CPS} and discuss 12 | the past impossibility result for dependent-type-preserving \tech{CPS}. 13 | I then develop \cpsslang, a restriction of \slang suitable for translations that 14 | rely on \emph{parametricity}, before developing both \tech{CBN} 15 | and \tech{CBV} \tech{type-preserving} \tech{CPS} translations for \cpsslang. 16 | 17 | \begin{typographical} 18 | In this chapter, I typeset the source language, \cpsslang, in a 19 | \emph{\sfonttext{blue, non-bold, sans-serif font}}, and the target language, 20 | \cpstlang, in a \emph{\tfonttext{bold, red, serif font}}. 21 | \end{typographical} 22 | 23 | \section{On {{CPS}}} 24 | The \tech{CPS} translation presented in this chapter allows 25 | implementing \tech{type-preserving} compilation for some \tech{dependently 26 | typed} languages, but it will not allow implementing control effects, a common 27 | use for \tech{CPS}. 28 | Many of the design decisions I make do not apply when implementing control 29 | effects, or specifically disallow implementing control effects. 30 | For example, my goal is to avoid restricting dependencies in the source 31 | language so that I can compile existing languages such as Coq. 32 | However, to allow mixing control effects and dependency, one must necessarily 33 | restrict certain \tech{dependencies}, at least for the effectful parts of the 34 | language. 35 | This requires different designs for the CPS language and translation; I discuss 36 | some related work in this vein in \fullref[]{sec:cps:related}. 37 | Before I present my CPS translation, I discuss the context of CPS translation as it 38 | applies to type preservation and the core features of dependency. 39 | 40 | Typically, type-preserving \tech{compilers} use one of two common type translations: (1) 41 | the \deftech{double-negation translation} that translates expressions of 42 | type \im{A} into a \tech{computation} of type \im{A^\div = (A^+ \to \False) \to \False} or 43 | \im{\lnot\lnot A^+} where \im{A^+} represents the \tech{value-type translation}, 44 | or (2) the \deftech*{locally polymorphic answer type,answer-type 45 | polymorphism,polymorphic answer type translation,polymorphic answer 46 | type}{locally polymorphic answer type translation} that translates 47 | \tech{expressions} of type \im{A} into \tech{computations} of type \im{A^\div = 48 | \forall \alpha. (A^+ \to \alpha) \to \alpha}. 49 | The value translation differs depending on whether the translation is 50 | encoding \tech{CBN} or \tech{CBV} evaluation order, but essentially recursively 51 | translates types that must be \tech{values} using the value translation \im{^+}, 52 | and types that could be \tech{computations} using the computation 53 | translation \im{^\div}. 54 | In each translation, the \tech{computation} expects a \tech{continuation}, 55 | either of type \im{A^+ \to \False} or \im{A^+ \to \alpha}. 56 | Intuitively, the computation is forced to call that \tech{continuation} with 57 | a \tech{value} of type \im{A^+}. 58 | The \tech{double-negation translation} represents the final result of a 59 | \tech{program} with the empty type \False to indicate that \tech{programs} 60 | \deftech{never return}, \ie, \tech{programs} no longer behave like mathematical 61 | functions that take inputs and produce output values but instead run to 62 | completion and end up in some final answer state. 63 | The \tech{locally polymorphic answer type} uses \tech{parametricity} to encode a 64 | similar idea: if the \tech{computation} behaves \tech{parametrically} in its 65 | answer type, then no \tech{computation} can do anything but call its 66 | \tech{continuation} with the \deftech{underlying value} that the 67 | \tech{computation} represents. 68 | At the level of a whole program, this is equivalent to a \tech{program} 69 | \tech{never returning}---each intermediate \tech{computation} cannot return 70 | because it must, by \tech{parametricity}, invoke its \tech{continuation}. 71 | 72 | These two translations admit different reasoning principles, however. 73 | 74 | The \tech{double-negation translation} supports encoding control 75 | effects but complicates compositional reasoning. 76 | A \tech{computation} is free to duplicate its \tech{continuation} and save it 77 | for later, or call a saved \tech{continuation}, since the type of 78 | \tech{computations} only requires that the \tech{computation} returns \im{\False} and 79 | every \tech{continuation} has the answer type \im{\False}. 80 | However, it becomes difficult to give a compositional interpretation 81 | of \tech{programs} compared to a standard \(\lambda\)-calculus. 82 | In \slang, for example, we define the meaning of \tech{expressions} simply by 83 | evaluation to a \tech{value}. 84 | Using the \tech{double-negation translation}, \tech{CPS} \tech{programs} 85 | \tech{never return}, so we cannot easily define the \tech{value} of an 86 | \tech{expression} by evaluation; we have to complete it first to get a whole 87 | program, then evaluate it, then look at the final state. 88 | 89 | The \tech{locally polymorphic answer type} translation makes it difficult to 90 | implement control effects, but easily supports compositional reasoning. 91 | Since each \tech{computation} is \tech{parametric} in its answer type, we are 92 | essentially forced to call the \tech{continuation} exactly once and as the final 93 | step in the \tech{computation}. 94 | If we want to encode control effects, this is a problem. 95 | However, it supports compositional reasoning. 96 | We can locally evaluate \tech{CPS}'d \tech{expressions} and get the meaning of 97 | that \tech{expression} as a \tech{value} by picking a meaningful answer type. 98 | For example, given the \tech{computation} 99 | \im{e : \forall \alpha. (Bool \to \alpha) \to \alpha}, we can either treat it 100 | as a \tech{program} and instantiate the answer type with \im{\False} 101 | (\im{e~\False : (Bool \to \False) \to \False}), or get the meaning of 102 | the \tech{underlying value} by instantiating the answer type with \im{Bool}, the 103 | type of the underlying value, and applying the identity function as 104 | the \tech{continuation} \im{e~Bool~\lambda x.x : Bool}. 105 | The \tech{expression} \im{e~Bool~\lambda x.x} will return the 106 | \tech{underlying value} of \im{e}, supporting compositional reasoning. 107 | 108 | The standard translation for \tech{type-preserving} compilation is 109 | the \tech{double-negation translation}~\cite{morrisett1998:ftotal}, but 110 | extending this translation to \tech{dependent types} has proven challenging. 111 | \citet{barthe1999} showed how to scale typed call-by-name 112 | (CBN) CPS translation to a large class of Pure Type Systems (PTSs), including 113 | the Calculus of Constructions (CC) without \(\Sigma\) types. 114 | To avoid certain technical difficulties (which I discuss 115 | in \fullref[]{sec:cps:cbn}), they consider only \emph{domain-free} PTSs, a 116 | variant of PTSs where \im{\lambda} abstractions do not carry the domain of their 117 | bound variable---\ie, they are of the form \im{\lambda x.\, e} instead 118 | of \im{\lambda x:A.\, e} as in the \emph{domain-ful} variant. 119 | \citet{barthe2002} tried to extend these results to the Calculus of 120 | Inductive Constructions (\tech{CIC}), but ended up reporting a negative 121 | result, namely that the \tech{CBN} \tech{CPS} \tech{double-negation translation} 122 | is \emph{not type preserving} for \tech{dependent pairs}. 123 | They go on to prove a general impossibility result: for \tech{dependent 124 | conditionals} (in particular, sum types with dependent case analysis) 125 | \tech{type preservation} is \emph{not possible} when the \tech{CPS} 126 | translation admits unrestricted control effects. 127 | 128 | In this chapter, I use the locally polymorphic CPS translation and prove type 129 | preservation for the core features of dependency that were proven ``impossible'' 130 | by \citeauthor{barthe2002}. 131 | The key is that the polymorphic CPS uses parametricity to guarantee absence of 132 | control effects. 133 | This first step is an over-approximation; we only need to prevent control 134 | effects in the presence of certain dependencies. 135 | However, it allows us to ignore effects and focus only on dependent-type 136 | preservation. 137 | -------------------------------------------------------------------------------- /chapters/cps/related.tex: -------------------------------------------------------------------------------- 1 | \section{Related and Future Work} 2 | \label{sec:cps:related} 3 | 4 | \paragraph{Dependent Conditionals} 5 | In addition to showing that the traditional double-negation CPS for \(\Sigma\) 6 | types is not type preserving, \citet{barthe2002} demonstrate an impossibility 7 | result for CPS and dependent conditionals. 8 | They prove that no type-correct CPS translation can exist for sums with 9 | dependent case. 10 | However, careful inspection of their proof reveals that this impossibility 11 | result only applies if the CPS translation allows unrestricted control effects. 12 | As my translation does not allow control effects, I conjecture it is possible to 13 | prove type preservation for dependent conditionals. 14 | 15 | The impossibility result by \citeauthor{barthe2002} relies on the ability to 16 | implement \texttt{call/cc} via the CPS translation. 17 | Assuming there is a type-preserving CPS translation, they construct a model of CoC extended with 18 | \texttt{call/cc} and sum types (CC\(\Delta+\)) in CoC with sum types (CoC+). 19 | Since CoC+ is consistent, this model proves that CoC\(\Delta+\) is consistent. 20 | However, it is known that CoC\(\Delta+\) is inconsistent~\cite{coquand1989}. 21 | Therefore, the type-preserving CPS translation of CoC\(\Delta\)+ cannot exist. 22 | 23 | Their proof is valid; however, it is well known that the polymorphic answer type 24 | CPS translation that I use cannot implement 25 | \texttt{call/cc}~\cite{ahmed2011}. 26 | Therefore, my translation does not give a model of CoC\(\Delta+\) in CoC+. 27 | 28 | I further conjecture that my CPS translation is type preserving for dependent 29 | conditionals. 30 | Since the original work by \citeauthor{barthe2002} uses sums with dependent 31 | case, I use these instead of the dependent if presented in 32 | \fullref[]{chp:source}, but the same core idea applies to either implementation 33 | of dependent conditionals. 34 | The typing rule for dependent case is the following. 35 | \begin{displaymath} 36 | \inferrule 37 | {\styjudg{\slenv,\sy:\ssumty{\sA}{\sB}}{\sApr}{\sstarty} \\ 38 | \styjudg{\slenv}{\se}{\ssumty{\sA}{\sB}} \\ 39 | \styjudg{\slenv,\sx:\sA}{\seone}{\subst{\sApr}{\ssume{1}{\sx}}{\sy}} \\ 40 | \styjudg{\slenv,\sx:\sB}{\setwo}{\subst{\sApr}{\ssume{2}{\sx}}{\sy}} 41 | } 42 | {\styjudg{\slenv}{\scasee{\se}{\sx}{\seone}{\sx}{\setwo}}{\subst{\sApr}{\se}{\sy}}} 43 | \end{displaymath} 44 | 45 | I would extend the \cbnname{} with the following rule. 46 | \begin{displaymath} 47 | (\scasee{\se}{\sx}{\seone}{\sx}{\setwo})^\div = 48 | \begin{stackTL} 49 | \cpsfune{\cpsalpha}{\cpsstarty}{ 50 | \cpsfune{\cpsk}{\cpsfunty{\subst{\sA^{\sprime+}}{\se^\div}{\cpsy}}{\cpsalpha}}{ 51 | \\\quad 52 | \cpscappe{\se^\div}{\cpsalpha}{(\cpsfune{\cpsy}{\cpssumty{\sA^+}{\sB^+}}{ 53 | \cpscasee{\cpsy}{\cpsx}{(\cpsappe{\seone^\div}{\cpsalpha~\cpsk})}{\cpsx}{(\cpsappe{\setwo^\div}{\cpsalpha~\cpsk})}})}}} 54 | \end{stackTL} 55 | \end{displaymath} 56 | 57 | Focusing on the first branch of the \im{\tfont{case}} above, note that 58 | \im{\cpsappe{\seone^\div}{\cpsalpha} : 59 | \cpsfunty{(\cpsfunty{\subst{\sA^{\sprime+}}{(\ssume{1}{\sx})^\div}{\cpsy}}{\cpsalpha})}{\cpsalpha}}, 60 | however \im{\cpsk : {\cpsfunty{\subst{\sA^{\sprime+}}{\se^\div}{\cpsy}}{\cpsalpha}}}. 61 | We need to show that \im{\se^\div \equiv (\ssume{1}{\sx})^\div}, similar to the problem with 62 | the second projection of dependent pairs. This time, however, to show 63 | \im{\se^\div \equiv (\ssume{1}{\sx})^\div} we need to reason 64 | about the flow of the underlying value of $\se^\div$ into $\cpsy$ 65 | and also about the relationship between $\cpsy$ and $\cpsx$. 66 | Specifically, we need to first use my new \rulename{T-Cont} rule, which 67 | allows us to assume \im{\cpsy = 68 | \cpsappe{\se^\div}{\cpsalpha~\cpsidk}}. Next, 69 | we need to know that since the case analysis is on the value $\cpsy$, 70 | in the first branch \im{\cpsy \equiv \cpssume{1}{\cpsx}} (and similarly 71 | for the other branch), but the problem is that the existing typing 72 | rule for dependent case does not let us assume that. 73 | 74 | Nonetheless, I conjecture the same extension I propose in \fullref[]{chp:anf} 75 | works to allow type preservation for CPS. 76 | We simply change the typing rule for case to record the equality \im{\se \equiv 77 | \ssume{1}{\sx}} while typing the first branch, and similarly for the second 78 | branch, like in the following rule. 79 | \begin{displaymath} 80 | \inferrule 81 | {\styjudg{\slenv,\sy:\ssumty{\sA}{\sB}}{\sApr}{\sstarty} \\ 82 | \styjudg{\slenv}{\se}{\ssumty{\sA}{\sB}} \\ 83 | \styjudg{\slenv,\sx:\sA,\sp:\se = \ssume{1}}{\seone}{\subst{\sApr}{\ssume{1}{\sx}}{\sy}} \\ 84 | \styjudg{\slenv,\sx:\sB,\sp:\se = \ssume{2}}{\setwo}{\subst{\sApr}{\ssume{2}{\sx}}{\sy}} 85 | } 86 | {\styjudg{\slenv}{\scasee{\se}{\sx}{\seone}{\sx}{\setwo}}{\subst{\sApr}{\se}{\sy}}} 87 | \end{displaymath} 88 | With this modification in the target language \cps{}, we can type check the 89 | above translation of dependent case. 90 | I have not yet shown the consistency of the target language \cps{} once it is 91 | extended with this modified typing rule for dependent case, primarily due to 92 | problems scaling the CPS translation to higher universes, which I discuss next, 93 | and further in \fullref[]{chp:conclusions}. 94 | 95 | \paragraph{Higher Universes} 96 | It is unclear how to scale this CPS translation to higher universes. 97 | In this work, I have a single impredicative universe \im{\cpsstarty}, and the 98 | locally polymorphic answer type \im{\cpsalpha} lives in that universe. 99 | With an infinite hierarchy, it is not clear what the universe of \im{\cpsalpha} should be. 100 | Furthermore, the translation relies on impredicativity in \im{\cpsstarty}. 101 | We can only use impredicativity at one universe level and the locally 102 | polymorphic answer-type translation does not seem possible in the context of 103 | predicative polymorphism, so it's unclear how to adapt our translation to the 104 | infinite predicative hierarchy. 105 | 106 | I initially conjectured that the right solution for handling universes was 107 | universe polymorphism~\cite{sozeau2014}. 108 | My thought was that since the type is provided by the calling context, it seems 109 | sensible that the calling context should also provide the universe. 110 | Then, we could modify the type translation to be 111 | \im{\cpspity{\tfont{\ell}}{\tfont{Level}}{\cpspity{\cpsalpha}{\cpstypety{\tfont{\ell}}}{\cpsfunty{(\cpsfunty{\sA^+}{\cpsalpha})}{\cpsalpha}}}}. 112 | However, one of the authors of \citeauthor{sozeau2014} (Sozeau) suggested to me 113 | that this would not work.\footnote{Personal communication. Jan. 2018.} 114 | 115 | \paragraph{Control Operators and Dependent Types} 116 | This chapter explicitly avoids control effects and dependent types to focus on 117 | type preservation. 118 | However, in general, we may want to combine the two. 119 | \citet{herbelin2005} shows that unrestricted use of \texttt{call/cc} and 120 | \texttt{throw} in a language with \(\Sigma\) types and equality leads to an inconsistent system. 121 | The inconsistency is caused by type dependency on terms involving control effects. 122 | \citet{herbelin2012} solves the inconsistency by constraining types to depend only 123 | on \emph{negative-elimination-free (NEF)} terms, which are free of effects. 124 | This restriction makes dependent types compatible with classical reasoning 125 | enabled by the control operators. 126 | 127 | After the initial publication of the work in this 128 | chapter~\cite{bowman2018:cps-sigma}, \citet{cong2018:lam-pi-s-r} extended the 129 | CPS translation to control effects in the NEF fragment, and use this locally 130 | polymorphic translation to disallow control effects for terms that use unsafe dependencies. 131 | 132 | This is similar to recent work by \citet{miquey2017} uses the NEF restriction to 133 | soundly extend the \im{\bar{\lambda}\mu\tilde{\mu}}-calculus of 134 | \citet{curien2000}, a computational classic calculus, with 135 | dependent types. 136 | He then extends the language with delimited continuations, and defines a 137 | type-preserving CPS to a standard intuitionistic dependent type theory. 138 | By comparison, our source language \cpsslang is effect-free, 139 | therefore we do not need the NEF condition to restrict dependency. 140 | My use of the identity function serves a similar role to their delimited 141 | continuations---allowing local evaluation of a CPS'd computation. 142 | 143 | \paragraph{Effects and Dependent Types} 144 | \citet{pedrot2017:weaning} define the \emph{weaning translation}, a monadic translation for 145 | adding effects to dependent type theory. 146 | The weaning translation allows us to represent self-algebraic monads, \ie, 147 | monads whose algebras' universe itself forms an algebra, such as exception and 148 | non-determinism monads. 149 | However, it does not apply to the standard continuation monad, which is not self-algebraic. 150 | The paper extends the translation to inductive types, with a restricted form of 151 | dependent elimination. 152 | Full dependent elimination can be implemented only for terms whose type is 153 | first-order, and this comes at the cost of inconsistency, although one can 154 | recover consistency by requiring that every inductive term be parametric. 155 | My translation does not lead to inconsistency, and requires no restrictions on 156 | the type to be translated. 157 | However, my translation appears to impose the self-algebraic structure on the 158 | computation types, and my use of parametricity to cast computations to values is 159 | similar to their 160 | parametricity restriction. 161 | 162 | \paragraph{Internalizing Parametricity} 163 | My work internalizes a specific free theorem, but ongoing work focuses on how to internalize 164 | parametricity more generally in a dependent type theory. 165 | \citet{krishnaswami2013} develop a technique for adding new rules to the extensional CoC. 166 | They present a logical relation for terms that are not syntactically well typed, but are 167 | semantically well behaved and equivalent at a particular type. 168 | Using this logical relation, they prove the consistency of several extensions to extensional CoC, 169 | including sum types, dependent records, and natural numbers. 170 | \citet{bernardy2012,keller2012} give translations from one dependent type theory 171 | into another that yield a parametric model of the original theory. 172 | These essentially encode the logical relation in the target type theory, similar to the approach we 173 | took in \fullref{sec:cps:consistent}. 174 | Recent work by \citet{nuyts2017} develops a theory that internalizes parametricity, including the 175 | important concept of \emph{identity extension}, and gives a thorough comparison of the literature. 176 | By building a target language based on one of these systems, it's possible that 177 | we could eliminate the rule \rulename{\im{\equiv}-Cont} as an axiom and instead 178 | derive it internally. 179 | This could allow the translation to scale to theories that are orthogonal to 180 | parametricity. 181 | 182 | \paragraph{Pervasive Translation} 183 | In this chapter, I only CPS translate \emph{terms}, \ie, run-time expressions. 184 | However, other work~\cite{barthe2001} studies \emph{pervasive} translation of PTSs. 185 | A pervasive CPS translation internalizes evaluation order for all 186 | expressions: terms, types, and kinds. 187 | This is necessary in general, since in a language such as Coq, we cannot easily 188 | distinguish between terms, types, and kinds. 189 | A pervasive translation may be necessary to scale type-preserving translation to 190 | higher universes and more type-level computation. 191 | The translations in \fullref[]{chp:anf} and \fullref[]{chp:abs-cc} both use 192 | pervasive translation for this reason. 193 | 194 | A pervasive CPS translation is also used in a partial solution to the 195 | Barendregt-Geuvers-Klop conjecture~\cite{geuvers1993} which essentially states 196 | that every weakly normalizing PTS is also strongly normalizing. 197 | The conjecture has been solved for non-dependent PTSs, but the solution for 198 | dependent PTSs remains open. 199 | -------------------------------------------------------------------------------- /chapters/param-cc/ideas.tex: -------------------------------------------------------------------------------- 1 | \section{Main Ideas} 2 | \label{sec:param-cc:ideas} 3 | As review, recall from \fullref[]{chp:abs-cc} the problem with 4 | preserving \tech{dependent types} through \tech{closure conversion} using the 5 | \tech{existential types}. 6 | I reproduce the example from \fullref[]{chp:abs-cc} below. 7 | % 8 | \begin{displaymath} 9 | \begin{array}{rcl} 10 | \sfune{\sA}{\sstarty}{\sfune{\sx}{\sA}{\sx}} & : & \spity{\sA}{\sstarty}{\spity{\sx}{\sA}{\sA}} 11 | \end{array} 12 | \end{displaymath} 13 | % 14 | Here we have the polymorphic identity function, where the first 15 | argument \im{\sA}, a \tech{type}, is free in the second function and is used in 16 | the type of the function. 17 | The standard \tech{parametric closure conversion} translation of this example 18 | is below, reproduced from \fullref[]{chp:abs-cc}. 19 | % 20 | \begin{displaymath} 21 | \begin{stackTL} 22 | \tcloe{\tnfune{(\tnone:\tunitty,\tA:\tstarty)}{ 23 | \tcloe{\tnfune{(\tntwo: 24 | \tpairty{\tstarty}{\tunitty},\tx:\tfste{\tntwo})}{\tx} 25 | }{\tnpaire{\tA,\tnpaire{}}}}}{\tnpaire{}} ~~ : ~~\\ 26 | ~~\begin{array}{@{\hspace{0pt}}l@{\hspace{0pt}}l@{\hspace{0pt}}l} 27 | \texistty{\talphaone}{\tstarty}{&\,\tpairty{(\tnpity{&(\tnone:\talphaone,\tA:\tstarty)}{ 28 | \\ && 29 | \texistty{\talphatwo}{\tboxty}{\tpairty{(\tnpity{(\tntwo:\talphatwo,\tx:\tfste{\tntwo})}{\tfste{\tntwo}})}{\talphatwo}}})\,}{\talphaone}} 30 | \end{array} 31 | \end{stackTL} 32 | \end{displaymath} 33 | % 34 | Unfortunately, while the \tech{code} is well-typed, the \tech{closure} is not. 35 | Recall that the code of the inner \tech{closure} has 36 | type \im{\tnpity{(\tntwo:\talphatwo,\tx:\tfste{\tntwo})}{\tfste{\tntwo}}}. 37 | In the type of the \tech{closure}, this projection \im{\tfste{\tntwo}} is not 38 | well-typed since the type of the \tech{environment} is 39 | hidden---\im{\tfste{\tntwo}} is invalid when \im{\tntwo} has 40 | type \im{\pccalphatwo}. 41 | 42 | A similar problem presents itself in the \tech{type-preserving} \tech{closure 43 | conversion} of System F by~\citet{minamide1996}. 44 | In that work, \tech{type} variables in System F are included in the 45 | \tech{environment}, introducing the same problem as above of \tech{closures} not 46 | being well-typed. 47 | In subsequent work, \citet{morrisett1998:ftotal} observed that in System F 48 | \tech{type} variables can be considered \tech{computationally irrelevant} (and will, 49 | therefore, be erased before runtime). 50 | This justified a simple translation where \tech{closure-converted} functions 51 | were allowed to have free \tech{type} (though not \tech{term}) variables. 52 | In \tech{dependently typed} languages, this solution does not apply since 53 | \tech{term} variables can also appear in \tech{types}. 54 | 55 | As before in \fullref[]{chp:abs-cc}, and in the work of \citet{minamide1996}, we 56 | have a dilemma. 57 | We must close the \tech{type} of \tech{code} since \tech{code} must be closed 58 | after \tech{closure conversion}, but we must also leave the \tech{type} of 59 | the \tech{closure} open since we cannot project from the 60 | hidden \tech{environment}. 61 | The challenge is to unify the closed type of the \tech{code} and the open type of 62 | the \tech{closure}. 63 | 64 | In this chapter, I adapt the \tech{parametric closure conversion} of 65 | \citet{minamide1996} to \tech{dependent types}. 66 | Previously, in \fullref[]{chp:abs-cc}, I essentially axiomatize the target language 67 | with \tech{closures}, adapting the \tech{abstract closure conversion} 68 | of \citet{minamide1996} to \tech{dependent types}. 69 | The primitive \tech{closure} captures exactly the above needs. 70 | I use \tech{existential types} to encode closures, and use a form 71 | of \tech{singleton types}, called \tech{translucent types}, to encode an 72 | equality between the hidden \tech{environment} and the free variables. 73 | The \tech{translucent type} unifies the hidden \tech{environment} with the type 74 | variables, thus unifying the closed type of the \tech{closure} and the open type 75 | of the \tech{code}. 76 | 77 | Below is the nearly complete translation to demonstrate how this unifies the two 78 | seemingly different types.% 79 | \marginpar{Notice that this type translation relies on \tech{impredicativity}, 80 | since the \tech{existential type} must be in the same \tech{universe} as the 81 | type variable \im{\alpha} over which it quantifies.}% 82 | \begin{displaymath} 83 | \begin{stackTL} 84 | \cctrans{(\spity{\sx}{\sA}{\sB})} = 85 | \pccexistty{\pccalpha}{\pccstarty}{(\pccpairty{\pccalpha}{\pcccodety{\pccn:\pccalpha,\pccx:\sA^\plus}{\cctrans{\sB}}})}\\ 86 | \cctrans{(\sfune{\sx}{\sA}{\se})} = \begin{stackTL}\pccpackoe{\pccApr}{\pccpaire{\pccnpaire{\pccxi\dots}}{\begin{stackTL}\pccnfune{(\pccn:\pccApr, \pccx:\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\cctrans{\sA}})}{ 87 | \\\quad\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\cctrans{\se}}}}} 88 | \end{stackTL}\\ 89 | \where{\begin{stackTL} 90 | \pccApr = \pccnsigmaty{\pccxi:\cctrans{\sAi}\dots} \\ 91 | \sxi:\sAi\dots \text{ are the free variables of \im{\se} and \im{\sA}}} 92 | \end{stackTL} 93 | \end{stackTL} 94 | \end{stackTL} 95 | \end{displaymath} 96 | % 97 | This translation uses the \tech{code} type as in \fullref[]{chp:abs-cc} to 98 | distinguish closed \tech{code} from \tech{closures}, but lacks a \(\Pi\)-type 99 | since \tech{closures} are encoded using existential types. 100 | Using this translation, we need to complete the following derivation in order to 101 | prove \tech{type preservation}. 102 | As we will see, we cannot complete the derivation for the 103 | standard \tech{existential type translation}. 104 | % 105 | \begin{displaymath} 106 | \inferrule 107 | { 108 | \inferrule 109 | {\text{stuck}} 110 | {\pcctyjudg{\pccn:\pccApr,\pccx:\pccA}{\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\pcce}}{\cctrans{\sB}}}} 111 | {\pcctyjudg{\pcclenv}{\pccnfune{(\pccn:\pccApr,\pccx:\pccA)}{{\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\pcce}}}}{\pcccodety{\pccn:\pccApr,\pccx:\cctrans{\sA}}{\cctrans{\sB}}}} 112 | \end{displaymath} 113 | % 114 | I focus on showing the \tech{code} is well-typed at the translated type, since 115 | that is where the problem arises. 116 | Notice that \im{\cctrans{\sB}} has free variables in the \tech{type}. 117 | To complete this derivation, we must show 118 | \im{{\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\pcce}}:\cctrans{\sB}}. 119 | However, by the typing rule for \tech{dependent let}, we can only conclude 120 | \im{\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\pcce} : \subst{\cctrans{\sB}}{\pccprje{i}{\pccn}}{\pccxi}}. 121 | We cannot complete the derivation and this translation is not \tech{type 122 | preserving} because \tech{dependent types} allow \tech{types} to depend on 123 | free \tech{term} variables from the \tech{environment}. 124 | 125 | However, intuitively, it should be the case that \im{\cctrans{\sB} \equiv 126 | \subst{\cctrans{\sB}}{\pccprje{i}{\pccn}}{\pccxi}}. 127 | By \tech{parametricity}, we know \im{\pccn} will only get replaced by 128 | exactly the \tech{environment} generated by our compiler. 129 | \marginpar{This is why we rely on \tech{parametricity}, and where the \tech{type 130 | preservation} argument fails if we cannot have \tech{parametricity} in the 131 | target language.} 132 | Since the compiler generates the \tech{environment} \im{\pccn = 133 | \pccnpaire{\pccxi\dots}}, this substitution is essentially a no-op, replacing 134 | \im{\pccxi} by \im{\pccxi}. 135 | To develop a \tech{type-preserving} translation, we need to find a way to 136 | internalize this reasoning so that the type system can decide this equivalence. 137 | 138 | To solve this, I use a form of singleton types called \tech{translucent 139 | types}~\cite{harper1994,lillibridge1997} to encode this \tech{equivalence} in a 140 | \tech{type}. 141 | The \deftech*{Translucent types,translucent types,translucent type,translucent 142 | function,translucent function type,translucent function types}{translucent 143 | function type}\footnote{This particular presentation of translucent function 144 | types is due to \citet{minamide1996}.}, 145 | written \im{\pcctrlufunty{\pccepr}{\pccB}}, represents a function (or in our 146 | case, \tech{code}) that must be applied to (an expression \tech{equivalent} to) 147 | the term \im{\pccepr} and produces something of type \im{\pccB}. 148 | The \tech{translucent function} type rules are the following. 149 | Essentially, any function \im{\pccf} can be statically specialized to a 150 | particular argument \im{\pccepr}. 151 | This has the effect of instantiating the \tech{dependent function} type before 152 | the function is actually applied. 153 | After that, it must be dynamically applied to that argument. 154 | \begin{mathpar} 155 | \inferrule*[right=\rulename{TrFun}] 156 | {\pcctyjudg{\pcclenv}{\pccf}{\pccpity{\pccn}{\pccApr}{\pccB}}\\ 157 | \pcctyjudg{\pcclenv}{\pccepr}{\pccApr}} 158 | {\pcctyjudg{\pcclenv}{\pccf}{\pcctrlufunty{\pccepr}{\subst{\pccB}{\pccepr}{\pccn}}}} 159 | 160 | \inferrule*[right=\rulename{TrApp}] 161 | {\pcctyjudg{\pcclenv}{\pccf}{\pcctrlufunty{\pccepr}{\pccB}}} 162 | {\pcctyjudg{\pcclenv}{\pccappe{\pccf}{\pccepr}}{\pccB}} 163 | \end{mathpar} 164 | 165 | To encode \tech{closures}, we existentially quantify over the \emph{value} of 166 | the \tech{environment} \im{\pccn}, in addition to the \tech{type} of the 167 | \tech{environment} \im{\pccalpha}, leveraging \tech{dependent types}. 168 | Then we describe type of the \tech{code} \im{\pccf} using a \tech{translucent type} 169 | \im{(\pcctrlufunty{\pccn}{\pcccodety{\pccx:\cctrans{\sA}}{\cctrans{sB}}})}, which 170 | requires that the \tech{code} be applied to exactly the \tech{environment} 171 | \im{\pccn} and an arbitrary argument \im{\pccx} of type \im{\cctrans{\sA}}, and 172 | produces an output of type \im{\cctrans{\sB}}. 173 | The translation becomes the following. 174 | \begin{displaymath} 175 | \begin{stackTL} 176 | \cctrans{(\spity{\sx}{\sA}{\sB})} = 177 | \pccnexistty{(\pccalpha:\pccstarty,\pccn:\pccalpha,\pccf:(\pcctrlufunty{\pccn}{\pcccodety{\pccx:\cctrans{\sA}}{\cctrans{\sB}}}))} \\ 178 | \cctrans{(\sfune{\sx}{\sA}{\se})} = 179 | \begin{stackTL}\pccnpackoe{\pccApr,\pccnpaire{\pccxi\dots},\begin{stackTL}\pccnfune{(\pccn:\pccApr,\pccx:\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\cctrans{\sA}})}{\\\quad\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\cctrans{\se}}}} 180 | \end{stackTL}\\ 181 | \where{ 182 | \begin{stackTL} 183 | \pccApr = \pccnsigmaty{(\pccxi:\cctrans{\sAi}\dots)} \\ 184 | \sxi:\sAi \dots \text{ are the free variables of \im{\se} and \im{\sA}} 185 | \end{stackTL}} 186 | \end{stackTL} 187 | \end{stackTL} 188 | \end{displaymath} 189 | Now, the \tech{environment} is still hidden from the client of the 190 | \tech{closure}, but when checking that the \tech{closure} is well-typed, the 191 | variable \im{\pccn} is replaced by the value of the \tech{closure}. 192 | Then all we need is to prove \im{\cctrans{\sB} \equiv 193 | \subst{\cctrans{\sB}}{\pccprje{i}{\pccnpaire{\pccxi\dots}}}{\pccxi}}, which is trivial. 194 | This was essentially the insight in \citet{minamide1996}, although in a simpler setting. 195 | 196 | As we descend into the \pccfonttext{pack} expression, the existentially 197 | quantified environment flows into the \tech{type} and the \tech{translucent 198 | type} yields an additional \tech{equivalence}. 199 | The completed derivation is the following. 200 | {\small 201 | \begin{mathpar} 202 | \inferrule 203 | { 204 | \inferrule 205 | { 206 | \inferrule 207 | {\vdots} 208 | {\pcctyjudg{\pcclenv}{\pccnfune{(\pccn:\pccApr,\pccx:\cctrans{\sA})}{\pccepr}}{\pcccodety{\pccn:\pccApr,\pccx:\cctrans{\sA}}{\subst{\cctrans{\sB}}{\pccprje{i}{\pccn}}{\pccxi}}}} 209 | \quad 210 | \inferrule 211 | { 212 | \text{trivial, since } env = \pccnpaire{\pccxi\dots}} 213 | {\subst{\subst{\cctrans{\sB}}{\pccprje{i}{\pccn}}{\pccxi}}{env}{\pccn} \equiv \cctrans{\sB}}} 214 | {\vdots \qquad\qquad\qquad \pcctyjudg{\pcclenv}{\pccnfune{(\pccn:\pccApr,\pccx:\cctrans{\sA})}{\pcclete{\pccnpaire{\pccxi\dots}}{\pccn}{\cctrans{\se}}}}{\pcctrlufunty{env}{\pcccodety{\pccx:\cctrans{\sA}}{\cctrans{\sB}}}}} 215 | } 216 | {\pcctyjudg{\pcclenv}{\pccnpackoe{\pccApr,env,\pccnfune{(\pccn:\pccApr,\pccx:\cctrans{\sA})}{\pccepr}}}{\pccnexistty{\pccalpha:\pccstarty,\pccn:\pccalpha,\pccf:(\pcctrlufunty{\pccn}{\pcccodety{\pccx:\cctrans{\sA}}})}{\cctrans{\sB}}}} 217 | \end{mathpar} 218 | } 219 | 220 | While the translation is similar in essence to \citet{minamide1996}, \tech{dependent types} 221 | introduce additional difficulties. 222 | The above translation disrupts the syntactic reasoning about functions 223 | that the type system relies on, in particular, \(\eta\)-equivalence of functions. 224 | 225 | To preserve \(\eta\)-equivalence, as in \fullref[]{chp:abs-cc}, I develop a 226 | principle of \tech{equivalence} for \tech{closures}. 227 | Essentially, \tech{closures} are \tech{equivalent} when we compute \tech{equivalent} 228 | results under \tech{equivalent} \tech{environments}. 229 | This gives us an \(\eta\)-principle for \tech{closures} that extends 230 | the \(\eta\)-principle for functions to include the \tech{environment}. 231 | This principle also relies on \tech{parametricity} to justify. 232 | -------------------------------------------------------------------------------- /chapters/anf/ideas.tex: -------------------------------------------------------------------------------- 1 | \section{Main Ideas} 2 | \label{sec:anf:ideas} 3 | The idea behind ANF is to make control flow explicit in the syntax of a program 4 | so transfer of control can be expressed as a jump. 5 | ANF encodes \emph{computation} (\eg, reducing an expression to a value) as 6 | sequencing simple intermediate computations with \im{let} expressions. 7 | To reduce \im{\se} to a value in a high-level language, we need to describe the 8 | evaluation order and control flow of each language primitive. 9 | For example, if \im{\se} is an application \im{\sappe{\seone}{\setwo}} and we 10 | want a call-by-value semantics, we say the language first evaluates 11 | \im{\seone} to a value, then evaluates \im{\setwo} to a value, then performs 12 | the function application. 13 | ANF makes this explicit in the syntax by decomposing \im{\se} into a 14 | series of primitive computations sequenced by \tfonttext{let}, as in 15 | \im{\tnlete{\txin{0}=\tNin{0},...,\txin{n}=\tNin{n}}{\tN}}, where each 16 | \im{\tNin{i}} is either a value or a primitive computation applied 17 | to a value. 18 | The ANF translation of \im{\sappe{\seone}{\setwo}} is the following. 19 | \begin{mathpar} 20 | \tnlete{\begin{stackTL} 21 | \txin{1_0}=\tNin{1_0},...,\txin{1_n}=\tNin{1_n}, 22 | \txin{1}=\tNin{1}\\ 23 | \txin{2_0}=\tNin{2_0},...,\txin{2_n}=\tNin{2_n}, 24 | \txin{2}=\tNin{2}\\} 25 | {(\tappe{\txone}{\txtwo})} 26 | \end{stackTL} 27 | \end{mathpar} 28 | \begin{mathpar} 29 | \text{where~}\begin{stackTL} 30 | \sembrace{\seone} = (\tnlete{\txin{1_0}=\tNin{1_0},...,\txin{1_n}=\tNin{1_n}}{\tNin{1}})\\ 31 | \sembrace{\setwo} = (\tnlete{\txin{2_0}=\tNin{2_0},...,\txin{2_n}=\tNin{2_n}}{\tNin{2}})\\ 32 | \end{stackTL} 33 | \end{mathpar} 34 | 35 | Roughly, we can think of this translation as 36 | \im{\tnlete{\txone=\sembrace{\seone},\txtwo=\sembrace{\setwo}}{\tappe{\txone}{\txtwo}}}. 37 | This rough translation is only ANF if \im{\seone} and \im{\setwo} are values or 38 | primitive computations. 39 | In general, the ANF translation reassociates all the intermediate computations 40 | from \im{\sembrace{\seone}} and \im{\sembrace{\setwo}} so there are no nested 41 | \tfonttext{let} expressions. 42 | Once in ANF, it is simple to formalize a machine semantics to implement 43 | evaluation by always reducing the left-most computation, which will be a 44 | primitive operation. 45 | 46 | The problem in developing a type-preserving translation for a dependently typed 47 | language is that changing the structure of a program disrupts 48 | the \tech{dependencies} described in \fullref[]{chp:source}, \ie, 49 | an expression \im{\sepr} whose type and evaluation depends on a sub-expression 50 | \im{\se}. 51 | Recall that I call to a sub-expression such as \im{\se} \emph{depended upon}. 52 | This pattern happens in dependent elimination forms, such as application and 53 | projection. 54 | For example, for a dependent function \im{\seone : \spity{\sx}{\sA}{\sB}}, 55 | the application is typed as \im{\sappe{\seone}{\setwo} : {\subst{\sB}{\setwo}{\sx}}}. 56 | Notice that the \emph{depended upon} sub-expression \im{\setwo} is copied into the type. 57 | If we transform the expression \im{\sappe{\seone}{\setwo}}, we can easily change 58 | the type. 59 | For example, recall the ANF translation for this expression given earlier. 60 | \begin{displaymath} 61 | \tnlete{\begin{stackTL} 62 | \txin{1_0}=\tNin{1_0},...,\txin{1_n}=\tNin{1_n}, 63 | \txin{1}=\tNin{1}\\ 64 | \txin{2_0}=\tNin{2_0},...,\txin{2_n}=\tNin{2_n}, 65 | \txin{2}=\tNin{2}\\} 66 | {(\tappe{\txone}{\txtwo})} 67 | \end{stackTL} 68 | \end{displaymath} 69 | Using the standard typing rule for dependent \tfonttext{let} (without 70 | definitions), the type of the ANF translation above is 71 | \im{\subst{\sembrace{\sB}}{\txtwo}{\tx}} (the type of the body), with all 72 | \tfonttext{let}-bindings substituted into this type, \ie, roughly 73 | \im{\subst{\subst{\sembrace{\sB}}{\txtwo}{\tx}}{\tNin{2_0}}{\txin{2_0}}[...][\tNin{2_n}/\txin{2_n}][\tNtwo/\txtwo]}. 74 | To show type preservation, we must show that the target type system can prove 75 | that this type is equivalent to the translation of the original type, \ie, 76 | \im{\sembrace{\subst{\sB}{\setwo}{\sx}}}. 77 | Intuitively, it \emph{ought} to be true that the intermediate computations 78 | \im{(\tNin{2_0},...,\tNin{2_n},\tNin{2})} are 79 | equivalent to \im{\sembrace{\setwo}}, since those are the computations that 80 | must happen to compute the value of \im{\setwo}. 81 | Therefore, the two types \emph{ought} to be equivalent. 82 | 83 | But the intuitive argument that ANF ought to be type preserving is wrong. 84 | Instead of the \im{(\tappe{\txone}{\txtwo})} appearing in the body directly as 85 | above, consider the expression below in which \im{(\tappe{\txone}{\txtwo})} is 86 | bound and then used. 87 | \begin{mathpar} 88 | \tnlete{...}{\tlete{\ty}{(\tappe{\txone}{\txtwo})}{\tappe{\tf}{\ty}}} 89 | 90 | \where{\tf : (\subst{\sembrace{\sB}}{\sembrace{\setwo}}{\tx}) \to \tC} 91 | \end{mathpar} 92 | To show type preservation, we now need to reestablish a dependent type that is 93 | \tfonttext{let}-bound, instead of in the body of a \tfonttext{let}. 94 | The type derivation fails, as follows. 95 | \begin{mathpar} 96 | \inferrule 97 | {\inferrule 98 | {~} 99 | {... \vdash {(\tappe{\txone}{\txtwo})} : \subst{\sembrace{\sB}}{\txtwo}{\tx}}\\ 100 | \inferrule 101 | {\text{fails}} 102 | {...,\ty:\subst{\sembrace{\sB}}{\txtwo}{\tx} \vdash \tappe{\tf}{\ty} : \tC}} 103 | {... \vdash \tlete{\ty}{(\tappe{\txone}{\txtwo})}{\tappe{\tf}{\ty}} : \subst{\tC}{(\tappe{\txone}{\txtwo})}{\ty}} 104 | \end{mathpar} 105 | This fails since \im{\tf} expects 106 | \im{\ty : (\subst{\sembrace{\sB}}{\sembrace{\setwo}}{\tx})}, but is applied to 107 | \im{\ty : \subst{\sembrace{\sB}}{\txtwo}{\tx}}. 108 | We cannot show the two types are equal without substituting all the (elided) 109 | bindings as described earlier, but that substitution happens after it is needed. 110 | The problem is that the dependent typing rule for \tfonttext{let} only binds a 111 | depended-upon expression in the type of the \emph{body} of the \tfonttext{let}, 112 | not in the types of \emph{bound expressions}. 113 | 114 | The typing rule for \tfonttext{let} only allows dependency in the type of the 115 | overall expression---that is, in the ``output'' of the typing judgment---but we 116 | need some way to thread dependencies into the sub-derivations while checking 117 | bound expressions. 118 | For example, if we could express dependencies in the \emph{input}, something 119 | like \im{... \txtwo = \sembrace{\setwo} \vdash {(\tappe{\txone}{\txtwo})} : 120 | \subst{\sembrace{\sB}}{\sembrace{\setwo}}{\tx}}, then we could complete the 121 | derivation and prove type-preservation. 122 | And this is exactly the intuition we formalize. 123 | 124 | We formalize this intuition using \emph{definitions}~\cite{severi1994:dpts} 125 | introduced in \fullref[]{chp:source}. 126 | Recall that the typing rule for \tfonttext{let} with definitions is the following. 127 | \begin{mathpar} 128 | \inferrule 129 | {\ttyjudg{\tlenv}{\te}{\tA}\\ 130 | \ttyjudg{\tlenv,\tx=\te}{\tepr}{\tApr}} 131 | {\ttyjudg{\tlenv}{\tlete{\tx}{\te}{\tepr}}{\subst{\tApr}{\te}{\tx}}} 132 | \end{mathpar} 133 | 134 | The definition \im{\tx = \te} is introduced when type checking the body of the 135 | \tfonttext{let}, and can be used to solve type equivalence in sub-derivations, 136 | instead of only in the substitution \im{\subst{\tApr}{\te}{\tx}} in the "output" 137 | of the typing rule. 138 | While this is an extension to the type theory (so one may worry that the 139 | \tech{ANF} translation applies to fewer dependently typed languages), it is a 140 | standard extension that is admissible in any Pure Type System 141 | (PTS)~\cite{severi1994:dpts}, and is a feature already found in dependently 142 | typed languages such as Coq. 143 | 144 | Using definitions, we can prove that, under the definitions \im{\txin{2_0} = 145 | \tNin{2_0}, ..., \txin{2_n} = \tNin{2_n},\txtwo = \tNtwo} produced from the ANF translation 146 | \im{\sembrace{\setwo}} (written \im{\edefs{\setwo}}), the desired equivalence 147 | \im{\txtwo \equiv \sembrace{\setwo}} holds. 148 | We use \im{\ehole{\setwo}} to refer to the innermost computation produced by 149 | the ANF translation, in this case \im{\ehole{\setwo} = \tNtwo}. 150 | Then we can prove \im{\edefs{\setwo} \vdash \ehole{\setwo} \equiv \anfh{\setwo}} 151 | (formally captured by \fullref[]{lem:anf:export-equal}). 152 | Since we also have the definition, \im{\txtwo = \tNtwo}, we conclude that 153 | \im{\edefs{\setwo} \vdash \txtwo \equiv \anfh{\setwo}}. 154 | 155 | To formalize this type preservation argument, we need to step back and define 156 | the ANF translation precisely. 157 | In the source, looking at an expression such as \im{\sappe{\seone}{\setwo}}, we 158 | do not know whether the expression is embedded in a larger context. 159 | This matters in ANF, since we can no longer compose expressions by nesting, but 160 | instead must compose expressions with \tfonttext{let}. 161 | This is why the above example translation disassembled the translation of 162 | \im{\seone} and \im{\setwo} into a new, larger, \tfonttext{let} expression. 163 | To formalize the ANF translation, it helps to have a more compositional syntax 164 | for translating an expression and composing it with an unknown context. 165 | 166 | I develop a compositional translation by indexing the ANF translation by a target language 167 | (non-first-class) \emph{continuation} \im{\tK} representing the rest of the computation in which a translated expression will be used. 168 | A continuation \im{\tK} is a program with a hole (single linear variable) 169 | \im{\hole}, and can be composed with a computation \im{\tK\hw{\tN}} to form a 170 | program \im{\tM}. 171 | In ANF, there are only two continuations: either \im{\hole} or 172 | \im{\tlete{\tx}{\hole}{\tM}}. 173 | Using continuations, we define ANF translation for functions and application as 174 | follows. 175 | \begin{displaymath} 176 | \begin{aligned}[t] 177 | \anf{\sfune{\sx}{\sA}{\se}}{\tK} &= \tK\hw{\tfune{\tx}{(\anf{\sA}{\hole})}{(\anf{\se}{\hole})}} \\ 178 | \anf{\sappe{\seone}{\setwo}}{\tK} &= \anf{\seone}{\tlete{\txone}{\hole}{\anf{\setwo}{\tlete{\txtwo}{\hole}{\tK\hw{\tappe{\txone}{\txtwo}}}}}} 179 | \end{aligned} 180 | \end{displaymath} 181 | This allows us to focus on composing the primitive operations instead of 182 | reassociating \tfonttext{let} bindings. 183 | 184 | The key typing rule for continuations is the following. 185 | \begin{mathpar} 186 | \inferrule*[right=\rulename{K-Bind}] 187 | {\styjudg{\tlenv}{\tN}{\tA} \\ 188 | \styjudg{\tlenv,\ty=\tN}{\tM}{\tB}} 189 | {\styjudg{\tlenv}{\tlete{\ty}{\hole}{\tM}}{(\tN : \tA) \Rightarrow \tB}} 190 | \end{mathpar} 191 | The type \im{(\tN : \tA) \Rightarrow \tB} of continuations describes that the 192 | continuation must be composed with the term \im{\tN} of type \im{\tA}, and the 193 | result will be of type \im{\tB}. 194 | This expresses syntactically the intuition that continuations must be used 195 | linearly to avoid control effects, which are known to cause inconsistency with dependent 196 | types~\cite{barthe2002,herbelin2005}. 197 | Note that this type allows us to introduce the definition \im{\ty = \tN} via the 198 | type, before we know how the continuation is used.\footnote{This is 199 | essentially a singleton type, but as mentioned in \fullref[]{chp:source}, I 200 | avoid explicit encoding with singleton types to focus on the intuition: 201 | tracking dependencies.} 202 | I discuss this rule further in \fullref[]{sec:anf:target}. 203 | The \fullref{lem:anf:ecca:cut} expresses that continuation typing is not an 204 | extension to the target type theory, which is important to ensure ANF can be 205 | applied in practice. 206 | 207 | The key lemma to prove type preservation is the following. 208 | \begin{lemma} 209 | If \im{\styjudg{\slenv}{\se}{\sA}} and \im{\ttyjudg{\tlenv,\edefs{\se}}{\tK}{(\ehole{\se}:\sembrace{\sA}) \Rightarrow \tB}}, then \im{\ttyjudg{\tlenv}{\anf{\se}{\tK}}{\tB}}. 210 | \end{lemma} 211 | \noindent This lemma captures the fact that each time we build a new \im{\tK} in the ANF translation, we must show it is 212 | well-typed, and that is where we apply the reasoning about definitions. 213 | Proving that \im{\tK} has the above type requires proving \im{\tlenv,\edefs{\se} \vdash 214 | \ehole{\se} : \anfh{\sA}}. 215 | For our running example, this means proving 216 | \im{\tlenv,\edefs{\sappe{\seone}{\setwo}} \vdash \tappe{\txone}{\txtwo} : 217 | \anfh{\subst{\sB}{\setwo}{\sx}}}. 218 | Recall that \im{\tappe{\txone}{\txtwo} : \subst{\anfh{\sB}}{\txtwo}{\tx}}. 219 | As we saw earlier, definitions allow us to prove that \im{\tlenv,\edefs{\sappe{\seone}{\setwo}} 220 | \vdash \txtwo \equiv \sembrace{\setwo}}; therefore, combined with compositionality 221 | (\im{\anfh{\subst{\sB}{\setwo}{\sx}} \equiv 222 | \subst{\anfh{\sB}}{\anfh{\setwo}}{\tx}}, \fullref[]{lem:anf:subst}), the proof is complete! 223 | The earlier failing derivation now succeeds: 224 | 225 | \begin{mathpar} 226 | \inferrule 227 | {\inferrule 228 | {\fullref[]{lem:anf:subst} \\ 229 | \fullref[]{lem:anf:export-equal}} 230 | {\edefs{\sappe{\seone}{\setwo}} \vdash {(\tappe{\txone}{\txtwo})} : \subst{\sembrace{\sB}}{\anfh{\setwo}}{\tx}}\quad 231 | \inferrule 232 | {\text{succeeds!}} 233 | {\ty:\subst{\sembrace{\sB}}{\anfh{\setwo}}{\tx} \vdash \tappe{\tf}{\ty} : \tC}} 234 | {\edefs{\sappe{\seone}{\setwo}} \vdash \tlete{\ty}{(\tappe{\txone}{\txtwo})}{\tappe{\tf}{\ty}} : \subst{\tC}{(\tappe{\txone}{\txtwo})}{\ty}} 235 | \end{mathpar} 236 | -------------------------------------------------------------------------------- /defs.tex: -------------------------------------------------------------------------------- 1 | % Misc 2 | 3 | \let\lemma\undefined 4 | \newtheorem{lemma}[theorem]{Lemma} 5 | \Crefname{lemma}{Lemma}{Lemmas} 6 | 7 | \newtheorem{conjecture}[theorem]{Conjecture} 8 | 9 | \newcommand{\code}[1]{\mintinline{agda}{#1}} 10 | % Don't want omitthis because it is conditionally displayed 11 | \newcommand{\discard}[1]{} 12 | 13 | \newenvironment{bnfgrammar} 14 | {\begin{displaymath}\begin{array}{lrcl}} 15 | {\end{array}\end{displaymath}} 16 | 17 | \newcommand{\bnfnewline}{ 18 | \\[6pt] 19 | } 20 | 21 | \newenvironment{reductionrules} 22 | {\begin{displaymath}\begin{array}{rlll}} 23 | {\end{array}\end{displaymath}} 24 | 25 | \newcommand{\stepnewline}{ 26 | \\[4pt] 27 | } 28 | 29 | \newcommand{\False}{\ensuremath{\bot}\xspace} 30 | \newcommand{\sFalse}{\ensuremath{\sfontsym{\False}}\xspace} 31 | \newcommand{\tFalse}{\ensuremath{\tfontsym{\False}}\xspace} 32 | 33 | %% Stuff that belongs in mttex 34 | \newenvironment{proofcases} 35 | {\begin{enumerate}[label={\bfseries Case:},itemindent=2em]\ignorespacesafterend} 36 | {\end{enumerate}} 37 | 38 | \newenvironment{lemmacases} 39 | {~\begin{enumerate}\ignorespacesafterend} 40 | {\end{enumerate}\ignorespacesafterend} 41 | 42 | \newcommand{\with}[1]{\mathrm{with}\:#1} 43 | 44 | %% Coercion 45 | \newexpr{coe}{2}{\kwopen{\langfont{subst}}_{#1}\mathpunct{}#2} 46 | 47 | \makeatletter 48 | %% NB: Removes parens around argument from \@fune 49 | \renewcommand{\@fune}[3]{% 50 | \kwopen{\langsymfont{\uplambda}}% 51 | {#1} \mathbin{:} {#2}% 52 | \mathclose{.}% 53 | \mathpunct{}#3% 54 | } 55 | 56 | %% NB: use ; as separator instead of | 57 | \renewcommand{\@casee}[5]{% 58 | \kwopen{\langfont{case}}% 59 | #1% 60 | \kwbin{\langfont{of}}% 61 | #2 \mathpunct{.} #3% 62 | \mathpunct{\langsymfont{;}}% 63 | #4 \mathpunct{.} #5% 64 | } 65 | 66 | \newexpr{nlet}{2}{% 67 | \kwopen{\langfont{let}}% 68 | #1% 69 | \kwbin{\langfont{in}}% 70 | #2} 71 | \makeatother 72 | 73 | \newcommand{\len}[1]{|#1|} 74 | \newtheorem*{digression}{Digression} 75 | \newtheorem*{typographical}{Typographical Note} 76 | 77 | %% The Generic Language 78 | %% For when I don't want to distinguish between languages, but still want to write terms in prefix. 79 | 80 | \newcommand{\gfont}{\mathrm} 81 | \newcommand{\gfonttext}{\nop} 82 | \newcommand{\gfontsym}{\nop} 83 | \newcommand{\gcolor}{\nop} 84 | \newlanguage{\gcolor}{\gfont}{\gfontsym}{g} 85 | {} 86 | {lenv/\Gamma} 87 | {pi,pair,fun,bool,sigma,sum,unit} 88 | {fun,app,pair,if,fst,snd,true,false,case,sum,unit,nfun} 89 | 90 | \newmetavars{\nop}{\nop}{g}{x,e,U,A,B,K,C,k} 91 | 92 | \newcommand{\gFalse}{\False} 93 | \newcommand{\ginje}{\gsume} 94 | 95 | \newcommand{\gjudg}{\judg} 96 | 97 | \newcommand{\model}[1]{\sembrace{#1}^\circ} 98 | 99 | %% The Source Language Macros 100 | %% These macros are used for both CoC and ECC 101 | 102 | % The slang macro should be redefine in each part. 103 | \renewcommand{\slang}{ECC\xspace} 104 | \renewcommand{\scolor}[1]{\mathcolor{RoyalBlue}{#1}} 105 | 106 | \newlanguage{\scolor}{\sfont}{\sfontsym}{s} 107 | {x,y,e,t,U,i,f,A,B,C,v,a,n,k,z,N,M,K,V,p,g} 108 | {lenv/\Gamma,alpha/\alpha,beta/\beta,subst/\gamma} 109 | {pi,fun,sigma,type,dpair,sum,prop,set,bool,npi,star,box,unit,exist,code} 110 | {fun,app,alet,let,fst,snd,dpair,pair,sum,case,true,false,if,nfun,unit,pack,unpack,coe,npair,clo} 111 | % TODO Do I need nfune and npity? 112 | 113 | \newcommand{\sinje}{\ssume} 114 | 115 | \renewcommand{\step}{\vartriangleright} 116 | \renewcommand{\stepstar}{\step^*} 117 | 118 | \newcommand{\subtypesym}{\preceq} 119 | \newcommand{\sstepjudg}[4][\step]{\wf{#2}{#3\mathbin{#1}#4}} 120 | \newcommand{\sequivjudg}[3]{\wf{#1}{#2\mathbin{\equiv}#3}} 121 | \newcommand{\ssubtyjudg}[3]{\wf{#1}{#2\mathbin{\subtypesym}#3}} 122 | \newcommand{\styjudg}[3]{\judg{#1}{#2}{#3}} 123 | \newcommand{\swf}[1]{\wf{}{#1}} 124 | 125 | \newcommand{\ecceqv}[4]{\judg{#1}{#2 \approx #3}{#4}} 126 | 127 | \newcommand{\seval}[1]{\sfont{eval}(#1)} 128 | 129 | \newcommand{\ssubstok}{\wf} 130 | 131 | %% Parametric CPS Macros: 132 | \newcommand{\cpsslang}{CoC$^{D}$\xspace} 133 | \newcommand{\eccidk}{\sfont{id}} 134 | \newcommand{\cps}{CoC$^{k}$} 135 | \newcommand{\cpstlang}{\cps\xspace} 136 | \newcommand{\cpsprime}{\tprime} 137 | 138 | \newlanguage{\tcolor}{\tfont}{\tfontsym}{cps} 139 | {k,x,y,e,t,U,A,B,z,f,v,l} 140 | {alpha/\alpha,lenv/\Gamma,subst/\gamma,K/\kappa} 141 | {pi,pair,sigma,fun,sum,prop,set,type,box,star,bool} 142 | {fun,app,alet,let,fst,snd,pair,dpair,nfun,sum,case,true,false,if} 143 | 144 | %\newcommand{\cpsstarty}{\cpspropty} 145 | %\newcommand{\cpsboxty}{\cpstypety{}} 146 | 147 | \newcommand{\cpscappe}[3]{\cpsappe{#1}{\tfontsym{@}~#2~#3}} 148 | \newcommand{\cpsncappe}[3]{\cpsappe{#1}{#2~#3}} 149 | \newcommand{\cpsidk}{\tfont{id}} 150 | 151 | \newcommand{\cpswf}{\swf} 152 | \newcommand{\cpstyjudg}{\styjudg} 153 | \newcommand{\cpsstepjudg}{\sstepjudg} 154 | \newcommand{\cpsequivjudg}{\sequivjudg} 155 | \newcommand{\cpssubtyjudg}{\ssubtyjudg} 156 | 157 | \newcommand{\cpsterm}[1]{\textsc{cps}\sembrace{#1}} 158 | \newcommand{\cpstype}[1]{\textsc{cpsT}\sembrace{#1}} 159 | 160 | \newcommand{\cbnsym}[1]{\leadsto^{n}_{#1}} 161 | \newcommand{\cbnsymK}{\cbnsym{\kappa}} 162 | \newcommand{\cbnsymU}{\cbnsym{U}} 163 | \newcommand{\cbnsymA}{\cbnsym{A}} 164 | \newcommand{\cbnsymAdiv}{\cbnsym{A^{\div}}} 165 | \newcommand{\cbnsyme}{\cbnsym{e}} 166 | \newcommand{\cbnsymlenv}{\cbnsym{}} 167 | \newcommand{\cbnU}[3]{\wf{#1}{#2\mathbin{\cbnsymU}#3}} 168 | \newcommand{\cbnK}[4]{\judg{#1}{#2}{#3\mathbin{\cbnsymK}#4}} 169 | \newcommand{\cbnA}[4]{\judg{#1}{#2}{#3\mathbin{\cbnsymA}#4}} 170 | \newcommand{\cbnAdiv}[4]{\judg{#1}{#2}{#3\mathbin{\cbnsymAdiv}#4}} 171 | \newcommand{\cbne}[4]{\judg{#1}{#2}{#3\mathbin{\cbnsyme}#4}} 172 | \newcommand{\cbnlenv}[2]{\wf{}{#1\cbnsymlenv}#2} 173 | 174 | \newcommand{\cbnnametext}{CPS$^{n}$\xspace} 175 | \newcommand{\cbnname}[1][{}]{CPS$^{n}#1$} 176 | \newcommand{\cbnAname}{\cbnname[_A]} 177 | \newcommand{\cbnArule}[1]{\refrule*{cbnA:#1}{\cbnAname-#1}} 178 | \newcommand{\defcbnArule}[1]{\defrule*{cbnA:#1}{\cbnAname-#1}} 179 | \newcommand{\cbnAdivname}{\cbnname[_{A^\div}]} 180 | \newcommand{\cbnAdivrule}[1]{\refrule*{cbnAdiv:#1}{\cbnAdivname-#1}} 181 | \newcommand{\defcbnAdivrule}[1]{\defrule*{cbnAdiv:#1}{\cbnAdivname-#1}} 182 | \newcommand{\cbnename}{\cbnname[_e]} 183 | \newcommand{\cbnerule}[2][\techprefix]{\refrule*[#1]{cbne:#2}{\cbnename-#2}} 184 | \newcommand{\defcbnerule}[1]{\defrule*{cbne:#1}{\cbnename-#1}} 185 | \newcommand{\cbnlenvname}{\cbnname[_\Gamma]} 186 | \newcommand{\cbnlenvrule}[2][\techprefix]{\refrule*[#1]{cbnlenv:#2}{\cbnlenvname-#2}} 187 | \newcommand{\defcbnlenvrule}[1]{\defrule*{cbnlenv:#1}{\cbnlenvname-#1}} 188 | \newcommand{\cbnKname}{\cbnname[_\kappa]} 189 | \newcommand{\cbnKrule}[1]{\refrule*{cbnK:#1}{\cbnKname-#1}} 190 | \newcommand{\defcbnKrule}[1]{\defrule*{cbnK:#1}{\cbnKname-#1}} 191 | \newcommand{\cbnUname}{\cbnname[_U]} 192 | \newcommand{\cbnUrule}[1]{\refrule{\cbnUname-#1}} 193 | \newcommand{\defcbnUrule}[1]{\defrule*{cbnU:#1}{\cbnUname-#1}} 194 | 195 | \newcommand{\cbvsym}[1]{\leadsto^{v}_{#1}} 196 | \newcommand{\cbvsymK}{\cbvsym{\kappa}} 197 | \newcommand{\cbvsymU}{\cbvsym{U}} 198 | \newcommand{\cbvsymA}{\cbvsym{A}} 199 | \newcommand{\cbvsymAdiv}{\cbvsym{A^{\div}}} 200 | \newcommand{\cbvsyme}{\cbvsym{e}} 201 | \newcommand{\cbvsymlenv}{\cbvsym{}} 202 | \newcommand{\cbvU}[3]{\wf{#1}{#2\mathbin{\cbvsymU}#3}} 203 | \newcommand{\cbvK}[4]{\judg{#1}{#2}{#3\mathbin{\cbvsymK}#4}} 204 | \newcommand{\cbvA}[4]{\judg{#1}{#2}{#3\mathbin{\cbvsymA}#4}} 205 | \newcommand{\cbvAdiv}[4]{\judg{#1}{#2}{#3\mathbin{\cbvsymAdiv}#4}} 206 | \newcommand{\cbve}[4]{\judg{#1}{#2}{#3\mathbin{\cbvsyme}#4}} 207 | \newcommand{\cbvlenv}[2]{\wf{}{#1\cbvsymlenv}#2} 208 | 209 | \newcommand{\cbvnametext}{CPS$^{v}$\xspace} 210 | \newcommand{\cbvname}[1][{}]{CPS$^{v}#1$} 211 | \newcommand{\cbvAname}{\cbvname[_A]} 212 | \newcommand{\cbvArule}[1]{\rulename{\cbvAname-#1}} 213 | \newcommand{\cbvAdivname}{\cbvname[_{A^div}]} 214 | \newcommand{\cbvAdivrule}[1]{\rulename{\cbvAdivname-#1}} 215 | \newcommand{\cbvename}{\cbvname[_e]} 216 | \newcommand{\cbverule}[1]{\rulename{\cbvename-#1}} 217 | \newcommand{\cbvlenvname}{\cbvname[_\Gamma]} 218 | \newcommand{\cbvlenvrule}[1]{\rulename{\cbvlenvname-#1}} 219 | \newcommand{\cbvKname}{\cbvname[_\kappa]} 220 | \newcommand{\cbvKrule}[1]{\refrule*{cbvK:#1}{\cbvKname-#1}} 221 | \newcommand{\defcbvKrule}[1]{\defrule*{cbvK:#1}{\cbvKname-#1}} 222 | \newcommand{\cbvUname}{\cbvname[_U]} 223 | \newcommand{\cbvUrule}[1]{\refrule*{cbvU:#1}{\cbvUname-#1}} 224 | \newcommand{\defcbvUrule}[1]{\defrule*{cbvU:#1}{\cbvUname-#1}} 225 | 226 | % CPS Model translation 227 | 228 | \newcommand{\anfsym}{\leadsto_{\circ}} 229 | \newcommand{\anfjudg}[4]{\judg{#1}{#2}{#3 \mathrel{\anfsym} #4}} 230 | \newcommand{\cpsmodel}[1]{{#1}^\circ} 231 | 232 | \newcommand{\cccolor}[1]{#1} 233 | \newcommand{\ccfont}[1]{#1} 234 | \newcommand{\ccfonttext}[1]{\textit{\cccolor{#1}}} 235 | \newcommand{\ccfontsym}[1]{#1} 236 | \newcommand{\ccFalse}{\ccfont{\False}} 237 | 238 | \newlanguage{\cccolor}{\ccfont}{\ccfontsym}{cc} 239 | {k,x,y,e,t,U,A,B,z,f,p} 240 | {alpha/\alpha,lenv/\Gamma,subst/\gamma,K/\kappa} 241 | {pi,star,box,pair,sigma,fun} 242 | {nfun,fun,app,alet,let,fst,snd,pair,dpair} 243 | 244 | \newcommand{\ccstepjudg}[4][\step]{\wf{#2}{#3\mathbin{#1}#4}} 245 | \newcommand{\ccequivjudg}[3]{\wf{#1}{#2\mathbin{\equiv}#3}} 246 | \newcommand{\cctyjudg}[3]{\judg{#1}{#2}{#3}} 247 | \newcommand{\ccidk}{\ccfont{id}} 248 | 249 | \newcommand{\paramtr}[1]{\sembrace{#1}^{\mathcal{P}}} 250 | 251 | %% Target language for ANF and CC 252 | 253 | 254 | \renewcommand{\tcolor}[1]{\mathcolor{OrangeRed}{#1}} 255 | 256 | %% Heterogeneous plugging 257 | %% (heterogeneous hole-with) 258 | \newcommand{\hhw}[1]{\boldsymbol{\langle}\!\boldsymbol{\langle}#1\boldsymbol{\rangle}\!\boldsymbol{\rangle}} 259 | 260 | %% Heterogeneous subst 261 | \newcommand{\hsubst}[3]{#1[#2/\!/#3]} 262 | 263 | \newlanguage{\tcolor}{\tfont}{\tfontsym}{t} 264 | {x,y,N,V,K,M,A,B,U,i,f,j,e,C,k,z,v,p,E} 265 | {lenv/\Gamma,alpha/\alpha,beta/\beta} 266 | {pi,fun,sigma,type,dpair,sum,prop,bool} 267 | {fun,app,let,fst,snd,dpair,pair,sum,case,coe,true,false,if,nlet} 268 | 269 | \newcommand{\tinje}{\tsume} 270 | 271 | \newcommand{\tstepjudg}[4][\mapsto]{#3 #1 #4} 272 | \newcommand{\tequivjudg}{\sequivjudg} 273 | \newcommand{\tsubtyjudg}{\ssubtyjudg} 274 | \newcommand{\ttyjudg}{\styjudg} 275 | \newcommand{\twf}{\swf} 276 | 277 | % ANF Translation 278 | \newcommand{\anfslang}{ECC$^{D}$\xspace} 279 | \newcommand{\anftlang}{ECC$^{A}$\xspace} 280 | \newcommand{\anf}[2]{\sembrace{#1}{#2}} 281 | \newcommand{\anfh}[1]{\sembrace{#1}} 282 | 283 | \newcommand{\teval}[1]{\tfont{eval}(#1)} 284 | 285 | % blue on the outside but red on the inside 286 | %\newcommand{\sbound}[1]{\sfont{\boldsymbol{\big((}}#1\sfont{\boldsymbol{)\big)}}} 287 | \newcommand{\sbound}[1]{\mathcal{ST}(#1)} 288 | 289 | % Exported definitions of the translation of #1 290 | \newcommand{\tedefs}[1]{\mathtt{defs}{({#1})}} 291 | \newcommand{\edefs}[1]{\tedefs{\anfh{#1}}} 292 | 293 | % Exported hole of the translation of #1 294 | \newcommand{\tehole}[1]{\mathtt{hole}{({#1})}} 295 | \newcommand{\ehole}[1]{\tehole{\anfh{#1}}} 296 | 297 | %% Closure Conversion: 298 | \newcommand{\absccslang}{ECC$^{D}$\xspace} 299 | \newcommand{\abscctlang}{ECC$^{CC}$\xspace} 300 | \newcommand{\absccmodel}[1]{\sembrace{#1}^{\circ}} 301 | \newcommand{\modelsym}{\leadsto_{\circ}} 302 | \newcommand{\mjudg}[4]{\judg{#1}{#2}{#3 \mathbin{\modelsym} #4}} 303 | 304 | \newcommand{\sNat}{\sfont{Nat}} 305 | \newcommand{\sS}{\sfont{S}} 306 | 307 | \makeatletter 308 | \renewcommand{\@existty}[3]{ 309 | \kwopen{\langsymfont{\exists}}% 310 | {#1} \mathbin{:} {#2}% 311 | \mathclose{.}% 312 | \mathpunct{}#3% 313 | } 314 | \makeatother 315 | 316 | \newcommand{\codety}[4]{ 317 | \kwopen{#2{Code}}(#3)\mathpunct{.}#4% 318 | } 319 | 320 | \newcommand{\cloe}[4]{ 321 | #2{\langle\!\langle}#3\mathpunct{#2{,}}#4#2{\rangle\!\rangle}% 322 | } 323 | 324 | \newcommand{\mcloe}[2]{\langle\!\langle#1,#2\rangle\!\rangle} 325 | 326 | \newlanguage{\tcolor}{\tfont}{\tfontsym}{t} 327 | {n,t} 328 | {alpha/\alpha,subst/\gamma} 329 | {unit,pair,nsigma,exist,npi,code} 330 | {unit,npair,dnpair,nfun,clo,pack,npack,npacko,unpack,alet} 331 | 332 | \newcommand{\tstarty}{\tpropty} 333 | \newcommand{\tboxty}{\ttypety{}} 334 | 335 | \newcommand{\DFV}[2]{\metafont{FV}{(#2, #1)}} 336 | 337 | \newcommand{\cctrans}[1]{\sembrace{#1}} 338 | \newcommand{\ccsym}{\leadsto} 339 | \newcommand{\ccenvjudg}[2]{\wf{}{#1\mathbin{\ccsym}#2}} 340 | \newcommand{\ccjudg}[4]{\judg{#1}{#2}{#3 \mathbin{\ccsym} #4}} 341 | 342 | %% Parametric Closure Conversion: 343 | 344 | % Translucent functions 345 | 346 | \makeatletter 347 | 348 | \newcommand{\trlufunarrow}{\Rightarrow} 349 | 350 | % trlufunty formats a translucent function type. 351 | % 352 | % #1 : The pre-formatted argument to the function 353 | % #2 : The pre-formatted result of the function 354 | \newtype{trlufun}{2}{#1 \mathbin{\langsymfont{\trlufunarrow}} #2} 355 | 356 | \renewcommand{\@nexistty}[2]{\kwopen{\langsymfont{\exists}}#1\mathclose{.}\mathpunct{}#2} 357 | 358 | \makeatother 359 | 360 | \newcommand{\tr}[1]{{#1}^+} 361 | \newcommand{\pccslang}{\cpsslang} 362 | \newcommand{\pcctlang}{CoC$^{CC}$\xspace} 363 | \newcommand{\pccwf}{\swf} 364 | \newcommand{\pcctyjudg}{\styjudg} 365 | \newcommand{\pccstepjudg}{\sstepjudg} 366 | \newcommand{\pccequivjudg}{\sequivjudg} 367 | \newcommand{\pccsubtyjudg}{\ssubtyjudg} 368 | 369 | \newcommand{\pcccolor}{\tcolor} 370 | \newcommand{\pccfont}{\tfont} 371 | \newcommand{\pccfontsym}{\tfontsym} 372 | \newcommand{\pccfonttext}{\tfonttext} 373 | \newlanguage{\pcccolor}{\pccfont}{\pccfontsym}{pcc} 374 | {k,x,y,e,t,U,f,A,B,b,v,a,n,c} 375 | {alpha/\alpha,lenv/\Gamma,subst/\gamma} 376 | {pi,npi,nsigma,exist,unit,trlufun,pair,nexist,sigma,bool,box,star,fun} 377 | {nfun,app,alet,if,dnpair,fst,snd,prj,pack,unpack,unit,packo,dpair,npair,let,nunpack,npack,npacko,pair,true,false} 378 | 379 | % Allow currying pcccode 380 | \newcommand{\pcccodety}[2]{ 381 | \kwopen{\pccfont{Code}}#1\mathpunct{.}#2% 382 | } 383 | 384 | \newcommand{\pccsym}{\leadsto} 385 | \newcommand{\pccenvjudg}[2]{\wf{}{#1\mathbin{\pccsym}#2}} 386 | \newcommand{\pccjudg}[4]{\judg{#1}{#2}{#3 \mathbin{\pccsym} #4}} 387 | 388 | \newcommand{\pccsubstok}{\ssubstok} 389 | -------------------------------------------------------------------------------- /chapters/cps/ideas.tex: -------------------------------------------------------------------------------- 1 | \section{Main Ideas} 2 | \label{sec:cps:cps:ideas} 3 | \label{sec:cps:ideas} 4 | 5 | Intuitively, in a \tech{dependently typed} language, the power of the type 6 | system comes from the ability to express decidable equality between 7 | \tech{terms} and \tech{types}. 8 | These equalities are decided by reducing \tech{expressions} to canonical forms 9 | and checking that the resulting \emph{values} are syntactically identical. 10 | In the source language---\ie, before \tech{CPS} translation---since the language 11 | is effect-free, every term can be thought of as a \tech{value} since every 12 | \tech{expression} reduces to a \tech{value}. 13 | But \tech{CPS} translation converts source \tech{expressions} into 14 | \tech{computations} of type \im{(A \rightarrow \bot) \rightarrow \bot}. 15 | This changes the \emph{interface} to the \tech{values}---now we can only access 16 | the \tech{value} indirectly, by providing a \tech{computation} that will do 17 | something with the \tech{value}. 18 | In essence, ensuring \tech{CPS} translations are \tech{type-preserving} is hard 19 | because every source \tech{value} has turned into a \tech{computation} whose 20 | \tech{underlying value} isn't directly accessible for purposes of deciding 21 | equivalence. 22 | In particular, with the \tech{double-negation translation}, one cannot recover 23 | the \tech{underlying value}, because every \tech{continuation} must return 24 | \im{\False}. 25 | 26 | This description in terms of interfaces is just a shallow description 27 | of the problem. 28 | At a deeper level, the problem is that \tech{dependently typed} languages rely 29 | on the ability of the type system to copy \tech{expressions} from a \emph{term-level} 30 | context into a \emph{type-level} context, but \tech{CPS} transforms 31 | \tech{expressions} into \emph{computations} whose meaning, or \tech{underlying 32 | value}, depends on its term-level context. 33 | This copying happens in particular in the elimination rules for features related 34 | to \tech{dependency}---\ie, \tech{dependent functions}, \tech{dependent pairs}, 35 | and \tech{dependent conditionals}---hence these features are at the heart of 36 | past negative results. 37 | After \tech{CPS}, we no longer copy an \tech{expression}, whose meaning is self-contained; instead we copy a \tech{computation}, whose meaning depends on its 38 | term-level context. 39 | Not only do we ``forget'' part of the meaning of \tech{computations}, but as we 40 | discussed before, a \tech{computation} cannot run in a type-level context---it requires 41 | a term-level context. 42 | As I describe next, the solution to these problems will be to record part of 43 | the term-level contexts during type checking and to provide an interface that 44 | allows \tech{types} to run {computations}. 45 | 46 | To make this intuition concrete, I present two examples. 47 | I focus on two cases of the \tech{double-negation translation} that fail to type 48 | check: the \tech{CBN} translation of \im{\ssnde{\se}} (reported 49 | by~\citet{barthe2002}) and the \tech{CBV} translation of 50 | \im{\sappe{\se}{\sepr}}. 51 | 52 | Consider the \tech{CBN} \tech{CPS} translation. 53 | We translate a term \im{\se} of type \im{\sA} into a \tech{CPS'd} 54 | \tech{computation}, written \im{{\se}^\div}, of type \im{{\sA}^\div}. 55 | Given a type \im{\sA}, we define its \deftech{computation translation} 56 | \im{{\sA}^\div} and its \deftech{value translation} \im{{\sA}^+}. 57 | Below, I define the translations for \tech{dependent pairs} and \tech{dependent functions}. 58 | As in \fullref[]{chp:abs-cc}, the translations are defined by induction on 59 | typing derivations, but I present them less formally in this section. 60 | \begin{mathpar} 61 | \sA^\div \defeq \cpsfunty{(\cpsfunty{\sA^+}{\tfont{\bot}})}{\tfont{\bot}} 62 | 63 | (\ssigmaty{\sx}{\sA}{\sB})^+ \defeq \cpssigmaty{\cpsx}{\sA^\div}{\sB^\div} 64 | 65 | (\spity{\sx}{\sA}{\sB})^+ \defeq \cpspity{\cpsx}{\sA^\div}{\sB^\div} 66 | \end{mathpar} 67 | Note that since this is the \tech{CBN} translation, the translated argument type 68 | for \tech{dependent functions} is a \tech{computation} type \im{\sA^\div} 69 | instead of a \tech{value} type \im{\sA^+}, and the translated component types 70 | for \tech{dependent pairs} are \tech{computation} types \im{\sA^\div} and \im{\sB^\div}. 71 | 72 | As a warm-up, consider the \tech{CBN} translation of \im{\sfste{\se}} 73 | (where \im{\se : \ssigmaty{\sx}{\sA}{\sB}}): 74 | \begin{displaymath} 75 | (\sfste{\se} : \sA)^\div \defeq 76 | \begin{stackTL} 77 | \cpsfune{\cpsk}{\cpsfunty{\sA^+}{\tfont{\bot}}}{ 78 | \\\quad\!\cpsappe{\se^\div}{\begin{stackTL}(\cpsfune{\cpsy}{(\cpssigmaty{\cpsx}{\sA^{\div}}{\sB^{\div}})}{ 79 | \\\quad\,\cpslete{\cpsz}{(\cpsfste{\cpsy}):\sA^\div}{\cpsappe{\cpsz}{\cpsk}}})} 80 | \end{stackTL}} 81 | \end{stackTL} 82 | \end{displaymath} 83 | It is easy to see that the above type checks (checking the types of 84 | \im{\cpsy}, \im{\cpsz}, and \im{\cpsk}). 85 | 86 | Next, consider the \tech{CBN} translation of \im{\ssnde{\se}}: 87 | \begin{displaymath} 88 | (\ssnde{\se} : \subst{\sB}{\sfste{\se}}{\sx})^\div \defeq 89 | \begin{stackTL} 90 | \cpsfune{\cpsk}{\cpsfunty{\subst{\sB^+}{(\sfste{\se})^{\div}}{\cpsx}}{\tfont{\bot}}}{ 91 | \\\quad\!\cpsappe{\se^\div}{\begin{stackTL}(\cpsfune{\cpsy}{(\cpssigmaty{\cpsx}{\sA^\div}{\sB^\div})}{ 92 | \\\quad\cpslete{\cpsz}{(\cpssnde{\cpsy}):\subst{\sB^\div}{\cpsfste{\cpsy}}{\cpsx}}{\cpsappe{\cpsz}{\cpsk}}})} 93 | \end{stackTL}} 94 | \end{stackTL} 95 | \end{displaymath} 96 | 97 | The above does not type check because the \tech{computation} \im{\cpsz} expects 98 | a \tech{continuation} of type 99 | \im{\cpsfunty{\subst{\sB^+}{\cpsfste{\cpsy}}{\cpsx}}{\tfont{\bot}}} but 100 | \im{\cpsk} has type 101 | \im{\cpsfunty{\subst{\sB^+}{(\sfste{\se})^{\div}}{\cpsx}}{\tfont{\bot}}}. 102 | Somehow we need to show that \im{\cpsfste{\cpsy} \equiv (\sfste{\se})^{\div}}. 103 | But what is the relationship between \im{\cpsy} and \im{\se}? 104 | Intuitively, \im{\se^\div : \sA^\div} is a \tech{computation} that will pass its 105 | result---\ie, the \tech{underlying value} of type \im{\sA^+} inside 106 | \im{\se^\div}, which corresponds to the value produced by evaluating the source 107 | term \im{\se}---to its \tech{continuation}. 108 | So when \im{\se^\div}'s \tech{continuation} is called, its argument \im{\cpsy} 109 | will always be equal to the unique \tech{underlying value} inside \im{\se^\div}. 110 | However, since we have used a \emph{function} to describe a \tech{continuation}, 111 | we must type check the body of the \tech{continuation} assuming that \im{\cpsy} 112 | is \emph{any} value of the appropriate type instead of the \emph{exactly one} 113 | \tech{underlying value} from \im{\se^\div}. 114 | 115 | Even if we could communicate that \im{\cpsy} is equal to exactly one value, we 116 | have no way to extract the underlying \im{\sA^+} value from \im{\se^\div} since 117 | the latter takes a \tech{continuation} that \tech{never returns} (since it must 118 | return a term of type \im{\tFalse}). 119 | To extract the \tech{underlying value} from a \tech{computation}, we need a 120 | means of converting from \im{\sA^\div} to \im{\sA^+}. 121 | In essence, after \tech{CPS}, we have an \emph{interoperability} problem between 122 | the \tech{term} language (where \tech{computations} have type \im{\sA^\div}) 123 | and the \tech{type} language (which needs \tech{values} of type \im{\sA^+}). 124 | In the source language, before \tech{CPS}, we are able to pretend that the 125 | \tech{term} and \tech{type} languages are the same because all 126 | \tech{computations} of type \im{\sA} reduce to \tech{values} of type \im{\sA}. 127 | However, the \tech{CPS} translation creates a gap between the \tech{term} and 128 | \tech{type} languages; it changes the \emph{interface} to \tech{terms} so that 129 | the only way to get a value out of a \tech{computation} is to have a 130 | \tech{continuation}, which can \tech{never return}, ready to receive that 131 | \tech{value}. 132 | 133 | The \tech{locally polymorphic answer type translation} solves both of the above problems. 134 | We change the \tech{computation} translation to \im{ \sA^\div = 135 | \cpspity{\cpsalpha}{\cpsstarty}{\cpsfunty{(\cpsfunty{\sA^+}{\cpsalpha})}{\cpsalpha}}}. 136 | Now, to extract the \tech{underlying value} of type \im{\sA^+} from \im{\se^\div 137 | : \sA^\div}, we can run \im{\se^\div} with the identity \tech{continuation} as 138 | follows: \im{\cpsappe{\se^\div}{\sA^+~\cpsidk}}. 139 | Moreover, we can now justify type checking the body of \im{\se^\div}'s 140 | \tech{continuation} under the assumption that \im{\cpsy \equiv 141 | \cpsappe{\se^\div}{\sA^+~\cpsidk}} thanks to a \emph{free theorem} we get from 142 | the type \im{\sA^\div}. 143 | The free theorem says that running some \im{\cpse : \sA^\div} with 144 | \tech{continuation} \im{\cpsk : \cpsfunty{\cpsA}{\cpsB}} is equivalent to 145 | running \im{\cpse} with the identity \tech{continuation} and then passing the 146 | result to \im{\cpsk}, \ie, \im{\cpsappe{\cpse}{\cpsB~\cpsk} \equiv 147 | \cpsappe{\cpsk}{(\cpsappe{\cpse}{\cpsA~\cpsidk})}}. 148 | 149 | To formalize this intuition in the target language, I first add new syntax for 150 | the application of a \tech{computation} to its answer type and \tech{continuation}: 151 | \im{\cpscappe{\cpse}{\cpsA}{\cpsepr}}. 152 | Next, I internalize the aforementioned free theorem by adding two rules to the 153 | target language. 154 | The first is the following typing rule which records (a representation of) the 155 | \tech{value} of a \tech{computation} while type checking a \tech{continuation}. 156 | That is, it allows us to assume \im{\cpsy \equiv \cpsappe{\se^\div}{\sA^+~\cpsidk}} when 157 | type checking the body of \im{\se^\div}'s \tech{continuation}. 158 | \begin{mathpar} 159 | \inferrule*[right={\refrule{T-Cont}}] 160 | {\cpstyjudg{\cpslenv}{\cpse}{\cpspity{\cpsalpha}{\cpsstarty}{\cpsfunty{(\cpsfunty{\cpsA}{\cpsalpha})}{\cpsalpha}}} \\ 161 | \cpstyjudg{\cpslenv}{\cpsB}{\cpsstarty} \\ 162 | \cpstyjudg{\cpslenv,\cpsx = \cpsappe{\cpse}{\cpsA~\cpsidk}}{\cpsepr}{\cpsB}} 163 | {\cpstyjudg{\cpslenv}{\cpscappe{\cpse}{\cpsB}{(\cpsfune{\cpsx}{\cpsA}{\cpsepr})}}{\cpsB}} 164 | \end{mathpar} 165 | The second is the following \tech{equivalence} rule, which is justified by the 166 | free theorem. 167 | Intuitively, this rule normalizes \tech{CPS'd} \tech{computations} to the 168 | ``\tech{value}'' \im{\cpsappe{\se^\div}{\sA^+~\cpsidk}}. 169 | { 170 | \vspace{-2ex} 171 | \begin{mathpar} 172 | \inferrule*[right={\refrule*{eqv-cont}{\inlinemath{\equiv}-Cont}}] 173 | {~} 174 | {\cpsequivjudg{\cpslenv}{{(\cpscappe{\cpseone}{\cpsB}{(\cpsfune{\cpsx}{\cpsA}{\cpsetwo})})}}{\cpsappe{(\cpsfune{\cpsx}{\cpsA}{\cpsetwo})}{(\cpsappe{\cpseone}{\cpsA~\cpsidk})}}} 175 | \end{mathpar} 176 | } 177 | I prove these rules admissable in \fullref[]{sec:cps:ecccps}. 178 | 179 | Here is the updated \tech{CPS} translation \im{(\ssnde{\se} : 180 | \subst{\sB}{\sfste{\se}}{\sx})^\div} that leverages \tech{answer-type 181 | polymorphism}: 182 | \begin{displaymath} 183 | {\cpsfune{\cpsalpha}{\cpsstarty}{ 184 | \begin{stackTL}\cpsfune{\cpsk}{\cpsfunty{\subst{\sB^+}{(\sfste{\se})^{\div}}{\cpsx}}{\cpsalpha}}{ 185 | \\\quad\cpscappe{\se^\div}{\cpsalpha}{\begin{stackTL}(\cpsfune{\cpsy}{(\cpssigmaty{\cpsx}{\sA^\div}{\sB^\div})}{ 186 | \cpslete{\cpsz}{(\cpssnde{\cpsy}):\subst{\sB^\div}{\cpsfste{\cpsy}}{\cpsx}}{\cpsappe{\cpsz}{\cpsalpha~\cpsk}}}) 187 | \end{stackTL}}}} 188 | \end{stackTL}} 189 | \end{displaymath} 190 | { 191 | \allowdisplaybreaks 192 | To type check \im{\cpscappe{\se^\div}{\cpsalpha}{\tfontsym{\dots}}} we use 193 | \refrule{T-Cont}. 194 | When type checking the body of \im{\se^\div}'s \tech{continuation}, we have that 195 | \im{\cpsy \equiv 196 | \cpsappe{\se^\div}{(\cpssigmaty{\cpsx}{\sA^\div}{\sB^\div})~\cpsidk}} and 197 | recall that we need to show that \im{\cpsfste{\cpsy} \equiv 198 | (\sfste{\se})^{\div}}. 199 | This requires expanding \im{(\sfste{\se})^{\div}} and making use of the 200 | \refrule*{eqv-cont}{\im{\equiv}-Cont} rule we now have available in 201 | the target language. 202 | Here is an informal sketch of the proof---I give the detailed proof in 203 | \fullref[]{sec:cps:cbn:proof}. 204 | \begin{align} 205 | (\sfste{\se})^{\div} & \equiv \cpscappe{\se^\div}{\cpsalphapr}{(\cpsnfune{\cpsy}{\cpsfste{\cpsy}})} 206 | & \text{by (roughly) the translation} \\ 207 | & \equiv \cpsappe{(\cpsnfune{\cpsy}{\cpsfste{\cpsy}})}{(\cpsappe{\se^\div}{(\cpssigmaty{\cpsx}{\sA^\div}{\sB^\div})~\cpsidk})} 208 | & \text{by \refrule*{eqv-cont}{\im{\equiv}-Cont}} \\ 209 | & \equiv \cpsfste{(\cpsappe{\se^\div}{(\cpssigmaty{\cpsx}{\sA^\div}{\sB^\div})~\cpsidk})} 210 | & \text{by reduction} \\ 211 | & \equiv \cpsfste{\cpsy} 212 | & \text{since \im{\cpsy \equiv \cpsappe{\se^\div}{(\cpssigmaty{\cpsx}{\sA^\div}{\sB^\div})~\cpsidk}}} 213 | \end{align} 214 | } 215 | 216 | Notice that the {CPS} translation---as well as the new rules \refrule{T-Cont} 217 | and \refrule*{eqv-cont}{\im{\equiv}-Cont}---only uses the new 218 | \im{\tfontsym{@}} syntax for certain applications. 219 | Intuitively, we only need to use \im{\tfontsym{@}} only when type checking 220 | requires the free theorem. 221 | This happens when \tech{CPS} translating a \tech{depended-upon} 222 | \tech{computation}, such as for \im{\se} in \im{\ssnde{\se}}. 223 | 224 | %%%%%%%%%%%%%% 225 | Next, let's look at the translation of \tech{dependent function} types. 226 | Again, we start with a warm-up; consider the following \tech{CBN} 227 | \tech{double-negation translation} of \im{\sappe{\se}{\sepr}} (where \im{\se: 228 | \spity{\sx}{\sA}{\sB}} and \im{\sepr : \sA}): 229 | \begin{displaymath} 230 | (\sappe{\se}{\sepr} : \subst{\sB}{\sepr}{\sx})^\div = 231 | {\cpsfune{\cpsk}{\cpsfunty{(\subst{\sB^+}{\se^{\sprime\div}}{\cpsx})}{\tfont{\bot}}}{ 232 | \cpsappe{\se^\div}{(\cpsfune{\cpsf}{\cpspity{\cpsx}{\sA^\div}{\sB^\div}}{ 233 | \cpsappe{(\cpsappe{\cpsf}{\se^{\sprime\div}})}{\cpsk})}}}} 234 | \end{displaymath} 235 | The above type checks (as seen by inspecting the types of \im{\cpsf} and 236 | \im{\cpsk}). Notice that \im{\se^{\sprime\div}} appears as an 237 | argument to \im{\cpsf} so the type of 238 | \im{\cpsappe{\cpsf}{{\sepr}^\div} : \subst{\sB^\div}{{\sepr}^\div}{\cpsx}}. 239 | 240 | Now consider the \tech{CBV} \tech{CPS} translation based on 241 | \deftech*{double-negation,double negation}{double negation}, which fails to type 242 | check. 243 | We define the \tech{CBV} \tech{computation} translation \im{{\sA}^\div} and 244 | \tech{value} translation \im{{\sA}^+} as follows. 245 | \begin{mathpar} 246 | \sA^\div = \cpsfunty{(\cpsfunty{\sA^+}{\tfont{\bot}})}{\tfont{\bot}} 247 | 248 | (\ssigmaty{\sx}{\sA}{\sB})^+ = \cpssigmaty{\cpsx}{\sA^+}{\sB^+} 249 | 250 | (\spity{\sx}{\sA}{\sB})^+ = \cpspity{\cpsx}{\sA^+}{\sB^\div} 251 | \end{mathpar} 252 | Since this is a \tech{CBV} translation, the translated argument for the 253 | \tech{dependent function} is a \tech{value} of type \im{\sA^+} and the 254 | translated component types for \tech{dependent pairs} are \tech{values} of types 255 | \im{\sA^+} and \im{\sB^+}. 256 | 257 | Here is the \tech{CBV} \tech{CPS} translation \im{\sappe{\se}{\sepr}} (where 258 | \im{\se: \spity{\sx}{\sA}{\sB}} and \im{\sepr : \sA}): 259 | \begin{displaymath} 260 | (\sappe{\se}{\sepr} : \subst{\sB}{\sepr}{\sx})^\div = 261 | { \begin{stackTL} 262 | \cpsfune{\cpsk}{\cpsfunty{(\subst{\sB^+}{\se^{\sprime+}}{\cpsx})}{\tfont{\bot}}}{ 263 | \cpsappe{\se^\div}{(\cpsfune{\cpsf}{\cpspity{\cpsx}{\sA^+}{\sB^{\div}}}{ 264 | {\cpsappe{\se^{\sprime\div}}{(\cpsfune{\cpsx}{\sA^+}{ 265 | \cpsappe{(\cpsappe{\cpsf}{\cpsx})}{\cpsk}})}}) 266 | }}} 267 | \end{stackTL} 268 | } 269 | \end{displaymath} 270 | For the moment, ignore that our type annotation on \im{\cpsk}, 271 | \im{(\subst{\sB^+}{\se^{\sprime+}}{\cpsx})}, seems to require a \tech{value} 272 | translation of \tech{terms} \im{\se^{\sprime+}}, which we can't normally define. 273 | Instead, notice that unlike in the \tech{CBN} translation, we now evaluate the 274 | argument \im{{\sepr}^\div} before calling \im{\cpsf}, so in \tech{CBV} we have 275 | the application \im{\cpsappe{\cpsf}{\cpsx} : \subst{\sB^\div}{\cpsx}{\cpsx}}. 276 | This translation fails to type check since the \tech{computation} 277 | \im{\cpsappe{\cpsf}{\cpsx}} expects a \tech{continuation} of type 278 | \im{\cpsfunty{(\subst{\sB^+}{\cpsx}{\cpsx})}{\tfont{\bot}}} but \im{\cpsk} 279 | has type \im{\cpsfunty{(\subst{\sB^+}{\se^{\sprime+}}{\cpsx})}{\tfont{\bot}}}. 280 | Somehow we need to show that \im{\cpsx \equiv {\sepr}^+}. 281 | This situation is almost identical to what we saw with the failing \tech{CBN} 282 | translation of \im{\ssnde{\se}}. 283 | Analogously, this time we ask what is the relationship between \im{\cpsx} and 284 | \im{{\sepr}^\div}, or \im{{\sepr}^+}? 285 | As before, note that the only \tech{value} that can flow into \im{\cpsx} is the 286 | unique \tech{underlying value} in \im{{\sepr}^\div}. 287 | 288 | Hence, fortunately, the solution is again to do what we did for the \tech{CBN} 289 | translation: adopt a \tech{CPS} translation based on \tech{answer-type 290 | polymorphism}. 291 | As before, we change the \tech{computation} translation to \im{ \sA^\div = 292 | \cpspity{\cpsalpha}{\cpsstarty}{\cpsfunty{(\cpsfunty{\sA^+}{\cpsalpha})}{\cpsalpha}}}. 293 | Here is the updated \tech{CBV} \tech{CPS} translation of \im{(\sappe{\se}{\sepr} 294 | : \subst{\sB}{\sepr}{\sx})^\div}: 295 | \begin{displaymath} 296 | \cpsfune{\cpsalpha}{\cpsstarty}{ 297 | \begin{stackTL} 298 | \cpsfune{\cpsk}{\cpsfunty{(\subst{\sB^+}{(\cpsappe{{\sepr}^\div}{\sA^+~\cpsidk})}{\cpsx})}{\cpsalpha}}{ 299 | \\\quad\!\cpsappe{\se^\div}{\cpsalpha~\begin{stackTL}(\cpsfune{\cpsf}{\cpspity{\cpsx}{\sA^+}{\sB^{\div}}}{ 300 | \\\quad{\cpscappe{\se^{\sprime\div}}{\cpsalpha}{(\cpsfune{\cpsx}{\sA^+}{ 301 | \cpsappe{(\cpsappe{\cpsf}{\cpsx})}{\cpsalpha~\cpsk}})}}) 302 | \end{stackTL}}}} 303 | \end{stackTL}} 304 | \end{displaymath} 305 | First, notice that this uses the new \im{\tfontsym{@}} form when evaluating the 306 | argument \im{{\sepr}^\div}, which tells us we're using our new typing rule to 307 | record the \tech{value} of \im{\se^{\sprime\div}} while we type check its 308 | \tech{continuation}. 309 | Second, notice the type annotation on \im{\cpsk}. 310 | Earlier I observed that the type annotation for \im{\cpsk}, 311 | \im{(\subst{\sB^+}{\se^{\sprime+}}{\cpsx})}, seemed to require a \tech{value} 312 | translation on \tech{terms} \im{{\sepr}^+} that cannot normally be defined. 313 | The translation gives us a sensible way of modeling the \tech{value} translation 314 | of a term by invoking a \tech{computation} with the identity 315 | \tech{continuation}---so \im{{\sepr}^+} is just the underlying value in 316 | \im{{\sepr}^\div}, \ie, \im{(\cpsncappe{{\sepr}^\div}{\sA^+}{\cpsidk})}. 317 | This is an important point to note: unlike \tech{CBN} \tech{CPS}, where we can 318 | substitute \tech{computations} for variables, in \tech{CBV} \tech{CPS} we must 319 | find a way to extract the \tech{underlying value} from \tech{computations} of 320 | type \im{\sA^\div} since variables expect \tech{values} of type \im{\sA^+}. 321 | Without \tech{answer-type polymorphism}, \tech{CBV} \tech{CPS} is, in some 322 | sense, much more broken than \tech{CBN} \tech{CPS}! 323 | Indeed,~\citet{barthe1999} already gave a \tech{CBN} \tech{double-negation 324 | translation} for \tech{dependent functions}, but typed \tech{CBV} 325 | \tech{double-negation translation} for \tech{dependent function} fails. 326 | 327 | Using the new typing rule and \tech{equivalence} rules from earlier, we are able 328 | to type check the above translation of \im{\sappe{\se}{\sepr}} in essentially 329 | the same way as for the \tech{CBN} translation of \im{\ssnde{\se}}. 330 | I show the detailed proof in \fullref[]{sec:cps:cbv:proof}. 331 | 332 | The reader may worry that our \tech{CBV} \tech{CPS} translation produces many 333 | terms of the form \im{\cpsappe{\cpsk}{(\sappe{\se^\div}{\sA^+~\cpsidk})}}, which 334 | aren't really in \tech{CPS} since \im{\sappe{\se^\div}{\sA^+~\cpsidk}} must return. 335 | However, notice that these only appear in \tech{types}, not \tech{terms}. 336 | That is, we only run a \tech{computation} with the identity \tech{continuation} 337 | to convert a \tech{CPS} \tech{computation} into a \tech{value} \emph{in the 338 | types} for deciding \tech{equivalence}. 339 | The run-time \tech{terms} are all in \tech{CPS} and can be run in a machine-like 340 | semantics in which \tech{computations} \tech{never return}. 341 | -------------------------------------------------------------------------------- /chapters/anf/discussion.tex: -------------------------------------------------------------------------------- 1 | \section{Related and Future Work} 2 | \label{sec:anf:discuss} 3 | \label{sec:anf:related} 4 | 5 | \subsection{Comparison to CPS} 6 | \label{sec:discuss:cps} 7 | ANF is usually seen in opposition to CPS, so I briefly discuss similarities and 8 | differences between our type-preserving ANF and prior work on-type preserving 9 | CPS. 10 | ANF is favored as a compiler intermediate representation, although not 11 | universally. 12 | \citet{maurer2017} argue for ANF, over alternatives such as CPS, because ANF 13 | makes control flow explicit but keeps evaluation order implicit, automatically 14 | avoids administrative redexes, simplifies many optimizations, and keeps code in 15 | direct style. 16 | \citet{kennedy2007} argues the opposite---that CPS is preferred to ANF---and 17 | summarizes the arguments for and against. 18 | 19 | Most recent work on CPS translation of dependently typed language has focused on 20 | expressing control effects. 21 | \citet{pedrot2017:lola} uses a non-standard CPS translation to internalize 22 | classical reasoning in the Calculus of Inductive Constructions (CIC). 23 | \citet{cong2018:lam-pi-s-r,cong2018:tfp} develop CPS translations to 24 | express delimited control effects, via \texttt{shift} and \texttt{reset}, in a 25 | dependently typed language. 26 | \citet{miquey2017} uses a CPS translation to model a dependent sequent calculus. 27 | When expressing control effects in dependently typed languages, it is 28 | \emph{necessary} to prevent certain dependencies from being expressed to 29 | maintain consistency~\cite{barthe2002,herbelin2005}, therefore these 30 | translations do not try to recover dependencies in the way we discuss in 31 | \fullref[]{sec:anf:ideas}. 32 | 33 | My own CPS translation, \fullref[]{chp:cps} (published before writing this 34 | chapter as \citet{bowman2018:cps-sigma}) does avoid control effects and seeks to 35 | develop a type preserving translation. 36 | The new typing rule I add is similar to my \refrule{K-Bind} in \tlang, and is 37 | used for the same purpose: to recover disrupted dependencies. 38 | Unfortunately, that encoding does not scale to higher universes, and relies on 39 | interpreting all functions as parametric (discussed further in 40 | \fullref[]{chp:conclusions}). 41 | By contrast, this ANF translation works with higher universes and, since \tlang 42 | is a subset of \slang, the ANF translation is orthogonal to parametricity. 43 | 44 | \subsection{Branching and Join Points} 45 | The \tech{ANF} translation presented so far does not support all of \slang; in 46 | particular, it omits the \tech{dependent conditional}. 47 | This is primarily for simplicity, as many of the problems with \tech{ANF} are 48 | orthogonal to \tech{dependent conditional}. 49 | However, \tech{dependent conditionals} do introduce non-trivial challenges for 50 | \tech{ANF} translation. 51 | In this section, I give the translation and argue that it is \tech{type preserving}. 52 | 53 | It is well-known that \tech{ANF} in the presence of branching constructs, such 54 | as \texttt{if}, can cause considerable code duplication for branches. 55 | For instance, supposing we have an (for the moment, non-dependent) \texttt{if}, 56 | the na\"ive \tech{ANF} translation is the following. 57 | % 58 | \begin{displaymath} 59 | \anf{\sife{\se}{\seone}{\setwo}}{\tK} = \anf{\se}{\tlete{\tx}{\hole}{\tife{\tx}{(\anf{\seone}{\tK})}{(\anf{\setwo}{\tK})}}} 60 | \end{displaymath} 61 | % 62 | Notice that the current \tech{continuation} \im{\tK} is duplicated in the branches. 63 | 64 | The well-known solution is to add a \emph{join point}---essentially, a 65 | continuation that is used only for avoiding code duplication in branch 66 | constructs. 67 | Using join points, we would translate \sfonttext{if} as follows. 68 | % 69 | \begin{displaymath} 70 | \anf{\sife{\se}{\seone}{\setwo}}{\tK} = 71 | \anf{\se}{\tlete{\tx}{\hole}{\begin{stackTL}\tlete{\tj}{\tfune{\txpr}{\tA}{\tK\hw{\txpr}}}{ 72 | \\\,\,\quad\begin{stackTL} 73 | \tife{\tx}{\anf{\seone}{\tlete{\txone}{\hole}{~\tappe{\tj}{\txone}}}\\\quad\,\,\,\,}{\,\,\anf{\setwo}{\tlete{\txtwo}{\hole}{~\tappe{\tj}{\txtwo}}}} 74 | \end{stackTL}}} 75 | \end{stackTL}} 76 | \end{displaymath} 77 | % 78 | Instead of duplicating \im{\tK}, we create a join point \im{\tj} which is called in the branches. 79 | 80 | Extending the translation to support join points requires (for decidable 81 | type checking) that the translation generate a type annotation \im{\tA} for the 82 | join point, where \im{\tA} is the translation of the type of the \tfonttext{if} 83 | statement. 84 | It is easy to extend the translation to be defined on typing derivations; in 85 | fact, the rest of the translation defined in this dissertation are defined on 86 | typing derivations, and the proof architecture described in 87 | \fullref[]{chp:type-pres} is designed to support this. 88 | The only disadvantage is that structuring the translation this way disallows 89 | typed equivalence, unless the problem discussed in \fullref[]{chp:type-pres} can 90 | be solved. 91 | 92 | The real problem arises when we have \emph{\techs{dependent conditional}}, in 93 | which the result type of the branches can depend on the scrutinee of the 94 | conditional. 95 | Recall that typing rule for dependent \texttt{if}, reproduced below. 96 | % 97 | \begin{displaymath} 98 | \inferrule 99 | {\styjudg{\slenv,\sy:\sboolty}{\sB}{\sU} \\ 100 | \styjudg{\slenv}{\se}{\sboolty}\\ 101 | \styjudg{\slenv}{\seone}{\subst{\sB}{\struee}{\sy}} \\ 102 | \styjudg{\slenv}{\setwo}{\subst{\sB}{\sfalsee}{\sy}}} 103 | {\styjudg{\slenv}{\sife{\se}{\seone}{\setwo}}{\subst{\sB}{\se}{\sy}}} 104 | \end{displaymath} 105 | 106 | We can describe the problem clearly using \tech{continuation} typing. 107 | The \tech{ANF} translation of this term is with respect to \tech{continuation} \im{\tK 108 | : (\ehole{\sife{\se}{\seone}{\setwo}} : \anfh{\subst{\sB}{\se}{\sy}}) 109 | \Rightarrow \tBpr}. 110 | However, the \tech{ANF} translation will use \im{\tK} in an ill-typed way, 111 | producing in one branch \im{\anfh{\seone}{\tK}}, and in the other branch 112 | \im{\anfh{\setwo}{\tK}}. 113 | For the first branch, for instance, we must show that now \im{\tK : (\ehole{\seone} : 114 | \anfh{\subst{\sB}{\struee}{\sy}}) \Rightarrow \tBpr}. 115 | The definitions, \im{\edefs{\se}}, introduced by the translation of \texttt{if} 116 | are not sufficient to show that this type is equivalent to the type of \im{\tK} 117 | expected for the translation of \texttt{if}. 118 | I discuss an essentially similar problem for \tech{CPS} with 119 | \techs{dependent conditional} in \fullref[]{chp:cps}. 120 | 121 | We could resolve this, it seems, if we could assume that while type checking the 122 | first branch that \im{\se = \struee}, and similarly for the second branch that 123 | \im{\se = \sfalsee}. 124 | But one of these is not true; we cannot have that both \im{\se = \struee} and 125 | \im{\se = \sfalsee}. 126 | Since in \tlang we require reduction under both branches during type checking, 127 | reduction under this inconsistent assumption could cause divergence. 128 | 129 | To enable us to make inconsistent assumptions like the above but ensure 130 | strong normalization, my idea is to use explicit equality proofs and explicit 131 | coercions. 132 | The coercions would block reduction until they have a \tech{proof} of 133 | equality, and since we will never end up with a \tech{proof} that 134 | \im{\struee = \sfalsee}, the inconsistent assumption can never be used in 135 | reduction. 136 | But the coercion would allow the ANF translation to type check. 137 | 138 | With this idea, the type rule for \sfonttext{if} would be the following. 139 | % 140 | \begin{displaymath} 141 | \inferrule 142 | {\styjudg{\slenv,\sy:\sboolty}{\sB}{\sU} \\ 143 | \styjudg{\slenv}{\se}{\sboolty} \\ 144 | \styjudg{\slenv,\sp:\se = \struee}{\seone}{\subst{\sB}{\struee}{\sy}} \\ 145 | \styjudg{\slenv,\sp:\se = \sfalsee}{\setwo}{\subst{\sB}{\sfalsee}{\sy}}} 146 | {\styjudg{\slenv}{\sife{\se}{\seone}{\setwo}}{\subst{\sB}{\se}{\sy}}} 147 | \end{displaymath} 148 | % 149 | Then, the \tech{ANF} na\"ive translation for \tech{dependent conditionals} would 150 | be the following; I give the join-point translation shortly. 151 | % 152 | \begin{displaymath} 153 | \anf{\sife{\se}{\seone}{\setwo}}{\tK} = \anf{\se}{ 154 | \tlete{\begin{stackTL}\tx}{\hole}{\\ 155 | \begin{stackTL}\tife{\tx}{\anf{\seone}{\tlete{\txone}{\hole}{\tK\hw{\tcoee{\tp}{\txone}}}}\\\quad\,\,\,\,}{\,\,\,{\anf{\setwo}{\tlete{\txtwo}{\hole}{\tK\hw{\tcoee{\tp}{\txtwo}}}}}}} 156 | \end{stackTL} 157 | \end{stackTL}} 158 | \end{displaymath} 159 | % 160 | The term \im{\tcoee{\tp}{\te}} is an elimination for the identity type 161 | (derivable from only axiom \im{J}, a standard axiom admitted in many dependent 162 | type theories), with the following standard semantics. 163 | Recall that the result type of \im{\tK} cannot depend on the term in its hole, 164 | so we do not need a similar conversion for the result of \im{\tK}. 165 | % 166 | \begin{mathpar} 167 | \inferrule 168 | {\styjudg{\tlenv,\tx:\tA}{\tB}{\tU} \\ 169 | \styjudg{\tlenv}{\tp}{\teone = \tetwo} \\ 170 | \styjudg{\tlenv}{\te}{\subst{\tB}{\teone}{\tx}}} 171 | {\styjudg{\tlenv}{\tcoee{\tp}{\te}}{\subst{\tB}{\tetwo}{\tx}}} 172 | 173 | \inferrule 174 | {\styjudg{\tlenv}{\te}{\tA}} 175 | {\styjudg{\tlenv}{\tappe{\tfonttext{refl}}{\te}}{\te = \te}} 176 | 177 | \tcoee{{\tappe{\tfonttext{refl}}{\te}}}{\te} \step \te 178 | \end{mathpar} 179 | 180 | The translation with join points is below, and requires no further additions. 181 | \begin{displaymath} 182 | \anf{\sife{\se}{\seone}{\setwo}}{\tK} = 183 | \anf{\se}{\tlete{\tx}{\hole}{\begin{stackTL}\tlete{\tj}{\tfune{\txpr}{\tA}{\tK\hw{\txpr}}}{ 184 | \\\quad 185 | \begin{stackTL}\tife{\tx}{\anf{\seone}{\tlete{\txone}{\hole}{\tappe{\tj}{\tcoee{\tp}{\txone}}}}\\\quad\,\,\,\,}{\,\,\anf{\setwo}{\tlete{\txtwo}{\hole}{\tappe{\tj}{\tcoee{\tp}{\txtwo}}}}} 186 | \end{stackTL}}} 187 | \end{stackTL}} 188 | \end{displaymath} 189 | The type annotation \im{\tA} would be the translation of the type source 190 | \sfonttext{if} expression. 191 | This is why, formally, the translation must be defined over typing derivations 192 | instead of syntax. 193 | 194 | I conjecture that this translation would be type preserving, but a full 195 | investigation is left as future work. 196 | 197 | \citet{cong2018:lam-pi-s-r} use a similar approach to extend the \tech{CPS} 198 | translation presented in \fullref[]{chp:cps} to inductive types with dependent 199 | case analysis. 200 | 201 | \subsection{Dependent Pattern Matching and Commutative Cuts} 202 | The above problem with \techs{dependent conditional} is exactly the problem of 203 | commutative cuts for case analysis~\cite{boutillier2012,herbelin2009:pps-talk}. 204 | Formally, the problem of commutative cuts can be phrased: Is the following 205 | transformation \tech{type preserving}? 206 | % 207 | \begin{displaymath} 208 | \sK\hw{\sife{\se}{\seone}{\setwo}} \leadsto \sife{\se}{\sK\hw{\seone}}{\sK\hw{\setwo}} 209 | \end{displaymath} 210 | % 211 | \tech{ANF} necessarily performs this transformation, as shown in the previous section. 212 | 213 | Ignoring \tech{ANF} for a moment, in general this \emph{is not} \tech{type 214 | preserving}, and \tech{stack} typing shows why. 215 | Suppose that we have a non-\tech{ANF} \tech{stack} \im{\sK} with the following 216 | typing derivation. 217 | % 218 | \begin{mathpar} 219 | \inferrule 220 | {\styjudg{\slenv,\sypr:\sboolty}{\sBpr}{\sUpr} \\ 221 | \styjudg{\slenv,\sy:\subst{\sBpr}{\se}{\sypr}}{\sB}{\sU} \\ 222 | \slenv \vdash {\sife{\se}{\seone}{\setwo}} : \subst{\sBpr}{\se}{\sypr}} 223 | {\slenv \vdash \sK : ({\sife{\se}{\seone}{\setwo}} : \subst{\sBpr}{\se}{\sypr}) \Rightarrow \subst{\sB}{({\sife{\se}{\seone}{\setwo}})}{\sy}} 224 | \end{mathpar} 225 | % 226 | Note that the result type of this \tech{stack}, \im{\sBpr}, \emph{may} depend on 227 | the term in the hole, since this is not in \tech{ANF}. 228 | The problem is that after the commutative cut, we must show that 229 | \im{\sK\hw{\seone}} and \im{\sK\hw{\setwo}} are well-typed in the branches, 230 | which is not true in general since \im{\sK} expects 231 | \im{\sife{\se}{\seone}{\setwo}}. 232 | 233 | If we add the equalities \im{\se = \struee} and \im{\se = \sfalsee} while type 234 | checking the branches, as proposed in the previous section, then it appears that 235 | we can make the terms \im{\sK\hw{\seone}} and \im{\sK\hw{\setwo}} well-typed, although 236 | it requires a generalization of \fullref[]{lem:anf:ecca:cut}. 237 | However, we still cannot show the commutative cut is \tech{type preserving}, 238 | since the result type \im{\sB} also depends on the term in the hole. 239 | And now, the two branches of the \sfonttext{if} have different types: 240 | \im{\sK\hw{\seone} : \subst{\sB}{\seone}{\sy}}, while \im{\sK\hw{\setwo} : 241 | \subst{\sB}{\setwo}{\sy}}. 242 | Since the branches of an \sfonttext{if} must have the same type (up to 243 | equivalence), it appears that we must show \im{\seone \equiv \setwo}. 244 | 245 | In fact, what we need to show is essentially another, smaller, commutative cut. 246 | Viewing \im{\sB} as a type-level \tech{stack}, we must show 247 | \im{\sB\hw{\sife{\se}{\seone}{\setwo}} \equiv 248 | \sife{\se}{\sB\hw{\seone}}{\sB\hw{\setwo}}}. 249 | This is smaller in the sense that the type of this type cannot contain a 250 | commutative cut. 251 | For booleans, we could pursue this by adding yet another appeal to 252 | \im{J}, but this approach does not scale to indexed inductive types. 253 | 254 | But, there is a solution: we can make the commutative cut type preserving, even 255 | for general inductive types. 256 | \citet{boutillier2012} give an extension to CIC that allows for typing commutative 257 | cuts, in particular, by relaxing the termination checker. 258 | Explaining the solution is out of scope for this work. 259 | I only want to point out two things. 260 | First, the solution adds an environment of \emph{equalities} to the termination 261 | checker, just as I propose for \tech{stack} typing and for typing \sfonttext{if} 262 | above. 263 | Second, the solution requires \emph{axiom K}, a very strong requirement which is 264 | inconsistent with certain extensions to type theory, such as univalence. 265 | This raises the question: does \tech{ANF} in general require axiom \emph{K}? 266 | 267 | I conjecture the answer is no, and the reason is the interesting property we 268 | observed in \fullref[]{sec:anf:target}: in \tech{ANF}, the result type cannot 269 | depend on the term in the hole. 270 | This additional structure seems to avoid some problems of commutative 271 | cuts, and I hope that it will be enough to scale \tech{ANF} to indexed inductive 272 | types without additional requirements on the type theory. 273 | I have one additional reason to be hopeful: even if we need a stronger axiom than 274 | \im{J}, work on dependent pattern matching suggests that univalence may replace 275 | axiom \emph{K} for our purposes. 276 | 277 | Recent work on dependent pattern matching creates typing rules similar to what we 278 | suggest above to yield additional equalities during a pattern 279 | match~\cite{barras2008:new-cic-elim,cockx2016}. 280 | There is another unfortunate similarity: some work on dependent pattern matching 281 | requires axiom \emph{K}. 282 | In particular, \citet{barras2008:new-cic-elim} give a new eliminator for CIC 283 | which adds additional equalities while checking branches of an elimination, and show 284 | that this new typing rule is equivalent to axiom \emph{K}. 285 | \citet{cockx2016} discuss a proof-relevant view of unification, in the context 286 | of Agda's dependent pattern matching. 287 | They note that the heterogeneous equalities usually required by dependent 288 | pattern matching require axiom \emph{K} to be useful. 289 | They also take a different approach, and build on an idea from homotopy type 290 | theory that one equality can ``layer over'' another, to get a proof relevant 291 | unification algorithm that does not rely on \emph{K}, and yet yields the 292 | additional equalities for dependent pattern matching. 293 | 294 | \subsection{Dependent Call-by-Push-Value and Monadic Form} 295 | Call-by-push-value (\deftech{CBPV}) is similar to the \tech{ANF} target 296 | language, and to \tech{CPS} target languages. 297 | In essence, \tech{CBPV} is a \(\lambda\) calculus in monadic normal form 298 | suitable for reasoning about \tech{CBV} or \tech{CBN}, due to explicit 299 | sequencing of computations. 300 | It has \techs{value}, \techs{computation}, and \techs{stack}, similar to 301 | \tech{ANF}, and has compositional typing rules (which inspired much of my own 302 | presentation). 303 | The particular structure of \tech{CBPV} is beneficial when modeling effects; all 304 | \techs{computation} should be considered to carry an arbitrary effect, while 305 | \techs{value} do not. 306 | 307 | Work on designing a dependent call-by-push-value (\deftech{dCBPV}) runs into 308 | some of the same design issues that we see in \tech{ANF} 309 | \cite{ahman2017:dissertation,vakar2017:dissertation}, but critically, avoids the 310 | central difficulties introduced in \fullref[]{sec:anf:ideas}. 311 | The reason is essentially that monadic normal form is more compositional than 312 | \tech{CPS} or \tech{ANF}, so \tech{dependency} is not disrupted in the same way. 313 | 314 | Recall from \fullref[]{sec:anf:target} that our definition of composition was 315 | entirely motivated by the need to compose \tech*{anf:config}{configurations} and 316 | \techs{stack}. 317 | In \tech{CBPV}, and monadic form generally, there is no distinction between 318 | \tech{computation} and \tech*{anf:config}{configurations}, and \texttt{let} is 319 | free to compose \tech*{anf:config}{configurations}. 320 | This means that \tech*{anf:config}{configurations} can return intermediate 321 | \techs{computation}, instead of composing the entire rest of the stack inside 322 | the body of a \texttt{let}. 323 | The monadic translation of \im{\ssnde{\se}} from \fullref[]{sec:anf:ideas}, which is 324 | problematic in \tech{CPS} and \tech{ANF}, is given below and is easily 325 | \tech{type preserving}. 326 | % 327 | \begin{displaymath} 328 | \anfh{\ssnde{\se} : \subst{\sB}{\se}{\sy}} = \tlete{\tx}{\anfh{\se}}{\tsnde{\tx}} : \subst{\anfh{\sB}}{\anfh{\se}}{\ty} 329 | \end{displaymath} 330 | 331 | Note that since \texttt{let} can bind the ``configuration'' \im{\anfh{\se}}, the 332 | typing rule \rulename{Let} and the compositionality lemma suffice to show 333 | \tech{type preservation}, without any reasoning about definitions. 334 | In fact, we don't even need \emph{definitions} for monadic form; we only need a 335 | dependent result type for \tfonttext{let}. 336 | The dependent typing rule for \tfonttext{let} without definition is essentially 337 | the rule given by \citet{vakar2017:dissertation}, called the dependent Kleisli 338 | extension, to support the \tech{CBV} monadic translation of type theory into 339 | \tech{dCBPV}, and the \tech{CBN} translation with strong dependent pairs. 340 | \citet{vakar2017:dissertation} observes that without the dependent Kleisli 341 | extension, \tech{CBV} translation is ill-defined (not \tech{type preserving}), 342 | and \tech{CBN} only works for dependent elimination of positive types. 343 | A similar observation was made independently in my own work with Nick Rioux, 344 | Youyou Cong, and Amal Ahmed~(\citeyear{bowman2018:cps-sigma}, presented in 345 | \fullref[]{chp:cps}): type-preserving \tech{CBV} \tech{CPS} fails for \(\Pi\) 346 | types, in addition to the well-known 347 | result that the \tech{CBN} translation failed for \(\Sigma\) 348 | types~\cite{barthe2002}. 349 | 350 | If we restrict the language so that types can only depend on 351 | \emph{\techs{value}}, then the extension with dependent \texttt{let} is not 352 | necessary. 353 | This restriction seems sensible in the context of modeling effects. 354 | \citet{ahman2017:dissertation} in \deftech{eMLTT}, a co-discovered variant of 355 | dependent \tech{CBPV}, avoids dependent let altogether, but comes up with many 356 | useful models of dependent types with effects. 357 | \citet{ahman2017:dissertation}, however, does not give a translation from type 358 | theory into \tech{eMLTT}, and it seems likely that an extension with dependent 359 | let would be required to do so. 360 | (This would be necessary to build on \tech{eMLTT} as a compiler IL.) 361 | However, as \citet{vakar2017:dissertation} points out, it is not clear what it 362 | \emph{means} to have a type depend on an effectful computation, and trying to do 363 | so makes it impossible to model effects in \tech{dCBPV} the way one would hope. 364 | 365 | In \tech{eMLTT}, stacks cannot have a result type that depends on the value it 366 | is composed with, just as in our \rulename{K-Bind} rule. 367 | However, the \tech{dCBPV} of \citet{vakar2017:dissertation} \emph{does} allow 368 | the result type of stacks to depend on values, but only on values. 369 | It is unclear what trade-offs each approach presents. 370 | -------------------------------------------------------------------------------- /chapters/cps/ecc.tex: -------------------------------------------------------------------------------- 1 | \renewcommand{\techprefix}{cpssrc} 2 | 3 | \newcommand{\FigCOCSyntax}[1][t]{ 4 | \begin{figure}[#1] 5 | \begin{bnfgrammar} 6 | \bnflabel{Universes} & 7 | \sU & \bnfdef & \sstarty \bnfalt \sboxty 8 | \bnfnewline 9 | 10 | \bnflabel{Expressions} & 11 | \st,\se,\sA,\sB & \bnfdef & \sx 12 | \bnfalt \sstarty 13 | \bnfalt \spity{\sx}{\sA}{\se} 14 | \bnfalt \sfune{\sx}{\sA}{\se} 15 | \bnfalt \sappe{\se}{\se} 16 | \bnfalt \ssigmaty{\sx}{\sA}{\sB} 17 | \\ && \bnfalt & \sdpaire{\seone}{\setwo}{\ssigmaty{\sx}{\sA}{\sB}} 18 | \bnfalt \sfste{\se} \bnfalt \ssnde{\se} 19 | \bnfalt \sboolty 20 | \bnfalt \struee 21 | \bnfalt \sfalsee 22 | \\ && \bnfalt & \sife{\se}{\seone}{\setwo} 23 | \bnfalt \salete{\sx}{\se}{\sA}{\se} 24 | 25 | \bnfnewline 26 | 27 | \bnflabel{Environments} & 28 | \slenv & \bnfdef & \cdot \bnfalt \slenv,\sx:\sA \bnfalt \slenv,\sx = \se:\sA 29 | \end{bnfgrammar} 30 | \caption{\cpsslang Syntax} 31 | \label{fig:cps:ecc:syntax} 32 | \end{figure} 33 | } 34 | 35 | \newcommand{\COCRed}{ 36 | \judgshape{\sstepjudg[\step]{\slenv}{\se}{\sepr}} 37 | \begin{reductionrules} 38 | \slenv \vdash \sappe{(\sfune{\sx}{\sA}{\seone})}{\setwo} & \step_{\beta} & \subst{\seone}{\setwo}{\sx} 39 | \stepnewline 40 | 41 | \slenv \vdash \sfste{\spaire{\seone}{\setwo}} & \step_{\pi_1} & \seone 42 | \stepnewline 43 | 44 | \slenv \vdash \ssnde{\spaire{\seone}{\setwo}} & \step_{\pi_2} & \setwo 45 | \stepnewline 46 | 47 | \slenv \vdash \sife{\struee}{\seone}{\setwo} & \step_{\iota_{1}} & \seone 48 | \stepnewline 49 | 50 | \slenv \vdash \sife{\sfalsee}{\seone}{\setwo} & \step_{\iota_{1}} & \setwo 51 | \stepnewline 52 | 53 | \slenv \vdash \sx & \step_{\delta} & \se & \where{\sx = \se : \sA \in \slenv} 54 | \stepnewline 55 | 56 | \slenv \vdash \salete{\sx}{\setwo}{\sA}{\seone} & \step_{\zeta} & \subst{\seone}{\setwo}{\sx} 57 | \stepnewline 58 | \end{reductionrules} 59 | } 60 | 61 | \newcommand{\FigCOCRed}[1][t]{ 62 | \begin{figure}[#1] 63 | \COCRed 64 | \caption{\cpsslang Reduction} 65 | \label{fig:cps:ecc:red} 66 | \end{figure} 67 | } 68 | 69 | \newcommand{\FigCOCConv}[1][t]{ 70 | \begin{figure}[#1] 71 | \judgshape{\sstepjudg[\stepstar]{\slenv}{\se}{\sepr}} 72 | \begin{mathpar} 73 | \inferrule*[right=\defrule{Red-Refl}] 74 | {~} 75 | {\sstepjudg[\stepstar]{\slenv}{\se}{\se}} 76 | 77 | \inferrule*[right=\defrule{Red-Trans}] 78 | {\sstepjudg{\slenv}{\se}{\seone} \\ 79 | \sstepjudg[\stepstar]{\slenv}{\seone}{\sepr}} 80 | {\sstepjudg[\stepstar]{\slenv}{\se}{\sepr}} 81 | 82 | \inferrule*[right=\defrule{Red-Cong-Lam}] 83 | {\sstepjudg[\stepstar]{\slenv}{\sA}{\sApr} \\ 84 | \sstepjudg[\stepstar]{\slenv,\sx:\sApr}{\se}{\sepr}} 85 | {\sstepjudg[\stepstar]{\slenv}{\sfune{\sx}{\sA}{\se}}{\sfune{\sx}{\sApr}{\sepr}}} 86 | 87 | \inferrule*[right=\defrule{Red-Cong-Pi}] 88 | {\sstepjudg[\stepstar]{\slenv}{\sA}{\sApr} \\ 89 | \sstepjudg[\stepstar]{\slenv,\sx:\sApr}{\se}{\sepr}} 90 | {\sstepjudg[\stepstar]{\slenv}{\spity{\sx}{\sA}{\se}}{\spity{\sx}{\sApr}{\sepr}}} 91 | 92 | \inferrule*[right=\defrule{Red-Cong-App}] 93 | {\sstepjudg[\stepstar]{\slenv}{\seone}{\seonepr} \\ 94 | \sstepjudg[\stepstar]{\slenv}{\setwo}{\setwopr}} 95 | {\sstepjudg[\stepstar]{\slenv}{\sappe{\seone}{\setwo}}{\sappe{\seonepr}{\setwopr}}} 96 | 97 | \inferrule*[right=\defrule{Red-Cong-Sig}] 98 | {\sstepjudg[\stepstar]{\slenv}{\sA}{\sApr} \\ 99 | \sstepjudg[\stepstar]{\slenv,\sx:\sApr}{\se}{\sepr}} 100 | {\sstepjudg[\stepstar]{\slenv}{\ssigmaty{\sx}{\sA}{\se}}{\ssigmaty{\sx}{\sApr}{\sepr}}} 101 | 102 | \inferrule*[right=\defrule{Red-Cong-Pair}] 103 | {\sstepjudg[\stepstar]{\slenv}{\seone}{\seonepr} \\ 104 | \sstepjudg[\stepstar]{\slenv}{\setwo}{\setwopr} \\ 105 | \sstepjudg[\stepstar]{\slenv}{\sA}{\sApr}} 106 | {\sstepjudg[\stepstar]{\slenv}{\sdpaire{\seone}{\setwo}{\sA}}{\sdpaire{\seonepr}{\setwopr}{\sApr}}} 107 | 108 | \inferrule*[right=\defrule{Red-Cong-Fst}] 109 | {\sstepjudg[\stepstar]{\slenv}{\se}{\sepr}} 110 | {\sstepjudg[\stepstar]{\slenv}{\sfste{\se}}{\sfste{\sepr}}} 111 | 112 | \inferrule*[right=\defrule{Red-Cong-Snd}] 113 | {\sstepjudg[\stepstar]{\slenv}{\se}{\sepr}} 114 | {\sstepjudg[\stepstar]{\slenv}{\ssnde{\se}}{\ssnde{\sepr}}} 115 | 116 | \inferrule*[right=\defrule{Red-Cong-If}] 117 | {\sstepjudg[\stepstar]{\slenv}{\se}{\sepr} \\ 118 | \sstepjudg[\stepstar]{\slenv}{\seone}{\seonepr} \\ 119 | \sstepjudg[\stepstar]{\slenv}{\setwo}{\setwopr}} 120 | {\sstepjudg[\stepstar]{\slenv}{\sife{\se}{\seone}{\setwo}}{\sife{\sepr}{\seonepr}{\setwopr}}} 121 | 122 | \inferrule*[right=\defrule{Red-Cong-Let}] 123 | {\sstepjudg[\stepstar]{\slenv}{\seone}{\seonepr} \\ 124 | \sstepjudg[\stepstar]{\slenv}{\sA}{\sApr} \\ 125 | \sstepjudg[\stepstar]{\slenv,\sx = \sepr : \sApr}{\setwo}{\setwopr}} 126 | {\sstepjudg[\stepstar]{\slenv}{\salete{\sx}{\seone}{\sA}{\setwo}}{\salete{\sx}{\seonepr}{\sApr}{\setwopr}}} 127 | \end{mathpar} 128 | \caption{\cpsslang Conversion} 129 | \label{fig:cps:ecc:conv} 130 | \end{figure} 131 | } 132 | 133 | \newcommand{\COCEqv}{ 134 | \judgshape{\sequivjudg{\slenv}{\se}{\sepr}} 135 | \begin{mathpar} 136 | \inferrule*[right=\defrule*{eqv}{\inlinemath{\equiv}}] 137 | {\sstepjudg[\stepstar]{\slenv}{\se}{\seone} \\ 138 | \sstepjudg[\stepstar]{\slenv}{\sepr}{\seone}} 139 | {\sequivjudg{\slenv}{\se}{\sepr}} 140 | 141 | \inferrule*[right=\defrule*{eqv-eta1}{\inlinemath{\equiv}-\im{\eta_1}}] 142 | {\sstepjudg[\stepstar]{\slenv}{\se}{\sfune{\sx}{\sA}{\seone}} \\ 143 | \sstepjudg[\stepstar]{\slenv}{\sepr}{\setwo} \\ 144 | \sequivjudg{\slenv,\sx:\sA}{\seone}{\sappe{\setwo}{\sx}}} 145 | {\sequivjudg{\slenv}{\se}{\sepr}} 146 | 147 | \inferrule*[right=\defrule*{eqv-eta2}{\inlinemath{\equiv}-\im{\eta_2}}] 148 | {\sstepjudg[\stepstar]{\slenv}{\se}{\seone} \\ 149 | \sstepjudg[\stepstar]{\slenv}{\sepr}{\sfune{\sx}{\sA}{\setwo}} \\ 150 | \sequivjudg{\slenv,\sx:\sA}{\sappe{\seone}{\sx}}{\setwo}} 151 | {\sequivjudg{\slenv}{\se}{\sepr}} 152 | \end{mathpar} 153 | } 154 | 155 | \newcommand{\FigCOCEqv}[1][t]{ 156 | \begin{figure}[#1] 157 | \COCEqv 158 | \caption{\cpsslang Equivalence} 159 | \label{fig:cps:ecc:eqv} 160 | \end{figure} 161 | } 162 | 163 | \newcommand{\FigCOCWF}[1][t]{ 164 | \begin{figure}[#1] 165 | \judgshape{\swf{\slenv}} 166 | \begin{mathpar} 167 | \inferrule*[right=\defrule{W-Empty}] 168 | {~} 169 | {\swf{\cdot}} 170 | 171 | \inferrule*[right=\defrule{W-Assum}] 172 | {\swf{\slenv} \\ 173 | \styjudg{\slenv}{\sA}{\sU}} 174 | {\swf{\slenv,\sx:\sA}} 175 | 176 | \inferrule*[right=\defrule{W-Def}] 177 | {\swf{\slenv} \\ 178 | \styjudg{\slenv}{\se}{\sA} \\ 179 | \styjudg{\slenv}{\sA}{\sU}} 180 | {\swf{\slenv,\sx = \se :\sA}} 181 | \end{mathpar} 182 | \caption{\cpsslang Well-Formed Environments} 183 | \end{figure} 184 | } 185 | 186 | \newcommand{\FigCoCTyping}[1][t]{ 187 | \begin{figure}[#1] 188 | \judgshape{\styjudg{\slenv}{\se}{\sA}} 189 | \begin{mathpar} 190 | \inferrule*[right=\defrule{Var}] 191 | {(\sx : \sA \in \slenv \text{ or } 192 | \sx = \se : \sA \in \slenv) \\ 193 | \swf{\slenv}} 194 | {\styjudg{\slenv}{\sx}{\sA}} 195 | 196 | \inferrule*[right=\defrule{*}] 197 | {\swf{\slenv}} 198 | {\styjudg{\slenv}{\sstarty}{\sboxty}} 199 | 200 | \inferrule*[right=\defrule{Pi-*}] 201 | {\styjudg{\slenv,\sx:\sA}{\sB}{\sstarty}} 202 | {\styjudg{\slenv}{\spity{\sx}{\sA}{\sB}}{\sstarty}} 203 | 204 | \inferrule*[right=\defrule*{Pi-Square}{Pi-\im{\square}}] 205 | {\styjudg{\slenv,\sx:\sA}{\sB}{\sboxty}} 206 | {\styjudg{\slenv}{\spity{\sx}{\sA}{\sB}}{\sboxty}} 207 | 208 | \inferrule*[right=\defrule{Lam}] 209 | {\styjudg{\slenv,\sx:\sA}{\se}{\sB} \\ 210 | \styjudg{\slenv}{\spity{\sx}{\sA}{\sB}}{\sU}} 211 | {\styjudg{\slenv}{\sfune{\sx}{\sA}{\se}}{\spity{\sx}{\sA}{\sB}}} 212 | 213 | \inferrule*[right=\defrule{App}] 214 | {\styjudg{\slenv}{\se}{\spity{\sx}{\sApr}{\sB}} \\ 215 | \styjudg{\slenv}{\sepr}{\sApr}} 216 | {\styjudg{\slenv}{\sappe{\se}{\sepr}}{\subst{\sB}{\sepr}{\sx}}} 217 | 218 | \inferrule*[right=\defrule{Sig}] 219 | {\styjudg{\slenv}{\sA}{\sstarty} \\ 220 | \styjudg{\slenv,\sx:\sA}{\sB}{\sstarty}} 221 | {\styjudg{\slenv}{\ssigmaty{\sx}{\sA}{\sB}}{\sstarty}} 222 | 223 | \inferrule*[right=\defrule{Pair}] 224 | {\styjudg{\slenv}{\seone}{\sA} \\ 225 | \styjudg{\slenv}{\setwo}{\subst{\sB}{\seone}{\sx}}} 226 | {\styjudg{\slenv}{\sdpaire{\seone}{\setwo}{\ssigmaty{\sx}{\sA}{\sB}}}{\ssigmaty{\sx}{\sA}{\sB}}} 227 | 228 | \inferrule*[right=\defrule{Fst}] 229 | {\styjudg{\slenv}{\se}{\ssigmaty{\sx}{\sA}{\sB}}} 230 | {\styjudg{\slenv}{\sfste{\se}}{\sA}} 231 | 232 | \inferrule*[right=\defrule{Snd}] 233 | {\styjudg{\slenv}{\se}{\ssigmaty{\sx}{\sA}{\sB}}} 234 | {\styjudg{\slenv}{\ssnde{\se}}{\subst{\sB}{\sfste{\se}}{\sx}}} 235 | 236 | \inferrule*[right=\defrule{Bool}] 237 | {\swf{\slenv}} 238 | {\styjudg{\slenv}{\sboolty}{\sstarty}} 239 | 240 | \inferrule*[right=\defrule{True}] 241 | {\swf{\slenv}} 242 | {\styjudg{\slenv}{\struee}{\sboolty}} 243 | 244 | \inferrule*[right=\defrule{False}] 245 | {\swf{\slenv}} 246 | {\styjudg{\slenv}{\sfalsee}{\sboolty}} 247 | 248 | \inferrule*[right=\defrule{If}] 249 | {\styjudg{\slenv}{\se}{\sboolty} \\ 250 | \styjudg{\slenv}{\seone}{\sB} \\ 251 | \styjudg{\slenv}{\setwo}{\sB}} 252 | {\styjudg{\slenv}{\sife{\se}{\seone}{\setwo}}{\sB}} 253 | 254 | \inferrule*[right=\defrule{Let}] 255 | {\styjudg{\slenv}{\sepr}{\sA} \\ 256 | \styjudg{\slenv,\sx=\sepr:\sA}{\se}{\sB}} 257 | {\styjudg{\slenv}{\salete{\sx}{\sepr}{\sA}{\se}}{\subst{\sB}{\sepr}{\sx}}} 258 | 259 | \inferrule*[right=\defrule{Conv}] 260 | {\styjudg{\slenv}{\se}{\sA} \\ 261 | \styjudg{\slenv}{\sB}{\sU} \\ 262 | \sequivjudg{\slenv}{\sA}{\sB}} 263 | {\styjudg{\slenv}{\se}{\sB}} 264 | \end{mathpar} 265 | \caption{\cpsslang Typing} 266 | \label{fig:cps:ecc:type} 267 | \end{figure} 268 | } 269 | 270 | \newcommand{\FigECCExplicitSyntax}[1][t]{ 271 | \begin{figure}[#1] 272 | \begin{bnfgrammar} 273 | \bnflabel{Kinds} & 274 | \sK & \bnfdef & \sstarty \bnfalt \spity{\salpha}{\sK}{\sK} \bnfalt 275 | \spity{\sx}{\sA}{\sK} 276 | 277 | \bnfnewline 278 | 279 | \bnflabel{Types} & 280 | \sA,\sB & \bnfdef & \salpha \bnfalt \spity{\sx}{\sA}{\sB} 281 | \bnfalt \spity{\salpha}{\sK}{\sB} 282 | \bnfalt \sfune{\sx}{\sA}{\sB} 283 | \bnfalt \sfune{\salpha}{\sK}{\sB} 284 | \bnfalt \sappe{\sA}{\se} 285 | \\ && \bnfalt & \sappe{\sA}{\sB} 286 | \bnfalt \ssigmaty{\sx}{\sA}{\sB} 287 | \bnfalt \sboolty 288 | \bnfalt \salete{\salpha}{\sA}{\sK}{\sB} 289 | \\ && \bnfalt & \salete{\sx}{\se}{\sA}{\sB} 290 | \bnfnewline 291 | 292 | \bnflabel{Terms} & 293 | \se & \bnfdef & \sx 294 | \bnfalt \sfune{\sx}{\sA}{\se} 295 | \bnfalt \sfune{\salpha}{\sK}{\se} 296 | \bnfalt \sappe{\se}{\se} 297 | \bnfalt \sappe{\se}{\sA} 298 | \bnfalt \sdpaire{\seone}{\setwo}{\ssigmaty{\sx}{\sA}{\sB}} 299 | \\ && \bnfalt & \sfste{\se} 300 | \bnfalt \ssnde{\se} 301 | \bnfalt \struee 302 | \bnfalt \sfalsee 303 | \bnfalt \sife{\se}{\seone}{\setwo} 304 | \\ && \bnfalt & \salete{\sx}{\se}{\sA}{\se} 305 | \bnfalt \salete{\salpha}{\sA}{\sK}{\se} 306 | \bnfnewline 307 | 308 | \bnflabel{Environments} & 309 | \slenv & \bnfdef & \cdot \bnfalt \slenv,\sx:\sA \bnfalt \slenv,\sx = \se : 310 | \sA, \bnfalt \slenv,\salpha : \sK \bnfalt \slenv,\salpha = \sA : \sK 311 | \end{bnfgrammar} 312 | \caption{\cpsslang Explicit Syntax} 313 | \label{fig:cps:ecc:explicit-syntax} 314 | \end{figure} 315 | } 316 | 317 | \newcommand{\FigCOCLinking}[1][t]{ 318 | \begin{figure}[#1] 319 | \begin{bnfgrammar} 320 | \bnflabel{Closing Substitutions} & 321 | \ssubst & \defeq & \cdot \bnfalt \ssubst[\sx \mapsto \se] 322 | \end{bnfgrammar} 323 | \judgshape{\wf{\slenv}{\ssubst}} 324 | \begin{mathpar} 325 | \inferrule 326 | {~} 327 | {\wf{\cdot}{\cdot}} 328 | 329 | \inferrule 330 | {\wf{\slenv}{\ssubst} \\ 331 | \styjudg{\cdot}{\se}{\sA}} 332 | {\wf{\slenv,\sx:\sA}{\ssubst[\sx \mapsto \se]}} 333 | 334 | \inferrule 335 | {\wf{\slenv}{\ssubst} \\ 336 | \styjudg{\slenv}{\se}{\sA}} 337 | {\wf{\slenv,\sx = \se : \sA}{\ssubst[\sx \mapsto \ssubst(\se)]}} 338 | \end{mathpar} 339 | \judgshape{\ssubst(\se) = \se} 340 | \begin{mathpar} 341 | \cdot(\se) = \se 342 | 343 | \ssubst[\sx \mapsto \sepr](\se) = \ssubst(\subst{\se}{\sx}{\sepr}) 344 | \end{mathpar} 345 | \caption{\cpsslang Closing Substitutions and Linking} 346 | \end{figure} 347 | } 348 | 349 | \section{The Calculus of Constructions with Definitions} 350 | \label{sec:cps:ecc} 351 | \FigCOCSyntax 352 | 353 | The language \cpsslang is an extension of the Calculus of 354 | Constructions (\deftech{CoC}) with booleans, \tech{dependent pairs} and 355 | \tech{definitions}. 356 | This is a restriction of the earlier \slang, without \tech{dependent 357 | conditionals}, \tech{higher universes}, and \tech{cumulativity}. 358 | Note that the booleans in \cpsslang do not allow dependent elimination; they 359 | only serve as a ground type for observations across languages. 360 | I return to dependent conditions in \fullref[]{sec:cps:related}. 361 | I adapt this presentation from the model of the Calculus of Inductive 362 | Constructions (\deftech{CIC}) given in the Coq reference manual~\cite[Chapter~4]{coq2017}. 363 | 364 | I present the syntax of \cpsslang in \fullref[]{fig:cps:ecc:syntax} in the style 365 | of a Pure Type System (\deftech{PTS}) with no syntactic distinction between 366 | \tech{terms} and \tech{types} and \deftech*{kind,kinds}{kinds}, which describe \tech{types}. 367 | This has been true of earlier chapters, but we will soon make an explicit 368 | distinction in order to selectively \tech{CPS} translate only \tech{terms}, as, 369 | unlike with \tech{ANF} and \tech{closure conversion}, it is unclear how to 370 | uniformly \tech{CPS} translate \tech{types} and \tech{kinds} in addition to 371 | \tech{terms}. 372 | As before, I use the phrase \tech{expression} to refer to a \tech{term}, 373 | \tech{type}, or \tech{kind} in the \tech{PTS} syntax. 374 | I usually use the meta-variable \im{\se} to evoke a term expression and 375 | I use \im{\st} for an expression to be explicitly ambiguous about its nature 376 | as a \tech{term}, \tech{type}, or \tech{kind}. 377 | 378 | The language includes one \tech{impredicative} \tech{universe}, or 379 | \deftech{sort}, \im{\sstarty}, and its \tech{type}, \im{\sboxty}. 380 | Compared to \slang from \fullref[]{chp:source}, we can think of \im{\sstarty} 381 | as \im{\spropty} and \im{\sboxty} as \im{\stypety{1}}. 382 | The syntax of \tech{expressions} includes the \tech{universe} \im{\sstarty}, 383 | variables \im{\sx} or \im{\salpha}, \tech{dependent function} types 384 | \im{\spity{\sx}{\sA}{\sB}}, \tech{dependent functions} 385 | \im{\sfune{\sx}{\sA}{\se}}, application 386 | \im{\sappe{\seone}{\setwo}}, dependent let 387 | \im{\salete{\sx}{\se}{\sA}{\sepr}}, \tech{dependent pair} types 388 | \im{\ssigmaty{\sx}{\sA}{\sB}}, \tech{dependent pairs} 389 | \im{\sdpaire{\seone}{\setwo}{\ssigmaty{\sx}{\sA}{\sB}}}, and first and second 390 | projections \im{\sfste{\se}} and \im{\ssnde{\se}}. 391 | Note that, unlike in \slang, we cannot write \im{\sboxty} in source 392 | programs---it is only used by the type system. 393 | The typing environment \im{\slenv} includes assumptions \im{\sx:\sA} and 394 | definitions \im{\sx = \se : \sA}. 395 | Note that definitions in this language include annotations, unlike in \slang. 396 | Unlike in the ANF translation in \fullref[]{chp:anf}, including annotations on 397 | definitions does not complicate the CPS translation because the \tech{CPS} 398 | translation is already inherently complicated by producing type annotations for 399 | continuations (which I discuss more later). 400 | 401 | As with \tech{dependent pairs}, I omit the type annotations on \im{\sfont{let}} 402 | expressions, \im{\slete{\sx}{\se}{\sepr}}, when they are clear from 403 | context. 404 | I use the notation \im{\sfunty{\sA}{\sB}} for a function type whose result 405 | \im{\sB} does not depend on the input. 406 | 407 | \FigCOCRed 408 | \FigCOCEqv 409 | The reduction relation, conversion relation, and equivalence relations for 410 | \cpsslang are essentially the same as for \slang. 411 | I partially duplicate them here anyway for convenience, and give full 412 | definitions in \fullref[]{sec:cps:appendix}. 413 | Notice, however, that there is no subtyping relation since \cpsslang excludes 414 | higher universes. 415 | In \fullref[]{fig:cps:ecc:red}, I present the reduction relation for \cpsslang, 416 | and in \fullref[]{fig:cps:ecc:eqv}, I present the equivalence relation. 417 | Note that \cpsslang does not fix an evaluation order, but this is not important since 418 | \cpsslang is effect-free. 419 | 420 | \FigCoCTyping 421 | The typing rules for \cpsslang, \fullref[]{fig:cps:ecc:type}, are also 422 | essentially the same, but missing removed features. 423 | The judgment \im{\swf{\slenv}} checks that the environment 424 | \im{\slenv} is well-formed; it is defined by mutual recursion with the 425 | typing judgment. 426 | Note that there is no typing rule analogous to \refrule[src]{Type}, so 427 | \im{\sboxty} is not a well-typed expression. 428 | This means \im{\sboxty} does not need a type, and means \cpsslang excludes 429 | \tech{higher universes}. 430 | Since I exclude \tech{higher universes}, I exclude \tech{cumulativity}, so the 431 | \refrule{Conv} is different. 432 | Notice that the \refrule{If} does not allow the result type to be dependent; I 433 | return to this in \fullref[]{sec:cps:related}. 434 | Instead of subtyping, \refrule{Conv} allows typing an expression 435 | \im{\se : \sA} as \im{\se : \sB} when \im{\sA \equiv \sB}. 436 | The remaining rules are essentially the same. 437 | \refrule{Pi-*} essentially corresponds to \refrule[srcapp]{Pi-Prop}, and 438 | implicitly allows \tech{impredicativity} in \im{\sstarty}, since the domain 439 | \im{\sA} could be in the \tech{higher universe} \im{\sboxty}. 440 | \refrule*{Pi-Square}{Pi-\(\square\)} is predicative, similar to 441 | \refrule[srcapp]{Pi-Type}. 442 | 443 | Note that for simplicity, I include only term-level pairs of type 444 | \im{\ssigmaty{\sx}{\sA}{\sB} : \sstarty}. 445 | Type-level pairs \im{\ssigmaty{\sx}{\sA}{\sB} : \sboxty} introduce 446 | numerous minor difficulties with \tech{CPS} translation. 447 | For instance, we can write pairs of terms and types \im{\spaire{\se}{\sA}} or 448 | \im{\spaire{\sA}{\se}}. 449 | It is unclear how these \tech{expression} should be \tech{CPS} translated; 450 | should they be considered terms or types? 451 | This is an instance of the more general problem of computational relevance. 452 | In general, dependent-type-preserving compilation is difficult when we try to 453 | compile only the computationally relevant terms, because we cannot easily decide 454 | relevance. 455 | I discuss this further in \fullref[]{chp:conclusions}. 456 | 457 | \FigECCExplicitSyntax 458 | To make the upcoming \tech{CPS} translation easier to follow, I present a second 459 | version of the syntax for \cpsslang in which we make the distinction between 460 | \tech{terms}, \tech{types}, and \tech{kinds} explicit (see 461 | \fullref[]{fig:cps:ecc:explicit-syntax}). 462 | The two presentations are equivalent~\cite{barthe1999}, at least as long as we 463 | do not include \tech{computationally relevant} \tech{higher universes}. 464 | Distinguishing \tech{terms} from \tech{types} and \tech{kinds} is useful since 465 | we only want to CPS translate \emph{terms}, because our goal is to internalize 466 | only \emph{run-time} evaluation contexts. 467 | I discuss \deftech{pervasive translation}, which also internalizes the 468 | \tech{type}-level evaluation context, in \fullref[]{sec:cps:related}. 469 | -------------------------------------------------------------------------------- /chapters/abs-cc/ideas.tex: -------------------------------------------------------------------------------- 1 | \section{Main Ideas} 2 | \label{sec:abs-cc:ideas} 3 | \tech{Closure conversion} makes the implicit \tech{closures} from a functional 4 | language explicit to facilitate statically allocating \tech{code} in memory. 5 | The idea is to translate each first-class function into an explicit 6 | \tech{closure}, \ie, a pair of closed \tech{code} and an \deftech{environment} 7 | data structure containing the values of the free variables. 8 | \deftech*{codes,code,Code}{Code} refers to functions with no free variables, as in a 9 | closure-converted language. 10 | The \tech{environment} is created dynamically, but the closed \tech{code} can be 11 | lifted to the top-level and statically allocated. 12 | Consider the following example translation. 13 | % 14 | \begin{displaymath} 15 | \begin{array}{@{\hspace{0pt}}r@{\hspace{1ex}}c@{\hspace{1ex}}l} 16 | \cctrans{(\lambda x.y)} &=& \left<(\lambda 17 | n\,x.\,\kwopen{\text{let}}y\mathrel{=}(\kwopen{\pi_1}n)\mathrel{\text{in}} 18 | y), \left\right> \\ 19 | \cctrans{((\lambda x.y)~\text{true})} &=& \begin{stackTL} 20 | \kwopen{\text{let}}\left\mathrel{=}\left<(\lambda 21 | n\,x.\,\kwopen{\text{let}}y\mathrel{=}(\kwopen{\pi_1}n)\mathrel{\text{in}}y), 22 | \left\right>\mathrel{\text{in}} 23 | \\\quad f~n~\text{true} 24 | \end{stackTL} 25 | \end{array} 26 | \end{displaymath} 27 | % 28 | I write \im{\cctrans{e}} to indicate the translation of an expression \im{e}. 29 | We translate each function into a pair of \tech{code} and its 30 | \tech{environment}. 31 | The \tech{code} accepts its free variables in an \tech{environment} argument, 32 | \im{n} (since \im{n} sounds similar to \emph{env}). 33 | In the body of the \tech{code}, we bind the names of all free variables by 34 | projecting from this \tech{environment} \im{n}. 35 | To call a \tech{closure}, we apply the code to its \tech{environment} and its 36 | argument. 37 | 38 | This translation is not \tech{type preserving} since the structure of the 39 | \tech{environment} shows up in the \tech{type}. 40 | For example, the following two functions have the same \tech{type} in the 41 | source language. 42 | They are both functions on booleans, and \im{y} is a free variable of type \im{\gboolty}. 43 | \begin{displaymath} 44 | \begin{array}{rcl} 45 | \lambda x.y &:& (\gboolty \to \gboolty) 46 | \\ 47 | \lambda x.x &:& (\gboolty \to \gboolty) 48 | \end{array} 49 | \end{displaymath} 50 | But after closure conversion when we encode closures as pairs, the two 51 | functions have different \techs{type} in the target language. 52 | % 53 | \begin{displaymath} 54 | \begin{array}{rcl} 55 | \cctrans{(\lambda x.y)} &:& ((\gboolty \times \gunitty) \to \gboolty \to \gboolty) \times (\gboolty \times \gunitty) 56 | \\ 57 | \cctrans{(\lambda x.x)} &:& (\gunitty \to \gboolty \to \gboolty) \times \gunitty 58 | \end{array} 59 | \end{displaymath} 60 | 61 | This is a well-known problem with typed \tech{closure conversion}, so we could 62 | try the well-known solution, called \emph{parametric closure 63 | conversion}~[\citealp{minamide1996}, \citealp{morrisett1998:reccc}, 64 | \citealp{morrisett1998:ftotal}, \citealp{ahmed2008}, \citealp{perconti2014}, 65 | \citealp{new2016}], which represents \techs{closure} as an existential package 66 | of a pair of the \tech{code} and its \tech{environment}, whose \tech{type} is 67 | hidden. 68 | The existential type hides the structure of the \tech{environment} in the 69 | \tech{type}. 70 | (Spoiler alert: it doesn't work for \slang.) 71 | % 72 | \begin{displaymath} 73 | \begin{array}{rcl} 74 | \cctrans{(\lambda x.y)} &:& \exists \alpha. (\alpha \to \gboolty \to \gboolty) \times \alpha 75 | \\ 76 | \cctrans{(\lambda x.x)} &:& \exists \alpha. (\alpha \to \gboolty \to \gboolty) \times \alpha 77 | \end{array} 78 | \end{displaymath} 79 | 80 | This translation works well for simply typed and polymorphic languages, but when 81 | we move to a \tech{dependently typed} language, we have new challenges. 82 | First, the \tech{environment} must now be ordered since the \tech{type} of each 83 | new variable can depend on all prior variables. 84 | Second, \techs{type} can now refer to variables in the \tech{closure}'s 85 | \tech{environment}. 86 | Consider the polymorphic identity function below. 87 | % 88 | \begin{displaymath} 89 | \begin{array}{rcl} 90 | \sfune{\sA}{\spropty}{\sfune{\sx}{\sA}{\sx}} & : & \spity{\sA}{\spropty}{\spity{\sx}{\sA}{\sA}} 91 | \end{array} 92 | \end{displaymath} 93 | % 94 | This function takes a type variable, \im{\sA}, whose type is \im{\spropty}. 95 | It returns a function that accepts an argument \im{\sx} of type \im{\sA} and returns it. 96 | There are two \tech{closures} in this example: the outer \tech{closure} has no 97 | free variables, and thus will have an empty \tech{environment}, while the inner 98 | \tech{closure} \im{\sfune{\sx}{\sA}{\sx}} has \im{\sA} free, and thus \im{\sA} 99 | will appear in its \tech{environment}. 100 | 101 | Below, I present the translation of this example using the existing parametric 102 | \tech{closure conversion} translation. 103 | The translation produces two \tech{closures}, one nested in the other. 104 | Note that we translate source variables \im{\sx} to \im{\tx}. 105 | In the outer \tech{closure}, the \tech{environment} is empty \im{\tnpaire{}}, 106 | and the code simply returns the inner \tech{closure}. 107 | The inner \tech{closure} has the argument \im{\tA} from the outer \tech{code} in 108 | its \tech{environment}. 109 | Since the inner \tech{code} takes an argument of type \im{\tA}, we project 110 | \im{\tA} from the \tech{environment} \emph{in the type annotation} for \im{\tx}. 111 | That is, the inner \tech{code} takes an \tech{environment} \im{\tntwo} that 112 | contains \im{\tA}, and the type annotation for \im{\tx} is \im{\tx : 113 | \tfste{\tntwo}}. 114 | The type \im{\tfste{\tntwo}} is unusual, but is no problem since \tech{dependent 115 | types} allow \techs{term} in \techs{type}. 116 | % 117 | \begin{displaymath} 118 | \begin{stackTL} 119 | \tcloe{\tnfune{(\tnone:\tunitty,\tA:\tpropty)}{ 120 | \tcloe{\tnfune{(\tntwo: 121 | \tpairty{\tpropty}{\tunitty},\tx:\tfste{\tntwo})}{\tx} 122 | }{\tnpaire{\tA,\tnpaire{}}}}}{\tnpaire{}} ~~ : ~~\\ 123 | ~~\begin{array}{@{\hspace{0pt}}l@{\hspace{0pt}}l@{\hspace{0pt}}l} 124 | \texistty{\talphaone}{\tpropty}{&\,\tpairty{(\tnpity{&(\tnone:\talphaone,\tA:\tpropty)}{ 125 | \\ && 126 | \texistty{\talphatwo}{\ttypety{i}}{\tpairty{(\tnpity{(\tntwo:\talphatwo,\tx:\tfste{\tntwo})}{\tfste{\tntwo}})}{\talphatwo}}})\,}{\talphaone}} 127 | \end{array} 128 | \end{stackTL} 129 | \end{displaymath} 130 | % 131 | We see that the inner \tech{code} on its own is well typed with the closed type 132 | \im{\tnpity{(\tntwo:\tpairty{\tpropty}{\tunitty},\tx:\tfste{\tntwo})}{\tfste{\tntwo}}}. 133 | That is, the \tech{code} takes two arguments: the first argument \im{\tntwo} is 134 | the \tech{environment}, and the second argument \im{\tx} is a value of type 135 | \im{\tfste{\tntwo}}. 136 | The result type of the \tech{code} is also \im{\tfste{\tntwo}}. 137 | As discussed above, we must hide the type of the \tech{environment} to ensure 138 | \tech{type preservation}. 139 | That is, when we build the \tech{closure} 140 | \im{\tcloe{\tnfune{(\tntwo: 141 | \tpairty{\tpropty}{\tunitty},\tx:\tfste{\tntwo})}{\tx} 142 | }{\tnpaire{\tA,\tnpaire{}}}}, we must hide the type of the 143 | \tech{environment} \im{\tnpaire{\tA,\tnpaire{}}}. 144 | We use an existential type to quantify over the type \im{\talphatwo} of the 145 | environment, and we produce the type 146 | \im{\tnpity{(\tntwo:\talphatwo,\tx:\tfste{\tntwo})}{\tfste{\tntwo}}} for the 147 | \tech{code} in the inner \tech{closure}. 148 | But this type is trying to take the \emph{first projection} of something of type 149 | \im{\talphatwo}. 150 | We can only project from pairs, and something of type \im{\talphatwo} isn't a 151 | pair! 152 | In hiding the type of the \tech{environment} to recover \tech{type 153 | preservation}, we've broken \tech{type preservation} for \tech{dependent 154 | types}. 155 | 156 | A similar problem also arises when \tech{closure converting} System F, since 157 | System F also features type variables~\cite{minamide1996,morrisett1998:ftotal}. 158 | To understand my solution, it is important to understand why the solutions that 159 | have historically worked for System F do not scale to \tech{dependent types}. 160 | I briefly present these past results and why they do not scale before moving on 161 | to the key idea behind my translation. 162 | Essentially, past work using existential types relies on assumptions about 163 | \tech{computational relevance}, \tech{parametricity}, and \tech{impredicativity} 164 | that do not necessarily hold in \tech{full-spectrum} \tech{dependent type} systems. 165 | 166 | \subsection{Why the well-known solution doesn't work} 167 | \citet{minamide1996} give a translation that encodes \tech{closure} types using 168 | \tech{existential types}, a standard type-theoretic feature that they use to 169 | make environment hiding explicit in the types. 170 | In essence, they encode \techs{closure} as objects; the \tech{environment} can 171 | be thought of as the private field of an object. 172 | Since then, existential types have been the standard way to encode 173 | \tech{closure} types has been all work on typed \tech{closure conversion}. 174 | 175 | However, the use of existential types to encode \techs{closure} in a dependently 176 | typed setting is problematic. 177 | First, let us just consider \tech{closure conversion} for System F. 178 | As \citet{minamide1996} observed, there is a problem when code must be closed 179 | with respect to both \tech{term} and \emph{\tech{type}} variables. 180 | This problem is similar to the one discussed above: when \tech{closure} 181 | \techs{environment} contain \tech{type} variables, since those \tech{type} 182 | variables can also appear in the \tech{closure}'s type, the \tech{closure}'s 183 | type needs to project from the \tech{closure}'s (hidden) \tech{environment} 184 | which has type \im{\alpha}. 185 | To fix the problem, \citet{minamide1996} extend their target language with 186 | \deftech{translucency} (essentially, a kind of \tech{type}-level equivalence 187 | that we now call singleton types), \tech{type}-level pairs, and kinds. 188 | All of these features can be encoded in \slang and most \tech{dependent type} 189 | systems, so we could extend their translation essentially as follows. 190 | (In fact, I present the extended translation in \fullref[]{chp:param-cc}.) 191 | % 192 | \begin{displaymath} 193 | \begin{array}{rcl} 194 | \!\!\!\cctrans{(\spity{\sx}{\sA}{\sB})} \defeq 195 | \texistty{\talpha}{\tU}{\texistty{\tn}{\talpha}{\tcodety{\tnpr{\,:\,}\talpha, \ty{\,:\,}\tnpr = \tn,\tx{\,:\,}\cctrans{\sA}}{\cctrans{\sB}}}} 196 | \end{array} 197 | \end{displaymath} 198 | 199 | In this translation, we existentially quantify over the \tech{type} of the 200 | \tech{environment} \im{\talpha}, the \emph{\tech{term}} representing the 201 | \tech{environment} \im{\tn}, and generate \tech{code} that requires an 202 | \tech{environment} \im{\tnpr} plus a proof that the code is only ever given the 203 | environment \im{\tn} as the argument \im{\tnpr}. 204 | The typing rule for an existential package copies the existentially quantified 205 | \techs{term} into the \tech{type}. 206 | That is, for a \tech{closure} \im{\tnpackoe{\tApr,\tv,\te}} of type 207 | \im{\texistty{\talpha}{\tU}{\texistty{\tn}{\talpha}{\tcodety{\tnpr{\,:\,}\talpha, 208 | \ty{\,:\,}\tnpr = \tn,\tx{\,:\,}\cctrans{\sA}}{\cctrans{\sB}}}}}, the typing rule for 209 | \im{\tfont{pack}} requires that we show \im{\te : \tcodety{\tnpr{\,:\,}\tApr, 210 | \ty{\,:\,}\tnpr = \tv,\tx{\,:\,}\cctrans{\sA}}{\cctrans{\sB}}}; notice that the variable \im{\tn} 211 | has been replaced by the \tech{term} representing the environment \im{\tv}. 212 | The equality \im{\tnpr = \tv} essentially unifies projections from \im{\tnpr} 213 | with projections from \im{\tv}, the list of free variables representing the 214 | \tech{environment}. 215 | 216 | The problem with this translation is that it relies on 217 | \emph{\tech{impredicativity}}. 218 | That is, if \im{(\spity{\sx}{\sA}{\sB}) : \spropty}, then we require that 219 | \im{\cctrans{(\spity{\sx}{\sA}{\sB})} : \tpropty}. 220 | Since the existential type quantifies over a type in an arbitrary 221 | \tech{universe} \im{\tU} but must be in the base \tech{universe} \im{\tpropty}, 222 | the existential type must be \tech{impredicative}. 223 | \tech{Impredicative} existential types (weak \techs{dependent pair}) are 224 | consistent on their own, but \tech{impredicativity} causes inconsistency when 225 | combined with other features, including \tech{computational relevance} and 226 | \tech{higher universes}. 227 | For example, in Coq by default, the base \tech{computationally relevant} \tech{universe} 228 | \texttt{Set} is \tech{predicative}, so this translation would not work. 229 | There is a flag to enable \tech{impredicative} \texttt{Set}, but this can 230 | introduce inconsistency with some axioms, such as a combination of the law of 231 | excluded middle plus the axiom of choice, or ad-hoc 232 | polymorphism~\cite{boulier2017}. 233 | Even with \tech{impredicative} \texttt{Set}, there are \tech{computationally relevant} 234 | \tech{higher universes} in Coq's universe hierarchy, and it would be unsound to 235 | allow \tech{impredicativity} at more than one \tech{universe}. 236 | Furthermore, some \tech{dependently typed} languages, such as Agda, do not allow 237 | \tech{impredicativity} at all. 238 | 239 | A second problem arises in developing an \(\eta\) principle, because the 240 | existential type encoding relies on \emph{\tech{parametricity}} to hide the 241 | \tech{environment}. 242 | So, any \(\eta\) principle would need to be justified by a parametric relation 243 | on \tech{environments}. 244 | Internalizing \tech{parametricity} for dependent type theory is an active area 245 | of research~\cite{krishnaswami2013,bernardy2012,keller2012,nuyts2017} and not 246 | all \tech{dependent type} theories admit \tech{parametricity}~\cite{boulier2017}. 247 | 248 | Later, \citet{morrisett1998:ftotal} improved the existential-type translation 249 | for System F, avoiding \tech{translucency} and kinds by relying on \emph{type 250 | erasure} before runtime, which meant that their \tech{code} didn't have to 251 | close over type variables. 252 | This translation does not apply in a \tech{dependently typed} setting, since 253 | \tech{dependent types} can contain \tech{term} variables, not just ``type 254 | erasable'' \tech{type} variables. 255 | 256 | \subsection{Abstract Closure Conversion} 257 | To solve \tech{type-preserving} \tech{closure conversion} for \slang, I avoid 258 | existential types altogether and instead take inspiration from the so-called 259 | \tech{abstract closure conversion} of \citet{minamide1996}. 260 | They add new forms to the target language to represent \tech{code} and 261 | \techs{closure} for a simply typed source language. 262 | In this chapter, I scale their design to \techs{dependent type}. 263 | 264 | I extend \slang with primitive types for \tech{code} and \techs{closure}. 265 | I represent \tech{code} as \im{\tnfune{(\tn:\tApr,\tx:\tA)}{\teone}} of 266 | the \deftech{code type} \im{\tcodety{\tn:\tApr,\tx:\tA}{\tB}}. 267 | These are still \techs{dependent type}, so \im{\tn} may appear in both \im{\tA} 268 | and \im{\tB}, and \im{\tx} may appear in \im{\tB}. 269 | \tech{Code} must be well-typed in an empty \tech{environment}, \ie, when it is 270 | closed. 271 | For simplicity, \tech{code} only takes two arguments. 272 | \begin{mathpar} 273 | \inferrule*[right=\rulename{Code}] 274 | {\ttyjudg{\cdot,\tn:\tApr,\tx:\tA}{\te}{\tB}} 275 | {\ttyjudg{\tlenv}{\tnfune{\tn:\tApr,\tx:\tA}{\te}}{\tcodety{\tn:\tApr,\tx:\tA}{\tB}}} 276 | \end{mathpar} 277 | 278 | I represent \tech{closures} as \im{\tcloe{\te}{\tepr}} of type 279 | \im{\tpity{\tx}{\subst{\tA}{\tepr}{\tn}}{\subst{\tB}{\tepr}{\tn}}}, where 280 | \im{\te} is code and \im{\tepr} is its \tech{environment}. 281 | We \emph{continue} to use \im{\tfontsym{\Pi}} types to describe \tech{closures}; 282 | note that ``functions'' in \slang are implicit \tech{closures}. 283 | The typing rule for \tech{closures} is the following. 284 | % 285 | \begin{mathpar} 286 | \inferrule*[right=\rulename{Clo}] 287 | {\ttyjudg{\tlenv}{\te}{\tcodety{\tn:\tApr,\tx:\tA}{\tB}} \\ 288 | \ttyjudg{\tlenv}{\tepr}{\tApr}} 289 | {\ttyjudg{\tlenv}{\tcloe{\te}{\tepr}}{\tpity{\tx}{\subst{\tA}{\tepr}{\tn}}{\subst{\tB}{\tepr}{\tn}}}} 290 | \end{mathpar} 291 | % 292 | We should think of a \tech{closure} \im{\tcloe{\te}{\tepr}} not as a pair, but 293 | as a delayed partial application of the \tech{code} \im{\te} to its 294 | \tech{environment} \im{\tepr}. 295 | This intuition is formalized in the typing rule since the \tech{environment} is 296 | substituted into the \tech{type}, just as in \tech{dependent-function} 297 | application in \slang. 298 | 299 | To understand the translation, let us start with the translation of functions; 300 | this is the key translation rule. 301 | % 302 | \begin{displaymath} 303 | \begin{array}{@{\hspace{0pt}}r@{\hspace{1ex}}c@{\hspace{1ex}}l} 304 | \cctrans{(\sfune{\sx}{\sA}{\se})} &\defeq& 305 | \tcloe{(\tnfune{\begin{stackTL}(\tn:\tsigmaty{(\txi}{\cctrans{\sAi}\dots)}{\tunitty},\tx:\tlete{\tnpaire{\txi\dots}}{\tn}{\cctrans{\sA}})}{ 306 | \\ 307 | \tlete{\tnpaire{\txi\dots}}{\tn}{\cctrans{\se}}})}{\tnpaire{\txi\dots}} 308 | \end{stackTL} \\ 309 | && \text{ where \im{\sxi:\sAi\dots} are the free variables of \im{\se} and \im{\sA}} 310 | \end{array} 311 | \end{displaymath} 312 | % 313 | The translation of functions is simple to construct. 314 | We know we want to produce a \tech{closure} containing \tech{code} and its 315 | \tech{environment}. 316 | We know the \tech{environment} should be constructed from the free variables of 317 | the body of the function, namely \im{\se}, and, due to \tech{dependent types}, 318 | the type annotation \im{\sA}. 319 | The type of the environment, 320 | \im{\tsigmaty{(\txi}{\cctrans{\sAi}\dots)}{\tunitty}}, is the type of a dependent 321 | list describing the free variables, with a final element of the unit type. 322 | This encodes the fact that type of each variable in the environment can 323 | depend on the value of previous variables. 324 | (The syntax \im{\tlete{\tnpaire{\txi\dots}}{\tn}{\te}} is syntactic sugar for 325 | nested projections from a list implemented with pairs, \ie, for 326 | \im{\tlete{\txone}{\tfste{\tn}}{(\tlete{\txtwo}{\tfste{\tsnde{\tn}}}{(...\tlete{\txin{n}}{(\tfste{\tsnde{...\tsnde{\tn}}}}{\te})})}}.) 327 | 328 | The question is what the translation of \im{\sfontsym{\Pi}} \tech{types} 329 | should look like. 330 | Let's return to the earlier example of the polymorphic identity function. 331 | If we apply the above translation, we produce the following for the inner 332 | \tech{closure}. 333 | We know its \tech{type} by following the typing rules \rulename{Clo} and 334 | \rulename{Code} above. 335 | % 336 | \begin{displaymath} 337 | \begin{array}{l} 338 | \tcloe{\tnfune{(\tntwo:\tpairty{\tpropty}{\tunitty},\tx:\tfste{\tntwo})}{\tx}} 339 | {\tnpaire{\tA,\tnpaire{}}} : 340 | \\ \tpity{(\tx}{\subst{(\tfste{\tntwo})}{\tnpaire{\tA,\tunite}}{\tntwo})}{\subst{(\tfste{\tntwo})}{\tnpaire{\tA,\tunite}}{\tntwo}} 341 | \end{array} 342 | \end{displaymath} 343 | % 344 | We know that the \tech{code} 345 | \im{\tnfune{(\tntwo:\tpairty{\tpropty}{\tunitty},\tx:\tfste{\tntwo})}{\tx}} has type 346 | \im{\tcodety{\tpairty{\tpropty}{\tunitty},\tx:\tfste{\tntwo}}{\tfste{\tntwo}}}. 347 | Following \rulename{Clo}, we substitute the \tech{environment} into this type, so we get the following. 348 | % 349 | \begin{displaymath} 350 | \tpity{(\tx}{\subst{(\tfste{\tntwo})}{\tnpaire{\tA,\tunite}}{\tntwo})}{\subst{(\tfste{\tntwo})}{\tnpaire{\tA,\tunite}}{\tntwo}} 351 | \end{displaymath} 352 | % 353 | So how do we translate the function type \im{\spity{\sx}{\sA}{\sA}} into the 354 | \tech{closure} type 355 | \im{\tpity{(\tx}{\subst{(\tfste{\tntwo})}{\tnpaire{\tA,\tunite}}{\tntwo})}{\subst{(\tfste{\tntwo})}{\tnpaire{\tA,\tunite}}{\tntwo}}}? 356 | Note that this type reduces to \im{\tpity{\tx}{\tA}{\tA}}. 357 | So by the rule \rulename{Conv}, we simply need to translate 358 | \im{\spity{\sx}{\sA}{\sA}} to \im{\tpity{\tx}{\tA}{\tA}}! 359 | 360 | The key translation rules are given below. 361 | % 362 | \begin{displaymath} 363 | \begin{array}{@{\hspace{0pt}}r@{\hspace{1ex}}c@{\hspace{1ex}}l} 364 | \cctrans{(\spity{\sx}{\sA}{\sB})} &\defeq &\tpity{\tx}{\cctrans{\sA}}{\cctrans{\sB}}\\ 365 | \cctrans{(\sfune{\sx}{\sA}{\se})} &\defeq& 366 | \tcloe{(\tnfune{\begin{stackTL}(\tn:\tnsigmaty{(\txi:\cctrans{\sAi}\dots)},\tx:\tlete{\tnpaire{\txi\dots}}{\tn}{\cctrans{\sA}})}{ 367 | \\ 368 | \tlete{\tnpaire{\txi\dots}}{\tn}{\cctrans{\se}}})}{\tnpaire{\txi\dots}} 369 | \end{stackTL} \\ 370 | && \text{ where \im{\sxi:\sAi\dots} are the free variables of \im{\se} and \im{\sA}} 371 | \end{array} 372 | \end{displaymath} 373 | 374 | Observe that, when the source is in \tech{ANF}, this translation maintains \tech{ANF}. 375 | A \tech{closure} ought to be a \tech*{anf:value}{value}, and therefore its 376 | sub-expressions should be \tech*{anf:value}{values}, and the translation 377 | guarantees this. 378 | The body of the \tech{code} should be a \tech*{anf:config}{configuration}, which 379 | is also true, since for any \tech*{anf:config}{configuration} \im{\tM}, 380 | \im{\tlete{\tx}{\tN}{\tM}} is a \tech*{anf:config}{configuration}. 381 | I show this in detail in \fullref[]{sec:abs-cc:cc:anf}. 382 | 383 | A final challenge remains in the design of our target language: we need to know 384 | when two \tech{closures} are equivalent. 385 | As we just saw, \slang partially evaluates terms while type checking. 386 | If two \tech{closures} get evaluated while resolving type equivalence, we may 387 | inline a \tech{term} into the \tech{environment} for one \tech{closure} but not 388 | the other. 389 | When this happens, two \tech{closures} that were syntactically identical and 390 | thus equivalent become inequivalent. 391 | I discuss this problem in detail in \fullref[]{sec:abs-cc:cc}, but essentially 392 | we need to know when two syntactically distinct \tech{closures} are equivalent. 393 | The solution is simple: get rid of the \tech{closures} and keep inlining things! 394 | %Our solution, given below, is simple: closures be damned, keep inlining things! 395 | % 396 | \begin{mathpar} 397 | \inferrule 398 | {\tequivjudg{\tlenv,\tx:\tA}{\subst{\teone}{\teonepr}{\tn}}{\subst{\tetwo}{\tetwopr}{\tn}}} 399 | {\tequivjudg{\tlenv}{\tcloe{(\tnfune{(\tn:\tApr,\tx:\tA)}{\teone})}{\teonepr}}{\tcloe{(\tnfune{(\tn:\tApr,\tx:\tA)}{\tetwo})}{\tetwopr}}} 400 | \end{mathpar} 401 | 402 | Two \techs{closure} are equivalent when we inline the \tech{environment}, free 403 | variables or not, and run the body of the \tech{code}. 404 | We leave the argument free, too. 405 | We run the bodies of the \tech{code} to normal forms, then compare the normal forms. 406 | Recall that equivalence runs \techs{term} while \emph{type checking} and does 407 | not change the program, so the free variables do no harm. 408 | 409 | This equivalence essentially corresponds to an \(\eta\)-principle for \techs{closure}. 410 | From it, we can derive a normal form for \techs{closure} \im{\tcloe{\te}{\tepr}} 411 | that says the \tech{environment} \im{\tepr} contains only free variables, \ie, 412 | \im{\tepr = \tnpaire{\txi\dots}}. 413 | 414 | The above is an intuitive, declarative presentation, but is incomplete without 415 | additional rules. 416 | I use an algorithmic presentation that is similar to the \(\eta\)-equivalence 417 | rules for functions in \slang, which I give in \fullref[]{sec:abs-cc:target}. 418 | --------------------------------------------------------------------------------