Skip to content

Commit 5af476f

Browse files
author
Louis Cheung
committed
Added more comments to the sum-example
1 parent 9d9f56d commit 5af476f

File tree

8 files changed

+385
-330
lines changed

8 files changed

+385
-330
lines changed

cogent/examples/cpp2022-artefact/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ARTEFACT
22
---
3-
This is an artefact for "Overcoming Restraint: Composing Verification of Foreign Functions with Cogent"
3+
This is an artefact for "Overcoming Restraint: Composing Verification of Foreign Functions with Cogent" ([published](https://doi.org/10.1145/3497775.3503686))
44

55
This formalisation works with [Isabelle2019](https://isabelle.in.tum.de/website-Isabelle2019/index.html),
66
[AutoCorres version 1.6.1](https://trustworthy.systems/projects/TS/autocorres/) and
@@ -10,6 +10,9 @@ The work that is being presented are all the files in **loops**, **sum-example**
1010

1111
Note that for MacOS users, some of the make files depend on the GNU version of **sed**, i.e. **gsed** on MacOS.
1212

13+
The top level make file only generates the C code and theory files for the loop and sum example.
14+
This is just to keep the GitHub build happy.
15+
1316
## Install Isabelle2019
1417

1518
Follow the instructions for installing.
@@ -27,6 +30,11 @@ Set the environment variable **COGENT_DIR** to be the top level directory of Cog
2730

2831
## Sum Example
2932

33+
The sum example was the first completed attempt at composing verification of foreign functions and abstract types with Cogent functions and types.
34+
This is the reason why some of the approaches and proofs have not been optimised,
35+
the word arrays in this example can only contain primitive numeric types,
36+
and that the update to C refinement is only for 32-bit word arrays.
37+
3038
### Important Files
3139

3240
* _Sum.cogent_: Contains the Cogent program for sum and other functions.
@@ -65,6 +73,14 @@ To run the example, go into the **sum-example** folder and use the command:
6573

6674
## Loops Example
6775

76+
The loops example drew from the lessons learnt from the sum example, and hence is much more polished, reusable,
77+
and resilient to changes made to the Cogent program.
78+
Some notable differences are:
79+
80+
* The value typing relation for arrays is defined for all non-heap element types.
81+
* The update to C refinement proof for the word array operations and generalise loop occurs on a polymorphic monadic Isabelle shallow embeddings.
82+
This means that it does not need to undergo alpha renaming when used by other Cogent programs.
83+
6884
### Important Files
6985

7086
* _repeat.cogent_: Cogent declarations for the higher order polymorphic loop.

cogent/examples/cpp2022-artefact/sum-example/WordArray_Abstractions.thy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ locale WordArray = main_pp_inferred begin
163163
UWA (TPrim (Num t)) _ _ \<Rightarrow> (''WordArray'', [RPrim (Num t)])
164164
| _ \<Rightarrow> (''Unknown Abstract Type'', [])"
165165

166+
text "Definition 3.2"
166167
definition "wa_abs_typing_u \<Xi>' a name \<tau>s sig (r :: ptrtyp set) (w :: ptrtyp set) \<sigma> \<equiv>
167168
(case a of
168169
UWA (TPrim (Num t)) len arr \<Rightarrow> name = ''WordArray'' \<and> \<tau>s = [TPrim (Num t)] \<and> sig \<noteq> Unboxed \<and>
@@ -174,12 +175,14 @@ locale WordArray = main_pp_inferred begin
174175
unat (size_of_num_type t) * unat len \<le> unat (max_word :: ptrtyp)
175176
| _ \<Rightarrow> name = ''Unknown Abstract Type'' \<and> \<tau>s = [] \<and> r = {} \<and> w = {} \<and> sig = Unboxed)"
176177

178+
text "Definition 3.1"
177179
definition "wa_abs_typing_v \<Xi>' a name \<tau>s \<equiv>
178180
(case a of
179181
VWA (TPrim (Num t)) xs \<Rightarrow> name = ''WordArray'' \<and> \<tau>s = [TPrim (Num t)] \<and>
180182
(\<forall>i < length xs. \<exists>x. xs ! i = VPrim x \<and> lit_type x = Num t)
181183
| _ \<Rightarrow> name = ''Unknown Abstract Type'' \<and> \<tau>s = [])"
182184

185+
text "Definition 3.5"
183186
definition "wa_abs_upd_val \<Xi>' au av name \<tau>s sig (r :: ptrtyp set) (w :: ptrtyp set) \<sigma> \<equiv>
184187
wa_abs_typing_u \<Xi>' au name \<tau>s sig r w \<sigma> \<and> wa_abs_typing_v \<Xi>' av name \<tau>s \<and>
185188
(case au of
@@ -271,6 +274,8 @@ end
271274

272275
section "Sublocale Proof"
273276

277+
text "Discharging the abstract type requirements (Definition 2.12)"
278+
274279
sublocale WordArray \<subseteq> update_sem wa_abs_typing_u wa_abs_repr
275280
apply (unfold wa_abs_repr_def[abs_def] wa_abs_typing_v_def[abs_def] wa_abs_typing_u_def[abs_def] wa_abs_upd_val_def[abs_def])
276281
apply (unfold_locales; clarsimp split: atyp.splits type.splits prim_type.splits)

cogent/examples/cpp2022-artefact/sum-example/WordArray_CorresProof_Asm.thy

Lines changed: 200 additions & 189 deletions
Large diffs are not rendered by default.

cogent/examples/cpp2022-artefact/sum-example/WordArray_SVCorres.thy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ theory WordArray_SVCorres
66
imports WordArray_Shallow WordArray_VAbsFun
77
begin
88

9-
subsection "Shallow Word Array Value Relation"
9+
subsection "Shallow Word Array Value Relation (Definition 3.4)"
1010
text
1111
"The shallow embedding of a word array is a list of words. Currently we only support word
1212
arrays with primitive words as values. In the future, we hope to extend this to elements which
@@ -69,7 +69,7 @@ end
6969
context WordArray begin
7070

7171

72-
section "Shallow to Deep Corresondence Lemmas (@{term scorres}) for Word Array Functions"
72+
section "Shallow to Deep Corresondence Lemmas (@{term scorres}) for Word Array Functions (Theorem 2.9)"
7373

7474
lemmas valRel_WordArray_simps = valRel_WordArrayUX
7575

cogent/examples/cpp2022-artefact/sum-example/WordArray_Shallow.thy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fun myslice :: "nat \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> 'a lis
99
where
1010
"myslice frm to xs = List.take (to - frm) (List.drop frm xs)"
1111

12-
section "Shallow Word Array Function Definitions"
12+
section "Shallow Word Array Function Definitions (Figure 4)"
1313

1414
overloading
1515
wordarray_put2' \<equiv> wordarray_put2

cogent/examples/cpp2022-artefact/sum-example/WordArray_UpdCCorres.thy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ theory WordArray_UpdCCorres
55
imports WordArray_UAbsFun
66
begin
77

8+
section "Helper definition and lemmas"
9+
810
context update_sem_init begin
911

1012
definition
@@ -56,8 +58,9 @@ sublocale WordArray \<subseteq> Generated _ wa_abs_typing_u wa_abs_repr
5658

5759
context WordArray begin
5860

59-
section "Correspondence Lemmas Between Update Semantics and C"
61+
section "Refinement from Update Semantics and C (Theorem 2.4)"
6062

63+
(* Theorem 3.6 *)
6164
lemma upd_C_wordarray_put2_corres_gen:
6265
"\<And>i \<gamma> v' \<Gamma>' \<sigma> st.
6366
\<lbrakk>i < length \<gamma>; val_rel (\<gamma> ! i) v'; \<Gamma>' ! i = option.Some (prod.fst (prod.snd (\<Xi> ''wordarray_put2_0'')));

cogent/examples/cpp2022-artefact/sum-example/WordArray_Update.thy

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ theory WordArray_Update
33

44
begin
55

6+
section "Word array method embeddings (Figure 6a)"
7+
68
type_synonym ('f, 'a, 'l) ufoldmapdef = "('f, 'a, 'l) uabsfuns \<Rightarrow> ('f, 'a, 'l) store \<Rightarrow>
79
'l \<Rightarrow> 32 word \<Rightarrow> 32 word \<Rightarrow> 'f expr \<Rightarrow>
810
type \<Rightarrow> ('f, 'a, 'l) uval \<Rightarrow> type \<Rightarrow> ('f, 'a, 'l) uval \<Rightarrow>
@@ -113,6 +115,8 @@ definition upd_wa_mapAccumnb
113115
(\<Xi>', [], [option.Some \<tau>i] \<turnstile> (App (uvalfun_to_exprfun func) (Var 0)) : \<tau>o) \<and>
114116
upd_wa_mapAccumnb_bod \<xi>\<^sub>u y1 p frm to (uvalfun_to_exprfun func) u acc v obsv z))"
115117

118+
section "Helper lemmas"
119+
116120
context update_sem begin
117121
lemma discardable_or_shareable_not_writable:
118122
assumes "D \<in> k \<or> S \<in> k"
@@ -143,13 +147,13 @@ and "u \<inter> r = {} \<Longrightarrow> r \<inter> u' = {}"
143147
simp: frame_def
144148
dest: abs_typing_valid)
145149

146-
147150
end (* of context *)
148151

152+
section "Type preservation and frame relation theorems (Theorem 2.2)"
149153

150154
context WordArray begin
151155

152-
section wordarray_length
156+
subsection wordarray_length
153157

154158
lemma upd_wa_length_preservation:
155159
"\<lbrakk>uval_typing \<Xi>' \<sigma> v (TCon ''WordArray'' [t] (Boxed ReadOnly ptrl)) r w;
@@ -161,7 +165,7 @@ lemma upd_wa_length_preservation:
161165
apply (clarsimp simp: frame_def intro!: u_t_prim')
162166
done
163167

164-
section wordarray_get
168+
subsection wordarray_get
165169

166170
lemma upd_wa_get_preservation:
167171
"\<lbrakk>uval_typing \<Xi>' \<sigma> v (TRecord [(a, TCon ''WordArray'' [t] (Boxed ReadOnly ptrl), Present),
@@ -188,7 +192,7 @@ lemma upd_wa_get_preservation:
188192
apply (case_tac ta; clarsimp intro!: u_t_prim')
189193
done
190194

191-
section wordarray_put2
195+
subsection wordarray_put2
192196

193197
lemma upd_wa_put2_preservation:
194198
"\<lbrakk>uval_typing \<Xi>' \<sigma> v (TRecord [(a, TCon ''WordArray'' [t] (Boxed Writable ptrl), Present),
@@ -237,7 +241,7 @@ lemma upd_wa_put2_preservation:
237241
apply (rule conjI; clarsimp)
238242
done
239243

240-
section wordarray_fold_no_break
244+
subsection wordarray_fold_no_break
241245

242246
lemma upd_wa_foldnb_bod_to_geq_len:
243247
"\<lbrakk> proc_ctx_wellformed \<Xi>';
@@ -736,7 +740,7 @@ lemma upd_wa_foldnb_bod_preservation:
736740
apply (rule frame_trans; simp)
737741
done
738742

739-
section wordarray_map_no_break
743+
subsection wordarray_map_no_break
740744

741745
lemma upd_wa_mapAccumnb_bod_to_geq_len:
742746
"\<lbrakk> proc_ctx_wellformed \<Xi>';

0 commit comments

Comments
 (0)