Skip to main content Accessibility help
×
Hostname: page-component-77c89778f8-fv566 Total loading time: 0 Render date: 2024-07-16T23:03:54.940Z Has data issue: false hasContentIssue false

14 - Generic Programming

from Part VI - Infinite Data Types

Published online by Cambridge University Press:  05 March 2016

Robert Harper
Affiliation:
Carnegie Mellon University, Pennsylvania
Get access

Summary

Many programs are instances of a pattern in a particular situation. Sometimes types determine the pattern by a technique called (type) generic programming. For example, in Chapter 9, recursion over the natural numbers is introduced in an ad hoc way. As we shall see, the pattern of recursion on values of an inductive type is expressed as a generic program.

To get a flavor of the concept, consider a function f of type ρρ', which transforms values of type ρ into values of type ρ'. For example, f might be the doubling function on natural numbers.We wish to extend f to a transformation from type [ρ/t]τ to type [ρ'/t]τ by applying f to various spots in the input, where a value of type ρ occurs to obtain a value of type ρ', leaving the rest of the data structure alone. For example, τ might be bool × t, in which case f could be extended to a function of type bool × ρ → bool × ρ' that sends the pairs ⟨a, b⟩ to the pair ⟨a, f (b⟩.

The foregoing example glosses over an ambiguity arising from the many-one nature of substitution. A type can have the form [ρ/t]τ in many different ways, according to how many occurrences of t there are within τ. Given f as above, it is not clear how to extend it to a function from [ρ'/t]τ to [ρ/t]τ. To resolve the ambiguity, we must be given a template that marks the occurrences of t in τ at which f is applied. Such a template is known as a type operator, t.τ, which is an abstractor binding a type variable t within a type τ. Given such an abstractor, we may unambiguously extend f to instances of τ given by substitution for t in τ.

The power of generic programming depends on the type operators that are allowed. The simplest case is that of a polynomial type operator, one constructed from sum and product of types, including their nullary forms. These are extended to positive type operators, which also allow certain forms of function types.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2016

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Generic Programming
  • Robert Harper, Carnegie Mellon University, Pennsylvania
  • Book: Practical Foundations for Programming Languages
  • Online publication: 05 March 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781316576892.016
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Generic Programming
  • Robert Harper, Carnegie Mellon University, Pennsylvania
  • Book: Practical Foundations for Programming Languages
  • Online publication: 05 March 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781316576892.016
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Generic Programming
  • Robert Harper, Carnegie Mellon University, Pennsylvania
  • Book: Practical Foundations for Programming Languages
  • Online publication: 05 March 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781316576892.016
Available formats
×