Skip to content

Commit f2cfd3e

Browse files
author
Gregory Malecha
committed
heterogeneous relations on hlists
1 parent 1b16f84 commit f2cfd3e

File tree

1 file changed

+91
-3
lines changed

1 file changed

+91
-3
lines changed

theories/Data/HList.v

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,12 @@ Arguments Hnil {_ _}.
567567
Arguments Hcons {_ _ _ _} _ _.
568568
Arguments equiv_hlist {_ F} R {_} _ _ : rename.
569569

570+
(** Weak Map
571+
** This is weak because it does not change the key type
572+
**)
570573
Section hlist_map.
571574
Variable A : Type.
572-
Variable F : A -> Type.
573-
Variable G : A -> Type.
575+
Variables F G : A -> Type.
574576
Variable ff : forall x, F x -> G x.
575577

576578
Fixpoint hlist_map (ls : list A) (hl : hlist F ls) {struct hl} : hlist G ls :=
@@ -579,6 +581,92 @@ Section hlist_map.
579581
| Hcons _ _ hd tl =>
580582
Hcons (ff hd) (hlist_map tl)
581583
end.
584+
582585
End hlist_map.
583586

584-
Arguments hlist_map {_ _ _} _ {_} _.
587+
Lemma equiv_hlist_map
588+
: forall T U (F : T -> Type) (R : forall t, F t -> F t -> Prop)
589+
(R' : forall t, U t -> U t -> Prop)
590+
(f g : forall t, F t -> U t),
591+
(forall t (x y : F t), R t x y -> R' t (f t x) (g t y)) ->
592+
forall ls (a b : hlist F ls),
593+
equiv_hlist R a b ->
594+
equiv_hlist R' (hlist_map _ f a) (hlist_map _ g b).
595+
Proof.
596+
clear. induction 2; simpl; intros.
597+
- constructor.
598+
- constructor; eauto.
599+
Qed.
600+
601+
Arguments hlist_map {_ _ _} _ {_} _.
602+
603+
(** Heterogeneous Relations **)
604+
Section hlist_rel.
605+
Variable A : Type.
606+
Variables F G : A -> Type.
607+
Variable R : forall x : A, F x -> G x -> Prop.
608+
609+
Inductive hlist_hrel : forall ls, hlist F ls -> hlist G ls -> Prop :=
610+
| hrel_Hnil : hlist_hrel Hnil Hnil
611+
| hrel_Hcons : forall t ts x y xs ys, @R t x y -> @hlist_hrel ts xs ys ->
612+
@hlist_hrel (t :: ts) (Hcons x xs) (Hcons y ys).
613+
614+
End hlist_rel.
615+
616+
Section hlist_rel_map.
617+
Variable A : Type.
618+
Variables F G F' G' : A -> Type.
619+
Variable R : forall x : A, F x -> G x -> Prop.
620+
Variable R' : forall x : A, F' x -> G' x -> Prop.
621+
Variable ff : forall x : A, F x -> F' x.
622+
Variable gg : forall x : A, G x -> G' x.
623+
624+
Hypothesis R_ff_R' :
625+
forall t x y, @R t x y ->
626+
@R' t (ff x) (gg y).
627+
628+
Theorem hlist_hrel_map
629+
: forall ls xs ys,
630+
@hlist_hrel A F G R ls xs ys ->
631+
@hlist_hrel A F' G' R' ls (hlist_map ff xs) (hlist_map gg ys).
632+
Proof.
633+
induction 1; simpl; constructor; eauto.
634+
Qed.
635+
636+
Theorem hlist_hrel_cons
637+
: forall l ls x xs y ys,
638+
@hlist_hrel A F G R (l :: ls) (Hcons x xs) (Hcons y ys) ->
639+
@R l x y /\ @hlist_hrel A F G R ls xs ys.
640+
Proof.
641+
intros.
642+
refine
643+
match H in @hlist_hrel _ _ _ _ ls' xs' ys'
644+
return
645+
match ls' as ls' return hlist F ls' -> hlist G ls' -> Prop with
646+
| nil => fun _ _ => True
647+
| l' :: ls' => fun x y =>
648+
R (hlist_hd x) (hlist_hd y)
649+
/\ hlist_hrel R (hlist_tl x) (hlist_tl y)
650+
end xs' ys'
651+
with
652+
| hrel_Hnil => I
653+
| hrel_Hcons _ _ _ _ _ _ pf pf' => conj pf pf'
654+
end.
655+
Qed.
656+
657+
Theorem hlist_hrel_app
658+
: forall l ls x xs y ys,
659+
@hlist_hrel A F G R (l ++ ls) (hlist_app x xs) (hlist_app y ys) ->
660+
@hlist_hrel A F G R l x y /\ @hlist_hrel A F G R ls xs ys.
661+
Proof.
662+
induction x.
663+
+ intros xs y ys. rewrite (hlist_eta y).
664+
simpl; intros; split; auto. constructor.
665+
+ intros xs y ys. rewrite (hlist_eta y).
666+
intros. eapply hlist_hrel_cons in H.
667+
destruct H.
668+
apply IHx in H0.
669+
intuition. constructor; auto.
670+
Qed.
671+
672+
End hlist_rel_map.

0 commit comments

Comments
 (0)