Inductive nat: Set := | O: nat | S: nat -> nat.
Is there a standard way to define integers (and maybe other sets like rationals and reals) constructively?
Asked By : Alex
Answered By : Gilles
Inductive Z1 := | Nonnegative : nat -> Z1 (* ⟦Nonnegative x⟧ = ⟦x⟧ *) | Negative : nat -> Z1. (* ⟦Negative x⟧ = -⟦x⟧-1 *)
However, this definition is oddly asymmetric, which can make it preferable to admit two different representations for zero:
Inductive Z2 := | Nonnegative : nat -> Z2 (* ⟦Nonnegative x⟧ = ⟦x⟧ *) | Nonpositive : nat -> Z2. (* ⟦Nonpostitive x⟧ = -⟦x⟧ *)
Or we can build the relative integers without using the naturals as a building block:
Inductive Pos3 := | I : Pos3 (* ⟦I⟧ = 1 *) | S3 : Pos3 -> Pos3 (* ⟦S3 x⟧ = ⟦x⟧+1 *) Inductive Z3 := | N3 : Pos3 -> Z3 (* ⟦N3 x⟧ = -⟦x⟧ *) | O3 : Z3 (* ⟦O3⟧ = 0 *) | P3 : Pos3 -> Z3 (* ⟦P3 x⟧ = ⟦x⟧ *)
The Coq standard library uses yet another definition: it constructs positive integers from their notation is base 2, as the digit 1 followed by a sequence of digits 0 or 1. It then constructs Z like Z3 from Pos3 above. This definition also has a unique representation for each integer. The choice of using binary notation is not for easier reasoning, but to produce more efficient code when programs are extracted from proofs. Ease of reasoning is a motivation in picking a definition, but it is never an insurmountable factor. If some construction makes a particular proof easier, one can use that definition in that particular proof, and prove that the construction is equivalent to the other construction that was chosen as the definition originally. For rational numbers, it’s difficult to escape quotients, unless we start from a representation of integers as a product of factors (which makes some fundamental operations such as addition and the total ordering on $mathbb{N}$ difficult to define). The Coq standard library defines Q as $mathbb{N} times mathbb{N}^*$ (numerator and denominator), and defines an operator =?= to test the equivalence of two elements of Q. This definition is pretty common because that’s as simple as it gets. Real numbers are a whole different kettle of fish because they are not constructible. It is impossible to define the real numbers as an inductive type (all inductive types are denumerable). Instead, any definition of the real numbers has to be axiomatic, i.e. non-constructive. It is possible to construct denumerable subsets of the real numbers; the way to do that depends on what subset you want to construct.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/40479 Ask a Question Download Related Notes/Documents