Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] heterogeneous tuple element function

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] heterogeneous tuple element function


Chronological Thread 
  • From: Vilhelm Sjöberg <vilhelm AT cis.upenn.edu>
  • To: coq-club AT inria.fr
  • Subject: Re: [Coq-Club] heterogeneous tuple element function
  • Date: Thu, 15 Jan 2015 19:02:06 -0500

On 2015-01-15 18:33, Jonathan Leivent wrote:
That does look familiar.  However, how to get 'get' to infer the ts arg from the tuple?  As this doesn't work:

  get _ 2 (1, (true, (42, tt))).

One way is to use typeclasses to infer this. The below code is inspired by the Data.Typeable class in Haskell; I'm not sure if there already is some existing similar framework in Coq.
Class Typeable (T : Set) := {
 getCode : list Set;
 getCode_spec : T = tupleType getCode
}.

Instance TypableUnit : Typeable unit :=
 Build_Typeable unit nil eq_refl.

Instance TypeablePair (a b : Set) (c : Typeable b) : Typeable (a*b).
  apply (Build_Typeable (a*b) (a::getCode)).
  simpl. rewrite <- getCode_spec.
  reflexivity.
Defined.

Definition cast {a b : Set} (p: a=b) (x: a) : b :=
  match p in _=b return b with eq_refl => x end.

Definition get' {a:Set} {c: Typeable a} (i:nat) (v : a)  := 
  get getCode i (cast getCode_spec v).

Eval compute in
  get' 2 (1,(true,(42,tt))).  (* = 42 *)
Vilhelm



Archive powered by MHonArc 2.6.18.

Top of Page