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: Jason -Zhong Sheng- Hu <fdhzs2010 AT hotmail.com>
  • To: Fred Smith <fsmith1024 AT gmail.com>, "coq-club AT inria.fr" <coq-club AT inria.fr>
  • Subject: Re: [Coq-Club] A question of encoding...
  • Date: Sun, 2 Dec 2018 20:50:40 +0000
  • Accept-language: en-CA, en-US
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=fdhzs2010 AT hotmail.com; spf=Pass smtp.mailfrom=fdhzs2010 AT hotmail.com; spf=Pass smtp.helo=postmaster AT NAM01-BN3-obe.outbound.protection.outlook.com
  • Ironport-phdr: 9a23:WNqmqhDaOGxob7fHcQ2CUyQJP3N1i/DPJgcQr6AfoPdwSPX6pcbcNUDSrc9gkEXOFd2Cra4c26yO6+jJYi8p2d65qncMcZhBBVcuqP49uEgeOvODElDxN/XwbiY3T4xoXV5h+GynYwAOQJ6tL1LdrWev4jEMBx7xKRR6JvjvGo7Vks+7y/2+94fcbglUhzexe69+IAmrpgjNq8cahpdvJLwswRXTuHtIfOpWxWJsJV2Nmhv3+9m98p1+/SlOovwt78FPX7n0cKQ+VrxYES8pM3sp683xtBnMVhWA630BWWgLiBVIAgzF7BbnXpfttybxq+Rw1DWGMcDwULs5Qiqp4bt1RxD0iScHLz85/3/Risxsl6JQvRatqwViz4LIfI2ZMfxzdb7fc9wHX2pMRsZfWTJcDIOgYYUBDOQBMuREoIbyvFYBtweyCRW2Ce/z1jNEmHn71rA63eQ7FgHG2RQtEdYUv3TPq9X1MroZXfm2w6nIyjXDafxW0irg5ojIbB8hp/6MUattesTT1EkkCgTIjluNpozlPjKVzfoBv3SG4+Z8Tu+vi2knqx10oje1x8csjpPFiZ4SylDB7Ch0xps+K96gSENjbtOoDIFcuiWEO4dsXs8uWWJltDoixr0Ip5G2fzQGxZEiyhHBdvOKcYqF7xfjWeuVPzh1hXdod6+kiBu29UWtz+nxWdK73VpXqCdOj8PCuWoX1xPJ78iKUvt98Vml2TaIzw3d8v1JL0comabGMpIs36Y+m5QKvUTEBSD5hl/6jKiLdkU44eeo7PnnYrP7qZOGL490kAb+MrgwlcOjHeQ4Mw8OX26B9eS7yb3j4Un5QLJNjv01iKXWrJfaJcEDqq64BQ9azJoj5g6wAju6ytgVmWcLIEhZdB6djIXlJ0nCIPXiAve+h1Ssni1rx/fDPrD5DJXCM3jDkbb6fbpj90JQ1RY/wMtf55JTFrEBJej8Wk71tNDCEhA5NAm0z/79CNphzoMeRX6PAqiBPazOtl+I//sjLPWIZI8IoznwMOMl5v7rjX8hg1ARZ6ip3Z0NaHC5BPtqOUuZYWC/yusGREkQswZ2d+nrlF3KBTBIYneuX/tkvG0TB4evDIOFTYeo1vjJliy8B9hdYn1MIlGKC3bhMYueEb9YYyWLZ8RljzYsVL67SoZn2wv45yHgzL8yDOPP/StQ8KDj0956r9bTmBc9sHRUEozJ3W2NXXouxjpQbz8xwKV2oEg7wVCGh/sry8dEHMBesqsaGjwxMoTRmqkjU4irC1DxO+yRQVPjee2IRDQ4T9Y/2dgLOh0vG9K+ixnC22yhBLpHzuXXVqxxybrV2j3KH+g402zPjfJzj146R8JOMSutgastr1GOVb6MqF2QkuORTYpZ3CPJ8zvcn0ynmRkBFSRWCuDCV31ZYVbKp9Pk4E+EV6WpFbksLgpGz4iFN7dObdrqy15BQaW6NQ==

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