11

Is there a well established Coq graph library for proving simple theorems ?

I would like to learn how to prove simple stuff like: "G1, G2 are isomorphic if and only if their complements are isomorphic".

Are there related/similar examples or tutorials available?

Vor
  • 365
  • 3
  • 17
  • 1
    There's a [contribution about graph theory](http://coq.inria.fr/pylons/contribs/view/GraphBasics/v8.4) and a few other contributions also develop some graph theory. Why don't you implement graphs like you would in a functional programming language and then prove things about them? If you're new to Coq, I don't think you'll find tutorials that specific. –  Jul 15 '14 at 12:42
  • @autorewrite: I'm a newbie, but I would like to follow some examples that are related (e.g. some proofs about simple combinatorial objects: permutations, set operations, ecc.). – Vor Jul 15 '14 at 13:37

1 Answers1

18

Here's a demonstration.

Allow rewriting with equivalence relations.

Require Import Coq.Setoids.Setoid.

A graph is a set of vertices along with an adjacency relation.

Definition graph : Type := {V : Type & V -> V -> bool}.

A graph from vertices and adjacency.

Definition create : forall V, (V -> V -> bool) -> graph := @existT _ _.

Vertices from a graph.

Definition vertices : graph -> Type := @projT1 _ _.

Adjacency from a graph.

Definition adjacent : forall g1, vertices g1 -> vertices g1 -> bool := @projT2 _ _.

The complement of a graph has the same vertices, but a negated adjacency relation.

Definition complement : graph -> graph := fun g1 => create (vertices g1) (fun v1 v2 => negb (adjacent g1 v1 v2)).

Standard stuff.

Definition injective : forall {t1 t2}, (t1 -> t2) -> Prop := fun t1 t2 f1 => forall x1 x2, f1 x1 = f1 x2 -> x1 = x2.

Definition surjective : forall {t1 t2}, (t1 -> t2) -> Prop := fun t1 t2 f1 => forall x1, exists x2, f1 x2 = x1.

Definition bijective : forall {t1 t2}, (t1 -> t2) -> Prop := fun t1 t2 f1 => injective f1 /\ surjective f1.

Two graphs are isomorphic if there's a bijection between their vertices that preserves adjacency.

Definition isomorphic : graph -> graph -> Prop := fun g1 g2 => exists f1, bijective f1 /\ (forall x1 x2, adjacent g1 x1 x2 = adjacent g2 (f1 x1) (f1 x2)).

Infix "~" := isomorphic (at level 70).

A helpful fact whose proof I leave to you.

Conjecture C1 : forall b1 b2, negb b1 = negb b2 <-> b1 = b2.

Your fact.

Goal forall g1 g2, g1 ~ g2 <-> complement g1 ~ complement g2.
Proof.

Access the components of the graphs.

destruct g1.
destruct g2.

Substitute defined by definition.

unfold isomorphic, complement, adjacent, vertices, create, projT2, projT1.

First-order logic simplifications.

firstorder.

Instanciation.

exists x1.
firstorder.
rewrite C1.
firstorder.
exists x1.
firstorder.

Instanciation.

specialize (H0 x2 x3).
rewrite C1 in H0.
firstorder.
Qed.

Actually, this is a formalization of graphs whose adjacency relation is decidable V -> V -> bool. In intuitionistic logic, not all graphs with a general adjacency relation V -> V -> Prop have the property you want to prove.

Instead of sticking to finite or otherwise decidable graphs, you can also move to classical logic or use double negation translation.

  • 1
    G.R.E.A.T.!!! Thank you! I'll read/test it carefully. I would like to have many of these examples ... they are a good way to learn. Did you use some books to learn Coq? – Vor Jul 18 '14 at 15:07
  • 1
    I did. I liked the first few chapters of [Software Foundations](http://www.cis.upenn.edu/~bcpierce/sf/current/index.html). Then it got a little too abstract for me. After that I just used the internet for reference and some other books like [Coq'Art](http://www.springer.com/mathematics/book/978-3-540-20854-9) and [CPDT](http://adam.chlipala.net/cpdt/). –  Jul 18 '14 at 15:32
  • thank you! I was going to buy Coq'Art, too ... and I already started reading the online version of [Certified Programming with Dependent Types](http://adam.chlipala.net/cpdt/). I'll give a look to Software Foundations. – Vor Jul 18 '14 at 15:37