2
votes

I am new to OCaml. I saw code like

let main_t = Term.(pure main $ address $ port $ pid_file $ log_file $ dbConf)

what does '$' symbol mean?

3
Not syntax, its a function from cmdliner, if you have merlin then you can check the type of it easily. - Edgar Aroutiounian

3 Answers

4
votes

There's no predefined meaning in OCaml for $. It can be defined as an infix operator; the meaning must come from a library that you're using.

If I had to guess, I'd say that $ has been defined as a low precedence function application operator. It is used for this in Haskell, and it is often quite handy.

2
votes

In OCaml it is possible to define your own infix and prefix operators. In cmdliner library the operator $ is defined as:

  val ( $ ) : ('a -> 'b) t -> 'a t -> 'b t
  (** [f $ v] is a term that evaluates to the result of applying
      the evaluation of [v] to the one of [f]. *)

And is actually an infix form of the apply function (named app in Cmdliner). It is used to accumulate the arguments. Basically, a construct of the form pure f $a $b $c $d accepts a function f that takes four arguments of type a, b, c and d, given, that a is a value of type a Term.t, b is a value of type b Term.t, etc. In general this is a pattern for building typesafe variadic functions. For more information, about the pattern read the Applicative Programming with Effects paper.

1
votes

there is no special meaning of $ in standard ocaml. In your case, this is coming from Term module where specific syntax may be defined. (BTW, which module is it ? - I mean how did you install it)