commit 2ba0cbe492feccd85eafa36a7a145f6faa3b4e98
parent 42c625271230e4d9a71b14f109dce5f792c05624
Author: Georges DupΓ©ron <georges.duperon@gmail.com>
Date: Tue, 4 Jul 2017 12:27:03 +0200
WIP polymorphic functions
Diffstat:
1 file changed, 36 insertions(+), 14 deletions(-)
diff --git a/scribblings/tr.scrbl b/scribblings/tr.scrbl
@@ -856,8 +856,8 @@ therefore keep our overview succinct and gloss over most details.
@list{\mathsf{@x}}
@list{\mathsf{@x}\ @y}))
(define ββ @${\overline{β}})
- (define uπ @${π_Ο
})
- (define nu @${Ο
})
+ (define tvarset @${V})
+ (define uπ @${π_@tvarset})
(define uπβ
@${π_β
})
(define (Ο x) @${Ο(\textit{@x})}))
@@ -937,15 +937,16 @@ therefore keep our overview succinct and gloss over most details.
@todo{Value-belongs-to-type relationship:}
- We define a universe of types @uπ parameterized by @${Ο
β π§}, which indicates
- the set of free variables which may occur in the type. We note individual
- types as @${Ο(\textit{Type})}. Unless otherwise specified, @${Ο(\textit{Type})
- β @uπ β Ο
}. @todo{The previous sentences are a bit fuzzy.} The universe of
- types with no free variables is @${@uπβ
β \mathcal{P}(π»)}.
+ We define a universe of types @uπ parameterized by @${@tvarset β π§}, which
+ indicates the set of free variables which may occur in the type. We note
+ individual types as @${Ο(\textit{Type})}, where @${Type} is the name of the
+ type being considered. Unless otherwise specified, @${Ο(\textit{Type}) β
+ @|uπ|\ β @tvarset}. @todo{The previous sentences are a bit fuzzy.} The
+ universe of types with no free variables is @${@uπβ
β \mathcal{P}(π»)}.
@$${
\begin{gathered}
- \textit{tvar} β Ο
β Ο(\textit{tvar}) β @uπ \\
+ \textit{tvar} β @tvarset β Ο(\textit{tvar}) β @uπ \\
@uπβ
β \mathcal{P}(π»)
\end{gathered}
}
@@ -1002,6 +1003,7 @@ therefore keep our overview succinct and gloss over most details.
@aligned{
Ο(\textit{List}\ A\ \overline{B})
&= Ο(\textit{Pairof}\ Aβ\ (List\ \overline{B})) \\
+ @where \text{$\overline{B} is a placeholder for any number of types$}
Ο(\textit{List}) &= Ο(Null)
}
}
@@ -1049,14 +1051,29 @@ therefore keep our overview succinct and gloss over most details.
&@|quad|@where
(oβ, β¦, oβ) β (Ο'β, β¦, Ο'β) @textif oα΅’ β Ο'α΅’\\[1ex]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- &@cat["fun"]{f} β Ο(β\ \textit{tvar}\ (Οβ, β¦, Οβ β Ο'β, β¦, Ο'β))\\
- &@|quad|@where
- Ο(β\ \textit{tvarβ}\ β¦\ \textit{tvarβ}\ (Οβ, β¦, Οβ β Ο'β, β¦, Ο'β)) β @uπ \\
- &@|quad|@where Ο
βΊ = Ο
βͺ \{\textit{tvarβ} β¦ \textit{tvarβ}\} \\
- &@|quad|@textif Οα΅’, Ο'β±Ό β π_{Ο
βΊ} \\ @;TODO: make @uπ take an argument
+ }
+ }
+
+ For polymorphic functions, we define a @${\operatorname{freetvars}(t)}
+ operator, which returns the set of bound variables accessible within a given
+ type @todo{This is backwards: we did not define well what it means for a bound
+ variable to be accessible.}
+
+ @$${t β @uπ β boundvars(t) = @tvarset}
+
+ @todo{We should not have the @${@textif Οα΅’, Ο'β±Ό β π_{@|tvarset|βΊ}} clause
+ below, instead we should define the notion of well-scopedness of a type.}
+
+ @$${
+ @aligned{
+ &@cat["fun"]{f} β t = Ο(β\ \textit{tvarβ}\ β¦\ \textit{tvarβ}
+ \ (Οβ, β¦, Οβ β Ο'β, β¦, Ο'β))\\
+ &@|quad|@where @tvarset = \operatorname{boundtvars}(t) \\
+ &@|quad|@where @|tvarset|βΊ = @tvarset βͺ \{\textit{tvarβ} β¦ \textit{tvarβ}\} \\
+ &@|quad|@textif Οα΅’, Ο'β±Ό β π_{@|tvarset|βΊ} \\ @;TODO: make @uπ take an argument
&@|quad|@textif
@aligned[#:valign 'top]{
- β \textit{instα΅’} β @uπ, vβ±Ό β Ο(Οβ±Ό) β i
+ β \textit{instα΅’} β @|uπ|\ vβ±Ό β Ο(Οβ±Ό) β j
β &(vβ, β¦, vβ) β dom(f) \\
&β§ f(vβ, β¦, vβ) β (Ο(Ο'β), β¦, Ο(Ο'β))
} \\
@@ -1080,6 +1097,11 @@ therefore keep our overview succinct and gloss over most details.
@todo{is the notation for tuples of values returned by functions okay?}
+ @todo{A function cannot forge a value of type @racket[A], where @racket[A] is
+ a polymorphic type variable. It must return an input value with the desired
+ type (or exit with an error, in which case the function's actual return type
+ is @racket[Nothing]).}
+
@htodo{something else I forgot?}
The association with types and values given above naturally yields the