leanprover / leanprover/lean4

Pass signed ints to and from the C FFI

Open
#10,561 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P-medium
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Currently, Lean can only send unsigned integers

While working on the SDL3 bindings for Lean, I found something that was particularly annoying

https://github.com/ValorZard/lean-sdl3/blob/22c92150b622592cff10214ed5cb8b2001dec1aa/c/sdl.c#L322

lean_obj_res sdl_get_texture_width(lean_object * g_texture, lean_obj_arg w) {
    SDL_Texture* texture = (SDL_Texture*)lean_get_external_data(g_texture);

    SDL_PropertiesID messageTexProps = SDL_GetTextureProperties(texture);

    // get number property always returns a signed 64-bit integer
    int64_t width = SDL_GetNumberProperty(messageTexProps, SDL_PROP_TEXTURE_WIDTH_NUMBER, 0);
    
    // however, there seems to be no way to return int64_t directly to Lean
    // so we box it as uint64_t instead
    // TODO: figure out a way to return int64_t directly
    return lean_io_result_mk_ok(lean_box_uint64(width));
}

This is using this specific function: https://wiki.libsdl.org/SDL3/SDL_GetNumberProperty

and then you cast it back to a signed int
https://github.com/ValorZard/lean-sdl3/blob/22c92150b622592cff10214ed5cb8b2001dec1aa/SDL.lean#L120

@[extern "sdl_get_texture_width"]
opaque getTextureWidth (texture : @& SDLTexture) : SDLIO Int64

This ... does not feel good. Is there a way for Lean to support signed integers natively?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The limitation is reproduced by c/sdl.c and SDL.lean in the linked SDL3 binding. Start by tracing Lean's existing unsigned-integer C FFI handling and determine how signed integers should be represented; done means SDL_GetNumberProperty can return an Int64 without boxing it as uint64_t and casting it back.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.