Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] Is there a name for this trick in programming with dependent types?

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] Is there a name for this trick in programming with dependent types?


Chronological Thread 
  • From: Jason Gross <jasongross9 AT gmail.com>
  • To: coq-club <coq-club AT inria.fr>
  • Subject: Re: [Coq-Club] Is there a name for this trick in programming with dependent types?
  • Date: Thu, 28 Feb 2019 13:57:45 -0500
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=jasongross9 AT gmail.com; spf=Pass smtp.mailfrom=jasongross9 AT gmail.com; spf=None smtp.helo=postmaster AT mail-ed1-f41.google.com
  • Ironport-phdr: 9a23:2f9x4hzgDFN6WObXCy+O+j09IxM/srCxBDY+r6Qd1O4SIJqq85mqBkHD//Il1AaPAd2Lraocw8Pt8InYEVQa5piAtH1QOLdtbDQizfssogo7HcSeAlf6JvO5JwYzHcBFSUM3tyrjaRsdF8nxfUDdrWOv5jAOBBr/KRB1JuPoEYLOksi7ze+/94HQbglSmDaxfa55IQmrownWqsQYm5ZpJLwryhvOrHtIeuBWyn1tKFmOgRvy5dq+8YB6/ShItP0v68BPUaPhf6QlVrNYFygpM3o05MLwqxbOSxaE62YGXWUXlhpIBBXF7A3/U5zsvCb2qvZx1S+HNsDwULs6Wymt771zRRDnhicINT43/m/UhMJtkqxUvAmsqAZjz4POeoyZKOZyc6HbcNgHRWRBRMFRVylZD424dYQPFO0BPeBFpIf8vVsOqgC+BRKxD+7oyD9HnGT23ao80+QuCwHG2xcgH8kKvXnPo9X1Mb0dUeGxzKXS0TrDaPZW1C775YPVfB4hpvSMUqhxccrX0UQvGALFjkmQqYz4JDyZzPgCs2+e7+dmSOmhiHYnphlvrjSzwsogkIrEi4IPxlza6Sl0w5w5KNKlREN9fNWqCoFftzuAOItzWs4iQ39nuCI9yrAevJ60ZikKyJA+yxHBdvOLbpGE4hzsWeqMOzt4i3VleLWwhxa270es0PHzVs6x0FpSrypFlMfDtmwV2hDN9sSKTuFx80Sh1DqVyQzf9uJJLVo7mKbGM5Ihx6Q/lpsXsUTNBC/2n0D2gbeUdko+4eio7OLnYrr8pp+bLI90kQXzP78hmsy6G+s4MwwOU3KH9uS70b3v5Vf5T6lSjv0qjqnZt4jXKtgcpq6gGgNazoIj6wukADq9y9QZnXwHLEpfdx6djojpPUvOIPHiAvuljVSsimQj+/eTNbr4R57JM3KLxLzmZPN271NW4As119FWoZxOXOIvOvX2D234r9veRjAjNBevi7LlAc560IwEXnmUU4eWNarTtRmD4ed5cLrEX5McpDuoc6tt3PXpl3JswQZML5ns5oMebTWDJtojJkyYZXT2hdJYSDUFuwM/SKrhj1jQCGcPNUb3ZLo143QAMKzjFZ3KH9n/j7mI3SP9FZpTNDgfVwK8VEzwfoDBYM8iLSKfJsg7z24BXLmlDoItjFSg6Fa8xL1gIe7ZvCYfsMC72Q==

Note that you can use a sort-of small inversions trick to make the definition more compact:

Definition ihd {n: nat} (l: ilist (S n)): A :=
match l in (ilist m) return (match m with O => True | S _ => A end) with
| Nil => I
| Cons _ h _ => h
end.

In fact, Coq is powerful enough to infer this, and the following works as well:

Definition ihd {n: nat} (l: ilist (S n)): A :=
  match l with
  | Cons _ h _ => h
  end.
Print ihd.
(* ihd =
fun (n : nat) (l : ilist (S n)) =>
match l in (ilist n0) return match n0 with
                             | 0 => IDProp
                             | S _ => A
                             end with
| Nil => idProp
| Cons _ h _ => h
end
     : forall n : nat, ilist (S n) -> A
*)


On Thu, Feb 28, 2019, 13:15 Lily Chung <ikdc AT mit.edu> wrote:
Seems to me that this is exactly the convoy pattern, just with a different proposition than equality.


On Feb 28, 2019 12:48, Shengyi Wang <txyyss AT gmail.com> wrote:
Hi,

Inspired by the definition of addvect in https://github.com/coq-contribs/matrices/blob/master/operators.v , I made a definition of head for length-indexed lists as follows:

Require Import Coq.Arith.PeanoNat.

Section ilist.

  Variable A : Set.
  
  Inductive ilist : nat -> Set :=
  | Nil : ilist O
  | Cons : forall n, A -> ilist n -> ilist (S n).

  Definition ihd {n: nat} (l: ilist (S n)): A :=
    match l in (ilist m) return (O < m -> A) with
    | Nil => fun p => False_rect _ (Nat.nle_succ_0 _ p)
    | Cons _ h _ => fun _ => h
    end (Nat.lt_0_succ n).

End ilist.

This definition introduces an extra parameter "O < m"  in return clause and applies the whole match to the proof "(Nat.lt_0_succ n)" to eliminate the parameter. With this trick we can claim false for the impossible branch.

I'm curious if there is a name for this trick like the "convoy pattern" trick named in Adam Chlipala's "Certified Programming with Dependent Types".

Thank you very much,

Shengyi Wang

Cell: +65-83509639




Archive powered by MHonArc 2.6.18.

Top of Page