What does `[< >]` mean in OCaml?
Asked Answered
M

4

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.

Markswoman answered 22/4, 2013 at 14:54 Comment(0)
B
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.

Bristletail answered 22/4, 2013 at 15:5 Comment(2)
what's >> inside let encode_to_string = encode_to_buffer >> Buffer.contentsMarkswoman
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).Bristletail
O
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.

Osi answered 22/4, 2013 at 15:3 Comment(0)
C
2

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

Chartulary answered 22/4, 2013 at 15:4 Comment(0)
A
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.

Addictive answered 22/4, 2013 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.