PFPL Chapter 14 Generic Programming
This part is not shown on PFPL preview version.
Introduction
Many programs are instances of a pattern in a particular situation. Sometimes types determine the pattern by a technique called (typed) generic programming.
Example: the natural number definition in Chapter 9 defines pattern of recursion on values of an inductive type, which is expressed as a generic program.
Consider a function
can be and can be extended to that transform to .
Ambiguity and Type Operator
Ambiguity arises in many-one nature of substitution. A type can have the form
The ambiguity is resolved by giving a template that marks the occurrences of
Such template is known as type operator
Polynomial Type Operators
A type operator is a type equipped with a designated variable whose occurrences mark the spots in the type where a transformation is applied. The abstractor has form
An instance for a type operator
Polynomial type operator is constructed from
- type variable
- types
void
andunit
- product type constructors
- sum type constructors
Inductive Definition
Closure under Substitution
If
By structural induction on
Generic Extension
Polynomial type operators are templates for structure of data structure with slots for values of particular type.
Type operator
specifies all types for any choices of .
Thus a polynomial type operator designates points of interest in a data structure that have a common type.
Generic extension of a polynomial type operator is a form of expression with syntax
It specifies a program that applies a given function to all values lying at points of interest in a compound data structure to obtain a new one with applications at those points.
Statics
- abstractor
serves as a mapping from to . - generic extension of
along with specifies a mapping from to .
Dynamics
Preservation Theorem
If
Positive Type Operators
Positive type operators extend the polynomial type operators to admit restricted forms of function types.
does not occur in is a positive type operator
Any occurrences of type variable
in domain of a function type are negative occurrences, whereas any occurrences of in range of a function type are positive occurrence. A positive type operator is one for which only positive occurrences of
are allowed. The origin of positive occurrence is that function type
is analogous to implication , which is classically equivalent to , so the occurrences in domain are negative occurrence.
Positive type operators are closed under substitution, just like polynomial type operators.
Inductive Definition
Judgment
Generic Extension
The generic extension follow similar definition as polynomial one with one more dynamics rule on function rule
Since
If the result type is
given that with type . Then we might see that the extension yield a function . The trouble is that we are given that
transforms values of type to . We might have to change the type to fit it to .
Exercise
Generic Extension of Const Type Op
It is easy to see that if the generic extension is applied on a constant type operator, where
If we extend this to positive type operators, then it will yield
Database Schema
Since we know that database schema first
and last
are in
Let
Finally the generic extension has form
Non-Negative Occurrence
(Non) Negative Generic Extension
Then the type operator