Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml type declaration and capital letters

Using ocamlc, in a windows environment :

type splitter = {varname:string; count:int; mean:float}

worked fine when I was compiling the source but :

type splitter = {Varname:string; count:int; mean:float}

caused the compiler to complain (without giving much information)

File "splitter.ml", line 1, characters 17-24:
Error: Syntax error

In the mean time, the second line was working perfectly with OCaml. Is there a good reason for this behavior ? I did not find anything online.

like image 859
RUser4512 Avatar asked Jan 06 '23 10:01

RUser4512


2 Answers

Traditionally FP languages have restricted the use of capitalized identifiers to certain cases. This makes the code more readable, and in effect enforces a certain coding style. It also makes parsing easier (I would assume), given that FP languages traditionally have not only a rich expression sublanguage but also a rich type sublanguage.

So, you can't use a capitalized name for the field of a record in OCaml.

To find this in the documentation, you can start with the definition of record syntax in Section 6.8, then observe that a field-name is a lowercase-ident in Section 6.3.

like image 135
Jeffrey Scofield Avatar answered Jan 21 '23 23:01

Jeffrey Scofield


There is a reason. OCaml uses a number of distinct namespaces, but in some cases two of them need to be distinguishable. This is then done by one of them being purely uppercase and the other purely lowercase. In the case at hand, consider the expressions a.b.c and a.B.c. The former is a field c of a record which in turn is a field of record a. The latter is a field c of record a, but the (respective record type definition and) field name c comes from module B. If OCaml allowed to use mixed case record names, the two expressions could not be syntactically distinguished, so the dot operator could not be used for both, record fields and module parts. Then, two distinct operators would be needed, making the syntax more cumbersome.

like image 20
guest Avatar answered Jan 21 '23 23:01

guest