9

I am having trouble understanding the principles of what a constructor is and how it works.

For example, in Coq, we have been taught to define the natural numbers like this:

Inductive nat : Type :=
   | O : nat
   | S : nat -> nat.

And have been told that S is a constructor, but what exactly does that mean?

If I then do:

Check (S (S (S (S O)))).

I get that it is 4 and of type nat.

How does this work, and how does Coq know that (S (S (S (S O)))) represents 4?

I guess the answer to this is some extremely clever background magic in Coq.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Jerome
  • 275
  • 2
  • 7
  • 4
    The pretty-printing of `(S (S (S (S O))))` as `4` is just a convenience that you should not let distract you. If you are at the point where you want to get a feeling of what a constructor is, define your own type `naturals` with constructors `Z` (for zero) and `N` (for next). Then nothing magic will happen. – Pascal Cuoq Feb 23 '11 at 12:05

2 Answers2

7
Inductive naturals : Type :=
   | Z : naturals
   | N : naturals -> naturals.

says the following things:

  1. Z is a term of type naturals

  2. when e is a term of type naturals, N e is a term of type naturals.

  3. Applying Z or N are the only two ways to create a natural. When given an arbitrary natural, you know that it was either made from one or from the other.

  4. two terms e1 and e2 of type naturals are equal if and only if they are both Z or if they are respectively N t1 and N t2 with t1 equal to t2.

You can see how these rules would generalize to arbitrary inductive type definitions. In general, in an arbitrary inductive type definition for the type t:

  • applying a constructor to arguments of the correct types produces a term of type t;
  • any term of type t is the result of applying one of the constructors that were associated with the type t when it was defined; in other words, given a term of type t, one can assume that it is the result of applying one of the constructors of t;
  • two terms of type t are equal only if they result from applying the same constructors to the same arguments.

(Side note: when the type definition is for constructors of the shape of Z and N, these properties correspond more or less exactly Peano's axioms for natural numbers. This is why a type named nat is pre-defined in Coq with constructors of these shapes to be used to represent natural numbers. This type receives special treatment because it gets tiring very quickly to manipulate the raw form, but the special treatments are only there for convenience.)

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • 1
    In bullet 2, did you mean to say that N e is a term of type naturals? – mushroom Nov 21 '12 at 00:42
  • @mushroom Yes, I did. – Pascal Cuoq Aug 02 '13 at 05:53
  • 1
    your answer is good at explaining the example but doesn't actually address the question title of "what is a constructor". To me a constructor is a function that give the right arguments returns an actual object/value of some type. That's how I would explain what a constructor. – Charlie Parker Dec 25 '18 at 00:35
  • @CharlieParker True. I tried to re-center the answer on that. – Pascal Cuoq Dec 25 '18 at 09:52
  • 1
    @PascalCuoq though is my description correct? I want to know the answer to the question. – Charlie Parker Dec 26 '18 at 01:13
  • @CharlieParker It is the definition of “function to type t1 -> t2” that “give[n] the right arguments [of type t1, it] returns an actual object/value of […] type [t2]”. Your proposal for defining a constructor can be summarized as “a function that is a function”. It's not a very good definition for a constructor, it omits two important things, which are listed as items two and three in my answer. – Pascal Cuoq Dec 27 '18 at 09:23
  • @CharlieParker One thing that's strange about this question, asked in the context of the proof assistant Coq, is that you cannot write the beginning of your first proof in Coq if you do not understand what a constructor is in that context. So perhaps I need to emphasize that while you claimed that the title of the question was “what is a constructor”, the title is actually “What is a constructor in Coq?”, and that if you have a question about what people call a constructor in other programming languages, you should perhaps find another SO question that's similar to that, or ask it yourself. – Pascal Cuoq Dec 27 '18 at 09:31
  • 1
    I don't care if my suggestion is wrong or incomplete. I am looking for a complete answer/definition of constructor in the context of Coq. Nothing more, nothing less. – Charlie Parker Dec 28 '18 at 00:30
  • @CharlieParker This is what my answer covers as far as I know, but I don't think SO comments are intended for this sort of confirmation. If you find a better answer, post it alongside this one. – Pascal Cuoq Dec 28 '18 at 07:54
0

In type theory, a (type) constructor is just a way to construct new types from existing ones ( http://en.wikipedia.org/wiki/Type_constructor ).

In your inductive definition of nat, S is a constructor because (if you look at the signature ) it takes a nat and produces another nat.

For example, a type constructor for a pair of nats will be:

Inductive pair : Type := P: nat->nat->pair.

Check P (S (O)) (S(S(O))).
GClaramunt
  • 3,148
  • 1
  • 21
  • 35
  • 3
    This answer is a somewhat misleading: the fact that `S` is a constructor is not determined by its type. For example `plus 1` is not a constructor (even though it has the same type as `S` and even eta-reduces to it). `S` is a constructor because it's one of the “canonical” ways of building a `nat`. It's important that the constructors of a type are the only ways to build objects of that type. – Gilles 'SO- stop being evil' Mar 04 '11 at 22:34
  • Yes, is worth clarifying that I was talking in the context of an (inductive) type definition. A plain function is not a type constructor even if it shares the same type signature. (if that's what you're talking about) – GClaramunt Mar 05 '11 at 03:31
  • I may have mixed the names, I think they're called value constructor – GClaramunt May 31 '11 at 20:06