Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] A question of encoding...

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] A question of encoding...


Chronological Thread 
  • From: Fred Smith <fsmith1024 AT gmail.com>
  • To: fdhzs2010 AT hotmail.com
  • Cc: coq-club AT inria.fr
  • Subject: Re: [Coq-Club] A question of encoding...
  • Date: Sun, 2 Dec 2018 17:44:10 -0500
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=fsmith1024 AT gmail.com; spf=Pass smtp.mailfrom=fsmith1024 AT gmail.com; spf=None smtp.helo=postmaster AT mail-ot1-f54.google.com
  • Ironport-phdr: 9a23:v0l5ARczh5mAFDKEYFDcTFoclGMj4u6mDksu8pMizoh2WeGdxcqzZR7h7PlgxGXEQZ/co6odzbaO4+a4ASQp2tWoiDg6aptCVhsI2409vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6nK94iQPFRrhKAF7Ovr6GpLIj8Swyuu+54Dfbx9HiTahYr5+Ngm6oRnMvcQKnIVuLbo8xAHUqXVSYeRWwm1oJVOXnxni48q74YBu/SdNtf8/7sBMSar1cbg2QrxeFzQmLns65Nb3uhnZTAuA/WUTX2MLmRdVGQfF7RX6XpDssivms+d2xSeXMdHqQb0yRD+v6bpgRh31hycdLzM2/2/Xhc5wgqxVoxyvugJxzJLPbY6PKPZzZLnQcc8GSWdDWMtaSixPApm7b4sKF+cPPfxYoJfgqFATsBW+AAisBP/vyjRVgXL207Y60+EgEQHa3AwsAs4CvGrbodjuO6cSVPq6zKjMzTnZc/xW3jL95ZHOfxs8ov+MRap9fdTNxUQrDQ/IjVWdpZb7Mz+IyugBqWiW4uVmWOmykWAosRtxrSKqxso0ionGmIYVylfc+CV82ok1JNm4RFd8Yd64DZdcri+aOot5T884TGFovyE6yrICuZGlZiQF1JMnxxvHZ/yGdYiH/A7jWf6PLTtkgH9pYrGyihao/US+1+HwStO43VZFoyZdl9nDrHEN1xjd6sidTft9+1+s2TiU1wDW6+FEPUA0mbfHJ5I/zbM9jZUTsUHZES/3nEX6lrOZdkIh+uSw8eTofq3mpoOAN49zkgzxLqMumtWmDeskNggOQnOU9P+n1Lzj+E35WK9Fguc3kqnfqpDaJN4UqrS3Aw9Pgc4f7EOxAze0y45AxCEvLFVZfRuGi87iPFSdDur/CKKRmVmg2AxuxuzGdunvGZDMMniayO29Vbl44k9YjgE0yIYMtNpvFrgdLaerCQfKv9vCA0phal3m86PcENx4k7gmdyeKC66dPrnVtAbRtO0qKuiIIoQSvWSkcqR317vVlXY83GQlU+yxx5JOMSK3G/1nJwOSZn++2o5cQ1dPhRI3SanRsHPHUTNXYCzvDac15zV+D57/SImeGsaih7uO2Cr9FZpTNDhL


If I am not mistaken, that is exactly my definition pathS?  It has the disadvantage as data that the path doesn't have a normal form; so strict equality (=) doesn't correspond directly to the desired semantic equality.  

One can always normalize before comparing, but it is exactly this kind of subtle tradeoff that puzzles me.  Is it better to make the relation construction simple, but give up equality; or make the relation have a normal form by definition (pathI) and then write all kinds of convenience utilities to make this convenient.  Both choices seem equally bad.  I am concerned that in a few weeks I'll discover that one or the other is better for some very specific reason and end up having both versions with translations between them.  

-Fred

On Sun, Dec 2, 2018 at 5:34 PM Jason -Zhong Sheng- Hu <fdhzs2010 AT hotmail.com> wrote:
Hi Fred,

yes, transitivity closure in Coq's standard library is defined to be a proposition. But as I said, it doesn't prevent you from re-defining the counter-part definition in Type, which gives you the full right to treat it as data and consequently extract a sequence of vertices along the path.

Sincerely Yours,

Jason Hu

From: Fred Smith <fsmith1024 AT gmail.com>
Sent: December 2, 2018 5:29 PM
To: fdhzs2010 AT hotmail.com
Cc: coq-club AT inria.fr
Subject: Re: [Coq-Club] A question of encoding...
 
Hi Jason,

Thanks for the suggestion.  Unfortunately, I need the actual paths as data and not just the relation between endpoints of paths.  If I understand your proposal correctly it would give me a relation (path v1 v2) which holds whenever there exists a path between v1 and v2 without providing the actual path.

-Fred

On Sun, Dec 2, 2018 at 3:50 PM Jason -Zhong Sheng- Hu <fdhzs2010 AT hotmail.com> wrote:
Hi Fred,

I would try to avoid mentioning equality here. You need that because you try to model an edge as a pair. Instead, I would model an edge to be a binary relation and a path to be its transitivity closure:

Require Import Relations.

Section Graph.  
  Variable V : Type.          (* type for vertices *)
  Variable edge : relation V. (* a predicate if an edge exists between two verices. *)

  Definition path : relation V := clos_trans V edge.
End Graph.

It's close to your second choice but your second choice will probably lead to some additional tedious proof steps, because pair in Coq doesn't have definitional eta expansion.

If you need to inspect a path as data, I suppose you want to redefine transitivity closure to be Set/Type. One advantage of defining data, is it gives a structure for induction.

Sincerely Yours,

Jason Hu

From: coq-club-request AT inria.fr <coq-club-request AT inria.fr> on behalf of Fred Smith <fsmith1024 AT gmail.com>
Sent: December 2, 2018 3:25 PM
To: coq-club AT inria.fr
Subject: [Coq-Club] A question of encoding...
 
Hi,

I am looking for engineering advice to help me understand the tradeoffs between different ways of encoding the same information in Coq.

As my exercise, I am trying to encode paths in a graph.  For simplicity these are just lists of edges where each edge ends where the next edge begins.

Below is a Coq file that uses 3 different encodings.  Two inductive ones, and the third as a predicate.  What are the pros and cons of these choices?

Thanks,

Fred

----- path.v below -----

(* Experiments in encoding paths in Coq.

A path is simply a list of edges where the destination of the
preceding edge is the source of the next edge.

*)

Require Import List.

Definition vertex := nat.
Definition edge := (vertex * vertex)%type.

(* Simple encoding as lists with a predicate *)
Fixpoint path (l : list edge) :=
  match l with
  | nil => False
  | hd :: tl => (match tl with
                 | nil => True
                 | hd2 :: tl2 => (snd hd) = (fst hd2)
                 end) /\ (path tl)
  end.

(* Define as a subset. *)
Definition Path := { l : list edge | path l }.

(* Encode as an inductive definition that makes the path correct by
construction.  Note, need to include the vertex endpoints in the type
to enforce the invariants. *)
Inductive pathI : vertex -> vertex -> Set :=
| path_tl : forall e:edge, pathI (fst e) (snd e)
| path_hd : forall e:edge, forall v1 v2:vertex, forall p:pathI v1 v2, (snd e) = v1 -> pathI (fst e) v2.

(* We could write a symmetrical encoding as well. *)
Inductive pathS : vertex -> vertex -> Set :=
| pathS_edge : forall e:edge, pathS (fst e) (snd e)
| pathS_compose : forall v1 v2 v3 : vertex, forall p1 : pathS v1 v2, forall p2 : pathS v2 v3, pathS v1 v3.

                                    
(* Which of these encoding is preferred?
  1. Predicate encoding allows reuse of list facts and definitions, but requires lemmas connecting them together.
  2. Inductive encoding requires reimplmenting all list elements.
  3. Symmetric encoding makes simple equality not work, requires a special equality operator.
 *)





Archive powered by MHonArc 2.6.18.

Top of Page