9

I have seen some source code having

let rec parse_document = parser
    | [< len = parse_int32; st; >] ->
      parse_list [] (ES.take_int32 len st)
    | [< >] -> malformed "parse_document"

Can I know what is [< >] inside? it is too hard to google about this kind of signs.

Dave Newman
  • 1,018
  • 10
  • 15
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

4 Answers4

7

This is a syntactic sugar for the Stream datatype. Its manipulation is described in detail in this chapter of the book Developping Applications with OCaml.

The syntactic sugar is not built-in in the compiler, it needs to be preprocessed by the Camlp4 preprocessor. To do that, you have to add -pp camlp4o to your compilation command line.

gasche
  • 31,259
  • 3
  • 78
  • 100
  • what's `>>` inside `let encode_to_string = encode_to_buffer >> Buffer.contents` – Jackson Tale Apr 22 '13 at 15:52
  • It's a user-defined infix operator: `let (>>) f g = ...`. You have to check the definition, but from the names it looks like reverse function composition: `let (>>) f g = fun x -> g (f x)`. – gasche Apr 22 '13 at 16:45
2

This is a stream. It is used mainly to create parsers. But streams have been removed from OCaml and are now provided as a camlp4 extension.

Thomash
  • 6,339
  • 1
  • 30
  • 50
2

It is part of the Stream parsing syntax extension, and it means the empty stream.

Török Edwin
  • 561
  • 2
  • 5
1

That's the literal syntax for streams. A stream is just like a list except that only one element is available at a time and you remove the first element by reading it.

It seems primarily used for parser code. Parsers--declared with the parser keyword as in your example--are the functions that can "consume" elements of the stream.

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214