Typinferenz.tex 13 KB

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