Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] How to represent tabular data in Coq

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] How to represent tabular data in Coq


Chronological Thread 
  • From: Ben Sherman <sherman AT csail.mit.edu>
  • To: coq-club AT inria.fr, Klaus Ostermann <klaus.ostermann AT uni-tuebingen.de>
  • Subject: Re: [Coq-Club] How to represent tabular data in Coq
  • Date: Tue, 9 May 2017 23:38:48 -0400
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=sherman AT csail.mit.edu; spf=Pass smtp.mailfrom=sherman AT csail.mit.edu; spf=None smtp.helo=postmaster AT outgoing-stata.csail.mit.edu
  • Ironport-phdr: 9a23:FPz2VxdOTsvykaYDpYKgjKFglGMj4u6mDksu8pMizoh2WeGdxcSyYh7h7PlgxGXEQZ/co6odzbGH7+a4ASQp2tWoiDg6aptCVhsI2409vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6nK94iQPFRrhKAF7Ovr6GpLIj8Swyuu+54Dfbx9GiTe5br5+Ngm6oRnMvcQKnIVuLbo8xAHUqXVSYeRWwm1oJVOXnxni48q74YBu/SdNtf8/7sBMSar1cbg2QrxeFzQmLns65Nb3uhnZTAuA/WUTX2MLmRdVGQfF7RX6XpDssivms+d2xSeXMdHqQb0yRD+v9LlgRgP2hygbNj456GDXhdJ2jKJHuxKquhhzz5fJbI2JKPZye6XQds4YS2VcRMZcTyxPDZ+/YYQAEuQPM+hYoZf/qFUJthaxHxWgBOb1xz9ImnP736s32PkhHwHc2wwgGsoDvWnKo9XzKawcTOC1w7fSzTXGdfxW3yr25Y/TchAhv/6MR7dwftDXyUQ0CwzFlU+fqY3hPz+P0+QNsnSb4/B+Wu2ylm4qsgd8qSWhyMcrj4nGnIMVylbc+CV2wYY1Od24SFNgbtK+DJRQsCSaO5NqTcMiR2FouT46xacCuZGhZiQKzoooxwLZZveacIaI+gruWPuPLTp7nn5odq6ziwys/US61OHwS9W43E5XoidGiNXAq3MA2hjJ5sWDSvZx5ESs1DSJ2gvO8O9LO1o0mrDeK5M5wr4/iJ4TsUPbEy/xhUX2kKCWdkIl+uiu5OTre67mpoOGOI9okA7yKLghmtelDuQ5NggCRW2b+eWg1LH540L2XahKguU3kqnfrp/aOdwWqrOnDwJWyIou5QqzAy243NgCg3ULMU5JdAqCj4fzOlHOJP74De24g1SpiDpr3PDGMaP7ApXINHfDlqzscqpm60FGyQozycpT55dVCrEdOv78RFL+tMHAAh8jLwO02/rnCMl61o4GRW2PBbaZPLrOvl+M++IgOPKBZJQVuTb4M/gq/eTijX4/mV8HfKmmx4EbaH6iHqcuH0LMan31x9wFDG0ivwwkTeWshkfGGR1XbmupF4Un+jwhDYugC8+XRYSknafH2zynE4dTb2ZAIk2KEDL0aoiOWvEDZSTULsI3wRIeUr30YYInnSquuRX/wrwvevHU9zcdvJTL38N85umVkBAuszF4EpLOgCm2U2hokzZQFHcN16dlrBk4kw/b3A==

If you're just looking to represent something like this (rather than implement it with some efficient data structure), you can basically do exactly what you wrote:

Inductive member {A} (x : A) : list A -> Type :=
  | here : forall xs, member x (x :: xs)
  | there : forall y xs, member x xs -> member x (y :: xs).

Infix "∈" := member (at level 30).

Record Table (R C : Type) (r : list R) (c : list C) A := MkTable
  { lookup : forall x y, x ∈ r -> y ∈ c -> A }.


(* Another version, which could be a bit easier in saving
   you from needing UIP unnecessarily *)

Inductive members {A} : list A -> Type :=
  | here' : forall x xs, members (x :: xs)
  | there' : forall x xs, members xs -> members (x :: xs).

Record Table' (R C : Type) (r : list R) (c : list C) A :=
  MkTable' { lookup' : members r -> members c -> A }.

The [member] inductive family is not in the Coq standard library, though it's a common thing to do. I can point you to various code defining such a family that could have lemmas you could use that you might find useful:
http://adam.chlipala.net/cpdt/html/DataStruct.html
https://github.com/coq-ext-lib/coq-ext-lib/blob/v8.5/theories/Data/Member.v
https://github.com/bmsherman/topology/blob/master/src/Types/List.v#L8-L10

(You might notice that [list unit] is isomorphic to [nat], and similarly [@members unit] is isomorphic to [Fin.t], and [Fin.t n -> A] is isomorphic to [Vector.t A n], which relates some of the definitions here to [Vector.t].) 

From: Klaus Ostermann <klaus.ostermann AT uni-tuebingen.de>
Subject: [Coq-Club] How to represent tabular data in Coq
Date: May 8, 2017 at 11:14:52 AM EDT


I have data that one would write down on a piece of paper as a table
with row and column headers.

The type of the headers is not "nat", though (otherwise I could use
nested vectors).
Let's call them R and C for the type of row headers and column headers,
respectively.
The type of the table elements is A.

Let's say I have finite sets r and c of R's and C's, respectively. r and
c are the labels
of a particular table.

Such a table should feature a function to look up an entry in the table,
which should
have a type similar to this:

lookup: Table R C r c A -> x in r -> y in c -> A

where "x in r" means a label x that is in the label set r.

In particular, the lookup function should not return an "option A".

Is there something like this in a standard library? How would I go about
this?

As a starter, is there something that is to standard finite maps as
vectors are to lists?
That is, finite maps where the type system keeps track of the available
keys?

Klaus




Archive powered by MHonArc 2.6.18.

Top of Page