cs3110 / textbook

The CS 3110 Textbook, "OCaml Programming: Correct + Efficient + Beautiful"
Other
740 stars 134 forks source link

Issue with tupled constructors on Algebraic Data Types page #104

Closed willbarkoff closed 2 years ago

willbarkoff commented 2 years ago

The ADT page refers to several constructors created with the syntax Constructor of type1 * type2 as being a type1, type2 pair. The tuple page says that a pair is another word for a 2-tuple. However, Constructor of type1 * type2 (a constructor with two elements) is different from Constructor of (type1 * type2) (a constructor with a single tuple element).

This can be demonstrated pretty easily. This snippet won't compile:

type thing_type = Thing of int * int;;
let mypair = (1, 2);;
Thing mypair;;
(* => Error: The constructor Thing expects 2 argument(s), but is applied here to 1 argument(s) *)

But this one will:

type thing_type = Thing of (int * int);;
let mypair = (1, 2);;
Thing mypair;;
(* => - : thing_type = Thing (1, 2) *)

I think that the language on this page could be clarified to explain that Thing of int * int and Thing of (int * int) are actually different.

See also: https://ocaml.org/docs/data-types#a-note-on-tupled-constructors

clarksmr commented 2 years ago

I'm going to finesse this to avoid explaining that difference (which I've also always avoided explaining in class).