Typinferenz.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 int$
  52. \item $\vdash(\lambda x.\ 2): \text{int} \rightarrow int$
  53. \item $\vdash(\lambda x.\ 2): \text{Char} \rightarrow int$
  54. \item $\vdash(\lambda x.\ 2): \alpha \rightarrow 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 arbeitet.
  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. \end{definition}
  78. \footnotetext{WS 2013 / 2014, Folie 192}
  79. Dabei ist der lange Strich kein Bruchstrich, sondern ein Symbol der Logik das als
  80. \textbf{Schlussstrich}\xindex{Schlussstrich} bezeichnet wird. Dabei ist der
  81. Zähler als Voraussetzung und der Nenner als Schlussfolgerung zu verstehen.
  82. \begin{definition}[Typsubstituition]\xindex{Typsubstituition}%
  83. Eine \textit{Typsubstituition} ist eine endliche Abbildung von Typvariablen auf
  84. Typen.
  85. \end{definition}
  86. Für eine Menge von Typsubsitutionen wird überlicherweise $\sigma$ als Symbol
  87. verwendet. Man schreibt also beispielsweise:
  88. \[\sigma = [\alpha_1 \text{\pointer} \text{\texttt{bool}}, \alpha_2 \text{\pointer} \alpha_1 \rightarrow \alpha_1]\]
  89. \begin{definition}[Lösung eines Typkontextes]
  90. Sei $t$ eine beliebige freie Variable, $\tau = \tau(t)$ ein beliebiger Typ
  91. $\sigma$ eine Menge von Typsubstitutionen und $\Gamma$ ein Typkontext.
  92. $(\sigma, \tau)$ heißt eine Lösung für $(\Gamma, t)$, falls gilt:
  93. \[\sigma \Gamma \vdash t : \tau\]
  94. \end{definition}
  95. \begin{beispiel}[Typisierungsregel]\xindex{Typisierungsregel}%
  96. Das Folgende nennt man eine Typisierungsregel:\footnote{Klausur WS 2010 / 2011}
  97. \[\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}\]
  98. \end{beispiel}
  99. \section{Constraint-Mengen}
  100. Die Konstraint-Mengen ergeben sich direkt aus den Typisierungsregeln:
  101. \begin{align*}
  102. \CONST:&\text{z.~B.} \CONST \frac{2 \in \text{Const}}{\Gamma \vdash 2 : \alpha_5} \text{ ergibt } \alpha_5 = \text{\texttt{int}}\\
  103. &\\
  104. \VAR: &\\
  105. &\\
  106. \ABS: &\frac{\alpha_2 \vdash \alpha_3}{\alpha_1} \text{ ergibt } \alpha_1 = \alpha_2 \rightarrow \alpha_3\\
  107. &\\
  108. \APP: &\frac{\vdash \alpha_2 \;\;\; \vdash \alpha_3}{\alpha_1} \text{ ergibt } \alpha_2 = \alpha_3 \rightarrow \alpha_1\\
  109. \end{align*}
  110. \section{Let-Polymorphismus}\xindex{let-Polymorphismus}\footnote{WS 2013 / 2014, Folie 205ff}%
  111. Das Programm $P = \text{let } f = \lambda x.\ 2 \text{ in } f\ (f\ \text{\texttt{true}})$
  112. ist eine polymorphe Hilfsfunktion, da sie beliebige Werte auf 2 Abbildet.
  113. Auch solche Ausdrücke sollen typisierbar sein.
  114. Die Kodierung
  115. \[\text{let } x = t_1 \text{ in } t_2\]
  116. ist bedeutungsgleich mit
  117. \[(\lambda x.\ t_2) t_1\]
  118. Das Problem ist, dass
  119. \[P = \lambda f. \ f (f\ \text{\texttt{true}})\ (\lambda x.\ 2)\]
  120. so nicht typisierbar ist, da in
  121. \[\ABS \frac{f: \tau_f \vdash f\ (f\ \text{\texttt{true}}): \dots}{\vdash \lambda f.\ f\ (f\ \text{\texttt{true}}): \dots}\]
  122. müsste
  123. \[\tau_f = \text{bool} \rightarrow \text{int}\]
  124. und zugleich
  125. \[\tau_f = \text{int} \rightarrow \text{int}\]
  126. in den Typkontext eingetragen werden. Dies ist jedoch nicht möglich. Stattdessen
  127. wird
  128. \[\text{let} x = t_1 \text{ in } t_2\]
  129. als neues Konstrukt im $\lambda$-Kalkül erlaubt.
  130. Der Term
  131. \[\text{let } x = t_1 \text{ in } t_2\]
  132. ist bedeutungsgleich zu
  133. \[\lambda x.\ (t_2)\ t_1\]
  134. \begin{definition}[Typschema]\xindex{Typschema}%
  135. Ein Typ der Gestalt $\forall \alpha_1.\ \forall \alpha_2.\ \dots\ \forall \alpha_n. \tau$
  136. heißt \textbf{Typschema}. Es bindet freie Variablen $\alpha_1, \dots, \alpha_n$
  137. in $\tau$.
  138. \end{definition}
  139. \begin{beispiel}[Typschema]
  140. Das Typschema $\forall \alpha.\ \alpha \rightarrow \alpha$ steht für unendlich
  141. viele Typen und insbesondere für folgende:
  142. \begin{bspenum}
  143. \item int $\rightarrow$ int, bool $\rightarrow$ bool, \dots
  144. \item (int $\rightarrow$ int) $\rightarrow$ (int $\rightarrow$ int), \dots
  145. \item \dots
  146. \end{bspenum}
  147. \end{beispiel}
  148. \begin{definition}[Typschemainstanziierung]\xindex{Typschemainstanziierung}%
  149. Sei $\tau_2$ ein Nicht-Schema-Typ. Dann heißt der Typ
  150. \[\tau[\alpha \mapsto \tau_2]\]
  151. eine \textbf{Instanziierung} vom Typschema $\forall \alpha.\ \tau$
  152. und man schreibt:
  153. \[(\forall \alpha.\ \tau) \succeq \tau [\alpha \mapsto \tau_2]\]
  154. \end{definition}
  155. \begin{beispiel}[Typschemainstanziierung]
  156. Folgendes sind Beispiele für Typschemainstanziierungen:
  157. \begin{bspenum}
  158. \item $\forall \alpha.\ \alpha \rightarrow \alpha \succeq \text{int} \rightarrow \text{int}$
  159. \item $\forall \alpha.\ \alpha \rightarrow \alpha \succeq (\text{int} \rightarrow \text{int}) \rightarrow (\text{int} \rightarrow \text{int})$
  160. \item $\text{int} \succeq \text{int}$
  161. \end{bspenum}
  162. Folgendes sind keine Typschemainstanziierungen:
  163. \begin{bspenum}
  164. \item $\alpha \rightarrow \alpha \nsucceq \text{int} \rightarrow \text{int}$
  165. \item $\alpha \nsucceq \text{bool}$
  166. \item $\forall \alpha.\ \alpha \rightarrow \alpha \nsucceq \text{bool}$
  167. \end{bspenum}
  168. \end{beispiel}
  169. Zu Typschemata gibt es angepasste Regeln:\xindex{Typregel!mit Typabstraktionen}%
  170. \[\VAR \frac{\Gamma(x)= \tau' \;\;\; \tau' \succeq \tau}{\gamma \vdash x: \tau}\]
  171. und
  172. \[\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}\]
  173. \todo[inline]{Folie 208ff}
  174. \section{Beispiele}
  175. Im Folgenden wird die Typinferenz für einige $\lambda$-Funktionen durchgeführt.
  176. \subsection[$\lambda x.\ \lambda y.\ x\ y$]{$\lambda x.\ \lambda y.\ x\ y$\footnote{Lösung von Übungsblatt 6, WS 2013 / 2014}}
  177. Gesucht ist ein Typ $\tau$, sodass sich $\vdash \lambda x.\ \lambda y.\ x\ y: \tau$
  178. mit einem Ableitungsbaum nachweisen lässt. Es gibt mehrere solche $\tau$, aber
  179. wir suchen das allgemeinste. Die Regeln unseres Typsystems (siehe \cpageref{def:typsystem-t1})
  180. sind \textit{syntaxgerichtet}, d.~h. zu jedem $\lambda$-(Teil)-Term gibt es genau
  181. eine passende Regel.
  182. Für $\lambda x.\ \lambda y.\ x\ y$ wissen wir also schon, dass jeder Ableitungsbaum\xindex{Ableitungsbaum}
  183. von folgender Gestalt ist. Dabei sind $\alpha_i$ Platzhalter:
  184. \[\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}\]
  185. Das was wir haben wollen steht am Ende, also unter dem unterstem Schlussstrich.
  186. Dann bedeutet die letzte Zeile
  187. \[\vdash \lambda x.\ \lambda \ y.\ x\ y: \alpha_1\]
  188. Ohne (weitere) Voraussetzungen lässt sich sagen, dass der Term
  189. \[\lambda x.\ \lambda \ y.\ x\ y\]
  190. vom Typ $\alpha_1$ ist.
  191. Links der Schlussstriche steht jeweils die Regel, die wir anwenden. Also entweder
  192. $\ABS$, $\VAR$, $\CONST$ oder $\APP$.
  193. Nun gehen wir eine Zeile höher:
  194. \[x:\alpha_2 \vdash \lambda y.\ x\ y\ :\ \alpha_3\]
  195. Diese Zeile ist so zu lesen: Mit der Voraussetzung, dass $x$ vom Typ $\alpha_2$
  196. ist, lässt sich syntaktisch Folgern, dass der Term $\lambda y.\ x\ y$ vom
  197. Typ $\alpha_3$ ist.
  198. \underline{Hinweis:} Alles was in Zeile $i$ dem $\vdash$ steht, steht auch in
  199. jedem \enquote{Nenner} in Zeile $j < i$ vor jedem einzelnen $\vdash$.
  200. Folgende Typgleichungen $C$ lassen sich aus dem Ableitungsbaum ablesen:
  201. \begin{align*}
  202. C &= \Set{\alpha_1 = \alpha_2 \rightarrow \alpha_3}\\
  203. &\cup \Set{\alpha_3 = \alpha_4 \rightarrow \alpha_5}\\
  204. &\cup \Set{\alpha_6 = \alpha_7 \rightarrow \alpha_5}\\
  205. &\cup \Set{\alpha_6 = \alpha_2}\\
  206. &\cup \Set{\alpha_7 = \alpha_4}
  207. \end{align*}
  208. Diese Bedingungen (engl. \textit{Constraints})\xindex{Constraints} haben eine
  209. allgemeinste Lösung mit einem allgemeinsten Unifikator $\sigma_C$:
  210. \begin{align*}
  211. \sigma_C = [&\alpha_1 \Parr (\alpha_4 \rightarrow \alpha_5) \rightarrow \alpha_4 \rightarrow \alpha_5,\\
  212. &\alpha_2 \Parr \alpha_4 \rightarrow \alpha_5,\\
  213. &\alpha_3 \Parr \alpha_4 \rightarrow \alpha_5,\\
  214. &\alpha_6 \Parr \alpha_4 \rightarrow \alpha_5,\\
  215. &\alpha_7 \Parr \alpha_4]
  216. \end{align*}
  217. \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)$
  218. 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$.
  219. \subsection[Selbstapplikation]{Selbstapplikation\footnote{Lösung von Übungsblatt 6, WS 2013 / 2014}}\xindex{Selbstapplikation}
  220. Im Folgenden wird eine Typinferenz für die Selbstapplikation, also
  221. \[\lambda x.\ x\ x\]
  222. durchgeführt.
  223. Zuerst erstellt man den Ableitungsbaum:
  224. \[\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}\]
  225. Dies ergibt die Constraint-Menge
  226. \begin{align}
  227. C&= \Set{\alpha_1 = \alpha_2 \rightarrow \alpha_3} &\text{$\ABS$-Regel}\label{eq:bsp2.c1}\\
  228. &\cup \Set{\alpha_5 = \alpha_4 \rightarrow \alpha_3} &\text{$\APP$-Regel}\label{eq:bsp2.c2}\\
  229. &\cup \Set{\alpha_5 = \alpha_2} &\text{Linke $\VAR$-Regel}\label{eq:bsp2.c3}\\
  230. &\cup \Set{\alpha_4 = \alpha_2} &\text{Rechte $\VAR$-Regel}\label{eq:bsp2.c4}
  231. \end{align}
  232. Aus \cref{eq:bsp2.c3} und \cref{eq:bsp2.c4} folgt:
  233. \[\alpha_2 = \alpha_4 = \alpha_5\]
  234. Also lässt sich \cref{eq:bsp2.c2} umformulieren:
  235. \[\alpha_2 = \alpha_2 \rightarrow \alpha_3\]
  236. Offensichtlich ist diese Bedingung nicht erfüllbar. Daher ist ist die Selbstapplikation
  237. nicht typisierbar. Dies würde im Unifikationsalgorithmus
  238. (vgl. \cref{alg:klassischer-unifikationsalgorithmus})
  239. durch den \textit{occur check} festgestellt werden.