Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explain weak type variable #71

Open
chenglou opened this issue Feb 2, 2017 · 1 comment
Open

explain weak type variable #71

chenglou opened this issue Feb 2, 2017 · 1 comment

Comments

@chenglou
Copy link
Collaborator

chenglou commented Feb 2, 2017

Hi, An Ocaml beginner here, what does it mean to have _ in your type signatures ? like 
val id2 : '_a -> '_a = <fun> 

IwanKaramazow - Today at 12:25 AM
A type variable '_a (the actual letter doesn't matter, the crucial thing is the underscore) is a so called weak type variable. This is a variable that cannot be generalized, i.e., it can be substituted with only one concrete type. It is like a mutable value, but in the realm of types.

A type denoted with a weak type variable '_a is not included in a type that is denoted with a generic type variable. Moreover, it even can't escape the compilation unit, and should be either hidden or concretized.

Weak type variables are created when an expression is not a pure value (that is defined syntactically). Usually, it is either a function application, or an abstraction. It is usually possible, to get rid of weak type variables by doing a so called eta-expansion, when you substitute a partially applied function to a normal function application by enumerating all function arguments
i.e. id3 = ( fun x -> id2 x)

cc @IwanKaramazow

@ghost
Copy link

ghost commented Feb 3, 2017

FWIW, I would probably use a message like this, given the following:

val f : '_a -> '_b

The type signature of f includes weakly polymorphic type variables ('_a, '_b) so f may not be as polymorphic as expected. Polymorphic generalization usually only occurs if f is defined as a syntactic value. Consider rewriting f using an explicit fun to make it into a value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant