module Pairs = struct
type 'a pair = 'a * 'a
let pair (a,b) = (a,b)
let first (a,_) = a
let second (_,b) = b
end;;
type 'a quad = 'a Pairs.pair Pairs.pair;;
I am confused about the last line, and the fact that Pairs.pair is both a function and a type.
Yes, I know that values and types have different namespaces. I still don’t get what the right hand side of the line does.
I am a beginner, so I am used to type definitions like type 'a some_type = 'a * 'a , where the star “*” is used to make a larger tuple. If I do in utop type 'a quad = 'a Pairs.pair * 'a Pairs.pair;; , the code compiles, but I guess it’s doing a pair of two pairs, and I believe the intention of the quad data structure above was to implement a 4-tuple (not a tuple of tuples).
The last paragraph may have complicated the question, which is simply “what does the last line mean?”
First of all thank you for your answer. I deepened my insight.
I still don’t feel confident with this particular line of code.
To illustrate, what you provided as an example (type 'a arbitrary_name = 'a list ) is very straightforward and I like it: whenever there is a list, I can consider as an arbitrary_name object. This is easy for me to visualise.
I’m still having a hard time to picture the right hand side of:
type 'a quad = 'a Pairs.pair Pairs.pair;;
Questions:
Is the first Pairs.pair the function (let pair (a,b) = (a,b) ), or the type (type 'a pair = 'a * 'a)?
Is the second Pairs.pair the function (let pair (a,b) = (a,b) ), or the type (type 'a pair = 'a * 'a)?
In the toy-examples I’ve seen thus far, when a type is defined the right hand side of the definition contains only types (like type 'a some_type = 'a * 'a ). Values and types have different namespaces. Is it anyway possible to include values (like functions) in the type definition?
I think I am finally getting it. Just one last (dumb) question.
Consider ('a list) list) . If both are types, in other words not the case where the first is a function/constructor and the second a type argument, how does OCaml establish the relation between the 2: a list of lists?
To be more precise list is a type constructor: it is a function from types to types.
In other words, 'a list is applying the type constructor list to 'a, resulting in the type of list of elements of type 'a. And 'a list list is applying the type constructor list to the type 'a list returning the type of list of lists of elements of type 'a.