Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
GHC
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
4,262
Issues
4,262
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
404
Merge Requests
404
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Glasgow Haskell Compiler
GHC
Commits
746f086e
Commit
746f086e
authored
Apr 22, 2015
by
Simon Peyton Jones
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Better documetation of higher rank types
In response to suggestions on Trac #10281
parent
c715166f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
14 deletions
+48
-14
docs/users_guide/glasgow_exts.xml
docs/users_guide/glasgow_exts.xml
+48
-14
No files found.
docs/users_guide/glasgow_exts.xml
View file @
746f086e
...
...
@@ -7979,10 +7979,21 @@ are clearly not Haskell-98, and an extra flag did not seem worth the bother.
</para>
<para>
The obselete language options
<option>
-XPolymorphicComponents
</option>
and
<option>
-XRank2Types
</option>
are synonyms for
<option>
-XRankNTypes
</option>
. They used to specify finer distinctions that
GHC no longer makes. (They should really elicit a deprecation warning, but they don't, purely
to avoid the need to library authors to change their old flags specifciations.)
In particular, in
<literal>
data
</literal>
and
<literal>
newtype
</literal>
declarations the constructor arguments may
be polymorphic types of any rank; see examples in
<xref
linkend=
"univ"
/>
.
Note that the declared types are
nevertheless always monomorphic. This is important because by default
GHC will not instantiate type variables to a polymorphic type
(
<xref
linkend=
"impredicative-polymorphism"
/>
).
</para>
<para>
The obsolete language options
<option>
-XPolymorphicComponents
</option>
and
<option>
-XRank2Types
</option>
are synonyms for
<option>
-XRankNTypes
</option>
. They used to specify finer
distinctions that GHC no longer makes. (They should really elicit a
deprecation warning, but they don't, purely to avoid the need to
library authors to change their old flags specifciations.)
</para>
<sect3
id=
"univ"
>
...
...
@@ -7990,12 +8001,8 @@ to avoid the need to library authors to change their old flags specifciations.)
</title>
<para>
In a
<literal>
data
</literal>
or
<literal>
newtype
</literal>
declaration one can quantify
the types of the constructor arguments. Here are several examples:
</para>
<para>
These are examples of
<literal>
data
</literal>
and
<literal>
newtype
</literal>
declarations whose data constructors have polymorphic argument types:
<programlisting>
data T a = T1 (forall b. b -> b -> b) a
...
...
@@ -8220,10 +8227,24 @@ for rank-2 types.
<sect2
id=
"impredicative-polymorphism"
>
<title>
Impredicative polymorphism
</title>
<para>
In general, GHC will only instantiate a polymorphic function at
a monomorphic type (one with no foralls). For example,
<programlisting>
runST :: (forall s. ST s a) -> a
id :: forall b. b -> b
foo = id runST -- Rejected
</programlisting>
The definition of
<literal>
foo
</literal>
is rejected because one would have to instantiate
<literal>
id
</literal>
's type with
<literal>
b := (forall s. ST s a) -> a
</literal>
, and
that is not allowed.
Instanting polymorpic type variables with polymorphic types is called
<emphasis>
impredicative polymorphism
</emphasis>
.
</para>
<para>
GHC has extremely flaky support for
<emphasis>
impredicative polymorphism
</emphasis>
,
enabled with
<option>
-XImpredicativeTypes
</option>
.
If it worked, this would mean
that you
can
call a polymorphic function at a polymorphic type, and
that you
<emphasis>
could
</emphasis>
call a polymorphic function at a polymorphic type, and
parameterise data structures over polymorphic types. For example:
<programlisting>
f :: Maybe (forall a. [a] -> [a]) -> Maybe ([Int], [Char])
...
...
@@ -8231,14 +8252,27 @@ parameterise data structures over polymorphic types. For example:
f Nothing = Nothing
</programlisting>
Notice here that the
<literal>
Maybe
</literal>
type is parameterised by the
<emphasis>
polymorphic
</emphasis>
type
<literal>
(forall a. [a] ->
[a])
</literal>
.
<emphasis>
polymorphic
</emphasis>
type
<literal>
(forall a. [a] -> [a])
</literal>
.
However
<emphasis>
the extension should be considered highly experimental, and certainly un-supported
</emphasis>
.
You are welcome to try it, but please don't rely on it working consistently, or
working the same in subsequent releases. See
working the same in subsequent releases. See
<ulink
url=
"https://ghc.haskell.org/trac/ghc/wiki/ImpredicativePolymorphism"
>
this wiki page
</ulink>
for more details.
</para>
<para>
If you want impredicative polymorphism, the main workaround is to use a newtype wrapper.
The
<literal>
id runST
</literal>
example can be written using theis workaround like this:
<programlisting>
runST :: (forall s. ST s a) -> a
id :: forall b. b -> b
nwetype Wrap a = Wrap { unWrap :: (forall s. ST s a) -> a }
foo :: (forall s. ST s a) -> a
foo = unWrap (id (Wrap runST))
-- Here id is called at monomorphic type (Wrap a)
</programlisting>
</para>
</sect2>
<sect2
id=
"scoped-type-variables"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment