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

Error: The type of this expression... contains type variables that cannot be generalized #58

Open
bkase opened this issue Aug 21, 2016 · 2 comments

Comments

@bkase
Copy link

bkase commented Aug 21, 2016

Hi!

This is a great project, it's really helping me learn Reason

I came across this error:

wat.re:14 28-43
1112type c 'a = option (tuple 'a);
1314let optSwap: c 'a => c 'a = Option.map swap

Error: The type of this expression, '_a c -> '_a c,
       contains type variables that cannot be generalized

when I was doing something similar to this:

let module Option = {
  let map = fun f =>
    fun
    | None => None
    | Some x => Some (f x);
};
type pair 'a = | Pair of 'a 'a;
let swap =
  fun
  | Pair x y => Pair y x;

type c 'a = option (pair 'a);

let optSwap: c 'a => c 'a = Option.map swap

This page shows a very simple repro case:
https://ocaml.org/learn/tutorials/common_error_messages.html#Thetypeofthisexpressioncontainstypevariablesthatcannotbegeneralized

I understand why ref None needs more type information, but why does Option.map swap? I feel like the language should let me do this:

wat.re:17 28-31
14let optSwap: c 'a => c 'a = Option.map swap;
1516let _ = optSwap (Some (Pair 1 2));
17let _ = optSwap (Some (Pair 'a' 'b'));

Error: The types don't match.
This is: char
Wanted:  int

In any case, this probably deserves a better error, right?

@jaredly
Copy link

jaredly commented Aug 31, 2016

@bkase generalization doesn't survive through partial application afaik.
you need to define a new function like

let optSwap val => Option.map swap val

@chenglou
Copy link
Collaborator

chenglou commented Sep 6, 2016

Merging #36 into here!

https://ocaml.org/learn/tutorials/common_error_messages.html

See the relevant section down there.

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

3 participants