Suppose we'd like to represent (signed) integers as the Grothendieck group over naturals (or, in other words, as a pair (m, n)
, where the integer that's understood is m - n
):
data ZTy : Type where
MkZ : (m, n : Nat) -> ZTy
Now the (structural) equality that the language gives us for free is no longer what we want: instead, we only care about a certain equivalence relation (namely, (m1, n1) ~ (m2, n2)
iff m1 + n2 = m2 + n1
). No biggie, let's write this down!
data Equiv : ZTy -> ZTy -> Type where
MkEquiv : (prf : m1 + n2 = m2 + n1) -> Equiv (MkZ m1 n1) (MkZ m2 n2)
But working with this gets messy really quickly. Any argument of type prop Const
(for prop : ZTy -> Type
) is replaced with (k : ZTy) -> (k `EqZ` Const) -> prop k
, to say the least (as a more applied example, I'm struggling with writing down the two-sided induction proof for this type, and I'm still not sure I got even the signature of that term right).
Moreover, a function like replaceZ : {P : ZTy -> Type} -> (k1 `Equiv` k2) -> P k1 -> P k2
(obviously) does not exist, but I cannot find a better candidate. As an interesting side note/observation, if we don't export the definition of ZTy
, no client-code P
can observe it, and this function would make sense for any P
defined in any other module, but looks like we cannot internalize this within the language.
Another thing I have in mind is to limit the set of predicates to the ones that hold under equivalence relations. That is, replacing P : ZTy -> Type
with something like P : ZTy -> Type, pAdmissible : ZPred P
where ZPred
carries along a proof of its invariance under the equivalence relation:
data ZPred : Type -> Type where
MkZPred : {P : ZTy -> Type} ->
(preservesEquiv : {k1, k2 : ZTy} -> (k1 `Equiv` k2) -> P k1 -> P k2) ->
ZPred P
Anyway, what's the common approach to working with such types? Is there anything else that would work nicely?
I've also heard something about quotient types, but I don't know much.
<!-- language: ___ -->
or<!-- language-all: ___ -->
HTML comments; we just don't have a highlighter for Idris. – HTNW