From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 444E781792 for ; Tue, 9 Jul 2013 11:36:26 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of anil@recoil.org) identity=pra; client-ip=89.16.177.154; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="anil@recoil.org"; x-sender="anil@recoil.org"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of anil@recoil.org) identity=mailfrom; client-ip=89.16.177.154; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="anil@recoil.org"; x-sender="anil@recoil.org"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@dark.recoil.org) identity=helo; client-ip=89.16.177.154; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="anil@recoil.org"; x-sender="postmaster@dark.recoil.org"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApgDAK/Y21FZELGadGdsb2JhbABagzuvLpImgTMOAQwVCDyCIwEBBAEnRgELBQsLJSEhJBIGExKHawMJCgiweAMKiFGNAII4MweDCWkDlWyBZ4EpinqINw X-IPAS-Result: ApgDAK/Y21FZELGadGdsb2JhbABagzuvLpImgTMOAQwVCDyCIwEBBAEnRgELBQsLJSEhJBIGExKHawMJCgiweAMKiFGNAII4MweDCWkDlWyBZ4EpinqINw X-IronPort-AV: E=Sophos;i="4.87,1027,1363129200"; d="scan'208";a="20466603" Received: from recoil.dh.bytemark.co.uk (HELO dark.recoil.org) ([89.16.177.154]) by mail3-smtp-sop.national.inria.fr with SMTP; 09 Jul 2013 11:36:25 +0200 Received: (qmail 25940 invoked by uid 634); 9 Jul 2013 09:36:24 -0000 X-Spam-Level: * X-Spam-Check-By: dark.recoil.org Received: from host81-149-102-120.in-addr.btopenworld.com (HELO clink-4.home) (81.149.102.120) (smtp-auth username remote@recoil.org, mechanism cram-md5) by dark.recoil.org (qpsmtpd/0.84) with ESMTPA; Tue, 09 Jul 2013 10:36:23 +0100 Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\)) From: Anil Madhavapeddy In-Reply-To: <51DBD740.3020109@gmail.com> Date: Tue, 9 Jul 2013 10:36:20 +0100 Cc: caml-list@inria.fr Content-Transfer-Encoding: quoted-printable Message-Id: References: <51DBD740.3020109@gmail.com> To: Michael X-Mailer: Apple Mail (2.1503) X-Virus-Checked: Checked by ClamAV on dark.recoil.org Subject: Re: [Caml-list] Re: Opening a module from toploop startup This (rather rough) snippet of code is what we're using in Real World OCaml= to automate the output generation. It's in turn based on Christophe's code= from http://github.com/ocaml/ocaml.org. It does run a set of initial phra= ses to activate and open Core, which is what you're looking for. https://gist.github.com/avsm/5949199 It won't quite compile out of the box, and it requires 4.01.0dev+trunk due = to its use of short_paths, so it's best to crib from it than try to use it = directly. One downside to the current toplevel is that it's rather hard to distinguis= h compiler output from the program output. We'll either switch to utop (wh= ich has support for this), or modify this toplevel to pass the compiler out= put formatter a separate fd from the stdin/stdout so that they can be disti= nguished more clearly. Hope that helps, Anil On 9 Jul 2013, at 10:26, Michael wrote: > Just a gentle ping on this one=85 >=20 > Maybe could someone suggest an open-source project where customisation of= the toplevel takes place? >=20 > Also I am sorry about the poor formatting of my original message, I got t= ripped by HTML editing. The clean code is: > ~~~~~~~~~ > open Format >=20 > module Tag =3D > struct > type t =3D int > let format fft tag =3D > fprintf fft "Tag#%d" tag > let test_open_module () =3D > true > end >=20 > let libdir =3D [ > "/usr/local/lib/ocaml/compiler-libs"; > ] >=20 > let execute_phrase ident phrase =3D > if Toploop.execute_phrase false Format.std_formatter phrase > then > () > else > failwith ident >=20 > let rec make_path_rev p =3D > match p with > | [] -> invalid_arg "make_path_rev" > | h :: [] -> Longident.Lident h > | h :: t -> Longident.Ldot (make_path_rev t, h) >=20 > let make_path p =3D > make_path_rev (List.rev p) >=20 > let install_printer path =3D > let longident =3D make_path_rev ("format" :: List.rev path) in > let phrase =3D Parsetree.Ptop_dir( > "install_printer", Parsetree.Pdir_ident longident > ) > in > execute_phrase "install_printer" phrase >=20 >=20 > let open_module p =3D > let open Parsetree in > let open Location in > let open Asttypes in > let loc =3D { > loc_start =3D Lexing.dummy_pos; > loc_end =3D Lexing.dummy_pos; > loc_ghost =3D false > } > in > let phrase =3D Ptop_def [ > { > pstr_desc =3D > Pstr_open { > txt =3D make_path p; > loc; > }; > pstr_loc =3D loc > }; > ] > in > execute_phrase "open_module" phrase >=20 > let startup () =3D > begin > List.iter Topdirs.dir_directory libdir; > List.iter install_printer [["Demonstrate"; "Tag"]]; > List.iter open_module [["Demonstrate"]; ["Demonstrate"; "Tag" ]]; > end >=20 > let _ =3D > Toploop.toplevel_startup_hook :=3D startup > ~~~~~~~~~ >=20 > Michael Le Barbier Gr=FCnewald schrieb: >> Dear Camlers, >>=20 >> I am learning to produce customised toplevel loops and, while I was able >> to automatically add custom printers, could not open modules. >>=20 >> Obviously, there is something I should know, and don't=85 could someone >> help me? >>=20 >> My code (working example, see below) is a bit long, so let me outline it: >>=20 >> The _startup_ procedure issues a few toploop directives, adding search >> directories, installing printers for the given modules (currently one) >> and opening them. >>=20 >> _open_module_ and _install_printer_ produce and interpret toplevel >> phrases corresponding to the directives with the same name. >>=20 >> Tank you for your help, >> Michael >>=20 >>=20 >> ----8<---- >> Compile: >>=20 >> |ocamlfind ocamlc -c -package "compiler-libs" -o demonstrate.cmo >> demonstrate.ml||| >> |||ocamlfind ocamlmktop -package "compiler-libs" -custom -linkall >> -dllpath-all -linkpkg -package "compiler-libs" demonstrate.cmo -o >> mytoplevel||| >> || >>=20 >> Demonstrate that the Demonstrate.Tag module is not opened: >>=20 >> |> ./mytoplevel||| >> ||| OCaml version 4.00.1||| >> |||| >> |||# test_open_module;;||| >> |||Error: Unbound value test_open_module||| >> |||# ^D||| >> || >>=20 >> Demonstrate that the custom printer is installed: >>=20 >> |> ./mytoplevel||| >> ||| OCaml version 4.00.1||| >> |||| >> |||# open Demonstrate;;||| >> |||# (5 : Tag.t);;||| >> |||- : Demonstrate.Tag.t =3D Tag#5||| >> |||# ^D| >>=20 >> The whole code: >> ~~~~ >> |open Format|| >> || >> ||module Tag =3D|| >> ||struct|| >> || type t =3D int|| >> || let format fft tag =3D|| >> || fprintf fft "Tag#%d" tag|| >> || let test_open_module () =3D|| >> || true|| >> ||end|| >> || >> ||let libdir =3D [|| >> || "/usr/local/lib/ocaml/compiler-libs";|| >> ||]|| >> || >> ||let execute_phrase ident phrase =3D|| >> || if Toploop.execute_phrase false Format.std_formatter phrase|| >> || then|| >> || ()|| >> || else|| >> || failwith ident|| >> || >> ||let rec make_path_rev p =3D|| >> || match p with|| >> || | [] -> invalid_arg "make_path_rev"|| >> || | h :: [] -> Longident.Lident h|| >> || | h :: t -> Longident.Ldot (make_path_rev t, h)|| >> || >> ||let make_path p =3D|| >> || make_path_rev (List.rev p)|| >> || || >> ||let install_printer path =3D|| >> || let longident =3D make_path_rev ("format" :: List.rev path) in|| >> || let phrase =3D Parsetree.Ptop_dir(|| >> || "install_printer", Parsetree.Pdir_ident longident|| >> || )|| >> || in|| >> || execute_phrase "install_printer" phrase|| >> || >> || >> ||let open_module p =3D|| >> || let open Parsetree in|| >> || let open Location in|| >> || let open Asttypes in|| >> || let loc =3D {|| >> || loc_start =3D Lexing.dummy_pos;|| >> || loc_end =3D Lexing.dummy_pos;|| >> || loc_ghost =3D false|| >> || }|| >> || in|| >> || let phrase =3D Ptop_def [|| >> || {|| >> || pstr_desc =3D|| >> || Pstr_open {|| >> || txt =3D make_path p;|| >> || loc;|| >> || };|| >> || pstr_loc =3D loc|| >> || };|| >> || ]|| >> || in|| >> || execute_phrase "open_module" phrase|| >> || >> ||let startup () =3D|| >> || begin|| >> || List.iter Topdirs.dir_directory libdir;|| >> || List.iter install_printer [["Demonstrate"; "Tag"]];|| >> || List.iter open_module [["Demonstrate"]; ["Demonstrate"; "Tag" ]];|| >> || end|| >> || >> ||let _ =3D|| >> || Toploop.toplevel_startup_hook :=3D startup| >> ---->8---- >=20 >=20 > --=20 > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >=20