Zulip Chat Archive

Stream: general

Topic: FFI - representing C structs in Lean


Greg Shuflin (Dec 18 2025 at 02:10):

I'm currently hacking on this SDL3 bindings library (https://github.com/ValorZard/lean-sdl3) that @Oliver Dressler and @Srayan Jana have contributed to, trying to understand how it works and expand its functionality. At the moment, I'm trying to get the SDL3 webcam bindings working.

I'm specifically trying to figure out how to best represent C SDL structures as types in Lean. I can see that the existing bindings are doing something like:

private opaque SDLWindow.nonemptyType : NonemptyType
def SDLWindow : Type := SDLWindow.nonemptyType.type
instance SDLWindow.instNonempty : Nonempty SDLWindow := SDLWindow.nonemptyType.property

to create a Lean equivalent of a SDL_Window structure. But I'm not sure how to create a Lean structure that has fields equivalent to a C structure, and I don't see any examples of this in these existing bindings. I specifically want to be able to represent a SDL_CameraSpec in Lean as a Lean structure and get access to the various C integer fields on it.

Mac Malone (Dec 18 2025 at 02:39):

If you want the C structure to also appear in the formalization, below is a brief example of how that could be done. In the example, SDL types are represented by Lean external objects with the assumption that the proper external classes are registered in the C code (e.g., g_pixelformat_external_class).

In general, I should probably explain the example a bit more, but I do not have the time at the present.

Example Code

Greg Shuflin (Dec 18 2025 at 10:51):

Mac Malone said:

If you want the C structure to also appear in the formalization, below is a brief example of how that could be done. In the example, SDL types are represented by Lean external objects with the assumption that the proper external classes are registered in the C code (e.g., g_pixelformat_external_class).

In general, I should probably explain the example a bit more, but I do not have the time at the present.

Example Code


yeah I think I need a bit more explanation to make sense of this, either that or this block of code as such doesn't work and I need to modify it some.


Last updated: Dec 20 2025 at 21:32 UTC