Typinferenz.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. %!TEX root = Programmierparadigmen.tex
  2. \chapter{Typinferenz}
  3. \begin{definition}[Datentyp]\index{Typ|see{Datentyp}}\xindex{Datentyp}%
  4. Ein \textit{Datentyp} oder kurz \textit{Typ} ist eine Menge von Werten, mit
  5. denen eine Bedeutung verbunden ist.
  6. \end{definition}
  7. \begin{beispiel}[Datentypen]
  8. \begin{itemize}
  9. \item $\text{\texttt{bool}} = \Set{\text{True}, \text{False}}$
  10. \item $\text{\texttt{char}} = \text{vgl. \cpageref{sec:ascii-tabelle}}$
  11. \item $\text{\texttt{int}}_{\text{Haskell}} = [-2^{29}, 2^{29}-1] \cap \mathbb{N}$
  12. \item $\text{\texttt{int}}_{\text{C90}} = [-2^{15}-1, 2^{15}-1] \cap \mathbb{N}$\footnote{siehe ISO/IEC 9899:TC2, Kapitel 7.10: Sizes of integer types <limits.h>}
  13. \item \texttt{float} = siehe IEEE 754
  14. \item Funktionstypen, z.~B. $\text{\texttt{int}} \rightarrow \text{\texttt{int}}$ oder
  15. $\text{\texttt{char}} \rightarrow \text{\texttt{int}}$
  16. \end{itemize}
  17. \end{beispiel}
  18. \underline{Hinweis:} Typen sind unabhängig von ihrer Repräsentation. So kann ein
  19. \texttt{bool} durch ein einzelnes Bit repräsentiert werden oder eine Bitfolge
  20. zugrunde liegen.
  21. Auf Typen sind Operationen definiert. So kann man auf numerischen Typen eine
  22. Addition (+), eine Subtraktion (-), eine Multiplikation (*) und eine Division (/)
  23. definieren.\\
  24. Ich schreibe hier bewusst \enquote{eine} Multiplikation und nicht \enquote{die}
  25. Multiplikation, da es verschiedene Möglichkeiten gibt auf Gleitpunktzahlen
  26. Multiplikationen zu definieren. So kann man beispielsweise die Assoziativität
  27. unterschiedlich wählen.
  28. \begin{beispiel}[Multiplikation ist nicht assoziativ]
  29. In Python 3 ist die Multiplikation linksassoziativ. Also:
  30. \inputminted[numbersep=5pt, tabsize=4]{python}{scripts/python/multiplikation.py}
  31. \end{beispiel}
  32. \begin{definition}[Typvariable]\xindex{Typvariable}%
  33. Eine Typvariable repräsentiert einen Typen.
  34. \end{definition}
  35. \underline{Hinweis:} Üblicherweise werden kleine griechische Buchstaben ($\alpha, \beta, \tau_1, \tau_2, \dots$) als Typvariablen gewählt.
  36. Genau wie Typen bestimmte Operationen haben, die auf ihnen definiert sind,
  37. kann man sagen, dass Operationen bestimmte Typen, auf die diese Anwendbar sind. So ist
  38. \[\alpha+\beta\]
  39. für numerische $\alpha$ und $\beta$ wohldefiniert, auch wenn $\alpha$ und $\beta$ boolesch sind
  40. oder beides Strings sind könnte das Sinn machen. Es macht jedoch z.~B. keinen Sinn,
  41. wenn $\alpha$ ein String ist und $\beta$ boolesch.
  42. Die Menge aller Operationen, die auf die Variablen angewendet werden, nennt man
  43. \textbf{Typkontext}\xindex{Typkontext}. Dieser wird üblicherweise mit $\Gamma$
  44. bezeichnet. Der Typkontext weist freien Variablen $x$ ihren Typ $\Gamma(x)$ zu.
  45. Das Ableiten einer Typisierung für einen Ausdruck nennt man \textbf{Typinferenz}\xindex{Typinferenz}.
  46. Man schreibt: $\vdash(\lambda x.2): \alpha \rightarrow \text{int}$.
  47. Bei solchen Ableitungen sind häufig viele Typen möglich. So kann der Ausdruck
  48. \[\lambda x.\ 2\]
  49. folgenderweise typisiert werden:
  50. \begin{itemize}
  51. \item $\vdash(\lambda x.\ 2): \text{bool} \rightarrow \text{int}$
  52. \item $\vdash(\lambda x.\ 2): \text{int} \rightarrow \text{int}$
  53. \item $\vdash(\lambda x.\ 2): \text{Char} \rightarrow \text{int}$
  54. \item $\vdash(\lambda x.\ 2): \alpha \rightarrow \text{int}$
  55. \end{itemize}
  56. In der letzten Typisierung stellt $\alpha$ einen beliebigen Typen dar.
  57. Wichtig ist, dass man sich von unten nach oben vorarbeitet.
  58. \begin{beispiel}[Typinferenz\footnotemark]
  59. Typisieren Sie den Term
  60. \[\lambda a.\ a\ \text{true}\]
  61. unter Verwendung der Regeln \verb+Var+, \verb+Abs+ und \verb+App+.
  62. \[\ABS: \frac{\APP \frac{\VAR \frac{(a:\alpha_2)(a) = \alpha_4}{a: \alpha_2 \vdash a: \alpha_4} \;\;\;\;\;\; a: \alpha_2 \vdash \text{true}: \text{bool}}{a: \alpha_2 \vdash a\ \text{true}: \alpha_3}}{\vdash \lambda a.\ a\ \text{true}: \alpha_1}\]
  63. \end{beispiel}
  64. \footnotetext{Dieses Beispiel stammt aus der Klausur vom WS2013/2014}
  65. \section{Typsystem}
  66. \begin{definition}[Typsystem $\Gamma \vdash t: T$\footnotemark]\label{def:typsystem-t1}\xindex{Typregel}\xindex{Typsystem}\xindex{APP@$\APP$}\xindex{ABS@$\ABS$}\xindex{VAR@$\VAR$}\xindex{CONST@$\CONST$}%
  67. Ein Typsystem besteht aus einem Typkontext $\Gamma$ und folgenden Regeln:
  68. \begin{align*}
  69. \CONST:&\frac{c \in \text{Const}}{\Gamma \vdash c: \tau_c}\\
  70. &\\
  71. \VAR: &\frac{\Gamma(x) = \tau}{\Gamma \vdash x: \tau}\\
  72. &\\
  73. \ABS: &\frac{\Gamma, x: \tau_1 \vdash t: \tau_2}{\Gamma \vdash \lambda x. t: \tau_1 \rightarrow \tau_2}\\
  74. &\\
  75. \APP: &\frac{\Gamma \vdash t_1: \tau_2 \rightarrow \tau\;\;\; \Gamma \vdash t_2: \tau_2}{\Gamma \vdash t_1 t_2: \tau} \\
  76. \end{align*}
  77. wobei $t_1, t_2$ immer $\lambda$-Terme bezeichnet.
  78. \end{definition}
  79. \footnotetext{WS 2013 / 2014, Folie 192}
  80. Dabei ist der lange Strich kein Bruchstrich, sondern ein Symbol der Logik das als
  81. \textbf{Schlussstrich}\xindex{Schlussstrich} bezeichnet wird. Dabei ist der
  82. Zähler als Voraussetzung und der Nenner als Schlussfolgerung zu verstehen.
  83. \begin{definition}[Typsubstituition]\xindex{Typsubstituition}%
  84. Eine \textit{Typsubstituition} ist eine endliche Abbildung von Typvariablen auf
  85. Typen.
  86. \end{definition}
  87. Für eine Menge von Typsubsitutionen wird überlicherweise $\sigma$ als Symbol
  88. verwendet. Man schreibt also beispielsweise:
  89. \[\sigma = [\alpha_1 \text{\pointer} \text{\texttt{bool}}, \alpha_2 \text{\pointer} \alpha_1 \rightarrow \alpha_1]\]
  90. \begin{definition}[Lösung eines Typkontextes]
  91. Sei $t$ eine beliebige freie Variable, $\tau = \tau(t)$ ein beliebiger Typ
  92. $\sigma$ eine Menge von Typsubstitutionen und $\Gamma$ ein Typkontext.
  93. $(\sigma, \tau)$ heißt eine Lösung für $(\Gamma, t)$, falls gilt:
  94. \[\sigma \Gamma \vdash t : \tau\]
  95. \end{definition}
  96. \begin{beispiel}[Typisierungsregel]\xindex{Typisierungsregel}%
  97. Das Folgende nennt man eine Typisierungsregel:\footnote{Klausur WS 2010 / 2011}
  98. \[\frac{\Gamma \vdash b: \text{\texttt{bool}}\;\;\; \Gamma \vdash x: \tau \;\;\; \Gamma \vdash y: \tau }{\Gamma \vdash \text{\textbf{if} b \textbf{then} x \textbf{else} y} : \tau}\]
  99. \end{beispiel}
  100. \section{Constraint-Mengen}
  101. Die Konstraint-Mengen ergeben sich direkt aus den Typisierungsregeln:
  102. \begin{align*}
  103. \CONST:&\text{z.~B.} \CONST \frac{2 \in \text{Const}}{\Gamma \vdash 2 : \alpha_5} \text{ ergibt } \alpha_5 = \text{\texttt{int}}\\
  104. &\\
  105. \VAR: &\\
  106. &\\
  107. \ABS: &\frac{\alpha_2 \vdash \alpha_3}{\alpha_1} \text{ ergibt } \alpha_1 = \alpha_2 \rightarrow \alpha_3\\
  108. &\\
  109. \APP: &\frac{\vdash \alpha_2 \;\;\; \vdash \alpha_3}{\alpha_1} \text{ ergibt } \alpha_2 = \alpha_3 \rightarrow \alpha_1\\
  110. \end{align*}
  111. \section{Let-Polymorphismus}\xindex{let-Polymorphismus}\footnote{WS 2013 / 2014, Folie 205ff}%
  112. Das Programm $P = \text{let } f = \lambda x.\ 2 \text{ in } f\ (f\ \text{\texttt{true}})$
  113. ist eine polymorphe Hilfsfunktion, da sie beliebige Werte auf 2 Abbildet.
  114. Auch solche Ausdrücke sollen typisierbar sein.
  115. Die Kodierung
  116. \[\text{let } x = t_1 \text{ in } t_2\]
  117. ist bedeutungsgleich mit
  118. \[(\lambda x.\ t_2) t_1\]
  119. Das Problem ist, dass
  120. \[P = \lambda f. \ f (f\ \text{\texttt{true}})\ (\lambda x.\ 2)\]
  121. so nicht typisierbar ist, da in
  122. \[\ABS \frac{f: \tau_f \vdash f\ (f\ \text{\texttt{true}}): \dots}{\vdash \lambda f.\ f\ (f\ \text{\texttt{true}}): \dots}\]
  123. müsste
  124. \[\tau_f = \text{bool} \rightarrow \text{int}\]
  125. und zugleich
  126. \[\tau_f = \text{int} \rightarrow \text{int}\]
  127. in den Typkontext eingetragen werden. Dies ist jedoch nicht möglich. Stattdessen
  128. wird
  129. \[\text{let} x = t_1 \text{ in } t_2\]
  130. als neues Konstrukt im $\lambda$-Kalkül erlaubt.
  131. Der Term
  132. \[\text{let } x = t_1 \text{ in } t_2\]
  133. ist bedeutungsgleich zu
  134. \[\lambda x.\ (t_2)\ t_1\]
  135. \begin{definition}[Typschema]\xindex{Typschema}%
  136. Ein Typ der Gestalt $\forall \alpha_1.\ \forall \alpha_2.\ \dots\ \forall \alpha_n. \tau$
  137. heißt \textbf{Typschema}. Es bindet freie Variablen $\alpha_1, \dots, \alpha_n$
  138. in $\tau$.
  139. \end{definition}
  140. \begin{beispiel}[Typschema]
  141. Das Typschema $\forall \alpha.\ \alpha \rightarrow \alpha$ steht für unendlich
  142. viele Typen und insbesondere für folgende:
  143. \begin{bspenum}
  144. \item int $\rightarrow$ int, bool $\rightarrow$ bool, \dots
  145. \item (int $\rightarrow$ int) $\rightarrow$ (int $\rightarrow$ int), \dots
  146. \item \dots
  147. \end{bspenum}
  148. \end{beispiel}
  149. \begin{definition}[Typschemainstanziierung]\xindex{Typschemainstanziierung}%
  150. Sei $\tau_2$ ein Nicht-Schema-Typ. Dann heißt der Typ
  151. \[\tau[\alpha \mapsto \tau_2]\]
  152. eine \textbf{Instanziierung} vom Typschema $\forall \alpha.\ \tau$
  153. und man schreibt:
  154. \[(\forall \alpha.\ \tau) \succeq \tau [\alpha \mapsto \tau_2]\]
  155. \end{definition}
  156. \begin{beispiel}[Typschemainstanziierung]
  157. Folgendes sind Beispiele für Typschemainstanziierungen:
  158. \begin{bspenum}
  159. \item $\forall \alpha.\ \alpha \rightarrow \alpha \succeq \text{int} \rightarrow \text{int}$
  160. \item $\forall \alpha.\ \alpha \rightarrow \alpha \succeq (\text{int} \rightarrow \text{int}) \rightarrow (\text{int} \rightarrow \text{int})$
  161. \item $\text{int} \succeq \text{int}$
  162. \end{bspenum}
  163. Folgendes sind keine Typschemainstanziierungen:
  164. \begin{bspenum}
  165. \item $\alpha \rightarrow \alpha \nsucceq \text{int} \rightarrow \text{int}$
  166. \item $\alpha \nsucceq \text{bool}$
  167. \item $\forall \alpha.\ \alpha \rightarrow \alpha \nsucceq \text{bool}$
  168. \end{bspenum}
  169. \end{beispiel}
  170. Zu Typschemata gibt es angepasste Regeln:\xindex{Typregel!mit Typabstraktionen}%
  171. \[\VAR \frac{\Gamma(x)= \tau' \;\;\; \tau' \succeq \tau}{\gamma \vdash x: \tau}\]
  172. und
  173. \[\ABS \frac{\Gamma, x: \tau_1 \vdash t: \tau_2 \;\;\; \tau_1 \text{ kein Typschema}}{\Gamma \vdash \lambda x. t: \tau_1 \rightarrow \tau_2}\]
  174. \todo[inline]{Folie 208ff}
  175. \section{Beispiele}
  176. Im Folgenden wird die Typinferenz für einige $\lambda$-Funktionen durchgeführt.
  177. \subsection[$\lambda x.\ \lambda y.\ x\ y$]{$\lambda x.\ \lambda y.\ x\ y$\footnote{Lösung von Übungsblatt 6, WS 2013 / 2014}}
  178. Gesucht ist ein Typ $\tau$, sodass sich $\vdash \lambda x.\ \lambda y.\ x\ y: \tau$
  179. mit einem Ableitungsbaum nachweisen lässt. Es gibt mehrere solche $\tau$, aber
  180. wir suchen das allgemeinste. Die Regeln unseres Typsystems (siehe \cpageref{def:typsystem-t1})
  181. sind \textit{syntaxgerichtet}, d.~h. zu jedem $\lambda$-(Teil)-Term gibt es genau
  182. eine passende Regel.
  183. Für $\lambda x.\ \lambda y.\ x\ y$ wissen wir also schon, dass jeder Ableitungsbaum\xindex{Ableitungsbaum}
  184. von folgender Gestalt ist. Dabei sind $\alpha_i$ Platzhalter:
  185. \[\ABS \frac{\ABS\frac{\textstyle\APP \frac{\textstyle\VAR \frac{(x: \alpha_2, y: \alpha_4)\ (x) = \alpha_6}{x: \alpha_2, y: \alpha_4 \vdash x: \alpha_6}\ \ \VAR \frac{(x:\alpha_2, y: \alpha_4)\ (y) = \alpha_7}{x: \alpha_2, y: \alpha_4 \vdash y : \alpha_7}}{\textstyle x: \alpha_2, y: \alpha_4 \vdash x\ y: \alpha_5}}{x:\alpha_2 \vdash \lambda y.\ x\ y\ :\ \alpha_3}}{\vdash \lambda x.\ \lambda \ y.\ x\ y: \alpha_1}\]
  186. Das was wir haben wollen steht am Ende, also unter dem unterstem Schlussstrich.
  187. Dann bedeutet die letzte Zeile
  188. \[\vdash \lambda x.\ \lambda \ y.\ x\ y: \alpha_1\]
  189. Ohne (weitere) Voraussetzungen lässt sich sagen, dass der Term
  190. \[\lambda x.\ \lambda \ y.\ x\ y\]
  191. vom Typ $\alpha_1$ ist.
  192. Links der Schlussstriche steht jeweils die Regel, die wir anwenden. Also entweder
  193. $\ABS$, $\VAR$, $\CONST$ oder $\APP$.
  194. Nun gehen wir eine Zeile höher:
  195. \[x:\alpha_2 \vdash \lambda y.\ x\ y\ :\ \alpha_3\]
  196. Diese Zeile ist so zu lesen: Mit der Voraussetzung, dass $x$ vom Typ $\alpha_2$
  197. ist, lässt sich syntaktisch Folgern, dass der Term $\lambda y.\ x\ y$ vom
  198. Typ $\alpha_3$ ist.
  199. \underline{Hinweis:} Alles was in Zeile $i$ dem $\vdash$ steht, steht auch in
  200. jedem \enquote{Nenner} in Zeile $j < i$ vor jedem einzelnen $\vdash$.
  201. Folgende Typgleichungen $C$ lassen sich aus dem Ableitungsbaum ablesen:
  202. \begin{align*}
  203. C &= \Set{\alpha_1 = \alpha_2 \rightarrow \alpha_3}\\
  204. &\cup \Set{\alpha_3 = \alpha_4 \rightarrow \alpha_5}\\
  205. &\cup \Set{\alpha_6 = \alpha_7 \rightarrow \alpha_5}\\
  206. &\cup \Set{\alpha_6 = \alpha_2}\\
  207. &\cup \Set{\alpha_7 = \alpha_4}
  208. \end{align*}
  209. Diese Bedingungen (engl. \textit{Constraints})\xindex{Constraints} haben eine
  210. allgemeinste Lösung mit einem allgemeinsten Unifikator $\sigma_C$:
  211. \begin{align*}
  212. \sigma_C = [&\alpha_1 \Parr (\alpha_4 \rightarrow \alpha_5) \rightarrow \alpha_4 \rightarrow \alpha_5,\\
  213. &\alpha_2 \Parr \alpha_4 \rightarrow \alpha_5,\\
  214. &\alpha_3 \Parr \alpha_4 \rightarrow \alpha_5,\\
  215. &\alpha_6 \Parr \alpha_4 \rightarrow \alpha_5,\\
  216. &\alpha_7 \Parr \alpha_4]
  217. \end{align*}
  218. \underline{Hinweis:} Es gilt $(\alpha_4 \rightarrow \alpha_5) \rightarrow \alpha_4 \rightarrow \alpha_5 = (\alpha_4 \rightarrow \alpha_5) \rightarrow (\alpha_4 \rightarrow \alpha_5)$
  219. Also gilt: Der allgemeinste Typ von $\lambda x.\ \lambda y.\ x\ y$ ist $\sigma_C (\alpha_1) = (\alpha_4 \rightarrow \alpha_5) \rightarrow \alpha_4 \rightarrow \alpha_5$.
  220. \subsection[Selbstapplikation]{Selbstapplikation\footnote{Lösung von Übungsblatt 6, WS 2013 / 2014}}\xindex{Selbstapplikation}
  221. Im Folgenden wird eine Typinferenz für die Selbstapplikation, also
  222. \[\lambda x.\ x\ x\]
  223. durchgeführt.
  224. Zuerst erstellt man den Ableitungsbaum:
  225. \[\ABS\frac{\APP \frac{\VAR \frac{(x:\alpha_2)\ (x) = \alpha_5}{x:\alpha_2 \vdash x: \alpha_5} \;\;\; \VAR \frac{(x:\alpha_2)\ (x) = \alpha_4}{x:\alpha_2 \vdash x:\alpha_4}}{x: \alpha_2 \vdash x\ x\ :\ \alpha_3}}{\vdash \lambda x.\ x\ x: \alpha_1}\]
  226. Dies ergibt die Constraint-Menge
  227. \begin{align}
  228. C&= \Set{\alpha_1 = \alpha_2 \rightarrow \alpha_3} &\text{$\ABS$-Regel}\label{eq:bsp2.c1}\\
  229. &\cup \Set{\alpha_5 = \alpha_4 \rightarrow \alpha_3} &\text{$\APP$-Regel}\label{eq:bsp2.c2}\\
  230. &\cup \Set{\alpha_5 = \alpha_2} &\text{Linke $\VAR$-Regel}\label{eq:bsp2.c3}\\
  231. &\cup \Set{\alpha_4 = \alpha_2} &\text{Rechte $\VAR$-Regel}\label{eq:bsp2.c4}
  232. \end{align}
  233. Aus \cref{eq:bsp2.c3} und \cref{eq:bsp2.c4} folgt:
  234. \[\alpha_2 = \alpha_4 = \alpha_5\]
  235. Also lässt sich \cref{eq:bsp2.c2} umformulieren:
  236. \[\alpha_2 = \alpha_2 \rightarrow \alpha_3\]
  237. Offensichtlich ist diese Bedingung nicht erfüllbar. Daher ist ist die Selbstapplikation
  238. nicht typisierbar. Dies würde im Unifikationsalgorithmus
  239. (vgl. \cref{alg:klassischer-unifikationsalgorithmus})
  240. durch den \textit{occur check} festgestellt werden.