Typinferenz.tex 13 KB

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