coq-club AT inria.fr
Subject: The Coq mailing list
List archive
- From: Benjamin Gregoire <Benjamin.Gregoire AT sophia.inria.fr>
- To: Satrajit Roy <admin AT satrajit.com>
- Cc: Coq Club <coq-club AT pauillac.inria.fr>
- Subject: Re: [Coq-Club]Polymorphic lists and functions
- Date: Mon, 11 Sep 2006 17:52:30 +0200
- List-archive: <http://pauillac.inria.fr/pipermail/coq-club/>
Satrajit Roy wrote:
Hi guys!!!!I have corrected some typos in the record declaration.
I have been grappling with polymorphism in Coq for a while. So thought a concrete example might make it easier for people to answer.
Let's say I have:
Inductive policy_kind:Set :=
policy_a | policy_b.
Record policy_A:Set := A {a_def : bool; a_cur:bool}.
Record policy_B:Set := B {b_def: nat; b_max:nat; b_cur:nat}.
What should be the correct definition for 'policy' if I want to do something like the following:Here is one solution :
Definition policy (k:policy_kind) (data:list <?>) : <?> :=
match k with
| policy_a => A (nth 0 data) (nth 1 data) (*nth must return a bool*)
| policy_b => B (nth 0 data) (nth 1 data) (nth 2 data) (*nth must return a nat*)
end.
I know I can do it differently, but I want to do it this way so that the accessor functions are auto generated.
Definition elem_of_pol k := match k with policy_a => bool | policy_b => nat end.
Definition type_of_pol k := match k with policy_a => policy_A | policy_b => policy_B end.
Require Import List.
(* The first arguments of nth is implicite, the last is a default value
(if the list contain less than n elements,
where n is the the second argument of nth) *)
Definition policy (k:policy_kind) (data : list (elem_of_pol k)) : type_of_pol k :=
match k return list (elem_of_pol k) -> type_of_pol k with
| policy_a => fun (lb:list bool) => A (nth 0 lb true) (nth 1 lb true)
| policy_b => fun (ln:list nat) => B (nth 0 ln 0) (nth 1 ln 0) (nth 2 ln 0)
end data.
Benjamin
There may be syntax errors in the above, but, basically I need to figure out what would the terms be that should replace the '?" in the above definition.
Thanks in anticipation.
- [Coq-Club]Polymorphic lists and functions, Satrajit Roy
- Re: [Coq-Club]Polymorphic lists and functions, Benjamin Gregoire
Archive powered by MhonArc 2.6.16.