1

I have a function f that takes a x of fintype A and a proof of P x to return an element of fintype B. I want to return the finset of f x for all x satisfying P, which could be written like this :

From mathcomp
Require Import ssreflect ssrbool fintype finset.

Variable A B : finType.
Variable P : pred A.
Variable f : forall x : A, P x -> B.

Definition myset := [set (@f x _) | x : A & P x].

However, this fails as Coq does not fill the placeholder with the information on the right, and I do not know how to provide it explicitly.

I did not find an indication on how to do that, both in the ssreflect code and book. I realize that I could probably do it by using a sigma-type {x : A ; P x} and modifying a bit f, but it feels more convoluted than it should. Is there a simple / readable way to do that ?

1 Answers1

2

Actually the easiest way to make things work in the way you propose is to use a sigma type:

Definition myset := [set f (tagged x) | x : { x : A | P x }].

But indeed, f is a bit of a strange function, I guess we'll need to know more details about your use case to understand where are you going.

ejgallego
  • 6,709
  • 1
  • 14
  • 29
  • I'm working with trees which have unicity over branches. My function f actually takes an element x of type tau, a list tl of "uniq trees" over type tau and a proof that x does not appear in any of the trees (which is P x) to build the tree with x as a root and tl as descendents. Having to provide a proof to build a term feels a bit weird to be honest, maybe I'm missing some good practice here. – Pierre-Léo Bégay May 02 '19 at 08:34
  • I suggest you use the subType infrastructure to work with trees that hold the invariant; that's pretty comfortable to use as it will coerce to the base type when you need it. – ejgallego May 02 '19 at 10:17
  • I'm not sure I follow: I need the return type of f to be a fintype in order to use a finset (which is required later-on), and trees without the unicity invariant are obviously not a fintype. However, since it is decidable to check whether an element already belongs to a tree, a workaround I started working on was to modify f to return an "option (uniq tree)". I then wanted to use an equivalent of pmap for finsets to filtter out all the none results, but my quick glance at the code was unfruitful. Would you know about something like that? – Pierre-Léo Bégay May 02 '19 at 10:51
  • I think I managed to do it via pmap: Definition pimset {A B : finType} (f : A -> option B) (s : {set A}) : {set B} := [set x in (pmap f [seq y | y in s])]. However, it seems a little hacky to me. – Pierre-Léo Bégay May 02 '19 at 11:47