What is Levity polymorphism

Sibi

As the title of the question indicates, I want to know what is Levity polymorphism and what is its motivation ? I know this page has some details in it, but most of the explanations there go over the top of my head. :)

While this page is a little friendlier, I'm still not able to understand the motivation behind it.

Zeta

Note: This answer is based on very recent observations on Levity discussions. Everything concerning Levity polymorphism is currently only implemented in the GHC 8.0 release candidates and as such subject to change (see #11471 for example).


TL;DR: It's a way to make functions polymorphic over lifted and unlifted types, which is not possible with regular functions. For example the following code doesn't type check with regular poly­mor­phi­sms, since Int# has kind #, but the type variables in id have kind *:

{-# LANGUAGE MagicHash #-}

import GHC.Prim

example :: Int# -> Int# 
example = id            -- does not work, since id :: a -> a
Couldn't match kind ‘*’ with ‘#’
When matching types
  a0 :: *
  Int# :: #
Expected type: Int# -> Int#
  Actual type: a0 -> a0
In the expression: id

Note that (->) still uses some magic.


Before I start to answer this question, let us take a step back and go to one of the most often used functions, ($).

What is ($)'s type? Well, according to Hackage and the report, it's

($) :: (a -> b) -> a -> b

However, that's not 100% complete. It's a convenient little lie. The problem is that polymorphic types (like a and b) have kind *. However, (library) developers wanted to use ($) not only for types with kind *, but also for those of kind #, e.g.

unwrapInt :: Int -> Int#

While Int has kind * (it can be bottom), Int# has kind # (and cannot be bottom at all). Still, the following code typechecks:

unwrapInt $ 42

That shouldn't work. Remember the return type of ($)? It was polymorphic, and polymorphic types have kind *, not #! So why did it work? First, it was a bug, and then it was a hack (excerpt of a mail by Ryan Scott on the ghc-dev mailing list):

So why is this happening?

The long answer is that prior to GHC 8.0, in the type signature ($) :: (a -> b) -> a -> b, b actually wasn't in kind *, but rather OpenKind. OpenKind is an awful hack that allows both lifted (kind *) and unlifted (kind #) types to inhabit it, which is why (unwrapInt $ 42) typechecks.

So what is ($)'s new type in GHC 8.0? It's

($) :: forall (w :: Levity) a (b :: TYPE w). (a -> b) -> a -> b
-- will likely change according to Richard E.

To understand it, we must look at Levity:

data Levity = Lifted | Unlifted

Now, we can think of ($) as having either one of the following types, since there are only two choices of w:

-- pseudo types
($) :: forall a (b :: TYPE   Lifted). (a -> b) -> a -> b
($) :: forall a (b :: TYPE Unlifted). (a -> b) -> a -> b

TYPE is a magical constant, and it redefines the kinds * and # as

type * = TYPE Lifted
type # = TYPE Unlifted

The quantification over kinds is also fairly new and part of the integration of dependent types in Haskell.

The name Levity polymorphism comes from the fact that you now can write polymorphic functions over both lifted and unlifted types, something that wasn't allowed/possible with the previous poly­mor­phism restrictions. It also gets rid of the OpenKind hack at the same time. It's really "just" about this, handling both kinds of kinds.

By the way, you're not alone with your question. Even Simon Peyton Jones said that there's a need for a Levity wiki page, and Richard E. (the current implementer of this) stated that the wiki page needs an update on the current process.

References

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is polymorphism, what is it for, and how is it used?

From Java

What is the difference between dynamic and static polymorphism in Java?

From Java

What is polymorphism in Javascript?

From Dev

What is the motivation behind static polymorphism in C++?

From Dev

What are the reasons that protocols and multimethods in Clojure are less powerful for polymorphism than typeclasses in Haskell?

From Dev

What is the difference between compile time polymorphism and static binding?

From Dev

What is the advantage of using dynamic_cast instead of conventional polymorphism?

From Dev

What are the problems that can arise in an implementation that lacks polymorphism?

From Dev

what is the difference between polymorphism and inheritance

From Dev

what is the equivalent of pure abstract function in static polymorphism?

From Dev

What is polymorphism, explained simply?

From Dev

What is Replace Conditional with Polymorphism Refactoring? How is it implemented in Ruby?

From Dev

What is the need Polymorphism in OOP?

From Dev

What is the difference between polymorphism and overloading?

From Dev

Polymorphism:what is the real type of the called method?

From Dev

What's the right behavior when auto meets polymorphism and virtual function?

From Dev

What is F-Bounded Polymorphism in TypeScript

From Dev

What happened to polymorphism for jOOQ-generated classes?

From Dev

How to choice what method should be called when use polymorphism

From Dev

Polymorphism - What am I not getting?

From Dev

What is the motivation behind static polymorphism in C++?

From Dev

What is the difference between compile time polymorphism and static binding?

From Dev

What are the problems that can arise in an implementation that lacks polymorphism?

From Dev

what is the difference between polymorphism and inheritance

From Dev

What do conditionals do to polymorphic objects in C++? (inclusion polymorphism)

From Dev

What is the need Polymorphism in OOP?

From Dev

What's the right behavior when auto meets polymorphism and virtual function?

From Dev

Purescript row polymorphism. What is the correct syntax?

From Dev

What is right to do to maintain the principle of inheritance polymorphism?

Related Related

  1. 1

    What is polymorphism, what is it for, and how is it used?

  2. 2

    What is the difference between dynamic and static polymorphism in Java?

  3. 3

    What is polymorphism in Javascript?

  4. 4

    What is the motivation behind static polymorphism in C++?

  5. 5

    What are the reasons that protocols and multimethods in Clojure are less powerful for polymorphism than typeclasses in Haskell?

  6. 6

    What is the difference between compile time polymorphism and static binding?

  7. 7

    What is the advantage of using dynamic_cast instead of conventional polymorphism?

  8. 8

    What are the problems that can arise in an implementation that lacks polymorphism?

  9. 9

    what is the difference between polymorphism and inheritance

  10. 10

    what is the equivalent of pure abstract function in static polymorphism?

  11. 11

    What is polymorphism, explained simply?

  12. 12

    What is Replace Conditional with Polymorphism Refactoring? How is it implemented in Ruby?

  13. 13

    What is the need Polymorphism in OOP?

  14. 14

    What is the difference between polymorphism and overloading?

  15. 15

    Polymorphism:what is the real type of the called method?

  16. 16

    What's the right behavior when auto meets polymorphism and virtual function?

  17. 17

    What is F-Bounded Polymorphism in TypeScript

  18. 18

    What happened to polymorphism for jOOQ-generated classes?

  19. 19

    How to choice what method should be called when use polymorphism

  20. 20

    Polymorphism - What am I not getting?

  21. 21

    What is the motivation behind static polymorphism in C++?

  22. 22

    What is the difference between compile time polymorphism and static binding?

  23. 23

    What are the problems that can arise in an implementation that lacks polymorphism?

  24. 24

    what is the difference between polymorphism and inheritance

  25. 25

    What do conditionals do to polymorphic objects in C++? (inclusion polymorphism)

  26. 26

    What is the need Polymorphism in OOP?

  27. 27

    What's the right behavior when auto meets polymorphism and virtual function?

  28. 28

    Purescript row polymorphism. What is the correct syntax?

  29. 29

    What is right to do to maintain the principle of inheritance polymorphism?

HotTag

Archive