Reputation: 2609
(* Data type definitions *)
type t =
| True
| False
| If of t * t * t
| Zero
| Succ of t
| Pred of t
| IsZero of t
In If of t * t * t
, why use '*' to connect, and how to understand?
Upvotes: 1
Views: 69
Reputation: 70397
The If
constructor for your t
type takes three arguments. In, say, Java, we would write it as a constructor like so
If(T a, T b, T c) { ... }
Now, we can think of a function which takes three arguments, conceptually, as a function which takes a tuple containing three things. A tuple is a sort of mathematical object which represents the "most general way" to have an ordered collection of a fixed size. So a tuple of three integers is the "most general way" (for some definition of "most", "general", and "way") to have three integers in a single data structure.
Now, category theorists call this "most general way" thing a product, in the same way we say the product of 3 and 5 is 15. You can read up on why we call it a product and what nice properties it has on Wikipedia if you like, but the basic idea is: tuples satisfy a lot of the "usual" mathematical properties of multiplication like commutativity, associativity, and distributivity over addition. So, to match this mathematical convention, OCaml uses *
(the same symbol we use to multiply numbers) to indicate the "product" of types. Hence,
t * t * t
is a collection of three t
values packed into a tuple in order. In Haskell, we would simply write this as (T, T, T)
. If you're familiar with Typescript, we would write it as [T, T, T]
(tuples in Typescript are just lists where we happen to know the length and types of elements in advance). Java doesn't really have an equivalent notion to this.
Upvotes: 2