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 2B7047F9BB for ; Thu, 3 Jul 2014 08:17:09 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of philippe.veber@gmail.com) identity=pra; client-ip=74.125.82.172; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="philippe.veber@gmail.com"; x-sender="philippe.veber@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of philippe.veber@gmail.com designates 74.125.82.172 as permitted sender) identity=mailfrom; client-ip=74.125.82.172; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="philippe.veber@gmail.com"; x-sender="philippe.veber@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-we0-f172.google.com) identity=helo; client-ip=74.125.82.172; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="philippe.veber@gmail.com"; x-sender="postmaster@mail-we0-f172.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlMBAH30tFNKfVKslGdsb2JhbABag19avQ6JFgGBAAgWDwEBAQEHCwsJEiqEAwEBAQMBEhUZARsdAQMBCwYFCwYDAQIBJwchAQERAQUBCwkIBhMiiAsBAwkInzRqjRmDEI9DChknDWSGAhEBBQ6MbIIXEQcGhD0FmG6Bf4FIjC6EIxgphHU7 X-IPAS-Result: AlMBAH30tFNKfVKslGdsb2JhbABag19avQ6JFgGBAAgWDwEBAQEHCwsJEiqEAwEBAQMBEhUZARsdAQMBCwYFCwYDAQIBJwchAQERAQUBCwkIBhMiiAsBAwkInzRqjRmDEI9DChknDWSGAhEBBQ6MbIIXEQcGhD0FmG6Bf4FIjC6EIxgphHU7 X-IronPort-AV: E=Sophos;i="5.01,593,1400018400"; d="scan'208";a="69964166" Received: from mail-we0-f172.google.com ([74.125.82.172]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 03 Jul 2014 08:17:07 +0200 Received: by mail-we0-f172.google.com with SMTP id u57so12214744wes.3 for ; Wed, 02 Jul 2014 23:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=6zTHryHLZ8f2iDmQIR8d1L/R15DeHJp54TUfjzLYM+Q=; b=hhwSBJVcz67yNt5JtZJZuoe5D7AKw6TQVeVUUxLekiPLcu/Bk891ZPe1Zl4lpywskN EBVZa3KKbW156LYS5IN9AVA+RuevTJgjZhYAsSDQNOM10+PbcNjTBYNdsHmQ0ihGdhIq fl+K0GgY8HVMgjMzrw2dZyFhEt3w6iHA28BVaHOcPZHfPHtyVaCPHsnMIaGJn+FP5yI6 qrTeFnunzLvW3Zgs8O80gSB6bS1nnEkmZXFxunxwd/wIGZZUKnT+3LyQ6Dxi6SZ6a4o4 CXbgamoVNlwf5bETvBHdl2TjXWVxQNEH0ZD2vGGYQiMkkxOqZ2v+BeWCkMZAsluwqtjy iUlA== X-Received: by 10.180.72.38 with SMTP id a6mr48558354wiv.29.1404368227667; Wed, 02 Jul 2014 23:17:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.175.232 with HTTP; Wed, 2 Jul 2014 23:16:47 -0700 (PDT) In-Reply-To: References: <53B3F496.2020908@inria.fr> From: Philippe Veber Date: Thu, 3 Jul 2014 08:16:47 +0200 Message-ID: To: Ashish Agarwal Cc: Romain Bardou , Fabrice Le Fessant , David Sheets , caml users Content-Type: multipart/alternative; boundary=001a11c2497858167a04fd43f541 X-Validation-by: philippe.veber@gmail.com Subject: Re: [Caml-list] Fwd: Toplevel and syntax extension. --001a11c2497858167a04fd43f541 Content-Type: text/plain; charset=ISO-8859-1 That's particularly neat! Not only does it work (I thought ocamlscript wouldn't accept syntax extensions, and did not even try), but also it gets me native code speed for free. Thanks Ashish (and Martin, of course)! 2014-07-02 17:20 GMT+02:00 Ashish Agarwal : > Yet another option is to use ocamlscript. The following works: > > $ cat script.ml > #! /usr/bin/env ocamlscript > Ocaml.ocamlflags := ["-thread"]; > Ocaml.packs := ["sexplib.syntax"] > -- > > open Sexplib.Std > type t = int with sexp > > $ ./script.ml > (* compiles without error *) > > > > > On Wed, Jul 2, 2014 at 10:48 AM, Philippe Veber > wrote: > >> Thanks Romain, I'll rather use Fabrice's suggestion, which handles >> command-line argument more simply. >> >> Cheers, >> >> Philippe. >> >> >> 2014-07-02 14:01 GMT+02:00 Romain Bardou : >> >> You could write a wrapper which start the ocaml process, sends a string >>> containing something like: >>> >>> module Sys = >>> struct >>> include Sys >>> let argv = ... (* fill this *) >>> end >>> >>> to the ocaml process (replace the ... by the arguments given to the >>> wrapper, using the array syntax, and don't forget that the first cell >>> must contain the executable path), and then pass the contents of your >>> script.ml. >>> >>> This does not work if your script uses other modules which themselves >>> use Sys.argv. >>> >>> Cheers, >>> >>> -- Romain Bardou >>> >>> On 02/07/2014 13:48, Philippe Veber wrote: >>> > Thanks Fabrice, this perfectly explains what I observe. Is this >>> behavior >>> > considered the right one? Reading from a pipe is regretfully not an >>> > option for me, as my script has command line arguments. Hence when I >>> type: >>> > >>> > cat script.ml | ocaml --foo --bar 1 >>> > >>> > the toplevel complains it knows nothing about the arguments foo and >>> bar. >>> > A "--" argument would be useful but it seems not available. If it's so, >>> > I'll file a feature request on Mantis, since without it, there seems to >>> > be no way to give a script to the toplevel that both takes command line >>> > arguments and uses a syntax extension. >>> > >>> > Thanks again! >>> > >>> > >>> > >>> > 2014-07-02 10:08 GMT+02:00 Fabrice Le Fessant >>> > >: >>> > >>> > If I remember well, I think "ocaml" has a different behavior >>> > depending on what it reads from: >>> > * From a pipe, it parses every sentence and execute each one >>> > immediatly. >>> > * From a file, it tries to parse the whole file, and then executes >>> > everything. >>> > >>> > In the second case, it means it will only execute the load of the >>> > syntax extension after parsing the whole file... which will fail, >>> > since the syntax extension is needed for that. >>> > >>> > --Fabrice >>> > INRIA & OCamlPro >>> > >>> > >>> > >>> > On Tue, Jul 1, 2014 at 7:06 PM, Philippe Veber >>> > > >>> wrote: >>> > >>> > Thanks David! >>> > >>> > The first call fails with a syntax error on "with sexp": >>> > >>> > [pbil:~ 18:58]$cat rien.ml >>> > >>> > let () = >>> > try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") >>> > with Not_found -> () >>> > ;; >>> > >>> > #use "topfind";; >>> > #camlp4o;; >>> > #require " sexplib.syntax";; >>> > >>> > open Sexplib.Std;; >>> > >>> > type t = int with sexp;; >>> > >>> > [pbil:~ 18:58]$ocaml rien.ml >>> > File "rien.ml ", line 12, characters 13-17: >>> > Error: Syntax error >>> > >>> > It seems like the sexp syntax extension is not loaded when the >>> > script is evaluated. But it's not really clear to me what going >>> > wrong... >>> > >>> > Cheers! >>> > ph. >>> > >>> > >>> > >>> > 2014-07-01 18:51 GMT+02:00 David Sheets >> > >: >>> > >>> > On Tue, Jul 1, 2014 at 5:38 PM, Philippe Veber >>> > >> >> >>> > wrote: >>> > > Reposting this question here, just in case. >>> > > >>> > > ---------- Forwarded message ---------- >>> > > From: Philippe Veber >> > > >>> > > Date: 2014-06-28 21:32 GMT+02:00 >>> > > Subject: Toplevel and syntax extension. >>> > > To: ocaml_beginners@yahoogroups.com >>> > >>> > > >>> > > >>> > > Dear camlers, >>> > > >>> > > Consider the following script: >>> > > >>> > > #use "topfind";; >>> > > #camlp4o;; >>> > > #require "sexplib.syntax";; >>> > > >>> > > open Sexplib.Std;; >>> > > >>> > > type t = int with sexp;; >>> > > >>> > > Saved as script.ml , the simple call: >>> > > >>> > > ocaml script.ml >>> > > >>> > > fails while the call: >>> > > >>> > > cat script.ml | ocaml >>> > > >>> > > succeeds. Any idea how I could fix the first call? >>> > >>> > How does the first call fail? A difference between the two >>> > is that, in >>> > the second, the .ocamlinit file is used. If you are using >>> > opam with >>> > ocamlfind installed via it, this file will contain your >>> > Topdirs setup. >>> > You can try: >>> > >>> > let () = >>> > try Topdirs.dir_directory (Sys.getenv >>> "OCAML_TOPLEVEL_PATH") >>> > with Not_found -> () >>> > ;; >>> > >>> > at the top of your script (after hashbang but before >>> > directives). >>> > >>> > Hope this helps, >>> > >>> > David >>> > >>> > >>> > >>> > >>> > >>> > -- >>> > Fabrice LE FESSANT >>> > Chercheur en Informatique >>> > INRIA Paris Rocquencourt -- OCamlPro >>> > Programming Languages and Distributed Systems >>> > >>> > >>> >>> >> > --001a11c2497858167a04fd43f541 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
That's particularly neat! Not only does it work (I tho= ught ocamlscript wouldn't accept syntax extensions, and did not even tr= y), but also it gets me native code speed for free. Thanks Ashish (and Mart= in, of course)!



201= 4-07-02 17:20 GMT+02:00 Ashish Agarwal <agarwal1975@gmail.com><= /span>:
Yet another option is to us= e ocamlscript. The following works:

$ cat script.ml
#! /usr/bin/env ocamlscript
Ocaml.ocamlflags :=3D ["-thread&qu= ot;];
Ocaml.packs :=3D ["sexplib.syntax"]
--

open Sexplib.Std
type t =3D int with sexp

(* compiles without error *)




On Wed, Jul 2, 2014 at 10:48 AM, Philipp= e Veber <philippe.veber@gmail.com> wrote:
Thanks Romain, I'll rather use Fabrice's= suggestion, which handles command-line argument more simply.

= Cheers,

=A0Philippe.

<= br>
2014-07-02 14:01 GMT+02:00 Romain Bardou <= romain.bardou@inria.fr>:

You could write a wrapper which start the ocaml process, sends a string
containing something like:

module Sys =3D
struct
=A0 include Sys
=A0 let argv =3D ... (* fill this *)
end

to the ocaml process (replace the ... by the arguments given to the
wrapper, using the array syntax, and don't forget that the first cell must contain the executable path), and then pass the contents of your
script.ml.

This does not work if your script uses other modules which themselves
use Sys.argv.

Cheers,

-- Romain Bardou

On 02/07/2014 13:48, Philippe Veber wrote:
> Thanks Fabrice, this perfectly explains what I observe. Is this behavi= or
> considered the right one? Reading from a pipe is regretfully not an
> option for me, as my script has command line arguments. Hence when I t= ype:
>
> cat script.ml= <http://script.ml>= ; | ocaml --foo --bar 1
>
> the toplevel complains it knows nothing about the arguments foo and ba= r.
> A "--" argument would be useful but it seems not available. = If it's so,
> I'll file a feature request on Mantis, since without it, there see= ms to
> be no way to give a script to the toplevel that both takes command lin= e
> arguments and uses a syntax extension.
>
> Thanks again!
>
>
>
> 2014-07-02 10:08 GMT+02:00 Fabrice Le Fessant
> <Fabrice.Le_fessant@inria.fr <mailto:Fabrice.Le_fessant@inria.fr>&g= t;:
>
> =A0 =A0 If I remember well, I think "ocaml" has a different = behavior
> =A0 =A0 depending on what it reads from:
> =A0 =A0 * From a pipe, it parses every sentence and execute each one > =A0 =A0 immediatly.
> =A0 =A0 * From a file, it tries to parse the whole file, and then exec= utes
> =A0 =A0 everything.
>
> =A0 =A0 In the second case, it means it will only execute the load of = the
> =A0 =A0 syntax extension after parsing the whole file... which will fa= il,
> =A0 =A0 since the syntax extension is needed for that.
>
> =A0 =A0 --Fabrice
> =A0 =A0 INRIA & OCamlPro
>
>
>
> =A0 =A0 On Tue, Jul 1, 2014 at 7:06 PM, Philippe Veber
> =A0 =A0 <philippe.veber@gmail.com <mailto:philippe.veber@gmail.com>&= gt; wrote:
>
> =A0 =A0 =A0 =A0 Thanks David!
>
> =A0 =A0 =A0 =A0 The first call fails with a syntax error on "with= sexp":
>
> =A0 =A0 =A0 =A0 [pbil:~ 18:58]$cat rien.ml <http://rien.ml>
>
> =A0 =A0 =A0 =A0 let () =3D
> =A0 =A0 =A0 =A0 =A0 try Topdirs.dir_directory (Sys.getenv "OCAML_= TOPLEVEL_PATH")
> =A0 =A0 =A0 =A0 =A0 with Not_found -> ()
> =A0 =A0 =A0 =A0 ;;
>
> =A0 =A0 =A0 =A0 #use "topfind";;
> =A0 =A0 =A0 =A0 #camlp4o;;
> =A0 =A0 =A0 =A0 #require " sexplib.syntax";;
>
> =A0 =A0 =A0 =A0 open Sexplib.Std;;
>
> =A0 =A0 =A0 =A0 type t =3D int with sexp;;
>
> =A0 =A0 =A0 =A0 [pbil:~ 18:58]$ocaml rien.ml <http://rien.ml>
> =A0 =A0 =A0 =A0 File "rien.ml <http://rien.= ml>", line 12, characters 13-17:
> =A0 =A0 =A0 =A0 Error: Syntax error
>
> =A0 =A0 =A0 =A0 It seems like the sexp syntax extension is not loaded = when the
> =A0 =A0 =A0 =A0 script is evaluated. But it's not really clear to = me what going
> =A0 =A0 =A0 =A0 wrong...
>
> =A0 =A0 =A0 =A0 Cheers!
> =A0 =A0 =A0 =A0 ph.
>
>
>
> =A0 =A0 =A0 =A0 2014-07-01 18:51 GMT+02:00 David Sheets <sheets@alum.mit.edu
> =A0 =A0 =A0 =A0 <mailto:sheets@alum.mit.edu>>:
>
> =A0 =A0 =A0 =A0 =A0 =A0 On Tue, Jul 1, 2014 at 5:38 PM, Philippe Veber=
> =A0 =A0 =A0 =A0 =A0 =A0 <philippe.veber@gmail.com <mailto:philippe.veber@gmail= .com>>
> =A0 =A0 =A0 =A0 =A0 =A0 wrote:
> =A0 =A0 =A0 =A0 =A0 =A0 > Reposting this question here, just in cas= e.
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > ---------- Forwarded message ---------- > =A0 =A0 =A0 =A0 =A0 =A0 > From: Philippe Veber <philippe.veber@gmail.com<= br>
> =A0 =A0 =A0 =A0 =A0 =A0 <mailto:philippe.veber@gmail.com>><= br> > =A0 =A0 =A0 =A0 =A0 =A0 > Date: 2014-06-28 21:32 GMT+02:00
> =A0 =A0 =A0 =A0 =A0 =A0 > Subject: Toplevel and syntax extension. > =A0 =A0 =A0 =A0 =A0 =A0 > To: ocaml_beginners@yahoogroups.com
> =A0 =A0 =A0 =A0 =A0 =A0 <mailto:ocaml_beginners@yahoogroups.co= m>
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > Dear camlers,
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > Consider the following script:
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > #use "topfind";;
> =A0 =A0 =A0 =A0 =A0 =A0 > #camlp4o;;
> =A0 =A0 =A0 =A0 =A0 =A0 > #require "sexplib.syntax";;
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > open Sexplib.Std;;
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > type t =3D int with sexp;;
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > Saved as script.ml <http://script.ml>, the simple call:
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > ocaml script.ml <http://script.ml>
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > fails while the call:
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > cat script.ml <http://script.ml> | ocaml
> =A0 =A0 =A0 =A0 =A0 =A0 >
> =A0 =A0 =A0 =A0 =A0 =A0 > succeeds. Any idea how I could fix the fi= rst call?
>
> =A0 =A0 =A0 =A0 =A0 =A0 How does the first call fail? A difference bet= ween the two
> =A0 =A0 =A0 =A0 =A0 =A0 is that, in
> =A0 =A0 =A0 =A0 =A0 =A0 the second, the .ocamlinit file is used. If yo= u are using
> =A0 =A0 =A0 =A0 =A0 =A0 opam with
> =A0 =A0 =A0 =A0 =A0 =A0 ocamlfind installed via it, this file will con= tain your
> =A0 =A0 =A0 =A0 =A0 =A0 Topdirs setup.
> =A0 =A0 =A0 =A0 =A0 =A0 You can try:
>
> =A0 =A0 =A0 =A0 =A0 =A0 let () =3D
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 try Topdirs.dir_directory (Sys.getenv &quo= t;OCAML_TOPLEVEL_PATH")
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 with Not_found -> ()
> =A0 =A0 =A0 =A0 =A0 =A0 ;;
>
> =A0 =A0 =A0 =A0 =A0 =A0 at the top of your script (after hashbang but = before
> =A0 =A0 =A0 =A0 =A0 =A0 directives).
>
> =A0 =A0 =A0 =A0 =A0 =A0 Hope this helps,
>
> =A0 =A0 =A0 =A0 =A0 =A0 David
>
>
>
>
>
> =A0 =A0 --
> =A0 =A0 Fabrice LE FESSANT
> =A0 =A0 Chercheur en Informatique
> =A0 =A0 INRIA Paris Rocquencourt -- OCamlPro
> =A0 =A0 Programming Languages and Distributed Systems
>
>




--001a11c2497858167a04fd43f541--