## Fun with type functions (2010)

### Cached

### Download Links

Citations: | 6 - 3 self |

### BibTeX

@MISC{Kiselyov10funwith,

author = {Oleg Kiselyov and Simon Peyton and Jones Chung-chieh Shan},

title = {Fun with type functions},

year = {2010}

}

### OpenURL

### Abstract

Tony Hoare has always been a leader in writing down and proving properties of programs. To prove properties of programs automatically, the most widely used technology today is by far the ubiquitous type checker. Alas, static type systems inevitably exclude some good programs and allow some bad ones. Thus motivated, we describe some fun we have been having with Haskell, by making the type system more expressive without losing the benefits of automatic proof and compact expression. Specifically, we offer a programmer’s tour of so-called type families, a recent extension to Haskell that allows functions on types to be expressed as straightforwardly as functions on values. This facility makes it easier for programmers to effectively extend the compiler by writing functional programs that execute during type-checking. This paper gives a programmer’s tour of type families as they are supported in GHC. Source code for all the examples is available at

### Citations

352 |
Proofs and Types
- Girard, Lafont, et al.
- 1989
(Show Context)
Citation Context ... of type-level computation to help mathematicians write more interesting programs that witness their theorems [3]. The motivation for type-level computations comes from the CurryHoward correspondence =-=[17, 23]-=- that underlies Martin-Löf’s intuitionistic type theory: propositions are types, and proofs are terms. The more expressive a type system, the more propositions we can state and prove in it, such as pr... |

77 | A comparative study of language support for generic programming
- Garcia, Jarvi, et al.
- 2003
(Show Context)
Citation Context ...x ys = fromIntegral x : ys -- ... With instances of this class similar to those of the class Add, we can cons an Integer to a list of Doubles without any explicit conversion. 2.3 Graphs Garcia et al. =-=[15]-=- compare the support for generic programming offered by Haskell, ML, C++, C#, and Java. They give a table of qualitative conclusions, in which Haskell is rated favourably in all respects except associ... |

76 | Associated types with class - Chakravarty, Keller, et al. |

75 |
Associated type synonyms
- Chakravarty, Keller, et al.
- 2005
(Show Context)
Citation Context ...l dependencies, but the programming style at the type level feels like logic programming rather than functional programming. The reader may find a programmer’s-eye comparison of the two approaches in =-=[6]-=-. Jones showed recently how the stylistic question can be at least partly addressed by a notational device [28] 25but, more fundamentally, the interaction of functional dependencies with other type-l... |

58 | Type-indexed data types
- HINZE, JEURING, et al.
- 2004
(Show Context)
Citation Context ...g in every leaf! Philosophically, it seems nicer to distinguish the mapping of a key to Nothing from the absence of the mapping for that key. For these reasons, it makes sense to implement Map afresh =-=[19, 22]-=-. As with Memo, we define a class Key and an associated data type Map: class Key k where data Map k :: * -> * empty :: Map k v lookup :: k -> Map k v -> Maybe v -- ...many other methods could be added... |

44 | Functional unparsing
- Danvy
- 1998
(Show Context)
Citation Context ...descriptor fs. Supporting such dependency directly requires a full-spectrum 14dependently typed language, but there is a small literature of neat techniques for getting close without such a language =-=[1, 9, 20]-=-. Here we show one technique using type families. In fact, we accomplish something more general: typed sprintf and sscanf sharing the same format descriptor. Typed sprintf has received a lot more atte... |

37 | Parameterised notions of computation
- Atkey
- 2009
(Show Context)
Citation Context ...es on actions and control flow as well as on values and data flow. In particular, we can express the preconditions and postconditions of monadic actions by generalising monads to parameterised monads =-=[2]-=-. A parameterised monad is a type constructor that takes three arguments, reminiscent of a Hoare triple: an initial state, a final state, and the type of values produced by the action. As shown in the... |

31 | Generalizing generalized tries
- Hinze
(Show Context)
Citation Context ... manage the lookup and update of the memo table. But type functions offer a new possibility: the type of the memo table can be determined automatically from the argument type of the memoised function =-=[12, 19]-=-. We begin by defining a type class Memo. The constraint Memo a means that the behaviour of a function from an argument type a to a result type w can be represented as a memo table of type Table a w, ... |

28 | Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languages
- Carette, Kiselyov, et al.
- 2009
(Show Context)
Citation Context ...sT a) Here CpsT maps a type a to its CPS-converted version [34]. Guillemette and Monnier express CpsT as a type-level function [18], whereas Carette et al. show how to do without type-level functions =-=[4]-=-. 6 Related work and reflections The goal of type families is to build on the success of static type systems, by extending their power and expressiveness without losing their brevity and comprehensibi... |

13 | A type-preserving compiler in Haskell
- Guillemette, Monnier
- 2008
(Show Context)
Citation Context ...function of the type of the argument term: cpsConvert :: Exp a -> Exp (CpsT a) Here CpsT maps a type a to its CPS-converted version [34]. Guillemette and Monnier express CpsT as a type-level function =-=[18]-=-, whereas Carette et al. show how to do without type-level functions [4]. 6 Related work and reflections The goal of type families is to build on the success of static type systems, by extending their... |

11 | Dependent types at work
- Bove, Dybjer
- 2009
(Show Context)
Citation Context ...hile, the theorem-proving and type-theory community has been drawing from its long history of type-level computation to help mathematicians write more interesting programs that witness their theorems =-=[3]-=-. The motivation for type-level computations comes from the CurryHoward correspondence [17, 23] that underlies Martin-Löf’s intuitionistic type theory: propositions are types, and proofs are terms. Th... |

4 |
Practical datatype specializations with phantom types and recursion schemes. Pages 203–228 of
- Fluet, Pucella
- 2005
(Show Context)
Citation Context ...t a program’s behaviour before running it, essentially by defining additional type-checking rules that refine Haskell’s built-in ones. The reader may find many applications of phantom types elsewhere =-=[13, 14, 21]-=-; our focus here is on the additional expressiveness offered by type families – to exclude more bad programs. 5.1 Pointer arithmetic and alignment The refined distinctions afforded by phantom types ar... |

3 |
A framework for point-free program transformation
- Cunha, Pinto, et al.
- 2006
(Show Context)
Citation Context ...ween the new type and a construction out of sum types, product types, and fixpoint types. These boilerplate Memo instances can in fact be defined generically, with the help of functional dependencies =-=[8]-=- or type functions. 3 3.3 Generic finite maps A finite map is a partial function from a domain of keys to a range of values. Finite maps can be represented using many standard data structures, such as... |

2 | Type families. http://haskell.org/ haskellwiki/GHC/Indexed_types - Chakravarty - 2008 |

2 |
Elegant memoization with functional memo tries. http://conal.net/blog/posts/elegant-memoization-withfunctional-memo-tries
- Elliott
- 2008
(Show Context)
Citation Context ... manage the lookup and update of the memo table. But type functions offer a new possibility: the type of the memo table can be determined automatically from the argument type of the memoised function =-=[12, 19]-=-. We begin by defining a type class Memo. The constraint Memo a means that the behaviour of a function from an argument type a to a result type w can be represented as a memo table of type Table a w, ... |