Skip to content

Commit ffb6cea

Browse files
committed
move interpretation of operations to a typeclass,
notations for judgements moved into modules, tentative rule for operations
1 parent 40b661a commit ffb6cea

2 files changed

Lines changed: 85 additions & 21 deletions

File tree

theories/Crypt/semantics/binary.v

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,25 @@ Section Def.
9595
Definition stsubdistr_rel [A1 A2 : choiceType] : M A1 -> M A2 -> Type :=
9696
fun d1 d2 => forall map1 map2, { d | d ~[ d1 map1 , d2 map2 ] }.
9797

98-
Context (eval_op : forall o, src o -> M (tgt o)).
99-
100-
Let eval [A] := eval eval_op (A:=A).
98+
Context `{EvalOp}.
10199

102100
Definition prerelation := rel heap.
103101
Definition postrelation A1 A2 := (A1 * heap) -> pred (A2 * heap).
104102

105-
Definition binary_judgement [A1 A2 : choiceType]
103+
Definition relspec_valid [A1 A2 : choiceType]
106104
(pre : prerelation)
107-
(c1 : C A1) (c2 : C A2)
105+
(m1 : M A1) (m2 : M A2)
108106
(post : postrelation A1 A2) :=
109107
forall map1 map2, pre map1 map2 ->
110-
exists d, d ~[eval c1 map1, eval c2 map2] /\
108+
exists d, d ~[m1 map1, m2 map2] /\
111109
forall p1 p2, d (p1,p2) <> 0%R -> post p1 p2.
112110

111+
Definition binary_judgement [A1 A2 : choiceType]
112+
(pre : prerelation)
113+
(c1 : C A1) (c2 : C A2)
114+
(post : postrelation A1 A2) :=
115+
relspec_valid pre (eval c1) (eval c2) post.
116+
113117

114118
(* Bindings in the pre/postcondition looks annoying here *)
115119
Notation "⊨ ⦃ pre ⦄ c1 ≈ c2 ⦃ post ⦄" :=
@@ -180,7 +184,7 @@ Section Def.
180184
set df' := fun x => bool_depelim _ (P x) (fun hx => df (exist _ x hx))
181185
(fun _ => dnull).
182186
exists (bind_coupling dm df'); split.
183-
+ rewrite /eval 2!eval_bind /Def.bind.
187+
+ rewrite 2!eval_bind /Def.bind.
184188
apply: bind_coupling_partial_prop=> //.
185189
move=> p1' p2' /(hpostm _ _).
186190
move: (hpostmpref p1' p2')=> /implyP/[apply] hpref.
@@ -194,3 +198,11 @@ Section Def.
194198
Qed.
195199

196200
End Def.
201+
202+
Module BinaryNotations.
203+
Notation "⊨ ⦃ pre ⦄ c1 ≈ c2 ⦃ post ⦄" :=
204+
(binary_judgement pre c1 c2 post) : Rules_scope.
205+
Open Scope Rules_scope.
206+
End BinaryNotations.
207+
208+

theories/Crypt/semantics/unary.v

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ Proof.
7171
intros. subst. reflexivity.
7272
Qed.
7373

74+
Lemma bool_depelim_false :
75+
∀ (X : Type) (b : bool) (htrue : b = true → X) (hfalse : b = false → X)
76+
(e : b = false),
77+
bool_depelim X b htrue hfalse = hfalse e.
78+
Proof.
79+
intros. subst. reflexivity.
80+
Qed.
81+
7482
(* helper lemma for multiplication of reals *)
7583
Lemma R_no0div : ∀ (u v : R), (u * v ≠ 0 → u ≠ 0 ∧ v ≠ 0)%R.
7684
Proof.
@@ -223,21 +231,23 @@ Arguments bind [_ _ _] _ _.
223231

224232
End Def.
225233

234+
Class EvalOp :=
235+
eval_interface_operations : forall o, src o -> Def.stsubdistr heap_choiceType (tgt o).
226236

227237
(** Semantic evaluation of code and commands *)
228238
Section Evaluation.
229239
#[local]
230240
Notation M := (Def.stsubdistr heap_choiceType).
231241

232242
(** Assume an interpretation of operations *)
233-
Context (eval_op : ∀ o, src o → M (tgt o)).
243+
Context `{EvalOp}.
234244

235245
Definition eval [A : choiceType] : raw_code A → M A.
236246
Proof.
237247
elim.
238248
- apply: Def.ret.
239249
- intros o x k ih.
240-
exact (Def.bind (eval_op o x) ih).
250+
exact (Def.bind (eval_interface_operations o x) ih).
241251
- intros l k ih.
242252
exact (λ map, let v := get_heap map l in ih v map).
243253
- intros l v k ih.
@@ -252,8 +262,10 @@ Section Evaluation.
252262
Definition precondition := pred heap.
253263
Definition postcondition A := pred (A * heap).
254264

255-
Definition unary_judgement [A : choiceType] (pre : precondition) (c : raw_code A) (post : postcondition A) : Prop :=
256-
forall map, pre map -> forall p, (eval c map p <> 0)%R -> post p.
265+
Definition spec_valid [A : choiceType] (pre : precondition) (m : M A) (post : postcondition A) : Prop :=
266+
forall map, pre map -> forall p, (m map p <> 0)%R -> post p.
267+
268+
Definition unary_judgement [A : choiceType] (pre : precondition) (c : raw_code A) (post : postcondition A) : Prop := spec_valid pre (eval c) post.
257269

258270
Declare Scope Rules_scope.
259271

@@ -336,6 +348,18 @@ Section Evaluation.
336348

337349
End Evaluation.
338350

351+
Module UnaryNotations.
352+
Notation "⊨ ⦃ pre ⦄ c ⦃ p , post ⦄" :=
353+
(unary_judgement pre c (fun p => post))
354+
(p pattern) : Rules_scope.
355+
Open Scope Rules_scope.
356+
End UnaryNotations.
357+
358+
359+
(** Default interpretation for operations *)
360+
Definition eval_op_null : EvalOp :=
361+
fun o _ => Def.stsubnull _ _.
362+
339363

340364
Section ScopedImportEval.
341365
#[local] Open Scope fset.
@@ -349,15 +373,43 @@ Section ScopedImportEval.
349373
(** Taking an interpretation of the imported operation as assumption *)
350374
Context (import_eval : ∀ o, o \in import → src o → M (tgt o)).
351375

352-
Definition eval_op : forall o, src o -> M (tgt o) :=
353-
fun o => bool_depelim _ (o \in import) (fun oval => import_eval o oval) (fun _ _ => Def.stsubnull _ _).
354-
355-
Let eval := (eval eval_op).
356-
357-
(* Problem: the notation for the judgement is parametrized by eval, how can I
358-
have access to this notation instantiated with eval without redefining it ?
359-
Use a functor ?*)
360-
361-
(* TODO: Add spec and Rule for imported operations *)
376+
#[local]
377+
Instance eval_op : EvalOp :=
378+
fun o => bool_depelim _ (o \in import)
379+
(fun oval => import_eval o oval)
380+
(fun _ _ => Def.stsubnull _ _).
381+
382+
Import UnaryNotations.
383+
384+
Context (op_precondition : forall o, o \in import -> src o -> precondition)
385+
(op_postcondition : forall o, o \in import -> src o -> postcondition (tgt o))
386+
(import_eval_valid : forall o (oval : o \in import) (s : src o),
387+
spec_valid (op_precondition o oval s)
388+
(import_eval o oval s)
389+
(op_postcondition o oval s)).
390+
391+
Definition op_pre : forall o, src o -> precondition :=
392+
fun o s => bool_depelim _ (o \in import)
393+
(fun oval => op_precondition o oval s)
394+
(fun _ => pred0).
395+
396+
Definition op_post : forall o, src o -> postcondition (tgt o) :=
397+
fun o s => bool_depelim _ (o \in import)
398+
(fun oval => op_postcondition o oval s)
399+
(fun _ => predT).
400+
401+
Lemma opr_rule [A : choiceType] (post : postcondition A) :
402+
forall o (s : src o) (k : tgt o -> raw_code A)
403+
(spec_k : forall r, ⊨ ⦃ [pred map | op_post o s (r, map)] ⦄ k r ⦃ p , post p ⦄),
404+
⊨ ⦃ op_pre o s ⦄ opr o s k ⦃ p , post p ⦄.
405+
Proof.
406+
move=> o s k; case oval: (o \in import); rewrite /op_pre /op_post.
407+
2: move=> _; by rewrite bool_depelim_false.
408+
rewrite !bool_depelim_true // => spec_k map hpre p /=.
409+
move=> /Def.bind_not_null [[r map0] []].
410+
rewrite /eval_interface_operations/eval_op bool_depelim_true=> ??.
411+
apply: (spec_k r map0)=> //=.
412+
apply: import_eval_valid; first apply: hpre; move => //.
413+
Qed.
362414

363415
End ScopedImportEval.

0 commit comments

Comments
 (0)