Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] monomorphic restriction or typing/scanf bug?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alain Frisch <frisch@c...>
Subject: [Caml-list] Local open (was: monomorphic restriction or typing/scanf bug?)
On Tue, 15 Oct 2002, Chris Hecker wrote:

> Of course, as that thread says, "let exception Blah" would be nice, or
> "local open" to make this pattern nicer to work with.

cf http://www.eleves.ens.fr/home/frisch/soft#openin

With the syntax extension below, you can do:

glouglou ~/openin $ ocaml camlp4o.cma pa_openin.cmo
        Objective Caml version 3.06

        Camlp4 Parsing version 3.06

# let () = open Unix in ();;
# let () = struct exception E end in raise E;;
Exception: E.


That is: local open and local structure items.

Of course, you get ugly error messages when trying to have a type escape
its scope:

# let () = struct type t = A end in A;;
This `let module' expression has type OPENIN_5.t
In this type, the locally bound module name OPENIN_5 escapes its scope


Here is the code of the syntax extension:

let no = ref 0

let local_struct loc st e =
  incr no;
  let x = "OPENIN_" ^ (string_of_int !no) in
  let st = st @ [<:str_item< value res = $e$ >>] in
  <:expr<  let module $x$ = struct $list: st$ end in ($uid:x$.res) >>

EXTEND
 GLOBAL: Pcaml.expr;

 Pcaml.expr: LEVEL "expr1" [
   [ "open"; i = LIST1 UIDENT SEP "."; "in";
        e = Pcaml.expr LEVEL "top" ->
          local_struct loc [<:str_item< open $i$ >>] e
   | "struct"; st = LIST0 [ s = Pcaml.str_item; OPT ";;" -> s ]; "end";
"in";
          e = Pcaml.expr LEVEL "top" ->
            local_struct loc st e
   ]
 ];

END


-- Alain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners