Require Export Monotonicity.
(** ** The [transport] tactic *)
(** Often, we know that a number of terms are related, and need to transport hypotheses built out the the left-hand sides into ones with similar shapes, but built out of the right-hand sides. For instance, in cirumstances where [rauto] can establish [option_rel R x y], we will want to turn a hypothesis of the form [x = Some a] into one of the form [y = Some b], and remember that [R a b]. This is the role of the [transport] tactic. *)
(** *** Transportable hypotheses *)
(* To make the tactic extensible, we use the following typeclass to declare patterns of relations and hypothesis shapes that can be handled in this way. *)
Class Transport {A B} (R: rel A B) (a: A) (b: B) (PA: Prop) (PB: Prop) :=
transport: R a b -> PA -> PB.
(** One stereotypical example is [option_le]: the [Transport] instance defined in the [OptionRel] library allows us to transport hypothese of the form [x = Some a] into hypotheses of the form [y = Some b], with [a] and [b] related by [R] whenever [x] and [y] are related by [option_le R], and similarly for [None] and [option_rel].  Other instances are declared below. *)
Global Instance prod_rel_transport_eq_pair {A1 B1 A2 B2} (R1: rel A1 B1) (R2: rel A2 B2) x y a1 a2:
Transport (prod_rel R1 R2) x y (x = (a1, a2)) (exists b1 b2, y = (b1, b2) /\ R1 a1 b1 /\ R2 a2 b2).
(** For [set_le] the situation is slightly more involved, for two reasons. First, a regular [eapply set_le_transport] fails to unify the parameter [B] of [Transport] against the [_ -> Prop] provied by the instance below. This can be worked around by pre-unifying that specific parameter. Second, because [set_le_transport] is potentially applicable to virtually any hypothesis (since there is no strongly distinguishing syntactic format in our target hypotheses), it makes [transport_hyps] very slow.  To address this, we explicitely register hints based on the [set_le_transport] tactic, which looks for "keywords" before doing anything, then applies the lemma with the required unification preparation. For example, [set_le_transport] is used in the following way in the [SimClight] library: << Hint Extern 1 (Transport _ _ _ _ _) => set_le_transport @assign_loc : typeclass_instances. >> Note that it's necessary to use [@] because [assign_loc] is parametrized by typeclasses, and we want to avoid undue specialization at hint registration time. *)

(* Specifications: *)
Lemma set_le_transport {A B} (R: rel A B) sA sB a:
Transport (set_le R) sA sB (sA a) (exists b, sB b /\ R a b).