1

What does it mean that Haskell is weakly polymorphic? Does it have something to do with the fact that a Haskell function can not return a type?

Thanks!

przemo_li
  • 3,932
  • 4
  • 35
  • 60
yonutix
  • 1,964
  • 1
  • 22
  • 51
  • 4
    Do you have a definition of "weakly polymorphic"? As far as I know, this term is only used in OCaml, whereas you usually have the monomorphism restriction in Haskell (which can be turned off). – Zeta Feb 09 '16 at 09:55
  • This question seems theoretical to me... I mean just remove the word Haskell and replace it with "A programming language" and the answers wont change except maybe for the code samples. If you want to ask theoretical questions about languages or other computer science related topics use [cs.SE](http://cs.stackexchange.com/) not SO. – Bakuriu Feb 09 '16 at 09:58
  • Haskell is based on a theoretical model, I'm interested what weakly polymorphism mean relative to Haskell not relative to other programming language. I met this term in the solution given by my Haskell profesor. He said that for normal polymorphism RankNTypes language extension for Haskell is needed. – yonutix Feb 09 '16 at 10:01
  • @Zeta No, I don' t have a definition, I'm searching for it. – yonutix Feb 09 '16 at 10:03
  • 1
    Given some examples from Ocaml, this definitely looks like the [monomorphism restriction](https://stackoverflow.com/questions/32496864/what-is-the-monomorphism-restriction), e.g. `f = map (+1); somelist = f [1..10] :: [Int]` will restrict `f` to `[Int] -> [Int]`, although it could be `Num a => [a] -> [a]`. The linked Q&A provides much information with many external resources. – Zeta Feb 09 '16 at 10:08
  • With the correct language extensions, GHC can do some type level programming which is a way of having a function return a type. It is used in many libraries. However, the syntax is not very nice. Idris is another language that handles type level programming and the syntax is closer to normal functions. – MCH May 10 '18 at 07:19

1 Answers1

1

TL;DR: Haskell is not weakly polymorphic, but some tooling can behave like that under some circumstances if developer what it.

It seams OCaml have notion of

weakly polymorphic type variables

https://caml.inria.fr/pub/docs/manual-ocaml/polymorphism.html

Such a type variable is characterized by being able to only represent a single type and as soon as that type is inferred its every occurrence will be replaced by that type.

By that definition Haskell is NOT weakly polymorphic. Haskell will always infer broadest possible definition of a given type variable.

However, Haskell GHC compiler have what's called:

monomorphism restriction

Which forces behavior as described in OCaml weakly polymorphic variable. However, while OCaml behavior is toggle per variable. Haskell GHC behavior is controlled by runtime flag that is only active when doing interactive REPL, and even that can be turned off!

przemo_li
  • 3,932
  • 4
  • 35
  • 60