Last active
April 5, 2020 03:04
-
-
Save zunction/cd1f72f24a255cae5dbf55de087601e5 to your computer and use it in GitHub Desktop.
Working with preds for KGs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From mathcomp Require Import all_ssreflect. | |
| From void Require Import void. | |
| From deriving Require Import deriving. | |
| Require Import Coq.Strings.String. | |
| Open Scope string_scope. | |
| Section uri. | |
| Variables A B : eqType. | |
| Variable x0 : A * B. | |
| Variable a : A. | |
| Variable b : B. | |
| Variable l : seq (A * B). | |
| Definition fstpred (p : A * B) := (p.1 == a). | |
| Definition sndpred (p : A * B) := (p.2 == b). | |
| Definition fst2pair := nth x0 l (find fstpred l). | |
| Definition snd2pair := nth x0 l (find sndpred l). | |
| Definition fst2snd := fst2pair.2. | |
| Definition snd2fst := snd2pair.1. | |
| (* added snd->fst *) | |
| Lemma fst2pair1P : has fstpred l -> fst2pair.1 = a. | |
| Proof. by move=> h; apply/eqP; move: h; apply/nth_find. Qed. | |
| Lemma snd2pair2P : has sndpred l -> snd2pair.2 = b. | |
| Proof. by move=> h; apply/eqP; move: h; apply/nth_find. Qed. | |
| Lemma fpredhas_find : has fstpred l -> (find fstpred l) < size l. | |
| Proof. by rewrite has_find. Qed. | |
| Lemma spredhas_find : has sndpred l -> (find sndpred l) < size l. | |
| Proof. by rewrite has_find. Qed. | |
| Lemma fst2sndP : has fstpred l -> (a, fst2snd) \in l. | |
| Proof. move=> h; pose x := fst2pair; have: x.1 = a. { apply/fst2pair1P/h. } | |
| move=> g; rewrite -{1} g; rewrite/fst2snd-/x. | |
| by rewrite -surjective_pairing; apply/mem_nth/fpredhas_find. Qed. | |
| Lemma snd2fstP : has sndpred l -> (snd2fst, b) \in l. | |
| Proof. move=> h; pose x := snd2pair; have: x.2 = b. { apply/snd2pair2P/h. } | |
| move=> g; rewrite -{1} g; rewrite/snd2fst-/x. | |
| by rewrite -surjective_pairing; apply/mem_nth/spredhas_find. Qed. | |
| (* headinlist to help with composite queries *) | |
| Lemma headinlist (al : seq A) (a0 : A): has (pred1 (head a0 al)) al-> (head a0 al) \in al. | |
| Proof. by move=> h; rewrite -has_pred1. Qed. | |
| End uri. | |
| (* Deriving the type structures of string *) | |
| Canonical string_indType := Eval hnf in IndType _ _ [indMixin for string_rect]. | |
| Canonical string_eqType := Eval hnf in EqType string [derive eqMixin for string]. | |
| Canonical string_choiceType := Eval hnf in ChoiceType string [derive choiceMixin for string]. | |
| Canonical string_countType := Eval hnf in CountType string [derive countMixin for string]. | |
| Inductive uri : Type := Build_uri (s : string). | |
| Definition defaultU := Build_uri "default". | |
| (* Deriving the type structures of uri *) | |
| Definition uri_indMixin := Eval hnf in [indMixin for uri_rect]. | |
| Canonical uri_indType := Eval hnf in IndType _ _ uri_indMixin. | |
| Canonical uri_eqType := Eval hnf in EqType uri [derive eqMixin for uri]. | |
| Canonical uri_choiceType := Eval hnf in ChoiceType uri [derive choiceMixin for uri]. | |
| Canonical uri_countType := Eval hnf in CountType uri [derive countMixin for uri]. | |
| Definition query_pred := pred uri. | |
| Record query_type (qp: query_pred) := Answer { query_uri :> uri ; _ : query_uri \in qp}. | |
| (* Building the uris *) | |
| Definition barackU := Build_uri "barack". | |
| Definition michelleU := Build_uri "michelle". | |
| Definition maliaU := Build_uri "malia". | |
| Definition sashaU := Build_uri "sasha". | |
| Definition personuriL := [:: barackU; michelleU; maliaU; sashaU]. | |
| Definition personP : query_pred := mem personuriL. | |
| Definition personT := query_type personP. | |
| Definition barack := Answer personP barackU erefl. | |
| (* eqType not established *) | |
| Fail Compute barack == barack. | |
| (* Building eqType instances for all query_type *) | |
| Canonical queryt_subType (qp:query_pred) := Eval hnf in [subType for query_uri qp]. | |
| Definition queryt_eqMixin (qp:query_pred) := Eval hnf in [eqMixin of (query_type qp) by <:]. | |
| Canonical queryt_eqType (qp:query_pred) := Eval hnf in EqType (query_type qp) (queryt_eqMixin qp). | |
| Definition queryt_choiceMixin (qp:query_pred) := Eval hnf in [choiceMixin of (query_type qp) by <:]. | |
| Canonical queryt_choiceType (qp:query_pred) := Eval hnf in ChoiceType (query_type qp) (queryt_choiceMixin qp). | |
| Definition queryt_countMixin (qp:query_pred) := Eval hnf in [countMixin of (query_type qp) by <:]. | |
| Canonical queryt_countType (qp:query_pred) := Eval hnf in CountType (query_type qp) (queryt_countMixin qp). | |
| (* FINTYPES ARE STILL UNSOLVED... | |
| Definition queryt_finMixin (qp:query_pred) := Eval hnf in [finMixin of (query_type qp) by <:]. | |
| Canonical queryt_finType (qp:query_pred) := Eval hnf in FinType (query_type qp) (queryt_finMixin qp). | |
| *) | |
| (* eqTypes available now *) | |
| Compute barack == barack. | |
| Definition bc_maliaU := Build_uri "birthcert_malia". | |
| Definition bc_sashaU := Build_uri "birthcert_sasha". | |
| Definition birthcerturiL := [:: bc_maliaU; bc_sashaU]. | |
| Definition birthcertP : pred uri := mem birthcerturiL. | |
| Definition birthcertT := query_type birthcertP. | |
| Compute bc_maliaU \in birthcertP. | |
| Compute barackU \in birthcertP. | |
| Definition bc_malia := Answer birthcertP bc_maliaU erefl. | |
| Eval compute in bc_malia == bc_malia. | |
| Definition bc_childL := [:: (bc_maliaU, maliaU); (bc_sashaU, sashaU)]. | |
| Definition bc_child : pred (uri*uri) := mem bc_childL. | |
| Compute (bc_maliaU,maliaU) \in bc_child. | |
| Compute (bc_maliaU,sashaU) \in bc_child. | |
| Compute bc_child (bc_maliaU,maliaU). | |
| Section Proj. | |
| (* Generalized predicates for fst and snd *) | |
| Variable A : eqType. | |
| Variable a : A. | |
| Definition fstP (pair_pred : pred (uri*uri)) (fstU : uri): pred uri := fun x => pair_pred (fstU, x). | |
| Definition sndP (pair_pred : pred (uri*uri)) (sndU : uri): pred uri := fun x => pair_pred (x, sndU). | |
| End Proj. | |
| Definition bc_child_1P (bc:uri) := fstP bc_child bc. | |
| Definition bc_child_2P (ch:uri) := sndP bc_child ch. | |
| Definition bc_child_1T (bc:uri) := query_type (bc_child_1P bc). | |
| Definition bc_child_2T (ch:uri) := query_type (bc_child_2P ch). | |
| Ltac dqt_snd := by apply/Answer/(fst2sndP _ _ (defaultU,defaultU)). | |
| Ltac dqt_fst := by apply/Answer/(snd2fstP _ _ (defaultU,defaultU)). | |
| Theorem bc_malia_child : bc_child_1T bc_maliaU. | |
| Proof. by apply/Answer/(fst2sndP _ _ (defaultU,defaultU)). Defined. | |
| Compute maliaU == bc_malia_child. | |
| Theorem malia_bc : bc_child_2T maliaU. | |
| Proof. by apply/Answer/(snd2fstP _ _ (defaultU,defaultU)). Defined. | |
| Compute bc_maliaU == malia_bc. | |
| Compute filter (bc_child_1P bc_maliaU) personuriL. | |
| Definition bc_fatherL := [:: (bc_maliaU, barackU); (bc_sashaU, barackU)]. | |
| Definition bc_father : pred (uri*uri) := mem bc_fatherL. | |
| Definition bc_father_1P (bc:uri) := fstP bc_father bc. | |
| Definition bc_father_2P (f:uri) := sndP bc_father f. | |
| Definition bc_father_1T (bc:uri) := query_type (bc_father_1P bc). | |
| Definition bc_father_2T (f:uri) := query_type (bc_father_2P f). | |
| Theorem bc_malia_father : bc_father_1T bc_maliaU. | |
| Proof. dqt_snd. Defined. | |
| Compute barackU == bc_malia_father. | |
| (* filter out the birthcerts of the person *) | |
| Definition bc_of_personL (x : uri) := filter (sndP bc_child x) birthcerturiL. | |
| Compute bc_of_personL maliaU. | |
| (* filter out the father in the birthcerts *) | |
| Definition father_of_bcL (x : uri) := filter (fstP bc_father (head defaultU (bc_of_personL x))) personuriL. | |
| Compute father_of_bcL maliaU. | |
| (* gives us the predicate below to feed into the pred_type to do query *) | |
| Definition father_of_bcP (x: uri):= mem (father_of_bcL x). | |
| Definition father_T (x : uri) := query_type (father_of_bcP x). | |
| Theorem malia_father : father_T maliaU. | |
| Proof. by apply/Answer/(headinlist _ _ defaultU). Defined. | |
| Compute barackU == malia_father. |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
my_pred.v