From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: OCAML <caml-list@inria.fr>
Subject: Typing of patterns
Date: Thu, 1 Jun 2000 15:23:39 +0200 [thread overview]
Message-ID: <20000601152339.A23433@miss.wu-wien.ac.at> (raw)
Hello,
I would like to know something about the typing rules of identifiers that
are bound in pattern matching:
Let's consider this small example:
module type FUNCTOR = sig
type 'a t
val map : ('a -> 'b) -> ('a t -> 'b t)
end
type 'a expr = Num of int | Add of 'a * 'a
module ExprFun : FUNCTOR = struct
type 'a t = 'a expr
let map f = function
| Num n as num -> num
| Add (e, e') -> Add (f e, f e')
end
This will lead to the (shortened) error message:
Values do not match:
val map : ('a -> 'a) -> 'a expr -> 'a expr
is not included in
val map : ('a -> 'b) -> 'a t -> 'b t
The problem arises in this line:
| Num n as num -> num
This looks perfectly ok at first sight, but a closer look reveals that
"num" is not only bound to the value "Num n", it also has the exact type of
the left-hand side. Thus, the rhs will also be forced to have this type if
we use this pattern name.
To correct the problem, we can write:
| Num n -> Num n
But isn't this a bit strange that an identifier cannot be used in a context
where replacing it syntactically with its bound value would be perfectly
ok?
Or has experience shown that using more general types in such cases leads
to more programming errors?
One can, of course, always explicitely restrict the type of an identifier,
but in the upper example we want to have the opposite, i.e. have it more
general. One side effect of this problem is that we cannot efficiently
return the value "as is": we have to construct it again, which may come
with a not insignficant performance penalty...
Are there any deeper insights behind this rationale? (The given code works
without problems if polymorphic variants are used instead).
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
next reply other threads:[~2000-06-03 7:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-06-01 13:23 Markus Mottl [this message]
2000-06-05 13:56 ` Pierre Weis
2000-06-05 15:29 ` Markus Mottl
2000-06-06 0:55 ` Jacques Garrigue
2000-06-06 15:32 ` Pierre Weis
2000-06-06 1:10 ` Patrick M Doane
2000-06-06 8:55 ` Jacques Garrigue
2000-06-06 7:24 Pierre Weis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20000601152339.A23433@miss.wu-wien.ac.at \
--to=mottl@miss.wu-wien.ac.at \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox