Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Oleg <oleg@okmij.org>
To: francois.pottier@inria.fr
Cc: Kenichi Asai <asai@is.ocha.ac.jp>, caml-list@inria.fr
Subject: Re: [Caml-list] automatically resolving open?
Date: Thu, 24 Apr 2025 13:33:45 +0900	[thread overview]
Message-ID: <aAm/KchC5IRwsnh8@Magus.localnet> (raw)
In-Reply-To: <b1043d81-1eb2-4175-aa64-308f51e7814c@inria.fr>


First of all, to some small degree, such tool already exists: MetaOCaml

> open List
> let test = map (fun x -> x + 1) [1; 2; 3]
>
> I want to obtain:
>
> let test = List.map (fun x -> x + 1) [1; 2; 3]

   open List;;
   .<map (fun x -> x + 1) [1; 2; 3]>.;;

- : int list code = .<
Stdlib.List.map (fun x_1 -> x_1 + 1)
  (Stdlib.List.(::)
     (1, (Stdlib.List.(::) (2, (Stdlib.List.(::) (3, Stdlib.List.[]))))))>.

Everything is fully qualified: perhaps even more than
expected. MetaOCaml has functions to write the code to the file
(without the enclosing brackets). The obvious and major limitation is
that MetaOCaml brackets may only contain expressions (rather than
structures). Furtherfore, local modules etc. in those expressions are
not allowed (since it opens a huge can of worms for an uncertain
benefit: I'm not aware of any compelling example for allowing local
modules in brackets, which cannot be simply worked around in
traditional ways.)

> As far as I know there is currently no syntax for absolute
> paths in OCaml (every path is relative, and every name can
> be shadowed). Maybe we should consider adding such a syntax?

Actually, there is a hack for it in OCaml (OCaml source itself says
it's a hack.) See typing/typeclass.ml in OCaml repo and search for
"*predef*". It comes in the context of default arguments, which are
re-written during type checking into code using Some x and None. 
Those Some and None must be the pre-defined ones, rather than
locally re-defined. Thus the re-writing machinery produces
*predef*.Some and *predef*.None. The typing/env.ml and 
typing/persistent_env.ml has code to deal with
*predef*. Specifically, env.ml when asked to locale *predef*.l
looks up `l' in the initial environment.

It might be good to right this hack. The reason I care about it is
that naively pretty-printing Parsetree (converted from a Typedtree)
may produce identifiers like *predef*.None, which are not
syntactically valid. Therefore, I have to hack the pretty-printer to
ensure that the output is at least parseable, always. Incidentally,
the same typeclass.ml code also generates variable names like *sth*
and *opt* (I think typecore.ml has something similar). Now that we
have Ident.create_local, there is no need for such strange names.


  parent reply	other threads:[~2025-04-24  4:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-23 14:10 Kenichi Asai
2025-04-23 14:32 ` Francois Pottier
2025-04-23 14:38   ` BOBOT François
2025-04-23 14:45   ` Ivan Gotovchits
2025-04-23 15:33     ` Jeremy Yallop
2025-04-24  4:33   ` Oleg [this message]
2025-04-24  6:39 ` Virgile Prevosto
2025-04-24  9:16   ` Ulysse Gérard
2025-04-24 13:06     ` Kenichi Asai
2025-04-24 13:44       ` Ulysse Gérard

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=aAm/KchC5IRwsnh8@Magus.localnet \
    --to=oleg@okmij.org \
    --cc=asai@is.ocha.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=francois.pottier@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