-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guard.v
223 lines (203 loc) · 9.71 KB
/
Guard.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
Require Import Kami.Syntax Kami.Notations.
Set Asymmetric Patterns.
Set Implicit Arguments.
Section ty.
Variable ty: Kind -> Type.
Definition boolTy (k: Kind) := bool.
Fixpoint goodDfExpr k (e: Expr boolTy k) {struct e}: bool.
refine
match e with
| Var k v => match k return fullType boolTy k -> bool with
| SyntaxKind k' => fun v => v
| _ => fun _ => true
end v
| Const k c => true
| UniBool op e => @goodDfExpr _ e
| CABool op es => forallb (@goodDfExpr _) es
| UniBit n1 n2 op e => @goodDfExpr _ e
| CABit n op es => forallb (@goodDfExpr _) es
| BinBit n1 n2 n3 op e1 e2 => (@goodDfExpr _ e1) && (@goodDfExpr _ e2)
| BinBitBool n1 n2 op e1 e2 => (@goodDfExpr _ e1) && (@goodDfExpr _ e2)
| ITE k p e1 e2 => (@goodDfExpr _ p) && (@goodDfExpr _ e1) && (@goodDfExpr _ e2)
| Eq k e1 e2 => (@goodDfExpr _ e1) && (@goodDfExpr _ e2)
| ReadStruct n sk e i => (@goodDfExpr _ e)
| ReadArray n m k e i => (@goodDfExpr _ e) && (@goodDfExpr _ i)
| ReadArrayConst n k e i => (@goodDfExpr _ e)
| BuildArray n k fv => forallb (fun i => @goodDfExpr _ (fv i)) (getFins n)
| BuildStruct n fsk fv => forallb (fun i => @goodDfExpr _ (fv i)) (getFins n)
| Kor k es => forallb(@goodDfExpr _ ) es
| ToNative _ e => goodDfExpr _ e
| FromNative _ e => goodDfExpr _ e
end.
Defined.
Fixpoint goodDfAction lret (a: ActionT boolTy lret) :=
match a with
| MCall name sig arg cont => goodDfAction (cont true)
| LetExpr k e cont =>
match k return Expr boolTy k -> (fullType boolTy k -> ActionT boolTy lret) -> bool with
| SyntaxKind k' => fun e cont => goodDfAction (cont (goodDfExpr e))
| _ => fun e cont => false
end e cont
| LetAction k a cont =>
goodDfAction a && (goodDfAction (cont true))
| ReadNondet k cont =>
match k return (fullType boolTy k -> ActionT boolTy lret) -> bool with
| SyntaxKind k' => fun cont => goodDfAction (cont false)
| _ => fun cont => false
end cont
| ReadReg name k cont =>
match k return (fullType boolTy k -> ActionT boolTy lret) -> bool with
| SyntaxKind k' => fun cont => goodDfAction (cont true)
| _ => fun cont => false
end cont
| WriteReg name k e cont =>
goodDfAction cont
| IfElse p k a1 a2 cont =>
goodDfExpr p && goodDfAction a1 && goodDfAction a2 &&
goodDfAction (cont true)
| Sys _ cont => goodDfAction cont
| Return e => (* goodDfExpr e *) true
end.
Local Open Scope kami_expr.
Local Open Scope kami_action.
Section aggressive.
Variable isAgg: bool.
Fixpoint getGuard lret (a: ActionT ty lret): ActionT ty Bool :=
match a with
| MCall name sig arg cont =>
Call gmeth: Bool <- (name ++ ".guard")(arg: _) ;
LET dummy <- Const _ (getDefaultConst _);
LETA grest <- getGuard (cont dummy);
Ret (#gmeth && #grest)
| LetExpr k e cont =>
LetExpr e (fun x => getGuard (cont x))
| LetAction k a cont =>
LETA ga <- getGuard a;
LET dummy <- Const _ (getDefaultConst _);
LETA grest <- getGuard (cont dummy);
Ret (#ga && #grest)
| ReadNondet k cont =>
ReadNondet k (fun x => getGuard (cont x))
| ReadReg name k cont =>
ReadReg name k (fun x => getGuard (cont x))
| WriteReg name k e cont =>
getGuard cont
| IfElse p k a1 a2 cont =>
LETA g1 <- getGuard a1;
LETA g2 <- getGuard a2;
LET dummy <- Const _ (getDefaultConst _);
LETA grest <- getGuard (cont dummy);
Ret (if isAgg then (IF p then #g1 else #g2) && #grest else #g1 && #g2 && #grest)
| Sys _ cont => getGuard cont
| Return e => Ret ($$ true)
end.
Definition addGuardGen lret (a: ActionT ty lret) :=
LETA g <- getGuard a;
If #g then a else Ret ($$ (getDefaultConst lret)) as sth;
Ret #sth.
End aggressive.
End ty.
Definition addGuardMeth (f: DefMethT) :=
(fst f, existT MethodT (projT1 (snd f))
(fun ty x =>
addGuardGen (goodDfAction (projT2 (snd f) boolTy true))
(projT2 (snd f) ty x))).
Definition MethodGuardT (sig: Signature) :=
forall ty : Kind -> Type, (ty (fst sig) -> ActionT ty (snd sig)) *
(ty (fst sig) -> ActionT ty Bool).
Definition DefMethGuardT := Attribute (sigT MethodGuardT).
Definition addGuardRule (ra: Attribute (Action Void)) :=
(fst ra, fun ty => addGuardGen (goodDfAction (snd ra boolTy)) (snd ra ty)).
Definition addGuardMethWithGuard (f: DefMethGuardT): (DefMethT * DefMethT) :=
(addGuardMeth (fst f, existT MethodT
(projT1 (snd f))
(fun ty => fst (projT2 (snd f) ty))),
addGuardMeth ((fst f ++ ".guard")%string,
existT MethodT
(fst (projT1 (snd f)), Bool)
(fun ty => snd (projT2 (snd f) ty)))).
(* IGNORE THE REST *)
(* Section ty. *)
(* Variable ty: Kind -> Type. *)
(* Definition optTy := (fun k => option (ty k)). *)
(* Definition liftSome A B (f: A -> B) (e: option A) := match e with *)
(* | Some x => Some (f x) *)
(* | None => None *)
(* end. *)
(* Fixpoint liftSomes A (ls: list (option A)) := *)
(* match ls with *)
(* | Some x :: xs => liftSome (cons x) (liftSomes xs) *)
(* | None :: xs => None *)
(* | nil => Some nil *)
(* end. *)
(* Definition liftSome2 A B C (f: A -> B -> C) (e1: option A) (e2: option B) *)
(* := match e1, e2 with *)
(* | Some x, Some y => Some (f x y) *)
(* | _, _ => None *)
(* end. *)
(* Definition liftSome3 A B C D (f: A -> B -> C -> D) (e1: option A) (e2: option B) (e3: option C) *)
(* := match e1, e2, e3 with *)
(* | Some x, Some y, Some z => Some (f x y z) *)
(* | _, _, _ => None *)
(* end. *)
(* Fixpoint exprForGuard k (e: Expr optTy k) {struct e}: option (Expr ty k). *)
(* refine *)
(* match e with *)
(* | Var k v => match k return fullType optTy k -> option (Expr ty k) with *)
(* | SyntaxKind k' => fun v => match v with *)
(* | None => None *)
(* | Some x => Some (Var _ (SyntaxKind k') x) *)
(* end *)
(* | NativeKind t c => fun v => Some (Var ty (NativeKind c) v) *)
(* end v *)
(* | Const k c => Some (Const _ c) *)
(* | UniBool op e => liftSome (UniBool op) (@exprForGuard _ e) *)
(* | CABool op es => liftSome (CABool op) (liftSomes (map (@exprForGuard _) es)) *)
(* | UniBit n1 n2 op e => liftSome (UniBit op) (@exprForGuard _ e) *)
(* | CABit n op es => liftSome (CABit op) (liftSomes (map (@exprForGuard _) es)) *)
(* | BinBit n1 n2 n3 op e1 e2 => liftSome2 (BinBit op) (@exprForGuard _ e1) *)
(* (@exprForGuard _ e2) *)
(* | BinBitBool n1 n2 op e1 e2 => liftSome2 (BinBitBool op) (@exprForGuard _ e1) *)
(* (@exprForGuard _ e2) *)
(* | ITE k p e1 e2 => liftSome3 (@ITE _ k) (@exprForGuard _ p) *)
(* (@exprForGuard _ e1) (@exprForGuard _ e2) *)
(* | Eq k e1 e2 => liftSome2 (@Eq _ k) (@exprForGuard _ e1) (@exprForGuard _ e2) *)
(* | ReadStruct n fk fs e i => liftSome (fun e => ReadStruct e i) (@exprForGuard _ e) *)
(* | ReadArray n m k e i => liftSome2 (@ReadArray ty n m k) *)
(* (@exprForGuard _ e) (@exprForGuard _ i) *)
(* | ReadArrayConst n k e i => liftSome (fun e => @ReadArrayConst ty n k e i) *)
(* (@exprForGuard _ e) *)
(* | BuildArray n k fv => _ *)
(* | BuildStruct n fk fs fv => _ *)
(* end. *)
(* - refine (match _ with *)
(* | None => None *)
(* | Some fv' => Some (BuildStruct fk fs fv') *)
(* end). *)
(* pose proof (fun i => @exprForGuard _ (fv i)) as sth. *)
(* induction n. *)
(* + exact (Some (fun i => Fin.case0 (fun i => Expr ty (SyntaxKind (fk i))) i)). *)
(* + refine (match sth Fin.F1, IHn (fun i => fk (Fin.FS i)) *)
(* (fun i => fs (Fin.FS i)) *)
(* (fun i => fv (Fin.FS i)) *)
(* (fun i => sth (Fin.FS i))with *)
(* | Some x, Some y => Some (fun i => fin_case *)
(* i *)
(* (fun i => Expr ty (SyntaxKind (fk i))) x y) *)
(* | _, _ => None *)
(* end). *)
(* - refine (match _ with *)
(* | None => None *)
(* | Some fv' => Some (BuildArray fv') *)
(* end). *)
(* pose proof (fun i => @exprForGuard _ (fv i)) as sth. *)
(* induction n. *)
(* + exact (Some (fun i => Fin.case0 (fun i => Expr ty (SyntaxKind k)) i)). *)
(* + refine (match sth Fin.F1, IHn (fun i => fv (Fin.FS i)) (fun i => sth (Fin.FS i)) with *)
(* | Some x, Some y => Some (fun i => fin_case *)
(* i *)
(* (fun i => Expr ty (SyntaxKind k)) x y) *)
(* | _, _ => None *)
(* end). *)
(* Defined. *)
(* End ty. *)