I'd like to define a predicate for list uniqueness and its decidability function in Coq. My first try was:
Section UNIQUE.
Variable A : Type.
Variable P : A -> Prop.
Variable PDec : forall (x : A), {P x} + {~ P x}.
Definition Unique (xs : list A) := exists! x, In x xs /\ P x.
Here I just have specified that predicate Unique xs
will hold if there's just one value x
in list xs
such that P x
holds. Now, comes the problem. When I've tried to define its Unique
decidability:
Definition Unique_dec : forall xs, {Unique xs} + {~ Unique xs}.
induction xs ; unfold Unique in *.
+
right ; intro ; unfold unique in * ; simpl in * ; crush.
+
destruct IHxs ; destruct (PDec a).
destruct e as [y [Hiy HPy]].
...
I've got the following nasty error message:
Error: Case analysis on sort Set is not allowed for inductive definition ex.
I've googled this message and seen several similar problems in different contexts. At least to me, it seems that such problem is related to some restrictions on Coq pattern matching, right?
Now that the problem is settled, my questions:
1) All I want is to define a decidability for a uniqueness test based on a decidable predicate. In the standard library, there are similar tests for existencial and universal quantifiers. Both can be defined as inductive predicates. Is there a way to define "exists unique" as an inductive predicate on lists?
2) It is possible to define such predicate in order to it match the standard logic meaning of exists unique? Like exists! x, P x = exists x, P x /\ forall y, P y -> x = y?
In
and use theseq
library from math-comp, whereFixpoint uniq s := if s is x :: s' then (x \notin s') && uniq s' else true.
– ejgallegox
occur in exactly one position of the listxs
. – Arthur Azevedo De AmorimInductive Unique : list A -> Prop := | Unique_base_case : forall x xs, P x -> Forall (fun y => ~ P y) xs -> Unique (x :: xs) | Unique_inductive_case : forall x xs, ~ P x -> Unique xs -> Unique (x :: xs).
– Daniel Scheplerforall x xs, P x -> Forall (fun y => P y -> y = x) xs -> Unique (x :: xs)
. – Daniel Schepleruniq
definition [for decidable eqtypes that is] – ejgallego