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
typing of pattern bindings not general enough #3053
Comments
Comment author: administrator
I think I understand the problem (I have been bitten too). However I
Hahh, but the compiler is of schizoprenic nature. In some ideal world
--Luc |
Comment author: administrator
I agree with you that it is inconsistent, but would argue that both The core ML typing discipline has a simple property: every variable Now, we could argue whether variables bound by pattern-matching are Still, I'd argue that the natural typing rules for pattern-matching Now, OCaml doesn't follow this natural typing rule, and I believe this As to recognizing that your second example is indeed equivalent to Cheers,
|
Comment author: administrator
Well, I read the first example a bit quickly and I apologize. This extension was introduced for other reasons, related polymorphic --Luc |
Comment author: administrator On Mon, 26 Nov 2001, Luc Maranget wrote:
I had feared that the OCaml-compiler might suffer from a split personality On Mon, 26 Nov 2001, Xavier Leroy wrote:
No problem for me! Reusing the matched value in a different typing context
I agree.
Understandable. I will just avoid this "feature" to be on the safe side Regards, -- |
Comment author: administrator From: Luc Maranget luc.maranget@inria.fr
This is not. You were confused by the overloading on "as", which has Note that the second meaning was introduced for polymorphic variants, As to whether the second example should be accepted as the first one,
The idea is indeed from polymorphic variants, but this specific case
I suppose you mean (_::xs | ([] as xs)). Jacques Garrigue |
Original bug ID: 652
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)
Bug description
Hello,
the following program compiles fine:
let foo = function
| Some n -> Some (float n)
| None as none -> none
But the following:
let foo = function
| Some n -> Some (float n)
| none -> none
gives this type error:
File "foo.ml", line 3, characters 12-16:
This expression has type int option but is here used with type float option
This seems a bit inconsistent, though I understand the reason: in the
first case the compiler is explicitly told that this case (and therefore
the variable) matches "None" whereas in the second case we have a joker
variable.
However, the compiler has knowledge about the alternatives the joker
can take (otherwise it couldn't tell us about non-exhaustive matches)
and should therefore be capable of inferring that this is sound.
An example where this can be quite surprising to the unsuspecting user
is this:
let map_option f = function
| Some x -> Some (f x)
| none -> none
The compiler will infer this type, which is not the most general one:
val map_option : ('a -> 'a) -> 'a option -> 'a option
This may cause quite some confusion at the point of use...
Best regards,
Markus
--
Markus Mottl markus@oefai.at
Austrian Research Institute
for Artificial Intelligence http://www.oefai.at/~markus
The text was updated successfully, but these errors were encountered: