Skip to content

feat: LeanCtorLayout trait and lean_inductive! macro#8

Merged
samuelburnham merged 17 commits intomainfrom
lean-ctor
Apr 16, 2026
Merged

feat: LeanCtorLayout trait and lean_inductive! macro#8
samuelburnham merged 17 commits intomainfrom
lean-ctor

Conversation

@samuelburnham
Copy link
Copy Markdown
Member

@samuelburnham samuelburnham commented Apr 10, 2026

Adds a LeanCtorLayout trait for inductives and structures with a LAYOUTS array that contains the number of objects, usizes, and scalar types (e.g. u64 or bool) for each inductive variant. The LAYOUTS index is the tag/variant index of the inductive:

pub struct SingleCtorLayout {
    pub num_obj: usize,
    pub num_usize: usize,
    pub num_64: usize,
    pub num_32: usize,
    pub num_16: usize,
    pub num_8: usize,
}
pub trait LeanCtorLayout {
    const LAYOUTS: &'static [SingleCtorLayout];
}

This allows the user to implement the trait as follows:

// In Lean:
//structure Pair where
//  fst : String
//  snd : bool

impl<R: LeanRef> LeanCtorLayout for LeanPair<R> {
    const LAYOUTS: &'static [SingleCtorLayout] = &[
        SingleCtorLayout { num_obj: 1, num_8: 1, ..SingleCtorLayout::ZERO },
    ];
}

Or use the lean_inductive! convenience macro, which also calls lean_domain_type! to create the LeanPair wrapper type and generates getters and setters for every type in SingleCtorLayout:

lean_inductive! {
    // Strings are stored as objects and booleans are stored as uint8_t in C
    LeanPair [ { num_obj: 1, num_8: 1 } ]  
}

Then the user can write:

let p = LeanPair::alloc(0); // Ctor tag is 0 for structures
p.set_obj(0, LeanString::new("hello")); // 0 is the object index 
p.set_num_8(0, true as u8); // 0 is the 8-bit type index (bool in this case)

Users still have to know how many fields of a given size are in the Lean structure and the index of each one they want to access, but it's better than having to manually compute byte offsets for scalar values.

@samuelburnham samuelburnham marked this pull request as ready for review April 14, 2026 15:28
Copy link
Copy Markdown
Member

@arthurpaulino arthurpaulino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first review pass focuses mainly on booleans, given the recent changes that happened on ix.

Comment thread src/object.rs
Comment thread src/object.rs Outdated
arthurpaulino
arthurpaulino previously approved these changes Apr 14, 2026
Comment thread src/object.rs Outdated
Copy link
Copy Markdown
Member

@arthurpaulino arthurpaulino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!
I'll wait for argumentcomputer/ix#380 to be updated with this new approach

Comment thread src/object.rs Outdated
@samuelburnham samuelburnham changed the title feat: LeanCtorScalar trait for easier field access feat: LeanCtorLayout trait and lean_inductive! macro Apr 16, 2026
Copy link
Copy Markdown
Member

@arthurpaulino arthurpaulino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@samuelburnham samuelburnham merged commit 345b210 into main Apr 16, 2026
10 checks passed
@samuelburnham samuelburnham deleted the lean-ctor branch April 16, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants