flintlib / python-flint

Python bindings for Flint and Arb
MIT License
132 stars 27 forks source link

Automatically generate `fmpz_*` pxds from FLINT headers #215

Closed oscarbenjamin closed 2 months ago

oscarbenjamin commented 2 months ago

This is a start towards having fully automatic updating of the flintlib .pxd files.

I have made a few improvements to the bin/rst_to_pxd.py script so that it can generate the files fully without editing by commenting out functions with unrecognised types.

I also added a script bin/all_rst_to_pxd.sh like:

#!/usr/bin/env bash

FLINT_DOC_DIR=$1

set -e

modules="\
    fmpz\
    fmpz_factor\
    fmpz_poly\
    fmpz_poly_factor\
    fmpz_mat\
    fmpz_lll\
    "

for module in $modules; do
    echo "Processing $module"
    bin/rst_to_pxd.py flint/$module --flint-doc-dir=$FLINT_DOC_DIR > src/flint/flintlib/$module.pxd
done

The idea would be for that modules list to end up having all of the FLINT modules and generating all of the .pxd files.

The script only extracts functions from the docs but not structs or macros. I moved one macro to flint.pxd but otherwise I factored out the structs from these files into a flint_types.pxd. The structs still need to be edited manually but we should organise them according to the way they are in the flint headers:

$ ls ../flint/src/*type*
../flint/src/acb_types.h   ../flint/src/fmpz_mod_types.h  ../flint/src/limb_types.h
../flint/src/acf_types.h   ../flint/src/fmpz_types.h      ../flint/src/mpoly_types.h
../flint/src/arb_types.h   ../flint/src/fq_nmod_types.h   ../flint/src/nmod_types.h
../flint/src/arf_types.h   ../flint/src/fq_types.h        ../flint/src/n_poly_types.h
../flint/src/ca_types.h    ../flint/src/fq_zech_types.h   ../flint/src/padic_types.h
../flint/src/fmpq_types.h  ../flint/src/gr_types.h

I checked the structs against the FLINT code and updating some things like long -> slong or mp_limb -> ulong.

There was one function missing from the docs fmpz_factor_expand which I manually added to flint_types.pxd.

Running the script like this has also update the types and names in a bunch of function definitions.

We should do this for all of the auto-generated files so that they can be kept up to date in a single command and then we can just add the entire lot in one go.

See also https://github.com/flintlib/python-flint/issues/54 which discusses auto-generating cython bindings for every Flint function.