Typinferenz.tex 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. \begin{definition}[Typsystem $\Gamma \vdash t: T$]\label{def:typsystem-t1}
  58. Ein Typkontext $\Gamma$ ordnet jeder freien Variable $x$ einen Typ $\Gamma(x)$
  59. durch folgende Regeln zu:
  60. \begin{align*}
  61. \CONST:&\frac{c \in \text{Const}}{\Gamma \vdash c: \tau_c}\\
  62. &\\
  63. \VAR: &\frac{\Gamma(x) = \tau}{\Gamma \vdash c: \tau}\\
  64. &\\
  65. \ABS: &\frac{\Gamma, x: \tau_1 \vdash t: \tau_2}{\Gamma \vdash \lambda x. t: \tau_1 \rightarrow \tau_2}\\
  66. &\\
  67. \APP: &\frac{\Gamma \vdash t_1, \tau_2 \tau\;\;\; \Gamma \vdash t_2: \tau_2}{\Gamma \vdash t_1 t_2: \tau}
  68. \end{align*}
  69. \end{definition}
  70. Dabei ist der lange Strich kein Bruchstrich, sondern ein Symbol der Logik das als
  71. \textbf{Schlussstrich}\xindex{Schlussstrich} bezeichnet wird. Dabei ist der
  72. Zähler als Voraussetzung und der Nenner als Schlussfolgerung zu verstehen.
  73. \begin{definition}[Typsubstituition]\xindex{Typsubstituition}%
  74. Eine \textit{Typsubstituition} ist eine endliche Abbildung von Typvariablen auf
  75. Typen.
  76. \end{definition}
  77. Für eine Menge von Typsubsitutionen wird überlicherweise $\sigma$ als Symbol
  78. verwendet. Man schreibt also beispielsweise:
  79. \[\sigma = [\alpha_1 \text{\pointer} \text{\texttt{bool}}, \alpha_2 \text{\pointer} \alpha_1 \rightarrow \alpha_1]\]
  80. \begin{definition}[Lösung eines Typkontextes]
  81. Sei $t$ eine beliebige freie Variable, $\tau = \tau(t)$ ein beliebiger Typ
  82. $\sigma$ eine Menge von Typsubstitutionen und $\Gamma$ ein Typkontext.
  83. $(\sigma, \tau)$ heißt eine Lösung für $(\Gamma, t)$, falls gilt:
  84. \[\sigma \Gamma \vdash t : \tau\]
  85. \end{definition}
  86. \section{Beispiele}
  87. Im Folgenden wird die Typinferenz für einige $\lambda$-Funktionen durchgeführt.
  88. \subsection[$\lambda x.\ \lambda y.\ x\ y$]{$\lambda x.\ \lambda y.\ x\ y$\footnote{Lösung von Übungsblatt 6, WS 2013 / 2014}}
  89. Gesucht ist ein Typ $\tau$, sodass sich $\vdash \lambda x.\ \lambda y.\ x\ y: \tau$
  90. mit einem Ableitungsbaum nachweisen lässt. Es gibt mehrere solche $\tau$, aber
  91. wir suchen das allgemeinste. Die Regeln unseres Typsystems (siehe \cpageref{def:typsystem-t1})
  92. sind \textit{syntaxgerichtet}, d.~h. zu jedem $\lambda$-(Teil)-Term gibt es genau
  93. eine passende Regel.
  94. Für $\lambda x.\ \lambda y.\ x\ y$ wissen wir also schon, dass jeder Ableitungsbaum\xindex{Ableitungsbaum}
  95. von folgender Gestalt ist. Dabei sind $\alpha_i$ Platzhalter:
  96. \[\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}\]
  97. Das was wir haben wollen steht am Ende, also unter dem unterstem Schlussstrich.
  98. Dann bedeutet die letzte Zeile
  99. \[\vdash \lambda x.\ \lambda \ y.\ x\ y: \alpha_1\]
  100. Ohne (weitere) Voraussetzungen lässt sich sagen, dass der Term
  101. \[\lambda x.\ \lambda \ y.\ x\ y\]
  102. vom Typ $\alpha_1$ ist.
  103. Links der Schlussstriche steht jeweils die Regel, die wir anwenden. Also entweder
  104. $\ABS$, $\VAR$, $\CONST$ oder $\APP$.
  105. Nun gehen wir eine Zeile höher:
  106. \[x:\alpha_2 \vdash \lambda y.\ x\ y\ :\ \alpha_3\]
  107. Diese Zeile ist so zu lesen: Mit der Voraussetzung, dass $x$ vom Typ $\alpha_2$
  108. ist, lässt sich syntaktisch Folgern, dass der Term $\lambda y.\ x\ y$ vom
  109. Typ $\alpha_3$ ist.
  110. \underline{Hinweis:} Alles was in Zeile $i$ dem $\vdash$ steht, steht auch in
  111. jedem \enquote{Nenner} in Zeile $j < i$ vor jedem einzelnen $\vdash$.
  112. Folgende Typgleichungen $C$ lassen sich aus dem Ableitungsbaum ablesen:
  113. \begin{align*}
  114. C &= \Set{\alpha_1 = \alpha_2 \rightarrow \alpha_3}\\
  115. &\cup \Set{\alpha_3 = \alpha_4 \rightarrow \alpha_5}\\
  116. &\cup \Set{\alpha_6 = \alpha_7 \rightarrow \alpha_5}\\
  117. &\cup \Set{\alpha_6 = \alpha_2}\\
  118. &\cup \Set{\alpha_7 = \alpha_4}
  119. \end{align*}
  120. Diese Bedingungen (engl. \textit{Constraints})\xindex{Constraints} haben eine
  121. allgemeinste Lösung mit einem allgemeinsten Unifikator $\sigma_C$:
  122. \begin{align*}
  123. \sigma_C = [&\alpha_1 \Parr (\alpha_4 \rightarrow \alpha_5) \rightarrow \alpha_4 \rightarrow \alpha_5,\\
  124. &\alpha_2 \Parr \alpha_4 \rightarrow \alpha_5,\\
  125. &\alpha_3 \Parr \alpha_4 \rightarrow \alpha_5,\\
  126. &\alpha_6 \Parr \alpha_4 \rightarrow \alpha_5,\\
  127. &\alpha_7 \Parr \alpha_4]
  128. \end{align*}
  129. \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)$
  130. 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$.
  131. \subsection[Selbstapplikation]{Selbstapplikation\footnote{Lösung von Übungsblatt 6, WS 2013 / 2014}}\xindex{Selbstapplikation}
  132. Im Folgenden wird eine Typinferenz für die Selbstapplikation, also
  133. \[\lambda x.\ x\ x\]
  134. durchgeführt.
  135. Zuerst erstellt man den Ableitungsbaum:
  136. \[\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}\]
  137. Dies ergibt die Constraint-Menge
  138. \begin{align}
  139. C&= \Set{\alpha_1 = \alpha_2 \rightarrow \alpha_3} &\text{$\ABS$-Regel}\label{eq:bsp2.c1}\\
  140. &\cup \Set{\alpha_5 = \alpha_4 \rightarrow \alpha_3} &\text{$\APP$-Regel}\label{eq:bsp2.c2}\\
  141. &\cup \Set{\alpha_5 = \alpha_2} &\text{Linke $\VAR$-Regel}\label{eq:bsp2.c3}\\
  142. &\cup \Set{\alpha_4 = \alpha_2} &\text{Rechte $\VAR$-Regel}\label{eq:bsp2.c4}
  143. \end{align}
  144. Aus \cref{eq:bsp2.c3} und \cref{eq:bsp2.c4} folgt:
  145. \[\alpha_2 = \alpha_4 = \alpha_5\]
  146. Also lässt sich \cref{eq:bsp2.c2} umformulieren:
  147. \[\alpha_2 = \alpha_2 \rightarrow \alpha_3\]
  148. Offensichtlich ist diese Bedingung nicht erfüllbar. Daher ist ist die Selbstapplikation
  149. nicht typisierbar. Dies würde im Unifikationsalgorithmus
  150. (vgl. \cref{alg:klassischer-unifikationsalgorithmus})
  151. durch den \textit{occur check} festgestellt werden.