From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id IAA00716 for caml-redistribution; Thu, 12 Nov 1998 08:56:42 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA03908 for ; Tue, 10 Nov 1998 17:05:27 +0100 (MET) Received: from post.tepkom.ru (relay.tepkom.ru [195.9.240.162]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id RAA03554 for ; Tue, 10 Nov 1998 17:05:18 +0100 (MET) Received: from localhost (msk@localhost) by post.tepkom.ru (8.8.7/8.8.7) with ESMTP id TAA29366; Tue, 10 Nov 1998 19:04:46 +0300 Date: Tue, 10 Nov 1998 19:04:45 +0300 (MSK) From: Anton Moscal To: John Prevost cc: caml-list@inria.fr Subject: Re: Local opening of modules In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: weis On Tue, 27 Oct 1998, John Prevost wrote: > This is a question that I found asked back in the logs, but which I > found no answer to: > > Is there any good reason that Caml doesn't have a mechanism like SML's > to open a module locally? For example: > > let open Num in > let a = num_of_int 2739 in > let b = num_of_int 234 in > let bar = a +/ b;; > [...] > > Maybe there's a technical constraint in the way Caml is organized that > keeps this from being done? > With CamlP4 preprocessor (tool for syntax extension of Ocaml) I got this effect by the following grammar extension: ====cut==== let (uniq_lid, uniq_uid) = let cnt = ref 0 in (incr cnt; fun () -> Printf.sprintf "l_uniq_id_%d" !cnt), (incr cnt; fun () -> Printf.sprintf "U_uniq_id_%d" !cnt) EXTEND expr: LEVEL "expr1" [[ "let"; "open"; path = LIST1 UIDENT SEP "."; "in"; e = expr LEVEL "top" -> let temp = uniq_uid () and res = uniq_lid () in <:expr< let module $temp$ = struct open $path$; value $lid:res$ = $e$; end in $uid:temp$.$lid:res$ >> ]]; END ====cut==== After making these changes you can write in your program something like: ====cut==== module M = struct module N = struct let x = 1 end;; end let x = let open M.N in x ====cut==== Regards, Anton