How to use camlp-streams?

I am trying a few things with Streams, and getting warnings that this library is deprecated and I should use camlp-streams instead.

I have camlp-streams installed in my current switch with opam.
I have found that I can use it in utop by starting with

$ utop -require camlp-streams

But how do I use it in my source file when running it as a script with ocaml, or compiling with ocamlc?

For ocaml:

$ ocaml
# #use "topfind";;
# #require "camlp-streams";;

For ocamlc:

$ ocamlfind ocamlc -package camlp-streams -linkpkg foo.ml

Cheers,
Nicolas

1 Like

I thought I’d better ask: you’re aware that Stream was implemented originally as the runtime component of Camlp4/5’s stream-parsers/printers, yes? Via Camlp5, there is syntax support for this. I’ve never written code against Stream directly, though I suppose it’s doable.

2 Likes

nojb: Both of those work, although, if I put the two directives for ocaml in my source file, then I get a bogus error message from tuareg from the line #use "topfind";;; and the two directives produce error messages when I run the ocamlfind ocamlc or just ocamlc command. It seems odd to me that different versions of the source file are needed depending on whether it is interpreted (ocaml) or compiled (ocamlc).

Anyway, thanks!

Chet_Murthy: No, I was completely unaware of that. I was just reading the Streams tutorial and trying a few things out.

I understand that Camlp5 is a preprocessor-pretty-printer for OCaml and I can learn more about it, but that will probably go on my long-term to-do list, since at the moment it looks like overkill for what I am trying to do.

Well, unless you want to use the syntax support for stream-parsers (which I think is incredibly useful and valuable), Camlp5 might be too big of a jump. Here’s why: OCaml has PPX extensions as its macro-system. If you want to use Camlp5, you cannot use the standard versions of PPX extensions, but rather, Camlp5 has implemented its own. I believe that Camlp5’s versions are better, but … certainly if you want to try to stick to the “standard thing”, whether they’re better or not is strictly immaterial: they’re different and that’s already bad.

So unless it’s just about experimenting, Camlp5 is a big, big jump. I’m the maintainer of Camlp5. I obviously want people to use it. But I would be a bad person if I didn’t note these problems that would dissuade most people from using it.

It is what it is.

P.S. I took a quick look at that tutorial. It’s OK, I guess. But geez, the syntax-extensions make all that stuff so … effortless. It’s so sad seeing how ugly it looks without syntax support.

1 Like

If you have been looking at the now-deprecated Stream module in the ocaml standard library as an abstraction for implementing streams in your code, and don’t want to go down the camlp5 route, the standard library module that I believe you are recommended to use instead is the Seq module, which has a functional interface. Comparing Stream.from on the one hand with Seq.unfold and Seq.iterate on the other should give you the flavour of how to generate and use such streams.

1 Like

When I put these or similar directives at the top of my script file, for example
(no longer trying to use camlp-streams):

#use "topfind";;
#require "owl";;
  
#require "owl-plplot";;

I get bogus “Syntax error” messages from merlin. Is there a way to fix this?

The #... directives are only valid in the toplevel and are not understood by Merlin which works based on the content of Dune files.

Instead you should set up a Dune build and add the dependencies there, eg

(executable
 (name ...)
 (libraries owl ...))

Cheers,
Nicolas

1 Like