Skip to content

Commit a5f5bf6

Browse files
authored
Use implicit parameters in the generated code (#333)
* Update the lean toolchain * Start computing which type parameters should be implicit/explicit * Update the Primitives.fst file * Update the Lean library * Fix an issue with builtin definitions * Update the computation of the implicit parameters * Update the extraction * Update the extraction of types, trait refs and trait decl refs * Update the extraction of globals * Regenerate some of the Lean files * Update the extraction of aggregated arrays * Update more Lean tests * Update the F* test files * Update the test proofs * Update the Primitives.v file * Regenerate the Coq files * Update the Primitives.v file * Fix minor mistakes * Update the README * Update the .gitignore * Fix minor issues * Fix a mistake in Primitives.fst * Fix more issues * Fix an issue with the Coq projectors * Reformat the code
1 parent 3f74062 commit a5f5bf6

File tree

95 files changed

+4023
-3513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+4023
-3513
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ tests/fstar/misc/obj/
5959
*.d
6060
*Makefile.coq
6161
*CoqMakefile.conf
62+
*_CoqProject*
6263

6364
# HOL4
6465
.HOLMK

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ You need to install OCaml, together with some packages.
3535
We suggest you to follow those [instructions](https://ocaml.org/docs/install.html),
3636
and install OPAM on the way (same instructions).
3737

38-
We use **OCaml 4.13.1**: `opam switch create 4.13.1+options`
38+
Any recent version of **OCaml 4** should do. For instance, if you want to use OCaml
39+
4.14.2:
40+
```
41+
opam switch create 4.14.2
42+
```
3943

40-
The dependencies can then be installed with the following command:
44+
You can then install the dependencies with the following command:
4145

4246
```
4347
opam install ppx_deriving visitors easy_logging zarith yojson core_unix odoc \

backends/coq/Primitives.v

+78-78
Large diffs are not rendered by default.

backends/fstar/Primitives.fst

+79-79
Large diffs are not rendered by default.

backends/lean/Base/Primitives/Alloc.lean

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ namespace boxed -- alloc.boxed
1111

1212
namespace Box -- alloc.boxed.Box
1313

14-
def deref (T : Type) (x : T) : Result T := ok x
15-
def deref_mut (T : Type) (x : T) : Result (T × (T → Result T)) := ok (x, λ x => ok x)
14+
def deref {T : Type} (x : T) : Result T := ok x
15+
def deref_mut {T : Type} (x : T) : Result (T × (T → Result T)) := ok (x, λ x => ok x)
1616

1717
/-- Trait instance -/
1818
def coreopsDerefInst (Self : Type) :
1919
core.ops.deref.Deref Self := {
2020
Target := Self
21-
deref := deref Self
21+
deref := deref
2222
}
2323

2424
/-- Trait instance -/
2525
def coreopsDerefMutInst (Self : Type) :
2626
core.ops.deref.DerefMut Self := {
2727
derefInst := coreopsDerefInst Self
28-
deref_mut := deref_mut Self
28+
deref_mut := deref_mut
2929
}
3030

3131
end Box -- alloc.boxed.Box

backends/lean/Base/Primitives/ArraySlice.lean

+78-78
Large diffs are not rendered by default.

backends/lean/Base/Primitives/Core.lean

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ def clone.CloneBool : clone.Clone Bool := {
5858
This acts like a swap effectively in a functional pure world.
5959
We return the old value of `dst`, i.e. `dst` itself.
6060
The new value of `dst` is `src`. -/
61-
@[simp] def mem.replace (a : Type) (dst : a) (src : a) : a × a := (dst, src)
61+
@[simp] def mem.replace {a : Type} (dst : a) (src : a) : a × a := (dst, src)
6262

6363
/- [core::mem::swap] -/
64-
@[simp] def mem.swap (T: Type) (a b: T): T × T := (b, a)
64+
@[simp] def mem.swap {T : Type} (a b : T): T × T := (b, a)
6565

6666
end core
6767

6868
/- [core::option::{core::option::Option<T>}::unwrap] -/
69-
@[simp] def core.option.Option.unwrap (T : Type) (x : Option T) : Result T :=
69+
@[simp] def core.option.Option.unwrap {T : Type} (x : Option T) : Result T :=
7070
Result.ofOption x Error.panic
7171

7272
/- [core::option::Option::take] -/
73-
@[simp] def core.option.Option.take (T: Type) (self: Option T): Option T × Option T := (self, .none)
73+
@[simp] def core.option.Option.take {T: Type} (self: Option T): Option T × Option T := (self, .none)
7474

7575
/- [core::option::Option::is_none] -/
76-
@[simp] def core.option.Option.is_none (T: Type) (self: Option T): Bool := self.isNone
76+
@[simp] def core.option.Option.is_none {T: Type} (self: Option T): Bool := self.isNone
7777

7878
/- [core::clone::Clone::clone_from]:
7979
Source: '/rustc/library/core/src/clone.rs', lines 175:4-175:43

backends/lean/Base/Primitives/Vec.lean

+28-42
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@ instance (α : Type u) : Inhabited (Vec α) := by
4848
constructor
4949
apply Vec.new
5050

51-
-- TODO: very annoying that the α is an explicit parameter
5251
@[simp]
53-
abbrev Vec.len (α : Type u) (v : Vec α) : Usize :=
52+
abbrev Vec.len {α : Type u} (v : Vec α) : Usize :=
5453
Usize.ofIntCore v.val.length (by constructor <;> scalar_tac)
5554

5655
@[simp]
57-
theorem Vec.len_val {α : Type u} (v : Vec α) : (Vec.len α v).val = v.length :=
56+
theorem Vec.len_val {α : Type u} (v : Vec α) : (Vec.len v).val = v.length :=
5857
by rfl
5958

6059
@[irreducible]
61-
def Vec.push (α : Type u) (v : Vec α) (x : α) : Result (Vec α)
60+
def Vec.push {α : Type u} (v : Vec α) (x : α) : Result (Vec α)
6261
:=
6362
let nlen := List.length v.val + 1
6463
if h : nlen ≤ U32.max || nlen ≤ Usize.max then
@@ -72,21 +71,13 @@ def Vec.push (α : Type u) (v : Vec α) (x : α) : Result (Vec α)
7271

7372
@[pspec]
7473
theorem Vec.push_spec {α : Type u} (v : Vec α) (x : α) (h : v.val.length < Usize.max) :
75-
∃ v1, v.push α x = ok v1 ∧
74+
∃ v1, v.push x = ok v1 ∧
7675
v1.val = v.val ++ [x] := by
7776
rw [push]; simp
78-
split <;> simp_all <;>
77+
split <;> simp_all
7978
scalar_tac
8079

81-
-- This shouldn't be used
82-
def Vec.insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α) : Result Unit :=
83-
if i.val < v.length then
84-
ok ()
85-
else
86-
fail arrayOutOfBounds
87-
88-
-- This is actually the backward function
89-
def Vec.insert (α : Type u) (v: Vec α) (i: Usize) (x: α) : Result (Vec α) :=
80+
def Vec.insert {α : Type u} (v: Vec α) (i: Usize) (x: α) : Result (Vec α) :=
9081
if i.val < v.length then
9182
ok ⟨ v.val.update i.toNat x, by have := v.property; simp [*] ⟩
9283
else
@@ -95,19 +86,14 @@ def Vec.insert (α : Type u) (v: Vec α) (i: Usize) (x: α) : Result (Vec α) :=
9586
@[pspec]
9687
theorem Vec.insert_spec {α : Type u} (v: Vec α) (i: Usize) (x: α)
9788
(hbound : i.val < v.length) :
98-
∃ nv, v.insert α i x = ok nv ∧ nv.val = v.val.update i.toNat x := by
89+
∃ nv, v.insert i x = ok nv ∧ nv.val = v.val.update i.toNat x := by
9990
simp [insert, *]
10091

10192
def Vec.index_usize {α : Type u} (v: Vec α) (i: Usize) : Result α :=
10293
match v.val.indexOpt i.toNat with
10394
| none => fail .arrayOutOfBounds
10495
| some x => ok x
10596

106-
/- In the theorems below: we don't always need the `∃ ..`, but we use one
107-
so that `progress` introduces an opaque variable and an equality. This
108-
helps control the context.
109-
-/
110-
11197
@[pspec]
11298
theorem Vec.index_usize_spec {α : Type u} [Inhabited α] (v: Vec α) (i: Usize)
11399
(hbound : i.val < v.length) :
@@ -154,90 +140,90 @@ theorem Vec.index_mut_usize_spec {α : Type u} [Inhabited α] (v: Vec α) (i: Us
154140
simp [h]
155141

156142
/- [alloc::vec::Vec::index]: forward function -/
157-
def Vec.index (T I : Type) (inst : core.slice.index.SliceIndex I (Slice T))
143+
def Vec.index {T I : Type} (inst : core.slice.index.SliceIndex I (Slice T))
158144
(self : Vec T) (i : I) : Result inst.Output :=
159145
sorry -- TODO
160146

161147
/- [alloc::vec::Vec::index_mut]: forward function -/
162-
def Vec.index_mut (T I : Type) (inst : core.slice.index.SliceIndex I (Slice T))
148+
def Vec.index_mut {T I : Type} (inst : core.slice.index.SliceIndex I (Slice T))
163149
(self : Vec T) (i : I) :
164150
Result (inst.Output × (inst.Output → Result (Vec T))) :=
165151
sorry -- TODO
166152

167153
/- Trait implementation: [alloc::vec::Vec] -/
168154
@[reducible]
169-
def Vec.coreopsindexIndexInst (T I : Type)
155+
def Vec.coreopsindexIndexInst {T I : Type}
170156
(inst : core.slice.index.SliceIndex I (Slice T)) :
171157
core.ops.index.Index (alloc.vec.Vec T) I := {
172158
Output := inst.Output
173-
index := Vec.index T I inst
159+
index := Vec.index inst
174160
}
175161

176162
/- Trait implementation: [alloc::vec::Vec] -/
177163
@[reducible]
178-
def Vec.coreopsindexIndexMutInst (T I : Type)
164+
def Vec.coreopsindexIndexMutInst {T I : Type}
179165
(inst : core.slice.index.SliceIndex I (Slice T)) :
180166
core.ops.index.IndexMut (alloc.vec.Vec T) I := {
181-
indexInst := Vec.coreopsindexIndexInst T I inst
182-
index_mut := Vec.index_mut T I inst
167+
indexInst := Vec.coreopsindexIndexInst inst
168+
index_mut := Vec.index_mut inst
183169
}
184170

185171
@[simp]
186172
theorem Vec.index_slice_index {α : Type} (v : Vec α) (i : Usize) :
187-
Vec.index α Usize (core.slice.index.SliceIndexUsizeSliceTInst α) v i =
173+
Vec.index (core.slice.index.SliceIndexUsizeSliceTInst α) v i =
188174
Vec.index_usize v i :=
189175
sorry
190176

191177
@[simp]
192178
theorem Vec.index_mut_slice_index {α : Type} (v : Vec α) (i : Usize) :
193-
Vec.index_mut α Usize (core.slice.index.SliceIndexUsizeSliceTInst α) v i =
179+
Vec.index_mut (core.slice.index.SliceIndexUsizeSliceTInst α) v i =
194180
index_mut_usize v i :=
195181
sorry
196182

197183
end alloc.vec
198184

199185
/-- [alloc::slice::{@Slice<T>}::to_vec] -/
200186
def alloc.slice.Slice.to_vec
201-
(T : Type) (cloneInst : core.clone.Clone T) (s : Slice T) : Result (alloc.vec.Vec T) :=
187+
{T : Type} (cloneInst : core.clone.Clone T) (s : Slice T) : Result (alloc.vec.Vec T) :=
202188
-- TODO: we need a monadic map
203189
sorry
204190

205191
/-- [core::slice::{@Slice<T>}::reverse] -/
206-
def core.slice.Slice.reverse (T : Type) (s : Slice T) : Slice T :=
192+
def core.slice.Slice.reverse {T : Type} (s : Slice T) : Slice T :=
207193
⟨ s.val.reverse, by sorry
208194

209-
def alloc.vec.Vec.with_capacity (T : Type) (_ : Usize) : alloc.vec.Vec T := Vec.new T
195+
def alloc.vec.Vec.with_capacity {T : Type} (_ : Usize) : alloc.vec.Vec T := Vec.new T
210196

211197
/- [alloc::vec::{(core::ops::deref::Deref for alloc::vec::Vec<T, A>)#9}::deref]:
212198
Source: '/rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/alloc/src/vec/mod.rs', lines 2624:4-2624:27
213199
Name pattern: alloc::vec::{core::ops::deref::Deref<alloc::vec::Vec<@T, @A>>}::deref -/
214-
def alloc.vec.DerefVec.deref (T : Type) (v : Vec T) : Slice T :=
200+
def alloc.vec.DerefVec.deref {T : Type} (v : Vec T) : Slice T :=
215201
⟨ v.val, v.property ⟩
216202

217203
@[reducible]
218-
def core.ops.deref.DerefVec (T : Type) : core.ops.deref.Deref (alloc.vec.Vec T) := {
204+
def core.ops.deref.DerefVec {T : Type} : core.ops.deref.Deref (alloc.vec.Vec T) := {
219205
Target := Slice T
220-
deref := fun v => ok (alloc.vec.DerefVec.deref T v)
206+
deref := fun v => ok (alloc.vec.DerefVec.deref v)
221207
}
222208

223209
/- [alloc::vec::{(core::ops::deref::DerefMut for alloc::vec::Vec<T, A>)#10}::deref_mut]:
224210
Source: '/rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/alloc/src/vec/mod.rs', lines 2632:4-2632:39
225211
Name pattern: alloc::vec::{core::ops::deref::DerefMut<alloc::vec::Vec<@T, @A>>}::deref_mut -/
226-
def alloc.vec.DerefMutVec.deref_mut (T : Type) (v : alloc.vec.Vec T) :
212+
def alloc.vec.DerefMutVec.deref_mut {T : Type} (v : alloc.vec.Vec T) :
227213
Result ((Slice T) × (Slice T → Result (alloc.vec.Vec T))) :=
228214
ok (⟨ v.val, v.property ⟩, λ s => ok ⟨ s.val, s.property ⟩)
229215

230216
/- Trait implementation: [alloc::vec::{(core::ops::deref::DerefMut for alloc::vec::Vec<T, A>)#10}]
231217
Source: '/rustc/d59363ad0b6391b7fc5bbb02c9ccf9300eef3753/library/alloc/src/vec/mod.rs', lines 2630:0-2630:49
232218
Name pattern: core::ops::deref::DerefMut<alloc::vec::Vec<@Self, @>> -/
233219
@[reducible]
234-
def core.ops.deref.DerefMutVec (T : Type) :
220+
def core.ops.deref.DerefMutVec {T : Type} :
235221
core.ops.deref.DerefMut (alloc.vec.Vec T) := {
236-
derefInst := core.ops.deref.DerefVec T
237-
deref_mut := alloc.vec.DerefMutVec.deref_mut T
222+
derefInst := core.ops.deref.DerefVec
223+
deref_mut := alloc.vec.DerefMutVec.deref_mut
238224
}
239225

240-
def alloc.vec.Vec.resize (T : Type) (cloneInst : core.clone.Clone T)
226+
def alloc.vec.Vec.resize {T : Type} (cloneInst : core.clone.Clone T)
241227
(v : alloc.vec.Vec T) (new_len : Usize) (value : T) : Result (alloc.vec.Vec T) := do
242228
if new_len.val < v.length then
243229
ok ⟨ v.val.resize new_len.toNat value, by scalar_tac ⟩
@@ -249,7 +235,7 @@ def alloc.vec.Vec.resize (T : Type) (cloneInst : core.clone.Clone T)
249235
theorem alloc.vec.Vec.resize_spec {T} (cloneInst : core.clone.Clone T)
250236
(v : alloc.vec.Vec T) (new_len : Usize) (value : T)
251237
(hClone : cloneInst.clone value = ok value) :
252-
∃ nv, alloc.vec.Vec.resize T cloneInst v new_len value = ok nv ∧
238+
∃ nv, alloc.vec.Vec.resize cloneInst v new_len value = ok nv ∧
253239
nv.val = v.val.resize new_len.toNat value := by
254240
rw [resize]
255241
split

compiler/AssociatedTypes.ml

+16-15
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ let compute_norm_trait_types_from_preds (span : Meta.span option)
8282
in
8383
TraitTypeRefMap.of_list rbindings
8484

85-
let ctx_add_norm_trait_types_from_preds (span : Meta.span) (ctx : eval_ctx)
86-
(trait_type_constraints : trait_type_constraint list) : eval_ctx =
85+
let ctx_add_norm_trait_types_from_preds (span : Meta.span option)
86+
(ctx : eval_ctx) (trait_type_constraints : trait_type_constraint list) :
87+
eval_ctx =
8788
let norm_trait_types =
88-
compute_norm_trait_types_from_preds (Some span) trait_type_constraints
89+
compute_norm_trait_types_from_preds span trait_type_constraints
8990
in
9091
{ ctx with norm_trait_types }
9192

@@ -417,9 +418,9 @@ let norm_ctx_normalize_trait_type_constraint (ctx : norm_ctx)
417418
let ty = norm_ctx_normalize_ty ctx ty in
418419
{ trait_ref; type_name; ty }
419420

420-
let mk_norm_ctx (span : Meta.span) (ctx : eval_ctx) : norm_ctx =
421+
let mk_norm_ctx (span : Meta.span option) (ctx : eval_ctx) : norm_ctx =
421422
{
422-
span = Some span;
423+
span;
423424
norm_trait_types = ctx.norm_trait_types;
424425
type_decls = ctx.type_ctx.type_decls;
425426
fun_decls = ctx.fun_ctx.fun_decls;
@@ -430,17 +431,17 @@ let mk_norm_ctx (span : Meta.span) (ctx : eval_ctx) : norm_ctx =
430431
const_generic_vars = ctx.const_generic_vars;
431432
}
432433

433-
let ctx_normalize_ty (span : Meta.span) (ctx : eval_ctx) (ty : ty) : ty =
434+
let ctx_normalize_ty (span : Meta.span option) (ctx : eval_ctx) (ty : ty) : ty =
434435
norm_ctx_normalize_ty (mk_norm_ctx span ctx) ty
435436

436437
(** Normalize a type and erase the regions at the same time *)
437438
let ctx_normalize_erase_ty (span : Meta.span) (ctx : eval_ctx) (ty : ty) : ty =
438-
let ty = ctx_normalize_ty span ctx ty in
439+
let ty = ctx_normalize_ty (Some span) ctx ty in
439440
Subst.erase_regions ty
440441

441442
let ctx_normalize_trait_type_constraint (span : Meta.span) (ctx : eval_ctx)
442443
(ttc : trait_type_constraint) : trait_type_constraint =
443-
norm_ctx_normalize_trait_type_constraint (mk_norm_ctx span ctx) ttc
444+
norm_ctx_normalize_trait_type_constraint (mk_norm_ctx (Some span) ctx) ttc
444445

445446
(** Same as [type_decl_get_instantiated_variants_fields_types] but normalizes the types *)
446447
let type_decl_get_inst_norm_variants_fields_rtypes (span : Meta.span)
@@ -451,7 +452,7 @@ let type_decl_get_inst_norm_variants_fields_rtypes (span : Meta.span)
451452
in
452453
List.map
453454
(fun (variant_id, types) ->
454-
(variant_id, List.map (ctx_normalize_ty span ctx) types))
455+
(variant_id, List.map (ctx_normalize_ty (Some span) ctx) types))
455456
res
456457

457458
(** Same as [type_decl_get_instantiated_field_types] but normalizes the types *)
@@ -461,15 +462,15 @@ let type_decl_get_inst_norm_field_rtypes (span : Meta.span) (ctx : eval_ctx)
461462
let types =
462463
Subst.type_decl_get_instantiated_field_types def opt_variant_id generics
463464
in
464-
List.map (ctx_normalize_ty span ctx) types
465+
List.map (ctx_normalize_ty (Some span) ctx) types
465466

466467
(** Same as [ctx_adt_value_get_instantiated_field_rtypes] but normalizes the types *)
467468
let ctx_adt_value_get_inst_norm_field_rtypes (span : Meta.span) (ctx : eval_ctx)
468469
(adt : adt_value) (id : type_id) (generics : generic_args) : ty list =
469470
let types =
470471
Subst.ctx_adt_value_get_instantiated_field_types span ctx adt id generics
471472
in
472-
List.map (ctx_normalize_ty span ctx) types
473+
List.map (ctx_normalize_ty (Some span) ctx) types
473474

474475
(** Same as [ctx_adt_value_get_instantiated_field_types] but normalizes the types
475476
and erases the regions. *)
@@ -479,7 +480,7 @@ let type_decl_get_inst_norm_field_etypes (span : Meta.span) (ctx : eval_ctx)
479480
let types =
480481
Subst.type_decl_get_instantiated_field_types def opt_variant_id generics
481482
in
482-
let types = List.map (ctx_normalize_ty span ctx) types in
483+
let types = List.map (ctx_normalize_ty (Some span) ctx) types in
483484
List.map Subst.erase_regions types
484485

485486
(** Same as [ctx_adt_get_instantiated_field_types] but normalizes the types and
@@ -491,7 +492,7 @@ let ctx_adt_get_inst_norm_field_etypes (span : Meta.span) (ctx : eval_ctx)
491492
Subst.ctx_adt_get_instantiated_field_types ctx def_id opt_variant_id
492493
generics
493494
in
494-
let types = List.map (ctx_normalize_ty span ctx) types in
495+
let types = List.map (ctx_normalize_ty (Some span) ctx) types in
495496
List.map Subst.erase_regions types
496497

497498
(** Same as [substitute_signature] but normalizes the types *)
@@ -507,8 +508,8 @@ let ctx_subst_norm_signature (span : Meta.span) (ctx : eval_ctx)
507508
sg regions_hierarchy
508509
in
509510
let { regions_hierarchy; inputs; output; trait_type_constraints } = sg in
510-
let inputs = List.map (ctx_normalize_ty span ctx) inputs in
511-
let output = ctx_normalize_ty span ctx output in
511+
let inputs = List.map (ctx_normalize_ty (Some span) ctx) inputs in
512+
let output = ctx_normalize_ty (Some span) ctx output in
512513
let trait_type_constraints =
513514
List.map
514515
(ctx_normalize_trait_type_constraint span ctx)

0 commit comments

Comments
 (0)