Copyright | © Frank Jung 2023 |
---|---|
License | GPL-3.0-only |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
RankNTypes
Description
Based on Haskell Design Patterns by Ryan Lemmer, Chapter 5, Abstracting function types: RankNTypes, page 88..
The applyToFive
example is from Thinking In
Types by Sandy Maguire.
The rank of a function is the "depth" of its polymorphism. Most of typical polymorphic functions in Haskell are of rank 1. e.g.:
const :: a -> b -> a head :: [a] -> a
Using -XRankNTypes
extension makes polymorphism first-class. This
allows us to introduce polymorphism anywhere a type is allowed. The comprimise
is that type inference becomes harder.
See also PolyList for other examples of RankNTypes.
Synopsis
Types
HasShow
type example.
See Thinking with Types, Section 7.1 Existential Types and Eliminators
Functions
applyToFive :: (forall a. a -> a) -> Int Source #
Due to Haskell's implicit quantification of type variables, the
forall a.
part needs to be inside the parentheses. This requires the
-XRankNTypes
extension.
This function is of rank 2 as it receives a function of rank 1 as an argument and runs it on the integer value 5.
elimHasShow :: (forall a. Show a => a -> r) -> HasShow -> r Source #