talk.tex 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. \documentclass{beamer}
  2. \usetheme{JuanLesPins}
  3. %\usetheme{Darmstadt}
  4. \usepackage{beamerthemesplit}
  5. \usepackage{pxfonts}
  6. \usepackage{proof}
  7. \usepackage{graphicx}
  8. \input{../../papers/implicit/macros}
  9. \newenvironment{code}{
  10. \begin{block}{}\(\begin{array}{l}
  11. }{
  12. \end{array}\)\end{block}
  13. }
  14. \setlength\parskip{2mm}
  15. \renewcommand\Bar{~~|~~}
  16. \newcommand\CheckType[4]{{#1} \vdash {#2} \uparrow {#3} \leadsto {#4}}
  17. \newcommand\InferType[4]{{#1} \vdash {#2} \downarrow {#3} \leadsto {#4}}
  18. \newcommand\FreshMeta[3]{\mathsf{FreshMeta({#2} \vdash {#1} : {#3})}}
  19. \newcommand\Instantiate[2]{\mathsf{Instantiate}({#1} := {#2})}
  20. \newcommand\Encapsulate[2]{\mathsf{Box}({#1} \Bar {#2})}
  21. \newcommand\MV[1]{\mathsf{MV}({#1})}
  22. \newcommand\False{\mathit{false}}
  23. \newcommand\True{\mathit{true}}
  24. \newcommand\Zero{\mathit{zero}}
  25. \newcommand\Suc{\mathit{suc}}
  26. \newcommand\Set{\mathit{Set}}
  27. \newcommand\Nat{\mathit{Nat}}
  28. \newcommand\Bool{\mathit{Bool}}
  29. \newcommand\Not{\mathit{not}}
  30. \begin{document}
  31. \title{A type-safe treatment of meta variables}
  32. \author{Ulf Norell}
  33. \institute{(joint work with Catarina Coquand)}
  34. \date{\today}
  35. \frame{\titlepage}
  36. \section{Introduction}
  37. \frame{
  38. \frametitle{Abstract}
  39. Type checking with meta variables is trickier than we have previously
  40. believed.
  41. In particular it is easy to violate the invariant that we only compute with
  42. well-typed terms, thus losing normalisation.
  43. We show how to maintain well-typedness by encapsulating possibly ill-typed
  44. terms in boxes that can only be opened if the contents is known to be well-typed.
  45. \vspace*{-8mm}
  46. \begin{flushright}
  47. \includegraphics[width=35mm]{danger_do_not_open_until.pdf}
  48. \end{flushright}
  49. }
  50. \frame{
  51. \frametitle{The Logical Framework}
  52. \[\begin{array}{lclr}
  53. A & ::= & \SET \Or \EL M \Or \PI xAA & \mathit{types} \\
  54. M & ::= & x \Or c \Or f \Or M\,M \Or \LAM xM & \mathit{terms} \\
  55. \Gamma & ::= & () \Or \Gamma,x:A & \mathit{contexts} \\
  56. \end{array}\]
  57. \[\begin{array}{ll}
  58. \IsTypeC\Gamma A & \mbox{$A$ is a valid type in $\Gamma$} \\
  59. \HasTypeC\Gamma MA & \mbox{$M$ has type $A$ in $\Gamma$} \\
  60. \EqualTypeC\Gamma AB & \mbox{$A$ and $B$ are convertible types in $\Gamma$}\\
  61. \EqualC\Gamma MNA & \mbox{$M$ and $N$ are convertible terms of type $A$ in $\Gamma$} \\
  62. \end{array}\]
  63. \[
  64. \infer[\mbox{the conversion rule}]{\HasTypeC\Gamma MB}{
  65. \HasTypeC\Gamma MA
  66. & \EqualTypeC\Gamma AB
  67. }
  68. \]
  69. }
  70. \frame{
  71. \frametitle{Checking conversion}
  72. One option (which scales to meta variables) is to interleave weak-head
  73. normalisation and comparison of heads.
  74. \[\begin{array}{c}
  75. \infer{\EqualC \Gamma {x~\vec M} {x~\vec N} A}{
  76. x : \Delta \to B \in \Gamma
  77. & \EqualC \Gamma {\vec M} {\vec N} \Delta
  78. }
  79. \\{}\\
  80. \infer{\EqualC \Gamma {M,\vec M} {N,\vec N} {(x : A)\Delta}}{
  81. \EqualC \Gamma M N A
  82. & \EqualC \Gamma {\vec M} {\vec N} {\Subst \Delta x M}
  83. }
  84. \end{array}\]
  85. {\small (Weak-head normalisation is left implicit.)}
  86. }
  87. \frame{
  88. \frametitle{Meta variables}
  89. Meta variables can be used for several things. For instance, to represent
  90. \begin{itemize}
  91. \item incomplete parts of a program (instantiated by the user)
  92. \item implicit parts of a program (instantiated by the type checker)
  93. \end{itemize}
  94. In this work we are mostly concerned with the latter use.
  95. }
  96. \frame{
  97. \frametitle{Typing rules for meta variables}
  98. We change the type checker to produce new terms:
  99. \[\begin{array}{lcl}
  100. \CheckType \Gamma e A M && \mbox{type checking} \\
  101. \InferType \Gamma e A M && \mbox{type inference}
  102. \end{array}\]
  103. and add a rule for meta variables (written $?$ in the input language)
  104. \[\infer{\CheckType \Gamma ? A \alpha}{
  105. \FreshMeta \alpha \Gamma A
  106. }\]
  107. }
  108. \section{The Problem}
  109. \frame{
  110. \frametitle{Convertibility with meta variables}
  111. In the presence of meta variables, checking convertibility becomes unification:
  112. \[
  113. \infer{\EqualC \Gamma \alpha M A}{
  114. \alpha \notin \MV M
  115. & \Instantiate \alpha M
  116. }
  117. \]
  118. Functions defined by pattern matching are troublesome.
  119. \[
  120. \EqualC \Gamma {\mathit{isZero}~\alpha} {\mathit{false}} {\mathit{Bool}}
  121. \]
  122. In this case unification gives up, and the constraint is postponed until
  123. more information is available about $\alpha$.
  124. }
  125. \frame{
  126. \frametitle{The conversion rule}
  127. A possible conversion rule (used by previous implementations of Agda):
  128. \[
  129. \infer{\CheckType \Gamma e A M}{
  130. \InferType \Gamma e B M
  131. & \EqualTypeC \Gamma A B
  132. }
  133. \]
  134. This rule makes no distinction between successful unification and postponed constraints.
  135. }
  136. \frame{
  137. \frametitle{What could possibly go wrong?}
  138. Consider the example
  139. \[
  140. \infer{\CheckType \Gamma \Zero {F~\alpha} \Zero}{
  141. \InferType \Gamma \Zero \Nat \Zero
  142. & \EqualTypeC \Gamma {F~\alpha} \Nat
  143. }
  144. \]
  145. where
  146. \[\begin{array}{lcl}
  147. \multicolumn3l{F : \Bool \to \Set} \\
  148. F~\False & = & \Nat \\
  149. F~\True & = & \Bool \\
  150. \end{array}\]
  151. The type checker will accept $\Zero : F~\alpha$ which doesn't seem that
  152. harmful. The problem is that the same rule will accept something of type
  153. $F~\alpha$ whenever a $\Bool$ is required and we can end up with $\Zero :
  154. \Bool$.
  155. }
  156. \frame{
  157. \frametitle{A concrete example}
  158. \[\begin{array}{l}
  159. F : \Bool \to \Set \\
  160. F~\False = \Nat \\
  161. F~\True = \Bool \\
  162. {}\\
  163. h : ((x : F~\alpha) \to F~(\Not~x)) \to \Nat \\
  164. h~g = g~\Zero
  165. \end{array}\]
  166. We get the constraints
  167. \[\begin{array}{lclcl}
  168. \Bool & = & F~\alpha && \mbox{from $x \uparrow \Bool$} \\
  169. F~\alpha & = & \Nat && \mbox{from $\Zero \uparrow {F~\alpha}$} \\
  170. F~(\Not~\Zero) & = & \Nat && \mbox{from ${g~\Zero} \uparrow \Nat$} \\
  171. \end{array}\]
  172. When checking the last constraint the type checker fails with an ``incomplete
  173. pattern matching'' error.
  174. }
  175. \section{The Solution}
  176. \frame{
  177. \frametitle{The Solution}
  178. Our solution is to encapsulate potentially ill-typed terms inside boxes
  179. which can only be opened if the corresponding constraint has been solved.
  180. \[
  181. \infer{\CheckType \Gamma e A c}{
  182. \InferType \Gamma e B M
  183. & \Encapsulate {c : A = M} {\EqualTypeC \Gamma A B}
  184. }
  185. \]
  186. As long as $A = B$ remains unsolved $c$ will not reduce, but as soon as we
  187. solve the constraint it reduces to $M$.
  188. }
  189. \frame{
  190. \frametitle{Type safety}
  191. With this approach we can guarantee type-safety.
  192. \[\CheckType \Gamma e A M \Longrightarrow \HasTypeC \Gamma M A
  193. \]
  194. This holds independent of any postponed constraints.
  195. }
  196. \frame{
  197. \frametitle{Live Demo}
  198. \begin{center}{\Huge Live Demo}
  199. \end{center}
  200. }
  201. \frame{
  202. \frametitle{Bonus Slide}
  203. Remember this rule?
  204. \[
  205. \infer{\EqualC \Gamma {M,\vec M} {N,\vec N} {(x : A)\Delta}}{
  206. \EqualC \Gamma M N A
  207. & \EqualC \Gamma {\vec M} {\vec N} {\Subst \Delta x M}
  208. }
  209. \]
  210. If $M = N$ is postponed we can't guarantee $\vec N : \Subst \Delta x M$.
  211. Options
  212. \begin{itemize}
  213. \item postpone the whole constraint if $M = N$ is postponed
  214. \item allow boxing of constraints
  215. \end{itemize}
  216. \[
  217. \infer{\EqualTypeC \Gamma {M,\vec M : (x : A_1)\Delta_1} {N,\vec N : (x : A_2)\Delta_2}}{
  218. \begin{array}{l}
  219. \Encapsulate {\EqualC \Gamma M N A_1} {\EqualTypeC \Gamma {A_1} {A_2}} \\
  220. \EqualTypeC \Gamma {\vec M : \Subst {\Delta_1} x M} {\vec N : \Subst {\Delta_2} x N}
  221. \end{array}
  222. }
  223. \]
  224. }
  225. \end{document}