Debian Codegraph @

src/Bedrock/Field/Common/Types.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import String.
3 From Coq Require Import List.
4 Require bedrock2.Syntax.
5 Require bedrock2.Semantics.
6 Require bedrock2.WeakestPrecondition.
7 Require Import bedrock2.Map.Separation.
8 Require Import bedrock2.Array bedrock2.Scalars.
9 Require Import coqutil.Map.Interface.
10 Require Import coqutil.Word.Interface.
11 Require Import Crypto.Language.API.
12 Import ListNotations. Local Open Scope Z_scope.
13 Import API.Compilers.
14
15 (*** This file contains the setup for the bedrock2 backend; the type
16 system, the parameter class, and some shorthand notations. ***)
17
18 (* Notations for commonly-used types in the fiat-crypto language *)
19 Module Import Notations.
20 Notation base_range := (base.type.type_base base.type.zrange).
21 Notation base_nat := (base.type.type_base base.type.nat).
22 Notation base_Z := (base.type.type_base base.type.Z).
23 Notation base_listZ := (base.type.list base_Z).
24 Notation base_range2 := (base.type.prod base_range base_range).
25 Notation base_ZZ := (base.type.prod base_Z base_Z).
26
27 Notation type_range := (type.base base_range).
28 Notation type_nat := (type.base base_nat).
29 Notation type_Z := (type.base base_Z).
30 Notation type_listZ := (type.base base_listZ).
31 Notation type_range2 := (type.base base_range2).
32 Notation type_ZZ := (type.base base_ZZ).
33 End Notations.
34
35 Class parameters
36 {width: Z} {BW: Bitwidth.Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}
37 {locals: map.map String.string word}
38 {ext_spec: bedrock2.Semantics.ExtSpec}
39 {varname_gen : nat -> String.string}
40 {error : Syntax.expr.expr} := parameters_sentinel : unit.
41
42 Section WithParameters.
43 Context
44 {width BW word mem locals ext_spec varname_gen error}
45 `{parameters_sentinel : @parameters
46 width BW word mem locals ext_spec varname_gen error}.
47 Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing).
48 Class ok {parameters_sentinel : parameters} :=
49 {
50 (* semantics_ok : Semantics.parameters_ok semantics *)
51 #[global] word_ok :: word.ok word;
52 #[global] mem_ok :: map.ok mem;
53 #[global] locals_ok :: map.ok locals;
54 #[global] ext_spec_ok :: Semantics.ext_spec.ok ext_spec;
55
56 varname_gen_unique :
57 forall i j : nat, varname_gen i = varname_gen j <-> i = j;
58 }.
59
60 Context {ok : ok}.
61 Lemma word_size_in_bytes_pos : 0 < Memory.bytes_per_word width.
62 Proof. destruct Bitwidth.width_cases as [H|H]; rewrite H; cbv; trivial. Qed.
63 Lemma width_0mod_8 : width mod 8 = 0.
64 Proof. destruct Bitwidth.width_cases as [H|H]; rewrite H; cbv; trivial. Qed.
65 End WithParameters.
66
67 Module rep.
68 Section rep.
69 Context
70 {width BW word mem locals ext_spec varname_gen error}
71 `{parameters_sentinel : @parameters
72 width BW word mem locals ext_spec varname_gen error}.
73 Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing).
74
75 Class rep {parameters_sentinel : parameters} (t : base.type) :=
76 { ltype : Type; (* type for LHS of assignment *)
77 rtype : Type; (* type for RHS of assignment *)
78 size : Type; (* amount of space taken in memory, if applicable *)
79 rtype_of_ltype : ltype -> rtype;
80 dummy_ltype : ltype;
81 make_error : rtype;
82 dummy_size : size;
83 varname_set : ltype -> PropSet.set string;
84 equiv : base.interp t -> rtype -> size -> locals -> mem -> Prop }.
85
86 (* store a list in local variables; each element of the list is
87 represented as a separate variable *)
88 Instance listZ_local {zrep : rep base_Z} : rep base_listZ :=
89 { ltype := list ltype;
90 rtype := list rtype;
91 size := size;
92 rtype_of_ltype := map rtype_of_ltype;
93 dummy_ltype := nil;
94 make_error := [make_error];
95 dummy_size := dummy_size;
96 varname_set :=
97 fold_right
98 (fun x s =>
99 PropSet.union (varname_set x) s) PropSet.empty_set;
100 equiv :=
101 fun (x : list Z) (y : list rtype) sz locals _ =>
102 Forall2 (fun a b => equiv a b sz locals map.empty) x y
103 }.
104
105 (* store a list in memory; the list is represented by one Z, which
106 is the location of the head of the list *)
107 Instance listZ_mem {zrep : rep base_Z} : rep base_listZ :=
108 { ltype := ltype;
109 rtype := rtype;
110 size := Syntax.access_size;
111 rtype_of_ltype := rtype_of_ltype;
112 dummy_ltype := dummy_ltype;
113 make_error := make_error;
114 dummy_size := Syntax.access_size.one;
115 varname_set := varname_set;
116 equiv :=
117 fun (x : list Z) (y : rtype) sz locals =>
118 Lift1Prop.ex1
119 (fun start : word =>
120 Lift1Prop.ex1
121 (fun ws : list word =>
122 let bytes :=
123 Z.of_nat (Memory.bytes_per (width:=width) sz) in
124 sep (map:=mem)
125 (sep
126 (emp (map word.unsigned ws = x /\
127 Forall
128 (fun z =>
129 (0 <= z < 2 ^ (bytes * 8))%Z)
130 x))
131 (fun mem : mem =>
132 equiv (word.unsigned start) y
133 (dummy_size (rep:=zrep))
134 locals mem))
135 (array (truncated_scalar sz)
136 (word.of_Z bytes) start
137 (map word.unsigned ws))))
138 }.
139
140 Instance Z : rep base_Z :=
141 { ltype := String.string;
142 rtype := Syntax.expr.expr;
143 size := unit;
144 rtype_of_ltype := Syntax.expr.var;
145 dummy_ltype := varname_gen 0%nat;
146 make_error := error;
147 dummy_size := tt;
148 varname_set := PropSet.singleton_set;
149 equiv :=
150 fun (x : Z) (y : Syntax.expr.expr) _ locals =>
151 Lift1Prop.ex1
152 (fun w : word =>
153 emp (word.unsigned w = x /\
154 WeakestPrecondition.dexpr
155 map.empty locals y w))
156 }.
157 End rep.
158 End rep.
159
160 Section defs.
161 Context
162 {width BW word mem locals ext_spec varname_gen error}
163 `{parameters_sentinel : @parameters
164 width BW word mem locals ext_spec varname_gen error}.
165 Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing).
166 Context
167 (* list representation -- could be local or in-memory *)
168 {listZ : rep.rep base_listZ}.
169 Existing Instance rep.Z.
170
171 (* Types that appear in the bedrock2 expressions on the left-hand-side of
172 assignments (or in return values). For example, if we want to assign three
173 integers, we need three strings.
174
175 Functions can't appear on the left-hand-side, so we return garbage output
176 (the unit type). *)
177 Fixpoint base_ltype (t : base.type) : Type :=
178 match t with
179 | base.type.prod a b => base_ltype a * base_ltype b
180 | base_listZ => rep.ltype (rep:=listZ)
181 | _ => rep.ltype (rep:=rep.Z)
182 end.
183 Definition ltype (t : type.type base.type) : Type :=
184 match t with
185 | type.base t => base_ltype t
186 | type.arrow s d => unit (* garbage *)
187 end.
188
189 (* Types that appear in the bedrock2 expressions on the right-hand-side of
190 assignments. For example, if we want to assign three integers, we need
191 three [Syntax.expr.expr]s. *)
192 Fixpoint base_rtype (t : base.type) : Type :=
193 match t with
194 | base.type.prod a b => base_rtype a * base_rtype b
195 | base_listZ => rep.rtype (rep:=listZ)
196 | _ => rep.rtype (rep:=rep.Z)
197 end.
198 Fixpoint rtype (t : type.type base.type) : Type :=
199 match t with
200 | type.base a => base_rtype a
201 | type.arrow a b => rtype a -> rtype b
202 end.
203
204 (* error creation *)
205 Fixpoint base_make_error t : base_rtype t :=
206 match t with
207 | base.type.prod a b => (base_make_error a, base_make_error b)
208 | base_listZ => rep.make_error
209 | _ => rep.make_error
210 end.
211 Fixpoint make_error t : rtype t :=
212 match t with
213 | type.base a => base_make_error a
214 | type.arrow a b => fun _ => make_error b
215 end.
216
217 (* These should only be used to fill holes in unreachable cases;
218 nothing about them should need to be proven *)
219 Fixpoint dummy_base_ltype (t : base.type) : base_ltype t :=
220 match t with
221 | base.type.prod a b => (dummy_base_ltype a, dummy_base_ltype b)
222 | base_listZ => rep.dummy_ltype
223 | _ => rep.dummy_ltype
224 end.
225 Definition dummy_ltype (t : API.type) : ltype t :=
226 match t with
227 | type.base a => dummy_base_ltype a
228 | type.arrow a b => tt
229 end.
230
231 (* convert ltypes to rtypes (used for renaming variables) - the opposite
232 direction is not permitted *)
233 Fixpoint base_rtype_of_ltype {t} : base_ltype t -> base_rtype t :=
234 match t with
235 | base.type.prod a b =>
236 fun x => (base_rtype_of_ltype (fst x),
237 base_rtype_of_ltype (snd x))
238 | base_listZ => rep.rtype_of_ltype
239 | _ => Syntax.expr.var
240 end.
241 Fixpoint rtype_of_ltype t
242 : ltype t -> rtype t :=
243 match t as t0 return ltype t0 -> rtype t0 with
244 | type.base b => base_rtype_of_ltype
245 | type.arrow a b =>
246 (* garbage; not a valid ltype *)
247 fun (_:unit) =>
248 fun (x : rtype a) =>
249 rtype_of_ltype b (dummy_ltype b)
250 end.
251
252 Fixpoint base_access_sizes t :=
253 match t with
254 | base.type.prod a b =>
255 (base_access_sizes a * base_access_sizes b)%type
256 | base_listZ => rep.size (rep:=listZ)
257 | _ => rep.size (rep:=rep.Z)
258 end.
259 Definition access_sizes t :=
260 match t with
261 | type.base b => base_access_sizes b
262 | _ => unit
263 end.
264
265 Fixpoint base_dummy_access_sizes t : base_access_sizes t :=
266 match t with
267 | base.type.prod a b =>
268 (base_dummy_access_sizes a, base_dummy_access_sizes b)
269 | base_listZ => rep.dummy_size
270 | _ => rep.dummy_size
271 end.
272 Definition dummy_access_sizes t : access_sizes t :=
273 match t with
274 | type.base b => base_dummy_access_sizes b
275 | _ => tt
276 end.
277 Fixpoint dummy_access_sizes_args t :
278 type.for_each_lhs_of_arrow access_sizes t :=
279 match t with
280 | type.base _ => tt
281 | type.arrow s d =>
282 (dummy_access_sizes s, dummy_access_sizes_args d)
283 end.
284
285 Definition baseonly (f : base.type -> Type) t : Type :=
286 match t with
287 | type.base b => f b
288 | type.arrow _ _ => unit
289 end.
290
291 (* Types for partitioning return values into list and non-list *)
292 Section ListOnlyListExcl.
293 Fixpoint base_listonly (T : Type) t : Type :=
294 match t with
295 | base.type.prod a b =>
296 (base_listonly T a * base_listonly T b)
297 | base_listZ => T
298 | _ => unit
299 end.
300 Fixpoint base_listexcl (f : base.type -> Type) t : Type :=
301 match t with
302 | base.type.prod a b =>
303 base_listexcl f a * base_listexcl f b
304 | base_listZ => unit
305 | _ => f t
306 end.
307 Definition listonly T := baseonly (base_listonly T).
308 Definition listexcl f := baseonly (base_listexcl f).
309
310 Definition list_lengths (t : API.type) :=
311 listonly nat t.
312 Definition listonly_base_ltype t :=
313 base_listonly (base_ltype base_listZ) t.
314 Definition listexcl_base_ltype t :=
315 base_listexcl base_ltype t.
316 Definition listonly_base_rtype t :=
317 base_listonly (base_rtype base_listZ) t.
318 Definition listexcl_base_rtype t :=
319 base_listexcl base_rtype t.
320
321 Fixpoint map_listonly {A B t} (f : A -> B)
322 : base_listonly A t -> base_listonly B t :=
323 match t as t0 return
324 base_listonly A t0 -> base_listonly B t0 with
325 | base.type.prod a b =>
326 fun x =>
327 (map_listonly f (fst x), map_listonly f (snd x))
328 | base_listZ => f
329 | _ => fun _ => tt
330 end.
331 Fixpoint map_listexcl {t}
332 {f g : base.type -> Type}
333 (F : forall t, f t -> g t)
334 : base_listexcl f t -> base_listexcl g t :=
335 match t as t0 return
336 base_listexcl f t0 -> base_listexcl g t0 with
337 | base.type.prod a b =>
338 fun x =>
339 (map_listexcl F (fst x), map_listexcl F (snd x))
340 | base_listZ => fun _ => tt
341 | _ => F _
342 end.
343 End ListOnlyListExcl.
344 End defs.