proposition: Lemma refine_Iterate_Equiv_QueryResultComp_body_Where_And_eq m {ResultT : Type} (heading : RawHeading) : forall (headings : Fin.t m -> RawHeading) (Ensembles : forall (idx : Fin.t m), @IndexedEnsemble (@RawTuple (headings idx))) (inj_Tuple : forall (idx : Fin.t m), @RawTuple (headings idx) -> @RawTuple heading) (FiniteEnsembles : forall idx, FiniteEnsemble (Ensembles idx)) (body : @RawTuple heading -> Comp (list ResultT)) idx P Q, (forall idx', idx <> idx' -> forall tup, In _ (Ensembles idx') tup -> ~ (P (inj_Tuple _ (indexedElement tup)))) -> (forall tup, In _ (Ensembles idx) tup -> P (inj_Tuple _ (indexedElement tup))) -> refine (Iterate_Equiv_QueryResultComp_body heading headings Ensembles inj_Tuple (fun tup => Where (P tup /\ Q tup) (body tup))) (QueryResultComp (Ensembles idx) (fun tup => Where (Q (inj_Tuple _ tup)) (body (inj_Tuple idx tup)))).
proof: Proof. induction m; simpl; intros. - intros; inversion idx. - destruct (fin_eq_dec idx Fin.F1). + etransitivity. * apply refine_under_bind_both; intros. apply refine_under_bind; intros. eapply refine_flatten_CompList_func'. intros; rewrite (refine_Query_Where_Cond (Q := Q (inj_Tuple _ v))). finish honing. intros; computes_to_inv; unfold UnIndexedEnsembleListEquivalence in H1; destruct_ex; intuition; subst. apply in_map_iff in H2; destruct_ex; intuition; subst. apply H1 in H6; apply H0 in H6; intuition. rewrite e. rewrite refine_Iterate_Equiv_QueryResultComp_body_Where_False. simplify with monad laws; rewrite app_nil_r; finish honing. intuition eauto. unfold not; intros; eapply H; intuition eauto. rewrite e in H4; discriminate. * simplify with monad laws. unfold UnConstrQuery_In, QueryResultComp. rewrite e. reflexivity. + rewrite refine_QueryResultComp_body_Where_False. * simplify with monad laws. revert IHm headings Ensembles inj_Tuple FiniteEnsembles P H H0 n; pattern m, idx. apply Fin.caseS; simpl; intros. congruence. apply (IHm (fun n => headings (Fin.FS n)) (fun n => Ensembles (Fin.FS n)) (fun n tup => inj_Tuple (Fin.FS n) tup)); intros; eauto. intro. eapply (H (Fin.FS idx')); eauto. intro; apply H1; apply Fin.FS_inj; eauto. * unfold not; intros. eapply H; intuition eauto. * apply FiniteEnsembles. Qed.
imports: Fiat.Common.Coq__8_4__8_5__Compat Coq.Vectors.Vector Coq.Strings.Ascii Coq.Bool.Bool Coq.Lists.List Fiat.Common.SumType Fiat.Computation.ListComputations Fiat.QueryStructure.Automation.AutoDB Bedrock.Word Bedrock.Memory