Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] How can I extract data from this exists Prop?

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] How can I extract data from this exists Prop?


Chronological Thread 
  • From: "jonikelee AT gmail.com" <jonikelee AT gmail.com>
  • To: Robbert Krebbers <mailinglists AT robbertkrebbers.nl>, coq-club <coq-club AT inria.fr>
  • Subject: Re: [Coq-Club] How can I extract data from this exists Prop?
  • Date: Wed, 26 Aug 2020 17:16:18 -0400
  • Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=jonikelee AT gmail.com; spf=Pass smtp.mailfrom=jonikelee AT gmail.com; spf=None smtp.helo=postmaster AT mail-qk1-f182.google.com
  • Ironport-phdr: 9a23:Osxw6R2G4LL9kH8BsmDT+DRfVm0co7zxezQtwd8ZsesWKPnxwZ3uMQTl6Ol3ixeRBMOHsqwC0rCM+Pm6BSQp2tWoiDg6aptCVhsI2409vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6nK94iQPFRrhKAF7Ovr6GpLIj8Swyuu+54Dfbx9HiTagYL5+Ngi6oRvPusUZnIduNKk8wQbVr3VVfOhb2XlmLk+JkRbm4cew8p9j8yBOtP8k6sVNT6b0cbkmQLJBFDgpPHw768PttRnYUAuA/WAcXXkMkhpJGAfK8hf3VYrsvyTgt+p93C6aPdDqTb0xRD+v4btnRAPuhSwaMTMy7WPZhdFqjK9DrhyvpwJxzZPXbo6XOvpweazScs8VS2daQsZRTjZMDp+mYocTDecMO/tToYnnp1sJqBuzHQegBOHoyj9Oh3/23rM10+A/Hg7YxwEgENcOv27VrNXxLqsdTee1zKzGwT7eaP5W2zD96I7JchAiv/6MWax/ftTKxEkgEgPKlFSQqYj/MzyJ0eQNtnGW4ux9Xu2gl2ApsRt+oiSzxsgykInJgJoYxF/E+yllxIs4Od21RVJ6bNO5H5Vdqj+WOpd4T84+XWxlpDs2xqAHtJOnYCUG1JspyRDBZvCafYWG4hDuWfqMLTp+mXlrdrW/hxOo/kihzO3xTsi00FBQripEiNbArH4N1wbL5siCUvt9/16t2S2B1gDI8O1EJlo0laXdJpU8wbAwjoIevVrfEiLygkn7j6+bel869uS29ejreKjqq5yCO4NslA3zM7giltG6DOglNgUBQ22W9Ou92bH/4UH0RbtHgeEsnaTcv53WOcAWqrO8Dg9b3Ysj5Ri/AjKo0NsGgXYKI1dIdR2agIXtNVzDJu3zA+2ljFS2ijhrwujLPr3/DZXJKXjOiLLhcqx8605Y0QYz1NNf649NBrEPPf7+WVH9uMbXDh8+NAy0zOLnB8tn2owCXmKPB7eVMKLUsVCW+uIiO/eAaJMRtTrnKPUo5+TigWEnlVMDZ6WlwJgaZG6gEvRjOUqZYH7sgtkbEWcNuwozVO7qiFqEUT5SZHa9QaY86S8lB4+9AofDQ5qigL2F3CuhApJWYWVGBkiWEXj0b4WER+sMaCWKL8B9lTwETKGtRJMl1RGzrwD30KFnL+rR+i0Ar53vztl15+vJlREz7zN4Fcqd03veB11zy1kPTTU7xrw3glZwwF2Oy7Mw1/lRFNhS4fxNUxwmLrbGyOZwBsruWRjMdN2EUk3gRNHwRXk6Sct0yNsTaQ4pENK7yxvHwiCCArkPlrXNCoZioYzG2H2kbcR6zXfF2a0sgnEpR8JOMSutgas1v1zRAIjIkEidmquCeqEV3SqL/2CGmznd9HpEWRJ9BP2WFUsUYVHb+IygtxHyCoS2ALFiCTNvjMuLK69EcNrs1AwUS/LqOdCYaGW0yT7pWUS4g4iUZY+vQF0zmT3HARFdwQ8W9HeCcwM5A3X5+j+MPHlVDVvqJnjU36x+pXe8FBFmygiLawh41OPw9EdF37qTTPQc2r9CsyAk+W15

It turns out that no assumptions about the countability of list
element's type is necessary, nor does one have to use a decidable
equality:

Require Export Coq.Lists.List.
Import ListNotations.

Definition isPrefixOf{A} (x y : list A) :=
exists z, x ++ z = y. (*note: plain Leibniz = here*)

Definition extract : forall {A}{y x : list A}(P : isPrefixOf x y), {z | x ++
z = y}.
Proof.
intro A. induction y. all:intros x P.
- exists []. destruct P as (z & E). rewrite app_nil_r. apply app_eq_nil in
E. tauto.
- destruct x as [|b x].
{ exists (a::y). reflexivity. }
enough (a=b /\ isPrefixOf x y) as (e & Q).
{ specialize (IHy x Q) as (z & f). exists z. cbn. congruence. }
destruct P as (z & E). cbn in E. injection E. intros H1 H2.
split.
+ congruence.
+ exists z. exact H1.
Defined.



On Tue, 25 Aug 2020 16:31:10 -0400
"jonikelee AT gmail.com" <jonikelee AT gmail.com> wrote:

> Oh - sorry, it wasn't intended to be off-list. Thanks for the
> detailed explanation.
>
> On Tue, 25 Aug 2020 20:58:12 +0200
> Robbert Krebbers <mailinglists AT robbertkrebbers.nl> wrote:
>
> > Not sure if this was intended to be off-list.
> >
> > It's like that, but more complicated for the general case. Since
> > bool is finite, termination is trivial. For types are are
> > countable, not non-finite, (take p : nat → bool) you need to find a
> > suitable termination measure.
> >
> > On 8/25/20 8:54 PM, jonikelee AT gmail.com wrote:
> > > On Tue, 25 Aug 2020 20:37:47 +0200
> > > Robbert Krebbers <mailinglists AT robbertkrebbers.nl> wrote:
> > >
> > >> The mentioned proof makes use of the following theorem:
> > >>
> > >> (∃ x : A, P x) → { x : A | P x }
> > >>
> > >> under the assumption that 1.) A is countable, and 2.) the
> > >> predicate is decidable.
> > >
> > > I see. I missed that there was a double "==" in isPrefixOf. Ok,
> > > so this is just something like:
> > >
> > > Definition extract{P : bool -> bool}(e : exists b, P b = true) :
> > > {b | P b = true}. Proof.
> > > assert bool as b by constructor.
> > > destruct (P b) eqn:E.
> > > - exists b. exact E.
> > > - destruct (P (negb b)) eqn:E2.
> > > + exists (negb b). exact E2.
> > > + exfalso. destruct e as (b' & F), b, b'.
> > > all:cbn in *. all:congruence.
> > > Defined.
> > >
> > > OK - not as magical as I thought. Thanks.
> > >
> > >>
> > >> This theorem holds intuitively because you can write an algorithm
> > >> that searches for the witness. Because A is countable, it can
> > >> simply enumerate all elements. Then, because P is decidable, it
> > >> can test for each element if P holds or not. Now, since (∃ x : A,
> > >> P x) holds, this algorithm will terminate, i.e. finally find a
> > >> witness.
> > >>
> > >> The Coq standard library also includes a version of this theorem,
> > >> see
> > >> https://coq.inria.fr/distrib/current/stdlib/Coq.Logic.ConstructiveEpsilon.html
> > >>
> > >> Also std++ includes a version of this theorem, see
> > >> https://gitlab.mpi-sws.org/iris/stdpp/-/blob/master/theories/countable.v#L84
> > >>
> > >> choiceType in ssreflect (or the Countable type class in std++)
> > >> expresses that a type is countable, and is used to automatically
> > >> infer assumption (1) in the above theorem.
> > >>
> > >> Hope this helps!
> > >>
> > >> On 8/25/20 8:27 PM, jonikelee AT gmail.com wrote:
> > >>> Can someone explain to a non-ssreflect/non-mathcomp user how
> > >>> choiceType works? It looks like it must rely on an axiom. Are
> > >>> there any types that fulfill it? For example, is there a
> > >>> non-axiomatic way in which bool is a choiceType?
> > >>>
> > >>> Because, the ability to pull the necessary x out of "exists x, P
> > >>> x" seems magical. How would a program using this extract?
> > >>>
> > >>> On Tue, 25 Aug 2020 19:15:14 +0200
> > >>> Emilio Jesús Gallego Arias <e AT x80.org> wrote:
> > >>>
> > >>>> Hi Agnishom,
> > >>>>
> > >>>> Agnishom Chattopadhyay <agnishom AT cmi.ac.in> writes:
> > >>>>
> > >>>>> I have a function that is something like this:
> > >>>>>
> > >>>>> Definition isPrefixOf (x : list A) (y : list A) :=
> > >>>>> exists z, x ++ z = y.
> > >>>>>
> > >>>>> Now, I want to define a witness function. Informally, it can
> > >>>>> be described as follows: Given x and y such that (isPrefixOf
> > >>>>> x y), return z which satisfies the criteria exists z, x ++ z
> > >>>>> = y.
> > >>>>>
> > >>>>> I would try to define it this way, but that would not work
> > >>>>> (possibly because exists z, x ++ z = y is a Prop and we are
> > >>>>> asking for something in Type) :
> > >>>>>
> > >>>>> Definition witness (x y : list A) {H : isPrefixOf x y} : list
> > >>>>> A. destruct H.
> > >>>>> Abort.
> > >>>>
> > >>>> If you can afford to work with a type A which has a notion of
> > >>>> choice principle you can indeed write your witness function.
> > >>>> Example:
> > >>>>
> > >>>> From mathcomp Require Import all_ssreflect.
> > >>>>
> > >>>> Set Implicit Arguments.
> > >>>> Unset Strict Implicit.
> > >>>> Unset Printing Implicit Defensive.
> > >>>>
> > >>>> Variable (A : choiceType).
> > >>>>
> > >>>> Definition isPrefixOf (x : list A) (y : list A) :=
> > >>>> exists z, x ++ z == y.
> > >>>>
> > >>>> Definition extract x y (w : isPrefixOf x y) : list A := xchoose
> > >>>> w.
> > >>>>
> > >>>> Kind regards,
> > >>>> Emilio
> > >>>
> > >
>




Archive powered by MHonArc 2.6.19+.

Top of Page