From: "Raphaël Proust" <raphlalou@gmail.com>
To: "Mr. Herr" <misterherr@freenet.de>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] what does let () = ... define?
Date: Fri, 18 Mar 2016 21:29:08 +0000 [thread overview]
Message-ID: <CAAmHUAmb_Oa1gaxMHfycosDkbK+_S3=OLboUUSte6_tT73DwHw@mail.gmail.com> (raw)
In-Reply-To: <56EC4318.2020701@freenet.de>
On 18 March 2016 at 18:04, Mr. Herr <misterherr@freenet.de> wrote:
> in a small presentation of OCaml (Linux User Group Bremen) I got some
> interesting questions, and
> trying to answer I noticed I took something for granted that was not fully
> understood.
>
> Looking at this in the toplevel:
>
> # let () = () ;;
> # () ;;
> - : unit = ()
> # let _ = () ;;
> - : unit = ()
The specific case of `let () = …` I would consider to be equivalent to
`let (_ : unit) = …`. I.e., it doesn't bind anything in the
environment; it runs something (the right hand-side of the `=`); it
constrains the result to be of type `unit`.
> # let None = None;;
> Warning 8: this pattern-matching is not exhaustive.
> Here is an example of a value that is not matched:
> Some _
> #
>
> ... the question is: okay, pattern matching left of the equal sign, but what
> does it define?
In this case, nothing. Nothing is “defined” by your pattern matching.
There is just a check that the expression on the right-hand-side of
`=` matches the form on the left-hand-side. In the case of `()` that
check is trivial and is guaranteed to succeed by the type checker. In
the case of `None`, the type checker can't make any guarantees about
`None` being exhaustive; hence the warning.
> It defines unit and None in the environment, and then that value is just
> sitting there?
To be clear, it does NOT define unit nor None in the environment. Both
`()` and `None` are constructors (in this case, destructors). They are
not variables that can be introduced in the environment.
The form `let None = <some_expr> in <some_term>` is equivalent to
`match <some_expr> with None -> <some_term>` which gives a warning
because `None` is not the only case for the option type. (The
exhaustiveness check relies on typing.)
Generally, you can transform most (all?) `let` into `match` using the
correspondence above: `let <some_pattern> = <some_expr> in
<some_term>` into `match <some_expr> with <some_pattern> ->
<some_term>`. The former form is limited to one pattern (which is not
useful in some situations).
Note that using a pattern more complex than a simple variable on the
left-hand-side of a let-binding is often useful.
- for destructing tuples (`let (x,y) = <some_expr> in`) or records
(`let {foo; bar} = <some_expr> in`)
- for enforcing typing constraints (`let (_ : baz) = <some_expr> in`)
- for extracting an element that is present in every part of a sum
(`let (A (pos, _, _) | B (pos, _) | C pos) = <some_expr> in`)
- probably some other things I can't think of right now.
-- r
next prev parent reply other threads:[~2016-03-18 21:29 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-18 18:04 Mr. Herr
2016-03-18 18:26 ` Nick Lucaroni
2016-03-18 18:52 ` Christoph Höger
2016-03-18 22:06 ` Nicolas Barnier
2016-03-18 19:00 ` octachron
2016-03-18 19:42 ` Stefan Monnier
2016-03-18 21:29 ` Raphaël Proust [this message]
2016-03-18 21:54 ` Mr. Herr
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='CAAmHUAmb_Oa1gaxMHfycosDkbK+_S3=OLboUUSte6_tT73DwHw@mail.gmail.com' \
--to=raphlalou@gmail.com \
--cc=caml-list@inria.fr \
--cc=misterherr@freenet.de \
/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