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

百般思念 提交于 2019-12-10 20:45:03

问题


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.


回答1:


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..




回答2:


I'd go about it by defining a record:

record Integer (A : Set) : Set where
  constructor integer
  field
    z : A
    succ : A -> A
    pred : A -> A
    spInv : (x : A) -> succ (pred x) == x
    psInv : (x : A) -> pred (succ x) == x

This record can be used as a proof that a certain type A behaves like an Integer should.



来源:https://stackoverflow.com/questions/26600007/in-agda-is-it-possible-to-define-a-datatype-that-has-equations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!