Skip to content

[ocaml-gen] rename macros and revert multi-declaration of custom types #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions circuits/plonk-15-wires/src/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<F: Field> JointLookup<F> {
)]
#[cfg_attr(
feature = "ocaml_types",
derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::OcamlEnum)
derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Enum)
)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
pub enum GateType {
Expand Down Expand Up @@ -423,10 +423,9 @@ pub mod caml {
use super::*;
use crate::wires::caml::CamlWire;
use itertools::Itertools;
use ocaml_gen::OcamlGen;
use std::convert::TryInto;

#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlCircuitGate<F> {
pub row: ocaml::Int,
pub typ: GateType,
Expand Down
7 changes: 3 additions & 4 deletions circuits/plonk-15-wires/src/nolookup/scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ impl<F: Field> Default for RandomOracles<F> {
#[cfg(feature = "ocaml_types")]
pub mod caml {
use super::*;
use ocaml_gen::OcamlGen;
use oracle::sponge::caml::CamlScalarChallenge;

//
// CamlLookupEvaluations<CamlF>
//

#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlLookupEvaluations<CamlF> {
pub sorted: Vec<Vec<CamlF>>,
pub aggreg: Vec<CamlF>,
Expand Down Expand Up @@ -155,7 +154,7 @@ pub mod caml {
// CamlProofEvaluations<CamlF>
//

#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlProofEvaluations<CamlF> {
pub w: (
Vec<CamlF>,
Expand Down Expand Up @@ -279,7 +278,7 @@ pub mod caml {
// RandomOracles<F> <-> CamlRandomOracles<CamlF>
//

#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlRandomOracles<CamlF> {
pub joint_combiner: (CamlScalarChallenge<CamlF>, CamlF),
pub beta: CamlF,
Expand Down
3 changes: 1 addition & 2 deletions circuits/plonk-15-wires/src/wires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ impl FromBytes for Wire {
#[cfg(feature = "ocaml_types")]
pub mod caml {
use super::*;
use ocaml_gen::OcamlGen;
use std::convert::TryInto;

#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlWire {
pub row: ocaml::Int,
pub col: ocaml::Int,
Expand Down
5 changes: 2 additions & 3 deletions circuits/plonk/src/scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ impl<F: Field> RandomOracles<F> {
#[cfg(feature = "ocaml_types")]
pub mod caml {
use super::*;
use ocaml_gen::OcamlGen;
use oracle::sponge::caml::CamlScalarChallenge;

//
// ProofEvaluations<F> <-> CamlProofEvaluations<CamlF>
//

#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlProofEvaluations<F> {
pub l: Vec<F>,
pub r: Vec<F>,
Expand Down Expand Up @@ -134,7 +133,7 @@ pub mod caml {
// RandomOracles<F> <-> CamlRandomOracles<CamlF>
//

#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlRandomOracles<CamlF> {
pub beta: CamlF,
pub gamma: CamlF,
Expand Down
5 changes: 2 additions & 3 deletions dlog/commitment/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,11 +1165,10 @@ mod tests {
#[cfg(feature = "ocaml_types")]
pub mod caml {
use super::*;
use ocaml_gen::OcamlGen;

// polynomial commitment

#[derive(Clone, Debug, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(Clone, Debug, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlPolyComm<CamlG> {
pub unshifted: Vec<CamlG>,
pub shifted: Option<CamlG>,
Expand Down Expand Up @@ -1229,7 +1228,7 @@ pub mod caml {

// opening proof

#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlOpeningProof<G, F> {
pub lr: Vec<(G, G)>, // vector of rounds of L & R commitments
pub delta: G,
Expand Down
5 changes: 2 additions & 3 deletions dlog/plonk-15-wires/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,9 @@ where
pub mod caml {
use super::*;
use commitment_dlog::commitment::caml::{CamlOpeningProof, CamlPolyComm};
use ocaml_gen::OcamlGen;
use plonk_15_wires_circuits::nolookup::scalars::caml::CamlProofEvaluations;

#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlProverProof<CamlG, CamlF> {
pub commitments: CamlProverCommitments<CamlG>,
pub proof: CamlOpeningProof<CamlG, CamlF>,
Expand All @@ -723,7 +722,7 @@ pub mod caml {
pub prev_challenges: Vec<(Vec<CamlF>, CamlPolyComm<CamlG>)>,
}

#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlProverCommitments<CamlG> {
// polynomial commitments
pub w_comm: (
Expand Down
5 changes: 2 additions & 3 deletions dlog/plonk/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,14 @@ where
pub mod caml {
use super::*;
use commitment_dlog::commitment::caml::{CamlOpeningProof, CamlPolyComm};
use ocaml_gen::OcamlGen;
use plonk_circuits::scalars::caml::CamlProofEvaluations;

//
// CamlProverCommitments<CamlG> <-> ProverCommitments<G>
//

/// The ocaml type for ProverCommitments
#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlProverCommitments<CamlG> {
pub l_comm: CamlPolyComm<CamlG>,
pub r_comm: CamlPolyComm<CamlG>,
Expand Down Expand Up @@ -487,7 +486,7 @@ pub mod caml {
// ProverProof<G> <-> CamlProverProof<CamlG, CamlF>
//

#[derive(ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlProverProof<CamlG, CamlF> {
pub commitments: CamlProverCommitments<CamlG>,
pub proof: CamlOpeningProof<CamlG, CamlF>,
Expand Down
22 changes: 8 additions & 14 deletions ocaml/ocaml-gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,38 @@ decl_module!(w, env, "ImportantType", {
To allow the previous example to work, you must derive the correct functions on your types and functions.
To do that, you can use the [ocaml-derive](./derive) crate.

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

```rust
use ocaml_gen::OcamlGen;

#[OcamlGen]
#[ocaml_gen::Struct]
struct MyType {
// ...
}
```

To allow generation of bindings on enums, use [OcamlEnum]():
To allow generation of bindings on enums, use [ocaml_gen::Enum]():

```rust
use ocaml_gen::OcamlEnum;

#[OcamlEnum]
#[ocaml_gen::Enum]
enum MyType {
// ...
}
```

To allow generation of bindings on functions, use [ocaml_gen]():
To allow generation of bindings on functions, use [ocaml_gen::func]():

```rust
#[ocaml_gen]
#[ocaml_gen::func]
#[ocaml::func] // if you use the crate ocaml-rs' macro, it must appear after
pub fn your_function(arg1: String) {
//...
}
```

To allow generation of bindings on custom types, use [OCamlCustomType]():
To allow generation of bindings on custom types, use [ocaml_gen::CustomType]():

```rust
use ocaml_gen::OCamlCustomType;

#[OCamlCustomType]
#[ocaml_gen::CustomType]
struct MyCustomType {
// ...
}
Expand Down
37 changes: 17 additions & 20 deletions ocaml/ocaml-gen/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ use syn::{
/// For example:
///
/// ```
/// #[ocaml_gen]
/// #[ocaml_gen::func]
/// #[ocaml::func]
/// pub fn something(arg1: String) {
/// //...
/// }
/// ```
///
#[proc_macro_attribute]
pub fn ocaml_gen(_attribute: TokenStream, item: TokenStream) -> TokenStream {
pub fn func(_attribute: TokenStream, item: TokenStream) -> TokenStream {
let item_fn: syn::ItemFn = syn::parse(item).unwrap();

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

//
// OcamlEnum
// Enum
//

/// The OcamlEnum derive macro.
/// The Enum derive macro.
/// It generates implementations of ToOCaml and OCamlBinding on an enum type.
/// The type must implement [ocaml::IntoValue] and [ocaml::FromValue]
/// For example:
///
/// ```
/// use ocaml_gen::OcamlEnum;
/// use ocaml_gen::Enum;
///
/// #[OcamlEnum]
/// #[Enum]
/// enum MyType {
/// // ...
/// }
/// ```
///
#[proc_macro_derive(OcamlEnum)]
#[proc_macro_derive(Enum)]
pub fn derive_ocaml_enum(item: TokenStream) -> TokenStream {
let item_enum: syn::ItemEnum =
syn::parse(item).expect("only enum are supported with OcamlEnum");
let item_enum: syn::ItemEnum = syn::parse(item).expect("only enum are supported with Enum");

//
// ocaml_desc
Expand Down Expand Up @@ -346,27 +345,25 @@ pub fn derive_ocaml_enum(item: TokenStream) -> TokenStream {
}

//
// OcamlGen
// Struct
//

/// The OcamlGen derive macro.
/// The Struct derive macro.
/// It generates implementations of ToOCaml and OCamlBinding on a struct.
/// The type must implement [ocaml::IntoValue] and [ocaml::FromValue]
/// For example:
///
/// ```
/// use ocaml_gen::OcamlGen;
///
/// #[OcamlGen]
/// #[ocaml_gen::Struct]
/// struct MyType {
/// // ...
/// }
/// ```
///
#[proc_macro_derive(OcamlGen)]
#[proc_macro_derive(Struct)]
pub fn derive_ocaml_gen(item: TokenStream) -> TokenStream {
let item_struct: syn::ItemStruct =
syn::parse(item).expect("only structs are supported with OCamlGen");
syn::parse(item).expect("only structs are supported with Struct");
let name = &item_struct.ident;
let generics = &item_struct.generics.params;
let fields = &item_struct.fields;
Expand Down Expand Up @@ -624,15 +621,15 @@ pub fn derive_ocaml_gen(item: TokenStream) -> TokenStream {
/// For example:
///
/// ```
/// use ocaml_gen::OCamlCustomType;
/// use ocaml_gen::CustomType;
///
/// #[OCamlCustomType]
/// #[CustomType]
/// struct MyCustomType {
/// // ...
/// }
/// ```
///
#[proc_macro_derive(OCamlCustomType)]
#[proc_macro_derive(CustomType)]
pub fn derive_ocaml_custom(item: TokenStream) -> TokenStream {
let item_struct: syn::ItemStruct =
syn::parse(item).expect("only structs are supported at the moment");
Expand Down Expand Up @@ -677,7 +674,7 @@ pub fn derive_ocaml_custom(item: TokenStream) -> TokenStream {
if new_type {
let ty_name = rename.unwrap_or(#ocaml_name);
let ty_id = <Self as ::ocaml_gen::OCamlDesc>::unique_id();
env.new_custom_type(ty_id, ty_name);
env.new_type(ty_id, ty_name);
}

let name = <Self as ::ocaml_gen::OCamlDesc>::ocaml_desc(env, &[]);
Expand Down
16 changes: 5 additions & 11 deletions ocaml/ocaml-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ impl Env {
};
}

/// Declares a new custom type. Unlike [new_type] this can be called several times with the same type.
pub fn new_custom_type(&mut self, ty: u128, name: &'static str) {
self.locations
.insert(ty, (self.current_module.clone(), name));
}

/// retrieves a type that was declared previously
pub fn get_type(&self, ty: u128, name: &str) -> String {
let (type_path, type_name) = self.locations.get(&ty).expect(&format!(
Expand Down Expand Up @@ -118,9 +112,9 @@ impl Env {
//

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

/// OCamlDesc is the trait implemented by types to facilitate generation of their OCaml bindings.
/// It is usually derived automatically via the [OcamlGen] macro,
/// or the [OCamlCustomType] macro for custom types.
/// It is usually derived automatically via the [Struct] macro,
/// or the [CustomType] macro for custom types.
pub trait OCamlDesc {
/// describes the type in OCaml, given the current environment [Env]
/// and the list of generic type parameters of the root type
Expand Down
6 changes: 3 additions & 3 deletions ocaml/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::OcamlGen)]
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct SingleTuple(String);

#[ocaml_gen::ocaml_gen]
#[ocaml_gen::func]
#[ocaml::func]
pub fn new() -> SingleTuple {
SingleTuple(String::from("Hello"))
}

#[ocaml_gen::ocaml_gen]
#[ocaml_gen::func]
#[ocaml::func]
pub fn print(s: SingleTuple) {
println!("{}", s.0);
Expand Down
3 changes: 1 addition & 2 deletions oracle/src/sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,12 @@ where
#[cfg(feature = "ocaml_types")]
pub mod caml {
use super::*;
use ocaml_gen::OcamlGen;

//
// ScalarChallenge<F> <-> CamlScalarChallenge<CamlF>
//

#[derive(Debug, Clone, ocaml::IntoValue, ocaml::FromValue, OcamlGen)]
#[derive(Debug, Clone, ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlScalarChallenge<CamlF>(pub CamlF);

impl<F, CamlF> From<ScalarChallenge<F>> for CamlScalarChallenge<CamlF>
Expand Down