Search code examples
agdaalgebraic-data-types

In Agda is it possible to define a datatype that has equations?


I want to describe the integers:

data Integer : Set where
    Z : Integer
    Succ : Integer -> Integer
    Pred : Integer -> Integer
    ?? what else

The above does not define the Integers. We need Succ (Pred x) = x and Pred (Succ x) = x. However,

    spReduce : (m : Integer) -> Succ (Pred m) = m
    psReduce : (m : Integer) -> Pred (Succ m) = m

Can't be added to the data type. A better definition of the integers is most certainly,

data Integers : Set where
    Pos : Nat -> Integers
    Neg : Nat -> Integers

But I am curious if there is a way to add equations to a datatype.


Solution

  • It seems that what you'd like to do is define your Integers type as a quotient type by the equivalence relation that identifies Succ (Pred m) with m, etc. Agda doesn't support that anymore -- there was an experimental library that tried to do that (by forcing all functions over a quotient type to be defined via a helper function that requires proof of representational invariance), but then someone discovered that the implementation wasn't watertight enough and so could lead to inconsistencies (basically by accessing one of its postulates that was supposed to be inaccessible from the outside), for the details you can see this message:

    We were not sure if this hack was sound or not. Now, thanks to Dan Doel, I know that it isn't.

    [...]

    Given these observations it is easy to prove that the postulate above is unsound:

    I think your best bet at the moment (if you want to/need to stick to a loose representation with an equivalency to tighten it up) is to define a Setoid for your type..