Skip to content

Commit a843dc0

Browse files
committed
Current status of RC formalization.
1 parent bcec8f4 commit a843dc0

14 files changed

+1836
-720
lines changed

AbstractRelation.v

+15-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Module Type REL.
1010
Parameter inter : forall n, R n -> R n -> R n.
1111
Parameter times : forall m n, R m -> R n -> R (m + n).
1212
Parameter sum : forall m n, R m -> (T m -> T n) -> R n.
13+
Parameter rsum : forall m n, R m -> (T m -> R n) -> R n.
1314
Parameter sel : forall n, R n -> (T n -> bool) -> R n.
1415
Parameter flat : forall n, R n -> R n.
1516
Parameter supp : forall n, R n -> list (T n).
@@ -20,15 +21,21 @@ Module Type REL.
2021
Implicit Arguments inter [n].
2122
Implicit Arguments times [m n].
2223
Implicit Arguments sum [m n].
24+
Implicit Arguments rsum [m n].
2325
Implicit Arguments sel [n].
2426
Implicit Arguments flat [n].
2527
Implicit Arguments supp [n].
2628

2729
Definition card := fun n S => memb (@sum n 0 S (fun _ => Vector.nil _)) (Vector.nil _).
2830
Implicit Arguments card [n].
2931

30-
Parameter Rnil : R 0.
32+
Parameter Rnil : forall n, R n.
3133
Parameter Rone : R 0.
34+
Parameter Rsingle : forall n, T n -> R n.
35+
36+
Implicit Arguments Rnil [n].
37+
Implicit Arguments Rsingle [n].
38+
3239
(* generalized cartesian product of m relations, each taken with its arity n *)
3340
Fixpoint prod (Rl : list { n : nat & R n })
3441
: R (list_sum (List.map (projT1 (P := fun n => R n)) Rl)) :=
@@ -46,16 +53,8 @@ Module Type REL.
4653
Hypothesis V_dec : forall (v w : V), {v = w} + {v <> w}.
4754
Hypothesis V_eqb_eq : forall (v w : V), V_eqb v w = true <-> v = w.
4855
Parameter T_dec : forall n, forall (x y : T n), {x = y} + {x <> y}.
49-
(*
50-
Proof.
51-
intros. eapply Vector.eq_dec. apply V_eqb_eq.
52-
Qed.
53-
*)
56+
5457
Parameter T_eqb_eq : forall n, forall (v w : T n), T_eqb v w = true <-> v = w.
55-
(* Proof.
56-
intros. eapply Vector.eqb_eq. apply V_eqb_eq.
57-
Qed.
58-
*)
5958

6059
Parameter p_fs : forall n, forall r : R n, forall t, memb r t > 0 -> List.In t (supp r).
6160
Parameter p_fs_r : forall n, forall r : R n, forall t, List.In t (supp r) -> memb r t > 0.
@@ -67,11 +66,15 @@ Module Type REL.
6766
t = Vector.append t1 t2 -> memb (times r1 r2) t = memb r1 t1 * memb r2 t2.
6867
Parameter p_sum : forall m n, forall r : R m, forall f : T m -> T n, forall t,
6968
memb (sum r f) t = list_sum (List.map (memb r) (filter (fun x => T_eqb (f x) t) (supp r))).
69+
Parameter p_rsum : forall m n, forall r : R m, forall f : T m -> R n, forall t,
70+
memb (rsum r f) t = list_sum (List.map (fun t0 => memb r t0 * memb (f t0) t) (supp r)).
7071
Parameter p_self : forall n, forall r : R n, forall p t, p t = false -> memb (sel r p) t = 0.
7172
Parameter p_selt : forall n, forall r : R n, forall p t, p t = true -> memb (sel r p) t = memb r t.
7273
Parameter p_flat : forall n, forall r : R n, forall t, memb (flat r) t = flatnat (memb r t).
73-
Parameter p_nil : forall r, memb Rnil r = 0.
74-
Parameter p_one : forall r, memb Rone r = 1.
74+
Parameter p_nil : forall n (t : T n), memb Rnil t = 0.
75+
Parameter p_one : forall t, memb Rone t = 1.
76+
Parameter p_single : forall n (t : T n), memb (Rsingle t) t = 1.
77+
Parameter p_single_neq : forall n (t1 t2 : T n), t1 <> t2 -> memb (Rsingle t1) t2 = 0.
7578

7679
Parameter p_ext : forall n, forall r s : R n, (forall t, memb r t = memb s t) -> r = s.
7780

Common.v

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Require Import Lists.List Lists.ListSet Vector Arith.PeanoNat AbstractRelation Omega.
2+
3+
4+
Fixpoint findpos {A} (l : list A) (p : A -> bool) start {struct l} : option nat :=
5+
match l with
6+
| List.nil => None
7+
| List.cons a l0 => if p a then Some start else findpos l0 p (S start)
8+
end.
9+
10+
Definition ret {A} : A -> option A := Some.
11+
Definition bind {A B} : option A -> (A -> option B) -> option B :=
12+
fun a f => match a with None => None | Some x => f x end.
13+
Definition monadic_map {A B} (f : A -> option B) (l : list A) : option (list B) :=
14+
List.fold_right (fun a mbl =>
15+
bind mbl (fun bl =>
16+
bind (f a) (fun b =>
17+
ret (b::bl))))
18+
(ret List.nil) l.
19+
20+
Lemma length_monadic_map (A B: Type) (f : A -> option B) (l1 : list A) :
21+
forall l2, monadic_map f l1 = Some l2 -> List.length l1 = List.length l2.
22+
elim l1.
23+
+ intro. simpl. unfold ret. intro. injection H. intro. subst. auto.
24+
+ simpl. intros a l. case (monadic_map f l).
25+
- simpl. case (f a).
26+
* simpl. unfold ret. intros b l0 IH l2 Hl2. injection Hl2. intro H.
27+
subst. simpl. apply f_equal. apply IH. reflexivity.
28+
* simpl. intros. discriminate.
29+
- simpl. intros. discriminate.
30+
Qed.
31+
32+
Lemma bind_elim (A B : Type) (x : option A) (f : A -> option B) (P : option B -> Prop) :
33+
(forall y, x = Some y -> P (f y)) -> (x = None -> P None) ->
34+
P (bind x f).
35+
intros. destruct x eqn:e; simpl; auto.
36+
Qed.
37+
38+
Lemma length_to_list (A : Type) (n : nat) (v : Vector.t A n) : length (to_list v) = n.
39+
elim v. auto.
40+
intros. transitivity (S (length (to_list t ))); auto.
41+
Qed.
42+
43+
Definition vec_monadic_map {A B n} (f : A -> option B) (v : Vector.t A n) : option (Vector.t B n).
44+
Proof.
45+
destruct (monadic_map f (Vector.to_list v)) eqn:e.
46+
+ refine (Some (cast (of_list l) _)).
47+
refine (let Hlen := length_monadic_map _ _ _ _ _ e in _).
48+
rewrite <- Hlen.
49+
apply length_to_list.
50+
+ apply None.
51+
Qed.
52+
53+
Lemma findpos_Some A (s : list A) p :
54+
forall m n, findpos s p m = Some n -> n < m + length s.
55+
Proof.
56+
elim s; simpl; intros; intuition.
57+
+ discriminate H.
58+
+ destruct (p a); intuition.
59+
- injection H0. omega.
60+
- pose (H _ _ H0). omega.
61+
Qed.
62+
63+
64+
Module Type DB.
65+
Parameter Name : Type.
66+
Hypothesis Name_dec : forall x y:Name, {x = y} + {x <> y}.
67+
Definition FullName := (Name * Name)%type.
68+
Definition FullVar := (nat * Name)%type.
69+
Definition Scm := list Name. (* schema (attribute names for a relation) *)
70+
Definition Ctx := list Scm. (* context (domain of an environment) = list of lists of names *)
71+
72+
Parameter BaseConst : Type.
73+
Definition Value := option BaseConst.
74+
Definition null : Value := None.
75+
Definition c_sem : BaseConst -> Value := Some. (* semantics of constants *)
76+
77+
Declare Module Rel : REL with Definition V := Value.
78+
79+
Parameter D : Type.
80+
Variable db_schema : D -> Name -> option (list Name).
81+
Hypothesis db_rel : forall d n xl, db_schema d n = Some xl -> Rel.R (List.length xl).
82+
Implicit Arguments db_rel [d n xl].
83+
84+
Lemma Scm_dec (s1 s2 : Scm) : {s1 = s2} + {s1 <> s2}.
85+
exact (list_eq_dec Name_dec s1 s2).
86+
Qed.
87+
88+
Definition Scm_eq : Scm -> Scm -> bool :=
89+
fun s1 s2 => match Scm_dec s1 s2 with left _ => true | right _ => false end.
90+
91+
Lemma Scm_eq_elim (P : bool -> Prop) (s s' : Scm) (Htrue : s = s' -> P true) (Hfalse : s <> s' -> P false)
92+
: P (Scm_eq s s').
93+
unfold Scm_eq. destruct (Scm_dec s s') eqn:e. all: auto.
94+
Qed.
95+
96+
97+
Lemma FullName_dec (A B : FullName) : {A = B} + {A <> B}.
98+
elim A; intros A1 A2.
99+
elim B; intros B1 B2.
100+
elim (Name_dec A1 B1); intro H1.
101+
+ elim (Name_dec A2 B2); intro H2.
102+
- subst. left. reflexivity.
103+
- right. intro. injection H. intro. contradiction H2.
104+
+ right. intro. injection H. intros _ H2. contradiction H1.
105+
Qed.
106+
107+
Definition FullName_eq : FullName -> FullName -> bool :=
108+
fun A B => match FullName_dec A B with left _ => true | right _ => false end.
109+
110+
Parameter c_eq : BaseConst -> BaseConst -> bool.
111+
Hypothesis BaseConst_dec : forall (c1 c2 : BaseConst), { c1 = c2 } + { c1 <> c2 }.
112+
Hypothesis BaseConst_eqb_eq : forall (c1 c2 : BaseConst), c_eq c1 c2 = true <-> c1 = c2.
113+
114+
End DB.
115+

0 commit comments

Comments
 (0)