Skip to content

Commit 1d4b454

Browse files
authored
Merge pull request #195 from o1-labs/mimoo/ocaml_custom2
[ocaml-gen] rename macros and revert multi-declaration of custom types
2 parents f42ead3 + dcf925e commit 1d4b454

File tree

12 files changed

+48
-71
lines changed

12 files changed

+48
-71
lines changed

circuits/plonk-15-wires/src/gate.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<F: Field> JointLookup<F> {
117117
)]
118118
#[cfg_attr(
119119
feature = "ocaml_types",
120-
derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::OcamlEnum)
120+
derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Enum)
121121
)]
122122
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
123123
pub enum GateType {
@@ -423,10 +423,9 @@ pub mod caml {
423423
use super::*;
424424
use crate::wires::caml::CamlWire;
425425
use itertools::Itertools;
426-
use ocaml_gen::OcamlGen;
427426
use std::convert::TryInto;
428427

429-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
428+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
430429
pub struct CamlCircuitGate<F> {
431430
pub row: ocaml::Int,
432431
pub typ: GateType,

circuits/plonk-15-wires/src/nolookup/scalars.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,13 @@ impl<F: Field> Default for RandomOracles<F> {
102102
#[cfg(feature = "ocaml_types")]
103103
pub mod caml {
104104
use super::*;
105-
use ocaml_gen::OcamlGen;
106105
use oracle::sponge::caml::CamlScalarChallenge;
107106

108107
//
109108
// CamlLookupEvaluations<CamlF>
110109
//
111110

112-
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
111+
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
113112
pub struct CamlLookupEvaluations<CamlF> {
114113
pub sorted: Vec<Vec<CamlF>>,
115114
pub aggreg: Vec<CamlF>,
@@ -155,7 +154,7 @@ pub mod caml {
155154
// CamlProofEvaluations<CamlF>
156155
//
157156

158-
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
157+
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
159158
pub struct CamlProofEvaluations<CamlF> {
160159
pub w: (
161160
Vec<CamlF>,
@@ -279,7 +278,7 @@ pub mod caml {
279278
// RandomOracles<F> <-> CamlRandomOracles<CamlF>
280279
//
281280

282-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
281+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
283282
pub struct CamlRandomOracles<CamlF> {
284283
pub joint_combiner: (CamlScalarChallenge<CamlF>, CamlF),
285284
pub beta: CamlF,

circuits/plonk-15-wires/src/wires.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ impl FromBytes for Wire {
5858
#[cfg(feature = "ocaml_types")]
5959
pub mod caml {
6060
use super::*;
61-
use ocaml_gen::OcamlGen;
6261
use std::convert::TryInto;
6362

64-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
63+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
6564
pub struct CamlWire {
6665
pub row: ocaml::Int,
6766
pub col: ocaml::Int,

circuits/plonk/src/scalars.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,13 @@ impl<F: Field> RandomOracles<F> {
7575
#[cfg(feature = "ocaml_types")]
7676
pub mod caml {
7777
use super::*;
78-
use ocaml_gen::OcamlGen;
7978
use oracle::sponge::caml::CamlScalarChallenge;
8079

8180
//
8281
// ProofEvaluations<F> <-> CamlProofEvaluations<CamlF>
8382
//
8483

85-
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
84+
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
8685
pub struct CamlProofEvaluations<F> {
8786
pub l: Vec<F>,
8887
pub r: Vec<F>,
@@ -134,7 +133,7 @@ pub mod caml {
134133
// RandomOracles<F> <-> CamlRandomOracles<CamlF>
135134
//
136135

137-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
136+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
138137
pub struct CamlRandomOracles<CamlF> {
139138
pub beta: CamlF,
140139
pub gamma: CamlF,

dlog/commitment/src/commitment.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,11 +1165,10 @@ mod tests {
11651165
#[cfg(feature = "ocaml_types")]
11661166
pub mod caml {
11671167
use super::*;
1168-
use ocaml_gen::OcamlGen;
11691168

11701169
// polynomial commitment
11711170

1172-
#[derive(Clone, Debug, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
1171+
#[derive(Clone, Debug, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
11731172
pub struct CamlPolyComm<CamlG> {
11741173
pub unshifted: Vec<CamlG>,
11751174
pub shifted: Option<CamlG>,
@@ -1229,7 +1228,7 @@ pub mod caml {
12291228

12301229
// opening proof
12311230

1232-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
1231+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
12331232
pub struct CamlOpeningProof<G, F> {
12341233
pub lr: Vec<(G, G)>, // vector of rounds of L & R commitments
12351234
pub delta: G,

dlog/plonk-15-wires/src/prover.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,9 @@ where
709709
pub mod caml {
710710
use super::*;
711711
use commitment_dlog::commitment::caml::{CamlOpeningProof, CamlPolyComm};
712-
use ocaml_gen::OcamlGen;
713712
use plonk_15_wires_circuits::nolookup::scalars::caml::CamlProofEvaluations;
714713

715-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
714+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
716715
pub struct CamlProverProof<CamlG, CamlF> {
717716
pub commitments: CamlProverCommitments<CamlG>,
718717
pub proof: CamlOpeningProof<CamlG, CamlF>,
@@ -723,7 +722,7 @@ pub mod caml {
723722
pub prev_challenges: Vec<(Vec<CamlF>, CamlPolyComm<CamlG>)>,
724723
}
725724

726-
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
725+
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
727726
pub struct CamlProverCommitments<CamlG> {
728727
// polynomial commitments
729728
pub w_comm: (

dlog/plonk/src/prover.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,14 @@ where
434434
pub mod caml {
435435
use super::*;
436436
use commitment_dlog::commitment::caml::{CamlOpeningProof, CamlPolyComm};
437-
use ocaml_gen::OcamlGen;
438437
use plonk_circuits::scalars::caml::CamlProofEvaluations;
439438

440439
//
441440
// CamlProverCommitments<CamlG> <-> ProverCommitments<G>
442441
//
443442

444443
/// The ocaml type for ProverCommitments
445-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
444+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
446445
pub struct CamlProverCommitments<CamlG> {
447446
pub l_comm: CamlPolyComm<CamlG>,
448447
pub r_comm: CamlPolyComm<CamlG>,
@@ -487,7 +486,7 @@ pub mod caml {
487486
// ProverProof<G> <-> CamlProverProof<CamlG, CamlF>
488487
//
489488

490-
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
489+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
491490
pub struct CamlProverProof<CamlG, CamlF> {
492491
pub commitments: CamlProverCommitments<CamlG>,
493492
pub proof: CamlOpeningProof<CamlG, CamlF>,

ocaml/ocaml-gen/README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,44 +49,38 @@ decl_module!(w, env, "ImportantType", {
4949
To allow the previous example to work, you must derive the correct functions on your types and functions.
5050
To do that, you can use the [ocaml-derive](./derive) crate.
5151

52-
To allow generation of bindings on structs, use [OCamlGen]():
52+
To allow generation of bindings on structs, use [Struct]():
5353

5454
```rust
55-
use ocaml_gen::OcamlGen;
56-
57-
#[OcamlGen]
55+
#[ocaml_gen::Struct]
5856
struct MyType {
5957
// ...
6058
}
6159
```
6260

63-
To allow generation of bindings on enums, use [OcamlEnum]():
61+
To allow generation of bindings on enums, use [ocaml_gen::Enum]():
6462

6563
```rust
66-
use ocaml_gen::OcamlEnum;
67-
68-
#[OcamlEnum]
64+
#[ocaml_gen::Enum]
6965
enum MyType {
7066
// ...
7167
}
7268
```
7369

74-
To allow generation of bindings on functions, use [ocaml_gen]():
70+
To allow generation of bindings on functions, use [ocaml_gen::func]():
7571

7672
```rust
77-
#[ocaml_gen]
73+
#[ocaml_gen::func]
7874
#[ocaml::func] // if you use the crate ocaml-rs' macro, it must appear after
7975
pub fn your_function(arg1: String) {
8076
//...
8177
}
8278
```
8379

84-
To allow generation of bindings on custom types, use [OCamlCustomType]():
80+
To allow generation of bindings on custom types, use [ocaml_gen::CustomType]():
8581

8682
```rust
87-
use ocaml_gen::OCamlCustomType;
88-
89-
#[OCamlCustomType]
83+
#[ocaml_gen::CustomType]
9084
struct MyCustomType {
9185
// ...
9286
}

ocaml/ocaml-gen/derive/src/lib.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ use syn::{
1919
/// For example:
2020
///
2121
/// ```
22-
/// #[ocaml_gen]
22+
/// #[ocaml_gen::func]
2323
/// #[ocaml::func]
2424
/// pub fn something(arg1: String) {
2525
/// //...
2626
/// }
2727
/// ```
2828
///
2929
#[proc_macro_attribute]
30-
pub fn ocaml_gen(_attribute: TokenStream, item: TokenStream) -> TokenStream {
30+
pub fn func(_attribute: TokenStream, item: TokenStream) -> TokenStream {
3131
let item_fn: syn::ItemFn = syn::parse(item).unwrap();
3232

3333
let rust_name = &item_fn.sig.ident;
@@ -94,27 +94,26 @@ pub fn ocaml_gen(_attribute: TokenStream, item: TokenStream) -> TokenStream {
9494
}
9595

9696
//
97-
// OcamlEnum
97+
// Enum
9898
//
9999

100-
/// The OcamlEnum derive macro.
100+
/// The Enum derive macro.
101101
/// It generates implementations of ToOCaml and OCamlBinding on an enum type.
102102
/// The type must implement [ocaml::IntoValue] and [ocaml::FromValue]
103103
/// For example:
104104
///
105105
/// ```
106-
/// use ocaml_gen::OcamlEnum;
106+
/// use ocaml_gen::Enum;
107107
///
108-
/// #[OcamlEnum]
108+
/// #[Enum]
109109
/// enum MyType {
110110
/// // ...
111111
/// }
112112
/// ```
113113
///
114-
#[proc_macro_derive(OcamlEnum)]
114+
#[proc_macro_derive(Enum)]
115115
pub fn derive_ocaml_enum(item: TokenStream) -> TokenStream {
116-
let item_enum: syn::ItemEnum =
117-
syn::parse(item).expect("only enum are supported with OcamlEnum");
116+
let item_enum: syn::ItemEnum = syn::parse(item).expect("only enum are supported with Enum");
118117

119118
//
120119
// ocaml_desc
@@ -346,27 +345,25 @@ pub fn derive_ocaml_enum(item: TokenStream) -> TokenStream {
346345
}
347346

348347
//
349-
// OcamlGen
348+
// Struct
350349
//
351350

352-
/// The OcamlGen derive macro.
351+
/// The Struct derive macro.
353352
/// It generates implementations of ToOCaml and OCamlBinding on a struct.
354353
/// The type must implement [ocaml::IntoValue] and [ocaml::FromValue]
355354
/// For example:
356355
///
357356
/// ```
358-
/// use ocaml_gen::OcamlGen;
359-
///
360-
/// #[OcamlGen]
357+
/// #[ocaml_gen::Struct]
361358
/// struct MyType {
362359
/// // ...
363360
/// }
364361
/// ```
365362
///
366-
#[proc_macro_derive(OcamlGen)]
363+
#[proc_macro_derive(Struct)]
367364
pub fn derive_ocaml_gen(item: TokenStream) -> TokenStream {
368365
let item_struct: syn::ItemStruct =
369-
syn::parse(item).expect("only structs are supported with OCamlGen");
366+
syn::parse(item).expect("only structs are supported with Struct");
370367
let name = &item_struct.ident;
371368
let generics = &item_struct.generics.params;
372369
let fields = &item_struct.fields;
@@ -624,15 +621,15 @@ pub fn derive_ocaml_gen(item: TokenStream) -> TokenStream {
624621
/// For example:
625622
///
626623
/// ```
627-
/// use ocaml_gen::OCamlCustomType;
624+
/// use ocaml_gen::CustomType;
628625
///
629-
/// #[OCamlCustomType]
626+
/// #[CustomType]
630627
/// struct MyCustomType {
631628
/// // ...
632629
/// }
633630
/// ```
634631
///
635-
#[proc_macro_derive(OCamlCustomType)]
632+
#[proc_macro_derive(CustomType)]
636633
pub fn derive_ocaml_custom(item: TokenStream) -> TokenStream {
637634
let item_struct: syn::ItemStruct =
638635
syn::parse(item).expect("only structs are supported at the moment");
@@ -677,7 +674,7 @@ pub fn derive_ocaml_custom(item: TokenStream) -> TokenStream {
677674
if new_type {
678675
let ty_name = rename.unwrap_or(#ocaml_name);
679676
let ty_id = <Self as ::ocaml_gen::OCamlDesc>::unique_id();
680-
env.new_custom_type(ty_id, ty_name);
677+
env.new_type(ty_id, ty_name);
681678
}
682679

683680
let name = <Self as ::ocaml_gen::OCamlDesc>::ocaml_desc(env, &[]);

ocaml/ocaml-gen/src/lib.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ impl Env {
4444
};
4545
}
4646

47-
/// Declares a new custom type. Unlike [new_type] this can be called several times with the same type.
48-
pub fn new_custom_type(&mut self, ty: u128, name: &'static str) {
49-
self.locations
50-
.insert(ty, (self.current_module.clone(), name));
51-
}
52-
5347
/// retrieves a type that was declared previously
5448
pub fn get_type(&self, ty: u128, name: &str) -> String {
5549
let (type_path, type_name) = self.locations.get(&ty).expect(&format!(
@@ -118,9 +112,9 @@ impl Env {
118112
//
119113

120114
/// OCamlBinding is the trait implemented by types to generate their OCaml bindings.
121-
/// It is usually derived automatically via the [OcamlGen] macro,
122-
/// or the [OCamlCustomType] macro for custom types.
123-
/// For functions, refer to the [ocaml_gen] macro.
115+
/// It is usually derived automatically via the [Struct] macro,
116+
/// or the [CustomType] macro for custom types.
117+
/// For functions, refer to the [func] macro.
124118
pub trait OCamlBinding {
125119
/// will generate the OCaml bindings for a type (called root type).
126120
/// It takes the current environment [Env],
@@ -129,8 +123,8 @@ pub trait OCamlBinding {
129123
}
130124

131125
/// OCamlDesc is the trait implemented by types to facilitate generation of their OCaml bindings.
132-
/// It is usually derived automatically via the [OcamlGen] macro,
133-
/// or the [OCamlCustomType] macro for custom types.
126+
/// It is usually derived automatically via the [Struct] macro,
127+
/// or the [CustomType] macro for custom types.
134128
pub trait OCamlDesc {
135129
/// describes the type in OCaml, given the current environment [Env]
136130
/// and the list of generic type parameters of the root type

ocaml/tests/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::OcamlGen)]
1+
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
22
pub struct SingleTuple(String);
33

4-
#[ocaml_gen::ocaml_gen]
4+
#[ocaml_gen::func]
55
#[ocaml::func]
66
pub fn new() -> SingleTuple {
77
SingleTuple(String::from("Hello"))
88
}
99

10-
#[ocaml_gen::ocaml_gen]
10+
#[ocaml_gen::func]
1111
#[ocaml::func]
1212
pub fn print(s: SingleTuple) {
1313
println!("{}", s.0);

oracle/src/sponge.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,12 @@ where
215215
#[cfg(feature = "ocaml_types")]
216216
pub mod caml {
217217
use super::*;
218-
use ocaml_gen::OcamlGen;
219218

220219
//
221220
// ScalarChallenge<F> <-> CamlScalarChallenge<CamlF>
222221
//
223222

224-
#[derive(Debug, Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
223+
#[derive(Debug, Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
225224
pub struct CamlScalarChallenge<CamlF>(pub CamlF);
226225

227226
impl<F, CamlF> From<ScalarChallenge<F>> for CamlScalarChallenge<CamlF>

0 commit comments

Comments
 (0)